Skip to content

Commit e8a1166

Browse files
OttoAllmendingerllm-git
andcommitted
feat(abstract-utxo): move utxo test files to abstract-utxo module
This makes the development cycle more pleasant because we don't have to go to some other module to test the `abstract-utxo` implementation. Replaced imports of `BitGo` which pull in the full cursed deptree with imports of the more modern TestBitGo which has a much lighter deptree. Issue: BTC-2652 Co-authored-by: llm-git <[email protected]>
1 parent ead5231 commit e8a1166

File tree

325 files changed

+554
-576
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

325 files changed

+554
-576
lines changed

modules/bitgo/test/v2/unit/coins/abstractUtxoCoin.ts renamed to modules/abstract-utxo/test/unit/abstractUtxoCoin.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import * as utxolib from '@bitgo/utxo-lib';
22
import should = require('should');
33
import * as sinon from 'sinon';
44
import { Wallet, UnexpectedAddressError, VerificationOptions, Triple } from '@bitgo/sdk-core';
5-
import { TestBitGo } from '@bitgo/sdk-test';
6-
import { BitGo } from '../../../../src/bitgo';
7-
import { psbtTxHex } from './payGoPSBTHexFixture/psbtHexProof';
8-
import { bip322Fixtures } from '../../fixtures/bip322/fixtures';
9-
import { AbstractUtxoCoin, UtxoWallet, Output, TransactionExplanation, TransactionParams } from '@bitgo/abstract-utxo';
5+
6+
import { UtxoWallet, Output, TransactionExplanation, TransactionParams } from '../../src';
7+
8+
import { bip322Fixtures } from './fixtures/bip322/fixtures';
9+
import { psbtTxHex } from './fixtures/psbtHexProof';
10+
import { defaultBitGo, getUtxoCoin } from './util';
1011

