-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateWallet.test.ts
More file actions
162 lines (145 loc) · 4.74 KB
/
Copy pathgenerateWallet.test.ts
File metadata and controls
162 lines (145 loc) · 4.74 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
import 'should';
import * as request from 'supertest';
import nock from 'nock';
import { app as expressApp } from '../../../masterExpressApp';
import { AppMode, MasterExpressConfig, TlsMode } from '../../../shared/types';
import { Environments } from '@bitgo/sdk-core';
import assert from 'assert';
describe('POST /api/:coin/wallet/generate', () => {
let agent: request.SuperAgentTest;
const enclavedExpressUrl = 'http://enclaved.invalid';
const bitgoApiUrl = Environments.test.uri;
const coin = 'tbtc';
const accessToken = 'test-token';
before(() => {
nock.disableNetConnect();
nock.enableNetConnect('127.0.0.1');
const config: MasterExpressConfig = {
appMode: AppMode.MASTER_EXPRESS,
port: 0, // Let OS assign a free port
bind: 'localhost',
timeout: 60000,
logFile: '',
env: 'test',
disableEnvCheck: true,
authVersion: 2,
enclavedExpressUrl: enclavedExpressUrl,
enclavedExpressCert: 'dummy-cert',
tlsMode: TlsMode.DISABLED,
mtlsRequestCert: false,
allowSelfSigned: true,
};
const app = expressApp(config);
agent = request.agent(app);
});
afterEach(() => {
nock.cleanAll();
});
it('should generate a wallet by calling the enclaved express service', async () => {
const userKeychainNock = nock(enclavedExpressUrl)
.post(`/api/${coin}/key/independent`, {
source: 'user',
})
.reply(200, {
pub: 'xpub_user',
source: 'user',
type: 'independent',
});
const backupKeychainNock = nock(enclavedExpressUrl)
.post(`/api/${coin}/key/independent`, {
source: 'backup',
})
.reply(200, {
pub: 'xpub_backup',
source: 'backup',
type: 'independent',
});
const bitgoAddUserKeyNock = nock(bitgoApiUrl)
.post(`/api/v2/${coin}/key`, {
pub: 'xpub_user',
keyType: 'independent',
source: 'user',
})
.matchHeader('any', () => true)
.reply(200, { id: 'user-key-id', pub: 'xpub_user' });
const bitgoAddBackupKeyNock = nock(bitgoApiUrl)
.post(`/api/v2/${coin}/key`, {
pub: 'xpub_backup',
keyType: 'independent',
source: 'backup',
})
.matchHeader('any', () => true)
.reply(200, { id: 'backup-key-id', pub: 'xpub_backup' });
const bitgoAddBitGoKeyNock = nock(bitgoApiUrl)
.post(`/api/v2/${coin}/key`, {
source: 'bitgo',
keyType: 'independent',
enterprise: 'test_enterprise',
})
.reply(200, { id: 'bitgo-key-id', pub: 'xpub_bitgo' });
const bitgoAddWalletNock = nock(bitgoApiUrl)
.post(`/api/v2/${coin}/wallet/add`, {
label: 'test_wallet',
m: 2,
n: 3,
keys: ['user-key-id', 'backup-key-id', 'bitgo-key-id'],
type: 'cold',
subType: 'onPrem',
multisigType: 'onchain',
enterprise: 'test_enterprise',
})
.matchHeader('any', () => true)
.reply(200, {
id: 'new-wallet-id',
multisigType: 'onchain',
type: 'cold',
subType: 'onPrem',
});
const response = await agent
.post(`/api/${coin}/wallet/generate`)
.set('Authorization', `Bearer ${accessToken}`)
.send({
label: 'test_wallet',
enterprise: 'test_enterprise',
});
response.status.should.equal(200);
response.body.should.have.property('wallet');
response.body.wallet.should.have.properties({
id: 'new-wallet-id',
multisigType: 'onchain',
type: 'cold',
subType: 'onPrem',
});
response.body.should.have.propertyByPath('userKeychain', 'pub').eql('xpub_user');
response.body.should.have.propertyByPath('backupKeychain', 'pub').eql('xpub_backup');
response.body.should.have.propertyByPath('bitgoKeychain', 'pub').eql('xpub_bitgo');
userKeychainNock.done();
backupKeychainNock.done();
bitgoAddUserKeyNock.done();
bitgoAddBackupKeyNock.done();
bitgoAddBitGoKeyNock.done();
bitgoAddWalletNock.done();
});
it('should fail when enclaved express client is not configured', async () => {
// Create a config without enclaved express settings
const invalidConfig: Partial<MasterExpressConfig> = {
appMode: AppMode.MASTER_EXPRESS,
port: 0,
bind: 'localhost',
timeout: 60000,
logFile: '',
env: 'test',
disableEnvCheck: true,
authVersion: 2,
tlsMode: TlsMode.DISABLED,
mtlsRequestCert: false,
allowSelfSigned: true,
};
try {
expressApp(invalidConfig as MasterExpressConfig);
assert(false, 'Expected error to be thrown when enclaved express client is not configured');
} catch (e) {
(e as Error).message.should.equal('enclavedExpressUrl and enclavedExpressCert are required');
}
});
});