-
Notifications
You must be signed in to change notification settings - Fork 299
feat(abstract-utxo): improve transaction format selection with PSBT defaults #7537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
51c2560
feat(abstract-utxo): add tx format unit tests
OttoAllmendinger d5abc10
feat(abstract-utxo): refactor tx format selection into dedicated func…
OttoAllmendinger a5d1612
feat(abstract-utxo): exclude Zcash from PSBT default tx format
OttoAllmendinger 45745c9
feat(abstract-utxo): default PSBT for all testnet coins minus zcash
OttoAllmendinger 076170e
feat(abstract-utxo): test over all wallet types
OttoAllmendinger 6681c74
feat(abstract-utxo): default testnet transactions to PSBT format
OttoAllmendinger 4c23213
feat(abstract-utxo): use defaultTxFormat in tests instead of hardcode…
OttoAllmendinger 7f4c3b2
feat(abstract-utxo): default to psbt-lite for testnet
OttoAllmendinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,171 @@ | ||
| import * as assert from 'assert'; | ||
|
|
||
| import * as utxolib from '@bitgo/utxo-lib'; | ||
| import { Wallet } from '@bitgo/sdk-core'; | ||
|
|
||
| import { AbstractUtxoCoin, TxFormat } from '../../src'; | ||
|
|
||
| import { utxoCoins, defaultBitGo } from './util'; | ||
|
|
||
| type WalletType = 'hot' | 'cold' | 'custodial' | 'custodialPaired' | 'trading'; | ||
| type WalletSubType = 'distributedCustody'; | ||
| type WalletFlag = { name: string; value: string }; | ||
|
|
||
| type WalletOptions = { | ||
| type?: WalletType; | ||
| subType?: WalletSubType; | ||
| walletFlags?: WalletFlag[]; | ||
| }; | ||
|
|
||
| /** | ||
| * Enumerates common wallet configurations for testing | ||
| */ | ||
| export function getWalletConfigurations(): Array<{ name: string; options: WalletOptions }> { | ||
| return [ | ||
| { name: 'hot wallet', options: { type: 'hot' } }, | ||
| { name: 'cold wallet', options: { type: 'cold' } }, | ||
| { name: 'custodial wallet', options: { type: 'custodial' } }, | ||
| { name: 'distributedCustody wallet', options: { type: 'cold', subType: 'distributedCustody' } }, | ||
| { name: 'musigKp wallet', options: { type: 'cold', walletFlags: [{ name: 'musigKp', value: 'true' }] } }, | ||
| { name: 'hot musigKp wallet', options: { type: 'hot', walletFlags: [{ name: 'musigKp', value: 'true' }] } }, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to create a mock wallet for testing | ||
| */ | ||
| export function createMockWallet(coin: AbstractUtxoCoin, options: WalletOptions = {}): Wallet { | ||
| const walletData = { | ||
| id: '5b34252f1bf349930e34020a', | ||
| coin: coin.getChain(), | ||
| type: options.type || 'hot', | ||
| ...(options.subType && { subType: options.subType }), | ||
| ...(options.walletFlags && { walletFlags: options.walletFlags }), | ||
| }; | ||
| return new Wallet(defaultBitGo, coin, walletData); | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to get the txFormat from a coin's getDefaultTxFormat method | ||
| */ | ||
| export function getTxFormat(coin: AbstractUtxoCoin, wallet: Wallet, requestedFormat?: TxFormat): TxFormat | undefined { | ||
| return coin.getDefaultTxFormat(wallet, requestedFormat); | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to run a txFormat test with named arguments. | ||
| * By default, iterates over all wallet configurations and all coins. | ||
| */ | ||
| function runTest(params: { | ||
| description: string; | ||
| expectedTxFormat: | ||
| | TxFormat | ||
| | undefined | ||
| | ((coin: AbstractUtxoCoin, walletConfig: WalletOptions) => TxFormat | undefined); | ||
| coinFilter?: (coin: AbstractUtxoCoin) => boolean; | ||
| walletFilter?: (walletConfig: { name: string; options: WalletOptions }) => boolean; | ||
| requestedTxFormat?: TxFormat; | ||
| }): void { | ||
| it(params.description, function () { | ||
| const walletConfigs = getWalletConfigurations(); | ||
|
|
||
| for (const walletConfig of walletConfigs) { | ||
| // Skip wallet configurations that don't match the filter | ||
| if (params.walletFilter && !params.walletFilter(walletConfig)) { | ||
| continue; | ||
| } | ||
|
|
||
| for (const coin of utxoCoins) { | ||
| // Skip coins that don't match the filter | ||
| if (params.coinFilter && !params.coinFilter(coin)) { | ||
| continue; | ||
| } | ||
|
|
||
| const wallet = createMockWallet(coin, walletConfig.options); | ||
| const txFormat = getTxFormat(coin, wallet, params.requestedTxFormat); | ||
|
|
||
| const expectedTxFormat = | ||
| typeof params.expectedTxFormat === 'function' | ||
| ? params.expectedTxFormat(coin, walletConfig.options) | ||
| : params.expectedTxFormat; | ||
|
|
||
| assert.strictEqual( | ||
| txFormat, | ||
| expectedTxFormat, | ||
| `${params.description} - ${ | ||
| walletConfig.name | ||
| } - ${coin.getChain()}: expected ${expectedTxFormat}, got ${txFormat}` | ||
| ); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| describe('txFormat', function () { | ||
| describe('getDefaultTxFormat', function () { | ||
| // All testnet wallets default to PSBT-lite | ||
| runTest({ | ||
| description: 'should always return psbt-lite for testnet', | ||
| coinFilter: (coin) => utxolib.isTestnet(coin.network), | ||
| expectedTxFormat: 'psbt-lite', | ||
| }); | ||
|
|
||
| // DistributedCustody wallets default to PSBT (mainnet only, testnet already covered) | ||
| runTest({ | ||
| description: 'should return psbt for distributedCustody wallets on mainnet', | ||
| coinFilter: (coin) => utxolib.isMainnet(coin.network), | ||
| walletFilter: (w) => w.options.subType === 'distributedCustody', | ||
| expectedTxFormat: 'psbt', | ||
| }); | ||
|
|
||
| // MuSig2 wallets default to PSBT (mainnet only, testnet already covered) | ||
| runTest({ | ||
| description: 'should return psbt for wallets with musigKp flag on mainnet', | ||
| coinFilter: (coin) => utxolib.isMainnet(coin.network), | ||
| walletFilter: (w) => Boolean(w.options.walletFlags?.some((f) => f.name === 'musigKp' && f.value === 'true')), | ||
| expectedTxFormat: 'psbt', | ||
| }); | ||
|
|
||
| // Mainnet Bitcoin hot wallets default to PSBT | ||
| runTest({ | ||
| description: 'should return psbt for mainnet bitcoin hot wallets', | ||
| coinFilter: (coin) => | ||
| utxolib.isMainnet(coin.network) && utxolib.getMainnet(coin.network) === utxolib.networks.bitcoin, | ||
| walletFilter: (w) => w.options.type === 'hot', | ||
| expectedTxFormat: 'psbt', | ||
| }); | ||
|
|
||
| // Other mainnet wallets do NOT default to PSBT | ||
| runTest({ | ||
| description: 'should return undefined for other mainnet wallets', | ||
| coinFilter: (coin) => utxolib.isMainnet(coin.network), | ||
| walletFilter: (w) => { | ||
| const isHotBitcoin = w.options.type === 'hot'; // This will be bitcoin hot wallets | ||
| const isDistributedCustody = w.options.subType === 'distributedCustody'; | ||
| const hasMusigKpFlag = Boolean(w.options.walletFlags?.some((f) => f.name === 'musigKp' && f.value === 'true')); | ||
| // Only test "other" wallets - exclude the special cases | ||
| return !isHotBitcoin && !isDistributedCustody && !hasMusigKpFlag; | ||
| }, | ||
| expectedTxFormat: undefined, | ||
| }); | ||
|
|
||
| // Test explicitly requested formats | ||
| runTest({ | ||
| description: 'should respect explicitly requested legacy format', | ||
| expectedTxFormat: 'legacy', | ||
| requestedTxFormat: 'legacy', | ||
| }); | ||
|
|
||
| runTest({ | ||
| description: 'should respect explicitly requested psbt format', | ||
| expectedTxFormat: 'psbt', | ||
| requestedTxFormat: 'psbt', | ||
| }); | ||
|
|
||
| runTest({ | ||
| description: 'should respect explicitly requested psbt-lite format', | ||
| expectedTxFormat: 'psbt-lite', | ||
| requestedTxFormat: 'psbt-lite', | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.