@btc-vision/bitcoin 6.3.6 → 6.4.0
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/.mocharc.json +13 -0
- package/browser/address.d.ts +1 -1
- package/browser/index.js +1 -1
- package/browser/index.js.LICENSE.txt +3 -3
- package/browser/networks.d.ts +1 -0
- package/build/address.d.ts +1 -1
- package/build/address.js +12 -5
- package/build/block.js +2 -2
- package/build/bufferutils.js +5 -5
- package/build/networks.d.ts +1 -0
- package/build/networks.js +11 -0
- package/build/psbt/psbtutils.js +2 -2
- package/build/psbt.js +3 -7
- package/package.json +26 -26
- package/src/address.ts +20 -6
- package/src/block.ts +233 -233
- package/src/bufferutils.ts +188 -180
- package/src/index.ts +86 -86
- package/src/networks.ts +12 -0
- package/src/psbt/bip371.ts +441 -441
- package/src/psbt/psbtutils.ts +4 -3
- package/src/psbt.ts +2187 -2187
- package/test/address.spec.ts +155 -177
- package/test/bitcoin.core.spec.ts +212 -234
- package/test/block.spec.ts +171 -194
- package/test/bufferutils.spec.ts +450 -513
- package/test/crypto.spec.ts +49 -55
- package/test/fixtures/address.json +3 -3
- package/test/integration/addresses.spec.ts +142 -154
- package/test/integration/bip32.spec.ts +130 -151
- package/test/integration/blocks.spec.ts +28 -28
- package/test/integration/cltv.spec.ts +241 -283
- package/test/integration/csv.spec.ts +452 -527
- package/test/integration/payments.spec.ts +110 -135
- package/test/integration/taproot.spec.ts +663 -707
- package/test/integration/transactions.spec.ts +668 -769
- package/test/payments.spec.ts +114 -125
- package/test/payments.utils.ts +165 -208
- package/test/psbt.spec.ts +1285 -1414
- package/test/script.spec.ts +186 -210
- package/test/script_number.spec.ts +26 -29
- package/test/script_signature.spec.ts +66 -66
- package/test/transaction.spec.ts +337 -387
- package/test/ts-node-register.js +7 -5
- package/test/tsconfig.json +4 -1
- package/test/types.spec.ts +53 -58
- package/.nyc_output/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
- package/.nyc_output/processinfo/6368a5b2-daa5-4821-8ed0-b742d6fc7eab.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/test/address.spec.js +0 -124
- package/test/bitcoin.core.spec.js +0 -170
- package/test/block.spec.js +0 -141
- package/test/bufferutils.spec.js +0 -427
- package/test/crypto.spec.js +0 -41
- package/test/integration/_regtest.js +0 -7
- package/test/integration/addresses.spec.js +0 -116
- package/test/integration/bip32.spec.js +0 -85
- package/test/integration/blocks.spec.js +0 -26
- package/test/integration/cltv.spec.js +0 -199
- package/test/integration/csv.spec.js +0 -362
- package/test/integration/payments.spec.js +0 -98
- package/test/integration/taproot.spec.js +0 -532
- package/test/integration/transactions.spec.js +0 -561
- package/test/payments.spec.js +0 -97
- package/test/payments.utils.js +0 -190
- package/test/psbt.spec.js +0 -1044
- package/test/script.spec.js +0 -151
- package/test/script_number.spec.js +0 -24
- package/test/script_signature.spec.js +0 -52
- package/test/transaction.spec.js +0 -269
- package/test/types.spec.js +0 -46
|
@@ -1,234 +1,212 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { describe, it } from 'mocha';
|
|
4
|
-
import * as bitcoin from '
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
|
|
14
|
-
describe('Bitcoin-core', () => {
|
|
15
|
-
// base58EncodeDecode
|
|
16
|
-
describe('base58', () => {
|
|
17
|
-
base58EncodeDecode.forEach(f => {
|
|
18
|
-
const fhex = f[0];
|
|
19
|
-
const fb58 = f[1];
|
|
20
|
-
|
|
21
|
-
it('can decode ' + fb58, () => {
|
|
22
|
-
const buffer = base58.decode(fb58);
|
|
23
|
-
const actual = buffer.toString('hex');
|
|
24
|
-
|
|
25
|
-
assert.strictEqual(actual, fhex);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('can encode ' + fhex, () => {
|
|
29
|
-
const buffer = Buffer.from(fhex, 'hex');
|
|
30
|
-
const actual = base58.encode(buffer);
|
|
31
|
-
|
|
32
|
-
assert.strictEqual(actual, fb58);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
// base58KeysValid
|
|
38
|
-
describe('address.toBase58Check', () => {
|
|
39
|
-
const typeMap: any = {
|
|
40
|
-
pubkey: 'pubKeyHash',
|
|
41
|
-
script: 'scriptHash',
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
base58KeysValid.forEach(f => {
|
|
45
|
-
const expected = f[0];
|
|
46
|
-
const hash = Buffer.from(f[1] as any, 'hex');
|
|
47
|
-
const params = f[2] as any;
|
|
48
|
-
|
|
49
|
-
if (params.isPrivkey) return;
|
|
50
|
-
|
|
51
|
-
const network: any = params.isTestnet
|
|
52
|
-
? bitcoin.networks.testnet
|
|
53
|
-
: bitcoin.networks.bitcoin;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
bitcoin.networks.
|
|
69
|
-
bitcoin.networks.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (
|
|
148
|
-
hashTypes.push('
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
(
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
sigNoncanonical.forEach((hex, i) => {
|
|
216
|
-
if (i === 0) return;
|
|
217
|
-
if (i % 2 !== 0) return;
|
|
218
|
-
|
|
219
|
-
const description = sigNoncanonical[i - 1].slice(0, -1);
|
|
220
|
-
const buffer = Buffer.from(hex, 'hex');
|
|
221
|
-
|
|
222
|
-
it('throws on ' + description, () => {
|
|
223
|
-
const reg = new RegExp(
|
|
224
|
-
'Expected DER (integer|sequence)|(R|S) value (excessively ' +
|
|
225
|
-
'padded|is negative)|(R|S|DER sequence) length is (zero|too ' +
|
|
226
|
-
'short|too long|invalid)|Invalid hashType',
|
|
227
|
-
);
|
|
228
|
-
assert.throws(() => {
|
|
229
|
-
bitcoin.script.signature.decode(buffer);
|
|
230
|
-
}, reg);
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
});
|
|
1
|
+
import assert from 'assert';
|
|
2
|
+
import base58 from 'bs58';
|
|
3
|
+
import { describe, it } from 'mocha';
|
|
4
|
+
import * as bitcoin from '../src/index.js';
|
|
5
|
+
import base58EncodeDecode from './fixtures/core/base58_encode_decode.json' with { type: 'json' };
|
|
6
|
+
import base58KeysInvalid from './fixtures/core/base58_keys_invalid.json' with { type: 'json' };
|
|
7
|
+
import base58KeysValid from './fixtures/core/base58_keys_valid.json' with { type: 'json' };
|
|
8
|
+
import blocksValid from './fixtures/core/blocks.json' with { type: 'json' };
|
|
9
|
+
import sigCanonical from './fixtures/core/sig_canonical.json' with { type: 'json' };
|
|
10
|
+
import sigNoncanonical from './fixtures/core/sig_noncanonical.json' with { type: 'json' };
|
|
11
|
+
import sigHash from './fixtures/core/sighash.json' with { type: 'json' };
|
|
12
|
+
import txValid from './fixtures/core/tx_valid.json' with { type: 'json' };
|
|
13
|
+
|
|
14
|
+
describe('Bitcoin-core', () => {
|
|
15
|
+
// base58EncodeDecode
|
|
16
|
+
describe('base58', () => {
|
|
17
|
+
base58EncodeDecode.forEach((f) => {
|
|
18
|
+
const fhex = f[0];
|
|
19
|
+
const fb58 = f[1];
|
|
20
|
+
|
|
21
|
+
it('can decode ' + fb58, () => {
|
|
22
|
+
const buffer = base58.decode(fb58);
|
|
23
|
+
const actual = Buffer.from(buffer).toString('hex');
|
|
24
|
+
|
|
25
|
+
assert.strictEqual(actual, fhex);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('can encode ' + fhex, () => {
|
|
29
|
+
const buffer = Buffer.from(fhex, 'hex');
|
|
30
|
+
const actual = base58.encode(buffer);
|
|
31
|
+
|
|
32
|
+
assert.strictEqual(actual, fb58);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// base58KeysValid
|
|
38
|
+
describe('address.toBase58Check', () => {
|
|
39
|
+
const typeMap: any = {
|
|
40
|
+
pubkey: 'pubKeyHash',
|
|
41
|
+
script: 'scriptHash',
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
base58KeysValid.forEach((f) => {
|
|
45
|
+
const expected = f[0];
|
|
46
|
+
const hash = Buffer.from(f[1] as any, 'hex');
|
|
47
|
+
const params = f[2] as any;
|
|
48
|
+
|
|
49
|
+
if (params.isPrivkey) return;
|
|
50
|
+
|
|
51
|
+
const network: any = params.isTestnet
|
|
52
|
+
? bitcoin.networks.testnet
|
|
53
|
+
: bitcoin.networks.bitcoin;
|
|
54
|
+
|
|
55
|
+
const version = network[typeMap[params.addrType]];
|
|
56
|
+
|
|
57
|
+
it(`can export ${expected as string}`, () => {
|
|
58
|
+
assert.strictEqual(bitcoin.address.toBase58Check(hash, version), expected);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// base58KeysInvalid
|
|
64
|
+
describe('address.fromBase58Check', () => {
|
|
65
|
+
const allowedNetworks = [
|
|
66
|
+
bitcoin.networks.bitcoin.pubKeyHash,
|
|
67
|
+
bitcoin.networks.bitcoin.scriptHash,
|
|
68
|
+
bitcoin.networks.testnet.pubKeyHash,
|
|
69
|
+
bitcoin.networks.testnet.scriptHash,
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
base58KeysInvalid.forEach((f) => {
|
|
73
|
+
const strng = f[0];
|
|
74
|
+
|
|
75
|
+
it('throws on ' + strng, () => {
|
|
76
|
+
assert.throws(() => {
|
|
77
|
+
const address = bitcoin.address.fromBase58Check(strng);
|
|
78
|
+
|
|
79
|
+
assert.notStrictEqual(
|
|
80
|
+
allowedNetworks.indexOf(address.version),
|
|
81
|
+
-1,
|
|
82
|
+
'Invalid network',
|
|
83
|
+
);
|
|
84
|
+
}, /(Invalid (checksum|network))|(too (short|long))/);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('Block.fromHex', () => {
|
|
90
|
+
blocksValid.forEach((f) => {
|
|
91
|
+
it('can parse ' + f.id, () => {
|
|
92
|
+
const block = bitcoin.Block.fromHex(f.hex);
|
|
93
|
+
|
|
94
|
+
assert.strictEqual(block.getId(), f.id);
|
|
95
|
+
assert.strictEqual(block.transactions!.length, f.transactions);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// txValid
|
|
101
|
+
describe('Transaction.fromHex', () => {
|
|
102
|
+
txValid.forEach((f) => {
|
|
103
|
+
// Objects that are only a single string are ignored
|
|
104
|
+
if (f.length === 1) return;
|
|
105
|
+
|
|
106
|
+
const inputs = f[0];
|
|
107
|
+
const fhex = f[1];
|
|
108
|
+
// const verifyFlags = f[2] // TODO: do we need to test this?
|
|
109
|
+
|
|
110
|
+
it('can decode ' + fhex, () => {
|
|
111
|
+
const transaction = bitcoin.Transaction.fromHex(fhex as string);
|
|
112
|
+
|
|
113
|
+
transaction.ins.forEach((txIn, i) => {
|
|
114
|
+
const input = inputs[i];
|
|
115
|
+
|
|
116
|
+
// reverse because test data is reversed
|
|
117
|
+
const prevOutHash = Buffer.from(input[0] as string, 'hex').reverse();
|
|
118
|
+
const prevOutIndex = input[1];
|
|
119
|
+
|
|
120
|
+
assert.deepStrictEqual(txIn.hash, prevOutHash);
|
|
121
|
+
|
|
122
|
+
// we read UInt32, not Int32
|
|
123
|
+
assert.strictEqual(txIn.index & 0xffffffff, prevOutIndex);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// sighash
|
|
130
|
+
describe('Transaction', () => {
|
|
131
|
+
sigHash.forEach((f) => {
|
|
132
|
+
// Objects that are only a single string are ignored
|
|
133
|
+
if (f.length === 1) return;
|
|
134
|
+
|
|
135
|
+
const txHex = f[0] as string;
|
|
136
|
+
const scriptHex = f[1] as string;
|
|
137
|
+
const inIndex = f[2] as number;
|
|
138
|
+
const hashType = f[3] as number;
|
|
139
|
+
const expectedHash = f[4];
|
|
140
|
+
|
|
141
|
+
const hashTypes = [];
|
|
142
|
+
if ((hashType & 0x1f) === bitcoin.Transaction.SIGHASH_NONE)
|
|
143
|
+
hashTypes.push('SIGHASH_NONE');
|
|
144
|
+
else if ((hashType & 0x1f) === bitcoin.Transaction.SIGHASH_SINGLE)
|
|
145
|
+
hashTypes.push('SIGHASH_SINGLE');
|
|
146
|
+
else hashTypes.push('SIGHASH_ALL');
|
|
147
|
+
if (hashType & bitcoin.Transaction.SIGHASH_ANYONECANPAY)
|
|
148
|
+
hashTypes.push('SIGHASH_ANYONECANPAY');
|
|
149
|
+
|
|
150
|
+
const hashTypeName = hashTypes.join(' | ');
|
|
151
|
+
|
|
152
|
+
it('should hash ' + txHex.slice(0, 40) + '... (' + hashTypeName + ')', () => {
|
|
153
|
+
const transaction = bitcoin.Transaction.fromHex(txHex);
|
|
154
|
+
assert.strictEqual(transaction.toHex(), txHex);
|
|
155
|
+
|
|
156
|
+
const script = Buffer.from(scriptHex, 'hex');
|
|
157
|
+
const scriptChunks = bitcoin.script.decompile(script);
|
|
158
|
+
assert.strictEqual(
|
|
159
|
+
bitcoin.script.compile(scriptChunks!).toString('hex'),
|
|
160
|
+
scriptHex,
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
const hash = transaction.hashForSignature(inIndex, script, hashType);
|
|
164
|
+
|
|
165
|
+
// reverse because test data is reversed
|
|
166
|
+
assert.strictEqual((hash.reverse() as Buffer).toString('hex'), expectedHash);
|
|
167
|
+
|
|
168
|
+
assert.doesNotThrow(() =>
|
|
169
|
+
transaction.hashForWitnessV0(
|
|
170
|
+
inIndex,
|
|
171
|
+
script,
|
|
172
|
+
0,
|
|
173
|
+
// convert to UInt32
|
|
174
|
+
hashType < 0 ? 0x100000000 + hashType : hashType,
|
|
175
|
+
),
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe('script.signature.decode', () => {
|
|
182
|
+
sigCanonical.forEach((hex) => {
|
|
183
|
+
const buffer = Buffer.from(hex, 'hex');
|
|
184
|
+
|
|
185
|
+
it('can parse ' + hex, () => {
|
|
186
|
+
const parsed = bitcoin.script.signature.decode(buffer);
|
|
187
|
+
const actual = bitcoin.script.signature.encode(parsed.signature, parsed.hashType);
|
|
188
|
+
|
|
189
|
+
assert.strictEqual(actual.toString('hex'), hex);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
sigNoncanonical.forEach((hex, i) => {
|
|
194
|
+
if (i === 0) return;
|
|
195
|
+
if (i % 2 !== 0) return;
|
|
196
|
+
|
|
197
|
+
const description = sigNoncanonical[i - 1].slice(0, -1);
|
|
198
|
+
const buffer = Buffer.from(hex, 'hex');
|
|
199
|
+
|
|
200
|
+
it('throws on ' + description, () => {
|
|
201
|
+
const reg = new RegExp(
|
|
202
|
+
'Expected DER (integer|sequence)|(R|S) value (excessively ' +
|
|
203
|
+
'padded|is negative)|(R|S|DER sequence) length is (zero|too ' +
|
|
204
|
+
'short|too long|invalid)|Invalid hashType',
|
|
205
|
+
);
|
|
206
|
+
assert.throws(() => {
|
|
207
|
+
bitcoin.script.signature.decode(buffer);
|
|
208
|
+
}, reg);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
});
|