1+ "use strict" ;
2+
13var assert = require ( "assert" ) ;
24var BigInteger = require ( "bigi" ) ;
5+ var crypto = require ( "crypto" ) ;
36var ecurve = require ( "ecurve" ) ;
4- var secureRandom = require ( "secure-random" ) ;
57var curve = ecurve . getCurveByName ( "secp256k1" ) ;
68var HDKey = require ( "../" ) ;
79var fixtures = require ( "./fixtures/hdkey" ) ;
@@ -13,7 +15,7 @@ describe("hdkey", function () {
1315 describe ( "+ fromMasterSeed" , function ( ) {
1416 fixtures . valid . forEach ( function ( f ) {
1517 it ( "should properly derive the chain path: " + f . path , function ( ) {
16- var hdkey = HDKey . fromMasterSeed ( Buffer . from ( f . seed , "hex" ) ) ;
18+ var hdkey = HDKey . fromMasterSeed ( hexToU8 ( f . seed ) ) ;
1719 var childkey = hdkey . derive ( f . path ) ;
1820
1921 assert . equal ( childkey . getPrivateExtendedKey ( ) , f . private ) ;
@@ -22,7 +24,7 @@ describe("hdkey", function () {
2224
2325 describe ( "> " + f . path + " toJSON() / fromJSON()" , function ( ) {
2426 it ( "should return an object read for JSON serialization" , function ( ) {
25- var hdkey = HDKey . fromMasterSeed ( Buffer . from ( f . seed , "hex" ) ) ;
27+ var hdkey = HDKey . fromMasterSeed ( hexToU8 ( f . seed ) ) ;
2628 var childkey = hdkey . derive ( f . path ) ;
2729
2830 var obj = {
@@ -44,7 +46,7 @@ describe("hdkey", function () {
4446 it ( "should throw an error if incorrect key size" , function ( ) {
4547 var hdkey = HDKey . create ( ) ;
4648 assert . throws ( function ( ) {
47- hdkey . setPrivateKey ( Buffer . from ( [ 1 , 2 , 3 , 4 ] ) ) ;
49+ hdkey . setPrivateKey ( Uint8Array . from ( [ 1 , 2 , 3 , 4 ] ) ) ;
4850 } , / k e y m u s t b e 3 2 / ) ;
4951 } ) ;
5052 } ) ;
@@ -53,24 +55,28 @@ describe("hdkey", function () {
5355 it ( "should throw an error if incorrect key size" , function ( ) {
5456 assert . throws ( function ( ) {
5557 var hdkey = HDKey . create ( ) ;
56- hdkey . setPublicKey ( Buffer . from ( [ 1 , 2 , 3 , 4 ] ) ) ;
58+ hdkey . setPublicKey ( Uint8Array . from ( [ 1 , 2 , 3 , 4 ] ) ) ;
5759 } , / k e y m u s t b e 3 3 o r 6 5 / ) ;
5860 } ) ;
5961
6062 it ( "should not throw if key is 33 bytes (compressed)" , function ( ) {
61- var priv = secureRandom . randomBuffer ( 32 ) ;
63+ var rnd = crypto . randomBytes ( 32 ) ;
64+ var priv = new Uint8Array ( rnd ) ;
65+
6266 var pub = curve . G . multiply ( BigInteger . fromBuffer ( priv ) ) . getEncoded ( true ) ;
6367 assert . equal ( pub . length , 33 ) ;
6468 var hdkey = HDKey . create ( ) ;
65- hdkey . setPublicKey ( pub ) ;
69+ hdkey . setPublicKey ( new Uint8Array ( pub ) ) ;
6670 } ) ;
6771
6872 it ( "should not throw if key is 65 bytes (not compressed)" , function ( ) {
69- var priv = secureRandom . randomBuffer ( 32 ) ;
73+ var rnd = crypto . randomBytes ( 32 ) ;
74+ var priv = new Uint8Array ( rnd ) ;
75+
7076 var pub = curve . G . multiply ( BigInteger . fromBuffer ( priv ) ) . getEncoded ( false ) ;
7177 assert . equal ( pub . length , 65 ) ;
7278 var hdkey = HDKey . create ( ) ;
73- hdkey . setPublicKey ( pub ) ;
79+ hdkey . setPublicKey ( new Uint8Array ( pub ) ) ;
7480 } ) ;
7581 } ) ;
7682
@@ -87,19 +93,19 @@ describe("hdkey", function () {
8793 assert . equal ( hdkey . parentFingerprint , 0x31a507b8 ) ;
8894 assert . equal ( hdkey . index , 2 ) ;
8995 assert . equal (
90- hdkey . chainCode . toString ( "hex" ) ,
96+ u8ToHex ( hdkey . chainCode ) ,
9197 "9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271" ,
9298 ) ;
9399 assert . equal (
94- hdkey . getPrivateKey ( ) . toString ( "hex" ) ,
100+ u8ToHex ( hdkey . getPrivateKey ( ) ) ,
95101 "bb7d39bdb83ecf58f2fd82b6d918341cbef428661ef01ab97c28a4842125ac23" ,
96102 ) ;
97103 assert . equal (
98- hdkey . publicKey . toString ( "hex" ) ,
104+ u8ToHex ( hdkey . publicKey ) ,
99105 "024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c" ,
100106 ) ;
101107 assert . equal (
102- hdkey . identifier . toString ( "hex" ) ,
108+ u8ToHex ( hdkey . identifier ) ,
103109 "26132fdbe7bf89cbc64cf8dafa3f9f88b8666220" ,
104110 ) ;
105111 } ) ;
@@ -117,16 +123,16 @@ describe("hdkey", function () {
117123 assert . equal ( hdkey . parentFingerprint , 0x31a507b8 ) ;
118124 assert . equal ( hdkey . index , 2 ) ;
119125 assert . equal (
120- hdkey . chainCode . toString ( "hex" ) ,
126+ u8ToHex ( hdkey . chainCode ) ,
121127 "9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271" ,
122128 ) ;
123129 assert . equal ( hdkey . getPrivateKey ( ) , null ) ;
124130 assert . equal (
125- hdkey . publicKey . toString ( "hex" ) ,
131+ u8ToHex ( hdkey . publicKey ) ,
126132 "024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c" ,
127133 ) ;
128134 assert . equal (
129- hdkey . identifier . toString ( "hex" ) ,
135+ u8ToHex ( hdkey . identifier ) ,
130136 "26132fdbe7bf89cbc64cf8dafa3f9f88b8666220" ,
131137 ) ;
132138 } ) ;
@@ -142,16 +148,16 @@ describe("hdkey", function () {
142148 assert . equal ( hdkey . parentFingerprint , 0x31a507b8 ) ;
143149 assert . equal ( hdkey . index , 2 ) ;
144150 assert . equal (
145- hdkey . chainCode . toString ( "hex" ) ,
151+ u8ToHex ( hdkey . chainCode ) ,
146152 "9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271" ,
147153 ) ;
148154 assert . equal ( hdkey . getPrivateKey ( ) , null ) ;
149155 assert . equal (
150- hdkey . publicKey . toString ( "hex" ) ,
156+ u8ToHex ( hdkey . publicKey ) ,
151157 "024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c" ,
152158 ) ;
153159 assert . equal (
154- hdkey . identifier . toString ( "hex" ) ,
160+ u8ToHex ( hdkey . identifier ) ,
155161 "26132fdbe7bf89cbc64cf8dafa3f9f88b8666220" ,
156162 ) ;
157163 } ) ;
@@ -164,29 +170,29 @@ describe("hdkey", function () {
164170 "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j" ;
165171 var hdkey = HDKey . fromExtendedKey ( key ) ;
166172
167- var ma = Buffer . alloc ( 32 , 0 ) ;
168- var mb = Buffer . alloc ( 32 , 8 ) ;
173+ var ma = new Uint8Array ( 32 ) ;
174+ var mb = new Uint8Array ( Buffer . alloc ( 32 , 8 ) ) ;
169175 var a = hdkey . sign ( ma ) ;
170176 var b = hdkey . sign ( mb ) ;
171177 assert . equal (
172- a . toString ( "hex" ) ,
178+ u8ToHex ( a ) ,
173179 "6ba4e554457ce5c1f1d7dbd10459465e39219eb9084ee23270688cbe0d49b52b7905d5beb28492be439a3250e9359e0390f844321b65f1a88ce07960dd85da06" ,
174180 ) ;
175181 assert . equal (
176- b . toString ( "hex" ) ,
182+ u8ToHex ( b ) ,
177183 "dfae85d39b73c9d143403ce472f7c4c8a5032c13d9546030044050e7d39355e47a532e5c0ae2a25392d97f5e55ab1288ef1e08d5c034bad3b0956fbbab73b381" ,
178184 ) ;
179185 assert . equal ( hdkey . verify ( ma , a ) , true ) ;
180186 assert . equal ( hdkey . verify ( mb , b ) , true ) ;
181- assert . equal ( hdkey . verify ( Buffer . alloc ( 32 ) , Buffer . alloc ( 64 ) ) , false ) ;
187+ assert . equal ( hdkey . verify ( new Uint8Array ( 32 ) , new Uint8Array ( 64 ) ) , false ) ;
182188 assert . equal ( hdkey . verify ( ma , b ) , false ) ;
183189 assert . equal ( hdkey . verify ( mb , a ) , false ) ;
184190
185191 assert . throws ( function ( ) {
186- hdkey . verify ( Buffer . alloc ( 99 ) , a ) ;
192+ hdkey . verify ( new Uint8Array ( 99 ) , a ) ;
187193 } , / m e s s a g e .* l e n g t h / ) ;
188194 assert . throws ( function ( ) {
189- hdkey . verify ( ma , Buffer . alloc ( 99 ) ) ;
195+ hdkey . verify ( ma , new Uint8Array ( 99 ) ) ;
190196 } , / s i g n a t u r e .* l e n g t h / ) ;
191197 } ) ;
192198 } ) ;
@@ -209,7 +215,7 @@ describe("hdkey", function () {
209215 describe ( "> when private key integer is less than 32 bytes" , function ( ) {
210216 it ( "should work" , function ( ) {
211217 var seed = "000102030405060708090a0b0c0d0e0f" ;
212- var masterKey = HDKey . fromMasterSeed ( Buffer . from ( seed , "hex" ) ) ;
218+ var masterKey = HDKey . fromMasterSeed ( hexToU8 ( seed ) ) ;
213219
214220 var newKey = masterKey . derive ( "m/44'/6'/4'" ) ;
215221 var expected =
@@ -230,12 +236,12 @@ describe("hdkey", function () {
230236 "xprv9s21ZrQH143K3ckY9DgU79uMTJkQRLdbCCVDh81SnxTgPzLLGax6uHeBULTtaEtcAvKjXfT7ZWtHzKjTpujMkUd9dDb8msDeAfnJxrgAYhr" ;
231237 var hdkey = HDKey . fromExtendedKey ( key ) ;
232238 assert . equal (
233- hdkey . getPrivateKey ( ) . toString ( "hex" ) ,
239+ u8ToHex ( hdkey . getPrivateKey ( ) ) ,
234240 "00000055378cf5fafb56c711c674143f9b0ee82ab0ba2924f19b64f5ae7cdbfd" ,
235241 ) ;
236242 var derived = hdkey . derive ( "m/44'/0'/0'/0/0'" ) ;
237243 assert . equal (
238- derived . getPrivateKey ( ) . toString ( "hex" ) ,
244+ u8ToHex ( derived . getPrivateKey ( ) ) ,
239245 "3348069561d2a0fb925e74bf198762acc47dce7db27372257d2d959a9e6f8aeb" ,
240246 ) ;
241247 } ) ;
@@ -244,7 +250,7 @@ describe("hdkey", function () {
244250 describe ( "> when private key is null" , function ( ) {
245251 it ( "privateExtendedKey should return null and not throw" , function ( ) {
246252 var seed = "000102030405060708090a0b0c0d0e0f" ;
247- var masterKey = HDKey . fromMasterSeed ( Buffer . from ( seed , "hex" ) ) ;
253+ var masterKey = HDKey . fromMasterSeed ( hexToU8 ( seed ) ) ;
248254
249255 assert . ok ( masterKey . getPrivateExtendedKey ( ) , "xpriv is truthy" ) ;
250256 masterKey . wipePrivateData ( ) ;
@@ -258,9 +264,7 @@ describe("hdkey", function () {
258264 } ) ;
259265
260266 describe ( " - when the path given to derive contains only the master extended key" , function ( ) {
261- const hdKeyInstance = HDKey . fromMasterSeed (
262- Buffer . from ( fixtures . valid [ 0 ] . seed , "hex" ) ,
263- ) ;
267+ const hdKeyInstance = HDKey . fromMasterSeed ( hexToU8 ( fixtures . valid [ 0 ] . seed ) ) ;
264268
265269 it ( "should return the same hdkey instance" , function ( ) {
266270 assert . equal ( hdKeyInstance . derive ( "m" ) , hdKeyInstance ) ;
@@ -282,12 +286,12 @@ describe("hdkey", function () {
282286 describe ( "- after wipePrivateData()" , function ( ) {
283287 it ( "should not have private data" , function ( ) {
284288 const hdkey = HDKey . fromMasterSeed (
285- Buffer . from ( fixtures . valid [ 6 ] . seed , "hex" ) ,
289+ hexToU8 ( fixtures . valid [ 6 ] . seed ) ,
286290 ) . wipePrivateData ( ) ;
287291 assert . equal ( hdkey . getPrivateKey ( ) , null ) ;
288292 assert . equal ( hdkey . getPrivateExtendedKey ( ) , null ) ;
289293 assert . throws (
290- ( ) => hdkey . sign ( Buffer . alloc ( 32 ) ) ,
294+ ( ) => hdkey . sign ( new Uint8Array ( 32 ) ) ,
291295 "shouldn't be able to sign" ,
292296 ) ;
293297 const childKey = hdkey . derive ( "m/0" ) ;
@@ -307,15 +311,15 @@ describe("hdkey", function () {
307311 assert . equal ( hdkey . parentFingerprint , 0x31a507b8 ) ;
308312 assert . equal ( hdkey . index , 2 ) ;
309313 assert . equal (
310- hdkey . chainCode . toString ( "hex" ) ,
314+ u8ToHex ( hdkey . chainCode ) ,
311315 "9452b549be8cea3ecb7a84bec10dcfd94afe4d129ebfd3b3cb58eedf394ed271" ,
312316 ) ;
313317 assert . equal (
314- hdkey . publicKey . toString ( "hex" ) ,
318+ u8ToHex ( hdkey . publicKey ) ,
315319 "024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c" ,
316320 ) ;
317321 assert . equal (
318- hdkey . identifier . toString ( "hex" ) ,
322+ u8ToHex ( hdkey . identifier ) ,
319323 "26132fdbe7bf89cbc64cf8dafa3f9f88b8666220" ,
320324 ) ;
321325 } ) ;
@@ -325,7 +329,7 @@ describe("hdkey", function () {
325329 // using JSON methods to clone before mutating
326330 const wipedKey = HDKey . fromJSON ( fullKey . toJSON ( ) ) . wipePrivateData ( ) ;
327331
328- const hash = Buffer . alloc ( 32 , 8 ) ;
332+ const hash = new Uint8Array ( Buffer . alloc ( 32 , 8 ) ) ;
329333 assert . ok ( wipedKey . verify ( hash , fullKey . sign ( hash ) ) ) ;
330334 } ) ;
331335
@@ -340,16 +344,16 @@ describe("hdkey", function () {
340344 it ( "should not mutate it when deriving with a private key" , function ( ) {
341345 const hdkey = HDKey . fromExtendedKey ( fixtures . valid [ 0 ] . private ) ;
342346 const path = "m/123" ;
343- const privateKeyBefore = hdkey . getPrivateKey ( ) . toString ( "hex" ) ;
347+ const privateKeyBefore = u8ToHex ( hdkey . getPrivateKey ( ) ) ;
344348
345349 const child = hdkey . derive ( path ) ;
346- assert . equal ( hdkey . getPrivateKey ( ) . toString ( "hex" ) , privateKeyBefore ) ;
350+ assert . equal ( u8ToHex ( hdkey . getPrivateKey ( ) ) , privateKeyBefore ) ;
347351
348352 const child2 = hdkey . derive ( path ) ;
349- assert . equal ( hdkey . getPrivateKey ( ) . toString ( "hex" ) , privateKeyBefore ) ;
353+ assert . equal ( u8ToHex ( hdkey . getPrivateKey ( ) ) , privateKeyBefore ) ;
350354
351355 const child3 = hdkey . derive ( path ) ;
352- assert . equal ( hdkey . getPrivateKey ( ) . toString ( "hex" ) , privateKeyBefore ) ;
356+ assert . equal ( u8ToHex ( hdkey . getPrivateKey ( ) ) , privateKeyBefore ) ;
353357
354358 assert . equal (
355359 child . getPrivateKey ( ) . toString ( "hex" ) ,
@@ -366,16 +370,16 @@ describe("hdkey", function () {
366370 const path = "m/123/123/123" ;
367371 hdkey . wipePrivateData ( ) ;
368372
369- const publicKeyBefore = hdkey . publicKey . toString ( "hex" ) ;
373+ const publicKeyBefore = u8ToHex ( hdkey . publicKey ) ;
370374
371375 const child = hdkey . derive ( path ) ;
372- assert . equal ( hdkey . publicKey . toString ( "hex" ) , publicKeyBefore ) ;
376+ assert . equal ( u8ToHex ( hdkey . publicKey ) , publicKeyBefore ) ;
373377
374378 const child2 = hdkey . derive ( path ) ;
375- assert . equal ( hdkey . publicKey . toString ( "hex" ) , publicKeyBefore ) ;
379+ assert . equal ( u8ToHex ( hdkey . publicKey ) , publicKeyBefore ) ;
376380
377381 const child3 = hdkey . derive ( path ) ;
378- assert . equal ( hdkey . publicKey . toString ( "hex" ) , publicKeyBefore ) ;
382+ assert . equal ( u8ToHex ( hdkey . publicKey ) , publicKeyBefore ) ;
379383
380384 assert . equal (
381385 child . publicKey . toString ( "hex" ) ,
@@ -388,3 +392,46 @@ describe("hdkey", function () {
388392 } ) ;
389393 } ) ;
390394} ) ;
395+
396+ /**
397+ * @param {String } hex
398+ * @returns {Uint8Array }
399+ */
400+ function hexToU8 ( hex ) {
401+ let bufLen = hex . length / 2 ;
402+ let u8 = new Uint8Array ( bufLen ) ;
403+
404+ let i = 0 ;
405+ let index = 0 ;
406+ let lastIndex = hex . length - 2 ;
407+ for ( ; ; ) {
408+ if ( i > lastIndex ) {
409+ break ;
410+ }
411+
412+ let h = hex . substr ( i , 2 ) ;
413+ let b = parseInt ( h , 16 ) ;
414+ u8 [ index ] = b ;
415+
416+ i += 2 ;
417+ index += 1 ;
418+ }
419+
420+ return u8 ;
421+ }
422+
423+ /**
424+ * @param {Uint8Array } u8
425+ * @returns {String } hex
426+ */
427+ function u8ToHex ( u8 ) {
428+ /** @type {Array<String> } */
429+ let hex = [ ] ;
430+
431+ u8 . forEach ( function ( b ) {
432+ let h = b . toString ( 16 ) . padStart ( 2 , "0" ) ;
433+ hex . push ( h ) ;
434+ } ) ;
435+
436+ return hex . join ( "" ) ;
437+ }
0 commit comments