@smartledger/bsv 9.0.0 → 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 (155) hide show
  1. package/CHANGELOG.md +137 -0
  2. package/README.md +152 -1041
  3. package/STABILITY.md +134 -0
  4. package/bsv-gdaf.min.js +53 -48
  5. package/bsv-ltp.min.js +23 -18
  6. package/bsv-smartcontract.min.js +18 -18
  7. package/bsv.bundle.js +53 -48
  8. package/bsv.min.js +53 -48
  9. package/docs/MODULE_REFERENCE_COMPLETE.md +27 -27
  10. package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
  11. package/docs/getting-started/INSTALLATION.md +23 -23
  12. package/docs/getting-started/QUICK_START.md +7 -7
  13. package/docs/migration/FROM_BSV_1_5_6.md +5 -5
  14. package/docs/proposals/10.0.0-package-split.md +131 -0
  15. package/index.js +5 -0
  16. package/lib/block/merkleblock.js +19 -5
  17. package/lib/hdprivatekey.js +15 -0
  18. package/lib/script/interpreter.js +109 -1
  19. package/lib/smart_contract/builder.js +14 -0
  20. package/lib/util/deprecate.js +159 -0
  21. package/package.json +19 -82
  22. package/version.js +1 -1
  23. package/.mocharc.json +0 -5
  24. package/test/address.js +0 -629
  25. package/test/block/block.js +0 -239
  26. package/test/block/blockheader.js +0 -270
  27. package/test/block/merkleblock.js +0 -207
  28. package/test/build/bundle_crypto_shim.js +0 -104
  29. package/test/build/bundle_externals.js +0 -125
  30. package/test/build/bundle_smoke.js +0 -55
  31. package/test/build/esbuild_main.js +0 -65
  32. package/test/build/esm_wrapper.js +0 -97
  33. package/test/build/exports_resolution.js +0 -84
  34. package/test/build/version_sync.js +0 -14
  35. package/test/cli/smoke.js +0 -261
  36. package/test/consensus/base58-vectors.js +0 -120
  37. package/test/consensus/sv-script-vectors.js +0 -81
  38. package/test/consensus/sv-sighash-vectors.js +0 -51
  39. package/test/consensus/sv-tx-vectors.js +0 -48
  40. package/test/credentials-test.js +0 -332
  41. package/test/crypto/backend_selection.js +0 -126
  42. package/test/crypto/bn.js +0 -168
  43. package/test/crypto/ecdsa.js +0 -426
  44. package/test/crypto/elliptic-fixed.js +0 -61
  45. package/test/crypto/hash.browser.js +0 -121
  46. package/test/crypto/hash.js +0 -122
  47. package/test/crypto/point.js +0 -193
  48. package/test/crypto/random.js +0 -105
  49. package/test/crypto/security.js +0 -176
  50. package/test/crypto/shamir.js +0 -177
  51. package/test/crypto/shamir_rngcap.js +0 -29
  52. package/test/crypto/signature.js +0 -399
  53. package/test/data/bip69.json +0 -215
  54. package/test/data/bitcoin-sv/README.md +0 -170
  55. package/test/data/bitcoin-sv/base58_encode_decode.json +0 -14
  56. package/test/data/bitcoin-sv/base58_keys_invalid.json +0 -152
  57. package/test/data/bitcoin-sv/base58_keys_valid.json +0 -452
  58. package/test/data/bitcoin-sv/script_tests.json +0 -2591
  59. package/test/data/bitcoin-sv/sighash.json +0 -1003
  60. package/test/data/bitcoin-sv/tx_invalid.json +0 -285
  61. package/test/data/bitcoin-sv/tx_valid.json +0 -367
  62. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  63. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  64. package/test/data/bitcoind/blocks.json +0 -27
  65. package/test/data/bitcoind/script_tests.json +0 -2244
  66. package/test/data/bitcoind/sig_canonical.json +0 -7
  67. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  68. package/test/data/bitcoind/tx_invalid.json +0 -177
  69. package/test/data/bitcoind/tx_valid.json +0 -224
  70. package/test/data/blk86756-testnet.dat +0 -0
  71. package/test/data/blk86756-testnet.js +0 -12
  72. package/test/data/blk86756-testnet.json +0 -684
  73. package/test/data/brc220-batch-vector.json +0 -129
  74. package/test/data/ecdsa.json +0 -230
  75. package/test/data/merkleblocks.js +0 -486
  76. package/test/data/messages.json +0 -22
  77. package/test/data/sighash.json +0 -1004
  78. package/test/data/tx_creation.json +0 -85
  79. package/test/didweb/relationships.js +0 -144
  80. package/test/ecies/bitcore-ecies.js +0 -178
  81. package/test/ecies/electrum-ecies.js +0 -206
  82. package/test/encoding/base58.js +0 -131
  83. package/test/encoding/base58check.js +0 -145
  84. package/test/encoding/bufferreader.js +0 -328
  85. package/test/encoding/bufferwriter.js +0 -160
  86. package/test/encoding/varint.js +0 -104
  87. package/test/gdaf/anchor_no_key_leak.js +0 -146
  88. package/test/gdaf/anchor_spv.js +0 -163
  89. package/test/gdaf/canonicalization.js +0 -106
  90. package/test/gdaf/canonicalize.js +0 -140
  91. package/test/gdaf/zk_prover.js +0 -204
  92. package/test/hdkeys.js +0 -365
  93. package/test/hdprivatekey.js +0 -339
  94. package/test/hdpublickey.js +0 -293
  95. package/test/index.js +0 -16
  96. package/test/ltp/ids.js +0 -74
  97. package/test/ltp/right.js +0 -198
  98. package/test/ltp/verify_failclosed.js +0 -134
  99. package/test/message/message.js +0 -190
  100. package/test/mnemonic/data/fixtures.json +0 -300
  101. package/test/mnemonic/mnemonic.js +0 -277
  102. package/test/mnemonic/mocha.opts +0 -1
  103. package/test/mnemonic/pbkdf2.test.js +0 -43
  104. package/test/networks.js +0 -207
  105. package/test/notaryhash/batch_leaf.js +0 -140
  106. package/test/notaryhash/batch_vector.js +0 -282
  107. package/test/notaryhash/certificate.js +0 -249
  108. package/test/notaryhash/encoding.js +0 -186
  109. package/test/notaryhash/interop.js +0 -112
  110. package/test/notaryhash/merkle.js +0 -181
  111. package/test/notaryhash/script.js +0 -270
  112. package/test/notaryhash/verify.js +0 -342
  113. package/test/opcode.js +0 -186
  114. package/test/ordinals/bsv20.js +0 -337
  115. package/test/ordinals/inscription.js +0 -329
  116. package/test/ordinals/ordlock.js +0 -567
  117. package/test/privatekey.js +0 -540
  118. package/test/publickey.js +0 -411
  119. package/test/regressions.js +0 -215
  120. package/test/script/chronicle.js +0 -543
  121. package/test/script/defaults.js +0 -160
  122. package/test/script/genesis_limits.js +0 -203
  123. package/test/script/interpreter.js +0 -776
  124. package/test/script/script.js +0 -1259
  125. package/test/script/string_ops.js +0 -88
  126. package/test/security/fail_closed_contracts.js +0 -104
  127. package/test/security/threat_model_coverage.js +0 -31
  128. package/test/smart_contract/covenants.js +0 -207
  129. package/test/smart_contract/dsl_debugger.js +0 -92
  130. package/test/smart_contract/extract_field.js +0 -61
  131. package/test/smart_contract/nonenforcing_guard.js +0 -44
  132. package/test/smart_contract/ordinal_transfer.js +0 -71
  133. package/test/smart_contract/preimage.js +0 -98
  134. package/test/smart_contract/sighash_marketplace.js +0 -98
  135. package/test/smart_contract/token_generalized.js +0 -175
  136. package/test/spv/headerchain.js +0 -86
  137. package/test/spv/merkleproof.js +0 -133
  138. package/test/statuslist/failclosed.js +0 -78
  139. package/test/transaction/deserialize.js +0 -33
  140. package/test/transaction/input/input.js +0 -92
  141. package/test/transaction/input/multisig.js +0 -174
  142. package/test/transaction/input/multisigscripthash.js +0 -111
  143. package/test/transaction/input/publickey.js +0 -68
  144. package/test/transaction/input/publickeyhash.js +0 -59
  145. package/test/transaction/output.js +0 -185
  146. package/test/transaction/sighash.js +0 -91
  147. package/test/transaction/signature.js +0 -127
  148. package/test/transaction/transaction.js +0 -1299
  149. package/test/transaction/unspentoutput.js +0 -97
  150. package/test/types/dts_drift.js +0 -124
  151. package/test/types/surface_honesty.js +0 -102
  152. package/test/util/id.js +0 -72
  153. package/test/util/js.js +0 -76
  154. package/test/util/preconditions.js +0 -79
  155. package/test/vcjwt/interop.js +0 -126
