@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
package/test/address.js DELETED
@@ -1,629 +0,0 @@
1
- 'use strict'
2
-
3
- var chai = require('chai')
4
- var should = chai.should()
5
- var expect = chai.expect
6
-
7
- var bsv = require('..')
8
- var PublicKey = bsv.PublicKey
9
- var PrivateKey = bsv.PrivateKey
10
- var Address = bsv.Address
11
- var Script = bsv.Script
12
- var Networks = bsv.Networks
13
-
14
- var validbase58 = require('./data/bitcoind/base58_keys_valid.json')
15
- var invalidbase58 = require('./data/bitcoind/base58_keys_invalid.json')
16
-
17
- describe('Address', function () {
18
- var pubkeyhash = Buffer.from('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex')
19
- var buf = Buffer.concat([Buffer.from([0]), pubkeyhash])
20
- var str = '16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r'
21
-
22
- it('can\'t build without data', function () {
23
- (function () {
24
- return new Address()
25
- }).should.throw('First argument is required, please include address data.')
26
- })
27
-
28
- it('should throw an error because of bad network param', function () {
29
- (function () {
30
- return new Address(PKHLivenet[0], 'main', 'pubkeyhash')
31
- }).should.throw('Second argument must be "livenet", "testnet", or "regtest".')
32
- })
33
-
34
- it('should throw an error because of bad type param', function () {
35
- (function () {
36
- return new Address(PKHLivenet[0], 'livenet', 'pubkey')
37
- }).should.throw('Third argument must be "pubkeyhash" or "scripthash"')
38
- })
39
-
40
- describe('bitcoind compliance', function () {
41
- validbase58.map(function (d) {
42
- if (!d[2].isPrivkey) {
43
- it('should describe address ' + d[0] + ' as valid', function () {
44
- var type
45
- if (d[2].addrType === 'script') {
46
- type = 'scripthash'
47
- } else if (d[2].addrType === 'pubkey') {
48
- type = 'pubkeyhash'
49
- }
50
- var network = 'livenet'
51
- if (d[2].isTestnet) {
52
- network = 'testnet'
53
- }
54
- return new Address(d[0], network, type)
55
- })
56
- }
57
- })
58
- invalidbase58.map(function (d) {
59
- it('should describe input ' + d[0].slice(0, 10) + '... as invalid', function () {
60
- expect(function () {
61
- return new Address(d[0])
62
- }).to.throw(Error)
63
- })
64
- })
65
- })
66
-
67
- describe('generic tests', function () {
68
- it('should pass these tests', function () {
69
- var str = '13k3vneZ3yvZnc9dNWYH2RJRFsagTfAERv'
70
- var address = Address.fromString(str)
71
- address.toString().should.equal(str)
72
- })
73
- })
74
-
75
- // livenet valid
76
- var PKHLivenet = [
77
- '15vkcKf7gB23wLAnZLmbVuMiiVDc1Nm4a2',
78
- '1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT',
79
- '1BpbpfLdY7oBS9gK7aDXgvMgr1DPvNhEB2',
80
- '1Jz2yCRd5ST1p2gUqFB5wsSQfdm3jaFfg7',
81
- ' 1Jz2yCRd5ST1p2gUqFB5wsSQfdm3jaFfg7 \t\n'
82
- ]
83
-
84
- // livenet p2sh
85
- var P2SHLivenet = [
86
- '342ftSRCvFHfCeFFBuz4xwbeqnDw6BGUey',
87
- '33vt8ViH5jsr115AGkW6cEmEz9MpvJSwDk',
88
- '37Sp6Rv3y4kVd1nQ1JV5pfqXccHNyZm1x3',
89
- '3QjYXhTkvuj8qPaXHTTWb5wjXhdsLAAWVy',
90
- '\t3QjYXhTkvuj8qPaXHTTWb5wjXhdsLAAWVy \n \r'
91
- ]
92
-
93
- // testnet p2sh
94
- var P2SHTestnet = [
95
- '2N7FuwuUuoTBrDFdrAZ9KxBmtqMLxce9i1C',
96
- '2NEWDzHWwY5ZZp8CQWbB7ouNMLqCia6YRda',
97
- '2MxgPqX1iThW3oZVk9KoFcE5M4JpiETssVN',
98
- '2NB72XtkjpnATMggui83aEtPawyyKvnbX2o'
99
- ]
100
-
101
- // livenet bad checksums
102
- var badChecksums = [
103
- '15vkcKf7gB23wLAnZLmbVuMiiVDc3nq4a2',
104
- '1A6ut1tWnUq1SEQLMr4ttDh24wcbj4w2TT',
105
- '1BpbpfLdY7oBS9gK7aDXgvMgr1DpvNH3B2',
106
- '1Jz2yCRd5ST1p2gUqFB5wsSQfdmEJaffg7'
107
- ]
108
-
109
- // livenet non-base58
110
- var nonBase58 = [
111
- '15vkcKf7g#23wLAnZLmb$uMiiVDc3nq4a2',
112
- '1A601ttWnUq1SEQLMr4ttDh24wcbj4w2TT',
113
- '1BpbpfLdY7oBS9gK7aIXgvMgr1DpvNH3B2',
114
- '1Jz2yCRdOST1p2gUqFB5wsSQfdmEJaffg7'
115
- ]
116
-
117
- // testnet valid
118
- var PKHTestnet = [
119
- 'n28S35tqEMbt6vNad7A5K3mZ7vdn8dZ86X',
120
- 'n45x3R2w2jaSC62BMa9MeJCd3TXxgvDEmm',
121
- 'mursDVxqNQmmwWHACpM9VHwVVSfTddGsEM',
122
- 'mtX8nPZZdJ8d3QNLRJ1oJTiEi26Sj6LQXS'
123
- ]
124
-
125
- describe('validation', function () {
126
- it('getValidationError detects network mismatchs', function () {
127
- var error = Address.getValidationError('HC1hAdrx7APHg1DkE4bVLsZhY1SE5Dik1r', 'testnet')
128
- should.exist(error)
129
- })
130
-
131
- it('isValid returns true on a valid livenet address', function () {
132
- Address.isValid('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'livenet').should.equal(true)
133
- })
134
-
135
- it('isValid returns false on network mismatch', function () {
136
- Address.isValid('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'testnet').should.equal(false)
137
- Address.isValid('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo', 'regtest').should.equal(false)
138
- })
139
-
140
- it('validates correctly the P2PKH test vector', function () {
141
- for (var i = 0; i < PKHLivenet.length; i++) {
142
- var error = Address.getValidationError(PKHLivenet[i])
143
- should.not.exist(error)
144
- }
145
- })
146
-
147
- it('validates correctly the P2SH test vector', function () {
148
- for (var i = 0; i < P2SHLivenet.length; i++) {
149
- var error = Address.getValidationError(P2SHLivenet[i])
150
- should.not.exist(error)
151
- }
152
- })
153
-
154
- it('validates correctly the P2SH testnet test vector', function () {
155
- for (var i = 0; i < P2SHTestnet.length; i++) {
156
- var error = Address.getValidationError(P2SHTestnet[i], 'testnet')
157
- should.not.exist(error)
158
- }
159
- })
160
-
161
- it('rejects correctly the P2PKH livenet test vector with "testnet" parameter', function () {
162
- for (var i = 0; i < PKHLivenet.length; i++) {
163
- var error = Address.getValidationError(PKHLivenet[i], 'testnet')
164
- should.exist(error)
165
- }
166
- })
167
-
168
- it('validates correctly the P2PKH livenet test vector with "livenet" parameter', function () {
169
- for (var i = 0; i < PKHLivenet.length; i++) {
170
- var error = Address.getValidationError(PKHLivenet[i], 'livenet')
171
- should.not.exist(error)
172
- }
173
- })
174
-
175
- it('should not validate if checksum is invalid', function () {
176
- for (var i = 0; i < badChecksums.length; i++) {
177
- var error = Address.getValidationError(badChecksums[i], 'livenet', 'pubkeyhash')
178
- should.exist(error)
179
- error.message.should.match(/Checksum mismatch/)
180
- }
181
- })
182
-
183
- it('should not validate on a network mismatch', function () {
184
- var error,
185
- i
186
- for (i = 0; i < PKHLivenet.length; i++) {
187
- error = Address.getValidationError(PKHLivenet[i], 'testnet', 'pubkeyhash')
188
- should.exist(error)
189
- error.message.should.equal('Address has mismatched network type.')
190
- }
191
- for (i = 0; i < PKHTestnet.length; i++) {
192
- error = Address.getValidationError(PKHTestnet[i], 'livenet', 'pubkeyhash')
193
- should.exist(error)
194
- error.message.should.equal('Address has mismatched network type.')
195
- }
196
- })
197
-
198
- it('should not validate on a type mismatch', function () {
199
- for (var i = 0; i < PKHLivenet.length; i++) {
200
- var error = Address.getValidationError(PKHLivenet[i], 'livenet', 'scripthash')
201
- should.exist(error)
202
- error.message.should.equal('Address has mismatched type.')
203
- }
204
- })
205
-
206
- it('should not validate on non-base58 characters', function () {
207
- for (var i = 0; i < nonBase58.length; i++) {
208
- var error = Address.getValidationError(nonBase58[i], 'livenet', 'pubkeyhash')
209
- should.exist(error)
210
- error.message.should.match(/Non-base58/)
211
- }
212
- })
213
-
214
- it('testnet addresses are validated correctly', function () {
215
- for (var i = 0; i < PKHTestnet.length; i++) {
216
- var error = Address.getValidationError(PKHTestnet[i], 'testnet')
217
- should.not.exist(error)
218
- }
219
- })
220
-
221
- it('addresses with whitespace are validated correctly', function () {
222
- var ws = ' \r \t \n 1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT \t \n \r'
223
- var error = Address.getValidationError(ws)
224
- should.not.exist(error)
225
- Address.fromString(ws).toString().should.equal('1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT')
226
- })
227
- })
228
-
229
- describe('instantiation', function () {
230
- it('can be instantiated from another address', function () {
231
- var address = Address.fromBuffer(buf)
232
- var address2 = new Address({
233
- hashBuffer: address.hashBuffer,
234
- network: address.network,
235
- type: address.type
236
- })
237
- address.toString().should.equal(address2.toString())
238
- })
239
- })
240
-
241
- describe('@fromBuffer', function () {
242
- it('can be instantiated from another address', function () {
243
- var address = Address.fromBuffer(buf)
244
- var address2 = new Address({
245
- hashBuffer: address.hashBuffer,
246
- network: address.network,
247
- type: address.type
248
- })
249
- address.toString().should.equal(address2.toString())
250
- })
251
- })
252
-
253
- describe('@fromHex', function () {
254
- it('can be instantiated from another address', function () {
255
- var address = Address.fromHex(buf.toString('hex'))
256
- var address2 = new Address({
257
- hashBuffer: address.hashBuffer,
258
- network: address.network,
259
- type: address.type
260
- })
261
- address.toString().should.equal(address2.toString())
262
- })
263
- })
264
-
265
- describe('encodings', function () {
266
- it('should make an address from a buffer', function () {
267
- Address.fromBuffer(buf).toString().should.equal(str)
268
- new Address(buf).toString().should.equal(str)
269
- new Address(buf).toString().should.equal(str)
270
- })
271
-
272
- it('should make an address from a string', function () {
273
- Address.fromString(str).toString().should.equal(str)
274
- new Address(str).toString().should.equal(str)
275
- })
276
-
277
- it('should make an address using a non-string network', function () {
278
- Address.fromString(str, Networks.livenet).toString().should.equal(str)
279
- })
280
-
281
- it('should throw with bad network param', function () {
282
- (function () {
283
- Address.fromString(str, 'somenet')
284
- }).should.throw('Unknown network')
285
- })
286
-
287
- it('should error because of unrecognized data format', function () {
288
- (function () {
289
- return new Address(new Error())
290
- }).should.throw(bsv.errors.InvalidArgument)
291
- })
292
-
293
- it('should error because of incorrect format for pubkey hash', function () {
294
- (function () {
295
- return new Address.fromPublicKeyHash('notahash') //eslint-disable-line
296
- }).should.throw('Address supplied is not a buffer.')
297
- })
298
-
299
- it('should error because of incorrect format for script hash', function () {
300
- (function () {
301
- return new Address.fromScriptHash('notascript') //eslint-disable-line
302
- }).should.throw('Address supplied is not a buffer.')
303
- })
304
-
305
- it('should error because of incorrect type for transform buffer', function () {
306
- (function () {
307
- return Address._transformBuffer('notabuffer')
308
- }).should.throw('Address supplied is not a buffer.')
309
- })
310
-
311
- it('should error because of incorrect length buffer for transform buffer', function () {
312
- (function () {
313
- return Address._transformBuffer(Buffer.alloc(20))
314
- }).should.throw('Address buffers must be exactly 21 bytes.')
315
- })
316
-
317
- it('should error because of incorrect type for pubkey transform', function () {
318
- (function () {
319
- return Address._transformPublicKey(Buffer.alloc(20))
320
- }).should.throw('Address must be an instance of PublicKey.')
321
- })
322
-
323
- it('should error because of incorrect type for script transform', function () {
324
- (function () {
325
- return Address._transformScript(Buffer.alloc(20))
326
- }).should.throw('Invalid Argument: script must be a Script instance')
327
- })
328
-
329
- it('should error because of incorrect type for string transform', function () {
330
- (function () {
331
- return Address._transformString(Buffer.alloc(20))
332
- }).should.throw('data parameter supplied is not a string.')
333
- })
334
-
335
- it('should make an address from a pubkey hash buffer', function () {
336
- var hash = pubkeyhash // use the same hash
337
- var a = Address.fromPublicKeyHash(hash, 'livenet')
338
- a.network.should.equal(Networks.livenet)
339
- a.toString().should.equal(str)
340
- var b = Address.fromPublicKeyHash(hash, 'testnet')
341
- b.network.should.equal(Networks.testnet)
342
- b.type.should.equal('pubkeyhash')
343
- new Address(hash, 'livenet').toString().should.equal(str)
344
- })
345
-
346
- it('should make an address using the default network', function () {
347
- var hash = pubkeyhash // use the same hash
348
- var network = Networks.defaultNetwork
349
- Networks.defaultNetwork = Networks.livenet
350
- var a = Address.fromPublicKeyHash(hash)
351
- a.network.should.equal(Networks.livenet)
352
- // change the default
353
- Networks.defaultNetwork = Networks.testnet
354
- var b = Address.fromPublicKeyHash(hash)
355
- b.network.should.equal(Networks.testnet)
356
- // restore the default
357
- Networks.defaultNetwork = network
358
- })
359
-
360
- it('should throw an error for invalid length hashBuffer', function () {
361
- (function () {
362
- return Address.fromPublicKeyHash(buf)
363
- }).should.throw('Address hashbuffers must be exactly 20 bytes.')
364
- })
365
-
366
- it('should make this address from a compressed pubkey', function () {
367
- var pubkey = new PublicKey('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004')
368
- var address = Address.fromPublicKey(pubkey, 'livenet')
369
- address.toString().should.equal('19gH5uhqY6DKrtkU66PsZPUZdzTd11Y7ke')
370
- })
371
-
372
- it('should use the default network for pubkey', function () {
373
- var pubkey = new PublicKey('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004')
374
- var address = Address.fromPublicKey(pubkey)
375
- address.network.should.equal(Networks.defaultNetwork)
376
- })
377
-
378
- it('should make this address from an uncompressed pubkey', function () {
379
- var pubkey = new PublicKey('0485e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b00' +
380
- '4833fef26c8be4c4823754869ff4e46755b85d851077771c220e2610496a29d98')
381
- var a = Address.fromPublicKey(pubkey, 'livenet')
382
- a.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX')
383
- var b = new Address(pubkey, 'livenet', 'pubkeyhash')
384
- b.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX')
385
- })
386
-
387
- it('should classify from a custom network', function () {
388
- var custom = {
389
- name: 'customnetwork',
390
- pubkeyhash: 10,
391
- privatekey: 0x1e,
392
- scripthash: 15,
393
- xpubkey: 0x02e8de8f,
394
- xprivkey: 0x02e8da54,
395
- networkMagic: 0x0c110907,
396
- port: 7333
397
- }
398
- Networks.add(custom)
399
- var addressString = '57gZdnwcQHLirKLwDHcFiWLq9jTZwRaxaE'
400
- var network = Networks.get('customnetwork')
401
- var address = Address.fromString(addressString)
402
- address.type.should.equal(Address.PayToPublicKeyHash)
403
- address.network.should.equal(network)
404
- Networks.remove(network)
405
- })
406
-
407
- describe('from a script', function () {
408
- it('should fail to build address from a non p2sh,p2pkh script', function () {
409
- var s = new Script('OP_CHECKMULTISIG');
410
- (function () {
411
- return new Address(s)
412
- }).should.throw('needs to be p2pkh in, p2pkh out, p2sh in, or p2sh out')
413
- })
414
- it('should make this address from a p2pkh output script', function () {
415
- var s = new Script('OP_DUP OP_HASH160 20 ' +
416
- '0xc8e11b0eb0d2ad5362d894f048908341fa61b6e1 OP_EQUALVERIFY OP_CHECKSIG')
417
- var a = Address.fromScript(s, 'livenet')
418
- a.toString().should.equal('1KK9oz4bFH8c1t6LmighHaoSEGx3P3FEmc')
419
- var b = new Address(s, 'livenet')
420
- b.toString().should.equal('1KK9oz4bFH8c1t6LmighHaoSEGx3P3FEmc')
421
- })
422
-
423
- it('should make this address from a p2sh input script', function () {
424
- var s = Script.fromString('OP_HASH160 20 0xa6ed4af315271e657ee307828f54a4365fa5d20f OP_EQUAL')
425
- var a = Address.fromScript(s, 'livenet')
426
- a.toString().should.equal('3GueMn6ruWVfQTN4XKBGEbCbGLwRSUhfnS')
427
- var b = new Address(s, 'livenet')
428
- b.toString().should.equal('3GueMn6ruWVfQTN4XKBGEbCbGLwRSUhfnS')
429
- })
430
-
431
- it('returns the same address if the script is a pay to public key hash out', function () {
432
- var address = '16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX'
433
- var script = Script.buildPublicKeyHashOut(new Address(address))
434
- Address(script, Networks.livenet).toString().should.equal(address)
435
- })
436
- it('returns the same address if the script is a pay to script hash out', function () {
437
- var address = '3BYmEwgV2vANrmfRymr1mFnHXgLjD6gAWm'
438
- var script = Script.buildScriptHashOut(new Address(address))
439
- Address(script, Networks.livenet).toString().should.equal(address)
440
- })
441
- })
442
-
443
- it('should derive from this known address string livenet', function () {
444
- var address = new Address(str)
445
- var buffer = address.toBuffer()
446
- var slice = buffer.slice(1)
447
- var sliceString = slice.toString('hex')
448
- sliceString.should.equal(pubkeyhash.toString('hex'))
449
- })
450
-
451
- it('should derive from this known address string testnet', function () {
452
- var a = new Address(PKHTestnet[0], 'testnet')
453
- var b = new Address(a.toString())
454
- b.toString().should.equal(PKHTestnet[0])
455
- b.network.should.equal(Networks.testnet)
456
- })
457
-
458
- it('should derive from this known address string livenet scripthash', function () {
459
- var a = new Address(P2SHLivenet[0], 'livenet', 'scripthash')
460
- var b = new Address(a.toString())
461
- b.toString().should.equal(P2SHLivenet[0])
462
- })
463
-
464
- it('should derive from this known address string testnet scripthash', function () {
465
- var address = new Address(P2SHTestnet[0], 'testnet', 'scripthash')
466
- address = new Address(address.toString())
467
- address.toString().should.equal(P2SHTestnet[0])
468
- })
469
- })
470
-
471
- describe('#toBuffer', function () {
472
- it('3c3fa3d4adcaf8f52d5b1843975e122548269937 corresponds to hash 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r', function () {
473
- var address = new Address(str)
474
- address.toBuffer().slice(1).toString('hex').should.equal(pubkeyhash.toString('hex'))
475
- })
476
- })
477
-
478
- describe('#toHex', function () {
479
- it('3c3fa3d4adcaf8f52d5b1843975e122548269937 corresponds to hash 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r', function () {
480
- var address = new Address(str)
481
- address.toHex().slice(2).should.equal(pubkeyhash.toString('hex'))
482
- })
483
- })
484
-
485
- describe('#object', function () {
486
- it('roundtrip to-from-to', function () {
487
- var obj = new Address(str).toObject()
488
- var address = Address.fromObject(obj)
489
- address.toString().should.equal(str)
490
- })
491
-
492
- it('will fail with invalid state', function () {
493
- expect(function () {
494
- return Address.fromObject('¹')
495
- }).to.throw(bsv.errors.InvalidState)
496
- })
497
- })
498
-
499
- describe('#toString', function () {
500
- it('livenet pubkeyhash address', function () {
501
- var address = new Address(str)
502
- address.toString().should.equal(str)
503
- })
504
-
505
- it('scripthash address', function () {
506
- var address = new Address(P2SHLivenet[0])
507
- address.toString().should.equal(P2SHLivenet[0])
508
- })
509
-
510
- it('testnet scripthash address', function () {
511
- var address = new Address(P2SHTestnet[0])
512
- address.toString().should.equal(P2SHTestnet[0])
513
- })
514
-
515
- it('testnet pubkeyhash address', function () {
516
- var address = new Address(PKHTestnet[0])
517
- address.toString().should.equal(PKHTestnet[0])
518
- })
519
- })
520
-
521
- describe('#inspect', function () {
522
- it('should output formatted output correctly', function () {
523
- var address = new Address(str)
524
- var output = '<Address: 16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r, type: pubkeyhash, network: livenet>'
525
- address.inspect().should.equal(output)
526
- })
527
- })
528
-
529
- describe('questions about the address', function () {
530
- it('should detect a P2SH address', function () {
531
- new Address(P2SHLivenet[0]).isPayToScriptHash().should.equal(true)
532
- new Address(P2SHLivenet[0]).isPayToPublicKeyHash().should.equal(false)
533
- new Address(P2SHTestnet[0]).isPayToScriptHash().should.equal(true)
534
- new Address(P2SHTestnet[0]).isPayToPublicKeyHash().should.equal(false)
535
- })
536
- it('should detect a Pay To PubkeyHash address', function () {
537
- new Address(PKHLivenet[0]).isPayToPublicKeyHash().should.equal(true)
538
- new Address(PKHLivenet[0]).isPayToScriptHash().should.equal(false)
539
- new Address(PKHTestnet[0]).isPayToPublicKeyHash().should.equal(true)
540
- new Address(PKHTestnet[0]).isPayToScriptHash().should.equal(false)
541
- })
542
- })
543
-
544
- it('throws an error if it couldn\'t instantiate', function () {
545
- expect(function () {
546
- return new Address(1)
547
- }).to.throw(TypeError)
548
- })
549
- it('can roundtrip from/to a object', function () {
550
- var address = new Address(P2SHLivenet[0])
551
- expect(new Address(address.toObject()).toString()).to.equal(P2SHLivenet[0])
552
- })
553
-
554
- it('will use the default network for an object', function () {
555
- var obj = {
556
- hash: '19a7d869032368fd1f1e26e5e73a4ad0e474960e',
557
- type: 'scripthash'
558
- }
559
- var address = new Address(obj)
560
- address.network.should.equal(Networks.defaultNetwork)
561
- })
562
-
563
- describe('creating a P2SH address from public keys', function () {
564
- var public1 = '02da5798ed0c055e31339eb9b5cef0d3c0ccdec84a62e2e255eb5c006d4f3e7f5b'
565
- var public2 = '0272073bf0287c4469a2a011567361d42529cd1a72ab0d86aa104ecc89342ffeb0'
566
- var public3 = '02738a516a78355db138e8119e58934864ce222c553a5407cf92b9c1527e03c1a2'
567
- var publics = [public1, public2, public3]
568
-
569
- it('can create an address from a set of public keys', function () {
570
- var address = Address.createMultisig(publics, 2, Networks.livenet)
571
- address.toString().should.equal('3FtqPRirhPvrf7mVUSkygyZ5UuoAYrTW3y')
572
- address = new Address(publics, 2, Networks.livenet)
573
- address.toString().should.equal('3FtqPRirhPvrf7mVUSkygyZ5UuoAYrTW3y')
574
- })
575
-
576
- it('works on testnet also', function () {
577
- var address = Address.createMultisig(publics, 2, Networks.testnet)
578
- address.toString().should.equal('2N7T3TAetJrSCruQ39aNrJvYLhG1LJosujf')
579
- })
580
-
581
- it('can also be created by Address.createMultisig', function () {
582
- var address = Address.createMultisig(publics, 2)
583
- var address2 = Address.createMultisig(publics, 2)
584
- address.toString().should.equal(address2.toString())
585
- })
586
-
587
- it('fails if invalid array is provided', function () {
588
- expect(function () {
589
- return Address.createMultisig([], 3, 'testnet')
590
- }).to.throw('Number of required signatures must be less than or equal to the number of public keys')
591
- })
592
- })
593
-
594
- describe('#fromPublicKey', function () {
595
- it('should derive from public key', function () {
596
- let privateKey = PrivateKey.fromRandom()
597
- let publicKey = PublicKey.fromPrivateKey(privateKey)
598
- let address = Address.fromPublicKey(publicKey)
599
- address.toString()[0].should.equal('1')
600
- })
601
-
602
- it('should derive from public key testnet', function () {
603
- let privateKey = PrivateKey.fromRandom('testnet')
604
- let publicKey = PublicKey.fromPrivateKey(privateKey)
605
- let address = Address.fromPublicKey(publicKey, 'testnet')
606
- ;(address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true)
607
- })
608
- })
609
-
610
- describe('#fromPrivateKey', function () {
611
- it('should derive from public key', function () {
612
- let privateKey = PrivateKey.fromRandom()
613
- let address = Address.fromPrivateKey(privateKey)
614
- address.toString()[0].should.equal('1')
615
- })
616
-
617
- it('should derive from public key testnet', function () {
618
- let privateKey = PrivateKey.fromRandom('testnet')
619
- let address = Address.fromPrivateKey(privateKey, 'testnet')
620
- ;(address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true)
621
- })
622
-
623
- it('should derive from public key testnet', function () {
624
- let privateKey = PrivateKey.fromRandom('testnet')
625
- let address = Address.fromPrivateKey(privateKey)
626
- ;(address.toString()[0] === 'm' || address.toString()[0] === 'n').should.equal(true)
627
- })
628
- })
629
- })