|
| 1 | +import 'should'; |
| 2 | +import sinon from 'sinon'; |
| 3 | + |
| 4 | +import { AbstractEthLikeNewCoins } from '@bitgo/abstract-eth'; |
| 5 | +import nock from 'nock'; |
| 6 | +import * as request from 'supertest'; |
| 7 | +import { app as expressApp } from '../../../masterExpressApp'; |
| 8 | +import { AppMode, MasterExpressConfig, TlsMode } from '../../../shared/types'; |
| 9 | +import { data as ethRecoveryData } from '../../mocks/ethRecoveryMusigMockData'; |
| 10 | + |
| 11 | +describe('POST /api/:coin/wallet/recovery', () => { |
| 12 | + let agent: request.SuperAgentTest; |
| 13 | + const enclavedExpressUrl = 'http://enclaved.invalid'; |
| 14 | + const coin = 'hteth'; |
| 15 | + const accessToken = 'test-token'; |
| 16 | + |
| 17 | + before(() => { |
| 18 | + nock.disableNetConnect(); |
| 19 | + nock.enableNetConnect('127.0.0.1'); |
| 20 | + |
| 21 | + const config: MasterExpressConfig = { |
| 22 | + appMode: AppMode.MASTER_EXPRESS, |
| 23 | + port: 0, // Let OS assign a free port |
| 24 | + bind: 'localhost', |
| 25 | + timeout: 60000, |
| 26 | + logFile: '', |
| 27 | + env: 'test', |
| 28 | + disableEnvCheck: true, |
| 29 | + authVersion: 2, |
| 30 | + enclavedExpressUrl: enclavedExpressUrl, |
| 31 | + enclavedExpressCert: 'dummy-cert', |
| 32 | + tlsMode: TlsMode.DISABLED, |
| 33 | + mtlsRequestCert: false, |
| 34 | + allowSelfSigned: true, |
| 35 | + }; |
| 36 | + |
| 37 | + const app = expressApp(config); |
| 38 | + agent = request.agent(app); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + nock.cleanAll(); |
| 43 | + sinon.restore(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should get the tx hex for broadcasting from eve on musig recovery ', async () => { |
| 47 | + // sdk call mock on mbe |
| 48 | + const recoverStub = sinon |
| 49 | + .stub(AbstractEthLikeNewCoins.prototype, 'recover') |
| 50 | + .resolves(ethRecoveryData.unsignedSweepPrebuildTx); |
| 51 | + |
| 52 | + // the call to eve.recoverWallet(...) |
| 53 | + // that contains the calls to sdk.signTransaction |
| 54 | + const eveRecoverWalletNock = nock(enclavedExpressUrl) |
| 55 | + .post(`/api/${coin}/multisig/recovery`, { |
| 56 | + userPub: ethRecoveryData.userKey, |
| 57 | + backupPub: ethRecoveryData.backupKey, |
| 58 | + apiKey: 'etherscan-api-token', |
| 59 | + unsignedSweepPrebuildTx: ethRecoveryData.unsignedSweepPrebuildTx, |
| 60 | + coinSpecificParams: undefined, |
| 61 | + walletContractAddress: ethRecoveryData.walletContractAddress, |
| 62 | + }) |
| 63 | + .reply(200, { |
| 64 | + txHex: ethRecoveryData.txHexFullSigned, |
| 65 | + }); |
| 66 | + |
| 67 | + // the call to our own master api express endpoint |
| 68 | + const response = await agent |
| 69 | + .post(`/api/${coin}/wallet/recovery`, (body) => { |
| 70 | + console.log('Nock received body:', body); |
| 71 | + return true; |
| 72 | + }) |
| 73 | + .set('Authorization', `Bearer ${accessToken}`) |
| 74 | + .send({ |
| 75 | + userPub: ethRecoveryData.userKey, |
| 76 | + backupPub: ethRecoveryData.backupKey, |
| 77 | + apiKey: 'etherscan-api-token', |
| 78 | + walletContractAddress: ethRecoveryData.walletContractAddress, |
| 79 | + recoveryDestinationAddress: ethRecoveryData.recoveryDestinationAddress, |
| 80 | + }); |
| 81 | + |
| 82 | + response.status.should.equal(200); |
| 83 | + response.body.should.have.property('txHex', ethRecoveryData.txHexFullSigned); |
| 84 | + sinon.assert.calledOnce(recoverStub); |
| 85 | + eveRecoverWalletNock.done(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should fail when walletContractAddress (origin) not provided', async () => { |
| 89 | + const response = await agent |
| 90 | + .post(`/api/${coin}/wallet/recovery`) |
| 91 | + .set('Authorization', `Bearer ${accessToken}`) |
| 92 | + .send({ |
| 93 | + userPub: ethRecoveryData.userKey, |
| 94 | + backupPub: ethRecoveryData.backupKey, |
| 95 | + apiKey: 'etherscan-api-token', |
| 96 | + walletContractAddress: undefined, |
| 97 | + recoveryDestinationAddress: ethRecoveryData.recoveryDestinationAddress, |
| 98 | + }); |
| 99 | + |
| 100 | + response.status.should.equal(400); |
| 101 | + response.body.should.have.property('error'); |
| 102 | + response.body.error.should.match(/walletContractAddress/i); |
| 103 | + }); |
| 104 | + it('should fail when recoveryDestinationAddress (destiny) not provided', async () => { |
| 105 | + const response = await agent |
| 106 | + .post(`/api/${coin}/wallet/recovery`) |
| 107 | + .set('Authorization', `Bearer ${accessToken}`) |
| 108 | + .send({ |
| 109 | + userPub: ethRecoveryData.userKey, |
| 110 | + backupPub: ethRecoveryData.backupKey, |
| 111 | + apiKey: 'etherscan-api-token', |
| 112 | + walletContractAddress: ethRecoveryData.walletContractAddress, |
| 113 | + recoveryDestinationAddress: undefined, |
| 114 | + }); |
| 115 | + |
| 116 | + response.status.should.equal(400); |
| 117 | + response.body.should.have.property('error'); |
| 118 | + response.body.error.should.match(/recoveryDestinationAddress/i); |
| 119 | + }); |
| 120 | + it('should fail when userPub or backupPub not provided', async () => { |
| 121 | + const responseNoUserKey = await agent |
| 122 | + .post(`/api/${coin}/wallet/recovery`) |
| 123 | + .set('Authorization', `Bearer ${accessToken}`) |
| 124 | + .send({ |
| 125 | + backupPub: ethRecoveryData.backupKey, |
| 126 | + apiKey: 'etherscan-api-token', |
| 127 | + walletContractAddress: ethRecoveryData.walletContractAddress, |
| 128 | + recoveryDestinationAddress: undefined, |
| 129 | + }); |
| 130 | + |
| 131 | + const responseNoBackupKey = await agent |
| 132 | + .post(`/api/${coin}/wallet/recovery`) |
| 133 | + .set('Authorization', `Bearer ${accessToken}`) |
| 134 | + .send({ |
| 135 | + userPub: ethRecoveryData.userKey, |
| 136 | + apiKey: 'etherscan-api-token', |
| 137 | + walletContractAddress: ethRecoveryData.walletContractAddress, |
| 138 | + recoveryDestinationAddress: undefined, |
| 139 | + }); |
| 140 | + |
| 141 | + responseNoUserKey.status.should.equal(400); |
| 142 | + responseNoUserKey.body.should.have.property('error'); |
| 143 | + responseNoUserKey.body.error.should.match(/userPub/i); |
| 144 | + |
| 145 | + responseNoBackupKey.status.should.equal(400); |
| 146 | + responseNoBackupKey.body.should.have.property('error'); |
| 147 | + responseNoBackupKey.body.error.should.match(/backupPub/i); |
| 148 | + }); |
| 149 | +}); |
0 commit comments