@@ -9,9 +9,10 @@ import nock = require('nock');
99import should = require( 'should' ) ;
1010import * 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' ;
1313import { TestBitGo } from '@bitgo/sdk-test' ;
1414import { BitGo } from '../../../src/bitgo' ;
15+ import { SinonStub } from 'sinon' ;
1516
1617describe ( '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} ) ;
0 commit comments