@btc-vision/bitcoin 6.4.8 → 6.4.10

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.
Files changed (43) hide show
  1. package/browser/index.d.ts +1 -1
  2. package/browser/index.js +1 -1
  3. package/browser/opcodes.d.ts +125 -0
  4. package/browser/script.d.ts +2 -2
  5. package/build/address.js +6 -5
  6. package/build/bufferutils.js +1 -1
  7. package/build/index.d.ts +1 -1
  8. package/build/index.js +1 -1
  9. package/build/opcodes.d.ts +125 -0
  10. package/build/opcodes.js +126 -0
  11. package/build/payments/embed.js +1 -1
  12. package/build/payments/p2ms.js +1 -1
  13. package/build/payments/p2op.js +1 -1
  14. package/build/payments/p2pk.js +1 -1
  15. package/build/payments/p2pkh.js +1 -1
  16. package/build/payments/p2sh.js +3 -3
  17. package/build/payments/p2tr.js +1 -1
  18. package/build/payments/p2wpkh.js +2 -2
  19. package/build/payments/p2wsh.js +2 -2
  20. package/build/psbt.js +2 -2
  21. package/build/push_data.js +9 -9
  22. package/build/script.d.ts +2 -2
  23. package/build/script.js +12 -12
  24. package/build/transaction.js +1 -1
  25. package/package.json +1 -1
  26. package/src/address.ts +304 -303
  27. package/src/bufferutils.ts +1 -1
  28. package/src/index.ts +1 -1
  29. package/src/{ops.ts → opcodes.ts} +4 -6
  30. package/src/payments/embed.ts +1 -1
  31. package/src/payments/p2ms.ts +1 -1
  32. package/src/payments/p2op.ts +1 -1
  33. package/src/payments/p2pk.ts +1 -1
  34. package/src/payments/p2pkh.ts +1 -1
  35. package/src/payments/p2sh.ts +210 -210
  36. package/src/payments/p2tr.ts +1 -1
  37. package/src/payments/p2wpkh.ts +144 -144
  38. package/src/payments/p2wsh.ts +217 -217
  39. package/src/psbt.ts +2 -2
  40. package/src/push_data.ts +9 -9
  41. package/src/script.ts +12 -12
  42. package/src/transaction.ts +1 -1
  43. package/test/script.spec.ts +1 -1
@@ -137,7 +137,7 @@ export interface Opcodes {
137
137
  OP_INVALIDOPCODE: number;
138
138
  }
139
139
 
