-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sdk-core): choose correct default apiVersion for tss
TICKET: WP-3038
- Loading branch information
1 parent
f6e5392
commit c3bc7f0
Showing
7 changed files
with
178 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ApiVersion, IWallet } from '../wallet'; | ||
import assert from 'assert'; | ||
|
||
export function validateTxRequestApiVersion(wallet: IWallet, requestedApiVersion: ApiVersion): void { | ||
if (wallet.multisigType() !== 'tss') { | ||
// only tss wallets have api version requirements | ||
return; | ||
} | ||
if (wallet.baseCoin.getMPCAlgorithm() === 'ecdsa') { | ||
// ecdsa wallets can only use full, even if they are hot wallets | ||
assert(requestedApiVersion === 'full', 'For ECDSA tss wallets, parameter `apiVersion` must be `full`.'); | ||
} else if (wallet.type() !== 'hot') { | ||
// all other cases should use full! | ||
assert( | ||
requestedApiVersion === 'full', | ||
'For non self-custodial (hot) tss wallets, parameter `apiVersion` must be `full`.' | ||
); | ||
} | ||
return; | ||
} | ||
|
||
/** | ||
* Get the api version for the provided wallet. | ||
* If the user requested api version is invalid, this will throw an error. | ||
* @param wallet | ||
* @param requestedApiVersion | ||
*/ | ||
export function getTxRequestApiVersion(wallet: IWallet, requestedApiVersion?: ApiVersion): ApiVersion { | ||
if (requestedApiVersion) { | ||
validateTxRequestApiVersion(wallet, requestedApiVersion); | ||
return requestedApiVersion; | ||
} | ||
if (wallet.baseCoin.getMPCAlgorithm() === 'ecdsa') { | ||
return 'full'; | ||
} else if (wallet.type() === 'hot') { | ||
// default to lite for hot eddsa tss wallets | ||
return 'lite'; | ||
} else { | ||
// default to full for all other wallet types | ||
return 'full'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import 'should'; | ||
import { ApiVersion, IWallet } from '../../../../src'; | ||
import { getTxRequestApiVersion } from '../../../../src/bitgo/utils/txRequest'; | ||
|
||
describe('txRequest utils', () => { | ||
describe('getTxRequestApiVersion', function () { | ||
const testCases = [ | ||
{ | ||
wallet: { | ||
baseCoin: { getMPCAlgorithm: () => 'ecdsa' }, | ||
type: () => 'hot', | ||
multisigType: () => 'tss', | ||
} as any as IWallet, | ||
requestedApiVersion: 'lite', | ||
expectedApiVersion: '', | ||
expectedErrorMessage: 'For ECDSA tss wallets, parameter `apiVersion` must be `full`.', | ||
}, | ||
{ | ||
wallet: { | ||
baseCoin: { getMPCAlgorithm: () => 'eddsa' }, | ||
type: () => 'cold', | ||
multisigType: () => 'tss', | ||
} as any as IWallet, | ||
requestedApiVersion: 'lite', | ||
expectedApiVersion: '', | ||
expectedErrorMessage: 'For non self-custodial (hot) tss wallets, parameter `apiVersion` must be `full`.', | ||
}, | ||
{ | ||
wallet: { | ||
baseCoin: { getMPCAlgorithm: () => 'eddsa' }, | ||
type: () => 'hot', | ||
multisigType: () => 'tss', | ||
} as any as IWallet, | ||
requestedApiVersion: undefined, | ||
expectedApiVersion: 'lite', | ||
expectedErrorMessage: '', | ||
}, | ||
...['hot', 'cold', 'custodial', 'backing'].map((walletType) => { | ||
return { | ||
wallet: { | ||
baseCoin: { getMPCAlgorithm: () => 'ecdsa' }, | ||
type: () => walletType, | ||
multisigType: () => 'tss', | ||
} as any as IWallet, | ||
requestedApiVersion: 'full', | ||
expectedApiVersion: 'full', | ||
expectedErrorMessage: '', | ||
shouldThrow: false, | ||
}; | ||
}), | ||
...['hot', 'cold', 'custodial', 'backing'].map((walletType) => { | ||
return { | ||
wallet: { | ||
baseCoin: { getMPCAlgorithm: () => 'ecdsa' }, | ||
type: () => walletType, | ||
multisigType: () => 'tss', | ||
} as any as IWallet, | ||
requestedApiVersion: undefined, | ||
expectedApiVersion: 'full', | ||
expectedErrorMessage: '', | ||
shouldThrow: false, | ||
}; | ||
}), | ||
...['hot', 'cold', 'custodial', 'backing'].map((walletType) => { | ||
return { | ||
wallet: { | ||
baseCoin: { getMPCAlgorithm: () => 'eddsa' }, | ||
type: () => walletType, | ||
multisigType: () => 'tss', | ||
} as any as IWallet, | ||
requestedApiVersion: 'full', | ||
expectedApiVersion: 'full', | ||
expectedErrorMessage: '', | ||
shouldThrow: false, | ||
}; | ||
}), | ||
...['cold', 'custodial', 'backing'].map((walletType) => { | ||
return { | ||
wallet: { | ||
baseCoin: { getMPCAlgorithm: () => 'eddsa' }, | ||
type: () => walletType, | ||
multisigType: () => 'tss', | ||
} as any as IWallet, | ||
requestedApiVersion: undefined, | ||
expectedApiVersion: 'full', | ||
expectedErrorMessage: '', | ||
shouldThrow: false, | ||
}; | ||
}), | ||
]; | ||
|
||
testCases.forEach((testCase) => { | ||
if (testCase.expectedErrorMessage) { | ||
it(`should throw an error if requested apiVersion is ${ | ||
testCase.requestedApiVersion | ||
} for wallet type ${testCase.wallet.type()} for a ${testCase.wallet.baseCoin.getMPCAlgorithm()} wallet`, () => { | ||
(() => | ||
getTxRequestApiVersion( | ||
testCase.wallet, | ||
testCase.requestedApiVersion as ApiVersion | undefined | ||
)).should.throw(testCase.expectedErrorMessage); | ||
}); | ||
} else { | ||
it(`should return ${testCase.expectedApiVersion} if requested apiVersion is ${ | ||
testCase.requestedApiVersion | ||
} for wallet type ${testCase.wallet.type()} for a ${testCase.wallet.baseCoin.getMPCAlgorithm()} wallet`, () => { | ||
getTxRequestApiVersion(testCase.wallet, testCase.requestedApiVersion as ApiVersion | undefined).should.equal( | ||
testCase.expectedApiVersion | ||
); | ||
}); | ||
} | ||
}); | ||
}); | ||
}); |