|
| 1 | +import 'should'; |
| 2 | + |
| 3 | +import express from 'express'; |
| 4 | +import nock from 'nock'; |
| 5 | +import * as request from 'supertest'; |
| 6 | +import { app as enclavedApp } from '../../../enclavedApp'; |
| 7 | +import { AppMode, EnclavedConfig, TlsMode } from '../../../shared/types'; |
| 8 | + |
| 9 | +import * as sinon from 'sinon'; |
| 10 | +import * as configModule from '../../../initConfig'; |
| 11 | + |
| 12 | +import { ebeData } from '../../mocks/ethRecoveryMusigMockData'; |
| 13 | +import unsignedSweepRecJSON from '../../mocks/unsigned-sweep-prebuild-hteth-musig-recovery.json'; |
| 14 | + |
| 15 | +describe('recoveryMultisigTransaction', () => { |
| 16 | + let cfg: EnclavedConfig; |
| 17 | + let app: express.Application; |
| 18 | + let agent: request.SuperAgentTest; |
| 19 | + |
| 20 | + // test cofig |
| 21 | + const kmsUrl = 'http://kms.invalid'; |
| 22 | + const coin = 'hteth'; |
| 23 | + const accessToken = 'test-token'; |
| 24 | + |
| 25 | + // sinon stubs |
| 26 | + let configStub: sinon.SinonStub; |
| 27 | + |
| 28 | + before(() => { |
| 29 | + // nock config |
| 30 | + nock.disableNetConnect(); |
| 31 | + nock.enableNetConnect('127.0.0.1'); |
| 32 | + |
| 33 | + // app config |
| 34 | + cfg = { |
| 35 | + appMode: AppMode.ENCLAVED, |
| 36 | + port: 0, // Let OS assign a free port |
| 37 | + bind: 'localhost', |
| 38 | + timeout: 60000, |
| 39 | + logFile: '', |
| 40 | + kmsUrl: kmsUrl, |
| 41 | + tlsMode: TlsMode.DISABLED, |
| 42 | + mtlsRequestCert: false, |
| 43 | + allowSelfSigned: true, |
| 44 | + }; |
| 45 | + |
| 46 | + configStub = sinon.stub(configModule, 'initConfig').returns(cfg); |
| 47 | + |
| 48 | + // app setup |
| 49 | + app = enclavedApp(cfg); |
| 50 | + agent = request.agent(app); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + nock.cleanAll(); |
| 55 | + }); |
| 56 | + |
| 57 | + after(() => { |
| 58 | + configStub.restore(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should generate a successful txHex from unsigned sweep prebuild data', async () => { |
| 62 | + const { userPub, backupPub, walletContractAddress, userPrv, backupPrv, txHexResult } = ebeData; |
| 63 | + const unsignedSweepPrebuildTx = unsignedSweepRecJSON as unknown as any; |
| 64 | + |
| 65 | + const mockKmsUserResponse = { |
| 66 | + prv: userPrv, |
| 67 | + pub: userPub, |
| 68 | + source: 'user', |
| 69 | + type: 'independent', |
| 70 | + }; |
| 71 | + |
| 72 | + const mockKmsBackupResponse = { |
| 73 | + prv: backupPrv, |
| 74 | + pub: backupPub, |
| 75 | + source: 'backup', |
| 76 | + type: 'independent', |
| 77 | + }; |
| 78 | + |
| 79 | + const kmsNockUser = nock(kmsUrl) |
| 80 | + .get(`/key/${userPub}`) |
| 81 | + .query({ source: 'user' }) |
| 82 | + .reply(200, mockKmsUserResponse); |
| 83 | + |
| 84 | + const kmsNockBackup = nock(kmsUrl) |
| 85 | + .get(`/key/${backupPub}`) |
| 86 | + .query({ source: 'backup' }) |
| 87 | + .reply(200, mockKmsBackupResponse); |
| 88 | + |
| 89 | + console.warn(nock.activeMocks()); |
| 90 | + console.warn(nock.isActive()); |
| 91 | + |
| 92 | + const response = await agent |
| 93 | + .post(`/api/${coin}/multisig/recovery`) |
| 94 | + .set('Authorization', `Bearer ${accessToken}`) |
| 95 | + .send({ |
| 96 | + userPub, |
| 97 | + backupPub, |
| 98 | + apiKey: 'etherscan-api-token', |
| 99 | + unsignedSweepPrebuildTx, |
| 100 | + walletContractAddress, |
| 101 | + coinSpecificParams: undefined, |
| 102 | + }); |
| 103 | + |
| 104 | + response.status.should.equal(200); |
| 105 | + response.body.should.have.property('txHex', txHexResult); |
| 106 | + |
| 107 | + kmsNockUser.done(); |
| 108 | + kmsNockBackup.done(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should fail when prv keys non related to pub keys', async () => { |
| 112 | + const { userPub, backupPub, walletContractAddress } = ebeData; |
| 113 | + const unsignedSweepPrebuildTx = unsignedSweepRecJSON as unknown as any; |
| 114 | + |
| 115 | + // Use invalid private keys |
| 116 | + const invalidUserPrv = 'invalid-prv'; |
| 117 | + const invalidBackupPrv = 'invalid-prv'; |
| 118 | + |
| 119 | + const mockKmsUserResponse = { |
| 120 | + prv: invalidUserPrv, |
| 121 | + pub: userPub, |
| 122 | + source: 'user', |
| 123 | + type: 'independent', |
| 124 | + }; |
| 125 | + |
| 126 | + const mockKmsBackupResponse = { |
| 127 | + prv: invalidBackupPrv, |
| 128 | + pub: backupPub, |
| 129 | + source: 'backup', |
| 130 | + type: 'independent', |
| 131 | + }; |
| 132 | + |
| 133 | + const kmsNockUser = nock(kmsUrl) |
| 134 | + .get(`/key/${userPub}`) |
| 135 | + .query({ source: 'user' }) |
| 136 | + .reply(200, mockKmsUserResponse); |
| 137 | + |
| 138 | + const kmsNockBackup = nock(kmsUrl) |
| 139 | + .get(`/key/${backupPub}`) |
| 140 | + .query({ source: 'backup' }) |
| 141 | + .reply(200, mockKmsBackupResponse); |
| 142 | + |
| 143 | + const response = await agent |
| 144 | + .post(`/api/${coin}/multisig/recovery`) |
| 145 | + .set('Authorization', `Bearer ${accessToken}`) |
| 146 | + .send({ |
| 147 | + userPub, |
| 148 | + backupPub, |
| 149 | + apiKey: 'etherscan-api-token', |
| 150 | + unsignedSweepPrebuildTx, |
| 151 | + walletContractAddress, |
| 152 | + coinSpecificParams: undefined, |
| 153 | + }); |
| 154 | + |
| 155 | + response.status.should.equal(500); |
| 156 | + response.body.should.have.property('error'); |
| 157 | + |
| 158 | + kmsNockUser.done(); |
| 159 | + kmsNockBackup.done(); |
| 160 | + }); |
| 161 | +}); |
0 commit comments