@smartledger/bsv 8.3.1 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/CHANGELOG.md +228 -0
  2. package/README.md +152 -1031
  3. package/STABILITY.md +134 -0
  4. package/bsv-gdaf.min.js +58 -53
  5. package/bsv-ltp.min.js +23 -18
  6. package/bsv-script-helper.min.js +1 -1
  7. package/bsv-smartcontract.min.js +21 -21
  8. package/bsv.bundle.js +58 -53
  9. package/bsv.d.ts +0 -4
  10. package/bsv.min.js +58 -53
  11. package/docs/BRC220_BATCH_LEAF_AMENDMENT.md +82 -7
  12. package/docs/MODULE_REFERENCE_COMPLETE.md +27 -27
  13. package/docs/README.md +2 -1
  14. package/docs/THREAT_MODEL.md +10 -2
  15. package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
  16. package/docs/getting-started/INSTALLATION.md +23 -23
  17. package/docs/getting-started/QUICK_START.md +7 -7
  18. package/docs/migration/FROM_BSV_1_5_6.md +5 -5
  19. package/docs/proposals/10.0.0-package-split.md +131 -0
  20. package/index.js +15 -26
  21. package/lib/block/merkleblock.js +19 -5
  22. package/lib/covenant/helpers.js +34 -35
  23. package/lib/covenant/pushtx.js +2 -2
  24. package/lib/custom-script-helper.js +0 -16
  25. package/lib/hdprivatekey.js +15 -0
  26. package/lib/ordinals/ordlock.js +1 -1
  27. package/lib/script/interpreter.js +109 -1
  28. package/lib/smart_contract/builder.js +14 -0
  29. package/lib/smart_contract/debugger.js +2 -2
  30. package/lib/smart_contract/dsl.js +2 -2
  31. package/lib/smart_contract/index.js +0 -3
  32. package/lib/smart_contract/locks.js +1 -41
  33. package/lib/smart_contract/pels.js +1 -1
  34. package/lib/smart_contract/token.js +1 -1
  35. package/lib/util/deprecate.js +159 -0
  36. package/package.json +19 -82
  37. package/tools/gen-brc220-batch-vector.js +205 -0
  38. package/version.js +1 -1
  39. package/.mocharc.json +0 -5
  40. package/test/address.js +0 -629
  41. package/test/block/block.js +0 -239
  42. package/test/block/blockheader.js +0 -270
  43. package/test/block/merkleblock.js +0 -207
  44. package/test/build/bundle_crypto_shim.js +0 -104
  45. package/test/build/bundle_externals.js +0 -125
  46. package/test/build/bundle_smoke.js +0 -55
  47. package/test/build/esbuild_main.js +0 -65
  48. package/test/build/esm_wrapper.js +0 -73
  49. package/test/build/exports_resolution.js +0 -84
  50. package/test/build/version_sync.js +0 -14
  51. package/test/cli/smoke.js +0 -261
  52. package/test/consensus/base58-vectors.js +0 -120
  53. package/test/consensus/sv-script-vectors.js +0 -81
  54. package/test/consensus/sv-sighash-vectors.js +0 -51
  55. package/test/consensus/sv-tx-vectors.js +0 -48
  56. package/test/credentials-test.js +0 -332
  57. package/test/crypto/backend_selection.js +0 -126
  58. package/test/crypto/bn.js +0 -168
  59. package/test/crypto/ecdsa.js +0 -426
  60. package/test/crypto/elliptic-fixed.js +0 -61
  61. package/test/crypto/hash.browser.js +0 -121
  62. package/test/crypto/hash.js +0 -122
  63. package/test/crypto/point.js +0 -193
  64. package/test/crypto/random.js +0 -105
  65. package/test/crypto/security.js +0 -176
  66. package/test/crypto/shamir.js +0 -177
  67. package/test/crypto/shamir_rngcap.js +0 -29
  68. package/test/crypto/signature.js +0 -399
  69. package/test/data/bip69.json +0 -215
  70. package/test/data/bitcoin-sv/README.md +0 -170
  71. package/test/data/bitcoin-sv/base58_encode_decode.json +0 -14
  72. package/test/data/bitcoin-sv/base58_keys_invalid.json +0 -152
  73. package/test/data/bitcoin-sv/base58_keys_valid.json +0 -452
  74. package/test/data/bitcoin-sv/script_tests.json +0 -2591
  75. package/test/data/bitcoin-sv/sighash.json +0 -1003
  76. package/test/data/bitcoin-sv/tx_invalid.json +0 -285
  77. package/test/data/bitcoin-sv/tx_valid.json +0 -367
  78. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  79. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  80. package/test/data/bitcoind/blocks.json +0 -27
  81. package/test/data/bitcoind/script_tests.json +0 -2244
  82. package/test/data/bitcoind/sig_canonical.json +0 -7
  83. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  84. package/test/data/bitcoind/tx_invalid.json +0 -177
  85. package/test/data/bitcoind/tx_valid.json +0 -224
  86. package/test/data/blk86756-testnet.dat +0 -0
  87. package/test/data/blk86756-testnet.js +0 -12
  88. package/test/data/blk86756-testnet.json +0 -684
  89. package/test/data/ecdsa.json +0 -230
  90. package/test/data/merkleblocks.js +0 -486
  91. package/test/data/messages.json +0 -22
  92. package/test/data/sighash.json +0 -1004
  93. package/test/data/tx_creation.json +0 -85
  94. package/test/didweb/relationships.js +0 -144
  95. package/test/ecies/bitcore-ecies.js +0 -178
  96. package/test/ecies/electrum-ecies.js +0 -206
  97. package/test/encoding/base58.js +0 -131
  98. package/test/encoding/base58check.js +0 -145
  99. package/test/encoding/bufferreader.js +0 -328
  100. package/test/encoding/bufferwriter.js +0 -160
  101. package/test/encoding/varint.js +0 -104
  102. package/test/gdaf/anchor_no_key_leak.js +0 -146
  103. package/test/gdaf/anchor_spv.js +0 -163
  104. package/test/gdaf/canonicalization.js +0 -106
  105. package/test/gdaf/canonicalize.js +0 -140
  106. package/test/gdaf/zk_prover.js +0 -204
  107. package/test/hdkeys.js +0 -365
  108. package/test/hdprivatekey.js +0 -339
  109. package/test/hdpublickey.js +0 -293
  110. package/test/index.js +0 -16
  111. package/test/ltp/ids.js +0 -74
  112. package/test/ltp/right.js +0 -198
  113. package/test/ltp/verify_failclosed.js +0 -134
  114. package/test/message/message.js +0 -190
  115. package/test/mnemonic/data/fixtures.json +0 -300
  116. package/test/mnemonic/mnemonic.js +0 -277
  117. package/test/mnemonic/mocha.opts +0 -1
  118. package/test/mnemonic/pbkdf2.test.js +0 -43
  119. package/test/networks.js +0 -207
  120. package/test/notaryhash/batch_leaf.js +0 -140
  121. package/test/notaryhash/certificate.js +0 -249
  122. package/test/notaryhash/encoding.js +0 -186
  123. package/test/notaryhash/interop.js +0 -112
  124. package/test/notaryhash/merkle.js +0 -181
  125. package/test/notaryhash/script.js +0 -270
  126. package/test/notaryhash/verify.js +0 -342
  127. package/test/opcode.js +0 -186
  128. package/test/ordinals/bsv20.js +0 -337
  129. package/test/ordinals/inscription.js +0 -319
  130. package/test/ordinals/ordlock.js +0 -613
  131. package/test/privatekey.js +0 -540
  132. package/test/publickey.js +0 -411
  133. package/test/regressions.js +0 -215
  134. package/test/script/chronicle.js +0 -543
  135. package/test/script/defaults.js +0 -160
  136. package/test/script/genesis_limits.js +0 -203
  137. package/test/script/interpreter.js +0 -776
  138. package/test/script/script.js +0 -1259
  139. package/test/script/string_ops.js +0 -88
  140. package/test/security/fail_closed_contracts.js +0 -104
  141. package/test/security/threat_model_coverage.js +0 -31
  142. package/test/smart_contract/covenants.js +0 -245
  143. package/test/smart_contract/dsl_debugger.js +0 -101
  144. package/test/smart_contract/extract_field.js +0 -61
  145. package/test/smart_contract/nonenforcing_guard.js +0 -44
  146. package/test/smart_contract/ordinal_transfer.js +0 -81
  147. package/test/smart_contract/preimage.js +0 -98
  148. package/test/smart_contract/sighash_marketplace.js +0 -108
  149. package/test/smart_contract/token_generalized.js +0 -185
  150. package/test/spv/headerchain.js +0 -86
  151. package/test/spv/merkleproof.js +0 -133
  152. package/test/statuslist/failclosed.js +0 -78
  153. package/test/transaction/deserialize.js +0 -33
  154. package/test/transaction/input/input.js +0 -92
  155. package/test/transaction/input/multisig.js +0 -174
  156. package/test/transaction/input/multisigscripthash.js +0 -111
  157. package/test/transaction/input/publickey.js +0 -68
  158. package/test/transaction/input/publickeyhash.js +0 -59
  159. package/test/transaction/output.js +0 -185
  160. package/test/transaction/sighash.js +0 -91
  161. package/test/transaction/signature.js +0 -127
  162. package/test/transaction/transaction.js +0 -1299
  163. package/test/transaction/unspentoutput.js +0 -97
  164. package/test/types/dts_drift.js +0 -124
  165. package/test/types/surface_honesty.js +0 -102
  166. package/test/util/id.js +0 -72
  167. package/test/util/js.js +0 -76
  168. package/test/util/preconditions.js +0 -79
  169. package/test/vcjwt/interop.js +0 -126
