Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions modules/abstract-utxo/test/unit/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import * as assert from 'assert';

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

import { getMainnetCoinName, utxoCoinsMainnet, utxoCoinsTestnet } from '../../src/names';
import { getMainnetCoinName, getNetworkFromCoinName, utxoCoinsMainnet, utxoCoinsTestnet } from '../../src/names';

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

describe('utxoCoins', function () {
it('has expected chain/network values for items', function () {
assert.deepStrictEqual(
utxoCoins.map((c) => [c.getChain(), c.getFamily(), c.getFullName(), utxolib.getNetworkName(c.network)]),
utxoCoins.map((c) => [
c.getChain(),
c.getFamily(),
c.getFullName(),
utxolib.getNetworkName(getNetworkFromCoinName(c.name)),
]),
[
['btc', 'btc', 'Bitcoin', 'bitcoin'],
['tbtc', 'btc', 'Testnet Bitcoin', 'testnet'],
Expand Down
15 changes: 7 additions & 8 deletions modules/abstract-utxo/test/unit/descriptorAddress.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import * as assert from 'node:assert';

import * as utxolib from '@bitgo/utxo-lib';
import { address as wasmAddress, CoinName } from '@bitgo/wasm-utxo';
import { IWallet, WalletCoinSpecific } from '@bitgo/sdk-core';

import { descriptor as utxod } from '../../src';

import { getUtxoCoin } from './util';

export function getDescriptorAddress(d: string, index: number, network: utxolib.Network): string {
const derivedScript = Buffer.from(
utxod.Descriptor.fromString(d, 'derivable').atDerivationIndex(index).scriptPubkey()
);
return utxolib.address.fromOutputScript(derivedScript, network);
export function getDescriptorAddress(d: string, index: number, coinName: CoinName): string {
const derivedScript = utxod.Descriptor.fromString(d, 'derivable').atDerivationIndex(index).scriptPubkey();
return wasmAddress.fromOutputScriptWithCoin(derivedScript, coinName);
}

describe('descriptor wallets', function () {
Expand Down Expand Up @@ -40,9 +39,9 @@ describe('descriptor wallets', function () {

const descFoo = getNamedDescriptor2Of2('foo', xpubs[0], xpubs[1]);
const descBar = getNamedDescriptor2Of2('bar', xpubs[1], xpubs[0]);
const addressFoo0 = getDescriptorAddress(descFoo.value, 0, coin.network);
const addressFoo1 = getDescriptorAddress(descFoo.value, 1, coin.network);
const addressBar0 = getDescriptorAddress(descBar.value, 0, coin.network);
const addressFoo0 = getDescriptorAddress(descFoo.value, 0, coin.name);
const addressFoo1 = getDescriptorAddress(descFoo.value, 1, coin.name);
const addressBar0 = getDescriptorAddress(descBar.value, 0, coin.name);

it('has expected values', function () {
assert.deepStrictEqual(
Expand Down
3 changes: 2 additions & 1 deletion modules/abstract-utxo/test/unit/prebuildAndSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { common, HalfSignedUtxoTransaction, Wallet } from '@bitgo/sdk-core';
import { getSeed } from '@bitgo/sdk-test';

import { AbstractUtxoCoin, getReplayProtectionAddresses } from '../../src';
import { getMainnetCoinName } from '../../src/names';

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

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

utxoCoins
.filter((coin) => utxolib.getMainnet(coin.network) !== utxolib.networks.bitcoinsv)
.filter((coin) => getMainnetCoinName(coin.name) !== 'bsv')
.forEach((coin) => {
scriptTypes
// Don't iterate over p2shP2pk - in no scenario would a wallet spend two p2shP2pk inputs as these
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BIP32Interface } from '@bitgo/utxo-lib';
import * as utxolib from '@bitgo/utxo-lib';
import { Config, krsProviders, Triple } from '@bitgo/sdk-core';
import { Dimensions } from '@bitgo/unspents';
import { fixedScriptWallet } from '@bitgo/wasm-utxo';
import { address as wasmAddress, fixedScriptWallet } from '@bitgo/wasm-utxo';

import {
AbstractUtxoCoin,
Expand All @@ -17,6 +17,7 @@ import {
CoingeckoApi,
FormattedOfflineVaultTxInfo,
} from '../../../src';
import { getCoinName } from '../../../src/names';
import {
defaultBitGo,
encryptKeychain,
Expand Down Expand Up @@ -319,7 +320,9 @@ function run(
it((params.hasKrsOutput ? 'has' : 'has no') + ' key recovery service output', function () {
const outs = recoveryTx instanceof utxolib.bitgo.UtxoPsbt ? recoveryTx.getUnsignedTx().outs : recoveryTx.outs;
outs.length.should.eql(1);
const outputAddresses = outs.map((o) => utxolib.address.fromOutputScript(o.script, recoveryTx.network));
const outputAddresses = outs.map((o) =>
wasmAddress.fromOutputScriptWithCoin(o.script, getCoinName(recoveryTx.network))
);
outputAddresses
.includes(keyRecoveryServiceAddress)
.should.eql(!!params.hasKrsOutput && params.krsProvider === 'keyternal');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
generateAddress,
convertLtcAddressToLegacyFormat,
} from '../../../src';
import { isMainnetCoin, isTestnetCoin } from '../../../src/names';
import {
getFixture,
keychainsBase58,
Expand Down Expand Up @@ -265,8 +266,8 @@ utxoCoins.forEach((coin) => {
(otherCoin) =>
coin !== otherCoin &&
isSupportedCrossChainRecovery(coin, otherCoin) &&
((utxolib.isMainnet(coin.network) && utxolib.isMainnet(otherCoin.network)) ||
(utxolib.isTestnet(coin.network) && utxolib.isTestnet(otherCoin.network)))
((isMainnetCoin(coin.name) && isMainnetCoin(otherCoin.name)) ||
(isTestnetCoin(coin.name) && isTestnetCoin(otherCoin.name)))
)
.forEach((otherCoin) => {
if (coin.amountType === 'bigint') {
Expand Down
7 changes: 4 additions & 3 deletions modules/abstract-utxo/test/unit/recovery/mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { bitgo } from '@bitgo/utxo-lib';
import { AddressInfo, TransactionIO } from '@bitgo/blockapis';
import * as utxolib from '@bitgo/utxo-lib';
import { address as wasmAddress, AddressFormat } from '@bitgo/wasm-utxo';

import { AbstractUtxoCoin, RecoveryProvider } from '../../../src';
import { Bch } from '../../../src/impl/bch';
Expand Down Expand Up @@ -51,7 +52,7 @@ export class MockRecoveryProvider implements RecoveryProvider {
}
export class MockCrossChainRecoveryProvider<TNumber extends number | bigint> implements RecoveryProvider {
private addressVersion: 'cashaddr' | 'base58';
private addressFormat: utxolib.addressFormat.AddressFormat;
private addressFormat: AddressFormat;
constructor(
public coin: AbstractUtxoCoin,
public unspents: Unspent<TNumber>[],
Expand All @@ -65,7 +66,7 @@ export class MockCrossChainRecoveryProvider<TNumber extends number | bigint> imp

async getUnspentsForAddresses(addresses: string[]): Promise<Unspent[]> {
return this.tx.outs.map((o, vout: number) => {
let address = utxolib.addressFormat.fromOutputScriptWithFormat(o.script, this.addressFormat, this.coin.network);
let address = wasmAddress.fromOutputScriptWithCoin(o.script, this.coin.name, this.addressFormat);
if (address.includes(':')) {
[, address] = address.split(':');
}
Expand All @@ -91,7 +92,7 @@ export class MockCrossChainRecoveryProvider<TNumber extends number | bigint> imp
};
}),
outputs: this.tx.outs.map((o) => {
let address = utxolib.addressFormat.fromOutputScriptWithFormat(o.script, this.addressFormat, this.coin.network);
let address = wasmAddress.fromOutputScriptWithCoin(o.script, this.coin.name, this.addressFormat);
if (address.includes(':')) {
[, address] = address.split(':');
}
Expand Down
3 changes: 2 additions & 1 deletion modules/abstract-utxo/test/unit/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as _ from 'lodash';
import * as utxolib from '@bitgo/utxo-lib';
import nock = require('nock');
import { BIP32Interface, bitgo, testutil } from '@bitgo/utxo-lib';
import { address as wasmAddress } from '@bitgo/wasm-utxo';
import {
common,
FullySignedTransaction,
Expand Down Expand Up @@ -507,7 +508,7 @@ function run<TNumber extends number | bigint = number>(
: getUnspents();
const prevOutputs = unspents.map(
(u): utxolib.TxOutput<TNumber> => ({
script: utxolib.address.toOutputScript(u.address, coin.network),
script: Buffer.from(wasmAddress.toOutputScriptWithCoin(u.address, coin.name)),
value: u.value,
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
import * as sinon from 'sinon';
import * as utxolib from '@bitgo/utxo-lib';
import { Wallet, VerificationOptions, ITransactionRecipient, Triple } from '@bitgo/sdk-core';
import { fixedScriptWallet } from '@bitgo/wasm-utxo';
import { address as wasmAddress, fixedScriptWallet } from '@bitgo/wasm-utxo';

import { parseTransaction } from '../../../../src/transaction/fixedScript/parseTransaction';
import { ParsedTransaction } from '../../../../src/transaction/types';
Expand Down Expand Up @@ -63,7 +63,7 @@ function getChangeInfoFromPsbt(psbt: utxolib.bitgo.UtxoPsbt): ChangeAddressInfo[
const path = derivations[0].path;
const { chain, index } = utxolib.bitgo.getChainAndIndexFromPath(path);
return {
address: utxolib.address.fromOutputScript(psbt.txOutputs[i].script, psbt.network),
address: wasmAddress.fromOutputScriptWithCoin(psbt.txOutputs[i].script, getCoinName(psbt.network)),
chain,
index,
};
Expand Down
17 changes: 8 additions & 9 deletions modules/abstract-utxo/test/unit/txFormat.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 { getMainnetCoinName, isMainnetCoin, isTestnetCoin } from '../../src/names';

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

Expand Down Expand Up @@ -106,39 +106,38 @@ describe('txFormat', function () {
// All testnet wallets default to PSBT-lite
runTest({
description: 'should always return psbt-lite for testnet',
coinFilter: (coin) => utxolib.isTestnet(coin.network),
coinFilter: (coin) => isTestnetCoin(coin.name),
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),
coinFilter: (coin) => isMainnetCoin(coin.name),
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),
coinFilter: (coin) => isMainnetCoin(coin.name),
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,
coinFilter: (coin) => isMainnetCoin(coin.name) && getMainnetCoinName(coin.name) === 'btc',
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),
coinFilter: (coin) => isMainnetCoin(coin.name),
walletFilter: (w) => {
const isHotBitcoin = w.options.type === 'hot'; // This will be bitcoin hot wallets
const isDistributedCustody = w.options.subType === 'distributedCustody';
Expand All @@ -152,7 +151,7 @@ describe('txFormat', function () {
// Test explicitly requested formats
runTest({
description: 'should respect explicitly requested legacy format on mainnet',
coinFilter: (coin) => utxolib.isMainnet(coin.network),
coinFilter: (coin) => isMainnetCoin(coin.name),
expectedTxFormat: 'legacy',
requestedTxFormat: 'legacy',
});
Expand All @@ -172,7 +171,7 @@ describe('txFormat', function () {
// 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)) {
if (!isTestnetCoin(coin.name)) {
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions modules/abstract-utxo/test/unit/util/nockIndexerAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import nock = require('nock');
import * as utxolib from '@bitgo/utxo-lib';
import { address as wasmAddress } from '@bitgo/wasm-utxo';

import { AbstractUtxoCoin } from '../../../src';

Expand All @@ -19,7 +20,7 @@ export function nockBitGoPublicTransaction<TNumber extends number | bigint = num
): nock.Scope {
const payload = {
input: unspents.map((u) => ({ address: u.address })),
outputs: tx.outs.map((o) => ({ address: utxolib.address.fromOutputScript(o.script, coin.network) })),
outputs: tx.outs.map((o) => ({ address: wasmAddress.fromOutputScriptWithCoin(o.script, coin.name) })),
};
return nockBitGo().get(`/api/v2/${coin.getChain()}/public/tx/${tx.getId()}`).reply(200, payload);
}
Expand All @@ -33,7 +34,7 @@ export function nockBitGoPublicAddressUnspents<TNumber extends number | bigint =
const payload: ImsUnspent[] = outputs.map(
(o, vout: number): ImsUnspent => ({
id: `${txid}:${vout}`,
address: utxolib.address.fromOutputScript(o.script, coin.network),
address: wasmAddress.fromOutputScriptWithCoin(o.script, coin.name),
value: Number(o.value),
valueString: coin.amountType === 'bigint' ? o.value.toString() : undefined,
})
Expand Down
5 changes: 4 additions & 1 deletion modules/abstract-utxo/test/unit/util/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as utxolib from '@bitgo/utxo-lib';
import { address as wasmAddress } from '@bitgo/wasm-utxo';

import { getCoinName } from '../../../src/names';
const { isWalletUnspent, signInputWithUnspent } = utxolib.bitgo;
type RootWalletKeys = utxolib.bitgo.RootWalletKeys;
type Unspent<TNumber extends number | bigint = number> = utxolib.bitgo.Unspent<TNumber>;
Expand All @@ -22,7 +25,7 @@ function toTxOutput<TNumber extends number | bigint = number>(
network: utxolib.Network
): utxolib.TxOutput<TNumber> {
return {
script: utxolib.address.toOutputScript(u.address, network),
script: Buffer.from(wasmAddress.toOutputScriptWithCoin(u.address, getCoinName(network))),
value: u.value,
};
}
Expand Down
5 changes: 4 additions & 1 deletion modules/abstract-utxo/test/unit/util/unspents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export function getWalletAddress(
if (utxolib.isTestnet(network)) {
return wasmUtxo.fixedScriptWallet.address(walletKeys, chain, index, network);
}
return utxolib.address.fromOutputScript(getOutputScript(walletKeys, chain, index).scriptPubKey, network);
return wasmUtxo.address.fromOutputScriptWithCoin(
getOutputScript(walletKeys, chain, index).scriptPubKey,
getCoinName(network)
);
}

function mockOutputIdForAddress(address: string) {
Expand Down