laconic-wallet/utils.ts
Adwait Gharpure 9ab3148aa9
Show account data specific to selected network (#11)
* Add state for selected network

* Make review changes

* Add cosmos signature

* Explicit check for cosmos

* Add dummy method for generating wallet

* Remove logic from component

* Add dummy sign method

* Change network state values

* Use separate file for types

* Add default case to switch

* Use consistent method names

---------

Co-authored-by: Adw8 <adwait@deepstacksoft.com>
2024-02-14 13:45:02 +05:30

83 lines
2.5 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 { utils } from 'ethers';
import { HDNode } from 'ethers/lib/utils';
import { Alert } from 'react-native';
import {
setInternetCredentials,
resetInternetCredentials,
} from 'react-native-keychain';
import { Account, WalletDetails } from './types';
const generateEthNode = async (): Promise<HDNode | undefined> => {
try {
const mnemonic = utils.entropyToMnemonic(utils.randomBytes(32));
const hdNode = HDNode.fromMnemonic(mnemonic);
const ethNode = hdNode.derivePath("m/44'/60'/0'/0/0");
await setInternetCredentials('keyServer', 'key', ethNode.privateKey);
await setInternetCredentials('mnemonicServer', 'mnemonic', mnemonic);
return ethNode;
} catch (error) {
console.error('Error creating wallet ', error);
}
};
const createWallet = (): WalletDetails => {
try {
const ethAccount = {
address: '0x873784c8A011A32C7635C8d8D3D2c83060532A49',
publicKey:
'0x02fd66d3487eb0567c321dac48b5e17c469df8ede7c0c79b74a9d0492249b32f1e',
};
const cosmosAccount = {
address: 'cosmos1sulk9q5fmagur6m3pctmcnfeeku25gp2ectt75',
publicKey:
'cosmospub1addwnpepqt9d597c5f6zqqyxy3msrstyc7zl3vyvrl5ku02r4ueuwt5vusw4gmt70dd',
};
return { ethAccount, cosmosAccount };
} catch (error) {
console.error('Error creating wallet ', error);
throw error;
}
};
const signMessage = async (network: string, index: Number, message: string) => {
try {
let signature: string | false;
switch (network) {
case 'eth':
signature =
'0x43jv95d5a9704z83h85d52ecee5c14bf6637fa2f95653e8499eac4e8285f37b2d9f446c027cac56f3b7840d1b3879ea943415190d7a358cdb3ee05451cdcf7c1c';
break;
case 'cosmos':
signature =
'0x56da25d5a9704e0cd685d52ecee5c14bf6637fa2f95653e8499eac4e8285f37b2d9f446c027cac56f3b7840d1b3879ea943415190d7a358cdb3ee05451cdcf7c1c';
break;
default:
signature = '';
}
Alert.alert('Message signature: ', signature as string);
} catch (error) {
console.error('Error signing transaction ', error);
}
};
const resetWallet = async () => {
await resetInternetCredentials('keyServer');
await resetInternetCredentials('mnemonicServer');
};
export { createWallet, signMessage, resetWallet };