-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathtxFormat.ts
More file actions
188 lines (164 loc) · 6.69 KB
/
txFormat.ts
File metadata and controls
188 lines (164 loc) · 6.69 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import * as assert from 'assert';
import * as utxolib from '@bitgo/utxo-lib';
import { Wallet } from '@bitgo/sdk-core';
import { AbstractUtxoCoin, ErrorDeprecatedTxFormat, 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 on mainnet',
coinFilter: (coin) => utxolib.isMainnet(coin.network),
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',
});
// Test that legacy format is prohibited on testnet
it('should throw ErrorDeprecatedTxFormat when legacy format is requested on testnet', function () {
for (const coin of utxoCoins) {
if (!utxolib.isTestnet(coin.network)) {
continue;
}
const wallet = createMockWallet(coin, { type: 'hot' });
assert.throws(
() => getTxFormat(coin, wallet, 'legacy'),
ErrorDeprecatedTxFormat,
`Expected ErrorDeprecatedTxFormat for ${coin.getChain()}`
);
}
});
});
});