Skip to content

Commit da490f1

Browse files
refactor(abstract-utxo): remove deprecated network getter and getNetworkFromCoinName
Drop the deprecated coin.network getter from AbstractUtxoCoin and the getNetworkFromCoinName helper from names.ts. Tests that previously called coin.network now call the test-local getNetworkForCoinName(coin.name) from util/utxoCoins. With this, names.ts no longer depends on utxolib. Refs: T1-3279
1 parent 7782b3c commit da490f1

9 files changed

Lines changed: 73 additions & 156 deletions

File tree

modules/abstract-utxo/src/abstractUtxoCoin.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,7 @@ import {
7575
ErrorImplicitExternalOutputs,
7676
} from './transaction/descriptor/verifyTransaction';
7777
import { assertDescriptorWalletAddress, getDescriptorMapFromWallet, isDescriptorWallet } from './descriptor';
78-
import {
79-
getFullNameFromCoinName,
80-
getMainnetCoinName,
81-
getNetworkFromCoinName,
82-
isMainnetCoin,
83-
UtxoCoinName,
84-
UtxoCoinNameMainnet,
85-
} from './names';
78+
import { getFullNameFromCoinName, getMainnetCoinName, isMainnetCoin, UtxoCoinName, UtxoCoinNameMainnet } from './names';
8679
import { assertFixedScriptWalletAddress } from './address/fixedScript';
8780
import { ParsedTransaction } from './transaction/types';
8881
import { decodeDescriptorPsbt, decodePsbt, encodeTransaction, stringToBufferTryFormats } from './transaction/decode';
@@ -413,14 +406,6 @@ export abstract class AbstractUtxoCoin extends BaseCoin implements Musig2Partici
413406
this.amountType = amountType;
414407
}
415408

416-
/**
417-
* @deprecated - will be removed when we drop support for utxolib
418-
* Use `name` property instead.
419-
*/
420-
get network(): utxolib.Network {
421-
return getNetworkFromCoinName(this.name);
422-
}
423-
424409
getChain(): UtxoCoinName {
425410
return this.name;
426411
}

modules/abstract-utxo/src/names.ts

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as utxolib from '@bitgo/utxo-lib';
2-
31
export const utxoCoinsMainnet = ['btc', 'bch', 'bcha', 'bsv', 'btg', 'dash', 'doge', 'ltc', 'zec'] as const;
42
export const utxoCoinsTestnet = [
53
'tbtc',
@@ -46,107 +44,6 @@ export function getMainnetCoinName(coinName: UtxoCoinName): UtxoCoinNameMainnet
4644
}
4745
}
4846

