-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecoveryMpcV2.test.ts
More file actions
172 lines (145 loc) · 5.75 KB
/
recoveryMpcV2.test.ts
File metadata and controls
172 lines (145 loc) · 5.75 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
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 stubs
let configStub: sinon.SinonStub;
// key provider nocks setup — initialized in before()
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 () => {
// nock config
nock.disableNetConnect();
nock.enableNetConnect('127.0.0.1');
// generate DKG key shares
const [userShare, backupShare] = await DklsUtils.generateDKGKeyShares();
const userKeyShare = userShare.getKeyShare().toString('base64');
const 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,
};
// 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 = sinon.stub(configModule, 'initConfig').returns(cfg);
// app setup
app = advancedWalletManagerApp(cfg);
agent = request.agent(app);
});
afterEach(() => {
nock.cleanAll();
});
after(() => {
configStub.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();
});
// 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',
);
});
});