forked from cerc-io/laconic-wallet
* Generate ethers wallet * Store wallet private key and mnemonic using react-native-keychain * Add semicolons * Edit ui heading * Make review changes * Use consistent pattern for functions * Add method to sign message * Change title * Improve signing time * Hide button on wallet initilisation --------- Co-authored-by: Adw8 <adwait@deepstacksoft.com>
35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import '@ethersproject/shims';
|
|
|
|
import { Wallet, utils } from 'ethers';
|
|
import { HDNode } from 'ethers/lib/utils';
|
|
import { Alert } from 'react-native';
|
|
import { getGenericPassword, setGenericPassword } from 'react-native-keychain';
|
|
|
|
const generateWallet = async () => {
|
|
try {
|
|
const mnemonic = utils.entropyToMnemonic(utils.randomBytes(32));
|
|
const hdNode = HDNode.fromMnemonic(mnemonic);
|
|
await setGenericPassword(mnemonic, hdNode.privateKey);
|
|
Alert.alert('Wallet stored successfully with address:', hdNode.address);
|
|
} catch (error) {
|
|
console.error('Error creating wallet ', error);
|
|
}
|
|
};
|
|
|
|
const signMessage = async (message: string) => {
|
|
try {
|
|
const creds = await getGenericPassword();
|
|
const wallet = creds && new Wallet(creds.password);
|
|
const signature = wallet && (await wallet.signMessage(message));
|
|
if (typeof signature === 'string') {
|
|
Alert.alert('Message signed successfully with signature', signature);
|
|
} else {
|
|
Alert.alert('Message signing failed. Please try again.');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error signing transaction ', error);
|
|
}
|
|
};
|
|
|
|
export { generateWallet, signMessage };
|