forked from cerc-io/laconic-wallet
* Sign message using signDirect method with cosmos accounts * Add explaination for signDirect method * Use existing utility function to convert hex string to uint8array * Handle review changes
112 lines
2.8 KiB
TypeScript
112 lines
2.8 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 { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
|
|
|
|
import { SignMessageParams } from '../types';
|
|
import {
|
|
getCosmosAccounts,
|
|
getDirectWallet,
|
|
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);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
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);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
const signDirectMessage = async (
|
|
network: string,
|
|
accountId: number,
|
|
signDoc: SignDoc,
|
|
): Promise<string | undefined> => {
|
|
try {
|
|
const path = (await getPathKey(network, accountId)).path;
|
|
const mnemonic = await getMnemonic();
|
|
const { directWallet, data } = await getDirectWallet(mnemonic, path);
|
|
|
|
const directSignature = await directWallet.signDirect(
|
|
data.address,
|
|
signDoc,
|
|
);
|
|
|
|
return directSignature.signature.signature;
|
|
} catch (error) {
|
|
console.error('Error signing Cosmos message:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export { signMessage, signEthMessage, signCosmosMessage, signDirectMessage };
|