@psf/bch-js 4.20.24 → 4.20.28

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.
@@ -5,15 +5,21 @@
5
5
  // Public npm libraries
6
6
  const assert = require('chai').assert
7
7
  const sinon = require('sinon')
8
+ const cloneDeep = require('lodash.clonedeep')
8
9
 
9
10
  const BCHJS = require('../../src/bch-js')
10
11
  const bchjs = new BCHJS()
11
12
 
12
- const mockData = require('./fixtures/transaction-mock.js')
13
+ const mockDataLib = require('./fixtures/transaction-mock.js')
13
14
 
14
15
  describe('#TransactionLib', () => {
15
- let sandbox
16
- beforeEach(() => (sandbox = sinon.createSandbox()))
16
+ let sandbox, mockData
17
+
18
+ beforeEach(() => {
19
+ sandbox = sinon.createSandbox()
20
+
21
+ mockData = cloneDeep(mockDataLib)
22
+ })
17
23
  afterEach(() => sandbox.restore())
18
24
 
19
25
  describe('#get', () => {
@@ -266,10 +272,10 @@ describe('#TransactionLib', () => {
266
272
  })
267
273
  })
268
274
 
269
- describe('#get2', () => {
275
+ describe('#get3', () => {
270
276
  it('should throw an error if txid is not specified', async () => {
271
277
  try {
272
- await bchjs.Transaction.get2()
278
+ await bchjs.Transaction.get3()
273
279
 
274
280
  assert.fail('Unexpected code path!')
275
281
  } catch (err) {
@@ -291,8 +297,9 @@ describe('#TransactionLib', () => {
291
297
  sandbox
292
298
  .stub(bchjs.Transaction.blockchain, 'getBlockHeader')
293
299
  .resolves({ height: 602405 })
300
+ sandbox.stub(bchjs.Transaction, 'getTokenInfo').resolves(false)
294
301
 
295
- const result = await bchjs.Transaction.get2(txid)
302
+ const result = await bchjs.Transaction.get3(txid)
296
303
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
297
304
 
298
305
  // Assert that there are stanardized properties.
@@ -305,14 +312,13 @@ describe('#TransactionLib', () => {
305
312
  // Assert that added properties exist.
306
313
  assert.property(result.vin[0], 'address')
307
314
  assert.property(result.vin[0], 'value')
308
- assert.property(result, 'isValidSLPTx')
309
- assert.equal(result.isValidSLPTx, false)
310
315
 
311
316
  // Assert blockheight is added
312
317
  assert.equal(result.blockheight, 602405)
318
+ assert.equal(result.isSlpTx, false)
313
319
  })
314
320
 
315
- it('should get details about a SLP transaction', async () => {
321
+ it('should get details about a SLP SEND tx with SEND input', async () => {
316
322
  // Mock dependencies
317
323
  sandbox
318
324
  .stub(bchjs.Transaction.rawTransaction, 'getTxData')
@@ -334,7 +340,7 @@ describe('#TransactionLib', () => {
334
340
  const txid =
335
341
  '266844d53e46bbd7dd37134688dffea6e54d944edff27a0add63dd0908839bc1'
336
342
 
337
- const result = await bchjs.Transaction.get2(txid)
343
+ const result = await bchjs.Transaction.get3(txid)
338
344
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
339
345
 
340
346
  // Assert that there are stanardized properties.
@@ -344,19 +350,30 @@ describe('#TransactionLib', () => {
344
350
  assert.property(result.vout[0], 'value')
345
351
  assert.property(result.vout[1].scriptPubKey, 'addresses')
346
352
 
347
- // Assert that added properties exist.
348
- assert.property(result.vout[0], 'tokenQty')
353
+ // Assert outputs have expected properties
349
354
  assert.equal(result.vout[0].tokenQty, null)
350
- assert.property(result.vin[0], 'address')
351
- assert.property(result.vin[0], 'value')
352
- assert.property(result.vin[0], 'tokenQty')
353
-
354
- // Assert that tokenIds and tokenQty are included in inputs.
355
- assert.property(result.vin[0], 'tokenId')
355
+ assert.equal(result.vout[0].tokenQtyStr, null)
356
+ assert.equal(result.vout[1].tokenQty, 1)
357
+ assert.equal(result.vout[1].tokenQtyStr, '1')
358
+ assert.equal(result.vout[2].tokenQty, 998833)
359
+ assert.equal(result.vout[2].tokenQtyStr, '998833')
360
+ assert.equal(result.vout[3].tokenQty, null)
361
+ assert.equal(result.vout[3].tokenQtyStr, null)
362
+
363
+ // Assert that inputs have expected properties
356
364
  assert.equal(result.vin[0].tokenQtyStr, '998834')
365
+ assert.equal(result.vin[0].tokenQty, 998834)
366
+ assert.equal(
367
+ result.vin[0].tokenId,
368
+ '497291b8a1dfe69c8daea50677a3d31a5ef0e9484d8bebb610dac64bbc202fb7'
369
+ )
370
+ assert.equal(result.vin[1].tokenQtyStr, '0')
371
+ assert.equal(result.vin[1].tokenQty, 0)
372
+ assert.equal(result.vin[1].tokenId, null)
357
373
 
358
374
  // Assert blockheight is added
359
375
  assert.equal(result.blockheight, 603424)
376
+ assert.equal(result.isSlpTx, true)
360
377
  })
361
378
 
362
379
  it('should catch and throw error on network error', async () => {
@@ -369,7 +386,7 @@ describe('#TransactionLib', () => {
369
386
  .stub(bchjs.Transaction.rawTransaction, 'getTxData')
370
387
  .rejects(new Error('test error'))
371
388
 
372
- await bchjs.Transaction.get(txid)
389
+ await bchjs.Transaction.get3(txid)
373
390
 
374
391
  assert.fail('Unexpected code path')
375
392
  } catch (err) {
@@ -380,7 +397,7 @@ describe('#TransactionLib', () => {
380
397
  // This test case was created in response to a bug. When the input TX
381
398
  // was a Genesis SLP transaction, the inputs of the transaction were not
382
399
  // being hydrated properly.
383
- it('should get input details when input is a genesis tx', async () => {
400
+ it('should get details about a SLP SEND tx with GENSIS input', async () => {
384
401
  // Mock dependencies
385
402
  sandbox
386
403
  .stub(bchjs.Transaction.rawTransaction, 'getTxData')
@@ -402,7 +419,7 @@ describe('#TransactionLib', () => {
402
419
  const txid =
403
420
  '874306bda204d3a5dd15e03ea5732cccdca4c33a52df35162cdd64e30ea7f04e'
404
421
 
405
- const result = await bchjs.Transaction.get2(txid)
422
+ const result = await bchjs.Transaction.get3(txid)
406
423
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
407
424
 
408
425
  // Assert that there are stanardized properties.
@@ -412,28 +429,33 @@ describe('#TransactionLib', () => {
412
429
  assert.property(result.vout[0], 'value')
413
430
  assert.property(result.vout[1].scriptPubKey, 'addresses')
414
431
 
415
- // Assert that added properties exist.
416
- assert.property(result.vout[0], 'tokenQty')
432
+ // Assert outputs have expected properties and values
417
433
  assert.equal(result.vout[0].tokenQty, null)
418
- assert.property(result.vin[0], 'address')
419
- assert.property(result.vin[0], 'value')
420
- assert.property(result.vin[0], 'tokenQty')
421
-
422
- // Assert inputs values unique to a Genesis input have the proper values.
434
+ assert.equal(result.vout[0].tokenQtyStr, null)
435
+ assert.equal(result.vout[1].tokenQty, 5000000)
436
+ assert.equal(result.vout[1].tokenQtyStr, '5000000')
437
+ assert.equal(result.vout[2].tokenQty, 5000000)
438
+ assert.equal(result.vout[2].tokenQtyStr, '5000000')
439
+ assert.equal(result.vout[3].tokenQty, null)
440
+ assert.equal(result.vout[3].tokenQtyStr, null)
441
+
442
+ // Assert inputs have expected properties and values
423
443
  assert.equal(result.vin[0].tokenQty, 10000000)
424
- assert.equal(result.vin[1].tokenQty, null)
425
-
426
- // Assert that tokenIds and tokenQty are included in inputs.
427
- assert.property(result.vin[0], 'tokenId')
428
444
  assert.equal(result.vin[0].tokenQtyStr, '10000000')
429
- assert.equal(result.vin[1].tokenQty, null)
430
- assert.equal(result.vin[1].tokenQtyStr, null)
445
+ assert.equal(
446
+ result.vin[0].tokenId,
447
+ '323a1e35ae0b356316093d20f2d9fbc995d19314b5c0148b78dc8d9c0dab9d35'
448
+ )
449
+ assert.equal(result.vin[1].tokenQty, 0)
450
+ assert.equal(result.vin[1].tokenQtyStr, '0')
451
+ assert.equal(result.vin[1].tokenId, null)
431
452
 
432
453
  // Assert blockheight is added
433
454
  assert.equal(result.blockheight, 543409)
455
+ assert.equal(result.isSlpTx, true)
434
456
  })
435
457
 
436
- it('should get input details when input is a mint tx', async () => {
458
+ it('should get details about a SLP SEND tx with MINT (and GENESIS) input', async () => {
437
459
  // Mock dependencies
438
460
  sandbox
439
461
  .stub(bchjs.Transaction.rawTransaction, 'getTxData')
@@ -457,7 +479,7 @@ describe('#TransactionLib', () => {
457
479
  const txid =
458
480
  '4640a734063ea79fa587a3cac38a70a2f6f3db0011e23514024185982110d0fa'
459
481
 
460
- const result = await bchjs.Transaction.get2(txid)
482
+ const result = await bchjs.Transaction.get3(txid)
461
483
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
462
484
 
463
485
  // Assert that there are stanardized properties.
@@ -467,25 +489,37 @@ describe('#TransactionLib', () => {
467
489
  assert.property(result.vout[0], 'value')
468
490
  assert.property(result.vout[1].scriptPubKey, 'addresses')
469
491
 
470
- // Assert that added properties exist.
471
- assert.property(result.vout[0], 'tokenQty')
492
+ // Assert expected output properties and values exist.
472
493
  assert.equal(result.vout[0].tokenQty, null)
473
- assert.property(result.vin[0], 'address')
474
- assert.property(result.vin[0], 'value')
475
- assert.property(result.vin[0], 'tokenQty')
494
+ assert.equal(result.vout[1].tokenQty, 43547.68657)
495
+ assert.equal(result.vout[1].tokenQtyStr, '43547.68657')
496
+ assert.equal(result.vout[2].tokenQty, null)
476
497
 
477
- // Assert inputs values unique to a Mint input have the proper values.
498
+ // Assert expected input properties and values exist.
478
499
  assert.equal(result.vin[0].tokenQty, 43545.34534)
500
+ assert.equal(result.vin[0].tokenQtyStr, '43545.34534')
501
+ assert.equal(
502
+ result.vin[0].tokenId,
503
+ '938cc18e618967d787897bbc64b9a8d201b94ec7c69b1a9949eab0433ba5cdf8'
504
+ )
479
505
  assert.equal(result.vin[1].tokenQty, 2.34123)
480
- assert.equal(result.vin[2].tokenQty, null)
506
+ assert.equal(result.vin[1].tokenQtyStr, '2.34123')
507
+ assert.equal(
508
+ result.vin[1].tokenId,
509
+ '938cc18e618967d787897bbc64b9a8d201b94ec7c69b1a9949eab0433ba5cdf8'
510
+ )
511
+ assert.equal(result.vin[2].tokenQty, 0)
512
+ assert.equal(result.vin[2].tokenQtyStr, '0')
513
+ assert.equal(result.vin[2].tokenId, null)
481
514
 
482
515
  // Assert blockheight is added
483
516
  assert.equal(result.blockheight, 543614)
517
+ assert.equal(result.isSlpTx, true)
484
518
  })
485
519
 
486
520
  // This test case was generated from the problematic transaction that
487
521
  // used inputs in a 'non-standard' way.
488
- it('should correctly assign quantities to mixed mint inputs', async () => {
522
+ it('should get details about a SLP SEND tx with MINT (and SEND) input', async () => {
489
523
  // Mock dependencies
490
524
  sandbox
491
525
  .stub(bchjs.Transaction.rawTransaction, 'getTxData')
@@ -509,7 +543,7 @@ describe('#TransactionLib', () => {
509
543
  const txid =
510
544
  '6bc111fbf5b118021d68355ca19a0e77fa358dd931f284b2550f79a51ab4792a'
511
545
 
512
- const result = await bchjs.Transaction.get2(txid)
546
+ const result = await bchjs.Transaction.get3(txid)
513
547
  // console.log(`result: ${JSON.stringify(result, null, 2)}`)
514
548
 
515
549
  // Assert that there are stanardized properties.
@@ -519,20 +553,156 @@ describe('#TransactionLib', () => {
519
553
  assert.property(result.vout[0], 'value')
520
554
  assert.property(result.vout[1].scriptPubKey, 'addresses')
521
555
 
522
- // Assert that added properties exist.
523
- assert.property(result.vout[0], 'tokenQty')
556
+ // Assert the outputs have expected properties and values.
524
557
  assert.equal(result.vout[0].tokenQty, null)
525
- assert.property(result.vin[0], 'address')
526
- assert.property(result.vin[0], 'value')
527
- assert.property(result.vin[0], 'tokenQty')
558
+ assert.equal(result.vout[1].tokenQty, 1000000)
559
+ assert.equal(result.vout[1].tokenQtyStr, '1000000')
560
+ assert.equal(result.vout[2].tokenQty, 198000000)
561
+ assert.equal(result.vout[2].tokenQtyStr, '198000000')
562
+ assert.equal(result.vout[3].tokenQty, null)
528
563
 
529
- // Assert inputs values unique to a Mint input have the proper values.
564
+ // Assert the inputs have expected properties and values.
530
565
  assert.equal(result.vin[0].tokenQty, 100000000)
531
- assert.equal(result.vin[1].tokenQty, null)
566
+ assert.equal(result.vin[0].tokenQtyStr, '100000000')
567
+ assert.equal(
568
+ result.vin[0].tokenId,
569
+ '550d19eb820e616a54b8a73372c4420b5a0567d8dc00f613b71c5234dc884b35'
570
+ )
571
+ assert.equal(result.vin[1].tokenQty, 0)
572
+ assert.equal(result.vin[1].tokenQtyStr, 0)
573
+ assert.equal(result.vin[1].tokenId, null)
532
574
  assert.equal(result.vin[2].tokenQty, 99000000)
575
+ assert.equal(result.vin[2].tokenQtyStr, '99000000')
576
+ assert.equal(
577
+ result.vin[2].tokenId,
578
+ '550d19eb820e616a54b8a73372c4420b5a0567d8dc00f613b71c5234dc884b35'
579
+ )
533
580
 
534
581
  // Assert blockheight is added
535
582
  assert.equal(result.blockheight, 543957)
583
+ assert.equal(result.isSlpTx, true)
584
+ })
585
+
586
+ // This was a problematic TX
587
+ it('should process MINT TX with GENESIS input', async () => {
588
+ // Mock dependencies
589
+ sandbox
590
+ .stub(bchjs.Transaction.rawTransaction, 'getTxData')
591
+ .resolves(mockData.mintTestInputTx02)
592
+ sandbox
593
+ .stub(bchjs.Transaction.blockchain, 'getBlockHeader')
594
+ .resolves({ height: 543614 })
595
+ sandbox
596
+ .stub(bchjs.Transaction.slpUtils, 'decodeOpReturn')
597
+ .onCall(0)
598
+ .resolves(mockData.mintTestOpReturnData04)
599
+ .onCall(1)
600
+ .resolves(mockData.mintTestOpReturnData05)
601
+ .onCall(2)
602
+ .resolves(mockData.mintTestOpReturnData05)
603
+ .onCall(3)
604
+ .resolves(mockData.mintTestOpReturnData05)
605
+ .onCall(4)
606
+ .resolves(mockData.mintTestOpReturnData05)
607
+ .onCall(5)
608
+ .resolves(mockData.mintTestOpReturnData05)
609
+
610
+ const txid =
611
+ 'ee9d3cf5153599c134147e3fac9844c68e216843f4452a1ce15a29452af6db34'
612
+
613
+ const result = await bchjs.Transaction.get3(txid)
614
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
615
+
616
+ // Assert that there are stanardized properties.
617
+ assert.property(result, 'txid')
618
+ assert.property(result, 'vin')
619
+ assert.property(result, 'vout')
620
+ assert.property(result.vout[0], 'value')
621
+ assert.property(result.vout[1].scriptPubKey, 'addresses')
622
+
623
+ // Assert outputs have expected properties and values
624
+ assert.equal(result.vout[0].tokenQty, 0)
625
+ assert.equal(result.vout[0].tokenQty, '0')
626
+ assert.equal(result.vout[1].tokenQty, 2.34123)
627
+ assert.equal(result.vout[1].tokenQty, '2.34123')
628
+ assert.equal(result.vout[2].tokenQty, 0)
629
+ assert.equal(result.vout[2].tokenQty, '0')
630
+ assert.equal(result.vout[2].isMintBaton, true)
631
+ assert.equal(result.vout[3].tokenQty, 0)
632
+ assert.equal(result.vout[3].tokenQty, '0')
633
+
634
+ // Assert inputs have expected properties and values
635
+ assert.equal(result.vin[0].tokenQty, 0)
636
+ assert.equal(result.vin[0].tokenQtyStr, '0')
637
+ assert.equal(result.vin[0].tokenId, null)
638
+ assert.equal(result.vin[1].tokenQty, 0)
639
+ assert.equal(result.vin[1].tokenQtyStr, '0')
640
+ assert.equal(
641
+ result.vin[1].tokenId,
642
+ '938cc18e618967d787897bbc64b9a8d201b94ec7c69b1a9949eab0433ba5cdf8'
643
+ )
644
+ assert.equal(result.vin[1].isMintBaton, true)
645
+
646
+ // Assert added TX data exists.
647
+ assert.equal(result.blockheight, 543614)
648
+ assert.equal(result.isSlpTx, true)
649
+ })
650
+
651
+ // It should process a GENESIS tx
652
+ it('should process a GENESIS tx', async () => {
653
+ // Mock dependencies
654
+ sandbox
655
+ .stub(bchjs.Transaction.rawTransaction, 'getTxData')
656
+ .resolves(mockData.genesisTestInputTx02)
657
+ sandbox
658
+ .stub(bchjs.Transaction.blockchain, 'getBlockHeader')
659
+ .resolves({ height: 571212 })
660
+ sandbox
661
+ .stub(bchjs.Transaction, 'getTokenInfo')
662
+ .onCall(0)
663
+ .resolves(mockData.genesisTestOpReturn03)
664
+ .onCall(1)
665
+ .resolves(mockData.genesisTestOpReturn03)
666
+ .onCall(2)
667
+ .resolves(false)
668
+ .onCall(3)
669
+ .resolves(false)
670
+ .onCall(4)
671
+ .resolves(false)
672
+
673
+ const txid =
674
+ '4de69e374a8ed21cbddd47f2338cc0f479dc58daa2bbe11cd604ca488eca0ddf'
675
+
676
+ const result = await bchjs.Transaction.get3(txid)
677
+ // console.log(`result: ${JSON.stringify(result, null, 2)}`)
678
+
679
+ // Assert that there are stanardized properties.
680
+ assert.property(result, 'txid')
681
+ assert.property(result, 'vin')
682
+ assert.property(result, 'vout')
683
+ assert.property(result.vout[0], 'value')
684
+ assert.property(result.vout[1].scriptPubKey, 'addresses')
685
+
686
+ // Assert output have expected properties and values
687
+ assert.equal(result.vout[0].tokenQty, 0)
688
+ assert.equal(result.vout[0].tokenQtyStr, '0')
689
+ assert.equal(result.vout[0].isMintBaton, true)
690
+ assert.equal(result.vout[1].tokenQty, 1000000000)
691
+ assert.equal(result.vout[1].tokenQtyStr, '1000000000')
692
+ assert.equal(result.vout[2].tokenQty, 0)
693
+ assert.equal(result.vout[2].tokenQtyStr, '0')
694
+
695
+ // Assert input have expected properties and values
696
+ assert.equal(result.vin[0].tokenQty, 0)
697
+ assert.equal(result.vin[0].tokenQtyStr, '0')
698
+ assert.equal(result.vin[0].tokenId, null)
699
+ assert.equal(result.vin[1].tokenQty, 0)
700
+ assert.equal(result.vin[1].tokenQtyStr, '0')
701
+ assert.equal(result.vin[1].tokenId, null)
702
+
703
+ // Assert added TX data exists.
704
+ assert.equal(result.blockheight, 571212)
705
+ assert.equal(result.isSlpTx, true)
536
706
  })
537
707
  })
538
708
  })