laconic-wallet/utils.ts
shreerang6921 5dd97e3860
Generate HD wallet and sign message (#3)
* 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>
2024-02-08 12:05:32 +05:30

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 };