Skip to content

Commit 8bd3940

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 c306c97 commit 8bd3940

File tree

3 files changed

+125
-4
lines changed

3 files changed

+125
-4
lines changed

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

Lines changed: 78 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;
@@ -171,9 +172,7 @@ describe('V2 Keychains', function () {
171172
'expected new password to be a string'
172173
);
173174

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

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

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ 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 interface RotateKeychainOptions {
98+
id: string;
99+
password: string;
100+
reqId?: IRequestTracer;
101+
}
102+
91103
export interface AddKeychainOptions {
92104
pub?: string;
93105
commonPub?: string;
@@ -214,4 +226,5 @@ export interface IKeychains {
214226
recreateMpc(params: RecreateMpcOptions): Promise<KeychainsTriplet>;
215227
createTssBitGoKeyFromOvcShares(ovcOutput: OvcToBitGoJSON, enterprise?: string): Promise<BitGoKeyFromOvcShares>;
216228
createUserKeychain(userPassword: string): Promise<Keychain>;
229+
rotateKeychain(params: RotateKeychainOptions): Promise<Keychain>;
217230
}

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

Lines changed: 34 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,37 @@ 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+
* @param params parameters for the rotate keychain method
526+
* @return rotatedKeychain
527+
*/
528+
async rotateKeychain(params: RotateKeychainOptions): Promise<Keychain> {
529+
const keyChain = await this.get({ id: params.id });
530+
if (!Keychains.isMultiUserKey(keyChain)) {
531+
throw new Error(`rotateKeychain is only permitted for ofc multi-user-key wallet`);
532+
}
533+
534+
const { pub, prv } = this.create();
535+
const encryptedPrv = this.bitgo.encrypt({ input: prv, password: params.password });
536+
537+
return this.bitgo
538+
.put(this.baseCoin.url(`/key/${params.id}`))
539+
.send({
540+
encryptedPrv,
541+
pub,
542+
reqId: params.reqId,
543+
})
544+
.result();
545+
}
546+
547+
/**
548+
* Static helper method to determine if a keychain is a ofc multi-user-key
549+
* @param keychain
550+
*/
551+
static isMultiUserKey(keychain: Keychain): boolean {
552+
return (keychain.coinSpecific?.ofc?.['features'] ?? []).includes('multi-user-key');
553+
}
520554
}

0 commit comments

Comments
 (0)