Skip to content

Commit ad6d1e4

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 ad6d1e4

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;
@@ -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,79 @@ 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 throw when trying to rotate non-ofc keychain', async function () {
898+
nock(bgUrl).get(`/api/v2/hteth/key/${mockNonOfcKeychain.id}`).query(true).reply(200, mockNonOfcKeychain);
899+
900+
await assert.rejects(
901+
async () => await nonOfcKeychains.rotateKeychain({ id: mockNonOfcKeychain.id, password: '1234' }),
902+
(err: Error) => err.message === 'rotateKeychain is only for ofc multi-user-key wallet'
903+
);
904+
});
905+
});
832906
});

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)