|
| 1 | +import * as assert from 'assert'; |
| 2 | + |
| 3 | +import * as utxolib from '@bitgo/utxo-lib'; |
| 4 | +import { Wallet } from '@bitgo/sdk-core'; |
| 5 | + |
| 6 | +import { AbstractUtxoCoin, TxFormat } from '../../src'; |
| 7 | + |
| 8 | +import { utxoCoins, defaultBitGo } from './util'; |
| 9 | + |
| 10 | +type WalletType = 'hot' | 'cold' | 'custodial' | 'custodialPaired' | 'trading'; |
| 11 | +type WalletSubType = 'distributedCustody'; |
| 12 | +type WalletFlag = { name: string; value: string }; |
| 13 | + |
| 14 | +type WalletOptions = { |
| 15 | + type?: WalletType; |
| 16 | + subType?: WalletSubType; |
| 17 | + walletFlags?: WalletFlag[]; |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * Enumerates common wallet configurations for testing |
| 22 | + */ |
| 23 | +export function getWalletConfigurations(): Array<{ name: string; options: WalletOptions }> { |
| 24 | + return [ |
| 25 | + { name: 'hot wallet', options: { type: 'hot' } }, |
| 26 | + { name: 'cold wallet', options: { type: 'cold' } }, |
| 27 | + { name: 'custodial wallet', options: { type: 'custodial' } }, |
| 28 | + { name: 'distributedCustody wallet', options: { type: 'cold', subType: 'distributedCustody' } }, |
| 29 | + { name: 'musigKp wallet', options: { type: 'cold', walletFlags: [{ name: 'musigKp', value: 'true' }] } }, |
| 30 | + { name: 'hot musigKp wallet', options: { type: 'hot', walletFlags: [{ name: 'musigKp', value: 'true' }] } }, |
| 31 | + ]; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Helper function to create a mock wallet for testing |
| 36 | + */ |
| 37 | +export function createMockWallet(coin: AbstractUtxoCoin, options: WalletOptions = {}): Wallet { |
| 38 | + const walletData = { |
| 39 | + id: '5b34252f1bf349930e34020a', |
| 40 | + coin: coin.getChain(), |
| 41 | + type: options.type || 'hot', |
| 42 | + ...(options.subType && { subType: options.subType }), |
| 43 | + ...(options.walletFlags && { walletFlags: options.walletFlags }), |
| 44 | + }; |
| 45 | + return new Wallet(defaultBitGo, coin, walletData); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Helper function to get the txFormat from a coin's getDefaultTxFormat method |
| 50 | + */ |
| 51 | +export function getTxFormat(coin: AbstractUtxoCoin, wallet: Wallet, requestedFormat?: TxFormat): TxFormat | undefined { |
| 52 | + return coin.getDefaultTxFormat(wallet, requestedFormat); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Helper function to run a txFormat test with named arguments. |
| 57 | + * By default, iterates over all wallet configurations and all coins. |
| 58 | + */ |
| 59 | +function runTest(params: { |
| 60 | + description: string; |
| 61 | + expectedTxFormat: |
| 62 | + | TxFormat |
| 63 | + | undefined |
| 64 | + | ((coin: AbstractUtxoCoin, walletConfig: WalletOptions) => TxFormat | undefined); |
| 65 | + coinFilter?: (coin: AbstractUtxoCoin) => boolean; |
| 66 | + walletFilter?: (walletConfig: { name: string; options: WalletOptions }) => boolean; |
| 67 | + requestedTxFormat?: TxFormat; |
| 68 | +}): void { |
| 69 | + it(params.description, function () { |
| 70 | + const walletConfigs = getWalletConfigurations(); |
| 71 | + |
| 72 | + for (const walletConfig of walletConfigs) { |
| 73 | + // Skip wallet configurations that don't match the filter |
| 74 | + if (params.walletFilter && !params.walletFilter(walletConfig)) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + for (const coin of utxoCoins) { |
| 79 | + // Skip coins that don't match the filter |
| 80 | + if (params.coinFilter && !params.coinFilter(coin)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + const wallet = createMockWallet(coin, walletConfig.options); |
| 85 | + const txFormat = getTxFormat(coin, wallet, params.requestedTxFormat); |
| 86 | + |
| 87 | + const expectedTxFormat = |
| 88 | + typeof params.expectedTxFormat === 'function' |
| 89 | + ? params.expectedTxFormat(coin, walletConfig.options) |
| 90 | + : params.expectedTxFormat; |
| 91 | + |
| 92 | + assert.strictEqual( |
| 93 | + txFormat, |
| 94 | + expectedTxFormat, |
| 95 | + `${params.description} - ${ |
| 96 | + walletConfig.name |
| 97 | + } - ${coin.getChain()}: expected ${expectedTxFormat}, got ${txFormat}` |
| 98 | + ); |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | +describe('txFormat', function () { |
| 105 | + describe('getDefaultTxFormat', function () { |
| 106 | + // All testnet wallets default to PSBT-lite |
| 107 | + runTest({ |
| 108 | + description: 'should always return psbt-lite for testnet', |
| 109 | + coinFilter: (coin) => utxolib.isTestnet(coin.network), |
| 110 | + expectedTxFormat: 'psbt-lite', |
| 111 | + }); |
| 112 | + |
| 113 | + // DistributedCustody wallets default to PSBT (mainnet only, testnet already covered) |
| 114 | + runTest({ |
| 115 | + description: 'should return psbt for distributedCustody wallets on mainnet', |
| 116 | + coinFilter: (coin) => utxolib.isMainnet(coin.network), |
| 117 | + walletFilter: (w) => w.options.subType === 'distributedCustody', |
| 118 | + expectedTxFormat: 'psbt', |
| 119 | + }); |
| 120 | + |
| 121 | + // MuSig2 wallets default to PSBT (mainnet only, testnet already covered) |
| 122 | + runTest({ |
| 123 | + description: 'should return psbt for wallets with musigKp flag on mainnet', |
| 124 | + coinFilter: (coin) => utxolib.isMainnet(coin.network), |
| 125 | + walletFilter: (w) => Boolean(w.options.walletFlags?.some((f) => f.name === 'musigKp' && f.value === 'true')), |
| 126 | + expectedTxFormat: 'psbt', |
| 127 | + }); |
| 128 | + |
| 129 | + // Mainnet Bitcoin hot wallets default to PSBT |
| 130 | + runTest({ |
| 131 | + description: 'should return psbt for mainnet bitcoin hot wallets', |
| 132 | + coinFilter: (coin) => |
| 133 | + utxolib.isMainnet(coin.network) && utxolib.getMainnet(coin.network) === utxolib.networks.bitcoin, |
| 134 | + walletFilter: (w) => w.options.type === 'hot', |
| 135 | + expectedTxFormat: 'psbt', |
| 136 | + }); |
| 137 | + |
| 138 | + // Other mainnet wallets do NOT default to PSBT |
| 139 | + runTest({ |
| 140 | + description: 'should return undefined for other mainnet wallets', |
| 141 | + coinFilter: (coin) => utxolib.isMainnet(coin.network), |
| 142 | + walletFilter: (w) => { |
| 143 | + const isHotBitcoin = w.options.type === 'hot'; // This will be bitcoin hot wallets |
| 144 | + const isDistributedCustody = w.options.subType === 'distributedCustody'; |
| 145 | + const hasMusigKpFlag = Boolean(w.options.walletFlags?.some((f) => f.name === 'musigKp' && f.value === 'true')); |
| 146 | + // Only test "other" wallets - exclude the special cases |
| 147 | + return !isHotBitcoin && !isDistributedCustody && !hasMusigKpFlag; |
| 148 | + }, |
| 149 | + expectedTxFormat: undefined, |
| 150 | + }); |
| 151 | + |
| 152 | + // Test explicitly requested formats |
| 153 | + runTest({ |
| 154 | + description: 'should respect explicitly requested legacy format', |
| 155 | + expectedTxFormat: 'legacy', |
| 156 | + requestedTxFormat: 'legacy', |
| 157 | + }); |
| 158 | + |
| 159 | + runTest({ |
| 160 | + description: 'should respect explicitly requested psbt format', |
| 161 | + expectedTxFormat: 'psbt', |
| 162 | + requestedTxFormat: 'psbt', |
| 163 | + }); |
| 164 | + |
| 165 | + runTest({ |
| 166 | + description: 'should respect explicitly requested psbt-lite format', |
| 167 | + expectedTxFormat: 'psbt-lite', |
| 168 | + requestedTxFormat: 'psbt-lite', |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments