Skip to content

Commit

Permalink
feature(key-store): Allow to add accounts with custom derivation in W…
Browse files Browse the repository at this point in the history
…eb (#4236)

* feature(key-store): Allow to add accounts with custom derivation

* feature(key-store): Small refactoring

* feature(key-store): Fix imports
  • Loading branch information
satoshiotomakan authored Jan 27, 2025
1 parent 8b6c042 commit 9efc9bb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
16 changes: 13 additions & 3 deletions wasm/src/keystore/default-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Copyright © 2017 Trust Wallet.

import {WalletCore, CoinType, PrivateKey, StoredKey, StoredKeyEncryption} from "../wallet-core";
import {WalletCore, CoinType, Derivation, PrivateKey, StoredKey, StoredKeyEncryption} from "../wallet-core";
import * as Types from "./types";

export class Default implements Types.IKeyStore {
Expand Down Expand Up @@ -109,11 +109,21 @@ export class Default implements Types.IKeyStore {
password: string,
coins: CoinType[]
): Promise<Types.Wallet> {
const { Derivation } = this.core;

let coins_with_derivations = coins.map(coin => ({
coin: coin,
derivation: Derivation.default,
}));
return this.addAccountsWithDerivations(id, password, coins_with_derivations);
}

addAccountsWithDerivations(id: string, password: string, coins: Types.CoinWithDerivation[]): Promise<Types.Wallet> {
return this.load(id).then((wallet) => {
let storedKey = this.mapStoredKey(wallet);
let hdWallet = storedKey.wallet(Buffer.from(password));
coins.forEach((coin) => {
storedKey.accountForCoin(coin, hdWallet);
coins.forEach((item) => {
storedKey.accountForCoinDerivation(item.coin, item.derivation, hdWallet);
});
let newWallet = this.mapWallet(storedKey);
storedKey.delete();
Expand Down
10 changes: 9 additions & 1 deletion wasm/src/keystore/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Copyright © 2017 Trust Wallet.

import { CoinType, PrivateKey, StoredKeyEncryption } from "../wallet-core";
import { CoinType, Derivation, PrivateKey, StoredKeyEncryption } from "../wallet-core";

export enum WalletType {
Mnemonic = "mnemonic",
Expand Down Expand Up @@ -37,6 +37,11 @@ export interface Wallet {
activeAccounts: ActiveAccount[];
}

export interface CoinWithDerivation {
coin: CoinType,
derivation: Derivation,
}

export interface IKeyStore {
// Check if wallet id exists
hasWallet(id: string): Promise<boolean>;
Expand Down Expand Up @@ -71,6 +76,9 @@ export interface IKeyStore {
// Add active accounts to a wallet by wallet id, password, coin
addAccounts(id: string, password: string, coins: CoinType[]): Promise<Wallet>;

// Add active accounts paired with corresponding derivations to a wallet by wallet id, password, coin.
addAccountsWithDerivations(id: string, password: string, coins: CoinWithDerivation[]): Promise<Wallet>;

// Get private key of an account by wallet id, password, coin and derivation path
getKey(
id: string,
Expand Down
12 changes: 10 additions & 2 deletions wasm/tests/KeyStore+extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe("KeyStore", async () => {
}).timeout(10000);

it("test ExtensionStorage AES256", async () => {
const { CoinType, HexCoding, StoredKeyEncryption } = globalThis.core;
const { CoinType, Derivation, HexCoding, StoredKeyEncryption } = globalThis.core;
const mnemonic = globalThis.mnemonic as string;
const password = globalThis.password as string;

Expand All @@ -84,7 +84,7 @@ describe("KeyStore", async () => {
assert.equal(wallet.type, "mnemonic");
assert.equal(wallet.version, 3);

const account = wallet.activeAccounts[0];
var account = wallet.activeAccounts[0];
const key = await keystore.getKey(wallet.id, password, account);

assert.equal(
Expand All @@ -110,6 +110,14 @@ describe("KeyStore", async () => {
assert.isTrue(await keystore.hasWallet(wallet.id));
assert.isFalse(await keystore.hasWallet("invalid-id"));

wallet = await keystore.addAccountsWithDerivations(wallet.id, password, [{
coin: CoinType.solana,
derivation: Derivation.solanaSolana,
}]);
assert.equal(wallet.activeAccounts.length, 4);
account = wallet.activeAccounts[3];
assert.equal(account.address, "CgWJeEWkiYqosy1ba7a3wn9HAQuHyK48xs3LM4SSDc1C");

const exported = await keystore.export(wallet.id, password);
assert.equal(exported, mnemonic);

Expand Down

0 comments on commit 9efc9bb

Please sign in to comment.