Skip to content

Commit 8d8baff

Browse files
committed
feat: added rotate keychain method for multi-user-key ofc wallet
added a new method to rotate ofc multi-user-key wallet's keychain TICKET: WP-6902
1 parent b2f6269 commit 8d8baff

File tree

3 files changed

+159
-4
lines changed

3 files changed

+159
-4
lines changed

modules/bitgo/test/v2/unit/keychains.ts

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import nock = require('nock');
99
import should = require('should');
1010
import * as sinon from 'sinon';
1111

12-
import { common, decodeOrElse, ECDSAUtils, EDDSAUtils, Keychains, OvcShare } from '@bitgo/sdk-core';
12+
import { common, decodeOrElse, ECDSAUtils, EDDSAUtils, Keychain, Keychains, OvcShare } from '@bitgo/sdk-core';
1313
import { TestBitGo } from '@bitgo/sdk-test';
1414
import { BitGo } from '../../../src/bitgo';
15+
import { SinonStub } from 'sinon';
1516

1617
describe('V2 Keychains', function () {
1718
let bitgo;
@@ -173,9 +174,7 @@ describe('V2 Keychains', function () {
173174
'expected new password to be a string'
174175
);
175176

176-
(() => keychains.updateSingleKeychainPassword({ oldPassword: '1234', newPassword: 5678 })).should.throw(
177-
'expected new password to be a string'
178-
);
177+
(() => keychains.updateSingleKeychainPassword({ oldPassword: '1234', newPassword: 5678 })).should.throw();
179178

180179
(() => keychains.updateSingleKeychainPassword({ oldPassword: '1234', newPassword: '5678' })).should.throw(
181180
'expected keychain to be an object with an encryptedPrv property'
@@ -829,4 +828,92 @@ describe('V2 Keychains', function () {
829828
const decryptedPrv = bitgo.decrypt({ input: backup.encryptedPrv, password: 't3stSicretly!' });
830829
decryptedPrv.should.startWith('xprv');
831830
});
831+
832+
describe('Rotate OFC multi-user-key keychains', function () {
833+
let ofcBaseCoin;
834+
let ofcKeychains;
835+
const mockOfcKeychain: Keychain = {
836+
id: 'ofcKeychainId',
837+
pub: 'ofcKeychainPub',
838+
encryptedPrv: 'ofcEncryptedPrv',
839+
source: 'user',
840+
coinSpecific: {
841+
ofc: {
842+
features: ['multi-user-key'],
843+
},
844+
},
845+
type: 'tss',
846+
};
847+
let nonOfcBaseCoin;
848+
let nonOfcKeychains;
849+
const mockNonOfcKeychain: Keychain = {
850+
id: 'nonOfcKeychainId',
851+
pub: 'nonOfcKeychainPub',
852+
source: 'user',
853+
type: 'tss',
854+
};
855+
856+
const mockNewKeypair = {
857+
pub: 'newPub',
858+
prv: 'newPrv',
859+
};
860+
861+
let sandbox;
862+
let updateKeychainStub: SinonStub;
863+
let createKeypairStub: SinonStub;
864+
let encryptionStub: SinonStub;
865+
866+
beforeEach(function () {
867+
ofcBaseCoin = bitgo.coin('ofc');
868+
ofcKeychains = ofcBaseCoin.keychains();
869+
870+
nonOfcBaseCoin = bitgo.coin('hteth');
871+
nonOfcKeychains = nonOfcBaseCoin.keychains();
872+
873+
sandbox = sinon.createSandbox();
874+
updateKeychainStub = sandbox.stub().returns({ result: sandbox.stub().resolves() });
875+
sandbox.stub(BitGo.prototype, 'put').returns({ send: updateKeychainStub });
876+
createKeypairStub = sandbox.stub(ofcKeychains, 'create').returns(mockNewKeypair);
877+
encryptionStub = sandbox.stub(BitGo.prototype, 'encrypt').returns('newEncryptedPrv');
878+
});
879+
880+
afterEach(function () {
881+
sandbox.restore();
882+
});
883+
884+
it('should rotate ofc multi-user-key properly', async function () {
885+
nock(bgUrl).get(`/api/v2/ofc/key/${mockOfcKeychain.id}`).query(true).reply(200, mockOfcKeychain);
886+
887+
await ofcKeychains.rotateKeychain({ id: mockOfcKeychain.id, password: '1234' });
888+
sinon.assert.called(createKeypairStub);
889+
sinon.assert.calledWith(encryptionStub, { input: mockNewKeypair.prv, password: '1234' });
890+
sinon.assert.calledWith(updateKeychainStub, {
891+
pub: mockNewKeypair.pub,
892+
encryptedPrv: 'newEncryptedPrv',
893+
reqId: undefined,
894+
});
895+
});
896+
897+
it('should allow user to supply pub and encryptedPrv directly', async function () {
898+
nock(bgUrl).get(`/api/v2/ofc/key/${mockOfcKeychain.id}`).query(true).reply(200, mockOfcKeychain);
899+
900+
await ofcKeychains.rotateKeychain({ id: mockOfcKeychain.id, pub: 'pub', encryptedPrv: 'encryptedPrv' });
901+
sinon.assert.notCalled(createKeypairStub);
902+
sinon.assert.notCalled(encryptionStub);
903+
sinon.assert.calledWith(updateKeychainStub, {
904+
pub: 'pub',
905+
encryptedPrv: 'encryptedPrv',
906+
reqId: undefined,
907+
});
908+
});
909+
910+
it('should throw when trying to rotate non-ofc keychain', async function () {
911+
nock(bgUrl).get(`/api/v2/hteth/key/${mockNonOfcKeychain.id}`).query(true).reply(200, mockNonOfcKeychain);
912+
913+
await assert.rejects(
914+
async () => await nonOfcKeychains.rotateKeychain({ id: mockNonOfcKeychain.id, password: '1234' }),
915+
(err: Error) => err.message === 'rotateKeychain is only permitted for ofc multi-user-key wallet'
916+
);
917+
});
918+
});
832919
});

modules/sdk-core/src/bitgo/keychain/iKeychains.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ export interface UpdateSingleKeychainPasswordOptions {
8888
newPassword?: string;
8989
}
9090

91+
/**
92+
* Parameters for the rotateKeychain method for ofc multi-user-key
93+
* @property {string} id - the public id of the keychain
94+
* @property {string} password - the user password use to encrypt/decrypt the private key
95+
* @property {IRequestTracer} reqId - optional reqId
96+
*/
97+
export type RotateKeychainOptions =
98+
| {
99+
id: string;
100+
password: string;
101+
reqId?: IRequestTracer;
102+
}
103+
| {
104+
id: string;
105+
pub: string;
106+
encryptedPrv: string;
107+
reqId?: IRequestTracer;
108+
};
109+
91110
export interface AddKeychainOptions {
92111
pub?: string;
93112
commonPub?: string;
@@ -214,4 +233,5 @@ export interface IKeychains {
214233
recreateMpc(params: RecreateMpcOptions): Promise<KeychainsTriplet>;
215234
createTssBitGoKeyFromOvcShares(ovcOutput: OvcToBitGoJSON, enterprise?: string): Promise<BitGoKeyFromOvcShares>;
216235
createUserKeychain(userPassword: string): Promise<Keychain>;
236+
rotateKeychain(params: RotateKeychainOptions): Promise<Keychain>;
217237
}

modules/sdk-core/src/bitgo/keychain/keychains.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
ListKeychainOptions,
2020
ListKeychainsResult,
2121
RecreateMpcOptions,
22+
RotateKeychainOptions,
2223
UpdatePasswordOptions,
2324
UpdateSingleKeychainPasswordOptions,
2425
} from './iKeychains';
@@ -517,4 +518,51 @@ export class Keychains implements IKeychains {
517518
prv: newKeychain.prv,
518519
};
519520
}
521+
522+
/**
523+
* Rotate an ofc multi-user-keychain by recreating a new keypair, encrypt it using the user password and sent it to WP
524+
* This is only meant to be called by ofc multi-user-wallet's key, and will throw if otherwise.
525+
*
526+
* Requires 2fa auth before calling. Call the bitgo.unlock() SDK method to unlock the session first.
527+
* @param params parameters for the rotate keychain method
528+
* @return rotatedKeychain
529+
*/
530+
async rotateKeychain(params: RotateKeychainOptions): Promise<Keychain> {
531+
const keyChain = await this.get({ id: params.id });
532+
if (!Keychains.isMultiUserKey(keyChain)) {
533+
throw new Error(`rotateKeychain is only permitted for ofc multi-user-key wallet`);
534+
}
535+
536+
let pub: string, encryptedPrv: string;
537+
if ('encryptedPrv' in params) {
538+
// client passed in pub and encryptedPrv directly
539+
pub = params.pub;
540+
encryptedPrv = params.encryptedPrv;
541+
} else {
542+
// bitgo generate pub and prv and encrypt it
543+
const { pub: keyPub, prv: keyPrv } = this.create();
544+
if (!keyPub) {
545+
throw Error('Expected a public key to be generated');
546+
}
547+
pub = keyPub;
548+
encryptedPrv = this.bitgo.encrypt({ input: keyPrv, password: params.password });
549+
}
550+
551+
return this.bitgo
552+
.put(this.baseCoin.url(`/key/${params.id}`))
553+
.send({
554+
encryptedPrv,
555+
pub,
556+
reqId: params.reqId,
557+
})
558+
.result();
559+
}
560+
561+
/**
562+
* Static helper method to determine if a keychain is a ofc multi-user-key
563+
* @param keychain
564+
*/
565+
static isMultiUserKey(keychain: Keychain): boolean {
566+
return (keychain.coinSpecific?.ofc?.['features'] ?? []).includes('multi-user-key');
567+
}
520568
}

0 commit comments

Comments
 (0)