@smartledger/bsv 7.5.0 → 7.5.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.
@@ -205,7 +205,13 @@ describe('PrivateKey', function () {
205
205
  network: 'livenet'
206
206
  })
207
207
  var key = PrivateKey.fromObject(JSON.parse(json))
208
- JSON.stringify(key).should.equal(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)
209
215
  })
210
216
 
211
217
  it('should input/output json', function () {
@@ -215,7 +221,13 @@ describe('PrivateKey', function () {
215
221
  network: 'livenet'
216
222
  })
217
223
  var key = PrivateKey.fromJSON(JSON.parse(json))
218
- JSON.stringify(key).should.equal(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)
219
231
  })
220
232
 
221
233
  it('input json should correctly initialize network field', function () {
@@ -427,4 +439,69 @@ describe('PrivateKey', function () {
427
439
  var privkey = new PrivateKey('92VYMmwFLXRwXn5688edGxYYgMFsc3fUXYhGp17WocQhU6zG1kd')
428
440
  privkey.publicKey.toAddress().toString().should.equal('moiAvLUw16qgrwhFGo1eDnXHC2wPMYiv7Y')
429
441
  })
442
+ // Reported from the field against 7.4.0 and reproduced here. Each of these returned a
443
+ // DIFFERENT key or address than the caller asked for, silently.
444
+ describe('argument handling that used to produce the wrong key', function () {
445
+ var HEX = '0000000000000000000000000000000000000000000000000000000000000001'
446
+
447
+ it('honours the network passed to fromString instead of discarding it', function () {
448
+ // Returned a livenet key — and therefore a MAINNET address — so funds sent to it
449
+ // landed on the wrong network.
450
+ var k = PrivateKey.fromString(HEX, 'testnet')
451
+ k.network.name.should.equal('testnet')
452
+ k.toAddress().toString().should.equal(new PrivateKey(HEX, 'testnet').toAddress().toString())
453
+ })
454
+
455
+ it('agrees with the constructor for WIF input', function () {
456
+ var wif = new PrivateKey(HEX, 'testnet').toWIF()
457
+ PrivateKey.fromString(wif).toWIF().should.equal(wif)
458
+ PrivateKey.fromString(wif, 'testnet').toWIF().should.equal(wif)
459
+ })
460
+
461
+ it('rejects a network that contradicts the WIF rather than ignoring it', function () {
462
+ var wif = new PrivateKey(HEX, 'testnet').toWIF()
463
+ ;(function () { PrivateKey.fromString(wif, 'livenet') }).should.throw()
464
+ })
465
+
466
+ it('fromHex and the constructor return the SAME key', function () {
467
+ // These disagreed on the compression flag, so the same input produced two different
468
+ // addresses and two different WIFs. Restore by the wrong route, derive an address
469
+ // you never funded.
470
+ var a = new PrivateKey(HEX)
471
+ var b = PrivateKey.fromHex(HEX)
472
+ var c = PrivateKey.fromBuffer(Buffer.from(HEX, 'hex'))
473
+ b.compressed.should.equal(a.compressed)
474
+ b.toWIF().should.equal(a.toWIF())
475
+ b.toAddress().toString().should.equal(a.toAddress().toString())
476
+ c.toWIF().should.equal(a.toWIF())
477
+ })
478
+
479
+ it('still allows the legacy uncompressed form explicitly', function () {
480
+ PrivateKey.fromHex(HEX, null, false).compressed.should.equal(false)
481
+ PrivateKey.fromBuffer(Buffer.from(HEX, 'hex'), null, false).compressed.should.equal(false)
482
+ })
483
+ })
484
+
485
+ // JSON.stringify() on a key — or on any object holding one — used to emit the secret
486
+ // scalar. That is how a private key reached an OP_RETURN through the GDAF anchor path.
487
+ describe('does not leak the secret through JSON', function () {
488
+ it('redacts bn from JSON.stringify', function () {
489
+ var k = new PrivateKey()
490
+ var scalar = k.bn.toString('hex')
491
+ JSON.stringify(k).indexOf(scalar).should.equal(-1)
492
+ JSON.parse(JSON.stringify(k)).bn.should.equal('[REDACTED]')
493
+ })
494
+
495
+ it('redacts it when the key is nested inside another object', function () {
496
+ var k = new PrivateKey()
497
+ var payload = JSON.stringify({ context: { signer: k } })
498
+ payload.indexOf(k.bn.toString('hex')).should.equal(-1)
499
+ })
500
+
501
+ it('keeps toObject() exact, so deliberate export still round-trips', function () {
502
+ var k = new PrivateKey()
503
+ k.toObject().bn.should.equal(k.bn.toString('hex'))
504
+ PrivateKey.fromObject(k.toObject()).toWIF().should.equal(k.toWIF())
505
+ })
506
+ })
430
507
  })
package/version.js CHANGED
@@ -1,3 +1,3 @@
1
1
  'use strict'
2
2
  // GENERATED by scripts/sync-version.js on `npm version` — do not edit.
3
- module.exports = '7.5.0'
3
+ module.exports = '7.5.1'