|
| 1 | +import { toHex, TransactionType } from '@bitgo/sdk-core'; |
| 2 | +import { coins } from '@bitgo/statics'; |
| 3 | +import { fromBase64 } from '@cosmjs/encoding'; |
| 4 | +import should from 'should'; |
| 5 | + |
| 6 | +import { CosmosTransaction, SendMessage } from '@bitgo/abstract-cosmos'; |
| 7 | +import { HashUtils } from '../../src/lib/utils'; |
| 8 | +import * as testData from '../resources/hash'; |
| 9 | + |
| 10 | +describe('Hash Token Transaction', () => { |
| 11 | + let tx: CosmosTransaction; |
| 12 | + const tokenName = 'thash:ylds'; |
| 13 | + const config = coins.get(tokenName); |
| 14 | + const utils = new HashUtils(config.network.type); |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + tx = new CosmosTransaction(config, utils); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('Empty transaction', () => { |
| 21 | + it('should throw empty transaction', function () { |
| 22 | + should.throws(() => tx.toBroadcastFormat(), 'Empty transaction'); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('From raw transaction', () => { |
| 27 | + it('should build a transfer from raw signed base64', function () { |
| 28 | + tx.enrichTransactionDetailsFromRawTransaction(testData.TEST_SEND_TX.signedTxBase64); |
| 29 | + const json = tx.toJson(); |
| 30 | + should.equal(json.sequence, testData.TEST_SEND_TX.sequence); |
| 31 | + should.deepEqual(json.gasBudget, testData.TEST_SEND_TX.gasBudget); |
| 32 | + should.equal(json.publicKey, toHex(fromBase64(testData.TEST_SEND_TX.pubKey))); |
| 33 | + should.equal( |
| 34 | + (json.sendMessages[0].value as SendMessage).toAddress, |
| 35 | + testData.TEST_SEND_TX.sendMessage.value.toAddress |
| 36 | + ); |
| 37 | + should.deepEqual( |
| 38 | + (json.sendMessages[0].value as SendMessage).amount, |
| 39 | + testData.TEST_SEND_TX.sendMessage.value.amount |
| 40 | + ); |
| 41 | + should.equal(Buffer.from(json.signature as any).toString('base64'), testData.TEST_SEND_TX.signature); |
| 42 | + should.equal(tx.type, TransactionType.Send); |
| 43 | + tx.loadInputsAndOutputs(); |
| 44 | + should.deepEqual(tx.inputs, [ |
| 45 | + { |
| 46 | + address: testData.TEST_SEND_TX.sender, |
| 47 | + value: testData.TEST_SEND_TX.sendMessage.value.amount[0].amount, |
| 48 | + coin: tokenName, |
| 49 | + }, |
| 50 | + ]); |
| 51 | + should.deepEqual(tx.outputs, [ |
| 52 | + { |
| 53 | + address: testData.TEST_SEND_TX.sendMessage.value.toAddress, |
| 54 | + value: testData.TEST_SEND_TX.sendMessage.value.amount[0].amount, |
| 55 | + coin: tokenName, |
| 56 | + }, |
| 57 | + ]); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should build a transfer from raw signed hex', function () { |
| 61 | + tx.enrichTransactionDetailsFromRawTransaction(toHex(fromBase64(testData.TEST_SEND_TX.signedTxBase64))); |
| 62 | + const json = tx.toJson(); |
| 63 | + should.equal(json.sequence, testData.TEST_SEND_TX.sequence); |
| 64 | + should.deepEqual(json.gasBudget, testData.TEST_SEND_TX.gasBudget); |
| 65 | + should.equal(json.publicKey, toHex(fromBase64(testData.TEST_SEND_TX.pubKey))); |
| 66 | + should.equal( |
| 67 | + (json.sendMessages[0].value as SendMessage).toAddress, |
| 68 | + testData.TEST_SEND_TX.sendMessage.value.toAddress |
| 69 | + ); |
| 70 | + should.deepEqual( |
| 71 | + (json.sendMessages[0].value as SendMessage).amount, |
| 72 | + testData.TEST_SEND_TX.sendMessage.value.amount |
| 73 | + ); |
| 74 | + should.equal(Buffer.from(json.signature as any).toString('base64'), testData.TEST_SEND_TX.signature); |
| 75 | + should.equal(tx.type, TransactionType.Send); |
| 76 | + tx.loadInputsAndOutputs(); |
| 77 | + should.deepEqual(tx.inputs, [ |
| 78 | + { |
| 79 | + address: testData.TEST_SEND_TX.sender, |
| 80 | + value: testData.TEST_SEND_TX.sendMessage.value.amount[0].amount, |
| 81 | + coin: tokenName, |
| 82 | + }, |
| 83 | + ]); |
| 84 | + should.deepEqual(tx.outputs, [ |
| 85 | + { |
| 86 | + address: testData.TEST_SEND_TX.sendMessage.value.toAddress, |
| 87 | + value: testData.TEST_SEND_TX.sendMessage.value.amount[0].amount, |
| 88 | + coin: tokenName, |
| 89 | + }, |
| 90 | + ]); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should fail to build a transfer from incorrect raw hex', function () { |
| 94 | + should.throws( |
| 95 | + () => tx.enrichTransactionDetailsFromRawTransaction('random' + testData.TEST_SEND_TX.signedTxBase64), |
| 96 | + 'incorrect raw data' |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should fail to explain transaction with invalid raw hex', function () { |
| 101 | + should.throws(() => tx.enrichTransactionDetailsFromRawTransaction('randomString'), 'Invalid transaction'); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('Explain transaction', () => { |
| 106 | + it('should explain a transfer pay transaction', function () { |
| 107 | + tx.enrichTransactionDetailsFromRawTransaction(testData.TEST_SEND_TX.signedTxBase64); |
| 108 | + const explainedTransaction = tx.explainTransaction(); |
| 109 | + explainedTransaction.should.deepEqual({ |
| 110 | + displayOrder: ['id', 'outputs', 'outputAmount', 'changeOutputs', 'changeAmount', 'fee', 'type'], |
| 111 | + id: testData.TEST_SEND_TX.hash, |
| 112 | + outputs: [ |
| 113 | + { |
| 114 | + address: testData.TEST_SEND_TX.recipient, |
| 115 | + amount: testData.TEST_SEND_TX.sendAmount, |
| 116 | + }, |
| 117 | + ], |
| 118 | + outputAmount: testData.TEST_SEND_TX.sendAmount, |
| 119 | + changeOutputs: [], |
| 120 | + changeAmount: '0', |
| 121 | + fee: { fee: testData.TEST_SEND_TX.feeAmount }, |
| 122 | + type: 0, |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should fail to explain transaction with invalid raw base64 string', function () { |
| 127 | + should.throws(() => tx.enrichTransactionDetailsFromRawTransaction('randomString'), 'Invalid transaction'); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments