@smartledger/bsv 9.0.0 → 9.1.1

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 +164 -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 +53 -9
  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
package/test/publickey.js DELETED
@@ -1,411 +0,0 @@
1
- 'use strict'
2
-
3
- var should = require('chai').should()
4
- var expect = require('chai').expect
5
-
6
- var bsv = require('..')
7
- var Point = bsv.crypto.Point
8
- var BN = bsv.crypto.BN
9
- var PublicKey = bsv.PublicKey
10
- var PrivateKey = bsv.PrivateKey
11
- var Address = bsv.Address
12
- var Networks = bsv.Networks
13
-
14
- describe('PublicKey', function () {
15
- var invalidPoint = '0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
16
-
17
- describe('validating errors on creation', function () {
18
- it('errors if data is missing', function () {
19
- (function () {
20
- return new PublicKey()
21
- }).should.throw('First argument is required, please include public key data.')
22
- })
23
-
24
- it('errors if an invalid point is provided', function () {
25
- (function () {
26
- return new PublicKey(invalidPoint)
27
- }).should.throw('Point does not lie on the curve')
28
- })
29
-
30
- it('errors if a point not on the secp256k1 curve is provided', function () {
31
- (function () {
32
- return new PublicKey(new Point(1000, 1000))
33
- }).should.throw('Point does not lie on the curve')
34
- })
35
-
36
- it('errors if the argument is of an unrecognized type', function () {
37
- (function () {
38
- return new PublicKey(new Error())
39
- }).should.throw('First argument is an unrecognized data format.')
40
- })
41
- })
42
-
43
- describe('instantiation', function () {
44
- it('from a private key', function () {
45
- var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'
46
- var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'
47
- var privkey = new PrivateKey(new BN(Buffer.from(privhex, 'hex')))
48
- var pk = new PublicKey(privkey)
49
- pk.toString().should.equal(pubhex)
50
- })
51
-
52
- it('problematic secp256k1 public keys', function () {
53
- var knownKeys = [
54
- {
55
- wif: 'KzsjKq2FVqVuQv2ueHVFuB65A9uEZ6S1L6F8NuokCrE3V3kE3Ack',
56
- priv: '6d1229a6b24c2e775c062870ad26bc261051e0198c67203167273c7c62538846',
57
- pub: '03d6106302d2698d6a41e9c9a114269e7be7c6a0081317de444bb2980bf9265a01',
58
- pubx: 'd6106302d2698d6a41e9c9a114269e7be7c6a0081317de444bb2980bf9265a01',
59
- puby: 'e05fb262e64b108991a29979809fcef9d3e70cafceb3248c922c17d83d66bc9d'
60
- },
61
- {
62
- wif: 'L5MgSwNB2R76xBGorofRSTuQFd1bm3hQMFVf3u2CneFom8u1Yt7G',
63
- priv: 'f2cc9d2b008927db94b89e04e2f6e70c180e547b3e5e564b06b8215d1c264b53',
64
- pub: '03e275faa35bd1e88f5df6e8f9f6edb93bdf1d65f4915efc79fd7a726ec0c21700',
65
- pubx: 'e275faa35bd1e88f5df6e8f9f6edb93bdf1d65f4915efc79fd7a726ec0c21700',
66
- puby: '367216cb35b086e6686d69dddd822a8f4d52eb82ac5d9de18fdcd9bf44fa7df7'
67
- }
68
- ]
69
-
70
- for (var i = 0; i < knownKeys.length; i++) {
71
- var privkey = new PrivateKey(knownKeys[i].wif)
72
- var pubkey = privkey.toPublicKey()
73
- pubkey.toString().should.equal(knownKeys[i].pub)
74
- pubkey.point.x.toString('hex').should.equal(knownKeys[i].pubx)
75
- pubkey.point.y.toString('hex').should.equal(knownKeys[i].puby)
76
- }
77
- })
78
-
79
- it('from a compressed public key', function () {
80
- var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'
81
- var publicKey = new PublicKey(publicKeyHex)
82
- publicKey.toString().should.equal(publicKeyHex)
83
- })
84
-
85
- it('from another publicKey', function () {
86
- var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'
87
- var publicKey = new PublicKey(publicKeyHex)
88
- var publicKey2 = new PublicKey(publicKey)
89
- publicKey.should.equal(publicKey2)
90
- })
91
-
92
- it('sets the network to defaultNetwork if none provided', function () {
93
- var publicKeyHex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'
94
- var publicKey = new PublicKey(publicKeyHex)
95
- publicKey.network.should.equal(Networks.defaultNetwork)
96
- })
97
-
98
- it('from a hex encoded DER string', function () {
99
- var pk = new PublicKey('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
100
- should.exist(pk.point)
101
- pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
102
- })
103
-
104
- it('from a hex encoded DER buffer', function () {
105
- var pk = new PublicKey(Buffer.from('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'))
106
- should.exist(pk.point)
107
- pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
108
- })
109
-
110
- it('from a point', function () {
111
- var p = new Point('86a80a5a2bfc48dddde2b0bd88bd56b0b6ddc4e6811445b175b90268924d7d48',
112
- '3b402dfc89712cfe50963e670a0598e6b152b3cd94735001cdac6794975d3afd')
113
- var a = new PublicKey(p)
114
- should.exist(a.point)
115
- a.point.toString().should.equal(p.toString())
116
- var c = new PublicKey(p)
117
- should.exist(c.point)
118
- c.point.toString().should.equal(p.toString())
119
- })
120
- })
121
-
122
- describe('#getValidationError', function () {
123
- it('should recieve an invalid point error', function () {
124
- var error = PublicKey.getValidationError(invalidPoint)
125
- should.exist(error)
126
- error.message.should.equal('Point does not lie on the curve')
127
- })
128
-
129
- it('should recieve a boolean as false', function () {
130
- var valid = PublicKey.isValid(invalidPoint)
131
- valid.should.equal(false)
132
- })
133
-
134
- it('should recieve a boolean as true for uncompressed', function () {
135
- var valid = PublicKey.isValid('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
136
- valid.should.equal(true)
137
- })
138
-
139
- it('should recieve a boolean as true for compressed', function () {
140
- var valid = PublicKey.isValid('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
141
- valid.should.equal(true)
142
- })
143
- })
144
-
145
- describe('#fromPoint', function () {
146
- it('should instantiate from a point', function () {
147
- var p = new Point('86a80a5a2bfc48dddde2b0bd88bd56b0b6ddc4e6811445b175b90268924d7d48',
148
- '3b402dfc89712cfe50963e670a0598e6b152b3cd94735001cdac6794975d3afd')
149
- var b = PublicKey.fromPoint(p)
150
- should.exist(b.point)
151
- b.point.toString().should.equal(p.toString())
152
- })
153
-
154
- it('should error because paramater is not a point', function () {
155
- (function () {
156
- PublicKey.fromPoint(new Error())
157
- }).should.throw('First argument must be an instance of Point.')
158
- })
159
- })
160
-
161
- describe('#json/object', function () {
162
- it('should input/ouput json', function () {
163
- var json = JSON.stringify({
164
- x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a',
165
- y: '7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
166
- compressed: false
167
- })
168
- var pubkey = new PublicKey(JSON.parse(json))
169
- JSON.stringify(pubkey).should.deep.equal(json)
170
- })
171
-
172
- it('fails if "y" is not provided', function () {
173
- expect(function () {
174
- return new PublicKey({
175
- x: '1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'
176
- })
177
- }).to.throw()
178
- })
179
-
180
- it('fails if invalid JSON is provided', function () {
181
- expect(function () {
182
- return PublicKey._transformJSON('¹')
183
- }).to.throw()
184
- })
185
-
186
- it('works for X starting with 0x00', function () {
187
- var a = new PublicKey('030589ee559348bd6a7325994f9c8eff12bd5d73cc683142bd0dd1a17abc99b0dc')
188
- var b = new PublicKey('03' + a.toObject().x)
189
- b.toString().should.equal(a.toString())
190
- })
191
- })
192
-
193
- describe('#fromPrivateKey', function () {
194
- it('should make a public key from a privkey', function () {
195
- should.exist(PublicKey.fromPrivateKey(PrivateKey.fromRandom()))
196
- })
197
-
198
- it('should error because not an instance of privkey', function () {
199
- (function () {
200
- PublicKey.fromPrivateKey(new Error())
201
- }).should.throw('Must be an instance of PrivateKey')
202
- })
203
- })
204
-
205
- describe('#fromBuffer', function () {
206
- it('should parse this uncompressed public key', function () {
207
- var pk = PublicKey.fromBuffer(Buffer.from('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'))
208
- pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
209
- pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
210
- })
211
-
212
- it('should parse this compressed public key', function () {
213
- var pk = PublicKey.fromBuffer(Buffer.from('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'))
214
- pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
215
- pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
216
- })
217
-
218
- it('should throw an error on this invalid public key', function () {
219
- (function () {
220
- PublicKey.fromBuffer(Buffer.from('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'))
221
- }).should.throw()
222
- })
223
-
224
- it('should throw error because not a buffer', function () {
225
- (function () {
226
- PublicKey.fromBuffer('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
227
- }).should.throw('Must be a buffer of DER encoded public key')
228
- })
229
-
230
- it('should throw error because buffer is the incorrect length', function () {
231
- (function () {
232
- PublicKey.fromBuffer(Buffer.from('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a34112', 'hex'))
233
- }).should.throw('Length of x and y must be 32 bytes')
234
- })
235
- })
236
-
237
- describe('#fromDER', function () {
238
- it('should parse this uncompressed public key', function () {
239
- var pk = PublicKey.fromDER(Buffer.from('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'))
240
- pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
241
- pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
242
- })
243
-
244
- it('should parse this compressed public key', function () {
245
- var pk = PublicKey.fromDER(Buffer.from('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'))
246
- pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
247
- pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
248
- })
249
-
250
- it('should throw an error on this invalid public key', function () {
251
- (function () {
252
- PublicKey.fromDER(Buffer.from('091ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'))
253
- }).should.throw()
254
- })
255
- })
256
-
257
- describe('#fromString', function () {
258
- it('should parse this known valid public key', function () {
259
- var pk = PublicKey.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
260
- pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
261
- pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
262
- })
263
- })
264
-
265
- describe('#fromHex', function () {
266
- it('should parse this known valid public key', function () {
267
- var pk = PublicKey.fromHex('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
268
- pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
269
- pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
270
- })
271
- })
272
-
273
- describe('#fromX', function () {
274
- it('should create this known public key', function () {
275
- var x = BN.fromBuffer(Buffer.from('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'))
276
- var pk = PublicKey.fromX(true, x)
277
- pk.point.getX().toString(16).should.equal('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
278
- pk.point.getY().toString(16).should.equal('7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
279
- })
280
-
281
- it('should error because odd was not included as a param', function () {
282
- var x = BN.fromBuffer(Buffer.from('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
283
- (function () {
284
- return PublicKey.fromX(null, x)
285
- }).should.throw('Must specify whether y is odd or not (true or false)')
286
- })
287
- })
288
-
289
- describe('#toBuffer', function () {
290
- it('should return this compressed DER format', function () {
291
- var x = BN.fromBuffer(Buffer.from('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'))
292
- var pk = PublicKey.fromX(true, x)
293
- pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
294
- })
295
-
296
- it('should return this uncompressed DER format', function () {
297
- var x = BN.fromBuffer(Buffer.from('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'))
298
- var pk = PublicKey.fromX(true, x)
299
- pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
300
- })
301
- })
302
-
303
- describe('#toDER', function () {
304
- it('should return this compressed DER format', function () {
305
- var x = BN.fromBuffer(Buffer.from('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'))
306
- var pk = PublicKey.fromX(true, x)
307
- pk.toDER().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
308
- })
309
-
310
- it('should return this uncompressed DER format', function () {
311
- var pk = PublicKey.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
312
- pk.toDER().toString('hex').should.equal('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
313
- })
314
- })
315
-
316
- describe('#toAddress', function () {
317
- it('should output this known mainnet address correctly', function () {
318
- var pk = new PublicKey('03c87bd0e162f26969da8509cafcb7b8c8d202af30b928c582e263dd13ee9a9781')
319
- var address = pk.toAddress('livenet')
320
- address.toString().should.equal('1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT')
321
- })
322
-
323
- it('should output this known testnet address correctly', function () {
324
- var pk = new PublicKey('0293126ccc927c111b88a0fe09baa0eca719e2a3e087e8a5d1059163f5c566feef')
325
- var address = pk.toAddress('testnet')
326
- address.toString().should.equal('mtX8nPZZdJ8d3QNLRJ1oJTiEi26Sj6LQXS')
327
- })
328
- })
329
-
330
- describe('hashes', function () {
331
- it('should pass this test', function () {
332
- // wif private key, address
333
- // see: https://github.com/bitcoin/bitcoin/blob/master/src/test/key_tests.cpp#L20
334
- var data = [
335
- ['5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj', '1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ'],
336
- ['5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3', '1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ'],
337
- ['Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw', '1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs'],
338
- ['L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g', '1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs']
339
- ]
340
-
341
- data.forEach(function (d) {
342
- var publicKey = PrivateKey.fromWIF(d[0]).toPublicKey()
343
- var address = Address.fromString(d[1])
344
- address.hashBuffer.should.deep.equal(publicKey._getID())
345
- })
346
- })
347
- })
348
-
349
- describe('#toString', function () {
350
- it('should print this known public key', function () {
351
- var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'
352
- var pk = PublicKey.fromString(hex)
353
- pk.toString().should.equal(hex)
354
- })
355
- })
356
-
357
- describe('#toHex', function () {
358
- it('should print this known public key', function () {
359
- var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'
360
- var pk = PublicKey.fromHex(hex)
361
- pk.toHex().should.equal(hex)
362
- })
363
- })
364
-
365
- describe('#inspect', function () {
366
- it('should output known uncompressed pubkey for console', function () {
367
- var pubkey = PublicKey.fromString('041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341')
368
- pubkey.inspect().should.equal('<PublicKey: 041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341, uncompressed>')
369
- })
370
-
371
- it('should output known compressed pubkey for console', function () {
372
- var pubkey = PublicKey.fromString('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a')
373
- pubkey.inspect().should.equal('<PublicKey: 031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a>')
374
- })
375
-
376
- it('should output known compressed pubkey with network for console', function () {
377
- var privkey = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m')
378
- var pubkey = new PublicKey(privkey)
379
- pubkey.inspect().should.equal('<PublicKey: 03c87bd0e162f26969da8509cafcb7b8c8d202af30b928c582e263dd13ee9a9781>')
380
- })
381
- })
382
-
383
- describe('#validate', function () {
384
- it('should not have an error if pubkey is valid', function () {
385
- var hex = '031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a'
386
- expect(function () {
387
- return PublicKey.fromString(hex)
388
- }).to.not.throw()
389
- })
390
-
391
- it('should throw an error if pubkey is invalid', function () {
392
- var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a0000000000000000000000000000000000000000000000000000000000000000';
393
- (function () {
394
- return PublicKey.fromString(hex)
395
- }).should.throw('Invalid y value for curve.')
396
- })
397
-
398
- it('should throw an error if pubkey is invalid', function () {
399
- var hex = '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a00000000000000000000000000000000000000000000000000000000000000FF';
400
- (function () {
401
- return PublicKey.fromString(hex)
402
- }).should.throw('Invalid y value for curve.')
403
- })
404
-
405
- it('should throw an error if pubkey is infinity', function () {
406
- (function () {
407
- return new PublicKey(Point.getG().mul(Point.getN()))
408
- }).should.throw('Point cannot be equal to Infinity')
409
- })
410
- })
411
- })
@@ -1,215 +0,0 @@
1
- 'use strict'
2
-
3
- /* global describe, it */
4
-
5
- // Regressions for three wrong calls that shipped through 7.5.8, each of which
6
- // no test exercised.
7
- //
8
- // Two were live: Transaction#fromObject threw on an input its own
9
- // checkArgument accepts, and OP_CHECKSEQUENCEVERIFY failed every script with a
10
- // misleading error. Those two tests fail on the unfixed code.
11
- //
12
- // The third — sFromPreimage's toBuffer call — is LATENT, and the tests say so
13
- // rather than pretending otherwise. See the note on that block.
14
-
15
- require('chai').should()
16
- var bsv = require('../')
17
- var BN = bsv.crypto.BN
18
-
19
- describe('regressions', function () {
20
- describe('Transaction#fromObject with a Transaction', function () {
21
- // `fromObject` read `transaction.toObject()` where it meant
22
- // `arg.toObject()`, so the branch that exists specifically to accept a
23
- // Transaction was the only branch that could not work. checkArgument
24
- // admits the input; the next line threw TypeError on it.
25
- it('accepts a Transaction, which its own checkArgument allows', function () {
26
- var key = bsv.PrivateKey.fromRandom()
27
- var source = new bsv.Transaction().to(key.toAddress(), 1000)
28
-
29
- var copy = new bsv.Transaction().fromObject(source)
30
-
31
- copy.outputs.length.should.equal(1)
32
- copy.outputs[0].satoshis.should.equal(1000)
33
- copy.toObject().should.deep.equal(source.toObject())
34
- })
35
-
36
- it('still accepts the plain object form', function () {
37
- var key = bsv.PrivateKey.fromRandom()
38
- var source = new bsv.Transaction().to(key.toAddress(), 1000)
39
-
40
- var copy = new bsv.Transaction().fromObject(source.toObject())
41
-
42
- copy.toObject().should.deep.equal(source.toObject())
43
- })
44
- })
45
-
46
- describe('BN#toBuffer with the native signature', function () {
47
- // lib/crypto/bn.js REPLACES toBuffer with an options-object form. Calling
48
- // it with bn.js's native (endian, length) signature silently ignores the
49
- // length: 'be' lands in `opts`, `opts.size` is undefined, and the result
50
- // is the natural-length buffer.
51
- it('pads to the requested size when given the options form', function () {
52
- // Leading zero byte, so the natural encoding is shorter than 32.
53
- var n = new BN('00ff' + '11'.repeat(30), 'hex')
54
-
55
- n.toBuffer({ size: 32 }).length.should.equal(32)
56
- })
57
-
58
- it('does NOT honour a positional length, which is why the options form is required', function () {
59
- var n = new BN('00ff' + '11'.repeat(30), 'hex')
60
-
61
- // Documents the trap rather than endorsing it: this is the call shape
62
- // that produced a 31-byte s inside the OP_PUSH_TX grind.
63
- n.toBuffer('be', 32).length.should.equal(31)
64
- })
65
- })
66
-
67
- describe('PushTx.sFromPreimage uses the right toBuffer signature', function () {
68
- // sFromPreimage called `s.toBuffer('be', 32)` — bn.js's NATIVE signature,
69
- // against a toBuffer that lib/crypto/bn.js has replaced with an
70
- // options-object form. The length argument was therefore ignored.
71
- //
72
- // This is a latent defect, NOT a live one, and the distinction is worth
73
- // stating precisely: the truncation is currently unreachable. `s` is
74
- // (HASH256(preimage) + Gx) mod n, and the caller has already required the
75
- // hash's leading byte to be 0x01..0x7f, so z is in [2**248, 2**255).
76
- // Adding Gx (about 0.476 * 2**256) never wraps, and the low-S filter caps
77
- // the result at n/2 — confining s to a narrow band whose leading byte is
78
- // always 0x7a..0x7f. It can never be zero, so the natural encoding is
79
- // always 32 bytes anyway.
80
- //
81
- // The fix is worth making because the correctness of the output currently
82
- // depends on two upstream filters that have nothing to do with buffer
83
- // length. Change the MINIMALDATA range or drop the low-S requirement and
84
- // the truncation becomes reachable, silently, in a covenant.
85
- it('always returns exactly 32 bytes', function () {
86
- var PushTx = bsv.SmartContract.PushTx
87
- var accepted = 0
88
-
89
- for (var i = 0; i < 4000; i++) {
90
- var preimage = bsv.crypto.Hash.sha256(Buffer.from('grind-' + i))
91
- var sBE = PushTx.sFromPreimage(preimage)
92
- if (sBE) {
93
- accepted++
94
- sBE.length.should.equal(32)
95
- }
96
- }
97
-
98
- accepted.should.be.above(0)
99
- })
100
-
101
- it('keeps s inside the band that makes the length safe', function () {
102
- // Pins the property the paragraph above relies on. If this ever fails,
103
- // the toBuffer call shape matters again — and the fix is already in.
104
- var PushTx = bsv.SmartContract.PushTx
105
-
106
- for (var i = 0; i < 2000; i++) {
107
- var preimage = bsv.crypto.Hash.sha256(Buffer.from('band-' + i))
108
- var sBE = PushTx.sFromPreimage(preimage)
109
- if (sBE) {
110
- sBE[0].should.be.within(0x01, 0x7f)
111
- }
112
- }
113
- })
114
- })
115
-
116
- describe('OP_CHECKSEQUENCEVERIFY', function () {
117
- // checkSequence masked with `nSequence.and(nLockTimeMask)` where the mask
118
- // is a plain number. bn.js `and` requires a BN, so it threw
119
- // 'num.clone is not a function'; the interpreter's try/catch swallowed
120
- // that and reported SCRIPT_ERR_UNKNOWN_ERROR, making every CSV script fail
121
- // with a misleading error.
122
- //
123
- // Note this is only reachable with SCRIPT_VERIFY_CHECKSEQUENCEVERIFY set:
124
- // post-Genesis BSV reverted CSV to OP_NOP3 and the flag is off by default.
125
- it('evaluates instead of failing with SCRIPT_ERR_UNKNOWN_ERROR', function () {
126
- var Interpreter = bsv.Script.Interpreter
127
- var key = bsv.PrivateKey.fromRandom()
128
- var address = key.toAddress()
129
- var prev = new bsv.Transaction().to(address, 1000)
130
- var spend = new bsv.Transaction().from({
131
- txId: prev.hash,
132
- outputIndex: 0,
133
- script: bsv.Script.buildPublicKeyHashOut(address).toHex(),
134
- satoshis: 1000
135
- })
136
- spend.version = 2
137
- spend.inputs[0].sequenceNumber = 20
138
-
139
- var interpreter = new Interpreter()
140
- var verified = interpreter.verify(
141
- bsv.Script.fromASM('OP_1'),
142
- bsv.Script.fromASM('OP_10 OP_CHECKSEQUENCEVERIFY'),
143
- spend, 0,
144
- Interpreter.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY,
145
- new BN(1000)
146
- )
147
-
148
- interpreter.errstr.should.equal('')
149
- verified.should.equal(true)
150
- })
151
-
152
- it('still rejects a sequence that does not satisfy the requirement', function () {
153
- var Interpreter = bsv.Script.Interpreter
154
- var key = bsv.PrivateKey.fromRandom()
155
- var address = key.toAddress()
156
- var prev = new bsv.Transaction().to(address, 1000)
157
- var spend = new bsv.Transaction().from({
158
- txId: prev.hash,
159
- outputIndex: 0,
160
- script: bsv.Script.buildPublicKeyHashOut(address).toHex(),
161
- satoshis: 1000
162
- })
163
- spend.version = 2
164
- // Below the required relative locktime of 10.
165
- spend.inputs[0].sequenceNumber = 5
166
-
167
- var interpreter = new Interpreter()
168
- var verified = interpreter.verify(
169
- bsv.Script.fromASM('OP_1'),
170
- bsv.Script.fromASM('OP_10 OP_CHECKSEQUENCEVERIFY'),
171
- spend, 0,
172
- Interpreter.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY,
173
- new BN(1000)
174
- )
175
-
176
- verified.should.equal(false)
177
- interpreter.errstr.should.equal('SCRIPT_ERR_UNSATISFIED_LOCKTIME')
178
- })
179
-
180
- // Same family as the bug above, and found the same way — a TypeError swallowed by
181
- // the evaluator's try/catch and reported as SCRIPT_ERR_UNKNOWN_ERROR.
182
- //
183
- // `checkSequence` dereferenced `this.tx.inputs[this.nin]` as its FIRST statement, so
184
- // verifying against a transaction with no input at that index threw
185
- // "Cannot read properties of undefined (reading 'sequenceNumber')". It fails closed,
186
- // but the error names the script when the real problem is the index.
187
- //
188
- // `checkLockTime` had the identical dereference; ordering hid it, because earlier
189
- // comparisons returned false before reaching the line. Both are guarded now, and
190
- // both report the consensus error a caller can act on.
191
- ;[
192
- ['OP_CHECKSEQUENCEVERIFY', 'SCRIPT_VERIFY_CHECKSEQUENCEVERIFY', 1000],
193
- ['OP_CHECKLOCKTIMEVERIFY', 'SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY', 500000000]
194
- ].forEach(function (c) {
195
- it('reports a consensus error, not a TypeError, when there is no input at nin (' + c[0] + ')', function () {
196
- var Interpreter = bsv.Script.Interpreter
197
- var script = new bsv.Script()
198
- script.add(new BN(c[2]).toScriptNumBuffer())
199
- script.add(bsv.Opcode[c[0]])
200
- script.add(bsv.Opcode.OP_DROP)
201
- script.add(bsv.Opcode.OP_1)
202
-
203
- var interpreter = new Interpreter()
204
- // A transaction with no inputs at all; nin 0 does not exist.
205
- var verified = interpreter.verify(
206
- new bsv.Script(), script, new bsv.Transaction(), 0, Interpreter[c[1]], new BN(0)
207
- )
208
-
209
- verified.should.equal(false)
210
- interpreter.errstr.should.equal('SCRIPT_ERR_UNSATISFIED_LOCKTIME')
211
- interpreter.errstr.should.not.match(/UNKNOWN_ERROR|TypeError/)
212
- })
213
- })
214
- })
215
- })