bsv2 2.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/.babelrc.test +10 -0
  2. package/.editorconfig +4 -0
  3. package/.env.defaults +1 -0
  4. package/.env.schema +1 -0
  5. package/.mocharc.yml +4 -0
  6. package/.nvmrc +1 -0
  7. package/.travis.yml +7 -0
  8. package/LICENSE +39 -0
  9. package/README.md +53 -0
  10. package/benchmark.js +50 -0
  11. package/bin/bsv.js +11 -0
  12. package/bin/network.js +92 -0
  13. package/build/bsv.js +3 -0
  14. package/build/index.html +20 -0
  15. package/dist/bsv.bundle.js +3 -0
  16. package/dist/bsv.bundle.js.LICENSE.txt +10 -0
  17. package/dist/bsv.bundle.js.map +1 -0
  18. package/dist/bsv.cjs.js +3 -0
  19. package/dist/bsv.cjs.js.LICENSE.txt +1 -0
  20. package/dist/bsv.cjs.js.map +1 -0
  21. package/dist/bsv.min.js +2 -0
  22. package/dist/bsv.module.js +3 -0
  23. package/dist/bsv.module.js.LICENSE.txt +1 -0
  24. package/dist/bsv.module.js.map +1 -0
  25. package/doc/assets/anchor.js +350 -0
  26. package/doc/assets/bass-addons.css +12 -0
  27. package/doc/assets/bass.css +544 -0
  28. package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  29. package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  30. package/doc/assets/fonts/LICENSE.txt +93 -0
  31. package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  32. package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  33. package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  34. package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  35. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  36. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  37. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  38. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  39. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  40. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  41. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  42. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  43. package/doc/assets/fonts/source-code-pro.css +23 -0
  44. package/doc/assets/github.css +123 -0
  45. package/doc/assets/site.js +168 -0
  46. package/doc/assets/split.css +15 -0
  47. package/doc/assets/split.js +784 -0
  48. package/doc/assets/style.css +147 -0
  49. package/doc/index.html +9387 -0
  50. package/docs/block.html +36 -0
  51. package/docs/ecies.html +81 -0
  52. package/docs/index.html +84 -0
  53. package/docs/networks.html +43 -0
  54. package/docs/script.html +101 -0
  55. package/docs/transaction.html +150 -0
  56. package/docs/unspentoutput.html +32 -0
  57. package/entry.js +82 -0
  58. package/index.js +102 -0
  59. package/jsdoc.json +5 -0
  60. package/karma.conf.js +78 -0
  61. package/lib/ach.js +59 -0
  62. package/lib/address.js +264 -0
  63. package/lib/aes.js +57 -0
  64. package/lib/aescbc.js +38 -0
  65. package/lib/base-58-check.js +83 -0
  66. package/lib/base-58.js +59 -0
  67. package/lib/bip-32.js +472 -0
  68. package/lib/bip-39-en-wordlist.js +2054 -0
  69. package/lib/bip-39-en.js +10 -0
  70. package/lib/bip-39-es-wordlist.js +2055 -0
  71. package/lib/bip-39-es.js +10 -0
  72. package/lib/bip-39-jp-wordlist.js +2053 -0
  73. package/lib/bip-39-jp.js +10 -0
  74. package/lib/bip-39-words.js +5 -0
  75. package/lib/bip-39.js +275 -0
  76. package/lib/block-header.js +79 -0
  77. package/lib/block.js +132 -0
  78. package/lib/bn.js +467 -0
  79. package/lib/br.js +172 -0
  80. package/lib/bsm.js +127 -0
  81. package/lib/bw.js +188 -0
  82. package/lib/cbc.js +140 -0
  83. package/lib/cmp.js +37 -0
  84. package/lib/config.js +50 -0
  85. package/lib/constants.js +150 -0
  86. package/lib/ecdsa.js +495 -0
  87. package/lib/ecies.js +151 -0
  88. package/lib/hash-cache.js +41 -0
  89. package/lib/hash.js +191 -0
  90. package/lib/interp.js +1853 -0
  91. package/lib/inv.js +49 -0
  92. package/lib/key-pair.js +133 -0
  93. package/lib/mainnet-address.js +5 -0
  94. package/lib/mainnet-bip-32.js +5 -0
  95. package/lib/mainnet-key-pair.js +5 -0
  96. package/lib/mainnet-priv-key.js +5 -0
  97. package/lib/merkle.js +92 -0
  98. package/lib/msg.js +184 -0
  99. package/lib/op-code.js +398 -0
  100. package/lib/point.js +134 -0
  101. package/lib/priv-key.js +157 -0
  102. package/lib/pub-key.js +230 -0
  103. package/lib/rabin.js +300 -0
  104. package/lib/random.js +28 -0
  105. package/lib/script.js +826 -0
  106. package/lib/sig-operations.js +120 -0
  107. package/lib/sig.js +399 -0
  108. package/lib/struct.js +389 -0
  109. package/lib/testnet-address.js +5 -0
  110. package/lib/testnet-bip-32.js +5 -0
  111. package/lib/testnet-key-pair.js +5 -0
  112. package/lib/testnet-priv-key.js +5 -0
  113. package/lib/tx-builder.js +593 -0
  114. package/lib/tx-in.js +172 -0
  115. package/lib/tx-out-map.js +57 -0
  116. package/lib/tx-out.js +73 -0
  117. package/lib/tx-verifier.js +209 -0
  118. package/lib/tx.js +429 -0
  119. package/lib/var-int.js +73 -0
  120. package/lib/version.js +92 -0
  121. package/lib/workers-result.js +65 -0
  122. package/lib/workers.js +77 -0
  123. package/load-babel.js +3 -0
  124. package/mkdocs.yml +37 -0
  125. package/package.json +138 -0
  126. package/test/ach.js +172 -0
  127. package/test/address.js +464 -0
  128. package/test/aes.js +96 -0
  129. package/test/aescbc.js +65 -0
  130. package/test/base-58-check.js +128 -0
  131. package/test/base-58.js +113 -0
  132. package/test/bip-32.js +721 -0
  133. package/test/bip-39.js +282 -0
  134. package/test/block-header.js +152 -0
  135. package/test/block.js +243 -0
  136. package/test/bn.js +643 -0
  137. package/test/br.js +353 -0
  138. package/test/bsm.js +141 -0
  139. package/test/bw.js +292 -0
  140. package/test/cbc.js +335 -0
  141. package/test/cmp.js +56 -0
  142. package/test/data/bip69.json +215 -0
  143. package/test/data/bitcoind/base58_keys_invalid.json +152 -0
  144. package/test/data/bitcoind/base58_keys_valid.json +452 -0
  145. package/test/data/bitcoind/blocks.json +27 -0
  146. package/test/data/bitcoind/script_tests.json +2244 -0
  147. package/test/data/bitcoind/sig_canonical.json +7 -0
  148. package/test/data/bitcoind/sig_noncanonical.json +22 -0
  149. package/test/data/bitcoind/tx_invalid.json +177 -0
  150. package/test/data/bitcoind/tx_valid.json +224 -0
  151. package/test/data/blk86756-testnet.dat +0 -0
  152. package/test/data/blk86756-testnet.js +12 -0
  153. package/test/data/blk86756-testnet.json +684 -0
  154. package/test/data/ecdsa.json +230 -0
  155. package/test/data/messages.json +22 -0
  156. package/test/data/sighash.json +1004 -0
  157. package/test/data/tx_creation.json +85 -0
  158. package/test/ecdsa.js +748 -0
  159. package/test/ecies.js +238 -0
  160. package/test/global.js +13 -0
  161. package/test/hash-cache.js +52 -0
  162. package/test/hash.js +354 -0
  163. package/test/index.js +10 -0
  164. package/test/interp.js +512 -0
  165. package/test/inv.js +65 -0
  166. package/test/key-pair.js +243 -0
  167. package/test/merkle.js +179 -0
  168. package/test/msg.js +295 -0
  169. package/test/op-code.js +79 -0
  170. package/test/point.js +375 -0
  171. package/test/priv-key.js +264 -0
  172. package/test/pub-key.js +549 -0
  173. package/test/random.js +31 -0
  174. package/test/script.js +1340 -0
  175. package/test/sig-operations.js +149 -0
  176. package/test/sig.js +662 -0
  177. package/test/struct.js +552 -0
  178. package/test/tx-builder.js +2104 -0
  179. package/test/tx-in.js +198 -0
  180. package/test/tx-out-map.js +104 -0
  181. package/test/tx-out.js +162 -0
  182. package/test/tx-verifier.js +246 -0
  183. package/test/tx.js +539 -0
  184. package/test/var-int.js +132 -0
  185. package/test/vectors/aes.json +10392 -0
  186. package/test/vectors/aescbc.json +620 -0
  187. package/test/vectors/bip39.json +502 -0
  188. package/test/vectors/bip69.json +215 -0
  189. package/test/vectors/bitcoin-abc/script_tests.json +48 -0
  190. package/test/vectors/bitcoin-abc/sighash.json +7004 -0
  191. package/test/vectors/bitcoin-sv/script_tests.json +248 -0
  192. package/test/vectors/bitcoind/script_invalid.json +1151 -0
  193. package/test/vectors/bitcoind/script_valid.json +1462 -0
  194. package/test/vectors/bitcoind/sighash.json +3503 -0
  195. package/test/vectors/bitcoind/tx_invalid.json +883 -0
  196. package/test/vectors/bitcoind/tx_valid.json +1150 -0
  197. package/test/vectors/coolest-tx-ever-sent.json +8 -0
  198. package/test/vectors/ecdsa.json +300 -0
  199. package/test/vectors/hash.json +6133 -0
  200. package/test/vectors/kdf.json +47 -0
  201. package/test/vectors/largesttx.json +5 -0
  202. package/test/vectors/largesttxblock.json +6 -0
  203. package/test/vectors/sig.json +79 -0
  204. package/test/vectors/sighash-single-bug.json +8 -0
  205. package/test/version.js +81 -0
  206. package/test/workers-result.js +89 -0
  207. package/test/workers.js +91 -0
  208. package/test-index.js +3 -0
  209. package/test1.html +88 -0
  210. package/webpack.config.js +60 -0
  211. package/webpack.config.js_entry +57 -0
  212. package/yarn-error.log +6199 -0
