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 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(
'eth:keyServer:0',
'eth:key:0',
ethNode.privateKey,
'eth:keyPath:0',
ethAccountInfo,
);
await setInternetCredentials(
'cosmos:keyServer:0',
'cosmos:key:0',
cosmosNode.privateKey,
'cosmos:keyPath:0',
cosmosAccountInfo,
);
// Counter to keep track of add account index
await setInternetCredentials('eth:accountIndices', 'ethAccount', '0');
await setInternetCredentials('cosmos:accountIndices', 'cosmosAccount', '0');
// Global counter
await setInternetCredentials('eth:globalCounter', 'ethCounter', '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');
}
await setInternetCredentials(
`${network}:keyServer:${id}`,
`${network}:key:${id}`,
privKey,
);
let indices = idStore.password;
indices += `,${id.toString()}`;
@ -141,7 +140,14 @@ const addAccount = async (network: string): Promise<Account | undefined> => {
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) {
console.error('Error creating account:', error);
}
@ -156,7 +162,7 @@ const createAccountFromHDPath = async (
throw new Error('Error while creating account');
}
const { privKey, pubKey, address } = account;
const { privKey, pubKey, address, network } = account;
const parts = hdPath.split('/');
const id = parts[5];
@ -165,22 +171,18 @@ const createAccountFromHDPath = async (
const accountInfo = `${path},${privKey}`;
let network = null;
switch (coinType) {
case '60':
network = 'eth';
case "60'":
await setInternetCredentials(
`Eth:keyServer:${id}`,
`Eth:key:${id}`,
`eth:keyServer:${id}`,
`eth:key:${id}`,
accountInfo,
);
break;
case '118':
network = 'cosmos';
case "118'":
await setInternetCredentials(
`Cosmos:keyServer${id}`,
`Cosmos:key:${id}`,
`cosmos:keyServer${id}`,
`cosmos:key:${id}`,
accountInfo,
);
break;
@ -189,6 +191,7 @@ const createAccountFromHDPath = async (
const counterStore = await getInternetCredentials(
`${network}:globalCounter`,
);
if (!counterStore) {
throw new Error('Error while fetching counter');
}
@ -215,7 +218,8 @@ const createAccountFromHDPath = async (
const accountInfoFromHDPath = async (
hdPath: string,
): Promise<
{ privKey: string; pubKey: string; address: string } | undefined
| { privKey: string; pubKey: string; address: string; network: string }
| undefined
> => {
const mnemonicStore = await getInternetCredentials('mnemonicServer');
if (!mnemonicStore) {
@ -228,9 +232,27 @@ const accountInfoFromHDPath = async (
const privKey = node.privateKey;
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 ({