-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathkeychains.ts
More file actions
568 lines (516 loc) · 19.4 KB
/
keychains.ts
File metadata and controls
568 lines (516 loc) · 19.4 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import { TssSettings } from '@bitgo/public-types';
import assert from 'assert';
import * as _ from 'lodash';
import * as common from '../../common';
import { IBaseCoin, KeychainsTriplet, KeyPair } from '../baseCoin';
import { BitGoBase } from '../bitgoBase';
import { decodeOrElse, ECDSAUtils, EDDSAUtils, generateRandomPassword, RequestTracer } from '../utils';
import {
AddKeychainOptions,
ApiKeyShare,
ChangedKeychains,
CreateBackupOptions,
CreateBitGoOptions,
CreateMpcOptions,
GetKeychainOptions,
GetKeysForSigningOptions,
IKeychains,
Keychain,
ListKeychainOptions,
ListKeychainsResult,
RecreateMpcOptions,
RotateKeychainOptions,
UpdatePasswordOptions,
UpdateSingleKeychainPasswordOptions,
} from './iKeychains';
import { BitGoKeyFromOvcShares, BitGoToOvcJSON, OvcToBitGoJSON } from './ovcJsonCodec';
export class Keychains implements IKeychains {
private readonly bitgo: BitGoBase;
private readonly baseCoin: IBaseCoin;
constructor(bitgo: BitGoBase, baseCoin: IBaseCoin) {
this.bitgo = bitgo;
this.baseCoin = baseCoin;
}
/**
* Get a keychain by ID
* @param params
* @param params.id
* @param params.xpub (optional)
* @param params.ethAddress (optional)
* @param params.reqId (optional)
*/
async get(params: GetKeychainOptions): Promise<Keychain> {
common.validateParams(params, [], ['xpub', 'ethAddress']);
if (_.isUndefined(params.id)) {
throw new Error('id must be defined');
}
const id = params.id;
if (params.reqId) {
this.bitgo.setRequestTracer(params.reqId);
}
return await this.bitgo.get(this.baseCoin.url('/key/' + encodeURIComponent(id))).result();
}
/**
* list the users keychains
* @param params
* @param params.limit - Max number of results in a single call.
* @param params.prevId - Continue iterating (provided by nextBatchPrevId in the previous list)
* @returns {*}
*/
async list(params: ListKeychainOptions = {}): Promise<ListKeychainsResult> {
const queryObject: any = {};
if (!_.isUndefined(params.limit)) {
if (!_.isNumber(params.limit)) {
throw new Error('invalid limit argument, expecting number');
}
queryObject.limit = params.limit;
}
if (!_.isUndefined(params.prevId)) {
if (!_.isString(params.prevId)) {
throw new Error('invalid prevId argument, expecting string');
}
queryObject.prevId = params.prevId;
}
return this.bitgo.get(this.baseCoin.url('/key')).query(queryObject).result();
}
/**
* Change the decryption password for all possible keychains associated with a user.
*
* This function iterates through all keys associated with the user, decrypts
* them with the old password and re-encrypts them with the new password.
*
* This should be called when a user changes their login password, and are expecting
* that their wallet passwords are changed to match the new login password.
*
* @param params
* @param params.oldPassword - The old password used for encrypting the key
* @param params.newPassword - The new password to be used for encrypting the key
* @returns changedKeys Object - e.g.:
* {
* xpub1: encryptedPrv,
* ...
* }
*/
async updatePassword(params: UpdatePasswordOptions): Promise<ChangedKeychains> {
common.validateParams(params, ['oldPassword', 'newPassword'], []);
const changedKeys: ChangedKeychains = {};
let prevId;
let keysLeft = true;
while (keysLeft) {
const result: ListKeychainsResult = await this.list({ limit: 500, prevId });
for (const key of result.keys) {
const oldEncryptedPrv = key.encryptedPrv;
if (_.isUndefined(oldEncryptedPrv)) {
continue;
}
try {
const updatedKeychain = this.updateSingleKeychainPassword({
keychain: key,
oldPassword: params.oldPassword,
newPassword: params.newPassword,
});
if (updatedKeychain.encryptedPrv) {
const changedKeyIdentifier = updatedKeychain.type === 'tss' ? updatedKeychain.id : updatedKeychain.pub;
if (changedKeyIdentifier) {
changedKeys[changedKeyIdentifier] = updatedKeychain.encryptedPrv;
}
}
} catch (e) {
// if the password was incorrect, silence the error, throw otherwise
if (!e.message.includes('private key is incorrect')) {
throw e;
}
}
}
if (result.nextBatchPrevId) {
prevId = result.nextBatchPrevId;
} else {
keysLeft = false;
}
}
return changedKeys;
}
/**
* Update the password used to decrypt a single keychain
* @param params
* @param params.keychain - The keychain whose password should be updated
* @param params.oldPassword - The old password used for encrypting the key
* @param params.newPassword - The new password to be used for encrypting the key
* @returns {object}
*/
updateSingleKeychainPassword(params: UpdateSingleKeychainPasswordOptions = {}): Keychain {
if (!_.isString(params.oldPassword)) {
throw new Error('expected old password to be a string');
}
if (!_.isString(params.newPassword)) {
throw new Error('expected new password to be a string');
}
if (!_.isObject(params.keychain) || !_.isString(params.keychain.encryptedPrv)) {
throw new Error('expected keychain to be an object with an encryptedPrv property');
}
const oldEncryptedPrv = params.keychain.encryptedPrv;
try {
const decryptedPrv = this.bitgo.decrypt({ input: oldEncryptedPrv, password: params.oldPassword });
const newEncryptedPrv = this.bitgo.encrypt({ input: decryptedPrv, password: params.newPassword });
return _.assign({}, params.keychain, { encryptedPrv: newEncryptedPrv });
} catch (e) {
// catching an error here means that the password was incorrect or, less likely, the input to decrypt is corrupted
throw new Error('password used to decrypt keychain private key is incorrect');
}
}
/**
* Create a public/private key pair
* @param params - optional params
* @param params.seed optional - seed to use for keypair generation
* @param params.isRootKey optional - whether the resulting keypair should be a root key
* @returns {KeyPair} - the generated keypair
*/
create(params: { seed?: Buffer; isRootKey?: boolean } = {}): KeyPair {
if (params?.isRootKey) {
return this.baseCoin.generateRootKeyPair(params.seed);
}
return this.baseCoin.generateKeyPair(params.seed);
}
/**
* Add a keychain to BitGo's records
* @param params
*/
async add(params: AddKeychainOptions = {}): Promise<Keychain> {
params = params || {};
common.validateParams(
params,
[],
[
'pub',
'encryptedPrv',
'keyType',
'type',
'source',
'originalPasscodeEncryptionCode',
'enterprise',
'derivedFromParentWithSeed',
]
);
if (!_.isUndefined(params.disableKRSEmail)) {
if (!_.isBoolean(params.disableKRSEmail)) {
throw new Error('invalid disableKRSEmail argument, expecting boolean');
}
}
if (params.reqId) {
this.bitgo.setRequestTracer(params.reqId);
}
return await this.bitgo
.post(this.baseCoin.url('/key'))
.send({
pub: params.pub,
commonPub: params.commonPub,
commonKeychain: params.commonKeychain,
encryptedPrv: params.encryptedPrv,
type: params.type,
keyType: params.keyType,
source: params.source,
provider: params.provider,
originalPasscodeEncryptionCode: params.originalPasscodeEncryptionCode,
enterprise: params.enterprise,
derivedFromParentWithSeed: params.derivedFromParentWithSeed,
disableKRSEmail: params.disableKRSEmail,
krsSpecific: params.krsSpecific,
keyShares: params.keyShares,
userGPGPublicKey: params.userGPGPublicKey,
backupGPGPublicKey: params.backupGPGPublicKey,
algoUsed: params.algoUsed,
isDistributedCustody: params.isDistributedCustody,
isMPCv2: params.isMPCv2,
coinSpecific: params.coinSpecific,
})
.result();
}
/**
* Create a BitGo key
* @param params (empty)
*/
async createBitGo(params: CreateBitGoOptions = {}): Promise<Keychain> {
params.source = 'bitgo';
this.baseCoin.preCreateBitGo(params as any);
return await this.add(params);
}
/**
* Create a backup key
* @param params
* @param params.provider (optional)
*/
async createBackup(params: CreateBackupOptions = {}): Promise<Keychain> {
params.source = 'backup';
const isTssBackupKey = params.prv && (params.commonKeychain || params.commonPub);
if (_.isUndefined(params.provider) && !isTssBackupKey) {
// if the provider is undefined, we generate a local key and add the source details
const key = this.create();
_.extend(params, key);
if (params.passphrase !== undefined) {
_.extend(params, { encryptedPrv: this.bitgo.encrypt({ input: key.prv, password: params.passphrase }) });
}
}
const serverResponse = await this.add(params);
return _.extend({}, serverResponse, _.pick(params, ['prv', 'encryptedPrv', 'provider', 'source']));
}
/**
* Gets keys for signing from a wallet
* @param params
* @returns {Promise<Keychain[]>}
*/
async getKeysForSigning(params: GetKeysForSigningOptions = {}): Promise<Keychain[]> {
if (!_.isObject(params.wallet)) {
throw new Error('missing required param wallet');
}
const wallet = params.wallet;
const reqId = params.reqId || new RequestTracer();
const ids = wallet.baseCoin.keyIdsForSigning();
const keychainQueriesBluebirds = ids.map((id) => this.get({ id: wallet.keyIds()[id], reqId }));
return Promise.all(keychainQueriesBluebirds);
}
/**
* Convenience function to create and store MPC keychains with BitGo.
* @param params passphrase used to encrypt secret materials
* @return {Promise<KeychainsTriplet>} newly created User, Backup, and BitGo keys
*/
async createMpc(params: CreateMpcOptions): Promise<KeychainsTriplet> {
let MpcUtils;
let multisigTypeVersion: 'MPCv2' | undefined = undefined;
if (params.multisigType === 'tss' && this.baseCoin.getMPCAlgorithm() === 'ecdsa') {
const tssSettings: TssSettings = await this.bitgo
.get(this.bitgo.microservicesUrl('/api/v2/tss/settings'))
.result();
multisigTypeVersion =
tssSettings.coinSettings[this.baseCoin.getFamily()]?.walletCreationSettings?.multiSigTypeVersion;
}
switch (params.multisigType) {
case 'tss':
MpcUtils =
this.baseCoin.getMPCAlgorithm() === 'eddsa'
? EDDSAUtils.default
: multisigTypeVersion === 'MPCv2'
? ECDSAUtils.EcdsaMPCv2Utils
: ECDSAUtils.EcdsaUtils;
break;
default:
throw new Error('Unsupported multi-sig type');
}
const mpcUtils = new MpcUtils(this.bitgo, this.baseCoin);
return await mpcUtils.createKeychains({
passphrase: params.passphrase,
enterprise: params.enterprise,
originalPasscodeEncryptionCode: params.originalPasscodeEncryptionCode,
retrofit: params.retrofit,
});
}
async recreateMpc(params: RecreateMpcOptions): Promise<KeychainsTriplet> {
assert(params.coin, new Error('missing required param coin'));
assert(params.walletId, new Error('missing required param walletId'));
assert(params.otp, new Error('missing required param otp'));
assert(params.passphrase, new Error('missing required param passphrase'));
assert(
params.encryptedMaterial.encryptedWalletPassphrase,
new Error('missing required param encryptedWalletPassphrase')
);
assert(params.encryptedMaterial.encryptedUserKey, new Error('missing required param encryptedUserKey'));
assert(params.encryptedMaterial.encryptedBackupKey, new Error('missing required param encryptedBackupKey'));
await this.bitgo.post(this.bitgo.microservicesUrl('/api/v1/user/unlock')).send({ otp: params.otp }).result();
const { recoveryInfo } = await this.bitgo
.post(this.bitgo.microservicesUrl(`/api/v2/${params.coin}/wallet/${params.walletId}/passcoderecovery`))
.result();
if (!recoveryInfo || !('passcodeEncryptionCode' in recoveryInfo)) {
throw new Error('failed to get recovery info');
}
const decryptedWalletPassphrase = this.bitgo.decrypt({
input: params.encryptedMaterial.encryptedWalletPassphrase,
password: recoveryInfo.passcodeEncryptionCode,
});
const decryptedUserKey = this.bitgo.decrypt({
input: params.encryptedMaterial.encryptedUserKey,
password: decryptedWalletPassphrase,
});
const decryptedBackupKey = this.bitgo.decrypt({
input: params.encryptedMaterial.encryptedBackupKey,
password: decryptedWalletPassphrase,
});
return this.createMpc({
...params,
multisigType: 'tss',
retrofit: {
decryptedUserKey,
decryptedBackupKey,
walletId: params.walletId,
},
});
}
/**
* It parses the JSON downloaded from the OVC for platform (BitGo),
* and creates a corresponding TSS BitGo key. It also returns the JSON that needs
* to be uploaded back to the OVCs containing the BitGo -> OVC shares.
* @param ovcOutputJson JSON format of the file downloaded from the OVC for platform
* @param enterprise Optional enterprise ID associated with the key
* @returns {BitGoKeyFromOvcShares}
*/
async createTssBitGoKeyFromOvcShares(ovcOutputJson: unknown, enterprise?: string): Promise<BitGoKeyFromOvcShares> {
const decodedOvcOutput = decodeOrElse(OvcToBitGoJSON.name, OvcToBitGoJSON, ovcOutputJson, (errors) => {
throw new Error(`Error(s) parsing OVC JSON: ${errors}`);
});
if (decodedOvcOutput.state !== 1) {
throw new Error('State expected to be "1". Please complete the first two OVC operations');
}
// OVC-1 is responsible for the User key
const ovc1 = decodedOvcOutput.ovc[1];
// OVC-2 is responsible for the Backup key
const ovc2 = decodedOvcOutput.ovc[2];
const keyShares: ApiKeyShare[] = [
{
from: 'user',
to: 'bitgo',
publicShare: ovc1.ovcToBitgoShare.publicShare,
privateShare: ovc1.ovcToBitgoShare.privateShare,
privateShareProof: ovc1.ovcToBitgoShare.uSig.toString() ?? '',
vssProof: ovc1.ovcToBitgoShare.vssProof ?? '',
},
{
from: 'backup',
to: 'bitgo',
publicShare: ovc2.ovcToBitgoShare.publicShare,
privateShare: ovc2.ovcToBitgoShare.privateShare,
privateShareProof: ovc2.ovcToBitgoShare.uSig.toString() ?? '',
vssProof: ovc2.ovcToBitgoShare.vssProof ?? '',
},
];
const key = await this.baseCoin.keychains().add({
source: 'bitgo',
keyShares,
keyType: 'tss',
userGPGPublicKey: ovc1.gpgPubKey,
backupGPGPublicKey: ovc2.gpgPubKey,
enterprise,
});
assert(key.keyShares);
assert(key.commonKeychain);
assert(key.walletHSMGPGPublicKeySigs);
const bitgoToUserShare = key.keyShares.find(
(value: { from: string; to: string }) => value.from === 'bitgo' && value.to === 'user'
);
assert(bitgoToUserShare);
assert(bitgoToUserShare.vssProof);
assert(bitgoToUserShare.paillierPublicKey);
const bitgoToBackupShare = key.keyShares.find(
(value: { from: string; to: string }) => value.from === 'bitgo' && value.to === 'backup'
);
assert(bitgoToBackupShare);
assert(bitgoToBackupShare.vssProof);
assert(bitgoToBackupShare.paillierPublicKey);
// Create JSON data with platform shares for OVC-1 and OVC-2
const bitgoToOvcOutput: BitGoToOvcJSON = {
wallet: {
...decodedOvcOutput,
platform: {
commonKeychain: key.commonKeychain,
walletGpgPubKeySigs: key.walletHSMGPGPublicKeySigs,
ovc: {
// BitGo to User (OVC-1)
1: {
bitgoToOvcShare: {
i: 1,
j: 3,
publicShare: bitgoToUserShare.publicShare,
privateShare: bitgoToUserShare.privateShare,
paillierPublicKey: bitgoToUserShare.paillierPublicKey,
vssProof: bitgoToUserShare.vssProof,
},
},
// BitGo to Backup (OVC-2)
2: {
bitgoToOvcShare: {
i: 2,
j: 3,
publicShare: bitgoToBackupShare.publicShare,
privateShare: bitgoToBackupShare.privateShare,
paillierPublicKey: bitgoToBackupShare.paillierPublicKey,
vssProof: bitgoToBackupShare.vssProof,
},
},
},
},
},
};
// Mark it ready for next operation, should be 2
bitgoToOvcOutput.wallet.state += 1;
const output: BitGoKeyFromOvcShares = {
bitGoKeyId: key.id,
bitGoOutputJsonForOvc: bitgoToOvcOutput,
};
return decodeOrElse(BitGoKeyFromOvcShares.name, BitGoKeyFromOvcShares, output, (errors) => {
throw new Error(`Error producing the output: ${errors}`);
});
}
/**
* Create user keychain, encrypt the private key with the wallet passphrase and store it in BitGo.
* @param walletPassphrase
* @returns Keychain including the decrypted private key
*/
async createUserKeychain(walletPassphrase: string): Promise<Keychain> {
const keychains = this.baseCoin.keychains();
const newKeychain = keychains.create();
const originalPasscodeEncryptionCode = generateRandomPassword(5);
const encryptedPrv = this.bitgo.encrypt({
password: walletPassphrase,
input: newKeychain.prv,
});
return {
...(await keychains.add({
encryptedPrv,
originalPasscodeEncryptionCode,
pub: newKeychain.pub,
source: 'user',
})),
prv: newKeychain.prv,
};
}
/**
* Rotate an ofc multi-user-keychain by recreating a new keypair, encrypt it using the user password and sent it to WP
* This is only meant to be called by ofc multi-user-wallet's key, and will throw if otherwise.
*
* Requires 2fa auth before calling. Call the bitgo.unlock() SDK method to unlock the session first.
* @param params parameters for the rotate keychain method
* @return rotatedKeychain
*/
async rotateKeychain(params: RotateKeychainOptions): Promise<Keychain> {
const keyChain = await this.get({ id: params.id });
if (!Keychains.isMultiUserKey(keyChain)) {
throw new Error(`rotateKeychain is only permitted for ofc multi-user-key wallet`);
}
let pub: string, encryptedPrv: string;
if ('encryptedPrv' in params) {
// client passed in pub and encryptedPrv directly
pub = params.pub;
encryptedPrv = params.encryptedPrv;
} else {
// bitgo generate pub and prv and encrypt it
const { pub: keyPub, prv: keyPrv } = this.create();
if (!keyPub) {
throw Error('Expected a public key to be generated');
}
pub = keyPub;
encryptedPrv = this.bitgo.encrypt({ input: keyPrv, password: params.password });
}
return this.bitgo
.put(this.baseCoin.url(`/key/${params.id}`))
.send({
encryptedPrv,
pub,
reqId: params.reqId,
})
.result();
}
/**
* Static helper method to determine if a keychain is a ofc multi-user-key
* @param keychain
*/
static isMultiUserKey(keychain: Keychain): boolean {
return (keychain.coinSpecific?.ofc?.['features'] ?? []).includes('multi-user-key');
}
}