-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
179 lines (161 loc) · 5.7 KB
/
test.js
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
"use strict";
let DashHd = require("./");
//let DashHd = require("dashhd");
let DashPhrase = require("dashphrase");
let DashKeys = require("dashkeys");
let words = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong";
let secret = "TREZOR";
// m/44'/5'/0'/0/0
let wif000 = "XCGKuZcKDjNhx8DaNKK4xwMMNzspaoToT6CafJAbBfQTi57buhLK";
let addr000 = "XrZJJfEKRNobcuwWKTD3bDu8ou7XSWPbc9";
let xprv00 =
"xprvA2L7qar7dyJNhxnE47gK5J6cc1oEHQuAk8WrZLnLeHTtnkeyP4w6Eo6Tt65trtdkTRtx8opazGnLbpWrkhzNaL6ZsgG3sQmc2yS8AxoMjfZ";
let xpub00 =
"xpub6FKUF6P1ULrfvSrhA9DKSS3MA3digsd27MSTMjBxCczsfYz7vcFLnbQwjP9CsAfEJsnD4UwtbU43iZaibv4vnzQNZmQAVcufN4r3pva8kTz";
// m/44'/5'/1'/1/1
let wif111 = "XF9murLtNpJaZXbwMxqJ6BhigEtu9NxfBCJDBokCJcqFkYkz3itz";
let addr111 = "XueHW2ELMxoXzXcaHMxmwVWhcADE1W5s8c";
async function getWalletKeys() {
let seed = await DashPhrase.toSeed(words, secret);
let wallet = await DashHd.fromSeed(seed);
let seedHex = DashKeys.utils.bytesToHex(seed);
console.info(`Testing against Zoomonic Seed:`);
console.info(` ${seedHex}`);
console.info();
// m/44'/5'/0'/0/0
let accountIndex = 0;
let account = await wallet.deriveAccount(accountIndex);
let use = DashHd.RECEIVE;
let xkey = await account.deriveXKey(use);
let xkey1 = await DashHd.derivePath(wallet, "m/44'/5'/0'/0");
let xprv = await DashHd.toXPrv(xkey);
if (xprv !== xprv00) {
throw new Error("wallet xprv derivation mismatch v canonical");
}
let xprv1 = await DashHd.toXPrv(xkey1);
if (xprv !== xprv1) {
throw new Error("wallet xprv derivation mismatch");
}
let xpub = await DashHd.toXPub(xkey);
if (xpub !== xpub00) {
throw new Error("wallet xpub derivation mismatch v canonical");
}
let xpub1 = await DashHd.toXPub(xkey1);
if (xpub !== xpub1) {
throw new Error("wallet xpub derivation mismatch");
}
console.info(` HD XKey Path: m/44'/5'/${accountIndex}'/${use}`);
console.info(` XPrv: `, xprv.slice(0, 56));
console.info(` `, xprv.slice(56));
console.info(` XPub: `, xpub.slice(0, 56));
console.info(` `, xpub.slice(56));
console.info();
let addressIndex = 0;
let key = await xkey.deriveAddress(addressIndex);
let key1 = await DashHd.derivePath(wallet, "m/44'/5'/0'/0/0");
let wif = await DashHd.toWif(key.privateKey);
if (wif !== wif000) {
throw new Error("wallet wif derivation mismatch");
}
let wif1 = await DashHd.toWif(key1.privateKey);
if (wif !== wif1) {
throw new Error("wallet wif derivation mismatch");
}
let addr = await DashHd.toAddr(key.publicKey);
if (addr !== addr000) {
throw new Error("wallet addr derivation mismatch");
}
let addr1 = await DashHd.toAddr(key1.publicKey);
if (addr !== addr1) {
throw new Error("wallet addr derivation mismatch");
}
let xkey2 = await DashHd.fromXKey(xpub);
let key2 = await xkey2.deriveAddress(addressIndex);
let addr2 = await DashHd.toAddr(key2.publicKey);
if (addr !== addr2) {
throw new Error("wallet xpub addr derivation mismatch");
}
console.info(
` HD Key Path: m/44'/5'/${accountIndex}'/${use}/${addressIndex}`,
);
console.info(` WIF: ${wif}`);
console.info(` Address: ${addr}`);
console.info();
console.info();
// m/44'/5'/1'/1/1
accountIndex = 1;
account = await wallet.deriveAccount(accountIndex);
use = DashHd.CHANGE;
xkey = await account.deriveXKey(use);
xkey1 = await DashHd.derivePath(wallet, "m/44'/5'/1'/1");
xprv = await DashHd.toXPrv(xkey);
xprv1 = await DashHd.toXPrv(xkey1);
if (xprv !== xprv1) {
throw new Error("wallet xprv derivation mismatch");
}
xpub = await DashHd.toXPub(xkey);
xpub1 = await DashHd.toXPub(xkey1);
if (xpub !== xpub1) {
throw new Error("wallet xpub derivation mismatch");
}
console.info(` HD XKey Path: m/44'/5'/${accountIndex}'/${use}`);
console.info(` XPrv: `, xprv.slice(0, 56));
console.info(` `, xprv.slice(56));
console.info(` XPub: `, xpub.slice(0, 56));
console.info(` `, xpub.slice(56));
console.info();
addressIndex = 1;
key = await xkey.deriveAddress(addressIndex);
key1 = await DashHd.derivePath(wallet, "m/44'/5'/1'/1/1");
wif = await DashHd.toWif(key.privateKey);
if (wif !== wif111) {
throw new Error("wallet wif derivation mismatch");
}
wif1 = await DashHd.toWif(key1.privateKey);
if (wif !== wif1) {
throw new Error("wallet wif derivation mismatch");
}
addr = await DashHd.toAddr(key.publicKey);
if (addr !== addr111) {
throw new Error("wallet addr derivation mismatch");
}
addr1 = await DashHd.toAddr(key1.publicKey);
if (addr !== addr1) {
throw new Error("wallet addr derivation mismatch");
}
xkey2 = await DashHd.fromXKey(xpub);
key2 = await xkey2.deriveAddress(addressIndex);
addr2 = await DashHd.toAddr(key2.publicKey);
if (addr !== addr2) {
throw new Error("wallet xpub addr derivation mismatch");
}
let xkey3 = await DashHd.fromXKey(xprv);
let key3 = await xkey3.deriveAddress(addressIndex);
let wif3 = await DashHd.toWif(key3.privateKey);
if (wif !== wif3) {
throw new Error("wallet xprv wif derivation mismatch");
}
let addr3 = await DashHd.toAddr(key3.publicKey);
if (addr !== addr3) {
throw new Error("wallet xprv addr derivation mismatch");
}
console.info(
` HD Key Path: m/44'/5'/${accountIndex}'/${use}/${addressIndex}`,
);
console.info(` WIF: ${wif}`);
console.info(` Address: ${addr}`);
console.info();
console.info(`PASS: all outputs match expected values`);
}
async function main() {
await getWalletKeys();
}
main()
.then(function () {
process.exit(0);
})
.catch(function (err) {
console.error("Fail:");
console.error(err.stack || err);
process.exit(1);
});