49-
function getNetworkName(n: utxolib.Network): utxolib.NetworkName {
50-
const name = utxolib.getNetworkName(n);
51-
if (!name) {
52-
throw new Error('Unknown network');
53-
}
54-
return name;
55-
}
56-
57-
/**
58-
* @deprecated - will be removed when we drop support for utxolib
59-
* @param n
60-
* @returns the family name for a network. Testnets and mainnets of the same coin share the same family name.
61-
*/
62-
export function getFamilyFromNetwork(n: utxolib.Network): UtxoCoinNameMainnet {
63-
switch (getNetworkName(n)) {
64-
case 'bitcoin':
65-
case 'testnet':
66-
case 'bitcoinPublicSignet':
67-
case 'bitcoinTestnet4':
68-
case 'bitcoinBitGoSignet':
69-
return 'btc';
70-
case 'bitcoincash':
71-
case 'bitcoincashTestnet':
72-
return 'bch';
73-
case 'ecash':
74-
case 'ecashTest':
75-
return 'bcha';
76-
case 'bitcoingold':
77-
case 'bitcoingoldTestnet':
78-
return 'btg';
79-
case 'bitcoinsv':
80-
case 'bitcoinsvTestnet':
81-
return 'bsv';
82-
case 'dash':
83-
case 'dashTest':
84-
return 'dash';
85-
case 'dogecoin':
86-
case 'dogecoinTest':
87-
return 'doge';
88-
case 'litecoin':
89-
case 'litecoinTest':
90-
return 'ltc';
91-
case 'zcash':
92-
case 'zcashTest':
93-
return 'zec';
94-
}
95-
}
96-
97-
/**
98-
* @deprecated - will be removed when we drop support for utxolib
99-
* Get the chain name for a network.
100-
* The chain is different for every network.
101-
*/
102-
export function getCoinName(n: utxolib.Network): UtxoCoinName {
103-
switch (getNetworkName(n)) {
104-
case 'bitcoinPublicSignet':
105-
return 'tbtcsig';
106-
case 'bitcoinTestnet4':
107-
return 'tbtc4';
108-
case 'bitcoinBitGoSignet':
109-
return 'tbtcbgsig';
110-
case 'bitcoin':
111-
case 'testnet':
112-
case 'bitcoincash':
113-
case 'bitcoincashTestnet':
114-
case 'ecash':
115-
case 'ecashTest':
116-
case 'bitcoingold':
117-
case 'bitcoingoldTestnet':
118-
case 'bitcoinsv':
119-
case 'bitcoinsvTestnet':
120-
case 'dash':
121-
case 'dashTest':
122-
case 'dogecoin':
123-
case 'dogecoinTest':
124-
case 'litecoin':
125-
case 'litecoinTest':
126-
case 'zcash':
127-
case 'zcashTest':
128-
const mainnetName = getFamilyFromNetwork(n);
129-
return utxolib.isTestnet(n) ? `t${mainnetName}` : mainnetName;
130-
}
131-
}
132-
133-
/**
134-
* @deprecated - will be removed when we drop support for utxolib
135-
* @param coinName - the name of the coin (e.g. 'btc', 'bch', 'ltc'). Also called 'chain' in some contexts.
136-
* @returns the network for a coin. This is the mainnet network for the coin.
137-
*/
138-
export function getNetworkFromCoinName(coinName: string): utxolib.Network {
139-
for (const network of utxolib.getNetworkList()) {
140-
if (getCoinName(network) === coinName) {
141-
return network;
142-
}
143-
}
144-
throw new Error(`Unknown coin name ${coinName}`);
145-
}
146-
147-
/** @deprecated - use getNetworkFromCoinName instead */
148-
export const getNetworkFromChain = getNetworkFromCoinName;
149-
15047
function getBaseNameFromMainnet(coinName: UtxoCoinNameMainnet): string {
15148
switch (coinName) {
15249
case 'btc':
@@ -189,11 +86,6 @@ export function getFullNameFromCoinName(coinName: UtxoCoinName): string {
18986
return prefix + getBaseNameFromMainnet(getMainnetCoinName(coinName));
19087
}
19188

192-
/** @deprecated - use getFullNameFromCoinName instead */
193-
export function getFullNameFromNetwork(n: utxolib.Network): string {
194-
return getFullNameFromCoinName(getCoinName(n));
195-
}
196-
19789
export function isTestnetCoin(coinName: UtxoCoinName): boolean {
19890
return isUtxoCoinNameTestnet(coinName);
19991
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
encryptKeychain,
1212
getDefaultWalletKeys,
1313
getMinUtxoCoins,
14+
getNetworkForCoinName,
1415
getUtxoWallet,
1516
keychainsBase58,
1617
getScriptTypes,
@@ -46,12 +47,17 @@ describe('prebuildAndSign-returnLegacyFormat', function () {
4647
const outputAmount = BigInt(inputScripts.length) * BigInt(1e8) - fee;
4748
const outputScriptType: utxolib.bitgo.outputScripts.ScriptType = 'p2sh';
4849
const outputChain = utxolib.bitgo.getExternalChainCode(outputScriptType);
49-
const outputAddress = utxolib.bitgo.getWalletAddress(rootWalletKeys, outputChain, 0, coin.network);
50+
const outputAddress = utxolib.bitgo.getWalletAddress(
51+
rootWalletKeys,
52+
outputChain,
53+
0,
54+
getNetworkForCoinName(coin.name)
55+
);
5056
recipient = { address: outputAddress, amount: outputAmount.toString() };
5157
prebuild = utxolib.testutil.constructPsbt(
5258
inputScripts.map((s) => ({ scriptType: s, value: BigInt(1e8) })),
5359
[{ scriptType: outputScriptType, value: outputAmount }],
54-
coin.network,
60+
getNetworkForCoinName(coin.name),
5561
rootWalletKeys,
5662
'unsigned'
5763
);

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import nock = require('nock');
33
import * as sinon from 'sinon';
44
import { CustomSigningFunction, common } from '@bitgo/sdk-core';
55

6-
import { defaultBitGo, getDefaultWalletKeys, getUtxoCoin, getUtxoWallet, assertHasProperty } from './util';
6+
import {
7+
defaultBitGo,
8+
getDefaultWalletKeys,
9+
getNetworkForCoinName,
10+
getUtxoCoin,
11+
getUtxoWallet,
12+
assertHasProperty,
13+
} from './util';
714

815
nock.disableNetConnect();
916

@@ -56,7 +63,7 @@ describe('UTXO Custom Signer Function', function () {
5663
const psbt = utxoLib.testutil.constructPsbt(
5764
[{ scriptType: 'taprootKeyPathSpend', value: BigInt(1000) }],
5865
[{ scriptType: 'p2sh', value: BigInt(900) }],
59-
basecoin.network,
66+
getNetworkForCoinName(basecoin.name),
6067
rootWalletKey,
6168
'unsigned'
6269
);
@@ -72,7 +79,7 @@ describe('UTXO Custom Signer Function', function () {
7279
const psbt = utxoLib.testutil.constructPsbt(
7380
[{ scriptType: 'p2wsh', value: BigInt(1000) }],
7481
[{ scriptType: 'p2sh', value: BigInt(900) }],
75-
basecoin.network,
82+
getNetworkForCoinName(basecoin.name),
7683
rootWalletKey,
7784
'unsigned'
7885
);

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
defaultBitGo,
1313
getDefaultWalletKeys,
1414
getMinUtxoCoins,
15+
getNetworkForCoinName,
1516
getUtxoWallet,
1617
keychainsBase58,
1718
getScriptTypes,
@@ -62,7 +63,7 @@ function run(coin: AbstractUtxoCoin, inputScripts: ScriptType[]): void {
6263
const psbt = utxolib.testutil.constructPsbt(
6364
inputs as utxolib.testutil.Input[],
6465
outputs,
65-
coin.network,
66+
getNetworkForCoinName(coin.name),
6667
rootWalletKeys,
6768
'unsigned',
6869
{
@@ -178,7 +179,12 @@ function run(coin: AbstractUtxoCoin, inputScripts: ScriptType[]): void {
178179
const outputAmount = BigInt(inputScripts.length) * BigInt(1e8) - fee;
179180
const outputScriptType: utxolib.bitgo.outputScripts.ScriptType = 'p2sh';
180181
const outputChain = utxolib.bitgo.getExternalChainCode(outputScriptType);
181-
const outputAddress = utxolib.bitgo.getWalletAddress(rootWalletKeys, outputChain, 0, coin.network);
182+
const outputAddress = utxolib.bitgo.getWalletAddress(
183+
rootWalletKeys,
184+
outputChain,
185+
0,
186+
getNetworkForCoinName(coin.name)
187+
);
182188

183189
recipient = {
184190
address: outputAddress,
@@ -222,7 +228,7 @@ function run(coin: AbstractUtxoCoin, inputScripts: ScriptType[]): void {
222228

223229
nocks.forEach((nock) => assert.ok(nock.isDone()));
224230

225-
assertSignable(res.txHex, inputScripts, coin.network);
231+
assertSignable(res.txHex, inputScripts, getNetworkForCoinName(coin.name));
226232
});
227233

228234
[true, false].forEach((selfSend) => {
@@ -254,7 +260,7 @@ function run(coin: AbstractUtxoCoin, inputScripts: ScriptType[]): void {
254260

255261
nocks.forEach((nock) => assert.ok(nock.isDone()));
256262

257-
assertSignable(res.txHex, inputScripts, coin.network);
263+
assertSignable(res.txHex, inputScripts, getNetworkForCoinName(coin.name));
258264
});
259265
});
260266
});

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ import { common, Triple } from '@bitgo/sdk-core';
99
import { getReplayProtectionPubkeys, ErrorDeprecatedTxFormat } from '../../src';
1010
import type { Unspent } from '../../src/unspent';
1111

12-
import { getUtxoWallet, getDefaultWalletKeys, getUtxoCoin, keychainsBase58, defaultBitGo } from './util';
12+
import {
13+
getUtxoWallet,
14+
getDefaultWalletKeys,
15+
getNetworkForCoinName,
16+
getUtxoCoin,
17+
keychainsBase58,
18+
defaultBitGo,
19+
} from './util';
1320

1421
describe('signTransaction', function () {
1522
const bgUrl = common.Environments[defaultBitGo.getEnv()].uri;
@@ -21,7 +28,7 @@ describe('signTransaction', function () {
2128
const pubs = keychainsBase58.map((v) => v.pub) as Triple<string>;
2229

2330
function validatePsbt(txHex: string, targetSigCount: 0 | 1, targetNonceCount?: 1 | 2) {
24-
const psbt = utxolib.bitgo.createPsbtFromHex(txHex, coin.network);
31+
const psbt = utxolib.bitgo.createPsbtFromHex(txHex, getNetworkForCoinName(coin.name));
2532
psbt.data.inputs.forEach((input, index) => {
2633
const parsed = utxolib.bitgo.parsePsbtInput(input);
2734
if (parsed.scriptType === 'taprootKeyPathSpend') {
@@ -38,7 +45,7 @@ describe('signTransaction', function () {
3845
}
3946

4047
function validateTx(txHex: string, unspents: Unspent<bigint>[], targetSigCount: 0 | 1) {
41-
const tx = utxolib.bitgo.createTransactionFromHex(txHex, coin.network);
48+
const tx = utxolib.bitgo.createTransactionFromHex(txHex, getNetworkForCoinName(coin.name));
4249
unspents.forEach((u, i) => {
4350
const sigCount = utxolib.bitgo.getStrictSignatureCount(tx.ins[i]);
4451
const expectedSigCount = utxolib.bitgo.isWalletUnspent(u) && !!targetSigCount ? 1 : 0;
@@ -56,7 +63,7 @@ describe('signTransaction', function () {
5663
const txHex = tx.toHex();
5764

5865
function nockSignPsbt(psbtHex: string): nock.Scope {
59-
const psbt = utxolib.bitgo.createPsbtFromHex(psbtHex, coin.network);
66+
const psbt = utxolib.bitgo.createPsbtFromHex(psbtHex, getNetworkForCoinName(coin.name));
6067
return nock(bgUrl)
6168
.post(`/api/v2/${wallet.coin()}/wallet/${wallet.id()}/tx/signpsbt`, (body) => body.psbt)
6269
.reply(200, { psbt: psbt.setAllInputsMusig2NonceHD(rootWalletKeys.bitgo).toHex() });
@@ -147,7 +154,7 @@ describe('signTransaction', function () {
147154
.map((scriptType) => ({ scriptType, value: BigInt(1000) }));
148155
const unspentSum = inputs.reduce((prev: bigint, curr) => prev + curr.value, BigInt(0));
149156
const outputs: testutil.Output[] = [{ scriptType: 'p2sh', value: unspentSum - BigInt(1000) }];
150-
const psbt = testutil.constructPsbt(inputs, outputs, coin.network, rootWalletKeys, 'unsigned', {
157+
const psbt = testutil.constructPsbt(inputs, outputs, getNetworkForCoinName(coin.name), rootWalletKeys, 'unsigned', {
151158
p2shP2pkKey: replayProtectionKey,
152159
});
153160

@@ -163,7 +170,7 @@ describe('signTransaction', function () {
163170
.map((scriptType) => ({ scriptType, value: BigInt(1000) }));
164171
const unspentSum = inputs.reduce((prev: bigint, cur) => prev + cur.value, BigInt(0));
165172
const outputs: testutil.Output[] = [{ scriptType: 'p2sh', value: unspentSum - BigInt(1000) }];
166-
const psbt = testutil.constructPsbt(inputs, outputs, coin.network, rootWalletKeys, 'unsigned', {
173+
const psbt = testutil.constructPsbt(inputs, outputs, getNetworkForCoinName(coin.name), rootWalletKeys, 'unsigned', {
167174
p2shP2pkKey: replayProtectionKey,
168175
});
169176

@@ -181,8 +188,16 @@ describe('signTransaction', function () {
181188
}));
182189
const unspentSum = inputs.reduce((prev: bigint, curr) => prev + curr.value, BigInt(0));
183190
const outputs: testutil.TxnOutput<bigint>[] = [{ scriptType: 'p2sh', value: unspentSum - BigInt(1000) }];
184-
const txBuilder = testutil.constructTxnBuilder(inputs, outputs, coin.network, rootWalletKeys, 'unsigned');
185-
const unspents = inputs.map((v, i) => testutil.toTxnUnspent(v, i, coin.network, rootWalletKeys));
191+
const txBuilder = testutil.constructTxnBuilder(
192+
inputs,
193+
outputs,
194+
getNetworkForCoinName(coin.name),
195+
rootWalletKeys,
196+
'unsigned'
197+
);
198+
const unspents = inputs.map((v, i) =>
199+
testutil.toTxnUnspent(v, i, getNetworkForCoinName(coin.name), rootWalletKeys)
200+
);
186201

187202
// Legacy format transactions are now deprecated and should throw ErrorDeprecatedTxFormat
188203
await assert.rejects(async () => {
@@ -194,7 +209,7 @@ describe('signTransaction', function () {
194209
const inputs: testutil.Input[] = [{ scriptType: 'taprootKeyPathSpend', value: BigInt(1000) }];
195210
const unspentSum = inputs.reduce((prev: bigint, curr) => prev + curr.value, BigInt(0));
196211
const outputs: testutil.Output[] = [{ scriptType: 'p2sh', value: unspentSum - BigInt(1000) }];
197-
const psbt = testutil.constructPsbt(inputs, outputs, coin.network, rootWalletKeys, 'unsigned');
212+
const psbt = testutil.constructPsbt(inputs, outputs, getNetworkForCoinName(coin.name), rootWalletKeys, 'unsigned');
198213

199214
await assert.rejects(
200215
async () => {

0 commit comments

Comments
 (0)