UNPKG

17.8 kBJavaScriptView Raw
1'use strict';
2
3// Relax some linter options:
4// * quote marks so "m/0'/1/2'/" doesn't need to be scaped
5// * too many tests, maxstatements -> 100
6// * store test vectors at the end, latedef: false
7// * should call is never defined
8/* jshint quotmark: false */
9/* jshint latedef: false */
10/* jshint maxstatements: 100 */
11/* jshint unused: false */
12
13var expect = require('chai').expect;
14var should = require('chai').should();
15var sinon = require('sinon');
16
17var keyLib = require('..');
18var owsCommon = require('@owstack/ows-common');
19var HDPrivateKey = keyLib.HDPrivateKey;
20var HDPublicKey = keyLib.HDPublicKey;
21var Networks = require('@owstack/network-lib');
22var lodash = require('lodash');
23
24// Setup some networks for tests.
25require('./data/networks');
26
27describe('HDKeys building with static methods', function() {
28
29 var classes = [HDPublicKey, HDPrivateKey];
30 var clazz, index;
31
32 lodash.each(classes, function(clazz) {
33 var expectStaticMethodFail = function(staticMethod, argument, message) {
34 expect(clazz[staticMethod].bind(null, argument)).to.throw(message);
35 };
36 it(clazz.name + ' fromJSON checks that a valid JSON is provided', function() {
37 var errorMessage = 'Invalid Argument: No valid argument was provided';
38 var method = 'fromObject';
39 expectStaticMethodFail(method, undefined, errorMessage);
40 expectStaticMethodFail(method, null, errorMessage);
41 expectStaticMethodFail(method, 'invalid JSON', errorMessage);
42 expectStaticMethodFail(method, '{\'singlequotes\': true}', errorMessage);
43 });
44 it(clazz.name + ' fromString checks that a string is provided', function() {
45 var errorMessage = 'No valid string was provided';
46 var method = 'fromString';
47 expectStaticMethodFail(method, undefined, errorMessage);
48 expectStaticMethodFail(method, null, errorMessage);
49 expectStaticMethodFail(method, {}, errorMessage);
50 });
51 it(clazz.name + ' fromObject checks that an object is provided', function() {
52 var errorMessage = 'No valid argument was provided';
53 var method = 'fromObject';
54 expectStaticMethodFail(method, undefined, errorMessage);
55 expectStaticMethodFail(method, null, errorMessage);
56 expectStaticMethodFail(method, '', errorMessage);
57 });
58 });
59
60});
61
62describe('BIP32 compliance', function() {
63
64 it('should initialize test vector 1 from the extended public key', function() {
65 new HDPublicKey(vector1_m_public).xpubkey.should.equal(vector1_m_public);
66 });
67
68 it('should initialize test vector 1 from the extended private key', function() {
69 new HDPrivateKey(vector1_m_private).xprivkey.should.equal(vector1_m_private);
70 });
71
72 it('can initialize a public key from an extended private key', function() {
73 new HDPublicKey(vector1_m_private).xpubkey.should.equal(vector1_m_public);
74 });
75
76 it('toString should be equal to the `xpubkey` member', function() {
77 var privateKey = new HDPrivateKey(vector1_m_private);
78 privateKey.toString().should.equal(privateKey.xprivkey);
79 });
80
81 it('toString should be equal to the `xpubkey` member', function() {
82 var publicKey = new HDPublicKey(vector1_m_public);
83 publicKey.toString().should.equal(publicKey.xpubkey);
84 });
85
86 it('should get the extended public key from the extended private key for test vector 1', function() {
87 HDPrivateKey(vector1_m_private).xpubkey.should.equal(vector1_m_public);
88 });
89
90 it("should get m/0' ext. private key from test vector 1", function() {
91 var privateKey = new HDPrivateKey(vector1_m_private).deriveChild("m/0'");
92 privateKey.xprivkey.should.equal(vector1_m0h_private);
93 });
94
95 it("should get m/0' ext. public key from test vector 1", function() {
96 HDPrivateKey(vector1_m_private).deriveChild("m/0'")
97 .xpubkey.should.equal(vector1_m0h_public);
98 });
99
100 it("should get m/0'/1 ext. private key from test vector 1", function() {
101 HDPrivateKey(vector1_m_private).deriveChild("m/0'/1")
102 .xprivkey.should.equal(vector1_m0h1_private);
103 });
104
105 it("should get m/0'/1 ext. public key from test vector 1", function() {
106 HDPrivateKey(vector1_m_private).deriveChild("m/0'/1")
107 .xpubkey.should.equal(vector1_m0h1_public);
108 });
109
110 it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function() {
111 var derivedPublic = HDPrivateKey(vector1_m_private).deriveChild("m/0'").hdPublicKey.deriveChild("m/1");
112 derivedPublic.xpubkey.should.equal(vector1_m0h1_public);
113 });
114
115 it("should get m/0'/1/2' ext. private key from test vector 1", function() {
116 var privateKey = new HDPrivateKey(vector1_m_private);
117 var derived = privateKey.deriveChild("m/0'/1/2'");
118 derived.xprivkey.should.equal(vector1_m0h12h_private);
119 });
120
121 it("should get m/0'/1/2' ext. public key from test vector 1", function() {
122 HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'")
123 .xpubkey.should.equal(vector1_m0h12h_public);
124 });
125
126 it("should get m/0'/1/2'/2 ext. private key from test vector 1", function() {
127 HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'/2")
128 .xprivkey.should.equal(vector1_m0h12h2_private);
129 });
130
131 it("should get m/0'/1/2'/2 ext. public key from m/0'/1/2' public key from test vector 1", function() {
132 var derived = HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'").hdPublicKey;
133 derived.deriveChild("m/2").xpubkey.should.equal(vector1_m0h12h2_public);
134 });
135
136 it("should get m/0'/1/2h/2 ext. public key from test vector 1", function() {
137 HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'/2")
138 .xpubkey.should.equal(vector1_m0h12h2_public);
139 });
140
141 it("should get m/0'/1/2h/2/1000000000 ext. private key from test vector 1", function() {
142 HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'/2/1000000000")
143 .xprivkey.should.equal(vector1_m0h12h21000000000_private);
144 });
145
146 it("should get m/0'/1/2h/2/1000000000 ext. public key from test vector 1", function() {
147 HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'/2/1000000000")
148 .xpubkey.should.equal(vector1_m0h12h21000000000_public);
149 });
150
151 it("should get m/0'/1/2'/2/1000000000 ext. public key from m/0'/1/2'/2 public key from test vector 1", function() {
152 var derived = HDPrivateKey(vector1_m_private).deriveChild("m/0'/1/2'/2").hdPublicKey;
153 derived.deriveChild("m/1000000000").xpubkey.should.equal(vector1_m0h12h21000000000_public);
154 });
155
156 it('should initialize test vector 2 from the extended public key', function() {
157 HDPublicKey(vector2_m_public).xpubkey.should.equal(vector2_m_public);
158 });
159
160 it('should initialize test vector 2 from the extended private key', function() {
161 HDPrivateKey(vector2_m_private).xprivkey.should.equal(vector2_m_private);
162 });
163
164 it('should get the extended public key from the extended private key for test vector 2', function() {
165 HDPrivateKey(vector2_m_private).xpubkey.should.equal(vector2_m_public);
166 });
167
168 it("should get m/0 ext. private key from test vector 2", function() {
169 HDPrivateKey(vector2_m_private).deriveChild(0).xprivkey.should.equal(vector2_m0_private);
170 });
171
172 it("should get m/0 ext. public key from test vector 2", function() {
173 HDPrivateKey(vector2_m_private).deriveChild(0).xpubkey.should.equal(vector2_m0_public);
174 });
175
176 it("should get m/0 ext. public key from m public key from test vector 2", function() {
177 HDPrivateKey(vector2_m_private).hdPublicKey.deriveChild(0).xpubkey.should.equal(vector2_m0_public);
178 });
179
180 it("should get m/0/2147483647h ext. private key from test vector 2", function() {
181 HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'")
182 .xprivkey.should.equal(vector2_m02147483647h_private);
183 });
184
185 it("should get m/0/2147483647h ext. public key from test vector 2", function() {
186 HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'")
187 .xpubkey.should.equal(vector2_m02147483647h_public);
188 });
189
190 it("should get m/0/2147483647h/1 ext. private key from test vector 2", function() {
191 HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1")
192 .xprivkey.should.equal(vector2_m02147483647h1_private);
193 });
194
195 it("should get m/0/2147483647h/1 ext. public key from test vector 2", function() {
196 HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1")
197 .xpubkey.should.equal(vector2_m02147483647h1_public);
198 });
199
200 it("should get m/0/2147483647h/1 ext. public key from m/0/2147483647h public key from test vector 2", function() {
201 var derived = HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'").hdPublicKey;
202 derived.deriveChild(1).xpubkey.should.equal(vector2_m02147483647h1_public);
203 });
204
205 it("should get m/0/2147483647h/1/2147483646h ext. private key from test vector 2", function() {
206 HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1/2147483646'")
207 .xprivkey.should.equal(vector2_m02147483647h12147483646h_private);
208 });
209
210 it("should get m/0/2147483647h/1/2147483646h ext. public key from test vector 2", function() {
211 HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1/2147483646'")
212 .xpubkey.should.equal(vector2_m02147483647h12147483646h_public);
213 });
214
215 it("should get m/0/2147483647h/1/2147483646h/2 ext. private key from test vector 2", function() {
216 HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1/2147483646'/2")
217 .xprivkey.should.equal(vector2_m02147483647h12147483646h2_private);
218 });
219
220 it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from test vector 2", function() {
221 HDPrivateKey(vector2_m_private).deriveChild("m/0/2147483647'/1/2147483646'/2")
222 .xpubkey.should.equal(vector2_m02147483647h12147483646h2_public);
223 });
224
225 it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from m/0/2147483647h/2147483646h public key from test vector 2", function() {
226 var derivedPublic = HDPrivateKey(vector2_m_private)
227 .deriveChild("m/0/2147483647'/1/2147483646'").hdPublicKey;
228 derivedPublic.deriveChild("m/2")
229 .xpubkey.should.equal(vector2_m02147483647h12147483646h2_public);
230 });
231
232 it('should use full 32 bytes for private key data that is hashed (as per bip32)', function() {
233 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
234 var privateKeyBuffer = new Buffer('00000055378cf5fafb56c711c674143f9b0ee82ab0ba2924f19b64f5ae7cdbfd', 'hex');
235 var chainCodeBuffer = new Buffer('9c8a5c863e5941f3d99453e6ba66b328bb17cf0b8dec89ed4fc5ace397a1c089', 'hex');
236 var key = HDPrivateKey.fromObject({
237 network: 'btc',
238 depth: 0,
239 parentFingerPrint: 0,
240 childIndex: 0,
241 privateKey: privateKeyBuffer,
242 chainCode: chainCodeBuffer
243 });
244 var derived = key.deriveChild("m/44'/0'/0'/0/0'");
245 derived.privateKey.toString().should.equal('3348069561d2a0fb925e74bf198762acc47dce7db27372257d2d959a9e6f8aeb');
246 });
247
248 describe('edge cases', function() {
249
250 var sandbox = sinon.sandbox.create();
251 afterEach(function() {
252 sandbox.restore();
253 });
254
255 it('will handle edge case that derived private key is invalid', function() {
256 var invalid = new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
257 var privateKeyBuffer = new Buffer('5f72914c48581fc7ddeb944a9616389200a9560177d24f458258e5b04527bcd1', 'hex');
258 var chainCodeBuffer = new Buffer('39816057bba9d952fe87fe998b7fd4d690a1bb58c2ff69141469e4d1dffb4b91', 'hex');
259 var unstubbed = owsCommon.BN.prototype.toBuffer;
260 var count = 0;
261 var stub = sandbox.stub(owsCommon.BN.prototype, 'toBuffer', function(args) {
262 // On the fourth call to the function give back an invalid private key
263 // otherwise use the normal behavior.
264 count++;
265 if (count === 4) {
266 return invalid;
267 }
268 var ret = unstubbed.apply(this, arguments);
269 return ret;
270 });
271 sandbox.spy(keyLib.PrivateKey, 'isValid');
272 var key = HDPrivateKey.fromObject({
273 network: 'btc',
274 depth: 0,
275 parentFingerPrint: 0,
276 childIndex: 0,
277 privateKey: privateKeyBuffer,
278 chainCode: chainCodeBuffer
279 });
280 var derived = key.deriveChild("m/44'");
281 derived.privateKey.toString().should.equal('b15bce3608d607ee3a49069197732c656bca942ee59f3e29b4d56914c1de6825');
282 keyLib.PrivateKey.isValid.callCount.should.equal(2);
283 });
284
285 it('will handle edge case that a derive public key is invalid', function() {
286 var publicKeyBuffer = new Buffer('029e58b241790284ef56502667b15157b3fc58c567f044ddc35653860f9455d099', 'hex');
287 var chainCodeBuffer = new Buffer('39816057bba9d952fe87fe998b7fd4d690a1bb58c2ff69141469e4d1dffb4b91', 'hex');
288 var key = new HDPublicKey({
289 network: 'btc',
290 depth: 0,
291 parentFingerPrint: 0,
292 childIndex: 0,
293 chainCode: chainCodeBuffer,
294 publicKey: publicKeyBuffer
295 });
296 var unstubbed = keyLib.PublicKey.fromPoint;
297 keyLib.PublicKey.fromPoint = function() {
298 keyLib.PublicKey.fromPoint = unstubbed;
299 throw new Error('Point cannot be equal to Infinity');
300 };
301 sandbox.spy(key, '_deriveWithNumber');
302 var derived = key.deriveChild("m/44");
303 key._deriveWithNumber.callCount.should.equal(2);
304 key.publicKey.toString().should.equal('029e58b241790284ef56502667b15157b3fc58c567f044ddc35653860f9455d099');
305 });
306 });
307
308 describe('seed', function() {
309
310 it('should initialize a new BIP32 correctly from test vector 1 seed', function() {
311 var seededKey = HDPrivateKey.fromSeed(vector1_master, Networks.get('btc'));
312 seededKey.xprivkey.should.equal(vector1_m_private);
313 seededKey.xpubkey.should.equal(vector1_m_public);
314 });
315
316 it('should initialize a new BIP32 correctly from test vector 2 seed', function() {
317 var seededKey = HDPrivateKey.fromSeed(vector2_master, Networks.get('btc'));
318 seededKey.xprivkey.should.equal(vector2_m_private);
319 seededKey.xpubkey.should.equal(vector2_m_public);
320 });
321 });
322
323});
324
325//test vectors: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
326var vector1_master = '000102030405060708090a0b0c0d0e0f';
327var vector1_m_public = 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8';
328var vector1_m_private = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
329var vector1_m0h_public = 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw';
330var vector1_m0h_private = 'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7';
331var vector1_m0h1_public = 'xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ';
332var vector1_m0h1_private = 'xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs';
333var vector1_m0h12h_public = 'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5';
334var vector1_m0h12h_private = 'xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM';
335var vector1_m0h12h2_public = 'xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV';
336var vector1_m0h12h2_private = 'xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334';
337var vector1_m0h12h21000000000_public = 'xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy';
338var vector1_m0h12h21000000000_private = 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76';
339var vector2_master = 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542';
340var vector2_m_public = 'xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB';
341var vector2_m_private = 'xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U';
342var vector2_m0_public = 'xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH';
343var vector2_m0_private = 'xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt';
344var vector2_m02147483647h_public = 'xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a';
345var vector2_m02147483647h_private = 'xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9';
346var vector2_m02147483647h1_public = 'xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon';
347var vector2_m02147483647h1_private = 'xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef';
348var vector2_m02147483647h12147483646h_public = 'xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL';
349var vector2_m02147483647h12147483646h_private = 'xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc';
350var vector2_m02147483647h12147483646h2_public = 'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt';
351var vector2_m02147483647h12147483646h2_private = 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j';