@@ -1,131 +0,0 @@
1
- 'use strict'
2
-
3
- var should = require('chai').should()
4
- var bsv = require('../..')
5
- var buffer = require('buffer')
6
- var Base58 = bsv.encoding.Base58
7
-
8
- describe('Base58', function () {
9
- var buf = Buffer.from([0, 1, 2, 3, 253, 254, 255])
10
- var enc = '1W7N4RuG'
11
-
12
- it('should make an instance with "new"', function () {
13
- var b58 = new Base58()
14
- should.exist(b58)
15
- })
16
-
17
- it('validates characters with no false negatives', function () {
18
- Base58.validCharacters(
19
- '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
20
- ).should.equal(true)
21
- })
22
- it('validates characters from buffer', function () {
23
- Base58.validCharacters(
24
- buffer.Buffer.from('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz')
25
- ).should.equal(true)
26
- })
27
-
28
- it('some characters are invalid (no false positives)', function () {
29
- Base58.validCharacters('!@#%^$&*()\\').should.equal(false)
30
- })
31
-
32
- it('should make an instance without "new"', function () {
33
- var b58 = Base58()
34
- should.exist(b58)
35
- })
36
-
37
- it('should allow this handy syntax', function () {
38
- Base58(buf).toString().should.equal(enc)
39
- Base58(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'))
40
- })
41
-
42
- describe('#set', function () {
43
- it('should set a blank buffer', function () {
44
- Base58().set({
45
- buf: buffer.Buffer.from([])
46
- })
47
- })
48
- })
49
-
50
- describe('@encode', function () {
51
- it('should encode the buffer accurately', function () {
52
- Base58.encode(buf).should.equal(enc)
53
- })
54
-
55
- it('should throw an error when the Input is not a buffer', function () {
56
- (function () {
57
- Base58.encode('string')
58
- }).should.throw('Input should be a buffer')
59
- })
60
- })
61
-
62
- describe('@decode', function () {
63
- it('should decode this encoded value correctly', function () {
64
- Base58.decode(enc).toString('hex').should.equal(buf.toString('hex'))
65
- })
66
-
67
- it('should throw an error when Input is not a string', function () {
68
- (function () {
69
- Base58.decode(5)
70
- }).should.throw('Input should be a string')
71
- })
72
- })
73
-
74
- describe('#fromBuffer', function () {
75
- it('should not fail', function () {
76
- should.exist(Base58().fromBuffer(buf))
77
- })
78
-
79
- it('should set buffer', function () {
80
- var b58 = Base58().fromBuffer(buf)
81
- b58.buf.toString('hex').should.equal(buf.toString('hex'))
82
- })
83
- })
84
-
85
- describe('@fromBuffer', function () {
86
- it('should set buffer', function () {
87
- var b58 = Base58.fromBuffer(buf)
88
- b58.toHex().should.equal(buf.toString('hex'))
89
- })
90
- })
91
-
92
- describe('@fromHex', function () {
93
- it('should set buffer', function () {
94
- var b58 = Base58.fromHex(buf.toString('hex'))
95
- b58.buf.toString('hex').should.equal(buf.toString('hex'))
96
- })
97
- })
98
-
99
- describe('#fromString', function () {
100
- it('should convert this known string to a buffer', function () {
101
- Base58().fromString(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'))
102
- })
103
- })
104
-
105
- describe('#fromString', function () {
106
- it('should convert this known string to a buffer', function () {
107
- Base58.fromString(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'))
108
- })
109
- })
110
-
111
- describe('#toBuffer', function () {
112
- it('should return the buffer', function () {
113
- var b58 = Base58(buf)
114
- b58.buf.toString('hex').should.equal(buf.toString('hex'))
115
- })
116
- })
117
-
118
- describe('#toHex', function () {
119
- it('should return the hex', function () {
120
- var b58 = Base58(buf)
121
- b58.toHex().should.equal(buf.toString('hex'))
122
- })
123
- })
124
-
125
- describe('#toString', function () {
126
- it('should return the buffer', function () {
127
- var b58 = Base58(buf)
128
- b58.toString().should.equal(enc)
129
- })
130
- })
131
- })
@@ -1,145 +0,0 @@
1
- 'use strict'
2
-
3
- var should = require('chai').should()
4
- var bsv = require('../..')
5
- var Base58Check = bsv.encoding.Base58Check
6
- var Base58 = bsv.encoding.Base58
7
-
8
- describe('Base58Check', function () {
9
- var buf = Buffer.from([0, 1, 2, 3, 253, 254, 255])
10
- var enc = '14HV44ipwoaqfg'
11
-
12
- it('should make an instance with "new"', function () {
13
- var b58 = new Base58Check()
14
- should.exist(b58)
15
- })
16
-
17
- it('can validate a serialized string', function () {
18
- var address = '3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy'
19
- Base58Check.validChecksum(address).should.equal(true)
20
- address = address + 'a'
21
- Base58Check.validChecksum(address).should.equal(false)
22
- })
23
-
24
- it('should make an instance without "new"', function () {
25
- var b58 = Base58Check()
26
- should.exist(b58)
27
- })
28
-
29
- it('should allow this handy syntax', function () {
30
- Base58Check(buf).toString().should.equal(enc)
31
- Base58Check(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'))
32
- })
33
-
34
- describe('#set', function () {
35
- it('should set a buf', function () {
36
- should.exist(Base58Check().set({ buf: buf }).buf)
37
- })
38
- })
39
-
40
- describe('@encode', function () {
41
- it('should encode the buffer accurately', function () {
42
- Base58Check.encode(buf).should.equal(enc)
43
- })
44
-
45
- it('should throw an error when the input is not a buffer', function () {
46
- (function () {
47
- Base58Check.encode('string')
48
- }).should.throw('Input must be a buffer')
49
- })
50
- })
51
-
52
- describe('@decode', function () {
53
- it('should decode this encoded value correctly', function () {
54
- Base58Check.decode(enc).toString('hex').should.equal(buf.toString('hex'))
55
- })
56
-
57
- it('should throw an error when input is not a string', function () {
58
- (function () {
59
- Base58Check.decode(5)
60
- }).should.throw('Input must be a string')
61
- })
62
-
63
- it('should throw an error when input is too short', function () {
64
- (function () {
65
- Base58Check.decode(enc.slice(0, 1))
66
- }).should.throw('Input string too short')
67
- })
68
-
69
- it('should throw an error when there is a checksum mismatch', function () {
70
- var buf2 = Base58.decode(enc)
71
- buf2[0] = buf2[0] + 1
72
- var enc2 = Base58.encode(buf2);
73
- (function () {
74
- Base58Check.decode(enc2)
75
- }).should.throw('Checksum mismatch')
76
- })
77
- })
78
-
79
- describe('#fromBuffer', function () {
80
- it('should not fail', function () {
81
- should.exist(Base58Check().fromBuffer(buf))
82
- })
83
-
84
- it('should set buffer', function () {
85
- var b58 = Base58Check().fromBuffer(buf)
86
- b58.toHex().should.equal(buf.toString('hex'))
87
- })
88
- })
89
-
90
- describe('@fromBuffer', function () {
91
- it('should set buffer', function () {
92
- var b58 = Base58Check.fromBuffer(buf)
93
- b58.buf.toString('hex').should.equal(buf.toString('hex'))
94
- })
95
- })
96
-
97
- describe('@fromHex', function () {
98
- it('should set hex', function () {
99
- var b58 = Base58Check.fromHex(buf.toString('hex'))
100
- b58.buf.toString('hex').should.equal(buf.toString('hex'))
101
- })
102
- })
103
-
104
- describe('#fromString', function () {
105
- it('should convert this known string to a buffer', function () {
106
- Base58Check().fromString(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'))
107
- })
108
- })
109
-
110
- describe('@fromString', function () {
111
- it('should convert this known string to a buffer', function () {
112
- Base58Check.fromString(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'))
113
- })
114
-
115
- // Regression: the static factory used to return a plain Base58 (no checksum),
116
- // so encode -> fromString -> toString silently round-tripped to a different
117
- // string. It must return a Base58Check that re-encodes with the checksum.
118
- it('returns a Base58Check that round-trips through toString', function () {
119
- var r = Base58Check.fromString(enc)
120
- r.should.be.instanceof(Base58Check)
121
- r.toString().should.equal(enc)
122
- })
123
- })
124
-
125
- describe('#toBuffer', function () {
126
- it('should return the buffer', function () {
127
- var b58 = Base58Check(buf)
128
- b58.toBuffer().toString('hex').should.equal(buf.toString('hex'))
129
- })
130
- })
131
-
132
- describe('#toHex', function () {
133
- it('should return the hex', function () {
134
- var b58 = Base58Check(buf)
135
- b58.toHex().should.equal(buf.toString('hex'))
136
- })
137
- })
138
-
139
- describe('#toString', function () {
140
- it('should return the buffer', function () {
141
- var b58 = Base58Check(buf)
142
- b58.toString().should.equal(enc)
143
- })
144
- })
145
- })
@@ -1,328 +0,0 @@
1
- 'use strict'
2
-
3
- var should = require('chai').should()
4
- var bsv = require('../..')
5
- var BufferWriter = bsv.encoding.BufferWriter
6
- var BufferReader = bsv.encoding.BufferReader
7
- var BN = bsv.crypto.BN
8
-
9
- describe('BufferReader', function () {
10
- it('should make a new BufferReader', function () {
11
- var br = new BufferReader()
12
- should.exist(br)
13
- br = BufferReader()
14
- should.exist(br)
15
- })
16
-
17
- it('should create a new bufferreader with a buffer', function () {
18
- var buf = Buffer.alloc(0)
19
- var br = new BufferReader(buf)
20
- should.exist(br)
21
- Buffer.isBuffer(br.buf).should.equal(true)
22
- })
23
- it('should fail for invalid object', function () {
24
- var fail = function () {
25
- return new BufferReader(5)
26
- }
27
- fail.should.throw('Unrecognized argument for BufferReader')
28
- })
29
-
30
- describe('#set', function () {
31
- it('should set pos', function () {
32
- should.exist(BufferReader().set({
33
- pos: 1
34
- }).pos)
35
- })
36
- })
37
-
38
- describe('#eof', function () {
39
- it('should return true for a blank br', function () {
40
- var br = new BufferReader(Buffer.from([]))
41
- br.finished().should.equal(true)
42
- })
43
- })
44
-
45
- describe('read', function () {
46
- it('should return the same buffer', function () {
47
- var buf = Buffer.from([0])
48
- var br = new BufferReader(buf)
49
- br.readAll().toString('hex').should.equal(buf.toString('hex'))
50
- })
51
-
52
- it('should return a buffer of this length', function () {
53
- var buf = Buffer.alloc(10)
54
- buf.fill(0)
55
- var br = new BufferReader(buf)
56
- var buf2 = br.read(2)
57
- buf2.length.should.equal(2)
58
- br.finished().should.equal(false)
59
- br.pos.should.equal(2)
60
- })
61
-
62
- it('should work with 0 length', function () {
63
- var buf = Buffer.alloc(10)
64
- buf.fill(1)
65
- var br = new BufferReader(buf)
66
- var buf2 = br.read(0)
67
- buf2.length.should.equal(0)
68
- br.finished().should.equal(false)
69
- buf2.toString('hex').should.equal('')
70
- })
71
- })
72
-
73
- describe('readVarLengthBuffer', function () {
74
- it('returns correct buffer', function () {
75
- var buf = Buffer.from('73010000003766404f00000000b305434f00000000f203' +
76
- '0000f1030000001027000048ee00000064000000004653656520626974636f696' +
77
- 'e2e6f72672f666562323020696620796f7520686176652074726f75626c652063' +
78
- '6f6e6e656374696e6720616674657220323020466562727561727900473045022' +
79
- '1008389df45f0703f39ec8c1cc42c13810ffcae14995bb648340219e353b63b53' +
80
- 'eb022009ec65e1c1aaeec1fd334c6b684bde2b3f573060d5b70c3a46723326e4e' +
81
- '8a4f1', 'hex')
82
- var br = new BufferReader(buf)
83
- var b1 = br.readVarLengthBuffer()
84
- b1.toString('hex').should.equal('010000003766404f00000000b305434f000' +
85
- '00000f2030000f1030000001027000048ee000000640000000046536565206269' +
86
- '74636f696e2e6f72672f666562323020696620796f7520686176652074726f756' +
87
- '26c6520636f6e6e656374696e6720616674657220323020466562727561727900')
88
- var b2 = br.readVarLengthBuffer()
89
- b2.toString('hex').should.equal('30450221008389df45f0703f39ec8c1cc42' +
90
- 'c13810ffcae14995bb648340219e353b63b53eb022009ec65e1c1aaeec1fd334c' +
91
- '6b684bde2b3f573060d5b70c3a46723326e4e8a4f1')
92
- })
93
- it('fails on length too big', function () {
94
- var buf = Buffer.from('0a00', 'hex')
95
- var br = new BufferReader(buf)
96
- br.readVarLengthBuffer.bind(br).should.throw('Invalid length while reading varlength buffer')
97
- })
98
- })
99
-
100
- describe('#readUInt8', function () {
101
- it('should return 1', function () {
102
- var buf = Buffer.alloc(1)
103
- buf.writeUInt8(1, 0)
104
- var br = new BufferReader(buf)
105
- br.readUInt8().should.equal(1)
106
- })
107
- })
108
-
109
- describe('#readUInt16BE', function () {
110
- it('should return 1', function () {
111
- var buf = Buffer.alloc(2)
112
- buf.writeUInt16BE(1, 0)
113
- var br = new BufferReader(buf)
114
- br.readUInt16BE().should.equal(1)
115
- })
116
- })
117
-
118
- describe('#readUInt16LE', function () {
119
- it('should return 1', function () {
120
- var buf = Buffer.alloc(2)
121
- buf.writeUInt16LE(1, 0)
122
- var br = new BufferReader(buf)
123
- br.readUInt16LE().should.equal(1)
124
- })
125
- })
126
-
127
- describe('#readUInt32BE', function () {
128
- it('should return 1', function () {
129
- var buf = Buffer.alloc(4)
130
- buf.writeUInt32BE(1, 0)
131
- var br = new BufferReader(buf)
132
- br.readUInt32BE().should.equal(1)
133
- })
134
- })
135
-
136
- describe('#readUInt32LE', function () {
137
- it('should return 1', function () {
138
- var buf = Buffer.alloc(4)
139
- buf.writeUInt32LE(1, 0)
140
- var br = new BufferReader(buf)
141
- br.readUInt32LE().should.equal(1)
142
- })
143
- })
144
-
145
- describe('#readUInt64BEBN', function () {
146
- it('should return 1', function () {
147
- var buf = Buffer.alloc(8)
148
- buf.fill(0)
149
- buf.writeUInt32BE(1, 4)
150
- var br = new BufferReader(buf)
151
- br.readUInt64BEBN().toNumber().should.equal(1)
152
- })
153
-
154
- it('should return 2^64', function () {
155
- var buf = Buffer.alloc(8)
156
- buf.fill(0xff)
157
- var br = new BufferReader(buf)
158
- br.readUInt64BEBN().toNumber().should.equal(Math.pow(2, 64))
159
- })
160
- })
161
-
162
- describe('#readUInt64LEBN', function () {
163
- it('should return 1', function () {
164
- var buf = Buffer.alloc(8)
165
- buf.fill(0)
166
- buf.writeUInt32LE(1, 0)
167
- var br = new BufferReader(buf)
168
- br.readUInt64LEBN().toNumber().should.equal(1)
169
- })
170
-
171
- it('should return 10 BSV', function () {
172
- var tenbsv = 10 * 1e8
173
- var tenbsvBuffer = Buffer.from('00ca9a3b00000000', 'hex')
174
- var br = new BufferReader(tenbsvBuffer)
175
- br.readUInt64LEBN().toNumber().should.equal(tenbsv)
176
- })
177
-
178
- it('should return 2^30', function () {
179
- var buf = Buffer.alloc(8)
180
- buf.fill(0)
181
- buf.writeUInt32LE(Math.pow(2, 30), 0)
182
- var br = new BufferReader(buf)
183
- br.readUInt64LEBN().toNumber().should.equal(Math.pow(2, 30))
184
- })
185
-
186
- it('should return 2^32 + 1', function () {
187
- var num = Math.pow(2, 32) + 1
188
- var numBuffer = Buffer.from('0100000001000000', 'hex')
189
- var br = new BufferReader(numBuffer)
190
- br.readUInt64LEBN().toNumber().should.equal(num)
191
- })
192
-
193
- it('should return max number of satoshis', function () {
194
- var maxSatoshis = 21000000 * 1e8
195
- var maxSatoshisBuffer = Buffer.from('0040075af0750700', 'hex')
196
- var br = new BufferReader(maxSatoshisBuffer)
197
- br.readUInt64LEBN().toNumber().should.equal(maxSatoshis)
198
- })
199
-
200
- it('should return 2^53 - 1', function () {
201
- var maxSafe = Math.pow(2, 53) - 1
202
- var maxSafeBuffer = Buffer.from('ffffffffffff1f00', 'hex')
203
- var br = new BufferReader(maxSafeBuffer)
204
- br.readUInt64LEBN().toNumber().should.equal(maxSafe)
205
- })
206
-
207
- it('should return 2^53', function () {
208
- var bn = new BN('20000000000000', 16)
209
- var bnBuffer = Buffer.from('0000000000002000', 'hex')
210
- var br = new BufferReader(bnBuffer)
211
- var readbn = br.readUInt64LEBN()
212
- readbn.cmp(bn).should.equal(0)
213
- })
214
-
215
- it('should return 0', function () {
216
- var buf = Buffer.alloc(8)
217
- buf.fill(0)
218
- var br = new BufferReader(buf)
219
- br.readUInt64LEBN().toNumber().should.equal(0)
220
- })
221
-
222
- it('should return 2^64', function () {
223
- var buf = Buffer.alloc(8)
224
- buf.fill(0xff)
225
- var br = new BufferReader(buf)
226
- br.readUInt64LEBN().toNumber().should.equal(Math.pow(2, 64))
227
- })
228
- })
229
-
230
- describe('#readVarintBuf', function () {
231
- it('should read a 1 byte varint', function () {
232
- var buf = Buffer.from([50])
233
- var br = new BufferReader(buf)
234
- br.readVarintBuf().length.should.equal(1)
235
- })
236
-
237
- it('should read a 3 byte varint', function () {
238
- var buf = Buffer.from([253, 253, 0])
239
- var br = new BufferReader(buf)
240
- br.readVarintBuf().length.should.equal(3)
241
- })
242
-
243
- it('should read a 5 byte varint', function () {
244
- var buf = Buffer.from([254, 0, 0, 0, 0])
245
- buf.writeUInt32LE(50000, 1)
246
- var br = new BufferReader(buf)
247
- br.readVarintBuf().length.should.equal(5)
248
- })
249
-
250
- it('should read a 9 byte varint', function () {
251
- var buf = BufferWriter().writeVarintBN(new BN(Math.pow(2, 54).toString())).concat()
252
- var br = new BufferReader(buf)
253
- br.readVarintBuf().length.should.equal(9)
254
- })
255
- })
256
-
257
- describe('#readVarintNum', function () {
258
- it('should read a 1 byte varint', function () {
259
- var buf = Buffer.from([50])
260
- var br = new BufferReader(buf)
261
- br.readVarintNum().should.equal(50)
262
- })
263
-
264
- it('should read a 3 byte varint', function () {
265
- var buf = Buffer.from([253, 253, 0])
266
- var br = new BufferReader(buf)
267
- br.readVarintNum().should.equal(253)
268
- })
269
-
270
- it('should read a 5 byte varint', function () {
271
- var buf = Buffer.from([254, 0, 0, 0, 0])
272
- buf.writeUInt32LE(50000, 1)
273
- var br = new BufferReader(buf)
274
- br.readVarintNum().should.equal(50000)
275
- })
276
-
277
- it('should throw an error on a 9 byte varint over the javascript uint precision limit', function () {
278
- var buf = BufferWriter().writeVarintBN(new BN(Math.pow(2, 54).toString())).concat()
279
- var br = new BufferReader(buf);
280
- (function () {
281
- br.readVarintNum()
282
- }).should.throw('number too large to retain precision - use readVarintBN')
283
- })
284
-
285
- it('should not throw an error on a 9 byte varint not over the javascript uint precision limit', function () {
286
- var buf = BufferWriter().writeVarintBN(new BN(Math.pow(2, 53).toString())).concat()
287
- var br = new BufferReader(buf);
288
- (function () {
289
- br.readVarintNum()
290
- }).should.not.throw('number too large to retain precision - use readVarintBN')
291
- })
292
- })
293
-
294
- describe('#readVarintBN', function () {
295
- it('should read a 1 byte varint', function () {
296
- var buf = Buffer.from([50])
297
- var br = new BufferReader(buf)
298
- br.readVarintBN().toNumber().should.equal(50)
299
- })
300
-
301
- it('should read a 3 byte varint', function () {
302
- var buf = Buffer.from([253, 253, 0])
303
- var br = new BufferReader(buf)
304
- br.readVarintBN().toNumber().should.equal(253)
305
- })
306
-
307
- it('should read a 5 byte varint', function () {
308
- var buf = Buffer.from([254, 0, 0, 0, 0])
309
- buf.writeUInt32LE(50000, 1)
310
- var br = new BufferReader(buf)
311
- br.readVarintBN().toNumber().should.equal(50000)
312
- })
313
-
314
- it('should read a 9 byte varint', function () {
315
- var buf = Buffer.concat([Buffer.from([255]), Buffer.from('ffffffffffffffff', 'hex')])
316
- var br = new BufferReader(buf)
317
- br.readVarintBN().toNumber().should.equal(Math.pow(2, 64))
318
- })
319
- })
320
-
321
- describe('#reverse', function () {
322
- it('should reverse this [0, 1]', function () {
323
- var buf = Buffer.from([0, 1])
324
- var br = new BufferReader(buf)
325
- br.reverse().readAll().toString('hex').should.equal('0100')
326
- })
327
- })
328
- })