|
| 1 | +import 'should'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import * as request from 'supertest'; |
| 4 | +import nock from 'nock'; |
| 5 | +import { app as expressApp } from '../../../masterExpressApp'; |
| 6 | +import { AppMode, MasterExpressConfig, TlsMode } from '../../../shared/types'; |
| 7 | +import { Trx } from '@bitgo/sdk-coin-trx'; |
| 8 | +import { Sol } from '@bitgo/sdk-coin-sol'; |
| 9 | +import { EnclavedExpressClient } from '../../../api/master/clients/enclavedExpressClient'; |
| 10 | + |
| 11 | +describe('POST /api/:coin/wallet/recoveryConsolidations', () => { |
| 12 | + let agent: request.SuperAgentTest; |
| 13 | + const enclavedExpressUrl = 'http://enclaved.invalid'; |
| 14 | + const accessToken = 'test-token'; |
| 15 | + |
| 16 | + before(() => { |
| 17 | + nock.disableNetConnect(); |
| 18 | + nock.enableNetConnect('127.0.0.1'); |
| 19 | + const config: MasterExpressConfig = { |
| 20 | + appMode: AppMode.MASTER_EXPRESS, |
| 21 | + port: 0, |
| 22 | + bind: 'localhost', |
| 23 | + timeout: 60000, |
| 24 | + logFile: '', |
| 25 | + env: 'test', |
| 26 | + disableEnvCheck: true, |
| 27 | + authVersion: 2, |
| 28 | + enclavedExpressUrl, |
| 29 | + enclavedExpressCert: 'dummy-cert', |
| 30 | + tlsMode: TlsMode.DISABLED, |
| 31 | + mtlsRequestCert: false, |
| 32 | + allowSelfSigned: true, |
| 33 | + }; |
| 34 | + const app = expressApp(config); |
| 35 | + agent = request.agent(app); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + nock.cleanAll(); |
| 40 | + sinon.restore(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle TRON consolidation recovery', async () => { |
| 44 | + const mockTransactions = [{ txHex: 'unsigned-tx-1', serializedTx: 'serialized-unsigned-tx-1' }]; |
| 45 | + |
| 46 | + const recoverConsolidationsStub = sinon.stub(Trx.prototype, 'recoverConsolidations').resolves({ |
| 47 | + transactions: mockTransactions, |
| 48 | + }); |
| 49 | + |
| 50 | + const recoveryMultisigStub = sinon |
| 51 | + .stub(EnclavedExpressClient.prototype, 'recoveryMultisig') |
| 52 | + .resolves({ txHex: 'signed-tx' }); |
| 53 | + |
| 54 | + const response = await agent |
| 55 | + .post(`/api/trx/wallet/recoveryConsolidations`) |
| 56 | + .set('Authorization', `Bearer ${accessToken}`) |
| 57 | + .send({ |
| 58 | + userPub: 'user-xpub', |
| 59 | + backupPub: 'backup-xpub', |
| 60 | + bitgoKey: 'bitgo-xpub', |
| 61 | + tokenContractAddress: 'tron-token', |
| 62 | + startingScanIndex: 1, |
| 63 | + endingScanIndex: 3, |
| 64 | + }); |
| 65 | + |
| 66 | + response.status.should.equal(200); |
| 67 | + response.body.should.have.property('signedTxs'); |
| 68 | + sinon.assert.calledOnce(recoverConsolidationsStub); |
| 69 | + sinon.assert.calledOnce(recoveryMultisigStub); |
| 70 | + const callArgs = recoverConsolidationsStub.firstCall.args[0]; |
| 71 | + callArgs.tokenContractAddress!.should.equal('tron-token'); |
| 72 | + callArgs.userKey.should.equal('user-xpub'); |
| 73 | + callArgs.backupKey.should.equal('backup-xpub'); |
| 74 | + callArgs.bitgoKey.should.equal('bitgo-xpub'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should handle Solana consolidation recovery', async () => { |
| 78 | + const mockTransactions = [{ txHex: 'unsigned-tx-1', serializedTx: 'serialized-unsigned-tx-1' }]; |
| 79 | + |
| 80 | + const recoverConsolidationsStub = sinon.stub(Sol.prototype, 'recoverConsolidations').resolves({ |
| 81 | + transactions: mockTransactions, |
| 82 | + }); |
| 83 | + |
| 84 | + const recoveryMultisigStub = sinon |
| 85 | + .stub(EnclavedExpressClient.prototype, 'recoveryMultisig') |
| 86 | + .resolves({ txHex: 'signed-tx' }); |
| 87 | + |
| 88 | + const response = await agent |
| 89 | + .post(`/api/sol/wallet/recoveryConsolidations`) |
| 90 | + .set('Authorization', `Bearer ${accessToken}`) |
| 91 | + .send({ |
| 92 | + userPub: 'user-xpub', |
| 93 | + backupPub: 'backup-xpub', |
| 94 | + bitgoKey: 'bitgo-xpub', |
| 95 | + durableNonces: { |
| 96 | + publicKeys: ['sol-pubkey-1', 'sol-pubkey-2'], |
| 97 | + secretKey: 'sol-secret', |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + response.status.should.equal(200); |
| 102 | + response.body.should.have.property('signedTxs'); |
| 103 | + sinon.assert.calledOnce(recoverConsolidationsStub); |
| 104 | + sinon.assert.calledOnce(recoveryMultisigStub); |
| 105 | + const callArgs = recoverConsolidationsStub.firstCall.args[0]; |
| 106 | + callArgs.durableNonces.should.have.property('publicKeys').which.is.an.Array(); |
| 107 | + callArgs.durableNonces.should.have.property('secretKey', 'sol-secret'); |
| 108 | + }); |
| 109 | +}); |
0 commit comments