@@ -1,540 +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 BN = bsv.crypto.BN
9
- var Point = bsv.crypto.Point
10
- var PrivateKey = bsv.PrivateKey
11
- var Networks = bsv.Networks
12
- var Base58Check = bsv.encoding.Base58Check
13
-
14
- var validbase58 = require('./data/bitcoind/base58_keys_valid.json')
15
- var invalidbase58 = require('./data/bitcoind/base58_keys_invalid.json')
16
-
17
- describe('PrivateKey', function () {
18
- var hex = '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a'
19
- var hex2 = '8080808080808080808080808080808080808080808080808080808080808080'
20
- var buf = Buffer.from(hex, 'hex')
21
- var wifTestnet = 'cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG'
22
- var wifTestnetUncompressed = '92jJzK4tbURm1C7udQXxeCBvXHoHJstDXRxAMouPG1k1XUaXdsu'
23
- var wifLivenet = 'L2Gkw3kKJ6N24QcDuH4XDqt9cTqsKTVNDGz1CRZhk9cq4auDUbJy'
24
- var wifLivenetUncompressed = '5JxgQaFM1FMd38cd14e3mbdxsdSa9iM2BV6DHBYsvGzxkTNQ7Un'
25
- var wifNamecoin = '74pxNKNpByQ2kMow4d9kF6Z77BYeKztQNLq3dSyU4ES1K5KLNiz'
26
-
27
- it('should create a new random private key', function () {
28
- var a = new PrivateKey()
29
- should.exist(a)
30
- should.exist(a.bn)
31
- var b = PrivateKey()
32
- should.exist(b)
33
- should.exist(b.bn)
34
- })
35
-
36
- it('should create a privatekey from hexa string', function () {
37
- var a = new PrivateKey(hex2)
38
- should.exist(a)
39
- should.exist(a.bn)
40
- })
41
-
42
- it('should create a new random testnet private key with only one argument', function () {
43
- var a = new PrivateKey(Networks.testnet)
44
- should.exist(a)
45
- should.exist(a.bn)
46
- })
47
-
48
- it('should create a private key from a custom network WIF string', function () {
49
- var nmc = {
50
- name: 'namecoin',
51
- alias: 'namecoin',
52
- pubkeyhash: 0x34,
53
- privatekey: 0xB4,
54
- // these below aren't the real NMC version numbers
55
- scripthash: 0x08,
56
- xpubkey: 0x0278b20e,
57
- xprivkey: 0x0278ade4,
58
- networkMagic: 0xf9beb4fe,
59
- port: 20001,
60
- dnsSeeds: [
61
- 'localhost',
62
- 'mynet.localhost'
63
- ]
64
- }
65
- Networks.add(nmc)
66
- var nmcNet = Networks.get('namecoin')
67
- var a = new PrivateKey(wifNamecoin, nmcNet)
68
- should.exist(a)
69
- should.exist(a.bn)
70
- Networks.remove(nmcNet)
71
- })
72
-
73
- it('should create a new random testnet private key with empty data', function () {
74
- var a = new PrivateKey(null, Networks.testnet)
75
- should.exist(a)
76
- should.exist(a.bn)
77
- })
78
-
79
- it('should create a private key from WIF string', function () {
80
- var a = new PrivateKey('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m')
81
- should.exist(a)
82
- should.exist(a.bn)
83
- })
84
-
85
- it('should create a private key from WIF buffer', function () {
86
- var a = new PrivateKey(Base58Check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m'))
87
- should.exist(a)
88
- should.exist(a.bn)
89
- })
90
-
91
- describe('bitcoind compliance', function () {
92
- validbase58.map(function (d) {
93
- if (d[2].isPrivkey) {
94
- it('should instantiate WIF private key ' + d[0] + ' with correct properties', function () {
95
- var network = Networks.livenet
96
- if (d[2].isTestnet) {
97
- network = Networks.testnet
98
- }
99
- var key = new PrivateKey(d[0])
100
- key.compressed.should.equal(d[2].isCompressed)
101
- key.network.should.equal(network)
102
- })
103
- }
104
- })
105
- invalidbase58.map(function (d) {
106
- it('should describe input ' + d[0].slice(0, 10) + '... as invalid', function () {
107
- expect(function () {
108
- return new PrivateKey(d[0])
109
- }).to.throw(Error)
110
- })
111
- })
112
- })
113
-
114
- describe('instantiation', function () {
115
- it('should not be able to instantiate private key greater than N', function () {
116
- expect(function () {
117
- return new PrivateKey(Point.getN())
118
- }).to.throw('Number must be less than N')
119
- })
120
-
121
- it('should not be able to instantiate private key because of network mismatch', function () {
122
- expect(function () {
123
- return new PrivateKey('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m', 'testnet')
124
- }).to.throw('Private key network mismatch')
125
- })
126
-
127
- it('should not be able to instantiate private key WIF is too long', function () {
128
- expect(function () {
129
- var buf = Base58Check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m')
130
- var buf2 = Buffer.concat([buf, Buffer.alloc(0x01)])
131
- return new PrivateKey(buf2)
132
- }).to.throw('Length of buffer must be 33 (uncompressed) or 34 (compressed')
133
- })
134
-
135
- it('should not be able to instantiate private key WIF because of unknown network byte', function () {
136
- expect(function () {
137
- var buf = Base58Check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m')
138
- var buf2 = Buffer.concat([Buffer.from('ff', 'hex'), buf.slice(1, 33)])
139
- return new PrivateKey(buf2)
140
- }).to.throw('Invalid network')
141
- })
142
-
143
- it('should not be able to instantiate private key WIF because of network mismatch', function () {
144
- expect(function () {
145
- new PrivateKey(wifNamecoin, 'testnet') //eslint-disable-line
146
- }).to.throw('Invalid network')
147
- })
148
-
149
- it('can be instantiated from a hex string', function () {
150
- var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'
151
- var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'
152
- var privkey = new PrivateKey(privhex)
153
- privkey.publicKey.toString().should.equal(pubhex)
154
- })
155
-
156
- it('should not be able to instantiate because of unrecognized data', function () {
157
- expect(function () {
158
- return new PrivateKey(new Error())
159
- }).to.throw('First argument is an unrecognized data type.')
160
- })
161
-
162
- it('should not be able to instantiate with unknown network', function () {
163
- expect(function () {
164
- return new PrivateKey(new BN(2), 'unknown')
165
- }).to.throw('Must specify the network ("livenet" or "testnet")')
166
- })
167
-
168
- it('should not create a zero private key', function () {
169
- expect(function () {
170
- var bn = new BN(0)
171
- return new PrivateKey(bn)
172
- }).to.throw(TypeError)
173
- })
174
-
175
- it('should create a livenet private key', function () {
176
- var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet')
177
- privkey.toWIF().should.equal(wifLivenet)
178
- })
179
-
180
- it('should create a default network private key', function () {
181
- // keep the original
182
- var network = Networks.defaultNetwork
183
- Networks.defaultNetwork = Networks.livenet
184
- var a = new PrivateKey(BN.fromBuffer(buf))
185
- a.network.should.equal(Networks.livenet)
186
- // change the default
187
- Networks.defaultNetwork = Networks.testnet
188
- var b = new PrivateKey(BN.fromBuffer(buf))
189
- b.network.should.equal(Networks.testnet)
190
- // restore the default
191
- Networks.defaultNetwork = network
192
- })
193
-
194
- it('returns the same instance if a PrivateKey is provided (immutable)', function () {
195
- var privkey = new PrivateKey()
196
- new PrivateKey(privkey).should.equal(privkey)
197
- })
198
- })
199
-
200
- describe('#json/object', function () {
201
- it('should input/output json', function () {
202
- var json = JSON.stringify({
203
- bn: '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a',
204
- compressed: false,
205
- network: 'livenet'
206
- })
207
- var key = PrivateKey.fromObject(JSON.parse(json))
208
- // toObject() is the deliberate export and still round-trips exactly...
209
- JSON.stringify(key.toObject()).should.equal(json)
210
- // ...but JSON.stringify() must NOT emit the secret. This test used to assert that
211
- // it did, which is how a private key reached an OP_RETURN via the GDAF anchor path:
212
- // anything that stringified an object holding a key published the scalar.
213
- JSON.parse(JSON.stringify(key)).bn.should.equal('[REDACTED]')
214
- JSON.stringify(key).indexOf('96c13222').should.equal(-1)
215
- })
216
-
217
- it('should input/output json', function () {
218
- var json = JSON.stringify({
219
- bn: '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a',
220
- compressed: false,
221
- network: 'livenet'
222
- })
223
- var key = PrivateKey.fromJSON(JSON.parse(json))
224
- // toObject() is the deliberate export and still round-trips exactly...
225
- JSON.stringify(key.toObject()).should.equal(json)
226
- // ...but JSON.stringify() must NOT emit the secret. This test used to assert that
227
- // it did, which is how a private key reached an OP_RETURN via the GDAF anchor path:
228
- // anything that stringified an object holding a key published the scalar.
229
- JSON.parse(JSON.stringify(key)).bn.should.equal('[REDACTED]')
230
- JSON.stringify(key).indexOf('96c13222').should.equal(-1)
231
- })
232
-
233
- it('input json should correctly initialize network field', function () {
234
- ['livenet', 'testnet', 'mainnet'].forEach(function (net) {
235
- var pk = PrivateKey.fromObject({
236
- bn: '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a',
237
- compressed: false,
238
- network: net
239
- })
240
- pk.network.should.be.deep.equal(Networks.get(net))
241
- })
242
- })
243
-
244
- it('fails on invalid argument', function () {
245
- expect(function () {
246
- return PrivateKey.fromJSON('¹')
247
- }).to.throw()
248
- })
249
-
250
- it('also accepts an object as argument', function () {
251
- expect(function () {
252
- return PrivateKey.fromObject(new PrivateKey().toObject())
253
- }).to.not.throw()
254
- })
255
-
256
- // This asserted that feeding toJSON() output back into fromObject does NOT throw.
257
- // Since 7.5.1 toJSON() redacts the secret, and bn.js 4 silently SKIPS characters it
258
- // cannot parse instead of failing — so the round-trip returned a DIFFERENT key
259
- // (a deterministic ~40-bit one, trivially recoverable by anyone) with no error at
260
- // all. The old expectation encoded that as intended behaviour; it is now inverted.
261
- it('refuses to rebuild a key from redacted toJSON() output', function () {
262
- expect(function () {
263
- return PrivateKey.fromObject(new PrivateKey().toJSON())
264
- }).to.throw(/REDACTED/)
265
- })
266
-
267
- it('round-trips exactly through toObject(), which is the real export', function () {
268
- var key = new PrivateKey()
269
- PrivateKey.fromObject(key.toObject()).toWIF().should.equal(key.toWIF())
270
- })
271
-
272
- it('rejects a malformed bn instead of silently deriving another key', function () {
273
- // bn.js 4 parses '[REDACTED]' as 768491671261 rather than erroring.
274
- var cases = ['zzzz', '[REDACTED]', 'nothex', '12g4']
275
- cases.forEach(function (bad) {
276
- expect(function () {
277
- return PrivateKey.fromObject({ bn: bad, compressed: true, network: 'livenet' })
278
- }).to.throw(/Invalid private key/)
279
- })
280
- })
281
-
282
- it('rejects a bn outside the valid key range', function () {
283
- expect(function () {
284
- return PrivateKey.fromObject({ bn: '00'.repeat(32), compressed: true, network: 'livenet' })
285
- }).to.throw(/outside the range/)
286
- expect(function () {
287
- return PrivateKey.fromObject({
288
- bn: 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364142',
289
- compressed: true,
290
- network: 'livenet'
291
- })
292
- }).to.throw(/outside the range/)
293
- })
294
- })
295
-
296
- it('coverage: public key cache', function () {
297
- expect(function () {
298
- var privateKey = new PrivateKey()
299
- var publicKey = privateKey.publicKey
300
- return publicKey
301
- }).to.not.throw()
302
- })
303
-
304
- describe('#toString', function () {
305
- it('should output this address correctly', function () {
306
- var privkey = PrivateKey.fromWIF(wifLivenetUncompressed)
307
- privkey.toWIF().should.equal(wifLivenetUncompressed)
308
- })
309
- })
310
-
311
- describe('#toAddress', function () {
312
- it('should output this known livenet address correctly', function () {
313
- var privkey = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m')
314
- var address = privkey.toAddress()
315
- address.toString().should.equal('1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT')
316
- })
317
-
318
- it('should output this known testnet address correctly', function () {
319
- var privkey = PrivateKey.fromWIF('cR4qogdN9UxLZJXCNFNwDRRZNeLRWuds9TTSuLNweFVjiaE4gPaq')
320
- var address = privkey.toAddress()
321
- address.toString().should.equal('mtX8nPZZdJ8d3QNLRJ1oJTiEi26Sj6LQXS')
322
- })
323
-
324
- it('creates network specific address', function () {
325
- var pk = PrivateKey.fromWIF('cR4qogdN9UxLZJXCNFNwDRRZNeLRWuds9TTSuLNweFVjiaE4gPaq')
326
- pk.toAddress(Networks.livenet).network.name.should.equal(Networks.livenet.name)
327
- pk.toAddress(Networks.testnet).network.name.should.equal(Networks.testnet.name)
328
- })
329
- })
330
-
331
- describe('#inspect', function () {
332
- it('should output known livenet address for console', function () {
333
- var privkey = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m')
334
- privkey.inspect().should.equal(
335
- '<PrivateKey: b9de6e778fe92aa7edb69395556f843f1dce0448350112e14906efc2a80fa61a, network: livenet>'
336
- )
337
- })
338
-
339
- it('should output known testnet address for console', function () {
340
- var privkey = PrivateKey.fromWIF('cR4qogdN9UxLZJXCNFNwDRRZNeLRWuds9TTSuLNweFVjiaE4gPaq')
341
- privkey.inspect().should.equal(
342
- '<PrivateKey: 67fd2209ce4a95f6f1d421ab3fbea47ada13df11b73b30c4d9a9f78cc80651ac, network: testnet>'
343
- )
344
- })
345
-
346
- it('outputs "uncompressed" for uncompressed imported WIFs', function () {
347
- var privkey = PrivateKey.fromWIF(wifLivenetUncompressed)
348
- privkey.inspect().should.equal('<PrivateKey: 96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a, network: livenet, uncompressed>')
349
- })
350
- })
351
-
352
- describe('#getValidationError', function () {
353
- it('should get an error because private key greater than N', function () {
354
- var n = Point.getN()
355
- var a = PrivateKey.getValidationError(n)
356
- a.message.should.equal('Number must be less than N')
357
- })
358
-
359
- it('should validate as false because private key greater than N', function () {
360
- var n = Point.getN()
361
- var a = PrivateKey.isValid(n)
362
- a.should.equal(false)
363
- })
364
-
365
- it('should recognize that undefined is an invalid private key', function () {
366
- PrivateKey.isValid().should.equal(false)
367
- })
368
-
369
- it('should validate as true', function () {
370
- var a = PrivateKey.isValid('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m')
371
- a.should.equal(true)
372
- })
373
- })
374
-
375
- describe('buffer serialization', function () {
376
- it('returns an expected value when creating a PrivateKey from a buffer', function () {
377
- var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet')
378
- privkey.toHex().should.equal(buf.toString('hex'))
379
- })
380
-
381
- it('roundtrips correctly when using toBuffer/fromBuffer', function () {
382
- var privkey = new PrivateKey(BN.fromBuffer(buf))
383
- var toBuffer = new PrivateKey(privkey.toBuffer())
384
- var fromBuffer = PrivateKey.fromBuffer(toBuffer.toBuffer())
385
- fromBuffer.toHex().should.equal(privkey.toHex())
386
- })
387
- })
388
-
389
- describe('#toBigNumber', function () {
390
- it('should output known BN', function () {
391
- var a = BN.fromBuffer(buf)
392
- var privkey = new PrivateKey(a, 'livenet')
393
- var b = privkey.toBigNumber()
394
- b.toString('hex').should.equal(a.toString('hex'))
395
- })
396
- })
397
-
398
- describe('#fromRandom', function () {
399
- it('should set bn gt 0 and lt n, and should be compressed', function () {
400
- var privkey = PrivateKey.fromRandom()
401
- privkey.bn.gt(new BN(0)).should.equal(true)
402
- privkey.bn.lt(Point.getN()).should.equal(true)
403
- privkey.compressed.should.equal(true)
404
- })
405
- })
406
-
407
- describe('#fromWIF', function () {
408
- it('should parse this compressed testnet address correctly', function () {
409
- var privkey = PrivateKey.fromWIF(wifLivenet)
410
- privkey.toWIF().should.equal(wifLivenet)
411
- })
412
- })
413
-
414
- describe('#toWIF', function () {
415
- it('should parse this compressed testnet address correctly', function () {
416
- var privkey = PrivateKey.fromWIF(wifTestnet)
417
- privkey.toWIF().should.equal(wifTestnet)
418
- })
419
- })
420
-
421
- describe('#fromString', function () {
422
- it('should parse this uncompressed testnet address correctly', function () {
423
- var privkey = PrivateKey.fromString(wifTestnetUncompressed)
424
- privkey.toWIF().should.equal(wifTestnetUncompressed)
425
- })
426
- })
427
-
428
- describe('#toString', function () {
429
- it('should parse this uncompressed livenet address correctly', function () {
430
- var privkey = PrivateKey.fromString(wifLivenetUncompressed)
431
- privkey.toHex().should.equal('96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a')
432
- })
433
- })
434
-
435
- describe('#toPublicKey', function () {
436
- it('should convert this known PrivateKey to known PublicKey', function () {
437
- var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'
438
- var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'
439
- var privkey = new PrivateKey(new BN(Buffer.from(privhex, 'hex')))
440
- var pubkey = privkey.toPublicKey()
441
- pubkey.toString().should.equal(pubhex)
442
- })
443
-
444
- it('should have a "publicKey" property', function () {
445
- var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'
446
- var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'
447
- var privkey = new PrivateKey(new BN(Buffer.from(privhex, 'hex')))
448
- privkey.publicKey.toString().should.equal(pubhex)
449
- })
450
-
451
- it('should convert this known PrivateKey to known PublicKey and preserve compressed=true', function () {
452
- var privwif = 'L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m'
453
- var privkey = new PrivateKey(privwif, 'livenet')
454
- var pubkey = privkey.toPublicKey()
455
- pubkey.compressed.should.equal(true)
456
- })
457
-
458
- it('should convert this known PrivateKey to known PublicKey and preserve compressed=false', function () {
459
- var privwif = '92jJzK4tbURm1C7udQXxeCBvXHoHJstDXRxAMouPG1k1XUaXdsu'
460
- var privkey = new PrivateKey(privwif, 'testnet')
461
- var pubkey = privkey.toPublicKey()
462
- pubkey.compressed.should.equal(false)
463
- })
464
- })
465
-
466
- it('creates an address as expected from WIF, livenet', function () {
467
- var privkey = new PrivateKey('5J2NYGstJg7aJQEqNwYp4enG5BSfFdKXVTtBLvHicnRGD5kjxi6')
468
- privkey.publicKey.toAddress().toString().should.equal('135bwugFCmhmNU3SeCsJeTqvo5ViymgwZ9')
469
- })
470
-
471
- it('creates an address as expected from WIF, testnet', function () {
472
- var privkey = new PrivateKey('92VYMmwFLXRwXn5688edGxYYgMFsc3fUXYhGp17WocQhU6zG1kd')
473
- privkey.publicKey.toAddress().toString().should.equal('moiAvLUw16qgrwhFGo1eDnXHC2wPMYiv7Y')
474
- })
475
- // Reported from the field against 7.4.0 and reproduced here. Each of these returned a
476
- // DIFFERENT key or address than the caller asked for, silently.
477
- describe('argument handling that used to produce the wrong key', function () {
478
- var HEX = '0000000000000000000000000000000000000000000000000000000000000001'
479
-
480
- it('honours the network passed to fromString instead of discarding it', function () {
481
- // Returned a livenet key — and therefore a MAINNET address — so funds sent to it
482
- // landed on the wrong network.
483
- var k = PrivateKey.fromString(HEX, 'testnet')
484
- k.network.name.should.equal('testnet')
485
- k.toAddress().toString().should.equal(new PrivateKey(HEX, 'testnet').toAddress().toString())
486
- })
487
-
488
- it('agrees with the constructor for WIF input', function () {
489
- var wif = new PrivateKey(HEX, 'testnet').toWIF()
490
- PrivateKey.fromString(wif).toWIF().should.equal(wif)
491
- PrivateKey.fromString(wif, 'testnet').toWIF().should.equal(wif)
492
- })
493
-
494
- it('rejects a network that contradicts the WIF rather than ignoring it', function () {
495
- var wif = new PrivateKey(HEX, 'testnet').toWIF()
496
- ;(function () { PrivateKey.fromString(wif, 'livenet') }).should.throw()
497
- })
498
-
499
- it('fromHex and the constructor return the SAME key', function () {
500
- // These disagreed on the compression flag, so the same input produced two different
501
- // addresses and two different WIFs. Restore by the wrong route, derive an address
502
- // you never funded.
503
- var a = new PrivateKey(HEX)
504
- var b = PrivateKey.fromHex(HEX)
505
- var c = PrivateKey.fromBuffer(Buffer.from(HEX, 'hex'))
506
- b.compressed.should.equal(a.compressed)
507
- b.toWIF().should.equal(a.toWIF())
508
- b.toAddress().toString().should.equal(a.toAddress().toString())
509
- c.toWIF().should.equal(a.toWIF())
510
- })
511
-
512
- it('still allows the legacy uncompressed form explicitly', function () {
513
- PrivateKey.fromHex(HEX, null, false).compressed.should.equal(false)
514
- PrivateKey.fromBuffer(Buffer.from(HEX, 'hex'), null, false).compressed.should.equal(false)
515
- })
516
- })
517
-
518
- // JSON.stringify() on a key — or on any object holding one — used to emit the secret
519
- // scalar. That is how a private key reached an OP_RETURN through the GDAF anchor path.
520
- describe('does not leak the secret through JSON', function () {
521
- it('redacts bn from JSON.stringify', function () {
522
- var k = new PrivateKey()
523
- var scalar = k.bn.toString('hex')
524
- JSON.stringify(k).indexOf(scalar).should.equal(-1)
525
- JSON.parse(JSON.stringify(k)).bn.should.equal('[REDACTED]')
526
- })
527
-
528
- it('redacts it when the key is nested inside another object', function () {
529
- var k = new PrivateKey()
530
- var payload = JSON.stringify({ context: { signer: k } })
531
- payload.indexOf(k.bn.toString('hex')).should.equal(-1)
532
- })
533
-
534
- it('keeps toObject() exact, so deliberate export still round-trips', function () {
535
- var k = new PrivateKey()
536
- k.toObject().bn.should.equal(k.bn.toString('hex'))
537
- PrivateKey.fromObject(k.toObject()).toWIF().should.equal(k.toWIF())
538
- })
539
- })
540
- })