forked from cerc-io/laconic-wallet
Make review changes
This commit is contained in:
parent
7a3ad2823d
commit
68eec9cc1a
2
types.ts
2
types.ts
@ -41,7 +41,7 @@ export type AccountsState = {
|
|||||||
export type SignMessageParams = {
|
export type SignMessageParams = {
|
||||||
message: string;
|
message: string;
|
||||||
network: string;
|
network: string;
|
||||||
accountId: number;
|
accountId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CreateWalletProps = {
|
export type CreateWalletProps = {
|
||||||
|
78
utils.ts
78
utils.ts
@ -26,12 +26,12 @@ const createWallet = async (): Promise<WalletDetails> => {
|
|||||||
const cosmosNode = hdNode.derivePath("m/44'/118'/0'/0/0");
|
const cosmosNode = hdNode.derivePath("m/44'/118'/0'/0/0");
|
||||||
|
|
||||||
const ethAddress = ethNode.address;
|
const ethAddress = ethNode.address;
|
||||||
const cosmosAddress = (await getCosmosAccounts(mnemonic, 0)).data.address;
|
const cosmosAddress = (await getCosmosAccounts(mnemonic, `/0'/0/0`)).data
|
||||||
|
.address;
|
||||||
|
|
||||||
const ethAccountInfo = `${0},${ethNode.privateKey}`;
|
const ethAccountInfo = `${0},${ethNode.privateKey}`;
|
||||||
const cosmosAccountInfo = `${0},${cosmosNode.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:keyPath:0',
|
'eth:keyPath:0',
|
||||||
@ -42,11 +42,10 @@ const createWallet = async (): Promise<WalletDetails> => {
|
|||||||
'cosmos:keyPath:0',
|
'cosmos:keyPath:0',
|
||||||
cosmosAccountInfo,
|
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
|
|
||||||
await setInternetCredentials('eth:globalCounter', 'ethCounter', '0');
|
await setInternetCredentials('eth:globalCounter', 'ethCounter', '0');
|
||||||
await setInternetCredentials('cosmos:globalCounter', 'cosmosCounter', '0');
|
await setInternetCredentials('cosmos:globalCounter', 'cosmosCounter', '0');
|
||||||
|
|
||||||
@ -104,7 +103,8 @@ const addAccount = async (network: string): Promise<Account | undefined> => {
|
|||||||
address = node.address;
|
address = node.address;
|
||||||
break;
|
break;
|
||||||
case 'cosmos':
|
case 'cosmos':
|
||||||
address = (await getCosmosAccounts(mnemonic, id)).data.address;
|
address = (await getCosmosAccounts(mnemonic, `0'/0/${id}`)).data
|
||||||
|
.address;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('Invalid wallet type');
|
throw new Error('Invalid wallet type');
|
||||||
@ -128,8 +128,8 @@ const addAccount = async (network: string): Promise<Account | undefined> => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let accountCounter = counterStore.password;
|
let accountCounter = counterStore.password;
|
||||||
const cIds = accountCounter.split(',').map(Number);
|
const counterIds = accountCounter.split(',').map(Number);
|
||||||
const counterId = cIds[cIds.length - 1] + 1;
|
const counterId = counterIds[counterIds.length - 1] + 1;
|
||||||
accountCounter += `,${counterId.toString()}`;
|
accountCounter += `,${counterId.toString()}`;
|
||||||
|
|
||||||
await resetInternetCredentials(`${network}:globalCounter`);
|
await resetInternetCredentials(`${network}:globalCounter`);
|
||||||
@ -140,7 +140,6 @@ const addAccount = async (network: string): Promise<Account | undefined> => {
|
|||||||
accountCounter,
|
accountCounter,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Store the path and private key against the global counter - while fetching accounts in UI, this counter will be used
|
|
||||||
await setInternetCredentials(
|
await setInternetCredentials(
|
||||||
`${network}:keyServer:${counterId}`,
|
`${network}:keyServer:${counterId}`,
|
||||||
`${network}:key:${counterId}`,
|
`${network}:key:${counterId}`,
|
||||||
@ -153,7 +152,7 @@ const addAccount = async (network: string): Promise<Account | undefined> => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const createAccountFromHDPath = async (
|
const addAccountFromHDPath = async (
|
||||||
hdPath: string,
|
hdPath: string,
|
||||||
): Promise<{ pubKey: string; address: string } | undefined> => {
|
): Promise<{ pubKey: string; address: string } | undefined> => {
|
||||||
try {
|
try {
|
||||||
@ -165,29 +164,9 @@ const createAccountFromHDPath = async (
|
|||||||
const { privKey, pubKey, address, network } = account;
|
const { privKey, pubKey, address, network } = account;
|
||||||
|
|
||||||
const parts = hdPath.split('/');
|
const parts = hdPath.split('/');
|
||||||
const id = parts[5];
|
|
||||||
const coinType = parts[2];
|
const coinType = parts[2];
|
||||||
const path = parts.slice(-3).join('/');
|
const path = parts.slice(-3).join('/');
|
||||||
|
|
||||||
const accountInfo = `${path},${privKey}`;
|
|
||||||
|
|
||||||
switch (coinType) {
|
|
||||||
case "60'":
|
|
||||||
await setInternetCredentials(
|
|
||||||
`eth:keyServer:${id}`,
|
|
||||||
`eth:key:${id}`,
|
|
||||||
accountInfo,
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case "118'":
|
|
||||||
await setInternetCredentials(
|
|
||||||
`cosmos:keyServer${id}`,
|
|
||||||
`cosmos:key:${id}`,
|
|
||||||
accountInfo,
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const counterStore = await getInternetCredentials(
|
const counterStore = await getInternetCredentials(
|
||||||
`${network}:globalCounter`,
|
`${network}:globalCounter`,
|
||||||
);
|
);
|
||||||
@ -197,8 +176,8 @@ const createAccountFromHDPath = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
let accountCounter = counterStore.password;
|
let accountCounter = counterStore.password;
|
||||||
const cIds = accountCounter.split(',').map(Number);
|
const counterIds = accountCounter.split(',').map(Number);
|
||||||
const counterId = cIds[cIds.length - 1] + 1;
|
const counterId = counterIds[counterIds.length - 1] + 1;
|
||||||
accountCounter += `,${counterId.toString()}`;
|
accountCounter += `,${counterId.toString()}`;
|
||||||
|
|
||||||
await resetInternetCredentials(`${network}:globalCounter`);
|
await resetInternetCredentials(`${network}:globalCounter`);
|
||||||
@ -209,6 +188,25 @@ const createAccountFromHDPath = async (
|
|||||||
accountCounter,
|
accountCounter,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const accountInfo = `${path},${privKey}`;
|
||||||
|
|
||||||
|
switch (coinType) {
|
||||||
|
case "60'":
|
||||||
|
await setInternetCredentials(
|
||||||
|
`eth:keyServer:${counterId}`,
|
||||||
|
`eth:key:${counterId}`,
|
||||||
|
accountInfo,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "118'":
|
||||||
|
await setInternetCredentials(
|
||||||
|
`cosmos:keyServer${counterId}`,
|
||||||
|
`cosmos:key:${counterId}`,
|
||||||
|
accountInfo,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return { pubKey, address };
|
return { pubKey, address };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
@ -234,8 +232,9 @@ const accountInfoFromHDPath = async (
|
|||||||
const pubKey = node.publicKey;
|
const pubKey = node.publicKey;
|
||||||
|
|
||||||
const parts = hdPath.split('/');
|
const parts = hdPath.split('/');
|
||||||
const id = parseInt(parts[5]);
|
const path = parts.slice(-3).join('/');
|
||||||
const coinType = parts[2];
|
const coinType = parts[2];
|
||||||
|
console.log(path);
|
||||||
|
|
||||||
let network: string;
|
let network: string;
|
||||||
let address: string;
|
let address: string;
|
||||||
@ -247,7 +246,7 @@ const accountInfoFromHDPath = async (
|
|||||||
break;
|
break;
|
||||||
case "118'":
|
case "118'":
|
||||||
network = 'cosmos';
|
network = 'cosmos';
|
||||||
address = (await getCosmosAccounts(mnemonic, id)).data.address;
|
address = (await getCosmosAccounts(mnemonic, path)).data.address;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error('Invalid wallet type');
|
throw new Error('Invalid wallet type');
|
||||||
@ -272,7 +271,7 @@ const signMessage = async ({
|
|||||||
|
|
||||||
const signEthMessage = async (
|
const signEthMessage = async (
|
||||||
message: string,
|
message: string,
|
||||||
id: number,
|
id: string,
|
||||||
): Promise<string | undefined> => {
|
): Promise<string | undefined> => {
|
||||||
try {
|
try {
|
||||||
const keyCred = await getInternetCredentials(`eth:keyServer:${id}`);
|
const keyCred = await getInternetCredentials(`eth:keyServer:${id}`);
|
||||||
@ -293,7 +292,7 @@ const signEthMessage = async (
|
|||||||
|
|
||||||
const signCosmosMessage = async (
|
const signCosmosMessage = async (
|
||||||
message: string,
|
message: string,
|
||||||
id: number,
|
id: string,
|
||||||
): Promise<string | undefined> => {
|
): Promise<string | undefined> => {
|
||||||
try {
|
try {
|
||||||
const mnemonicStore = await getInternetCredentials('mnemonicServer');
|
const mnemonicStore = await getInternetCredentials('mnemonicServer');
|
||||||
@ -336,10 +335,10 @@ const signCosmosMessage = async (
|
|||||||
|
|
||||||
const getCosmosAccounts = async (
|
const getCosmosAccounts = async (
|
||||||
mnemonic: string,
|
mnemonic: string,
|
||||||
id: number,
|
id: string,
|
||||||
): Promise<{ cosmosWallet: Secp256k1HdWallet; data: AccountData }> => {
|
): Promise<{ cosmosWallet: Secp256k1HdWallet; data: AccountData }> => {
|
||||||
const cosmosWallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, {
|
const cosmosWallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, {
|
||||||
hdPaths: [stringToPath(`m/44'/118'/0'/0/${id}`)],
|
hdPaths: [stringToPath(`m/44'/118'/${id}`)],
|
||||||
});
|
});
|
||||||
|
|
||||||
const accountsData = await cosmosWallet.getAccounts();
|
const accountsData = await cosmosWallet.getAccounts();
|
||||||
@ -372,6 +371,9 @@ const resetWallet = async () => {
|
|||||||
|
|
||||||
await resetInternetCredentials('eth:accountIndices');
|
await resetInternetCredentials('eth:accountIndices');
|
||||||
await resetInternetCredentials('cosmos:accountIndices');
|
await resetInternetCredentials('cosmos:accountIndices');
|
||||||
|
|
||||||
|
await resetInternetCredentials('eth:globalCounter');
|
||||||
|
await resetInternetCredentials('cosmos:globalCounter');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error resetting wallet:', error);
|
console.error('Error resetting wallet:', error);
|
||||||
throw error;
|
throw error;
|
||||||
@ -383,5 +385,5 @@ export {
|
|||||||
addAccount,
|
addAccount,
|
||||||
signMessage,
|
signMessage,
|
||||||
resetWallet,
|
resetWallet,
|
||||||
createAccountFromHDPath,
|
addAccountFromHDPath,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user