|
| 1 | +import nock = require('nock'); |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import * as assert from 'assert'; |
| 4 | +import 'should'; |
| 5 | + |
| 6 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 7 | +import { TestBitGo } from '@bitgo/sdk-test'; |
| 8 | +import { Tbtc } from '@bitgo/sdk-coin-btc'; |
| 9 | +import { common, BaseCoin, BitGoBase, Wallet, WalletSignTransactionOptions } from '@bitgo/sdk-core'; |
| 10 | + |
| 11 | +describe('Wallet signTransaction with verifyTxParams', function () { |
| 12 | + let wallet: Wallet; |
| 13 | + let basecoin: BaseCoin; |
| 14 | + let verifyTransactionStub: sinon.SinonStub; |
| 15 | + |
| 16 | + beforeEach(function () { |
| 17 | + const bitgo = TestBitGo.decorate(BitGoAPI, { env: 'mock' }); |
| 18 | + bitgo.initializeTestVars(); |
| 19 | + bitgo.safeRegister('tbtc', Tbtc.createInstance); |
| 20 | + basecoin = bitgo.coin('tbtc'); |
| 21 | + |
| 22 | + // Mock wallet data |
| 23 | + const walletData = { |
| 24 | + id: 'test-wallet-id', |
| 25 | + coin: 'tbtc', |
| 26 | + label: 'Test Wallet', |
| 27 | + m: 2, |
| 28 | + n: 3, |
| 29 | + keys: ['key1', 'key2', 'key3'], |
| 30 | + multisigType: 'onchain', |
| 31 | + type: 'hot', |
| 32 | + balance: 100000, |
| 33 | + balanceString: '100000', |
| 34 | + confirmedBalance: 100000, |
| 35 | + confirmedBalanceString: '100000', |
| 36 | + spendableBalance: 100000, |
| 37 | + spendableBalanceString: '100000', |
| 38 | + }; |
| 39 | + |
| 40 | + wallet = new Wallet(bitgo as unknown as BitGoBase, basecoin as unknown as BaseCoin, walletData); |
| 41 | + |
| 42 | + // Create stubs for verification |
| 43 | + sinon.stub(basecoin, 'signTransaction').resolves({ txHex: 'mock-signed-tx-hex' }); |
| 44 | + verifyTransactionStub = sinon.stub(basecoin, 'verifyTransaction'); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(function () { |
| 48 | + sinon.restore(); |
| 49 | + nock.cleanAll(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should fail verification when verifyTransaction throws an error', async function () { |
| 53 | + // Mock the verification function to throw an error (simulating verification failure) |
| 54 | + verifyTransactionStub.throws(new Error('Transaction verification failed')); |
| 55 | + |
| 56 | + const txPrebuild = { |
| 57 | + txHex: 'mock-tx-hex', |
| 58 | + walletId: 'test-wallet-id', |
| 59 | + }; |
| 60 | + |
| 61 | + const verifyTxParams = { |
| 62 | + txParams: { |
| 63 | + recipients: [ |
| 64 | + { |
| 65 | + address: 'test-address', |
| 66 | + amount: '10000', |
| 67 | + }, |
| 68 | + ], |
| 69 | + type: 'send', |
| 70 | + }, |
| 71 | + }; |
| 72 | + |
| 73 | + const signParams: WalletSignTransactionOptions = { |
| 74 | + txPrebuild, |
| 75 | + verifyTxParams, |
| 76 | + }; |
| 77 | + |
| 78 | + try { |
| 79 | + await wallet.signTransaction(signParams); |
| 80 | + assert.fail('Should have thrown verification error'); |
| 81 | + } catch (error) { |
| 82 | + assert.ok( |
| 83 | + error.message.includes('Transaction verification failed'), |
| 84 | + `Error message should contain 'Transaction verification failed', got: ${error.message}` |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + // Verify that the verification function was called with the expected parameters |
| 89 | + sinon.assert.calledOnce(verifyTransactionStub); |
| 90 | + const callArgs = verifyTransactionStub.getCall(0).args; |
| 91 | + const verifyParams = callArgs[0]; |
| 92 | + assert.strictEqual(verifyParams.txPrebuild.txHex, 'mock-tx-hex'); |
| 93 | + assert.deepStrictEqual(verifyParams.txParams, verifyTxParams.txParams); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should pass verification when verifyTransaction succeeds', async function () { |
| 97 | + // Mock the verification function to succeed (no error thrown) |
| 98 | + verifyTransactionStub.returns(true); |
| 99 | + |
| 100 | + // Mock key retrieval endpoints |
| 101 | + const bgUrl = common.Environments['mock'].uri; |
| 102 | + nock(bgUrl).get('/api/v2/tbtc/key/key1').reply(200, { |
| 103 | + id: 'key1', |
| 104 | + pub: 'pub', |
| 105 | + prv: 'prv', |
| 106 | + }); |
| 107 | + |
| 108 | + nock(bgUrl).get('/api/v2/tbtc/key/key2').reply(200, { |
| 109 | + id: 'key2', |
| 110 | + pub: 'pub', |
| 111 | + prv: 'prv', |
| 112 | + }); |
| 113 | + |
| 114 | + nock(bgUrl).get('/api/v2/tbtc/key/key3').reply(200, { |
| 115 | + id: 'key3', |
| 116 | + pub: 'pub', |
| 117 | + }); |
| 118 | + |
| 119 | + const txPrebuild = { |
| 120 | + txHex: 'mock-tx-hex', |
| 121 | + walletId: 'test-wallet-id', |
| 122 | + }; |
| 123 | + |
| 124 | + const verifyTxParams: WalletSignTransactionOptions['verifyTxParams'] = { |
| 125 | + txParams: { |
| 126 | + recipients: [ |
| 127 | + { |
| 128 | + address: 'test-address', |
| 129 | + amount: '1000', |
| 130 | + }, |
| 131 | + ], |
| 132 | + type: 'send', |
| 133 | + }, |
| 134 | + }; |
| 135 | + |
| 136 | + const signParams: WalletSignTransactionOptions = { |
| 137 | + txPrebuild, |
| 138 | + verifyTxParams, |
| 139 | + prv: 'prv', |
| 140 | + }; |
| 141 | + |
| 142 | + const result = await wallet.signTransaction(signParams); |
| 143 | + |
| 144 | + // Verify the result |
| 145 | + result.should.have.property('txHex', 'mock-signed-tx-hex'); |
| 146 | + |
| 147 | + // Verify that the verification function was called with the expected parameters |
| 148 | + sinon.assert.calledOnce(verifyTransactionStub); |
| 149 | + const callArgs = verifyTransactionStub.getCall(0).args; |
| 150 | + const verifyParams = callArgs[0]; |
| 151 | + assert.strictEqual(verifyParams.txPrebuild.txHex, 'mock-tx-hex'); |
| 152 | + assert.deepStrictEqual(verifyParams.txParams, verifyTxParams.txParams); |
| 153 | + }); |
| 154 | +}); |
0 commit comments