Skip to content

Commit bfef631

Browse files
OttoAllmendingerllm-git
andcommitted
feat(abstract-utxo): use coin name instead of network to determine chain
Refactor chain determination to use coin name instead of deprecated direct network usage. This approach is more consistent and avoids accessing internal network objects directly. Issue: BTC-2916 Co-authored-by: llm-git <llm-git@ttll.de>
1 parent 8230c95 commit bfef631

4 files changed

Lines changed: 20 additions & 14 deletions

File tree

modules/abstract-utxo/test/unit/coins.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ import * as assert from 'assert';
22

33
import * as utxolib from '@bitgo/utxo-lib';
44

5-
import { getMainnetCoinName, utxoCoinsMainnet, utxoCoinsTestnet } from '../../src/names';
5+
import { getMainnetCoinName, getNetworkFromCoinName, utxoCoinsMainnet, utxoCoinsTestnet } from '../../src/names';
66

77
import { getUtxoCoinForNetwork, utxoCoins } from './util';
88

99
describe('utxoCoins', function () {
1010
it('has expected chain/network values for items', function () {
1111
assert.deepStrictEqual(
12-
utxoCoins.map((c) => [c.getChain(), c.getFamily(), c.getFullName(), utxolib.getNetworkName(c.network)]),
12+
utxoCoins.map((c) => [
13+
c.getChain(),
14+
c.getFamily(),
15+
c.getFullName(),
16+
utxolib.getNetworkName(getNetworkFromCoinName(c.name)),
17+
]),
1318
[
1419
['btc', 'btc', 'Bitcoin', 'bitcoin'],
1520
['tbtc', 'btc', 'Testnet Bitcoin', 'testnet'],

modules/abstract-utxo/test/unit/prebuildAndSign.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { common, HalfSignedUtxoTransaction, Wallet } from '@bitgo/sdk-core';
66
import { getSeed } from '@bitgo/sdk-test';
77

88
import { AbstractUtxoCoin, getReplayProtectionAddresses } from '../../src';
9+
import { getMainnetCoinName } from '../../src/names';
910

1011
import { defaultBitGo, encryptKeychain, getDefaultWalletKeys, getUtxoWallet, keychainsBase58, utxoCoins } from './util';
1112

@@ -295,7 +296,7 @@ function run(coin: AbstractUtxoCoin, inputScripts: ScriptType[], txFormat: TxFor
295296
}
296297

297298
utxoCoins
298-
.filter((coin) => utxolib.getMainnet(coin.network) !== utxolib.networks.bitcoinsv)
299+
.filter((coin) => getMainnetCoinName(coin.name) !== 'bsv')
299300
.forEach((coin) => {
300301
scriptTypes
301302
// Don't iterate over p2shP2pk - in no scenario would a wallet spend two p2shP2pk inputs as these

modules/abstract-utxo/test/unit/recovery/crossChainRecovery.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
generateAddress,
1717
convertLtcAddressToLegacyFormat,
1818
} from '../../../src';
19+
import { isMainnetCoin, isTestnetCoin } from '../../../src/names';
1920
import {
2021
getFixture,
2122
keychainsBase58,
@@ -265,8 +266,8 @@ utxoCoins.forEach((coin) => {
265266
(otherCoin) =>
266267
coin !== otherCoin &&
267268
isSupportedCrossChainRecovery(coin, otherCoin) &&
268-
((utxolib.isMainnet(coin.network) && utxolib.isMainnet(otherCoin.network)) ||
269-
(utxolib.isTestnet(coin.network) && utxolib.isTestnet(otherCoin.network)))
269+
((isMainnetCoin(coin.name) && isMainnetCoin(otherCoin.name)) ||
270+
(isTestnetCoin(coin.name) && isTestnetCoin(otherCoin.name)))
270271
)
271272
.forEach((otherCoin) => {
272273
if (coin.amountType === 'bigint') {

modules/abstract-utxo/test/unit/txFormat.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as assert from 'assert';
22

3-
import * as utxolib from '@bitgo/utxo-lib';
43
import { Wallet } from '@bitgo/sdk-core';
54

65
import { AbstractUtxoCoin, ErrorDeprecatedTxFormat, TxFormat } from '../../src';
6+
import { getMainnetCoinName, isMainnetCoin, isTestnetCoin } from '../../src/names';
77

88
import { utxoCoins, defaultBitGo } from './util';
99

@@ -106,39 +106,38 @@ describe('txFormat', function () {
106106
// All testnet wallets default to PSBT-lite
107107
runTest({
108108
description: 'should always return psbt-lite for testnet',
109-
coinFilter: (coin) => utxolib.isTestnet(coin.network),
109+
coinFilter: (coin) => isTestnetCoin(coin.name),
110110
expectedTxFormat: 'psbt-lite',
111111
});
112112

113113
// DistributedCustody wallets default to PSBT (mainnet only, testnet already covered)
114114
runTest({
115115
description: 'should return psbt for distributedCustody wallets on mainnet',
116-
coinFilter: (coin) => utxolib.isMainnet(coin.network),
116+
coinFilter: (coin) => isMainnetCoin(coin.name),
117117
walletFilter: (w) => w.options.subType === 'distributedCustody',
118118
expectedTxFormat: 'psbt',
119119
});
120120

121121
// MuSig2 wallets default to PSBT (mainnet only, testnet already covered)
122122
runTest({
123123
description: 'should return psbt for wallets with musigKp flag on mainnet',
124-
coinFilter: (coin) => utxolib.isMainnet(coin.network),
124+
coinFilter: (coin) => isMainnetCoin(coin.name),
125125
walletFilter: (w) => Boolean(w.options.walletFlags?.some((f) => f.name === 'musigKp' && f.value === 'true')),
126126
expectedTxFormat: 'psbt',
127127
});
128128

129129
// Mainnet Bitcoin hot wallets default to PSBT
130130
runTest({
131131
description: 'should return psbt for mainnet bitcoin hot wallets',
132-
coinFilter: (coin) =>
133-
utxolib.isMainnet(coin.network) && utxolib.getMainnet(coin.network) === utxolib.networks.bitcoin,
132+
coinFilter: (coin) => isMainnetCoin(coin.name) && getMainnetCoinName(coin.name) === 'btc',
134133
walletFilter: (w) => w.options.type === 'hot',
135134
expectedTxFormat: 'psbt',
136135
});
137136

138137
// Other mainnet wallets do NOT default to PSBT
139138
runTest({
140139
description: 'should return undefined for other mainnet wallets',
141-
coinFilter: (coin) => utxolib.isMainnet(coin.network),
140+
coinFilter: (coin) => isMainnetCoin(coin.name),
142141
walletFilter: (w) => {
143142
const isHotBitcoin = w.options.type === 'hot'; // This will be bitcoin hot wallets
144143
const isDistributedCustody = w.options.subType === 'distributedCustody';
@@ -152,7 +151,7 @@ describe('txFormat', function () {
152151
// Test explicitly requested formats
153152
runTest({
154153
description: 'should respect explicitly requested legacy format on mainnet',
155-
coinFilter: (coin) => utxolib.isMainnet(coin.network),
154+
coinFilter: (coin) => isMainnetCoin(coin.name),
156155
expectedTxFormat: 'legacy',
157156
requestedTxFormat: 'legacy',
158157
});
@@ -172,7 +171,7 @@ describe('txFormat', function () {
172171
// Test that legacy format is prohibited on testnet
173172
it('should throw ErrorDeprecatedTxFormat when legacy format is requested on testnet', function () {
174173
for (const coin of utxoCoins) {
175-
if (!utxolib.isTestnet(coin.network)) {
174+
if (!isTestnetCoin(coin.name)) {
176175
continue;
177176
}
178177

0 commit comments

Comments
 (0)