|
| 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