From 0fcfbb3353314421017d555f95d286af049523a9 Mon Sep 17 00:00:00 2001 From: David Kaplan Date: Wed, 31 Jan 2024 12:50:15 -0500 Subject: [PATCH] feat(sdk-core): util func to get the avail scripts for wallettype TICKET: BTC-849 --- .../src/bitgo/utils/abstractUtxoCoinUtil.ts | 16 ++++++++++ .../unit/bitgo/utils/abstractUtxoCoinUtil.ts | 29 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/modules/sdk-core/src/bitgo/utils/abstractUtxoCoinUtil.ts b/modules/sdk-core/src/bitgo/utils/abstractUtxoCoinUtil.ts index dc0d7f646a..709cd12f19 100644 --- a/modules/sdk-core/src/bitgo/utils/abstractUtxoCoinUtil.ts +++ b/modules/sdk-core/src/bitgo/utils/abstractUtxoCoinUtil.ts @@ -2,6 +2,7 @@ import assert from 'assert'; import { coins, UtxoCoin } from '@bitgo/statics'; import * as utxolib from '@bitgo/utxo-lib'; import ScriptType2Of3 = utxolib.bitgo.outputScripts.ScriptType2Of3; +import { WalletType } from '../wallet'; export function inferAddressType(addressDetails: { chain: number }): ScriptType2Of3 | null { return utxolib.bitgo.isChainCode(addressDetails.chain) @@ -20,3 +21,18 @@ export function getUtxoCoinScriptTypes2Of3(coinName: string): utxolib.bitgo.outp utxolib.bitgo.outputScripts.isSupportedScriptType(network, v) ); } + +/** + * Get the supported 2 of 3 script types for a given utxo coin and wallet type + * @param coinName + * @param walletType + */ +export function getUtxoCoinScriptTypesForWalletType( + coinName: string, + walletType: WalletType +): utxolib.bitgo.outputScripts.ScriptType2Of3[] { + const scriptTypes = getUtxoCoinScriptTypes2Of3(coinName); + + // Only return true for p2trMusig2 if the wallet type is hot + return scriptTypes.filter((scriptType) => (scriptType === 'p2trMusig2' ? walletType === 'hot' : true)); +} diff --git a/modules/sdk-core/test/unit/bitgo/utils/abstractUtxoCoinUtil.ts b/modules/sdk-core/test/unit/bitgo/utils/abstractUtxoCoinUtil.ts index 753934edab..2a03cbddd2 100644 --- a/modules/sdk-core/test/unit/bitgo/utils/abstractUtxoCoinUtil.ts +++ b/modules/sdk-core/test/unit/bitgo/utils/abstractUtxoCoinUtil.ts @@ -28,3 +28,32 @@ describe('getUtxoCoinScriptTypes', function () { ); }); }); + +describe('getUtxoCoinScriptTypesForWalletType', function () { + it('success', function () { + const fn = (coin: string, walletType: 'hot' | 'cold', arr: utxolib.bitgo.outputScripts.ScriptType2Of3[]) => { + const scriptTypes = getUtxoCoinScriptTypes2Of3(coin); + return arr.find((v) => scriptTypes.includes(v)); + }; + assert.ok(fn('btc', 'hot', ['p2sh', 'p2shP2wsh', 'p2wsh', 'p2tr', 'p2trMusig2'])); + assert.ok(fn('btc', 'cold', ['p2sh', 'p2shP2wsh', 'p2wsh', 'p2tr'])); + assert.ok(fn('ltc', 'hot', ['p2sh', 'p2shP2wsh', 'p2wsh'])); + assert.ok(fn('ltc', 'cold', ['p2sh', 'p2shP2wsh', 'p2wsh'])); + assert.ok(fn('doge', 'hot', ['p2sh'])); + assert.ok(fn('doge', 'cold', ['p2sh'])); + }); + + it('fail for invalid coin name', function () { + assert.throws( + () => getUtxoCoinScriptTypes2Of3('dummy'), + (e: any) => e.message === `coin 'dummy' is not defined` + ); + }); + + it('fail for non-utxo coin name', function () { + assert.throws( + () => getUtxoCoinScriptTypes2Of3('eth'), + (e: any) => e.message === 'coin eth is not a utxo coin' + ); + }); +});