|
| 1 | +import assert from 'assert'; |
| 2 | +import should from 'should'; |
| 3 | +import { Transaction as WasmTonTransaction, parseTransaction } from '@bitgo/wasm-ton'; |
| 4 | +import { explainTonTransaction } from '../../src/lib/explainTransactionWasm'; |
| 5 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 6 | +import * as testData from '../resources/ton'; |
| 7 | + |
| 8 | +describe('TON WASM explainTransaction', function () { |
| 9 | + describe('explainTonTransaction', function () { |
| 10 | + it('should explain a signed send transaction', function () { |
| 11 | + const txBase64 = testData.signedSendTransaction.tx; |
| 12 | + const explained = explainTonTransaction({ txBase64 }); |
| 13 | + |
| 14 | + explained.type.should.equal(TransactionType.Send); |
| 15 | + explained.outputs.length.should.be.greaterThan(0); |
| 16 | + explained.outputs[0].amount.should.equal(testData.signedSendTransaction.recipient.amount); |
| 17 | + explained.changeOutputs.should.be.an.Array(); |
| 18 | + explained.changeAmount.should.equal('0'); |
| 19 | + should.exist(explained.id); |
| 20 | + should.exist(explained.sender); |
| 21 | + explained.isSigned.should.be.true(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should explain a signed token send transaction', function () { |
| 25 | + const txBase64 = testData.signedTokenSendTransaction.tx; |
| 26 | + const explained = explainTonTransaction({ txBase64 }); |
| 27 | + |
| 28 | + explained.type.should.equal(TransactionType.SendToken); |
| 29 | + explained.outputs.length.should.be.greaterThan(0); |
| 30 | + should.exist(explained.id); |
| 31 | + should.exist(explained.sender); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should explain a single nominator withdraw transaction', function () { |
| 35 | + const txBase64 = testData.signedSingleNominatorWithdrawTransaction.tx; |
| 36 | + const explained = explainTonTransaction({ txBase64 }); |
| 37 | + |
| 38 | + explained.type.should.equal(TransactionType.SingleNominatorWithdraw); |
| 39 | + should.exist(explained.id); |
| 40 | + should.exist(explained.sender); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should explain a Ton Whales deposit transaction', function () { |
| 44 | + const txBase64 = testData.signedTonWhalesDepositTransaction.tx; |
| 45 | + const explained = explainTonTransaction({ txBase64 }); |
| 46 | + |
| 47 | + explained.type.should.equal(TransactionType.TonWhalesDeposit); |
| 48 | + should.exist(explained.id); |
| 49 | + should.exist(explained.sender); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should explain a Ton Whales withdrawal transaction', function () { |
| 53 | + const txBase64 = testData.signedTonWhalesWithdrawalTransaction.tx; |
| 54 | + const explained = explainTonTransaction({ txBase64 }); |
| 55 | + |
| 56 | + explained.type.should.equal(TransactionType.TonWhalesWithdrawal); |
| 57 | + should.exist(explained.id); |
| 58 | + should.exist(explained.sender); |
| 59 | + should.exist(explained.withdrawAmount); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should explain a Ton Whales full withdrawal transaction', function () { |
| 63 | + const txBase64 = testData.signedTonWhalesFullWithdrawalTransaction.tx; |
| 64 | + const explained = explainTonTransaction({ txBase64 }); |
| 65 | + |
| 66 | + explained.type.should.equal(TransactionType.TonWhalesWithdrawal); |
| 67 | + should.exist(explained.id); |
| 68 | + should.exist(explained.sender); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('WASM Transaction signing flow', function () { |
| 73 | + it('should produce correct signable payload from WASM Transaction', function () { |
| 74 | + const txBase64 = testData.signedSendTransaction.tx; |
| 75 | + const tx = WasmTonTransaction.fromBytes(Buffer.from(txBase64, 'base64')); |
| 76 | + const signablePayload = tx.signablePayload(); |
| 77 | + |
| 78 | + signablePayload.should.be.instanceOf(Uint8Array); |
| 79 | + signablePayload.length.should.equal(32); |
| 80 | + |
| 81 | + // Compare against known signable from test fixtures |
| 82 | + const expectedSignable = Buffer.from(testData.signedSendTransaction.signable, 'base64'); |
| 83 | + Buffer.from(signablePayload).toString('base64').should.equal(expectedSignable.toString('base64')); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should parse transaction and preserve bigint amounts', function () { |
| 87 | + const txBase64 = testData.signedSendTransaction.tx; |
| 88 | + const tx = WasmTonTransaction.fromBytes(Buffer.from(txBase64, 'base64')); |
| 89 | + const parsed = parseTransaction(tx); |
| 90 | + |
| 91 | + parsed.type.should.equal('Transfer'); |
| 92 | + should.exist(parsed.amount); |
| 93 | + (typeof parsed.amount).should.equal('bigint'); |
| 94 | + parsed.seqno.should.be.a.Number(); |
| 95 | + parsed.expireTime.should.be.a.Number(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should get transaction id', function () { |
| 99 | + const txBase64 = testData.signedSendTransaction.tx; |
| 100 | + const tx = WasmTonTransaction.fromBytes(Buffer.from(txBase64, 'base64')); |
| 101 | + const id = tx.id; |
| 102 | + |
| 103 | + should.exist(id); |
| 104 | + id.should.be.a.String(); |
| 105 | + id.length.should.be.greaterThan(0); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should report isSigned correctly', function () { |
| 109 | + const txBase64 = testData.signedSendTransaction.tx; |
| 110 | + const tx = WasmTonTransaction.fromBytes(Buffer.from(txBase64, 'base64')); |
| 111 | + |
| 112 | + tx.isSigned.should.be.true(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('WASM parseTransaction types', function () { |
| 117 | + it('should parse Transfer type', function () { |
| 118 | + const tx = WasmTonTransaction.fromBytes(Buffer.from(testData.signedSendTransaction.tx, 'base64')); |
| 119 | + const parsed = parseTransaction(tx); |
| 120 | + parsed.type.should.equal('Transfer'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should parse TokenTransfer type', function () { |
| 124 | + const tx = WasmTonTransaction.fromBytes(Buffer.from(testData.signedTokenSendTransaction.tx, 'base64')); |
| 125 | + const parsed = parseTransaction(tx); |
| 126 | + parsed.type.should.equal('TokenTransfer'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should parse SingleNominatorWithdraw type', function () { |
| 130 | + const tx = WasmTonTransaction.fromBytes( |
| 131 | + Buffer.from(testData.signedSingleNominatorWithdrawTransaction.tx, 'base64') |
| 132 | + ); |
| 133 | + const parsed = parseTransaction(tx); |
| 134 | + parsed.type.should.equal('SingleNominatorWithdraw'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should parse WhalesDeposit type', function () { |
| 138 | + const tx = WasmTonTransaction.fromBytes(Buffer.from(testData.signedTonWhalesDepositTransaction.tx, 'base64')); |
| 139 | + const parsed = parseTransaction(tx); |
| 140 | + parsed.type.should.equal('WhalesDeposit'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should parse WhalesWithdraw type', function () { |
| 144 | + const tx = WasmTonTransaction.fromBytes(Buffer.from(testData.signedTonWhalesWithdrawalTransaction.tx, 'base64')); |
| 145 | + const parsed = parseTransaction(tx); |
| 146 | + parsed.type.should.equal('WhalesWithdraw'); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments