@smartledger/bsv 8.3.1 → 9.1.0

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 (169) hide show
  1. package/CHANGELOG.md +228 -0
  2. package/README.md +152 -1031
  3. package/STABILITY.md +134 -0
  4. package/bsv-gdaf.min.js +58 -53
  5. package/bsv-ltp.min.js +23 -18
  6. package/bsv-script-helper.min.js +1 -1
  7. package/bsv-smartcontract.min.js +21 -21
  8. package/bsv.bundle.js +58 -53
  9. package/bsv.d.ts +0 -4
  10. package/bsv.min.js +58 -53
  11. package/docs/BRC220_BATCH_LEAF_AMENDMENT.md +82 -7
  12. package/docs/MODULE_REFERENCE_COMPLETE.md +27 -27
  13. package/docs/README.md +2 -1
  14. package/docs/THREAT_MODEL.md +10 -2
  15. package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
  16. package/docs/getting-started/INSTALLATION.md +23 -23
  17. package/docs/getting-started/QUICK_START.md +7 -7
  18. package/docs/migration/FROM_BSV_1_5_6.md +5 -5
  19. package/docs/proposals/10.0.0-package-split.md +131 -0
  20. package/index.js +15 -26
  21. package/lib/block/merkleblock.js +19 -5
  22. package/lib/covenant/helpers.js +34 -35
  23. package/lib/covenant/pushtx.js +2 -2
  24. package/lib/custom-script-helper.js +0 -16
  25. package/lib/hdprivatekey.js +15 -0
  26. package/lib/ordinals/ordlock.js +1 -1
  27. package/lib/script/interpreter.js +109 -1
  28. package/lib/smart_contract/builder.js +14 -0
  29. package/lib/smart_contract/debugger.js +2 -2
  30. package/lib/smart_contract/dsl.js +2 -2
  31. package/lib/smart_contract/index.js +0 -3
  32. package/lib/smart_contract/locks.js +1 -41
  33. package/lib/smart_contract/pels.js +1 -1
  34. package/lib/smart_contract/token.js +1 -1
  35. package/lib/util/deprecate.js +159 -0
  36. package/package.json +19 -82
  37. package/tools/gen-brc220-batch-vector.js +205 -0
  38. package/version.js +1 -1
  39. package/.mocharc.json +0 -5
  40. package/test/address.js +0 -629
  41. package/test/block/block.js +0 -239
  42. package/test/block/blockheader.js +0 -270
  43. package/test/block/merkleblock.js +0 -207
  44. package/test/build/bundle_crypto_shim.js +0 -104
  45. package/test/build/bundle_externals.js +0 -125
  46. package/test/build/bundle_smoke.js +0 -55
  47. package/test/build/esbuild_main.js +0 -65
  48. package/test/build/esm_wrapper.js +0 -73
  49. package/test/build/exports_resolution.js +0 -84
  50. package/test/build/version_sync.js +0 -14
  51. package/test/cli/smoke.js +0 -261
  52. package/test/consensus/base58-vectors.js +0 -120
  53. package/test/consensus/sv-script-vectors.js +0 -81
  54. package/test/consensus/sv-sighash-vectors.js +0 -51
  55. package/test/consensus/sv-tx-vectors.js +0 -48
  56. package/test/credentials-test.js +0 -332
  57. package/test/crypto/backend_selection.js +0 -126
  58. package/test/crypto/bn.js +0 -168
  59. package/test/crypto/ecdsa.js +0 -426
  60. package/test/crypto/elliptic-fixed.js +0 -61
  61. package/test/crypto/hash.browser.js +0 -121
  62. package/test/crypto/hash.js +0 -122
  63. package/test/crypto/point.js +0 -193
  64. package/test/crypto/random.js +0 -105
  65. package/test/crypto/security.js +0 -176
  66. package/test/crypto/shamir.js +0 -177
  67. package/test/crypto/shamir_rngcap.js +0 -29
  68. package/test/crypto/signature.js +0 -399
  69. package/test/data/bip69.json +0 -215
  70. package/test/data/bitcoin-sv/README.md +0 -170
  71. package/test/data/bitcoin-sv/base58_encode_decode.json +0 -14
  72. package/test/data/bitcoin-sv/base58_keys_invalid.json +0 -152
  73. package/test/data/bitcoin-sv/base58_keys_valid.json +0 -452
  74. package/test/data/bitcoin-sv/script_tests.json +0 -2591
  75. package/test/data/bitcoin-sv/sighash.json +0 -1003
  76. package/test/data/bitcoin-sv/tx_invalid.json +0 -285
  77. package/test/data/bitcoin-sv/tx_valid.json +0 -367
  78. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  79. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  80. package/test/data/bitcoind/blocks.json +0 -27
  81. package/test/data/bitcoind/script_tests.json +0 -2244
  82. package/test/data/bitcoind/sig_canonical.json +0 -7
  83. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  84. package/test/data/bitcoind/tx_invalid.json +0 -177
  85. package/test/data/bitcoind/tx_valid.json +0 -224
  86. package/test/data/blk86756-testnet.dat +0 -0
  87. package/test/data/blk86756-testnet.js +0 -12
  88. package/test/data/blk86756-testnet.json +0 -684
  89. package/test/data/ecdsa.json +0 -230
  90. package/test/data/merkleblocks.js +0 -486
  91. package/test/data/messages.json +0 -22
  92. package/test/data/sighash.json +0 -1004
  93. package/test/data/tx_creation.json +0 -85
  94. package/test/didweb/relationships.js +0 -144
  95. package/test/ecies/bitcore-ecies.js +0 -178
  96. package/test/ecies/electrum-ecies.js +0 -206
  97. package/test/encoding/base58.js +0 -131
  98. package/test/encoding/base58check.js +0 -145
  99. package/test/encoding/bufferreader.js +0 -328
  100. package/test/encoding/bufferwriter.js +0 -160
  101. package/test/encoding/varint.js +0 -104
  102. package/test/gdaf/anchor_no_key_leak.js +0 -146
  103. package/test/gdaf/anchor_spv.js +0 -163
  104. package/test/gdaf/canonicalization.js +0 -106
  105. package/test/gdaf/canonicalize.js +0 -140
  106. package/test/gdaf/zk_prover.js +0 -204
  107. package/test/hdkeys.js +0 -365
  108. package/test/hdprivatekey.js +0 -339
  109. package/test/hdpublickey.js +0 -293
  110. package/test/index.js +0 -16
  111. package/test/ltp/ids.js +0 -74
  112. package/test/ltp/right.js +0 -198
  113. package/test/ltp/verify_failclosed.js +0 -134
  114. package/test/message/message.js +0 -190
  115. package/test/mnemonic/data/fixtures.json +0 -300
  116. package/test/mnemonic/mnemonic.js +0 -277
  117. package/test/mnemonic/mocha.opts +0 -1
  118. package/test/mnemonic/pbkdf2.test.js +0 -43
  119. package/test/networks.js +0 -207
  120. package/test/notaryhash/batch_leaf.js +0 -140
  121. package/test/notaryhash/certificate.js +0 -249
  122. package/test/notaryhash/encoding.js +0 -186
  123. package/test/notaryhash/interop.js +0 -112
  124. package/test/notaryhash/merkle.js +0 -181
  125. package/test/notaryhash/script.js +0 -270
  126. package/test/notaryhash/verify.js +0 -342
  127. package/test/opcode.js +0 -186
  128. package/test/ordinals/bsv20.js +0 -337
  129. package/test/ordinals/inscription.js +0 -319
  130. package/test/ordinals/ordlock.js +0 -613
  131. package/test/privatekey.js +0 -540
  132. package/test/publickey.js +0 -411
  133. package/test/regressions.js +0 -215
  134. package/test/script/chronicle.js +0 -543
  135. package/test/script/defaults.js +0 -160
  136. package/test/script/genesis_limits.js +0 -203
  137. package/test/script/interpreter.js +0 -776
  138. package/test/script/script.js +0 -1259
  139. package/test/script/string_ops.js +0 -88
  140. package/test/security/fail_closed_contracts.js +0 -104
  141. package/test/security/threat_model_coverage.js +0 -31
  142. package/test/smart_contract/covenants.js +0 -245
  143. package/test/smart_contract/dsl_debugger.js +0 -101
  144. package/test/smart_contract/extract_field.js +0 -61
  145. package/test/smart_contract/nonenforcing_guard.js +0 -44
  146. package/test/smart_contract/ordinal_transfer.js +0 -81
  147. package/test/smart_contract/preimage.js +0 -98
  148. package/test/smart_contract/sighash_marketplace.js +0 -108
  149. package/test/smart_contract/token_generalized.js +0 -185
  150. package/test/spv/headerchain.js +0 -86
  151. package/test/spv/merkleproof.js +0 -133
  152. package/test/statuslist/failclosed.js +0 -78
  153. package/test/transaction/deserialize.js +0 -33
  154. package/test/transaction/input/input.js +0 -92
  155. package/test/transaction/input/multisig.js +0 -174
  156. package/test/transaction/input/multisigscripthash.js +0 -111
  157. package/test/transaction/input/publickey.js +0 -68
  158. package/test/transaction/input/publickeyhash.js +0 -59
  159. package/test/transaction/output.js +0 -185
  160. package/test/transaction/sighash.js +0 -91
  161. package/test/transaction/signature.js +0 -127
  162. package/test/transaction/transaction.js +0 -1299
  163. package/test/transaction/unspentoutput.js +0 -97
  164. package/test/types/dts_drift.js +0 -124
  165. package/test/types/surface_honesty.js +0 -102
  166. package/test/util/id.js +0 -72
  167. package/test/util/js.js +0 -76
  168. package/test/util/preconditions.js +0 -79
  169. package/test/vcjwt/interop.js +0 -126