package/test/bip-32.js ADDED
@@ -0,0 +1,721 @@
1
+ /* global describe,it */
2
+ 'use strict'
3
+ import { Bip32 } from '../lib/bip-32'
4
+ import { Base58Check } from '../lib/base-58-check'
5
+ import { PrivKey } from '../lib/priv-key'
6
+ import should from 'should'
7
+
8
+ describe('Bip32', function () {
9
+ it('should satisfy these basic API features', function () {
10
+ Bip32.fromRandom()
11
+ .toString()
12
+ .slice(0, 4)
13
+ .should.equal('xprv')
14
+ Bip32.fromRandom()
15
+ .toPublic()
16
+ .toString()
17
+ .slice(0, 4)
18
+ .should.equal('xpub')
19
+ Bip32.Testnet.fromRandom()
20
+ .toString()
21
+ .slice(0, 4)
22
+ .should.equal('tprv')
23
+ Bip32.Testnet.fromRandom()
24
+ .toPublic()
25
+ .toString()
26
+ .slice(0, 4)
27
+ .should.equal('tpub')
28
+ })
29
+
30
+ // test vectors: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
31
+ const vector1master = '000102030405060708090a0b0c0d0e0f'
32
+ const vector1mPublic =
33
+ 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8'
34
+ const vector1mPrivate =
35
+ 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
36
+ const vector1m0hPublic =
37
+ 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw'
38
+ const vector1m0hPrivate =
39
+ 'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7'
40
+ const vector1m0h1Public =
41
+ 'xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ'
42
+ const vector1m0h1Private =
43
+ 'xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs'
44
+ const vector1m0h12hPublic =
45
+ 'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5'
46
+ const vector1m0h12hPrivate =
47
+ 'xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM'
48
+ const vector1m0h12h2Public =
49
+ 'xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV'
50
+ const vector1m0h12h2Private =
51
+ 'xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334'
52
+ const vector1m0h12h21000000000Public =
53
+ 'xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy'
54
+ const vector1m0h12h21000000000Private =
55
+ 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76'
56
+ const vector2master =
57
+ 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542'
58
+ const vector2mPublic =
59
+ 'xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB'
60
+ const vector2mPrivate =
61
+ 'xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U'
62
+ const vector2m0Public =
63
+ 'xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH'
64
+ const vector2m0Private =
65
+ 'xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt'
66
+ const vector2m02147483647hPublic =
67
+ 'xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a'
68
+ const vector2m02147483647hPrivate =
69
+ 'xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9'
70
+ const vector2m02147483647h1Public =
71
+ 'xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon'
72
+ const vector2m02147483647h1Private =
73
+ 'xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef'
74
+ const vector2m02147483647h12147483646hPublic =
75
+ 'xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL'
76
+ const vector2m02147483647h12147483646hPrivate =
77
+ 'xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc'
78
+ const vector2m02147483647h12147483646h2Public =
79
+ 'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt'
80
+ const vector2m02147483647h12147483646h2Private =
81
+ 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j'
82
+
83
+ it('should make a new a bip32', function () {
84
+ let bip32
85
+ bip32 = new Bip32()
86
+ should.exist(bip32)
87
+ bip32 = new Bip32()
88
+ should.exist(bip32)
89
+ new Bip32()
90
+ .fromString(vector1mPrivate)
91
+ .toString()
92
+ .should.equal(vector1mPrivate)
93
+ new Bip32()
94
+ .fromString(vector1mPrivate)
95
+ .toString()
96
+ .should.equal(vector1mPrivate)
97
+ new Bip32()
98
+ .fromString(new Bip32().fromString(vector1mPrivate).toString())
99
+ .toString()
100
+ .should.equal(vector1mPrivate)
101
+ })
102
+
103
+ it('should initialize test vector 1 from the extended public key', function () {
104
+ const bip32 = new Bip32().fromString(vector1mPublic)
105
+ should.exist(bip32)
106
+ })
107
+
108
+ it('should initialize test vector 1 from the extended private key', function () {
109
+ const bip32 = new Bip32().fromString(vector1mPrivate)
110
+ should.exist(bip32)
111
+ })
112
+
113
+ it('should get the extended public key from the extended private key for test vector 1', function () {
114
+ const bip32 = new Bip32().fromString(vector1mPrivate)
115
+ bip32
116
+ .toPublic()
117
+ .toString()
118
+ .should.equal(vector1mPublic)
119
+ })
120
+
121
+ it("should get m/0' ext. private key from test vector 1", function () {
122
+ const bip32 = new Bip32().fromString(vector1mPrivate)
123
+ const child = bip32.derive("m/0'")
124
+ should.exist(child)
125
+ child.toString().should.equal(vector1m0hPrivate)
126
+ })
127
+
128
+ it("should asynchronously get m/0' ext. private key from test vector 1", async function () {
129
+ const bip32 = new Bip32().fromString(vector1mPrivate)
130
+ const child = await bip32.asyncDerive("m/0'")
131
+ should.exist(child)
132
+ child.toString().should.equal(vector1m0hPrivate)
133
+ })
134
+
135
+ it("should get m/0' ext. public key from test vector 1", function () {
136
+ const bip32 = new Bip32().fromString(vector1mPrivate)
137
+ const child = bip32.derive("m/0'")
138
+ should.exist(child)
139
+ child
140
+ .toPublic()
141
+ .toString()
142
+ .should.equal(vector1m0hPublic)
143
+ })
144
+
145
+ it("should get m/0'/1 ext. private key from test vector 1", function () {
146
+ const bip32 = new Bip32().fromString(vector1mPrivate)
147
+ const child = bip32.derive("m/0'/1")
148
+ should.exist(child)
149
+ child.toString().should.equal(vector1m0h1Private)
150
+ })
151
+
152
+ it("should get m/0'/1 ext. public key from test vector 1", function () {
153
+ const bip32 = new Bip32().fromString(vector1mPrivate)
154
+ const child = bip32.derive("m/0'/1")
155
+ should.exist(child)
156
+ child
157
+ .toPublic()
158
+ .toString()
159
+ .should.equal(vector1m0h1Public)
160
+ })
161
+
162
+ it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function () {
163
+ const bip32 = new Bip32().fromString(vector1mPrivate)
164
+ const child = bip32.derive("m/0'")
165
+ const childPub = new Bip32().fromString(child.toPublic().toString())
166
+ const child2 = childPub.derive('m/1')
167
+ should.exist(child2)
168
+ child2
169
+ .toPublic()
170
+ .toString()
171
+ .should.equal(vector1m0h1Public)
172
+ })
173
+
174
+ it("should asynchronously get m/0'/1 ext. public key from m/0' public key from test vector 1", async function () {
175
+ const bip32 = new Bip32().fromString(vector1mPrivate)
176
+ const child = bip32.derive("m/0'")
177
+ const childPub = new Bip32().fromString(child.toPublic().toString())
178
+ const child2 = await childPub.asyncDerive('m/1')
179
+ should.exist(child2)
180
+ child2
181
+ .toPublic()
182
+ .toString()
183
+ .should.equal(vector1m0h1Public)
184
+ })
185
+
186
+ it("should get m/0'/1/2h ext. private key from test vector 1", function () {
187
+ const bip32 = new Bip32().fromString(vector1mPrivate)
188
+ const child = bip32.derive("m/0'/1/2'")
189
+ should.exist(child)
190
+ child.toString().should.equal(vector1m0h12hPrivate)
191
+ })
192
+
193
+ it("should get m/0'/1/2h ext. public key from test vector 1", function () {
194
+ const bip32 = new Bip32().fromString(vector1mPrivate)
195
+ const child = bip32.derive("m/0'/1/2'")
196
+ should.exist(child)
197
+ child
198
+ .toPublic()
199
+ .toString()
200
+ .should.equal(vector1m0h12hPublic)
201
+ })
202
+
203
+ it("should get m/0'/1/2h/2 ext. private key from test vector 1", function () {
204
+ const bip32 = new Bip32().fromString(vector1mPrivate)
205
+ const child = bip32.derive("m/0'/1/2'/2")
206
+ should.exist(child)
207
+ child.toString().should.equal(vector1m0h12h2Private)
208
+ })
209
+
210
+ it("should get m/0'/1/2'/2 ext. public key from m/0'/1/2' public key from test vector 1", function () {
211
+ const bip32 = new Bip32().fromString(vector1mPrivate)
212
+ const child = bip32.derive("m/0'/1/2'")
213
+ const childPub = new Bip32().fromString(child.toPublic().toString())
214
+ const child2 = childPub.derive('m/2')
215
+ should.exist(child2)
216
+ child2
217
+ .toPublic()
218
+ .toString()
219
+ .should.equal(vector1m0h12h2Public)
220
+ })
221
+
222
+ it("should get m/0'/1/2h/2 ext. public key from test vector 1", function () {
223
+ const bip32 = new Bip32().fromString(vector1mPrivate)
224
+ const child = bip32.derive("m/0'/1/2'/2")
225
+ should.exist(child)
226
+ child
227
+ .toPublic()
228
+ .toString()
229
+ .should.equal(vector1m0h12h2Public)
230
+ })
231
+
232
+ it("should get m/0'/1/2h/2/1000000000 ext. private key from test vector 1", function () {
233
+ const bip32 = new Bip32().fromString(vector1mPrivate)
234
+ const child = bip32.derive("m/0'/1/2'/2/1000000000")
235
+ should.exist(child)
236
+ child.toString().should.equal(vector1m0h12h21000000000Private)
237
+ })
238
+
239
+ it("should get m/0'/1/2h/2/1000000000 ext. public key from test vector 1", function () {
240
+ const bip32 = new Bip32().fromString(vector1mPrivate)
241
+ const child = bip32.derive("m/0'/1/2'/2/1000000000")
242
+ should.exist(child)
243
+ child
244
+ .toPublic()
245
+ .toString()
246
+ .should.equal(vector1m0h12h21000000000Public)
247
+ })
248
+
249
+ 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 () {
250
+ const bip32 = new Bip32().fromString(vector1mPrivate)
251
+ const child = bip32.derive("m/0'/1/2'/2")
252
+ const childPub = new Bip32().fromString(child.toPublic().toString())
253
+ const child2 = childPub.derive('m/1000000000')
254
+ should.exist(child2)
255
+ child2
256
+ .toPublic()
257
+ .toString()
258
+ .should.equal(vector1m0h12h21000000000Public)
259
+ })
260
+
261
+ it('should initialize test vector 2 from the extended public key', function () {
262
+ const bip32 = new Bip32().fromString(vector2mPublic)
263
+ should.exist(bip32)
264
+ })
265
+
266
+ it('should initialize test vector 2 from the extended private key', function () {
267
+ const bip32 = new Bip32().fromString(vector2mPrivate)
268
+ should.exist(bip32)
269
+ })
270
+
271
+ it('should get the extended public key from the extended private key for test vector 2', function () {
272
+ const bip32 = new Bip32().fromString(vector2mPrivate)
273
+ bip32
274
+ .toPublic()
275
+ .toString()
276
+ .should.equal(vector2mPublic)
277
+ })
278
+
279
+ it('should get m/0 ext. private key from test vector 2', function () {
280
+ const bip32 = new Bip32().fromString(vector2mPrivate)
281
+ const child = bip32.derive('m/0')
282
+ should.exist(child)
283
+ child.toString().should.equal(vector2m0Private)
284
+ })
285
+
286
+ it('should get m/0 ext. public key from test vector 2', function () {
287
+ const bip32 = new Bip32().fromString(vector2mPrivate)
288
+ const child = bip32.derive('m/0')
289
+ should.exist(child)
290
+ child
291
+ .toPublic()
292
+ .toString()
293
+ .should.equal(vector2m0Public)
294
+ })
295
+
296
+ it('should get m/0 ext. public key from m public key from test vector 2', function () {
297
+ const bip32 = new Bip32().fromString(vector2mPrivate)
298
+ const child = bip32.derive('m')
299
+ const childPub = new Bip32().fromString(child.toPublic().toString())
300
+ const child2 = childPub.derive('m/0')
301
+ should.exist(child2)
302
+ child2
303
+ .toPublic()
304
+ .toString()
305
+ .should.equal(vector2m0Public)
306
+ })
307
+
308
+ it('should get m/0/2147483647h ext. private key from test vector 2', function () {
309
+ const bip32 = new Bip32().fromString(vector2mPrivate)
310
+ const child = bip32.derive("m/0/2147483647'")
311
+ should.exist(child)
312
+ child.toString().should.equal(vector2m02147483647hPrivate)
313
+ })
314
+
315
+ it('should get m/0/2147483647h ext. public key from test vector 2', function () {
316
+ const bip32 = new Bip32().fromString(vector2mPrivate)
317
+ const child = bip32.derive("m/0/2147483647'")
318
+ should.exist(child)
319
+ child
320
+ .toPublic()
321
+ .toString()
322
+ .should.equal(vector2m02147483647hPublic)
323
+ })
324
+
325
+ it('should get m/0/2147483647h/1 ext. private key from test vector 2', function () {
326
+ const bip32 = new Bip32().fromString(vector2mPrivate)
327
+ const child = bip32.derive("m/0/2147483647'/1")
328
+ should.exist(child)
329
+ child.toString().should.equal(vector2m02147483647h1Private)
330
+ })
331
+
332
+ it('should get m/0/2147483647h/1 ext. public key from test vector 2', function () {
333
+ const bip32 = new Bip32().fromString(vector2mPrivate)
334
+ const child = bip32.derive("m/0/2147483647'/1")
335
+ should.exist(child)
336
+ child
337
+ .toPublic()
338
+ .toString()
339
+ .should.equal(vector2m02147483647h1Public)
340
+ })
341
+
342
+ it('should get m/0/2147483647h/1 ext. public key from m/0/2147483647h public key from test vector 2', function () {
343
+ const bip32 = new Bip32().fromString(vector2mPrivate)
344
+ const child = bip32.derive("m/0/2147483647'")
345
+ const childPub = new Bip32().fromString(child.toPublic().toString())
346
+ const child2 = childPub.derive('m/1')
347
+ should.exist(child2)
348
+ child2
349
+ .toPublic()
350
+ .toString()
351
+ .should.equal(vector2m02147483647h1Public)
352
+ })
353
+
354
+ it('should get m/0/2147483647h/1/2147483646h ext. private key from test vector 2', function () {
355
+ const bip32 = new Bip32().fromString(vector2mPrivate)
356
+ const child = bip32.derive("m/0/2147483647'/1/2147483646'")
357
+ should.exist(child)
358
+ child.toString().should.equal(vector2m02147483647h12147483646hPrivate)
359
+ })
360
+
361
+ it('should get m/0/2147483647h/1/2147483646h ext. public key from test vector 2', function () {
362
+ const bip32 = new Bip32().fromString(vector2mPrivate)
363
+ const child = bip32.derive("m/0/2147483647'/1/2147483646'")
364
+ should.exist(child)
365
+ child
366
+ .toPublic()
367
+ .toString()
368
+ .should.equal(vector2m02147483647h12147483646hPublic)
369
+ })
370
+
371
+ it('should get m/0/2147483647h/1/2147483646h/2 ext. private key from test vector 2', function () {
372
+ const bip32 = new Bip32().fromString(vector2mPrivate)
373
+ const child = bip32.derive("m/0/2147483647'/1/2147483646'/2")
374
+ should.exist(child)
375
+ child.toString().should.equal(vector2m02147483647h12147483646h2Private)
376
+ })
377
+
378
+ it('should get m/0/2147483647h/1/2147483646h/2 ext. public key from test vector 2', function () {
379
+ const bip32 = new Bip32().fromString(vector2mPrivate)
380
+ const child = bip32.derive("m/0/2147483647'/1/2147483646'/2")
381
+ should.exist(child)
382
+ child
383
+ .toPublic()
384
+ .toString()
385
+ .should.equal(vector2m02147483647h12147483646h2Public)
386
+ })
387
+
388
+ it('should get m/0/2147483647h/1/2147483646h/2 ext. public key from m/0/2147483647h/2147483646h public key from test vector 2', function () {
389
+ const bip32 = new Bip32().fromString(vector2mPrivate)
390
+ const child = bip32.derive("m/0/2147483647'/1/2147483646'")
391
+ const childPub = new Bip32().fromString(child.toPublic().toString())
392
+ const child2 = childPub.derive('m/2')
393
+ should.exist(child2)
394
+ child2
395
+ .toPublic()
396
+ .toString()
397
+ .should.equal(vector2m02147483647h12147483646h2Public)
398
+ })
399
+
400
+ describe('testnet', function () {
401
+ it('should initialize a new Bip32 correctly from a random Bip32', function () {
402
+ const b1 = new Bip32.Testnet()
403
+ b1.fromRandom()
404
+ ;(b1.privKey instanceof PrivKey.Testnet).should.equal(true)
405
+ const b2 = new Bip32.Testnet().fromString(b1.toPublic().toString())
406
+ b2
407
+ .toPublic()
408
+ .toString()
409
+ .should.equal(b1.toPublic().toString())
410
+ })
411
+
412
+ it('should generate valid ext pub key for testnet', function () {
413
+ const b = new Bip32.Testnet()
414
+ b.fromRandom()
415
+ ;(b.privKey instanceof PrivKey.Testnet).should.equal(true)
416
+ b
417
+ .toPublic()
418
+ .toString()
419
+ .substring(0, 4)
420
+ .should.equal('tpub')
421
+ })
422
+ })
423
+
424
+ describe('#fromObject', function () {
425
+ it('should set this bip32', function () {
426
+ const bip32 = new Bip32().fromString(vector1mPrivate)
427
+ const bip322 = new Bip32().fromObject({
428
+ versionBytesNum: bip32.versionBytesNum,
429
+ depth: bip32.depth,
430
+ parentFingerPrint: bip32.parentFingerPrint,
431
+ childIndex: bip32.childIndex,
432
+ chainCode: bip32.chainCode,
433
+ privKey: bip32.privKey,
434
+ pubKey: bip32.pubKey,
435
+ hasPrivKey: bip32.hasPrivKey
436
+ })
437
+ bip322.toString().should.equal(bip32.toString())
438
+ bip322
439
+ .fromObject({})
440
+ .toString()
441
+ .should.equal(bip32.toString())
442
+ })
443
+ })
444
+
445
+ describe('#fromRandom', function () {
446
+ it('should not return the same one twice', function () {
447
+ const bip32a = new Bip32().fromRandom()
448
+ const bip32b = new Bip32().fromRandom()
449
+ bip32a.toString().should.not.equal(bip32b.toString())
450
+ })
451
+ })
452
+
453
+ describe('@fromRandom', function () {
454
+ it('should not return the same one twice', function () {
455
+ const bip32a = Bip32.fromRandom()
456
+ const bip32b = Bip32.fromRandom()
457
+ bip32a.toString().should.not.equal(bip32b.toString())
458
+ })
459
+ })
460
+
461
+ describe('#fromSeed', function () {
462
+ it('should initialize a new Bip32 correctly from test vector 1 seed', function () {
463
+ const hex = vector1master
464
+ const bip32 = new Bip32().fromSeed(Buffer.from(hex, 'hex'), 'mainnet')
465
+ should.exist(bip32)
466
+ bip32.toString().should.equal(vector1mPrivate)
467
+ bip32
468
+ .toPublic()
469
+ .toString()
470
+ .should.equal(vector1mPublic)
471
+ })
472
+
473
+ it('should initialize a new Bip32 correctly from test vector 2 seed', function () {
474
+ const hex = vector2master
475
+ const bip32 = new Bip32().fromSeed(Buffer.from(hex, 'hex'), 'mainnet')
476
+ should.exist(bip32)
477
+ bip32.toString().should.equal(vector2mPrivate)
478
+ bip32
479
+ .toPublic()
480
+ .toString()
481
+ .should.equal(vector2mPublic)
482
+ })
483
+ })
484
+
485
+ describe('@fromSeed', function () {
486
+ it('should initialize a new Bip32 correctly from test vector 1 seed', function () {
487
+ const hex = vector1master
488
+ const bip32 = Bip32.fromSeed(Buffer.from(hex, 'hex'), 'mainnet')
489
+ should.exist(bip32)
490
+ bip32.toString().should.equal(vector1mPrivate)
491
+ bip32
492
+ .toPublic()
493
+ .toString()
494
+ .should.equal(vector1mPublic)
495
+ })
496
+
497
+ it('should initialize a new Bip32 correctly from test vector 2 seed', function () {
498
+ const hex = vector2master
499
+ const bip32 = Bip32.fromSeed(Buffer.from(hex, 'hex'), 'mainnet')
500
+ should.exist(bip32)
501
+ bip32.toString().should.equal(vector2mPrivate)
502
+ bip32
503
+ .toPublic()
504
+ .toString()
505
+ .should.equal(vector2mPublic)
506
+ })
507
+ })
508
+
509
+ describe('#asyncFromSeed', function () {
510
+ it('should initialize a new Bip32 correctly from test vector 1 seed', async function () {
511
+ const hex = vector1master
512
+ const bip32 = await new Bip32().asyncFromSeed(
513
+ Buffer.from(hex, 'hex'),
514
+ 'mainnet'
515
+ )
516
+ should.exist(bip32)
517
+ bip32.toString().should.equal(vector1mPrivate)
518
+ bip32
519
+ .toPublic()
520
+ .toString()
521
+ .should.equal(vector1mPublic)
522
+ })
523
+
524
+ it('should initialize a new Bip32 correctly from test vector 2 seed', async function () {
525
+ const hex = vector2master
526
+ const bip32 = await new Bip32().asyncFromSeed(
527
+ Buffer.from(hex, 'hex'),
528
+ 'mainnet'
529
+ )
530
+ should.exist(bip32)
531
+ bip32.toString().should.equal(vector2mPrivate)
532
+ bip32
533
+ .toPublic()
534
+ .toString()
535
+ .should.equal(vector2mPublic)
536
+ })
537
+ })
538
+
539
+ describe('@asyncFromSeed', function () {
540
+ it('should initialize a new Bip32 correctly from test vector 1 seed', async function () {
541
+ const hex = vector1master
542
+ const bip32 = await Bip32.asyncFromSeed(Buffer.from(hex, 'hex'), 'mainnet')
543
+ should.exist(bip32)
544
+ bip32.toString().should.equal(vector1mPrivate)
545
+ bip32
546
+ .toPublic()
547
+ .toString()
548
+ .should.equal(vector1mPublic)
549
+ })
550
+
551
+ it('should initialize a new Bip32 correctly from test vector 2 seed', async function () {
552
+ const hex = vector2master
553
+ const bip32 = await Bip32.asyncFromSeed(Buffer.from(hex, 'hex'), 'mainnet')
554
+ should.exist(bip32)
555
+ bip32.toString().should.equal(vector2mPrivate)
556
+ bip32
557
+ .toPublic()
558
+ .toString()
559
+ .should.equal(vector2mPublic)
560
+ })
561
+ })
562
+
563
+ describe('#fromHex', function () {
564
+ it('should make a bip32 from a hex string', function () {
565
+ const str =
566
+ 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
567
+ const buf = Base58Check.decode(str)
568
+ const hex = buf.toString('hex')
569
+ let bip32 = new Bip32().fromHex(hex)
570
+ should.exist(bip32)
571
+ bip32.toString().should.equal(str)
572
+ bip32 = bip32.toPublic()
573
+ const xpub = bip32.toString()
574
+ bip32 = new Bip32().fromHex(bip32.toHex())
575
+ bip32.toString().should.equal(xpub)
576
+ })
577
+ })
578
+
579
+ describe('#fromBuffer', function () {
580
+ it('should make a bip32 from a buffer', function () {
581
+ const str =
582
+ 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
583
+ const buf = Base58Check.decode(str)
584
+ let bip32 = new Bip32().fromBuffer(buf)
585
+ should.exist(bip32)
586
+ bip32.toString().should.equal(str)
587
+ bip32 = bip32.toPublic()
588
+ const xpub = bip32.toString()
589
+ bip32 = new Bip32().fromBuffer(bip32.toBuffer())
590
+ bip32.toString().should.equal(xpub)
591
+ })
592
+ })
593
+
594
+ describe('#toHex', function () {
595
+ it('should return a bip32 hex string', function () {
596
+ const str =
597
+ 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
598
+ const hex = Base58Check.decode(str).toString('hex')
599
+ const bip32 = new Bip32().fromString(str)
600
+ bip32.toHex().should.equal(hex)
601
+ })
602
+ })
603
+
604
+ describe('#toBuffer', function () {
605
+ it('should return a bip32 buffer', function () {
606
+ const str =
607
+ 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
608
+ const buf = Base58Check.decode(str)
609
+ const bip32 = new Bip32().fromString(str)
610
+ bip32
611
+ .toBuffer()
612
+ .toString('hex')
613
+ .should.equal(buf.toString('hex'))
614
+ })
615
+ })
616
+
617
+ describe('#fromString', function () {
618
+ it('should make a bip32 from a string', function () {
619
+ const str =
620
+ 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
621
+ const bip32 = new Bip32().fromString(str)
622
+ should.exist(bip32)
623
+ bip32.toString().should.equal(str)
624
+ })
625
+ })
626
+
627
+ describe('#asyncFromString', function () {
628
+ it('should make a bip32 from a string asynchronously', async function () {
629
+ const str =
630
+ 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
631
+ const bip32 = new Bip32().fromString(str)
632
+ should.exist(bip32)
633
+ const bip32b = await new Bip32().asyncFromString(str)
634
+ bip32.toString().should.equal(str)
635
+ bip32.toString().should.equal(bip32b.toString())
636
+ })
637
+ })
638
+
639
+ describe('#toString', function () {
640
+ const bip32 = new Bip32()
641
+ bip32.fromRandom()
642
+ const tip32 = new Bip32.Testnet()
643
+ tip32.fromRandom()
644
+
645
+ it('should return an xprv string', function () {
646
+ bip32
647
+ .toString()
648
+ .slice(0, 4)
649
+ .should.equal('xprv')
650
+ })
651
+
652
+ it('should return an xpub string', function () {
653
+ bip32
654
+ .toPublic()
655
+ .toString()
656
+ .slice(0, 4)
657
+ .should.equal('xpub')
658
+ })
659
+
660
+ it('should return a tprv string', function () {
661
+ tip32
662
+ .toString()
663
+ .slice(0, 4)
664
+ .should.equal('tprv')
665
+ ;(tip32.privKey instanceof PrivKey.Testnet).should.equal(true)
666
+ })
667
+
668
+ it('should return a tpub string', function () {
669
+ tip32
670
+ .toPublic()
671
+ .toString()
672
+ .slice(0, 4)
673
+ .should.equal('tpub')
674
+ })
675
+ })
676
+
677
+ describe('#asyncToString', function () {
678
+ it('should convert to a string same as toString', async function () {
679
+ const bip32 = new Bip32().fromRandom()
680
+ const str1 = bip32.toString()
681
+ const str2 = await bip32.asyncToString()
682
+ str1.should.equal(str2)
683
+ })
684
+ })
685
+
686
+ describe('#toJSON', function () {
687
+ it('should be the same as toFastHex', function () {
688
+ const bip32 = Bip32.fromRandom()
689
+ bip32.toJSON().should.equal(bip32.toFastHex())
690
+ })
691
+ })
692
+
693
+ describe('#fromJSON', function () {
694
+ it('should be the same as fromFastHex', function () {
695
+ const bip32 = Bip32.fromRandom()
696
+ const hex = bip32.toHex()
697
+ const bip32a = new Bip32().fromJSON(hex)
698
+ const bip32b = new Bip32().fromFastHex(hex)
699
+ bip32a.toString().should.equal(bip32b.toString())
700
+ })
701
+ })
702
+
703
+ describe('@fromJSON', function () {
704
+ it('should be the same as fromFastHex', function () {
705
+ const bip32 = Bip32.fromRandom()
706
+ const hex = bip32.toHex()
707
+ const bip32a = Bip32.fromJSON(hex)
708
+ const bip32b = Bip32.fromFastHex(hex)
709
+ bip32a.toString().should.equal(bip32b.toString())
710
+ })
711
+ })
712
+
713
+ describe('#isPrivate', function () {
714
+ it('should know if this bip32 is private', function () {
715
+ const bip32priv = new Bip32().fromRandom()
716
+ const bip32pub = bip32priv.toPublic()
717
+ bip32priv.isPrivate().should.equal(true)
718
+ bip32pub.isPrivate().should.equal(false)
719
+ })
720
+ })
721
+ })