This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
wallet.factory.test.ts
204 lines (169 loc) · 7.06 KB
/
wallet.factory.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import "jest-extended";
import "reflect-metadata";
import { decrypt, encrypt } from "bip38";
import nock from "nock";
import { decode } from "wif";
import { bootContainer } from "../../../../test/helpers";
import { Profile } from "../profiles/profile";
import { WalletFactory } from "./wallet.factory";
import { WalletData } from "../../../contracts";
jest.setTimeout(60000);
let subject: WalletFactory;
beforeAll(() => {
bootContainer();
nock.disableNetConnect();
subject = new WalletFactory(new Profile({ id: "id", name: "name", avatar: "avatar", data: "" }));
});
beforeEach(async () => {
nock.cleanAll();
nock("https://dwallets.ark.io")
.get("/api/node/configuration")
.reply(200, require("../../../../test/fixtures/client/configuration.json"))
.get("/api/peers")
.reply(200, require("../../../../test/fixtures/client/peers.json"))
.get("/api/node/configuration/crypto")
.reply(200, require("../../../../test/fixtures/client/cryptoConfiguration.json"))
.get("/api/node/syncing")
.reply(200, require("../../../../test/fixtures/client/syncing.json"))
.get("/api/wallets/D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib")
.reply(200, require("../../../../test/fixtures/client/wallet.json"))
.persist();
nock("https://wallets.ark.io")
.get("/api/node/configuration")
.reply(200, require("../../../../test/fixtures/coins/ark/mainnet/configuration.json"))
.get("/api/peers")
.reply(200, require("../../../../test/fixtures/coins/ark/mainnet/peers.json"))
.get("/api/node/configuration/crypto")
.reply(200, require("../../../../test/fixtures/coins/ark/mainnet/cryptoConfiguration.json"))
.get("/api/node/syncing")
.reply(200, require("../../../../test/fixtures/coins/ark/mainnet/syncing.json"))
.get("/api/wallets/AdVSe37niA3uFUPgCgMUH2tMsHF4LpLoiX")
.reply(
200,
require("../../../../test/fixtures/coins/ark/mainnet/wallets/AdVSe37niA3uFUPgCgMUH2tMsHF4LpLoiX.json"),
)
.persist();
});
describe("#fromMnemonicWithBIP39", () => {
it("should create a wallet using BIP39", async () => {
const wallet = await subject.fromMnemonicWithBIP39({
coin: "ARK",
network: "ark.devnet",
mnemonic: "this is a top secret passphrase",
});
expect(wallet.address()).toBe("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib");
expect(wallet.publicKey()).toBe("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192");
});
it("should throw if BIP39 is requested but extended public keys are used", async () => {
await expect(
subject.fromMnemonicWithBIP39({
coin: "ADA",
network: "ada.testnet",
mnemonic: "this is a top secret passphrase",
}),
).rejects.toThrow("The configured network uses extended public keys with BIP44 for derivation.");
});
it("should create a wallet using BIP39 with encryption", async () => {
const wallet = await subject.fromMnemonicWithBIP39({
coin: "ARK",
network: "ark.devnet",
mnemonic: "this is a top secret passphrase",
password: "password",
});
expect(wallet.address()).toBe("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib");
expect(wallet.publicKey()).toBe("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192");
expect(wallet.data().get(WalletData.Bip38EncryptedKey)).toBeString();
// @ts-ignore
expect(decrypt(wallet.data().get(WalletData.Bip38EncryptedKey)!, "password").privateKey.toString("hex")).toBe(
"d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712",
);
});
});
describe("#fromMnemonicWithBIP44", () => {
it("should create a wallet using BIP44 (passphrase > address)", async () => {
const wallet = await subject.fromMnemonicWithBIP44({
coin: "BTC",
network: "btc.testnet",
mnemonic: "this is a top secret passphrase",
});
expect(wallet.address()).toBe("muJmwqMYzXinDoAxueuCBr5y3e3zca8MXY");
expect(wallet.publicKey()).toBe("025b7319847965a1dfda25e5337cb6f5bd4665a55fe0b456de2eff79c157d5d21e");
});
it("should create a wallet using BIP44 (passphrase > extended public key)", async () => {
const wallet = await subject.fromMnemonicWithBIP44({
coin: "ADA",
network: "ada.testnet",
mnemonic:
"excess behave track soul table wear ocean cash stay nature item turtle palm soccer lunch horror start stumble month panic right must lock dress",
});
expect(wallet.address()).toBe(
"aec30330deaecdd7503195a0d730256faef87027022b1bdda7ca0a61bca0a55e4d575af5a93bdf4905a3702fadedf451ea584791d233ade90965d608bac57304",
);
expect(wallet.publicKey()).toBe(
"aec30330deaecdd7503195a0d730256faef87027022b1bdda7ca0a61bca0a55e4d575af5a93bdf4905a3702fadedf451ea584791d233ade90965d608bac57304",
);
});
});
test("#fromAddress", async () => {
const wallet = await subject.fromAddress({
coin: "ARK",
network: "ark.devnet",
address: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
});
expect(wallet.address()).toBe("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib");
expect(wallet.publicKey()).toBe("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192");
const mainnetWallet = await subject.fromAddress({
coin: "ARK",
network: "ark.mainnet",
address: "AdVSe37niA3uFUPgCgMUH2tMsHF4LpLoiX",
});
expect(mainnetWallet.address()).toBe("AdVSe37niA3uFUPgCgMUH2tMsHF4LpLoiX");
});
test("#fromPublicKey", async () => {
const wallet = await subject.fromPublicKey({
coin: "ARK",
network: "ark.devnet",
publicKey: "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192",
});
expect(wallet.address()).toBe("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib");
expect(wallet.publicKey()).toBe("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192");
});
test("#fromPrivateKey", async () => {
const wallet = await subject.fromPrivateKey({
coin: "ARK",
network: "ark.devnet",
privateKey: "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712",
});
expect(wallet.address()).toBe("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib");
expect(wallet.publicKey()).toBe("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192");
});
test("#fromAddressWithDerivationPath", async () => {
const wallet = await subject.fromAddressWithDerivationPath({
coin: "ARK",
network: "ark.devnet",
address: "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
path: "1",
});
expect(wallet.address()).toBe("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib");
expect(wallet.publicKey()).toBe("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192");
});
test("#fromWIF", async () => {
const wallet = await subject.fromWIF({
coin: "ARK",
network: "ark.devnet",
wif: "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA",
});
expect(wallet.address()).toBe("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib");
expect(wallet.publicKey()).toBe("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192");
});
test("#fromWIFWithEncryption", async () => {
const { compressed, privateKey } = decode("SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA");
const wallet = await subject.fromWIFWithEncryption({
coin: "ARK",
network: "ark.devnet",
wif: encrypt(privateKey, compressed, "password"),
password: "password",
});
expect(wallet.address()).toBe("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib");
expect(wallet.publicKey()).toBe("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192");
});