140
- const OPS: Opcodes = {
140
+ export const opcodes: Opcodes = {
141
141
  OP_FALSE: 0,
142
142
  OP_0: 0,
143
143
  OP_PUSHDATA1: 76,
@@ -273,10 +273,8 @@ const OPS: Opcodes = {
273
273
  OP_INVALIDOPCODE: 255,
274
274
  };
275
275
 
276
- const REVERSE_OPS: { [key: number]: string } = {};
277
- for (const op of Object.keys(OPS)) {
278
- const code = OPS[op as keyof Opcodes];
276
+ export const REVERSE_OPS: { [key: number]: string } = {};
277
+ for (const op of Object.keys(opcodes)) {
278
+ const code = opcodes[op as keyof Opcodes];
279
279
  REVERSE_OPS[code] = op;
280
280
  }
281
-
282
- export { OPS, REVERSE_OPS };
@@ -4,7 +4,7 @@ import { stacksEqual, typeforce as typef } from '../types.js';
4
4
  import { EmbedPayment, PaymentOpts, PaymentType, Stack } from './index.js';
5
5
  import * as lazy from './lazy.js';
6
6
 
7
- const OPS = bscript.OPS;
7
+ const OPS = bscript.opcodes;
8
8
 
9
9
  // output: OP_RETURN ...
10
10
  /**
@@ -4,7 +4,7 @@ import { isPoint, stacksEqual, typeforce as typef } from '../types.js';
4
4
  import { P2MSPayment, PaymentOpts, PaymentType, Stack } from './index.js';
5
5
  import * as lazy from './lazy.js';
6
6
 
7
- const OPS = bscript.OPS;
7
+ const OPS = bscript.opcodes;
8
8
 
9
9
  const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
10
10
 
@@ -7,7 +7,7 @@ import { typeforce as typef } from '../types.js';
7
7
  import * as lazy from './lazy.js';
8
8
  import { BasePayment, P2OPPayment, PaymentOpts, PaymentType } from './index.js';
9
9
 
10
- const OPS = bscript.OPS;
10
+ const OPS = bscript.opcodes;
11
11
  const P2OP_WITNESS_VERSION = 0x10;
12
12
  const MIN_SIZE = 2;
13
13
  const MAX_SIZE = 40;
@@ -4,7 +4,7 @@ import { isPoint, typeforce as typef } from '../types.js';
4
4
  import { P2PKPayment, PaymentOpts, PaymentType, StackFunction } from './index.js';
5
5
  import * as lazy from './lazy.js';
6
6
 
7
- const OPS = bscript.OPS;
7
+ const OPS = bscript.opcodes;
8
8
 
9
9
  // input: {signature}
10
10
  // output: {pubKey} OP_CHECKSIG
@@ -7,7 +7,7 @@ import { P2PKHPayment, PaymentOpts, PaymentType, StackFunction } from './index.j
7
7
  import * as lazy from './lazy.js';
8
8
  import { decompressPublicKey } from '../psbt/psbtutils.js';
9
9
 
10
- const OPS = bscript.OPS;
10
+ const OPS = bscript.opcodes;
11
11
 
12
12
  // input: {signature} {pubkey}
13
13
  // output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
@@ -1,210 +1,210 @@
1
- import * as bs58check from 'bs58check';
2
- import * as bcrypto from '../crypto.js';
3
- import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
4
- import * as bscript from '../script.js';
5
- import { stacksEqual, typeforce as typef } from '../types.js';
6
- import { P2SHPayment, Payment, PaymentOpts, PaymentType, ScriptRedeem, Stack, StackFunction } from './index.js';
7
- import * as lazy from './lazy.js';
8
-
9
- const OPS = bscript.OPS;
10
-
11
- // input: [redeemScriptSig ...] {redeemScript}
12
- // witness: <?>
13
- // output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
14
- /**
15
- * Creates a Pay-to-Script-Hash (P2SH) payment object.
16
- *
17
- * @param a - The payment object containing the necessary data.
18
- * @param opts - Optional payment options.
19
- * @returns The P2SH payment object.
20
- * @throws {TypeError} If the required data is not provided or if the data is invalid.
21
- */
22
- export function p2sh(a: Omit<P2SHPayment, 'name'>, opts?: PaymentOpts): P2SHPayment {
23
- if (!a.address && !a.hash && !a.output && !a.redeem && !a.input) {
24
- throw new TypeError('Not enough data');
25
- }
26
- opts = Object.assign({ validate: true }, opts || {});
27
-
28
- typef(
29
- {
30
- network: typef.maybe(typef.Object),
31
-
32
- address: typef.maybe(typef.String),
33
- hash: typef.maybe(typef.BufferN(20)),
34
- output: typef.maybe(typef.BufferN(23)),
35
-
36
- redeem: typef.maybe({
37
- network: typef.maybe(typef.Object),
38
- output: typef.maybe(typef.Buffer),
39
- input: typef.maybe(typef.Buffer),
40
- witness: typef.maybe(typef.arrayOf(typef.Buffer)),
41
- }),
42
- input: typef.maybe(typef.Buffer),
43
- witness: typef.maybe(typef.arrayOf(typef.Buffer)),
44
- },
45
- a,
46
- );
47
-
48
- let network = a.network;
49
- if (!network) {
50
- network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK;
51
- }
52
-
53
- const o: P2SHPayment = {
54
- network,
55
- name: PaymentType.P2SH
56
- };
57
-
58
- const _address = lazy.value(() => {
59
- const payload = Buffer.from(bs58check.default.decode(a.address!));
60
- const version = payload.readUInt8(0);
61
- const hash = payload.slice(1);
62
- return { version, hash };
63
- });
64
- const _chunks = lazy.value(() => {
65
- return bscript.decompile(a.input!);
66
- }) as StackFunction;
67
-
68
- const _redeem = lazy.value((): ScriptRedeem => {
69
- const chunks = _chunks();
70
- const lastChunk = chunks[chunks.length - 1];
71
- return {
72
- network,
73
- output: lastChunk === OPS.OP_FALSE ? Buffer.from([]) : (lastChunk as Buffer),
74
- input: bscript.compile(chunks.slice(0, -1)),
75
- witness: a.witness || [],
76
- };
77
- });
78
-
79
- // output dependents
80
- lazy.prop(o, 'address', () => {
81
- if (!o.hash) return;
82
-
83
- const payload = Buffer.allocUnsafe(21);
84
- payload.writeUInt8(o.network!.scriptHash, 0);
85
- o.hash.copy(payload, 1);
86
- return bs58check.default.encode(payload);
87
- });
88
- lazy.prop(o, 'hash', () => {
89
- // in order of least effort
90
- if (a.output) return a.output.slice(2, 22);
91
- if (a.address) return _address().hash;
92
- if (o.redeem && o.redeem.output) return bcrypto.hash160(o.redeem.output);
93
- });
94
- lazy.prop(o, 'output', () => {
95
- if (!o.hash) return;
96
- return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]);
97
- });
98
-
99
- // input dependents
100
- lazy.prop(o, 'redeem', () => {
101
- if (!a.input) return;
102
- return _redeem();
103
- });
104
- lazy.prop(o, 'input', () => {
105
- if (!a.redeem || !a.redeem.input || !a.redeem.output) return;
106
- return bscript.compile(
107
- ([] as Stack).concat(bscript.decompile(a.redeem.input) as Stack, a.redeem.output),
108
- );
109
- });
110
- lazy.prop(o, 'witness', () => {
111
- if (o.redeem && o.redeem.witness) return o.redeem.witness;
112
- if (o.input) return [];
113
- });
114
- lazy.prop(o, 'name', () => {
115
- const nameParts = ['p2sh'];
116
- if (o.redeem !== undefined && o.redeem.name !== undefined) nameParts.push(o.redeem.name!);
117
- return nameParts.join('-');
118
- });
119
-
120
- if (opts.validate) {
121
- let hash: Buffer = Buffer.from([]);
122
- if (a.address) {
123
- if (_address().version !== network.scriptHash)
124
- throw new TypeError('Invalid version or Network mismatch');
125
- if (_address().hash.length !== 20) throw new TypeError('Invalid address');
126
- hash = _address().hash;
127
- }
128
-
129
- if (a.hash) {
130
- if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch');
131
- else hash = a.hash;
132
- }
133
-
134
- if (a.output) {
135
- if (
136
- a.output.length !== 23 ||
137
- a.output[0] !== OPS.OP_HASH160 ||
138
- a.output[1] !== 0x14 ||
139
- a.output[22] !== OPS.OP_EQUAL
140
- )
141
- throw new TypeError('Output is invalid');
142
-
143
- const hash2 = a.output.slice(2, 22);
144
- if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
145
- else hash = hash2;
146
- }
147
-
148
- // inlined to prevent 'no-inner-declarations' failing
149
- const checkRedeem = (redeem: Payment): void => {
150
- // is the redeem output empty/invalid?
151
- if (redeem.output) {
152
- const decompile = bscript.decompile(redeem.output);
153
- if (!decompile || decompile.length < 1)
154
- throw new TypeError('Redeem.output too short');
155
- if (redeem.output.byteLength > 520)
156
- throw new TypeError('Redeem.output unspendable if larger than 520 bytes');
157
- if (bscript.countNonPushOnlyOPs(decompile) > 201)
158
- throw new TypeError(
159
- 'Redeem.output unspendable with more than 201 non-push ops',
160
- );
161
-
162
- // match hash against other sources
163
- const hash2 = bcrypto.hash160(redeem.output);
164
- if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
165
- else hash = hash2;
166
- }
167
-
168
- if (redeem.input) {
169
- const hasInput = redeem.input.length > 0;
170
- const hasWitness = redeem.witness && redeem.witness.length > 0;
171
- if (!hasInput && !hasWitness) throw new TypeError('Empty input');
172
- if (hasInput && hasWitness) throw new TypeError('Input and witness provided');
173
- if (hasInput) {
174
- const richunks = bscript.decompile(redeem.input) as Stack;
175
- if (!bscript.isPushOnly(richunks))
176
- throw new TypeError('Non push-only scriptSig');
177
- }
178
- }
179
- };
180
-
181
- if (a.input) {
182
- const chunks = _chunks();
183
- if (!chunks || chunks.length < 1) throw new TypeError('Input too short');
184
- if (!Buffer.isBuffer(_redeem().output)) throw new TypeError('Input is invalid');
185
-
186
- checkRedeem(_redeem());
187
- }
188
-
189
- if (a.redeem) {
190
- if (a.redeem.network && a.redeem.network !== network)
191
- throw new TypeError('Network mismatch');
192
- if (a.input) {
193
- const redeem = _redeem();
194
- if (a.redeem.output && !a.redeem.output.equals(redeem.output!))
195
- throw new TypeError('Redeem.output mismatch');
196
- if (a.redeem.input && !a.redeem.input.equals(redeem.input!))
197
- throw new TypeError('Redeem.input mismatch');
198
- }
199
-
200
- checkRedeem(a.redeem);
201
- }
202
-
203
- if (a.witness) {
204
- if (a.redeem && a.redeem.witness && !stacksEqual(a.redeem.witness, a.witness))
205
- throw new TypeError('Witness and redeem.witness mismatch');
206
- }
207
- }
208
-
209
- return Object.assign(o, a);
210
- }
1
+ import * as bs58check from 'bs58check';
2
+ import * as bcrypto from '../crypto.js';
3
+ import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
4
+ import * as bscript from '../script.js';
5
+ import { stacksEqual, typeforce as typef } from '../types.js';
6
+ import { P2SHPayment, Payment, PaymentOpts, PaymentType, ScriptRedeem, Stack, StackFunction, } from './index.js';
7
+ import * as lazy from './lazy.js';
8
+
9
+ const OPS = bscript.opcodes;
10
+
11
+ // input: [redeemScriptSig ...] {redeemScript}
12
+ // witness: <?>
13
+ // output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
14
+ /**
15
+ * Creates a Pay-to-Script-Hash (P2SH) payment object.
16
+ *
17
+ * @param a - The payment object containing the necessary data.
18
+ * @param opts - Optional payment options.
19
+ * @returns The P2SH payment object.
20
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
21
+ */
22
+ export function p2sh(a: Omit<P2SHPayment, 'name'>, opts?: PaymentOpts): P2SHPayment {
23
+ if (!a.address && !a.hash && !a.output && !a.redeem && !a.input) {
24
+ throw new TypeError('Not enough data');
25
+ }
26
+ opts = Object.assign({ validate: true }, opts || {});
27
+
28
+ typef(
29
+ {
30
+ network: typef.maybe(typef.Object),
31
+
32
+ address: typef.maybe(typef.String),
33
+ hash: typef.maybe(typef.BufferN(20)),
34
+ output: typef.maybe(typef.BufferN(23)),
35
+
36
+ redeem: typef.maybe({
37
+ network: typef.maybe(typef.Object),
38
+ output: typef.maybe(typef.Buffer),
39
+ input: typef.maybe(typef.Buffer),
40
+ witness: typef.maybe(typef.arrayOf(typef.Buffer)),
41
+ }),
42
+ input: typef.maybe(typef.Buffer),
43
+ witness: typef.maybe(typef.arrayOf(typef.Buffer)),
44
+ },
45
+ a,
46
+ );
47
+
48
+ let network = a.network;
49
+ if (!network) {
50
+ network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK;
51
+ }
52
+
53
+ const o: P2SHPayment = {
54
+ network,
55
+ name: PaymentType.P2SH,
56
+ };
57
+
58
+ const _address = lazy.value(() => {
59
+ const payload = Buffer.from(bs58check.default.decode(a.address!));
60
+ const version = payload.readUInt8(0);
61
+ const hash = payload.slice(1);
62
+ return { version, hash };
63
+ });
64
+ const _chunks = lazy.value(() => {
65
+ return bscript.decompile(a.input!);
66
+ }) as StackFunction;
67
+
68
+ const _redeem = lazy.value((): ScriptRedeem => {
69
+ const chunks = _chunks();
70
+ const lastChunk = chunks[chunks.length - 1];
71
+ return {
72
+ network,
73
+ output: lastChunk === OPS.OP_FALSE ? Buffer.from([]) : (lastChunk as Buffer),
74
+ input: bscript.compile(chunks.slice(0, -1)),
75
+ witness: a.witness || [],
76
+ };
77
+ });
78
+
79
+ // output dependents
80
+ lazy.prop(o, 'address', () => {
81
+ if (!o.hash) return;
82
+
83
+ const payload = Buffer.allocUnsafe(21);
84
+ payload.writeUInt8(o.network!.scriptHash, 0);
85
+ o.hash.copy(payload, 1);
86
+ return bs58check.default.encode(payload);
87
+ });
88
+ lazy.prop(o, 'hash', () => {
89
+ // in order of least effort
90
+ if (a.output) return a.output.slice(2, 22);
91
+ if (a.address) return _address().hash;
92
+ if (o.redeem && o.redeem.output) return bcrypto.hash160(o.redeem.output);
93
+ });
94
+ lazy.prop(o, 'output', () => {
95
+ if (!o.hash) return;
96
+ return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]);
97
+ });
98
+
99
+ // input dependents
100
+ lazy.prop(o, 'redeem', () => {
101
+ if (!a.input) return;
102
+ return _redeem();
103
+ });
104
+ lazy.prop(o, 'input', () => {
105
+ if (!a.redeem || !a.redeem.input || !a.redeem.output) return;
106
+ return bscript.compile(
107
+ ([] as Stack).concat(bscript.decompile(a.redeem.input) as Stack, a.redeem.output),
108
+ );
109
+ });
110
+ lazy.prop(o, 'witness', () => {
111
+ if (o.redeem && o.redeem.witness) return o.redeem.witness;
112
+ if (o.input) return [];
113
+ });
114
+ lazy.prop(o, 'name', () => {
115
+ const nameParts = ['p2sh'];
116
+ if (o.redeem !== undefined && o.redeem.name !== undefined) nameParts.push(o.redeem.name!);
117
+ return nameParts.join('-');
118
+ });
119
+
120
+ if (opts.validate) {
121
+ let hash: Buffer = Buffer.from([]);
122
+ if (a.address) {
123
+ if (_address().version !== network.scriptHash)
124
+ throw new TypeError('Invalid version or Network mismatch');
125
+ if (_address().hash.length !== 20) throw new TypeError('Invalid address');
126
+ hash = _address().hash;
127
+ }
128
+
129
+ if (a.hash) {
130
+ if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch');
131
+ else hash = a.hash;
132
+ }
133
+
134
+ if (a.output) {
135
+ if (
136
+ a.output.length !== 23 ||
137
+ a.output[0] !== OPS.OP_HASH160 ||
138
+ a.output[1] !== 0x14 ||
139
+ a.output[22] !== OPS.OP_EQUAL
140
+ )
141
+ throw new TypeError('Output is invalid');
142
+
143
+ const hash2 = a.output.slice(2, 22);
144
+ if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
145
+ else hash = hash2;
146
+ }
147
+
148
+ // inlined to prevent 'no-inner-declarations' failing
149
+ const checkRedeem = (redeem: Payment): void => {
150
+ // is the redeem output empty/invalid?
151
+ if (redeem.output) {
152
+ const decompile = bscript.decompile(redeem.output);
153
+ if (!decompile || decompile.length < 1)
154
+ throw new TypeError('Redeem.output too short');
155
+ if (redeem.output.byteLength > 520)
156
+ throw new TypeError('Redeem.output unspendable if larger than 520 bytes');
157
+ if (bscript.countNonPushOnlyOPs(decompile) > 201)
158
+ throw new TypeError(
159
+ 'Redeem.output unspendable with more than 201 non-push ops',
160
+ );
161
+
162
+ // match hash against other sources
163
+ const hash2 = bcrypto.hash160(redeem.output);
164
+ if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
165
+ else hash = hash2;
166
+ }
167
+
168
+ if (redeem.input) {
169
+ const hasInput = redeem.input.length > 0;
170
+ const hasWitness = redeem.witness && redeem.witness.length > 0;
171
+ if (!hasInput && !hasWitness) throw new TypeError('Empty input');
172
+ if (hasInput && hasWitness) throw new TypeError('Input and witness provided');
173
+ if (hasInput) {
174
+ const richunks = bscript.decompile(redeem.input) as Stack;
175
+ if (!bscript.isPushOnly(richunks))
176
+ throw new TypeError('Non push-only scriptSig');
177
+ }
178
+ }
179
+ };
180
+
181
+ if (a.input) {
182
+ const chunks = _chunks();
183
+ if (!chunks || chunks.length < 1) throw new TypeError('Input too short');
184
+ if (!Buffer.isBuffer(_redeem().output)) throw new TypeError('Input is invalid');
185
+
186
+ checkRedeem(_redeem());
187
+ }
188
+
189
+ if (a.redeem) {
190
+ if (a.redeem.network && a.redeem.network !== network)
191
+ throw new TypeError('Network mismatch');
192
+ if (a.input) {
193
+ const redeem = _redeem();
194
+ if (a.redeem.output && !a.redeem.output.equals(redeem.output!))
195
+ throw new TypeError('Redeem.output mismatch');
196
+ if (a.redeem.input && !a.redeem.input.equals(redeem.input!))
197
+ throw new TypeError('Redeem.input mismatch');
198
+ }
199
+
200
+ checkRedeem(a.redeem);
201
+ }
202
+
203
+ if (a.witness) {
204
+ if (a.redeem && a.redeem.witness && !stacksEqual(a.redeem.witness, a.witness))
205
+ throw new TypeError('Witness and redeem.witness mismatch');
206
+ }
207
+ }
208
+
209
+ return Object.assign(o, a);
210
+ }
@@ -16,7 +16,7 @@ import {
16
16
  import { P2TRPayment, PaymentOpts, PaymentType } from './index.js';
17
17
  import * as lazy from './lazy.js';
18
18
 
19
- const OPS = bscript.OPS;
19
+ const OPS = bscript.opcodes;
20
20
  const TAPROOT_WITNESS_VERSION = 0x01;
21
21
  const ANNEX_PREFIX = 0x50;
22
22