- 
                Notifications
    
You must be signed in to change notification settings  - Fork 300
 
feat(sdk-core): add verification options for signing txHex #7164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            mohammadalfaiyazbitgo
  merged 1 commit into
  master
from
WP-6188/add-sign-verification-options
  
      
      
   
  Oct 30, 2025 
      
    
      
        
          +181
        
        
          −1
        
        
          
        
      
    
  
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
  
    
      
          
            154 changes: 154 additions & 0 deletions
          
          154 
        
  modules/bitgo/test/v2/unit/signTransactionVerification.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import nock = require('nock'); | ||
| import * as sinon from 'sinon'; | ||
| import * as assert from 'assert'; | ||
| import 'should'; | ||
| 
     | 
||
| import { BitGoAPI } from '@bitgo/sdk-api'; | ||
| import { TestBitGo } from '@bitgo/sdk-test'; | ||
| import { Tbtc } from '@bitgo/sdk-coin-btc'; | ||
| import { common, BaseCoin, BitGoBase, Wallet, WalletSignTransactionOptions } from '@bitgo/sdk-core'; | ||
| 
     | 
||
| describe('Wallet signTransaction with verifyTxParams', function () { | ||
| let wallet: Wallet; | ||
| let basecoin: BaseCoin; | ||
| let verifyTransactionStub: sinon.SinonStub; | ||
| 
     | 
||
| beforeEach(function () { | ||
| const bitgo = TestBitGo.decorate(BitGoAPI, { env: 'mock' }); | ||
| bitgo.initializeTestVars(); | ||
| bitgo.safeRegister('tbtc', Tbtc.createInstance); | ||
| basecoin = bitgo.coin('tbtc'); | ||
| 
     | 
||
| // Mock wallet data | ||
| const walletData = { | ||
| id: 'test-wallet-id', | ||
| coin: 'tbtc', | ||
| label: 'Test Wallet', | ||
| m: 2, | ||
| n: 3, | ||
| keys: ['key1', 'key2', 'key3'], | ||
| multisigType: 'onchain', | ||
| type: 'hot', | ||
| balance: 100000, | ||
| balanceString: '100000', | ||
| confirmedBalance: 100000, | ||
| confirmedBalanceString: '100000', | ||
| spendableBalance: 100000, | ||
| spendableBalanceString: '100000', | ||
| }; | ||
| 
     | 
||
| wallet = new Wallet(bitgo as unknown as BitGoBase, basecoin as unknown as BaseCoin, walletData); | ||
| 
     | 
||
| // Create stubs for verification | ||
| sinon.stub(basecoin, 'signTransaction').resolves({ txHex: 'mock-signed-tx-hex' }); | ||
| verifyTransactionStub = sinon.stub(basecoin, 'verifyTransaction'); | ||
| }); | ||
| 
     | 
||
| afterEach(function () { | ||
| sinon.restore(); | ||
| nock.cleanAll(); | ||
| }); | ||
| 
     | 
||
| it('should fail verification when verifyTransaction throws an error', async function () { | ||
| // Mock the verification function to throw an error (simulating verification failure) | ||
| verifyTransactionStub.throws(new Error('Transaction verification failed')); | ||
| 
     | 
||
| const txPrebuild = { | ||
| txHex: 'mock-tx-hex', | ||
| walletId: 'test-wallet-id', | ||
| }; | ||
| 
     | 
||
| const verifyTxParams = { | ||
| txParams: { | ||
| recipients: [ | ||
| { | ||
| address: 'test-address', | ||
| amount: '10000', | ||
| }, | ||
| ], | ||
| type: 'send', | ||
| }, | ||
| }; | ||
| 
     | 
||
| const signParams: WalletSignTransactionOptions = { | ||
| txPrebuild, | ||
| verifyTxParams, | ||
| }; | ||
| 
     | 
||
| try { | ||
| await wallet.signTransaction(signParams); | ||
| assert.fail('Should have thrown verification error'); | ||
| } catch (error) { | ||
| assert.ok( | ||
| error.message.includes('Transaction verification failed'), | ||
| `Error message should contain 'Transaction verification failed', got: ${error.message}` | ||
| ); | ||
| } | ||
| 
     | 
||
| // Verify that the verification function was called with the expected parameters | ||
| sinon.assert.calledOnce(verifyTransactionStub); | ||
| const callArgs = verifyTransactionStub.getCall(0).args; | ||
| const verifyParams = callArgs[0]; | ||
| assert.strictEqual(verifyParams.txPrebuild.txHex, 'mock-tx-hex'); | ||
| assert.deepStrictEqual(verifyParams.txParams, verifyTxParams.txParams); | ||
| }); | ||
| 
     | 
||
| it('should pass verification when verifyTransaction succeeds', async function () { | ||
| // Mock the verification function to succeed (no error thrown) | ||
| verifyTransactionStub.returns(true); | ||
| 
     | 
||
| // Mock key retrieval endpoints | ||
| const bgUrl = common.Environments['mock'].uri; | ||
| nock(bgUrl).get('/api/v2/tbtc/key/key1').reply(200, { | ||
| id: 'key1', | ||
| pub: 'pub', | ||
| prv: 'prv', | ||
| }); | ||
| 
     | 
||
| nock(bgUrl).get('/api/v2/tbtc/key/key2').reply(200, { | ||
| id: 'key2', | ||
| pub: 'pub', | ||
| prv: 'prv', | ||
| }); | ||
| 
     | 
||
| nock(bgUrl).get('/api/v2/tbtc/key/key3').reply(200, { | ||
| id: 'key3', | ||
| pub: 'pub', | ||
| }); | ||
| 
     | 
||
| const txPrebuild = { | ||
| txHex: 'mock-tx-hex', | ||
| walletId: 'test-wallet-id', | ||
| }; | ||
| 
     | 
||
| const verifyTxParams: WalletSignTransactionOptions['verifyTxParams'] = { | ||
| txParams: { | ||
| recipients: [ | ||
| { | ||
| address: 'test-address', | ||
| amount: '1000', | ||
| }, | ||
| ], | ||
| type: 'send', | ||
| }, | ||
| }; | ||
| 
     | 
||
| const signParams: WalletSignTransactionOptions = { | ||
| txPrebuild, | ||
| verifyTxParams, | ||
| prv: 'prv', | ||
| }; | ||
| 
     | 
||
| const result = await wallet.signTransaction(signParams); | ||
| 
     | 
||
| // Verify the result | ||
| result.should.have.property('txHex', 'mock-signed-tx-hex'); | ||
| 
     | 
||
| // Verify that the verification function was called with the expected parameters | ||
| sinon.assert.calledOnce(verifyTransactionStub); | ||
| const callArgs = verifyTransactionStub.getCall(0).args; | ||
| const verifyParams = callArgs[0]; | ||
| assert.strictEqual(verifyParams.txPrebuild.txHex, 'mock-tx-hex'); | ||
| assert.deepStrictEqual(verifyParams.txParams, verifyTxParams.txParams); | ||
| }); | ||
| }); | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.