forked from cerc-io/laconic-wallet
* Display HD path on sign message page * Create component for displaying account details * Add retrieve accounts function * Load accounts after closing app * Fix the retrieve accounts function * Use hdpath instead of id * Check if keystore is empty while retrieving accounts * Add spinner when accounts are being fetched * Display complete hd paths after reloading the app * Remove any return type * Store public key and address * Modify sign message function to use path * Fix the add accounts functionality
83 lines
2.1 KiB
TypeScript
83 lines
2.1 KiB
TypeScript
/* Importing this library provides react native with a secure random source.
|
|
For more information, "visit https://docs.ethers.org/v5/cookbook/react-native/#cookbook-reactnative-security" */
|
|
import 'react-native-get-random-values';
|
|
import '@ethersproject/shims';
|
|
|
|
import { Wallet } from 'ethers';
|
|
|
|
import { SignMessageParams } from '../types';
|
|
import { getCosmosAccounts, getMnemonic, getPathKey } from './utils';
|
|
|
|
const signMessage = async ({
|
|
message,
|
|
network,
|
|
accountId,
|
|
}: SignMessageParams): Promise<string | undefined> => {
|
|
const path = (await getPathKey(network, accountId)).path;
|
|
|
|
switch (network) {
|
|
case 'eth':
|
|
return await signEthMessage(message, accountId);
|
|
case 'cosmos':
|
|
return await signCosmosMessage(message, path);
|
|
default:
|
|
throw new Error('Invalid wallet type');
|
|
}
|
|
};
|
|
|
|
const signEthMessage = async (
|
|
message: string,
|
|
accountId: number,
|
|
): Promise<string | undefined> => {
|
|
try {
|
|
const privKey = (await getPathKey('eth', accountId)).privKey;
|
|
const wallet = new Wallet(privKey);
|
|
const signature = await wallet.signMessage(message);
|
|
|
|
return signature;
|
|
} catch (error) {
|
|
console.error('Error signing Ethereum message:', error);
|
|
return undefined;
|
|
}
|
|
};
|
|
|
|
const signCosmosMessage = async (
|
|
message: string,
|
|
path: string,
|
|
): Promise<string | undefined> => {
|
|
try {
|
|
const mnemonic = await getMnemonic();
|
|
const cosmosAccount = await getCosmosAccounts(mnemonic, path);
|
|
const address = cosmosAccount.data.address;
|
|
const cosmosSignature = await cosmosAccount.cosmosWallet.signAmino(
|
|
address,
|
|
{
|
|
chain_id: '',
|
|
account_number: '0',
|
|
sequence: '0',
|
|
fee: {
|
|
gas: '0',
|
|
amount: [],
|
|
},
|
|
msgs: [
|
|
{
|
|
type: 'sign/MsgSignData',
|
|
value: {
|
|
signer: address,
|
|
data: btoa(message),
|
|
},
|
|
},
|
|
],
|
|
memo: '',
|
|
},
|
|
);
|
|
|
|
return cosmosSignature.signature.signature;
|
|
} catch (error) {
|
|
console.error('Error signing Cosmos message:', error);
|
|
return undefined;
|
|
}
|
|
};
|
|
|
|
export { signMessage, signEthMessage, signCosmosMessage };
|