@btc-vision/bitcoin 6.4.0 → 6.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser/address.d.ts +1 -0
- package/browser/index.js +1 -1
- package/browser/payments/index.d.ts +4 -0
- package/browser/payments/p2op.d.ts +2 -0
- package/browser/psbt/psbtutils.d.ts +1 -0
- package/build/address.d.ts +1 -0
- package/build/address.js +56 -8
- package/build/networks.js +6 -6
- package/build/payments/index.d.ts +4 -0
- package/build/payments/index.js +1 -0
- package/build/payments/p2op.d.ts +2 -0
- package/build/payments/p2op.js +108 -0
- package/build/psbt/psbtutils.d.ts +1 -0
- package/build/psbt/psbtutils.js +2 -0
- package/package.json +1 -1
- package/src/address.ts +68 -9
- package/src/block.ts +233 -233
- package/src/bufferutils.ts +188 -188
- package/src/networks.ts +6 -6
- package/src/payments/index.ts +68 -62
- package/src/payments/p2op.ts +136 -0
- package/src/psbt/psbtutils.ts +2 -0
- package/src/psbt.ts +2187 -2187
- package/test/address.spec.ts +155 -155
- package/test/bitcoin.core.spec.ts +212 -212
- package/test/block.spec.ts +171 -171
- package/test/bufferutils.spec.ts +450 -450
- package/test/crypto.spec.ts +49 -49
- package/test/integration/addresses.spec.ts +142 -142
- package/test/integration/bip32.spec.ts +130 -130
- package/test/integration/blocks.spec.ts +28 -28
- package/test/integration/cltv.spec.ts +241 -241
- package/test/integration/csv.spec.ts +452 -452
- package/test/integration/payments.spec.ts +110 -110
- package/test/integration/taproot.spec.ts +663 -663
- package/test/integration/transactions.spec.ts +668 -668
- package/test/payments.spec.ts +114 -114
- package/test/payments.utils.ts +165 -165
- package/test/psbt.spec.ts +1285 -1285
- package/test/script.spec.ts +186 -186
- package/test/script_number.spec.ts +26 -26
- package/test/script_signature.spec.ts +66 -66
- package/test/transaction.spec.ts +337 -337
- package/test/ts-node-register.js +7 -7
- package/test/types.spec.ts +53 -53
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
import { ECPairFactory } from 'ecpair';
|
|
2
|
-
import * as ecc from 'tiny-secp256k1';
|
|
3
|
-
import { describe, it } from 'mocha';
|
|
4
|
-
import * as bitcoin from '../../src/index.js';
|
|
5
|
-
import { regtestUtils } from './_regtest.js';
|
|
6
|
-
import * as fs from 'node:fs';
|
|
7
|
-
|
|
8
|
-
const ECPair = ECPairFactory(ecc);
|
|
9
|
-
const NETWORK = regtestUtils.network;
|
|
10
|
-
const keyPairs = [ECPair.makeRandom({ network: NETWORK }), ECPair.makeRandom({ network: NETWORK })];
|
|
11
|
-
|
|
12
|
-
async function buildAndSign(
|
|
13
|
-
depends: any,
|
|
14
|
-
prevOutput: any,
|
|
15
|
-
redeemScript: any,
|
|
16
|
-
witnessScript: any,
|
|
17
|
-
): Promise<null> {
|
|
18
|
-
const unspent = await regtestUtils.faucetComplex(prevOutput, 5e4);
|
|
19
|
-
const utx = await regtestUtils.fetch(unspent.txId);
|
|
20
|
-
|
|
21
|
-
const psbt = new bitcoin.Psbt({ network: NETWORK })
|
|
22
|
-
.addInput({
|
|
23
|
-
hash: unspent.txId,
|
|
24
|
-
index: unspent.vout,
|
|
25
|
-
nonWitnessUtxo: Buffer.from(utx.txHex, 'hex'),
|
|
26
|
-
...(redeemScript ? { redeemScript } : {}),
|
|
27
|
-
...(witnessScript ? { witnessScript } : {}),
|
|
28
|
-
})
|
|
29
|
-
.addOutput({
|
|
30
|
-
address: regtestUtils.RANDOM_ADDRESS,
|
|
31
|
-
value: 2e4,
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
if (depends.signatures) {
|
|
35
|
-
keyPairs.forEach((keyPair) => {
|
|
36
|
-
psbt.signInput(0, keyPair);
|
|
37
|
-
});
|
|
38
|
-
} else if (depends.signature) {
|
|
39
|
-
psbt.signInput(0, keyPairs[0]);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return regtestUtils.broadcast(psbt.finalizeAllInputs().extractTransaction().toHex());
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
['p2ms', 'p2pk', 'p2pkh', 'p2wpkh'].forEach((k) => {
|
|
46
|
-
const fixtures = JSON.parse(fs.readFileSync('test/fixtures/' + k + '.json', 'utf8'));
|
|
47
|
-
const { depends } = fixtures.dynamic;
|
|
48
|
-
const fn: any = (bitcoin.payments as any)[k];
|
|
49
|
-
|
|
50
|
-
const base: any = {};
|
|
51
|
-
if (depends.pubkey) base.pubkey = keyPairs[0].publicKey;
|
|
52
|
-
if (depends.pubkeys) base.pubkeys = keyPairs.map((x) => x.publicKey);
|
|
53
|
-
if (depends.m) base.m = base.pubkeys.length;
|
|
54
|
-
|
|
55
|
-
const { output } = fn(base);
|
|
56
|
-
if (!output) throw new TypeError('Missing output');
|
|
57
|
-
|
|
58
|
-
describe('bitcoinjs-lib (payments - ' + k + ')', () => {
|
|
59
|
-
it('can broadcast as an output, and be spent as an input', async () => {
|
|
60
|
-
Object.assign(depends, { prevOutScriptType: k });
|
|
61
|
-
await buildAndSign(depends, output, undefined, undefined);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it(
|
|
65
|
-
'can (as P2SH(' + k + ')) broadcast as an output, and be spent as an input',
|
|
66
|
-
async () => {
|
|
67
|
-
const p2sh = bitcoin.payments.p2sh({
|
|
68
|
-
redeem: { output },
|
|
69
|
-
network: NETWORK,
|
|
70
|
-
});
|
|
71
|
-
Object.assign(depends, { prevOutScriptType: 'p2sh-' + k });
|
|
72
|
-
await buildAndSign(depends, p2sh.output, p2sh.redeem!.output, undefined);
|
|
73
|
-
},
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
// NOTE: P2WPKH cannot be wrapped in P2WSH, consensus fail
|
|
77
|
-
if (k === 'p2wpkh') return;
|
|
78
|
-
|
|
79
|
-
it(
|
|
80
|
-
'can (as P2WSH(' + k + ')) broadcast as an output, and be spent as an input',
|
|
81
|
-
async () => {
|
|
82
|
-
const p2wsh = bitcoin.payments.p2wsh({
|
|
83
|
-
redeem: { output },
|
|
84
|
-
network: NETWORK,
|
|
85
|
-
});
|
|
86
|
-
Object.assign(depends, { prevOutScriptType: 'p2wsh-' + k });
|
|
87
|
-
await buildAndSign(depends, p2wsh.output, undefined, p2wsh.redeem!.output);
|
|
88
|
-
},
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
it(
|
|
92
|
-
'can (as P2SH(P2WSH(' + k + '))) broadcast as an output, and be spent as an input',
|
|
93
|
-
async () => {
|
|
94
|
-
const p2wsh = bitcoin.payments.p2wsh({
|
|
95
|
-
redeem: { output },
|
|
96
|
-
network: NETWORK,
|
|
97
|
-
});
|
|
98
|
-
const p2sh = bitcoin.payments.p2sh({
|
|
99
|
-
redeem: { output: p2wsh.output },
|
|
100
|
-
network: NETWORK,
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
Object.assign(depends, {
|
|
104
|
-
prevOutScriptType: 'p2sh-p2wsh-' + k,
|
|
105
|
-
});
|
|
106
|
-
await buildAndSign(depends, p2sh.output, p2sh.redeem!.output, p2wsh.redeem!.output);
|
|
107
|
-
},
|
|
108
|
-
);
|
|
109
|
-
});
|
|
110
|
-
});
|
|
1
|
+
import { ECPairFactory } from 'ecpair';
|
|
2
|
+
import * as ecc from 'tiny-secp256k1';
|
|
3
|
+
import { describe, it } from 'mocha';
|
|
4
|
+
import * as bitcoin from '../../src/index.js';
|
|
5
|
+
import { regtestUtils } from './_regtest.js';
|
|
6
|
+
import * as fs from 'node:fs';
|
|
7
|
+
|
|
8
|
+
const ECPair = ECPairFactory(ecc);
|
|
9
|
+
const NETWORK = regtestUtils.network;
|
|
10
|
+
const keyPairs = [ECPair.makeRandom({ network: NETWORK }), ECPair.makeRandom({ network: NETWORK })];
|
|
11
|
+
|
|
12
|
+
async function buildAndSign(
|
|
13
|
+
depends: any,
|
|
14
|
+
prevOutput: any,
|
|
15
|
+
redeemScript: any,
|
|
16
|
+
witnessScript: any,
|
|
17
|
+
): Promise<null> {
|
|
18
|
+
const unspent = await regtestUtils.faucetComplex(prevOutput, 5e4);
|
|
19
|
+
const utx = await regtestUtils.fetch(unspent.txId);
|
|
20
|
+
|
|
21
|
+
const psbt = new bitcoin.Psbt({ network: NETWORK })
|
|
22
|
+
.addInput({
|
|
23
|
+
hash: unspent.txId,
|
|
24
|
+
index: unspent.vout,
|
|
25
|
+
nonWitnessUtxo: Buffer.from(utx.txHex, 'hex'),
|
|
26
|
+
...(redeemScript ? { redeemScript } : {}),
|
|
27
|
+
...(witnessScript ? { witnessScript } : {}),
|
|
28
|
+
})
|
|
29
|
+
.addOutput({
|
|
30
|
+
address: regtestUtils.RANDOM_ADDRESS,
|
|
31
|
+
value: 2e4,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (depends.signatures) {
|
|
35
|
+
keyPairs.forEach((keyPair) => {
|
|
36
|
+
psbt.signInput(0, keyPair);
|
|
37
|
+
});
|
|
38
|
+
} else if (depends.signature) {
|
|
39
|
+
psbt.signInput(0, keyPairs[0]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return regtestUtils.broadcast(psbt.finalizeAllInputs().extractTransaction().toHex());
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
['p2ms', 'p2pk', 'p2pkh', 'p2wpkh'].forEach((k) => {
|
|
46
|
+
const fixtures = JSON.parse(fs.readFileSync('test/fixtures/' + k + '.json', 'utf8'));
|
|
47
|
+
const { depends } = fixtures.dynamic;
|
|
48
|
+
const fn: any = (bitcoin.payments as any)[k];
|
|
49
|
+
|
|
50
|
+
const base: any = {};
|
|
51
|
+
if (depends.pubkey) base.pubkey = keyPairs[0].publicKey;
|
|
52
|
+
if (depends.pubkeys) base.pubkeys = keyPairs.map((x) => x.publicKey);
|
|
53
|
+
if (depends.m) base.m = base.pubkeys.length;
|
|
54
|
+
|
|
55
|
+
const { output } = fn(base);
|
|
56
|
+
if (!output) throw new TypeError('Missing output');
|
|
57
|
+
|
|
58
|
+
describe('bitcoinjs-lib (payments - ' + k + ')', () => {
|
|
59
|
+
it('can broadcast as an output, and be spent as an input', async () => {
|
|
60
|
+
Object.assign(depends, { prevOutScriptType: k });
|
|
61
|
+
await buildAndSign(depends, output, undefined, undefined);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it(
|
|
65
|
+
'can (as P2SH(' + k + ')) broadcast as an output, and be spent as an input',
|
|
66
|
+
async () => {
|
|
67
|
+
const p2sh = bitcoin.payments.p2sh({
|
|
68
|
+
redeem: { output },
|
|
69
|
+
network: NETWORK,
|
|
70
|
+
});
|
|
71
|
+
Object.assign(depends, { prevOutScriptType: 'p2sh-' + k });
|
|
72
|
+
await buildAndSign(depends, p2sh.output, p2sh.redeem!.output, undefined);
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
// NOTE: P2WPKH cannot be wrapped in P2WSH, consensus fail
|
|
77
|
+
if (k === 'p2wpkh') return;
|
|
78
|
+
|
|
79
|
+
it(
|
|
80
|
+
'can (as P2WSH(' + k + ')) broadcast as an output, and be spent as an input',
|
|
81
|
+
async () => {
|
|
82
|
+
const p2wsh = bitcoin.payments.p2wsh({
|
|
83
|
+
redeem: { output },
|
|
84
|
+
network: NETWORK,
|
|
85
|
+
});
|
|
86
|
+
Object.assign(depends, { prevOutScriptType: 'p2wsh-' + k });
|
|
87
|
+
await buildAndSign(depends, p2wsh.output, undefined, p2wsh.redeem!.output);
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
it(
|
|
92
|
+
'can (as P2SH(P2WSH(' + k + '))) broadcast as an output, and be spent as an input',
|
|
93
|
+
async () => {
|
|
94
|
+
const p2wsh = bitcoin.payments.p2wsh({
|
|
95
|
+
redeem: { output },
|
|
96
|
+
network: NETWORK,
|
|
97
|
+
});
|
|
98
|
+
const p2sh = bitcoin.payments.p2sh({
|
|
99
|
+
redeem: { output: p2wsh.output },
|
|
100
|
+
network: NETWORK,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
Object.assign(depends, {
|
|
104
|
+
prevOutScriptType: 'p2sh-p2wsh-' + k,
|
|
105
|
+
});
|
|
106
|
+
await buildAndSign(depends, p2sh.output, p2sh.redeem!.output, p2wsh.redeem!.output);
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
});
|