Skip to content

Commit 7a6e727

Browse files
authored
Allows extension to import a wallet with private key (#4297)
1 parent e384f67 commit 7a6e727

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

wasm/src/keystore/default-impl.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,9 @@ export class Default implements Types.IKeyStore {
139139
): Promise<PrivateKey> {
140140
return this.load(id).then((wallet) => {
141141
let storedKey = this.mapStoredKey(wallet);
142-
let hdWallet = storedKey.wallet(Buffer.from(password));
143142
let coin = (this.core.CoinType as any).values["" + account.coin];
144-
let privateKey = hdWallet.getKey(coin, account.derivationPath);
143+
let privateKey = storedKey.privateKey(coin, Buffer.from(password));
145144
storedKey.delete();
146-
hdWallet.delete();
147145
return privateKey;
148146
});
149147
}

wasm/src/keystore/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { CoinType, Derivation, PrivateKey, StoredKeyEncryption } from "../wallet
66

77
export enum WalletType {
88
Mnemonic = "mnemonic",
9-
PrivateKey = "privateKey",
9+
PrivateKey = "private-key",
1010
WatchOnly = "watchOnly",
1111
Hardware = "hardware",
1212
}

wasm/tests/KeyStore.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// Copyright © 2017 Trust Wallet.
4+
5+
import "mocha";
6+
import { assert } from "chai";
7+
import { Buffer } from "buffer";
8+
import { KeyStore } from "../dist";
9+
10+
describe("KeyStore", () => {
11+
it("test get key", async () => {
12+
const { CoinType, StoredKeyEncryption, HexCoding } = globalThis.core;
13+
const mnemonic = globalThis.mnemonic as string;
14+
const password = globalThis.password as string;
15+
16+
const storage = new KeyStore.FileSystemStorage("/tmp");
17+
const keystore = new KeyStore.Default(globalThis.core, storage);
18+
19+
var wallet = await keystore.import(mnemonic, "Coolw", password, [
20+
CoinType.bitcoin,
21+
], StoredKeyEncryption.aes128Ctr);
22+
23+
const account = wallet.activeAccounts[0];
24+
const key = await keystore.getKey(wallet.id, password, account);
25+
26+
assert.equal(
27+
HexCoding.encode(key.data()),
28+
"0xd2568511baea8dc347f14c4e0479eb8ebe29eb5f664ed796e755896250ffd11f"
29+
);
30+
31+
const inputPrivateKey = Buffer.from("9cdb5cab19aec3bd0fcd614c5f185e7a1d97634d4225730eba22497dc89a716c", "hex");
32+
wallet = await keystore.importKey(inputPrivateKey, "Coolw", password, CoinType.bitcoin, StoredKeyEncryption.aes128Ctr);
33+
34+
const account2 = wallet.activeAccounts[0];
35+
const key2 = await keystore.getKey(wallet.id, password, account2);
36+
37+
assert.equal(
38+
HexCoding.encode(key2.data()),
39+
"0x9cdb5cab19aec3bd0fcd614c5f185e7a1d97634d4225730eba22497dc89a716c"
40+
);
41+
}).timeout(10000);
42+
43+
it("test export", async () => {
44+
const { CoinType, StoredKeyEncryption, HexCoding } = globalThis.core;
45+
const mnemonic = globalThis.mnemonic as string;
46+
const password = globalThis.password as string;
47+
48+
const storage = new KeyStore.FileSystemStorage("/tmp");
49+
const keystore = new KeyStore.Default(globalThis.core, storage);
50+
51+
var wallet = await keystore.import(mnemonic, "Coolw", password, [
52+
CoinType.bitcoin,
53+
], StoredKeyEncryption.aes128Ctr);
54+
55+
const exported = await keystore.export(wallet.id, password);
56+
assert.equal(exported, mnemonic);
57+
58+
const inputPrivateKey = Buffer.from("9cdb5cab19aec3bd0fcd614c5f185e7a1d97634d4225730eba22497dc89a716c", "hex");
59+
wallet = await keystore.importKey(inputPrivateKey, "Coolw", password, CoinType.bitcoin, StoredKeyEncryption.aes128Ctr);
60+
61+
const exported2 = await keystore.export(wallet.id, password);
62+
assert.equal(HexCoding.encode(exported2), "0x9cdb5cab19aec3bd0fcd614c5f185e7a1d97634d4225730eba22497dc89a716c");
63+
}).timeout(10000);
64+
});

0 commit comments

Comments
 (0)