Skip to content

Commit

Permalink
Merge pull request #4245 from BitGo/BTC-849-script-types-for-wallet-type
Browse files Browse the repository at this point in the history


feat(sdk-core): util func to get the avail scripts for wallettype
  • Loading branch information
davidkaplanbitgo authored Jan 31, 2024
2 parents c40d563 + 0fcfbb3 commit 4cef261
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions modules/sdk-core/src/bitgo/utils/abstractUtxoCoinUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));
}
29 changes: 29 additions & 0 deletions modules/sdk-core/test/unit/bitgo/utils/abstractUtxoCoinUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});
});

0 comments on commit 4cef261

Please sign in to comment.