@@ -1,339 +0,0 @@
1
- 'use strict'
2
-
3
- var _ = require('../lib/util/_')
4
- var assert = require('assert')
5
- var should = require('chai').should()
6
- var expect = require('chai').expect
7
- var bsv = require('..')
8
- var errors = bsv.errors
9
- var hdErrors = errors.HDPrivateKey
10
- var buffer = require('buffer')
11
- var Networks = bsv.Networks
12
- var JSUtil = require('../lib/util/js')
13
- var HDPrivateKey = bsv.HDPrivateKey
14
- var Base58Check = bsv.encoding.Base58Check
15
-
16
- var xprivkey = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
17
- var json = '{"network":"livenet","depth":0,"fingerPrint":876747070,"parentFingerPrint":0,"childIndex":0,"chainCode":"873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508","privateKey":"e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35","checksum":3883834737,"xprivkey":"xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"}'
18
- describe('HDPrivate key interface', function () {
19
- var expectFail = function (func, error) {
20
- var got = null
21
- try {
22
- func()
23
- } catch (e) {
24
- got = e instanceof error
25
- }
26
- expect(got).to.equal(true)
27
- }
28
-
29
- var expectDerivationFail = function (argument, error) {
30
- return expectFail(function () {
31
- var privateKey = new HDPrivateKey(xprivkey)
32
- privateKey.deriveChild(argument)
33
- }, error)
34
- }
35
-
36
- var expectFailBuilding = function (argument, error) {
37
- return expectFail(function () {
38
- return new HDPrivateKey(argument)
39
- }, error)
40
- }
41
-
42
- var expectSeedFail = function (argument, error) {
43
- return expectFail(function () {
44
- return HDPrivateKey.fromSeed(argument)
45
- }, error)
46
- }
47
-
48
- it('should make a new private key from random', function () {
49
- should.exist(new HDPrivateKey().xprivkey)
50
- })
51
-
52
- it('should make a new private key from random for testnet', function () {
53
- var key = new HDPrivateKey('testnet')
54
- should.exist(key.xprivkey)
55
- key.network.name.should.equal('testnet')
56
- })
57
-
58
- it('should not be able to change read-only properties', function () {
59
- var hdkey = new HDPrivateKey()
60
- expect(function () {
61
- hdkey.fingerPrint = 'notafingerprint'
62
- }).to.throw(TypeError)
63
- })
64
-
65
- it('should error with an invalid checksum', function () {
66
- expectFailBuilding(xprivkey + '1', errors.InvalidB58Checksum)
67
- })
68
-
69
- it('can be rebuilt from a json generated by itself', function () {
70
- var regenerate = new HDPrivateKey(json)
71
- regenerate.xprivkey.should.equal(xprivkey)
72
- })
73
-
74
- it('builds a json keeping the structure and same members', function () {
75
- assert.deepStrictEqual(
76
- new HDPrivateKey(json).toJSON(),
77
- new HDPrivateKey(xprivkey).toJSON()
78
- )
79
- })
80
-
81
- describe('instantiation', function () {
82
- it('invalid argument: can not instantiate from a number', function () {
83
- expectFailBuilding(1, hdErrors.UnrecognizedArgument)
84
- })
85
- it('allows no-new calling', function () {
86
- HDPrivateKey(xprivkey).toString().should.equal(xprivkey)
87
- })
88
- it('allows the use of a copy constructor', function () {
89
- HDPrivateKey(HDPrivateKey(xprivkey))
90
- .xprivkey.should.equal(xprivkey)
91
- })
92
- })
93
-
94
- describe('public key', function () {
95
- var testnetKey = new HDPrivateKey('tprv8ZgxMBicQKsPdEeU2KiGFnUgRGriMnQxrwrg6FWCBg4jeiidHRyCCdA357kfkZiGaXEapWZsGDKikeeEbvgXo3UmEdbEKNdQH9VXESmGuUK')
96
- var livenetKey = new HDPrivateKey('xprv9s21ZrQH143K3e39bnn1vyS7YFa1EAJAFGDoeHaSBsgBxgAkTEXeSx7xLvhNQNJxJwhzziWcK3znUFKRPRwWBPkKZ8ijUBa5YYpYPQmeBDX')
97
-
98
- it('matches the network', function () {
99
- testnetKey.publicKey.network.should.equal(Networks.testnet)
100
- livenetKey.publicKey.network.should.equal(Networks.livenet)
101
- })
102
-
103
- it('cache for xpubkey works', function () {
104
- var privateKey = new HDPrivateKey(xprivkey)
105
- should.not.exist(privateKey._hdPublicKey)
106
- privateKey.xpubkey.should.equal(privateKey.xpubkey)
107
- should.exist(privateKey._hdPublicKey)
108
- })
109
- })
110
-
111
- it('inspect() displays correctly', function () {
112
- HDPrivateKey(xprivkey).inspect().should.equal('<HDPrivateKey: ' + xprivkey + '>')
113
- })
114
- it('fails when trying to derive with an invalid argument', function () {
115
- expectDerivationFail([], hdErrors.InvalidDerivationArgument)
116
- })
117
-
118
- it('catches early invalid paths', function () {
119
- expectDerivationFail('s', hdErrors.InvalidPath)
120
- })
121
-
122
- it('allows derivation of hardened keys by passing a very big number', function () {
123
- var privateKey = new HDPrivateKey(xprivkey)
124
- var derivedByNumber = privateKey.deriveChild(0x80000000)
125
- var derivedByArgument = privateKey.deriveChild(0, true)
126
- derivedByNumber.xprivkey.should.equal(derivedByArgument.xprivkey)
127
- })
128
-
129
- it('returns itself with \'m\' parameter', function () {
130
- var privateKey = new HDPrivateKey(xprivkey)
131
- privateKey.should.equal(privateKey.deriveChild('m'))
132
- })
133
-
134
- it('returns InvalidArgument if invalid data is given to getSerializedError', function () {
135
- expect(
136
- HDPrivateKey.getSerializedError(1) instanceof hdErrors.UnrecognizedArgument
137
- ).to.equal(true)
138
- })
139
-
140
- it('returns InvalidLength if data of invalid length is given to getSerializedError', function () {
141
- var b58s = Base58Check.encode(buffer.Buffer.from('onestring'))
142
- expect(
143
- HDPrivateKey.getSerializedError(b58s) instanceof hdErrors.InvalidLength
144
- ).to.equal(true)
145
- })
146
-
147
- it('returns InvalidNetworkArgument if an invalid network is provided', function () {
148
- expect(
149
- HDPrivateKey.getSerializedError(xprivkey, 'invalidNetwork') instanceof errors.InvalidNetworkArgument
150
- ).to.equal(true)
151
- })
152
-
153
- it('recognizes that the wrong network was asked for', function () {
154
- expect(
155
- HDPrivateKey.getSerializedError(xprivkey, 'testnet') instanceof errors.InvalidNetwork
156
- ).to.equal(true)
157
- })
158
-
159
- it('recognizes the correct network', function () {
160
- expect(HDPrivateKey.getSerializedError(xprivkey, 'livenet')).to.equal(null)
161
- })
162
-
163
- describe('on creation from seed', function () {
164
- it('converts correctly from an hexa string', function () {
165
- should.exist(HDPrivateKey.fromSeed('01234567890abcdef01234567890abcdef').xprivkey)
166
- })
167
- it('fails when argument is not a buffer or string', function () {
168
- expectSeedFail(1, hdErrors.InvalidEntropyArgument)
169
- })
170
- it('fails when argument doesn\'t provide enough entropy', function () {
171
- expectSeedFail('01', hdErrors.InvalidEntropyArgument.NotEnoughEntropy)
172
- })
173
- it('fails when argument provides too much entropy', function () {
174
- var entropy = '0'
175
- for (var i = 0; i < 129; i++) {
176
- entropy += '1'
177
- }
178
- expectSeedFail(entropy, hdErrors.InvalidEntropyArgument.TooMuchEntropy)
179
- })
180
- })
181
-
182
- it('correctly errors if an invalid checksum is provided', function () {
183
- var privKey = new HDPrivateKey(xprivkey)
184
- var error = null
185
- try {
186
- var buffers = privKey._buffers
187
- buffers.checksum = JSUtil.integerAsBuffer(0)
188
- new HDPrivateKey(buffers) //eslint-disable-line
189
- } catch (e) {
190
- error = e
191
- }
192
- expect(error instanceof errors.InvalidB58Checksum).to.equal(true)
193
- })
194
- it('correctly validates the checksum', function () {
195
- var privKey = new HDPrivateKey(xprivkey)
196
- expect(function () {
197
- var buffers = privKey._buffers
198
- return new HDPrivateKey(buffers)
199
- }).to.not.throw()
200
- })
201
-
202
- it('shouldn\'t matter if derivations are made with strings or numbers', function () {
203
- var privateKey = new HDPrivateKey(xprivkey)
204
- var derivedByString = privateKey.deriveChild('m/0\'/1/2\'')
205
- var derivedByNumber = privateKey.deriveChild(0, true).deriveChild(1).deriveChild(2, true)
206
- derivedByNumber.xprivkey.should.equal(derivedByString.xprivkey)
207
- })
208
-
209
- describe('validates paths', function () {
210
- it('validates correct paths', function () {
211
- var valid
212
-
213
- valid = HDPrivateKey.isValidPath('m/0\'/1/2\'')
214
- valid.should.equal(true)
215
-
216
- valid = HDPrivateKey.isValidPath('m')
217
- valid.should.equal(true)
218
-
219
- valid = HDPrivateKey.isValidPath(123, true)
220
- valid.should.equal(true)
221
-
222
- valid = HDPrivateKey.isValidPath(123)
223
- valid.should.equal(true)
224
-
225
- valid = HDPrivateKey.isValidPath(HDPrivateKey.Hardened + 123)
226
- valid.should.equal(true)
227
-
228
- valid = HDPrivateKey.isValidPath(HDPrivateKey.Hardened + 123, true)
229
- valid.should.equal(true)
230
- })
231
-
232
- var invalid = [
233
- 'm/-1/12',
234
- 'bad path',
235
- 'K',
236
- 'm/',
237
- 'm/12asd',
238
- 'm/1/2//3'
239
- ]
240
-
241
- invalid.forEach(function (datum) {
242
- it('rejects illegal path ' + datum, function () {
243
- HDPrivateKey.isValidPath(datum).should.equal(false)
244
- expect(HDPrivateKey._getDerivationIndexes(datum)).to.equal(null)
245
- })
246
- })
247
-
248
- it('generates deriving indexes correctly', function () {
249
- var indexes
250
-
251
- indexes = HDPrivateKey._getDerivationIndexes('m/-1/12')
252
- expect(indexes).to.equal(null)
253
-
254
- indexes = HDPrivateKey._getDerivationIndexes('m/0/12/12\'')
255
- indexes.should.eql([0, 12, HDPrivateKey.Hardened + 12])
256
-
257
- indexes = HDPrivateKey._getDerivationIndexes('m/0/12/12\'')
258
- indexes.should.eql([0, 12, HDPrivateKey.Hardened + 12])
259
- })
260
- })
261
-
262
- describe('conversion to/from buffer', function () {
263
- var str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
264
- it('should roundtrip to/from a buffer', function () {
265
- var priv = new HDPrivateKey(str)
266
- var toBuffer = priv.toBuffer()
267
- var fromBuffer = HDPrivateKey.fromBuffer(toBuffer)
268
- var roundTrip = new HDPrivateKey(fromBuffer.toString())
269
- roundTrip.xprivkey.should.equal(str)
270
- })
271
- })
272
-
273
- describe('conversion to/from hex', function () {
274
- var str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
275
- it('should roundtrip to/from a buffer', function () {
276
- var priv = new HDPrivateKey(str)
277
- var toBuffer = priv.toBuffer()
278
- var fromBuffer = HDPrivateKey.fromBuffer(toBuffer)
279
- var roundTrip = new HDPrivateKey(fromBuffer.toString())
280
- roundTrip.xprivkey.should.equal(str)
281
- })
282
- })
283
-
284
- describe('from random', function () {
285
- var str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
286
- it('should roundtrip to/from a buffer', function () {
287
- var xprv1 = new HDPrivateKey(str)
288
- var xprv2 = HDPrivateKey.fromRandom()
289
- var xprv3 = HDPrivateKey.fromRandom()
290
- xprv1.toString().should.not.equal(xprv2.toString())
291
- xprv2.toString().should.not.equal(xprv3.toString())
292
- xprv1.toString().should.not.equal(xprv3.toString())
293
- })
294
-
295
- // Regression: fromRandom used to drop its argument and always return a
296
- // mainnet key, so fromRandom('testnet') silently produced an xprv.
297
- it('honours the network argument', function () {
298
- HDPrivateKey.fromRandom('testnet').network.name.should.equal('testnet')
299
- HDPrivateKey.fromRandom('testnet').toString().slice(0, 4).should.equal('tprv')
300
- HDPrivateKey.fromRandom('livenet').network.name.should.equal('livenet')
301
- HDPrivateKey.fromRandom().network.name.should.equal('livenet') // default unchanged
302
- })
303
- })
304
-
305
- describe('conversion to plain object/json', function () {
306
- var plainObject = {
307
- 'network': 'livenet',
308
- 'depth': 0,
309
- 'fingerPrint': 876747070,
310
- 'parentFingerPrint': 0,
311
- 'childIndex': 0,
312
- 'chainCode': '873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508',
313
- 'privateKey': 'e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35',
314
- 'checksum': 3883834737,
315
- 'xprivkey': 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvN' +
316
- 'KmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
317
- }
318
- it('toObject leaves no Buffer instances', function () {
319
- var privKey = new HDPrivateKey(xprivkey)
320
- var object = privKey.toObject()
321
- _.each(_.values(object), function (value) {
322
- expect(Buffer.isBuffer(value)).to.equal(false)
323
- })
324
- })
325
- it('roundtrips toObject', function () {
326
- expect(HDPrivateKey.fromObject(new HDPrivateKey(xprivkey).toObject()).xprivkey).to.equal(xprivkey)
327
- })
328
- it('roundtrips to JSON and to Object', function () {
329
- var privkey = new HDPrivateKey(xprivkey)
330
- expect(HDPrivateKey.fromObject(privkey.toJSON()).xprivkey).to.equal(xprivkey)
331
- })
332
- it('recovers state from JSON', function () {
333
- new HDPrivateKey(JSON.stringify(plainObject)).xprivkey.should.equal(xprivkey)
334
- })
335
- it('recovers state from Object', function () {
336
- new HDPrivateKey(plainObject).xprivkey.should.equal(xprivkey)
337
- })
338
- })
339
- })
@@ -1,293 +0,0 @@
1
- 'use strict'
2
-
3
- var assert = require('assert')
4
- require('chai').should()
5
- var expect = require('chai').expect
6
- var bsv = require('..')
7
- var buffer = require('buffer')
8
- var errors = bsv.errors
9
- var hdErrors = bsv.errors.HDPublicKey
10
- var JSUtil = require('../lib/util/js')
11
- var HDPrivateKey = bsv.HDPrivateKey
12
- var HDPublicKey = bsv.HDPublicKey
13
- var Base58Check = bsv.encoding.Base58Check
14
- var Networks = bsv.Networks
15
-
16
- var xprivkey = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
17
- var xpubkey = 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8'
18
- var xpubkeyTestnet = 'tpubD6NzVbkrYhZ4WZaiWHz59q5EQ61bd6dUYfU4ggRWAtNAyyYRNWT6ktJ7UHJEXURvTfTfskFQmK7Ff4FRkiRN5wQH8nkGAb6aKB4Yyeqsw5m'
19
- var json = '{"network":"livenet","depth":0,"fingerPrint":876747070,"parentFingerPrint":0,"childIndex":0,"chainCode":"873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508","publicKey":"0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2","checksum":2873572129,"xpubkey":"xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"}'
20
- var derived01200000 = 'xpub6BqyndF6rkBNTV6LXwiY8Pco8aqctqq7tGEUdA8fmGDTnDJphn2fmxr3eM8Lm3m8TrNUsLbEjHvpa3adBU18YpEx4tp2Zp6nqax3mQkudhX'
21
-
22
- describe('HDPublicKey interface', function () {
23
- var expectFail = function (func, errorType) {
24
- (function () {
25
- func()
26
- }).should.throw(errorType)
27
- }
28
-
29
- var expectDerivationFail = function (argument, error) {
30
- (function () {
31
- var pubkey = new HDPublicKey(xpubkey)
32
- pubkey.deriveChild(argument)
33
- }).should.throw(error)
34
- }
35
-
36
- var expectFailBuilding = function (argument, error) {
37
- (function () {
38
- return new HDPublicKey(argument)
39
- }).should.throw(error)
40
- }
41
-
42
- describe('creation formats', function () {
43
- it('returns same argument if already an instance of HDPublicKey', function () {
44
- var publicKey = new HDPublicKey(xpubkey)
45
- publicKey.should.equal(new HDPublicKey(publicKey))
46
- })
47
-
48
- it('returns the correct xpubkey for a xprivkey', function () {
49
- var publicKey = new HDPublicKey(xprivkey)
50
- publicKey.xpubkey.should.equal(xpubkey)
51
- })
52
-
53
- it('allows to call the argument with no "new" keyword', function () {
54
- HDPublicKey(xpubkey).xpubkey.should.equal(new HDPublicKey(xpubkey).xpubkey)
55
- })
56
-
57
- it('fails when user doesn\'t supply an argument', function () {
58
- expectFailBuilding(null, hdErrors.MustSupplyArgument)
59
- })
60
-
61
- it('should not be able to change read-only properties', function () {
62
- var publicKey = new HDPublicKey(xprivkey)
63
- expect(function () {
64
- publicKey.fingerPrint = 'notafingerprint'
65
- }).to.throw(TypeError)
66
- })
67
-
68
- it('doesn\'t recognize an invalid argument', function () {
69
- expectFailBuilding(1, hdErrors.UnrecognizedArgument)
70
- expectFailBuilding(true, hdErrors.UnrecognizedArgument)
71
- })
72
-
73
- describe('xpubkey string serialization errors', function () {
74
- it('fails on invalid length', function () {
75
- expectFailBuilding(
76
- Base58Check.encode(buffer.Buffer.from([1, 2, 3])),
77
- hdErrors.InvalidLength
78
- )
79
- })
80
- it('fails on invalid base58 encoding', function () {
81
- expectFailBuilding(
82
- xpubkey + '1',
83
- errors.InvalidB58Checksum
84
- )
85
- })
86
- it('user can ask if a string is valid', function () {
87
- (HDPublicKey.isValidSerialized(xpubkey)).should.equal(true)
88
- })
89
- })
90
-
91
- it('can be generated from a json', function () {
92
- expect(new HDPublicKey(JSON.parse(json)).xpubkey).to.equal(xpubkey)
93
- })
94
-
95
- it('can generate a json that has a particular structure', function () {
96
- assert.deepStrictEqual(
97
- new HDPublicKey(JSON.parse(json)).toJSON(),
98
- new HDPublicKey(xpubkey).toJSON()
99
- )
100
- })
101
-
102
- it('builds from a buffer object', function () {
103
- (new HDPublicKey(new HDPublicKey(xpubkey)._buffers)).xpubkey.should.equal(xpubkey)
104
- })
105
-
106
- it('checks the checksum', function () {
107
- var buffers = new HDPublicKey(xpubkey)._buffers
108
- buffers.checksum = JSUtil.integerAsBuffer(1)
109
- expectFail(function () {
110
- return new HDPublicKey(buffers)
111
- }, errors.InvalidB58Checksum)
112
- })
113
- })
114
-
115
- describe('error checking on serialization', function () {
116
- var compareType = function (a, b) {
117
- expect(a instanceof b).to.equal(true)
118
- }
119
- it('throws invalid argument when argument is not a string or buffer', function () {
120
- compareType(HDPublicKey.getSerializedError(1), hdErrors.UnrecognizedArgument)
121
- })
122
- it('if a network is provided, validates that data corresponds to it', function () {
123
- compareType(HDPublicKey.getSerializedError(xpubkey, 'testnet'), errors.InvalidNetwork)
124
- })
125
- it('recognizes invalid network arguments', function () {
126
- compareType(HDPublicKey.getSerializedError(xpubkey, 'invalid'), errors.InvalidNetworkArgument)
127
- })
128
- it('recognizes a valid network', function () {
129
- expect(HDPublicKey.getSerializedError(xpubkey, 'livenet')).to.equal(null)
130
- })
131
- })
132
-
133
- it('toString() returns the same value as .xpubkey', function () {
134
- var pubKey = new HDPublicKey(xpubkey)
135
- pubKey.toString().should.equal(pubKey.xpubkey)
136
- })
137
-
138
- it('publicKey property matches network', function () {
139
- var livenet = new HDPublicKey(xpubkey)
140
- var testnet = new HDPublicKey(xpubkeyTestnet)
141
-
142
- livenet.publicKey.network.should.equal(Networks.livenet)
143
- testnet.publicKey.network.should.equal(Networks.testnet)
144
- })
145
-
146
- it('inspect() displays correctly', function () {
147
- var pubKey = new HDPublicKey(xpubkey)
148
- pubKey.inspect().should.equal('<HDPublicKey: ' + pubKey.xpubkey + '>')
149
- })
150
-
151
- describe('conversion to/from buffer', function () {
152
- it('should roundtrip to an equivalent object', function () {
153
- var pubKey = new HDPublicKey(xpubkey)
154
- var toBuffer = pubKey.toBuffer()
155
- var fromBuffer = HDPublicKey.fromBuffer(toBuffer)
156
- var roundTrip = new HDPublicKey(fromBuffer.toBuffer())
157
- roundTrip.xpubkey.should.equal(xpubkey)
158
- })
159
- })
160
-
161
- describe('conversion to/from hex', function () {
162
- it('should roundtrip to an equivalent object', function () {
163
- var pubKey = new HDPublicKey(xpubkey)
164
- var toHex = pubKey.toHex()
165
- var fromHex = HDPublicKey.fromHex(toHex)
166
- var roundTrip = new HDPublicKey(fromHex.toBuffer())
167
- roundTrip.xpubkey.should.equal(xpubkey)
168
- })
169
- })
170
-
171
- describe('from hdprivatekey', function () {
172
- var str = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi'
173
- it('should roundtrip to/from a buffer', function () {
174
- var xprv1 = new HDPrivateKey(str)
175
- var xprv2 = HDPrivateKey.fromRandom()
176
- var xprv3 = HDPrivateKey.fromRandom()
177
- var xpub1 = HDPublicKey.fromHDPrivateKey(xprv1)
178
- var xpub2 = HDPublicKey.fromHDPrivateKey(xprv2)
179
- var xpub3 = HDPublicKey.fromHDPrivateKey(xprv3)
180
- xpub1.toString().should.equal('xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8')
181
- xpub1.toString().should.not.equal(xpub2.toString())
182
- xpub1.toString().should.not.equal(xpub3.toString())
183
- })
184
- })
185
-
186
- describe('conversion to different formats', function () {
187
- var plainObject = {
188
- 'network': 'livenet',
189
- 'depth': 0,
190
- 'fingerPrint': 876747070,
191
- 'parentFingerPrint': 0,
192
- 'childIndex': 0,
193
- 'chainCode': '873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508',
194
- 'publicKey': '0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2',
195
- 'checksum': 2873572129,
196
- 'xpubkey': 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8'
197
- }
198
- it('roundtrips to JSON and to Object', function () {
199
- var pubkey = new HDPublicKey(xpubkey)
200
- expect(HDPublicKey.fromObject(pubkey.toJSON()).xpubkey).to.equal(xpubkey)
201
- })
202
- it('recovers state from Object', function () {
203
- new HDPublicKey(plainObject).xpubkey.should.equal(xpubkey)
204
- })
205
- })
206
-
207
- describe('derivation', function () {
208
- it('derivation is the same whether deriving with number or string', function () {
209
- var pubkey = new HDPublicKey(xpubkey)
210
- var derived1 = pubkey.deriveChild(0).deriveChild(1).deriveChild(200000)
211
- var derived2 = pubkey.deriveChild('m/0/1/200000')
212
- derived1.xpubkey.should.equal(derived01200000)
213
- derived2.xpubkey.should.equal(derived01200000)
214
- })
215
-
216
- it('allows special parameters m, M', function () {
217
- var expectDerivationSuccess = function (argument) {
218
- new HDPublicKey(xpubkey).deriveChild(argument).xpubkey.should.equal(xpubkey)
219
- }
220
- expectDerivationSuccess('m')
221
- expectDerivationSuccess('M')
222
- })
223
-
224
- it('doesn\'t allow object arguments for derivation', function () {
225
- expectFail(function () {
226
- return new HDPublicKey(xpubkey).deriveChild({})
227
- }, hdErrors.InvalidDerivationArgument)
228
- })
229
-
230
- it('needs first argument for derivation', function () {
231
- expectFail(function () {
232
- return new HDPublicKey(xpubkey).deriveChild('s')
233
- }, hdErrors.InvalidPath)
234
- })
235
-
236
- it('doesn\'t allow other parameters like m\' or M\' or "s"', function () {
237
- expectDerivationFail("m'", hdErrors.InvalidIndexCantDeriveHardened)
238
- expectDerivationFail("M'", hdErrors.InvalidIndexCantDeriveHardened)
239
- expectDerivationFail('1', hdErrors.InvalidPath)
240
- expectDerivationFail('S', hdErrors.InvalidPath)
241
- })
242
-
243
- it('can\'t derive hardened keys', function () {
244
- expectFail(function () {
245
- return new HDPublicKey(xpubkey).deriveChild(HDPublicKey.Hardened)
246
- }, hdErrors.InvalidIndexCantDeriveHardened)
247
- })
248
-
249
- it('can\'t derive hardened keys via second argument', function () {
250
- expectFail(function () {
251
- return new HDPublicKey(xpubkey).deriveChild(5, true)
252
- }, hdErrors.InvalidIndexCantDeriveHardened)
253
- })
254
-
255
- it('validates correct paths', function () {
256
- var valid
257
-
258
- valid = HDPublicKey.isValidPath('m/123/12')
259
- valid.should.equal(true)
260
-
261
- valid = HDPublicKey.isValidPath('m')
262
- valid.should.equal(true)
263
-
264
- valid = HDPublicKey.isValidPath(123)
265
- valid.should.equal(true)
266
- })
267
-
268
- it('rejects illegal paths', function () {
269
- var valid
270
-
271
- valid = HDPublicKey.isValidPath('m/-1/12')
272
- valid.should.equal(false)
273
-
274
- valid = HDPublicKey.isValidPath("m/0'/12")
275
- valid.should.equal(false)
276
-
277
- valid = HDPublicKey.isValidPath('m/8000000000/12')
278
- valid.should.equal(false)
279
-
280
- valid = HDPublicKey.isValidPath('bad path')
281
- valid.should.equal(false)
282
-
283
- valid = HDPublicKey.isValidPath(-1)
284
- valid.should.equal(false)
285
-
286
- valid = HDPublicKey.isValidPath(8000000000)
287
- valid.should.equal(false)
288
-
289
- valid = HDPublicKey.isValidPath(HDPublicKey.Hardened)
290
- valid.should.equal(false)
291
- })
292
- })
293
- })
package/test/index.js DELETED
@@ -1,16 +0,0 @@
1
- 'use strict'
2
-
3
- var should = require('chai').should()
4
- var bsv = require('../')
5
-
6
- describe('#versionGuard', function () {
7
- it('global._bsv should be defined', function () {
8
- should.equal(global._bsv, bsv.version)
9
- })
10
-
11
- it('throw an error if version is already defined', function () {
12
- (function () {
13
- bsv.versionGuard('version')
14
- }).should.not.throw('More than one instance of bsv')
15
- })
16
- })