-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathtxFormat.ts
More file actions
139 lines (120 loc) · 4.53 KB
/
txFormat.ts
File metadata and controls
139 lines (120 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as assert from 'assert';
import { Wallet } from '@bitgo/sdk-core';
import { AbstractUtxoCoin, TxFormat } from '../../src';
import { isMainnetCoin, isTestnetCoin } from '../../src/names';
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) => isTestnetCoin(coin.name),
expectedTxFormat: 'psbt-lite',
});
// All mainnet wallets default to psbt-lite
runTest({
description: 'should return psbt-lite for all mainnet wallets',
coinFilter: (coin) => isMainnetCoin(coin.name),
expectedTxFormat: 'psbt-lite',
});
// Test explicitly requested formats
runTest({
description: 'should map explicitly requested legacy format to psbt-lite',
expectedTxFormat: 'psbt-lite',
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',
});
});
});