1112
describe('Abstract UTXO Coin:', () => {
1213
describe('Parse Transaction:', () => {
13-
const bitgo: BitGo = TestBitGo.decorate(BitGo, { env: 'mock' });
14-
const coin = bitgo.coin('tbtc') as AbstractUtxoCoin;
14+
const coin = getUtxoCoin('tbtc');
1515

1616
/*
1717
* mock objects which get passed into parse transaction.
@@ -117,8 +117,7 @@ describe('Abstract UTXO Coin:', () => {
117117
});
118118

119119
describe('Custom Change Wallets', () => {
120-
const bitgo: BitGo = TestBitGo.decorate(BitGo, { env: 'mock' });
121-
const coin = bitgo.coin('tbtc') as AbstractUtxoCoin;
120+
const coin = getUtxoCoin('tbtc');
122121

123122
const keys = {
124123
send: {
@@ -249,8 +248,7 @@ describe('Abstract UTXO Coin:', () => {
249248
});
250249

251250
describe('Verify Transaction', () => {
252-
const bitgo: BitGo = TestBitGo.decorate(BitGo, { env: 'mock' });
253-
const coin = bitgo.coin('tbtc') as AbstractUtxoCoin;
251+
const coin = getUtxoCoin('tbtc');
254252

255253
const userKeychain = coin.keychains().create();
256254
const otherKeychain = coin.keychains().create();
@@ -277,7 +275,7 @@ describe('Abstract UTXO Coin:', () => {
277275
// user public key swapped out
278276
user: {
279277
pub: otherKeychain.pub,
280-
encryptedPrv: bitgo.encrypt({
278+
encryptedPrv: defaultBitGo.encrypt({
281279
input: userKeychain.prv,
282280
password: passphrase,
283281
}),
@@ -547,7 +545,7 @@ describe('Abstract UTXO Coin:', () => {
547545

548546
it('should work with bigint amounts', async () => {
549547
// need a coin that uses bigint
550-
const bigintCoin = bitgo.coin('tdoge') as AbstractUtxoCoin;
548+
const bigintCoin = getUtxoCoin('tdoge');
551549

552550
const coinMock = sinon.stub(bigintCoin, 'parseTransaction').resolves({
553551
keychains: {} as any,
@@ -596,12 +594,7 @@ describe('Abstract UTXO Coin:', () => {
596594

597595
describe('Explain Transaction', function () {
598596
describe('Verify paygo output when explaining psbt transaction', function () {
599-
const bitgo: BitGo = TestBitGo.decorate(BitGo, { env: 'mock' });
600-
let coin: AbstractUtxoCoin;
601-
602-
beforeEach(() => {
603-
coin = bitgo.coin('tbtc4') as AbstractUtxoCoin;
604-
});
597+
const coin = getUtxoCoin('tbtc4');
605598

606599
it('should detect and verify paygo address proof in PSBT', async function () {
607600
// Call explainTransaction
@@ -610,14 +603,9 @@ describe('Abstract UTXO Coin:', () => {
610603
});
611604

612605
describe('BIP322 Proof', function () {
613-
const bitgo: BitGo = TestBitGo.decorate(BitGo, { env: 'mock' });
614-
let coin: AbstractUtxoCoin;
606+
const coin = getUtxoCoin('btc');
615607
const pubs = bip322Fixtures.valid.rootWalletKeys.triple.map((b) => b.neutered().toBase58()) as Triple<string>;
616608

617-
beforeEach(() => {
618-
coin = bitgo.coin('btc') as AbstractUtxoCoin;
619-
});
620-
621609
it('should successfully run with a user nonce', async function () {
622610
const psbtHex = bip322Fixtures.valid.userNonce;
623611
const result = await coin.explainTransaction({ txHex: psbtHex, pubs });

modules/bitgo/test/v2/unit/coins/utxo/address.ts renamed to modules/abstract-utxo/test/unit/address.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
/**
2-
* @prettier
3-
*/
41
import 'should';
52
import * as assert from 'assert';
3+
64
import * as utxolib from '@bitgo/utxo-lib';
75
const { chainCodes } = utxolib.bitgo;
86

9-
import { AbstractUtxoCoin, GenerateFixedScriptAddressOptions } from '@bitgo/abstract-utxo';
7+
import { AbstractUtxoCoin, GenerateFixedScriptAddressOptions } from '../../src';
108

119
import { utxoCoins, keychains as keychainsBip32, getFixture, shouldEqualJSON } from './util';
1210

modules/bitgo/test/v2/unit/coins/utxo/coins.ts renamed to modules/abstract-utxo/test/unit/coins.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as assert from 'assert';
2+
23
import * as utxolib from '@bitgo/utxo-lib';
34

45
import { getUtxoCoinForNetwork, utxoCoins } from './util';
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import * as utxoLib from '@bitgo/utxo-lib';
2+
import 'should';
3+
import 'should-sinon';
4+
import nock = require('nock');
5+
import * as sinon from 'sinon';
6+
import { CustomSigningFunction, common } from '@bitgo/sdk-core';
7+
8+
import { defaultBitGo, getDefaultWalletKeys, getUtxoCoin, getUtxoWallet } from './util';
9+
10+
nock.disableNetConnect();
11+
12+
const bgUrl = common.Environments[defaultBitGo.getEnv()].uri;
13+
const basecoin = getUtxoCoin('tbtc');
14+
const wallet = getUtxoWallet(basecoin, {
15+
id: '5b34252f1bf349930e34020a',
16+
coin: 'tbtc',
17+
keys: ['5b3424f91bf349930e340175', '5b3424f91bf349930e340176', '5b3424f91bf349930e340177'],
18+
});
19+
20+
describe('UTXO Custom Signer Function', function () {
21+
const recipients = [
22+
{ address: 'abc', amount: 123 },
23+
{ address: 'def', amount: 456 },
24+
];
25+
const rootWalletKey = getDefaultWalletKeys();
26+
let customSigningFunction: CustomSigningFunction;
27+
let stubs: sinon.SinonStub[];
28+
29+
beforeEach(function () {
30+
customSigningFunction = sinon.stub().returns({
31+
txHex: 'this-is-a-tx',
32+
});
33+
stubs = [
34+
sinon.stub(wallet.baseCoin, 'postProcessPrebuild').returnsArg(0),
35+
sinon.stub(wallet.baseCoin, 'verifyTransaction').resolves(true),
36+
sinon.stub(wallet.baseCoin, 'signTransaction').resolves({ txHex: 'this-is-a-tx' }),
37+
];
38+
});
39+
40+
afterEach(function () {
41+
stubs.forEach((s) => s.restore());
42+
nock.cleanAll();
43+
});
44+
45+
function nocks(txPrebuild: { txHex: string }) {
46+
nock(bgUrl)
47+
.post(`/api/v2/${wallet.coin()}/wallet/${wallet.id()}/tx/build`)
48+
.reply(200, { ...txPrebuild, txInfo: {} });
49+
nock(bgUrl).get(`/api/v2/${wallet.coin()}/public/block/latest`).reply(200, { height: 1000 });
50+
nock(bgUrl).persist().get(`/api/v2/${wallet.coin()}/key/${wallet.keyIds()[0]}`).reply(200, { pub: 'pub' });
51+
nock(bgUrl).persist().get(`/api/v2/${wallet.coin()}/key/${wallet.keyIds()[1]}`).reply(200, { pub: 'pub' });
52+
nock(bgUrl).persist().get(`/api/v2/${wallet.coin()}/key/${wallet.keyIds()[2]}`).reply(200, { pub: 'pub' });
53+
return nock(bgUrl).post(`/api/v2/${wallet.coin()}/wallet/${wallet.id()}/tx/send`).reply(200, { ok: true });
54+
}
55+
56+
it('should use a custom signing function if provided for PSBT with taprootKeyPathSpend input', async function () {
57+
const psbt = utxoLib.testutil.constructPsbt(
58+
[{ scriptType: 'taprootKeyPathSpend', value: BigInt(1000) }],
59+
[{ scriptType: 'p2sh', value: BigInt(900) }],
60+
basecoin.network,
61+
rootWalletKey,
62+
'unsigned'
63+
);
64+
const scope = nocks({ txHex: psbt.toHex() });
65+
const result = await wallet.sendMany({ recipients, customSigningFunction });
66+
67+
result.should.have.property('ok', true);
68+
customSigningFunction.should.have.been.calledTwice();
69+
scope.done();
70+
});
71+
72+
it('should use a custom signing function if provided for PSBT without taprootKeyPathSpend input', async function () {
73+
const psbt = utxoLib.testutil.constructPsbt(
74+
[{ scriptType: 'p2wsh', value: BigInt(1000) }],
75+
[{ scriptType: 'p2sh', value: BigInt(900) }],
76+
basecoin.network,
77+
rootWalletKey,
78+
'unsigned'
79+
);
80+
const scope = nocks({ txHex: psbt.toHex() });
81+
const result = await wallet.sendMany({ recipients, customSigningFunction });
82+
83+
result.should.have.property('ok', true);
84+
customSigningFunction.should.have.been.calledOnce();
85+
scope.done();
86+
});
87+
88+
it('should use a custom signing function if provided for Tx without taprootKeyPathSpend input', async function () {
89+
const tx = utxoLib.testutil.constructTxnBuilder(
90+
[{ scriptType: 'p2wsh', value: BigInt(1000) }],
91+
[{ scriptType: 'p2sh', value: BigInt(900) }],
92+
basecoin.network,
93+
rootWalletKey,
94+
'unsigned'
95+
);
96+
const scope = nocks({ txHex: tx.buildIncomplete().toHex() });
97+
const result = await wallet.sendMany({ recipients, customSigningFunction });
98+
99+
result.should.have.property('ok', true);
100+
customSigningFunction.should.have.been.calledOnce();
101+
scope.done();
102+
});
103+
});

modules/bitgo/test/v2/unit/coins/utxo/descriptorAddress.ts renamed to modules/abstract-utxo/test/unit/descriptorAddress.ts

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

3-
import { TestBitGo } from '@bitgo/sdk-test';
4-
import { AbstractUtxoCoin, descriptor as utxod } from '@bitgo/abstract-utxo';
53
import * as utxolib from '@bitgo/utxo-lib';
64
import { IWallet, WalletCoinSpecific } from '@bitgo/sdk-core';
75

8-
import { BitGo } from '../../../../../src';
6+
import { descriptor as utxod } from '../../src';
7+
8+
import { getUtxoCoin } from './util';
99

1010
export function getDescriptorAddress(d: string, index: number, network: utxolib.Network): string {
1111
const derivedScript = Buffer.from(
@@ -15,8 +15,7 @@ export function getDescriptorAddress(d: string, index: number, network: utxolib.
1515
}
1616

1717
describe('descriptor wallets', function () {
18-
const bitgo: BitGo = TestBitGo.decorate(BitGo, { env: 'mock' });
19-
const coin = bitgo.coin('tbtc') as AbstractUtxoCoin;
18+
const coin = getUtxoCoin('tbtc');
2019
const xpubs = utxolib.testutil.getKeyTriple('setec astronomy').map((k) => k.neutered().toBase58());
2120

2221
function withChecksum(descriptor: string): string {

0 commit comments

Comments
 (0)