Store the private keys and paths for all the accounts

This commit is contained in:
IshaVenikar 2024-02-20 10:33:49 +05:30
parent 4aa442b5b1
commit 7a3ad2823d

View File

@ -28,19 +28,24 @@ const createWallet = async (): Promise<WalletDetails> => {
const ethAddress = ethNode.address; const ethAddress = ethNode.address;
const cosmosAddress = (await getCosmosAccounts(mnemonic, 0)).data.address; const cosmosAddress = (await getCosmosAccounts(mnemonic, 0)).data.address;
const ethAccountInfo = `${0},${ethNode.privateKey}`;
const cosmosAccountInfo = `${0},${cosmosNode.privateKey}`;
// Store HD Id and private key of accounts created using add account
await setInternetCredentials( await setInternetCredentials(
'eth:keyServer:0', 'eth:keyServer:0',
'eth:key:0', 'eth:keyPath:0',
ethNode.privateKey, ethAccountInfo,
); );
await setInternetCredentials( await setInternetCredentials(
'cosmos:keyServer:0', 'cosmos:keyServer:0',
'cosmos:key:0', 'cosmos:keyPath:0',
cosmosNode.privateKey, cosmosAccountInfo,
); );
// Counter to keep track of add account index
await setInternetCredentials('eth:accountIndices', 'ethAccount', '0'); await setInternetCredentials('eth:accountIndices', 'ethAccount', '0');
await setInternetCredentials('cosmos:accountIndices', 'cosmosAccount', '0'); await setInternetCredentials('cosmos:accountIndices', 'cosmosAccount', '0');
// Global counter // Global counter
await setInternetCredentials('eth:globalCounter', 'ethCounter', '0'); await setInternetCredentials('eth:globalCounter', 'ethCounter', '0');
await setInternetCredentials('cosmos:globalCounter', 'cosmosCounter', '0'); await setInternetCredentials('cosmos:globalCounter', 'cosmosCounter', '0');
@ -105,12 +110,6 @@ const addAccount = async (network: string): Promise<Account | undefined> => {
throw new Error('Invalid wallet type'); throw new Error('Invalid wallet type');
} }
await setInternetCredentials(
`${network}:keyServer:${id}`,
`${network}:key:${id}`,
privKey,
);
let indices = idStore.password; let indices = idStore.password;
indices += `,${id.toString()}`; indices += `,${id.toString()}`;
@ -141,7 +140,14 @@ const addAccount = async (network: string): Promise<Account | undefined> => {
accountCounter, accountCounter,
); );
return { pubKey, address, id, hdPath: hdPath }; // Store the path and private key against the global counter - while fetching accounts in UI, this counter will be used
await setInternetCredentials(
`${network}:keyServer:${counterId}`,
`${network}:key:${counterId}`,
`${id}${privKey}`,
);
return { counterId, pubKey, address, hdPath };
} catch (error) { } catch (error) {
console.error('Error creating account:', error); console.error('Error creating account:', error);
} }
@ -156,7 +162,7 @@ const createAccountFromHDPath = async (
throw new Error('Error while creating account'); throw new Error('Error while creating account');
} }
const { privKey, pubKey, address } = account; const { privKey, pubKey, address, network } = account;
const parts = hdPath.split('/'); const parts = hdPath.split('/');
const id = parts[5]; const id = parts[5];
@ -165,22 +171,18 @@ const createAccountFromHDPath = async (
const accountInfo = `${path},${privKey}`; const accountInfo = `${path},${privKey}`;
let network = null;
switch (coinType) { switch (coinType) {
case '60': case "60'":
network = 'eth';
await setInternetCredentials( await setInternetCredentials(
`Eth:keyServer:${id}`, `eth:keyServer:${id}`,
`Eth:key:${id}`, `eth:key:${id}`,
accountInfo, accountInfo,
); );
break; break;
case '118': case "118'":
network = 'cosmos';
await setInternetCredentials( await setInternetCredentials(
`Cosmos:keyServer${id}`, `cosmos:keyServer${id}`,
`Cosmos:key:${id}`, `cosmos:key:${id}`,
accountInfo, accountInfo,
); );
break; break;
@ -189,6 +191,7 @@ const createAccountFromHDPath = async (
const counterStore = await getInternetCredentials( const counterStore = await getInternetCredentials(
`${network}:globalCounter`, `${network}:globalCounter`,
); );
if (!counterStore) { if (!counterStore) {
throw new Error('Error while fetching counter'); throw new Error('Error while fetching counter');
} }
@ -215,7 +218,8 @@ const createAccountFromHDPath = async (
const accountInfoFromHDPath = async ( const accountInfoFromHDPath = async (
hdPath: string, hdPath: string,
): Promise< ): Promise<
{ privKey: string; pubKey: string; address: string } | undefined | { privKey: string; pubKey: string; address: string; network: string }
| undefined
> => { > => {
const mnemonicStore = await getInternetCredentials('mnemonicServer'); const mnemonicStore = await getInternetCredentials('mnemonicServer');
if (!mnemonicStore) { if (!mnemonicStore) {
@ -228,9 +232,27 @@ const accountInfoFromHDPath = async (
const privKey = node.privateKey; const privKey = node.privateKey;
const pubKey = node.publicKey; const pubKey = node.publicKey;
const address = node.address;
return { privKey, pubKey, address }; const parts = hdPath.split('/');
const id = parseInt(parts[5]);
const coinType = parts[2];
let network: string;
let address: string;
switch (coinType) {
case "60'":
network = 'eth';
address = node.address;
break;
case "118'":
network = 'cosmos';
address = (await getCosmosAccounts(mnemonic, id)).data.address;
break;
default:
throw new Error('Invalid wallet type');
}
return { privKey, pubKey, address, network };
}; };
const signMessage = async ({ const signMessage = async ({