Add a global counter for eth and cosmos

This commit is contained in:
IshaVenikar 2024-02-19 19:10:21 +05:30
parent 25f9243d85
commit a3c434178b

View File

@ -41,6 +41,9 @@ const createWallet = async (): Promise<WalletDetails> => {
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');
const ethAccounts = {
id: 0,
@ -108,23 +111,37 @@ const addAccount = async (network: string): Promise<Account | undefined> => {
privKey,
);
const accountIndicesKey = `${network}:accountIndices`;
const accountIndices = await getInternetCredentials(accountIndicesKey);
if (!accountIndices) {
throw new Error('Account not found!');
}
let indices = accountIndices.password;
let indices = idStore.password;
indices += `,${id.toString()}`;
await resetInternetCredentials(accountIndicesKey);
await resetInternetCredentials(`${network}:accountIndices`);
await setInternetCredentials(
accountIndicesKey,
`${network}:accountIndices`,
`${network}Account`,
indices,
);
return { id, pubKey, address, hdPath };
const counterStore = await getInternetCredentials(
`${network}:globalCounter`,
);
if (!counterStore) {
throw new Error('Error while fetching counter');
}
let accountCounter = counterStore.password;
const cIds = accountCounter.split(',').map(Number);
const counterId = cIds[cIds.length - 1] + 1;
accountCounter += `,${counterId.toString()}`;
await resetInternetCredentials(`${network}:globalCounter`);
await setInternetCredentials(
`${network}:globalCounter`,
`${network}Counter`,
accountCounter,
);
return { pubKey, address, id, hdPath: hdPath };
} catch (error) {
console.error('Error creating account:', error);
}
@ -135,7 +152,9 @@ const createAccountFromHDPath = async (
): Promise<{ pubKey: string; address: string } | undefined> => {
try {
const account = await accountInfoFromHDPath(hdPath);
if (!account) throw new Error('Error while creating account');
if (!account) {
throw new Error('Error while creating account');
}
const { privKey, pubKey, address } = account;
@ -146,8 +165,11 @@ const createAccountFromHDPath = async (
const accountInfo = `${path},${privKey}`;
let network = null;
switch (coinType) {
case '60':
network = 'eth';
await setInternetCredentials(
`Eth:keyServer:${id}`,
`Eth:key:${id}`,
@ -155,6 +177,7 @@ const createAccountFromHDPath = async (
);
break;
case '118':
network = 'cosmos';
await setInternetCredentials(
`Cosmos:keyServer${id}`,
`Cosmos:key:${id}`,
@ -163,9 +186,29 @@ const createAccountFromHDPath = async (
break;
}
const counterStore = await getInternetCredentials(
`${network}:globalCounter`,
);
if (!counterStore) {
throw new Error('Error while fetching counter');
}
let accountCounter = counterStore.password;
const cIds = accountCounter.split(',').map(Number);
const counterId = cIds[cIds.length - 1] + 1;
accountCounter += `,${counterId.toString()}`;
await resetInternetCredentials(`${network}:globalCounter`);
await setInternetCredentials(
`${network}:globalCounter`,
`${network}Counter`,
accountCounter,
);
return { pubKey, address };
} catch (error) {
console.error('Error creating account:', error);
console.error(error);
return undefined;
}
};
@ -175,23 +218,20 @@ const accountInfoFromHDPath = async (
): Promise<
{ privKey: string; pubKey: string; address: string } | undefined
> => {
try {
const mnemonicStore = await getInternetCredentials('mnemonicServer');
if (!mnemonicStore) throw new Error('Mnemonic not found!');
const mnemonic = mnemonicStore.password;
const hdNode = HDNode.fromMnemonic(mnemonic);
const node = hdNode.derivePath(hdPath);
const privKey = node.privateKey;
const pubKey = node.publicKey;
const address = node.address;
return { privKey, pubKey, address };
} catch (error) {
console.error('Error creating account:', error);
return undefined;
const mnemonicStore = await getInternetCredentials('mnemonicServer');
if (!mnemonicStore) {
throw new Error('Mnemonic not found!');
}
const mnemonic = mnemonicStore.password;
const hdNode = HDNode.fromMnemonic(mnemonic);
const node = hdNode.derivePath(hdPath);
const privKey = node.privateKey;
const pubKey = node.publicKey;
const address = node.address;
return { privKey, pubKey, address };
};
const signMessage = async ({