-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrecoveryMpcV2.test.ts
More file actions
229 lines (193 loc) · 7.35 KB
/
Copy pathrecoveryMpcV2.test.ts
File metadata and controls
229 lines (193 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { AppMode, AdvancedWalletManagerConfig, TlsMode, SigningMode } from '../../../initConfig';
import { app as advancedWalletManagerApp } from '../../../advancedWalletManagerApp';
import express from 'express';
import nock from 'nock';
import 'should';
import * as request from 'supertest';
import * as sinon from 'sinon';
import * as configModule from '../../../initConfig';
import { DklsTypes, DklsUtils } from '@bitgo-beta/sdk-lib-mpc';
describe('recoveryMpcV2', () => {
let cfg: AdvancedWalletManagerConfig;
let app: express.Application;
let agent: request.SuperAgentTest;
// test config
const keyProviderUrl = 'http://key-provider.invalid';
const ethLikeCoin = 'hteth';
const cosmosLikeCoin = 'tsei';
const accessToken = 'test-token';
// sinon sandbox
const sandbox = sinon.createSandbox();
let configStub: sinon.SinonStub;
// key provider nocks setup
let userKeyShare: string;
let backupKeyShare: string;
let commonKeychain: string;
let mockKeyProviderUserResponse: { prv: string; pub: string; source: string; type: string };
let mockKeyProviderBackupResponse: { prv: string; pub: string; source: string; type: string };
let input: { txHex: string; pub: string };
before(async () => {
const [userShare, backupShare] = await DklsUtils.generateDKGKeyShares();
userKeyShare = userShare.getKeyShare().toString('base64');
backupKeyShare = backupShare.getKeyShare().toString('base64');
commonKeychain = DklsTypes.getCommonKeychain(userShare.getKeyShare());
mockKeyProviderUserResponse = {
prv: JSON.stringify(userKeyShare),
pub: commonKeychain,
source: 'user',
type: 'tss',
};
mockKeyProviderBackupResponse = {
prv: JSON.stringify(backupKeyShare),
pub: commonKeychain,
source: 'backup',
type: 'tss',
};
input = {
txHex:
'02f6824268018502540be4008504a817c80083030d409443442e403d64d29c4f64065d0c1a0e8edc03d6c88801550f7dca700000823078c0',
pub: commonKeychain,
};
// nock config
nock.disableNetConnect();
nock.enableNetConnect('127.0.0.1');
// app config
cfg = {
appMode: AppMode.ADVANCED_WALLET_MANAGER,
signingMode: SigningMode.LOCAL,
port: 0, // Let OS assign a free port
bind: 'localhost',
timeout: 60000,
keyProviderUrl: keyProviderUrl,
httpLoggerFile: '',
tlsMode: TlsMode.DISABLED,
clientCertAllowSelfSigned: true,
recoveryMode: true,
};
configStub = sandbox.stub(configModule, 'initConfig').returns(cfg);
// app setup
app = advancedWalletManagerApp(cfg);
agent = request.agent(app);
});
afterEach(() => {
nock.cleanAll();
});
after(() => {
sandbox.restore();
});
// happy path test
it('should be sign a Mpc V2 Recovery', async () => {
// nocks for key provider responses
const userKeyProviderNock = nock(keyProviderUrl)
.get(`/key/${input.pub}`)
.query({ source: 'user' })
.reply(200, mockKeyProviderUserResponse)
.persist();
const backupKeyProviderNock = nock(keyProviderUrl)
.get(`/key/${input.pub}`)
.query({ source: 'backup' })
.reply(200, mockKeyProviderBackupResponse)
.persist();
const ethLikeSignatureResponse = await agent
.post(`/api/${ethLikeCoin}/mpcv2/recovery`)
.set('Authorization', `Bearer ${accessToken}`)
.send(input);
ethLikeSignatureResponse.status.should.equal(200);
ethLikeSignatureResponse.body.should.have.property('txHex');
ethLikeSignatureResponse.body.txHex.should.equal(input.txHex);
ethLikeSignatureResponse.body.should.have.property('stringifiedSignature');
const ethLikeSignature = JSON.parse(ethLikeSignatureResponse.body.stringifiedSignature);
ethLikeSignature.should.have.property('recid');
ethLikeSignature.should.have.property('r');
ethLikeSignature.should.have.property('s');
ethLikeSignature.should.have.property('y');
const cosmosLikeSignatureResponse = await agent
.post(`/api/${cosmosLikeCoin}/mpcv2/recovery`)
.set('Authorization', `Bearer ${accessToken}`)
.send(input);
cosmosLikeSignatureResponse.status.should.equal(200);
cosmosLikeSignatureResponse.body.should.have.property('txHex');
cosmosLikeSignatureResponse.body.txHex.should.equal(input.txHex);
cosmosLikeSignatureResponse.body.should.have.property('stringifiedSignature');
const cosmosLikeSignature = JSON.parse(cosmosLikeSignatureResponse.body.stringifiedSignature);
cosmosLikeSignature.should.have.property('recid');
cosmosLikeSignature.should.have.property('r');
cosmosLikeSignature.should.have.property('s');
cosmosLikeSignature.should.have.property('y');
userKeyProviderNock.isDone().should.be.true();
backupKeyProviderNock.isDone().should.be.true();
});
it('should route backup key retrieval to backup KMS when configured', async () => {
const kmsUrl = 'http://kms.invalid';
const backupKmsUrl = 'http://backup-kms.invalid';
const mockKmsUserResponse = {
prv: JSON.stringify(userKeyShare),
pub: commonKeychain,
source: 'user',
type: 'tss',
};
const mockKmsBackupResponse = {
prv: JSON.stringify(backupKeyShare),
pub: commonKeychain,
source: 'backup',
type: 'tss',
};
// Reconfigure app with backup KMS URL
const dualCfg: AdvancedWalletManagerConfig = {
...cfg,
keyProviderUrl: kmsUrl,
backupKmsUrl,
};
configStub.returns(dualCfg);
const dualApp = advancedWalletManagerApp(dualCfg);
const dualAgent = request.agent(dualApp);
// User key served from primary KMS
const userKmsNock = nock(kmsUrl)
.get(`/key/${input.pub}`)
.query({ source: 'user' })
.reply(200, mockKmsUserResponse)
.persist();
// Backup key served from backup KMS
const backupKmsNock = nock(backupKmsUrl)
.get(`/key/${input.pub}`)
.query({ source: 'backup' })
.reply(200, mockKmsBackupResponse)
.persist();
const response = await dualAgent
.post(`/api/${ethLikeCoin}/mpcv2/recovery`)
.set('Authorization', `Bearer ${accessToken}`)
.send(input);
response.status.should.equal(200);
response.body.should.have.property('txHex');
response.body.should.have.property('stringifiedSignature');
userKmsNock.isDone().should.be.true();
backupKmsNock.isDone().should.be.true();
});
// failure test case
it('should throw 400 Bad Request if failed to construct eth transaction from message hex', async () => {
const input = {
txHex: 'invalid-hex',
pub: commonKeychain,
};
// nocks for key provider responses
nock(keyProviderUrl)
.get(`/key/${input.pub}`)
.query({ source: 'user' })
.reply(200, mockKeyProviderUserResponse);
nock(keyProviderUrl)
.get(`/key/${input.pub}`)
.query({ source: 'backup' })
.reply(200, mockKeyProviderBackupResponse);
const signatureResponse = await agent
.post(`/api/${ethLikeCoin}/mpcv2/recovery`)
.set('Authorization', `Bearer ${accessToken}`)
.send(input);
signatureResponse.status.should.equal(400);
signatureResponse.body.should.have.property('error');
signatureResponse.body.error.should.equal('BadRequestError');
signatureResponse.body.should.have.property('details');
signatureResponse.body.details.should.startWith(
'Failed to construct eth transaction from message hex',
);
});
});