@btc-vision/bitcoin 6.4.2 → 6.4.3

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 (49) hide show
  1. package/browser/index.js +1 -1
  2. package/browser/payments/embed.d.ts +2 -2
  3. package/browser/payments/index.d.ts +58 -13
  4. package/browser/payments/lazy.d.ts +1 -1
  5. package/browser/payments/p2ms.d.ts +2 -2
  6. package/browser/payments/p2op.d.ts +25 -2
  7. package/browser/payments/p2pk.d.ts +2 -2
  8. package/browser/payments/p2pkh.d.ts +2 -2
  9. package/browser/payments/p2sh.d.ts +2 -2
  10. package/browser/payments/p2tr.d.ts +2 -2
  11. package/browser/payments/p2wpkh.d.ts +2 -2
  12. package/browser/payments/p2wsh.d.ts +2 -2
  13. package/build/address.js +0 -1
  14. package/build/payments/embed.d.ts +2 -2
  15. package/build/payments/embed.js +6 -2
  16. package/build/payments/index.d.ts +58 -13
  17. package/build/payments/lazy.d.ts +1 -1
  18. package/build/payments/p2ms.d.ts +2 -2
  19. package/build/payments/p2ms.js +8 -2
  20. package/build/payments/p2op.d.ts +25 -2
  21. package/build/payments/p2op.js +5 -1
  22. package/build/payments/p2pk.d.ts +2 -2
  23. package/build/payments/p2pk.js +5 -1
  24. package/build/payments/p2pkh.d.ts +2 -2
  25. package/build/payments/p2pkh.js +5 -1
  26. package/build/payments/p2sh.d.ts +2 -2
  27. package/build/payments/p2sh.js +5 -1
  28. package/build/payments/p2tr.d.ts +2 -2
  29. package/build/payments/p2tr.js +5 -2
  30. package/build/payments/p2wpkh.d.ts +2 -2
  31. package/build/payments/p2wpkh.js +5 -1
  32. package/build/payments/p2wsh.d.ts +2 -2
  33. package/build/payments/p2wsh.js +5 -1
  34. package/build/psbt.js +3 -1
  35. package/package.json +1 -1
  36. package/src/address.ts +283 -284
  37. package/src/payments/embed.ts +61 -55
  38. package/src/payments/index.ts +159 -68
  39. package/src/payments/lazy.ts +28 -28
  40. package/src/payments/p2ms.ts +11 -5
  41. package/src/payments/p2op.ts +170 -136
  42. package/src/payments/p2pk.ts +93 -85
  43. package/src/payments/p2pkh.ts +214 -210
  44. package/src/payments/p2sh.ts +211 -206
  45. package/src/payments/p2tr.ts +9 -4
  46. package/src/payments/p2wpkh.ts +7 -3
  47. package/src/payments/p2wsh.ts +7 -3
  48. package/src/psbt.ts +5 -3
  49. package/test/payments.spec.ts +2 -2
@@ -1,136 +1,170 @@
1
- import { bech32m } from 'bech32';
2
- import { Buffer as NBuffer } from 'buffer';
3
- import { fromBech32 } from '../address.js';
4
- import { bitcoin as BITCOIN_NETWORK, Network } from '../networks.js';
5
- import * as bscript from '../script.js';
6
- import { typeforce as typef } from '../types.js';
7
- import * as lazy from './lazy.js';
8
- import { Payment, PaymentOpts } from './index.js';
9
-
10
- const OPS = bscript.OPS;
11
- const P2OP_WITNESS_VERSION = 0x10;
12
- const MIN_SIZE = 2;
13
- const MAX_SIZE = 40;
14
-
15
- /**
16
- * Pay-to-OPNet (P2OP) decoder / encoder.
17
- *
18
- * ▪ witness program = <deploymentVersion:uint8><hash160:20-bytes|...>
19
- * ▪ scriptPubKey = OP_16 <program>
20
- * ▪ address HRP = network.bech32Opnet, encoded with Bech32m
21
- *
22
- * Accepts any combination of { address, output, program } and returns a
23
- * fully lazy payment object, mirroring the style of `p2tr`.
24
- */
25
- export function p2op(a: Payment, opts?: PaymentOpts): Payment {
26
- if (!a.address && !a.output && !a.program) throw new TypeError('Not enough data for p2op');
27
-
28
- opts = Object.assign({ validate: true }, opts || {});
29
-
30
- typef(
31
- {
32
- address: typef.maybe(typef.String),
33
- output: typef.maybe(typef.Buffer),
34
- program: typef.maybe(typef.Buffer),
35
- network: typef.maybe(typef.Object),
36
- deploymentVersion: typef.maybe(typef.Number),
37
- hash160: typef.maybe(typef.BufferN(20)),
38
- },
39
- a,
40
- );
41
-
42
- const _address = lazy.value(() => fromBech32(a.address!));
43
-
44
- const network: Network = a.network || BITCOIN_NETWORK;
45
- const o: Payment = { name: 'p2op', network };
46
-
47
- lazy.prop(o, 'program', () => {
48
- if (a.program) return a.program;
49
-
50
- if (a.output) {
51
- if (a.output[0] !== OPS.OP_16) throw new TypeError('Invalid P2OP script');
52
- let pushPos = 1,
53
- progLen: number;
54
- if (a.output[1] < 0x4c) {
55
- progLen = a.output[1];
56
- pushPos = 2;
57
- } else if (a.output[1] === 0x4c) {
58
- progLen = a.output[2];
59
- pushPos = 3;
60
- } else {
61
- throw new TypeError('Unsupported push opcode in P2OP script');
62
- }
63
- return a.output.slice(pushPos, pushPos + progLen);
64
- }
65
-
66
- if (a.address) {
67
- const dec = _address();
68
- return dec.data;
69
- }
70
- });
71
-
72
- lazy.prop(o, 'deploymentVersion', () => {
73
- if (!o.program) return;
74
- return o.program[0];
75
- });
76
-
77
- lazy.prop(o, 'hash160', () => {
78
- if (!o.program) return;
79
- return o.program.slice(1);
80
- });
81
-
82
- lazy.prop(o, 'output', () => {
83
- if (!o.program) return;
84
- return bscript.compile([OPS.OP_16, o.program]);
85
- });
86
-
87
- lazy.prop(o, 'address', () => {
88
- if (!o.program) return;
89
- if (!network.bech32Opnet) {
90
- throw new TypeError('Network does not support opnet');
91
- }
92
-
93
- const words = bech32m.toWords(o.program);
94
- words.unshift(P2OP_WITNESS_VERSION);
95
-
96
- return bech32m.encode(network.bech32Opnet, words);
97
- });
98
-
99
- // extended validation
100
- if (opts.validate) {
101
- let prog: Buffer = NBuffer.alloc(0);
102
-
103
- if (a.address) {
104
- const dec = _address();
105
- if (network.bech32Opnet !== dec.prefix)
106
- throw new TypeError('Invalid prefix or network mismatch');
107
- if (dec.version !== P2OP_WITNESS_VERSION)
108
- throw new TypeError('Invalid witness version for p2op');
109
- if (dec.data.length < MIN_SIZE || dec.data.length > MAX_SIZE)
110
- throw new TypeError('Invalid witness program length');
111
- prog = dec.data;
112
- }
113
-
114
- if (a.program) {
115
- if (prog.length && !prog.equals(a.program)) throw new TypeError('Program mismatch');
116
- prog = a.program;
117
- }
118
-
119
- if (a.output) {
120
- const outProg = o.program!;
121
- if (prog.length && !prog.equals(outProg))
122
- throw new TypeError('Program mismatch (output vs other source)');
123
- prog = outProg;
124
- }
125
-
126
- if (prog.length < MIN_SIZE || prog.length > MAX_SIZE)
127
- throw new TypeError('Witness program must be 2–40 bytes');
128
-
129
- if (a.deploymentVersion !== undefined && a.deploymentVersion !== prog[0])
130
- throw new TypeError('deploymentVersion mismatch');
131
-
132
- if (a.hash160 && !a.hash160.equals(prog.slice(1))) throw new TypeError('hash160 mismatch');
133
- }
134
-
135
- return Object.assign(o, a);
136
- }
1
+ import { bech32m } from 'bech32';
2
+ import { Buffer as NBuffer } from 'buffer';
3
+ import { fromBech32 } from '../address.js';
4
+ import { bitcoin as BITCOIN_NETWORK, Network } from '../networks.js';
5
+ import * as bscript from '../script.js';
6
+ import { typeforce as typef } from '../types.js';
7
+ import * as lazy from './lazy.js';
8
+ import { BasePayment, P2OPPayment, PaymentOpts } from './index.js';
9
+
10
+ const OPS = bscript.OPS;
11
+ const P2OP_WITNESS_VERSION = 0x10;
12
+ const MIN_SIZE = 2;
13
+ const MAX_SIZE = 40;
14
+
15
+ interface P2OPBase extends BasePayment {
16
+ name: 'p2op';
17
+ }
18
+
19
+ interface P2OP_fromOutput extends P2OPBase {
20
+ output: Buffer;
21
+
22
+ program?: undefined;
23
+ deploymentVersion?: undefined;
24
+ hash160?: undefined;
25
+ }
26
+
27
+ interface P2OP_fromProgram extends P2OPBase {
28
+ program: Buffer;
29
+
30
+ output?: undefined;
31
+ deploymentVersion?: never;
32
+ hash160?: never;
33
+ }
34
+
35
+ interface P2OP_fromParts extends P2OPBase {
36
+ deploymentVersion: number;
37
+ hash160: Buffer;
38
+
39
+ output?: undefined;
40
+ program?: undefined;
41
+ }
42
+
43
+ export type P2OPPaymentParams = P2OP_fromOutput | P2OP_fromProgram | P2OP_fromParts;
44
+
45
+ /**
46
+ * Pay-to-OPNet (P2OP) decoder / encoder.
47
+ *
48
+ * ▪ witness program = <deploymentVersion:uint8><hash160:20-bytes|...>
49
+ * ▪ scriptPubKey = OP_16 <program>
50
+ * ▪ address HRP = network.bech32Opnet, encoded with Bech32m
51
+ *
52
+ * Accepts any combination of { address, output, program } and returns a
53
+ * fully lazy payment object, mirroring the style of `p2tr`.
54
+ */
55
+ export function p2op(a: Omit<P2OPPaymentParams, 'name'>, opts?: PaymentOpts): P2OPPayment {
56
+ if (!a.address && !a.output && !a.program) throw new TypeError('Not enough data for p2op');
57
+
58
+ opts = Object.assign({ validate: true }, opts || {});
59
+
60
+ typef(
61
+ {
62
+ address: typef.maybe(typef.String),
63
+ output: typef.maybe(typef.Buffer),
64
+ program: typef.maybe(typef.Buffer),
65
+ network: typef.maybe(typef.Object),
66
+ deploymentVersion: typef.maybe(typef.Number),
67
+ hash160: typef.maybe(typef.BufferN(20)),
68
+ },
69
+ a,
70
+ );
71
+
72
+ const _address = lazy.value(() => fromBech32(a.address!));
73
+
74
+ const network: Network = a.network || BITCOIN_NETWORK;
75
+ const o: P2OPPayment = {
76
+ name: 'p2op',
77
+ network,
78
+ deploymentVersion: 0,
79
+ };
80
+
81
+ lazy.prop(o, 'program', () => {
82
+ if (a.program) return a.program;
83
+
84
+ if (a.output) {
85
+ if (a.output[0] !== OPS.OP_16) throw new TypeError('Invalid P2OP script');
86
+ let pushPos = 1,
87
+ progLen: number;
88
+ if (a.output[1] < 0x4c) {
89
+ progLen = a.output[1];
90
+ pushPos = 2;
91
+ } else if (a.output[1] === 0x4c) {
92
+ progLen = a.output[2];
93
+ pushPos = 3;
94
+ } else {
95
+ throw new TypeError('Unsupported push opcode in P2OP script');
96
+ }
97
+ return a.output.slice(pushPos, pushPos + progLen);
98
+ }
99
+
100
+ if (a.address) {
101
+ const dec = _address();
102
+ return dec.data;
103
+ }
104
+ });
105
+
106
+ lazy.prop(o, 'deploymentVersion', () => {
107
+ if (!o.program) return;
108
+ return o.program[0];
109
+ });
110
+
111
+ lazy.prop(o, 'hash160', () => {
112
+ if (!o.program) return;
113
+ return o.program.slice(1);
114
+ });
115
+
116
+ lazy.prop(o, 'output', () => {
117
+ if (!o.program) return;
118
+ return bscript.compile([OPS.OP_16, o.program]);
119
+ });
120
+
121
+ lazy.prop(o, 'address', () => {
122
+ if (!o.program) return;
123
+ if (!network.bech32Opnet) {
124
+ throw new TypeError('Network does not support opnet');
125
+ }
126
+
127
+ const words = bech32m.toWords(o.program);
128
+ words.unshift(P2OP_WITNESS_VERSION);
129
+
130
+ return bech32m.encode(network.bech32Opnet, words);
131
+ });
132
+
133
+ // extended validation
134
+ if (opts.validate) {
135
+ let prog: Buffer = NBuffer.alloc(0);
136
+
137
+ if (a.address) {
138
+ const dec = _address();
139
+ if (network.bech32Opnet !== dec.prefix)
140
+ throw new TypeError('Invalid prefix or network mismatch');
141
+ if (dec.version !== P2OP_WITNESS_VERSION)
142
+ throw new TypeError('Invalid witness version for p2op');
143
+ if (dec.data.length < MIN_SIZE || dec.data.length > MAX_SIZE)
144
+ throw new TypeError('Invalid witness program length');
145
+ prog = dec.data;
146
+ }
147
+
148
+ if (a.program) {
149
+ if (prog.length && !prog.equals(a.program)) throw new TypeError('Program mismatch');
150
+ prog = a.program;
151
+ }
152
+
153
+ if (a.output) {
154
+ const outProg = o.program!;
155
+ if (prog.length && !prog.equals(outProg))
156
+ throw new TypeError('Program mismatch (output vs other source)');
157
+ prog = outProg;
158
+ }
159
+
160
+ if (prog.length < MIN_SIZE || prog.length > MAX_SIZE)
161
+ throw new TypeError('Witness program must be 2–40 bytes');
162
+
163
+ if (a.deploymentVersion !== undefined && a.deploymentVersion !== prog[0])
164
+ throw new TypeError('deploymentVersion mismatch');
165
+
166
+ if (a.hash160 && !a.hash160.equals(prog.slice(1))) throw new TypeError('hash160 mismatch');
167
+ }
168
+
169
+ return Object.assign(o, a);
170
+ }
@@ -1,85 +1,93 @@
1
- import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
2
- import * as bscript from '../script.js';
3
- import { isPoint, typeforce as typef } from '../types.js';
4
- import { Payment, PaymentOpts, StackFunction } from './index.js';
5
- import * as lazy from './lazy.js';
6
-
7
- const OPS = bscript.OPS;
8
-
9
- // input: {signature}
10
- // output: {pubKey} OP_CHECKSIG
11
- /**
12
- * Creates a pay-to-public-key (P2PK) payment object.
13
- *
14
- * @param a - The payment object containing the necessary data.
15
- * @param opts - Optional payment options.
16
- * @returns The P2PK payment object.
17
- * @throws {TypeError} If the required data is not provided or if the data is invalid.
18
- */
19
- export function p2pk(a: Payment, opts?: PaymentOpts): Payment {
20
- if (!a.input && !a.output && !a.pubkey && !a.input && !a.signature)
21
- throw new TypeError('Not enough data');
22
- opts = Object.assign({ validate: true }, opts || {});
23
-
24
- typef(
25
- {
26
- network: typef.maybe(typef.Object),
27
- output: typef.maybe(typef.Buffer),
28
- pubkey: typef.maybe(isPoint),
29
-
30
- signature: typef.maybe(bscript.isCanonicalScriptSignature),
31
- input: typef.maybe(typef.Buffer),
32
- },
33
- a,
34
- );
35
-
36
- const _chunks = lazy.value(() => {
37
- return bscript.decompile(a.input!);
38
- }) as StackFunction;
39
-
40
- const network = a.network || BITCOIN_NETWORK;
41
- const o: Payment = { name: 'p2pk', network };
42
-
43
- lazy.prop(o, 'output', () => {
44
- if (!a.pubkey) return;
45
- return bscript.compile([a.pubkey, OPS.OP_CHECKSIG]);
46
- });
47
- lazy.prop(o, 'pubkey', () => {
48
- if (!a.output) return;
49
- return a.output.slice(1, -1);
50
- });
51
- lazy.prop(o, 'signature', () => {
52
- if (!a.input) return;
53
- return _chunks()[0] as Buffer;
54
- });
55
- lazy.prop(o, 'input', () => {
56
- if (!a.signature) return;
57
- return bscript.compile([a.signature]);
58
- });
59
- lazy.prop(o, 'witness', () => {
60
- if (!o.input) return;
61
- return [];
62
- });
63
-
64
- // extended validation
65
- if (opts.validate) {
66
- if (a.output) {
67
- if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG)
68
- throw new TypeError('Output is invalid');
69
- if (!isPoint(o.pubkey)) throw new TypeError('Output pubkey is invalid');
70
- if (a.pubkey && !a.pubkey.equals(o.pubkey!)) throw new TypeError('Pubkey mismatch');
71
- }
72
-
73
- if (a.signature) {
74
- if (a.input && !a.input.equals(o.input!)) throw new TypeError('Signature mismatch');
75
- }
76
-
77
- if (a.input) {
78
- if (_chunks().length !== 1) throw new TypeError('Input is invalid');
79
- if (!bscript.isCanonicalScriptSignature(o.signature!))
80
- throw new TypeError('Input has invalid signature');
81
- }
82
- }
83
-
84
- return Object.assign(o, a);
85
- }
1
+ import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
2
+ import * as bscript from '../script.js';
3
+ import { isPoint, typeforce as typef } from '../types.js';
4
+ import { P2PKPayment, PaymentOpts, StackFunction } from './index.js';
5
+ import * as lazy from './lazy.js';
6
+
7
+ const OPS = bscript.OPS;
8
+
9
+ // input: {signature}
10
+ // output: {pubKey} OP_CHECKSIG
11
+ /**
12
+ * Creates a pay-to-public-key (P2PK) payment object.
13
+ *
14
+ * @param a - The payment object containing the necessary data.
15
+ * @param opts - Optional payment options.
16
+ * @returns The P2PK payment object.
17
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
18
+ */
19
+ export function p2pk(a: Omit<P2PKPayment, 'name'>, opts?: PaymentOpts): P2PKPayment {
20
+ if (!a.input && !a.output && !a.pubkey && !a.input && !a.signature)
21
+ throw new TypeError('Not enough data');
22
+ opts = Object.assign({ validate: true }, opts || {});
23
+
24
+ typef(
25
+ {
26
+ network: typef.maybe(typef.Object),
27
+ output: typef.maybe(typef.Buffer),
28
+ pubkey: typef.maybe(isPoint),
29
+
30
+ signature: typef.maybe(bscript.isCanonicalScriptSignature),
31
+ input: typef.maybe(typef.Buffer),
32
+ },
33
+ a,
34
+ );
35
+
36
+ const _chunks = lazy.value(() => {
37
+ return bscript.decompile(a.input!);
38
+ }) as StackFunction;
39
+
40
+ const network = a.network || BITCOIN_NETWORK;
41
+ const o: P2PKPayment = {
42
+ name: 'p2pk',
43
+ network,
44
+ pubkey: undefined,
45
+ };
46
+
47
+ lazy.prop(o, 'output', () => {
48
+ if (!a.pubkey) return;
49
+ return bscript.compile([a.pubkey, OPS.OP_CHECKSIG]);
50
+ });
51
+
52
+ lazy.prop(o, 'pubkey', () => {
53
+ if (!a.output) return;
54
+ return a.output.slice(1, -1);
55
+ });
56
+
57
+ lazy.prop(o, 'signature', () => {
58
+ if (!a.input) return;
59
+ return _chunks()[0] as Buffer;
60
+ });
61
+
62
+ lazy.prop(o, 'input', () => {
63
+ if (!a.signature) return;
64
+ return bscript.compile([a.signature]);
65
+ });
66
+
67
+ lazy.prop(o, 'witness', () => {
68
+ if (!o.input) return;
69
+ return [];
70
+ });
71
+
72
+ // extended validation
73
+ if (opts.validate) {
74
+ if (a.output) {
75
+ if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG)
76
+ throw new TypeError('Output is invalid');
77
+ if (!isPoint(o.pubkey)) throw new TypeError('Output pubkey is invalid');
78
+ if (a.pubkey && !a.pubkey.equals(o.pubkey!)) throw new TypeError('Pubkey mismatch');
79
+ }
80
+
81
+ if (a.signature) {
82
+ if (a.input && !a.input.equals(o.input!)) throw new TypeError('Signature mismatch');
83
+ }
84
+
85
+ if (a.input) {
86
+ if (_chunks().length !== 1) throw new TypeError('Input is invalid');
87
+ if (!bscript.isCanonicalScriptSignature(o.signature!))
88
+ throw new TypeError('Input has invalid signature');
89
+ }
90
+ }
91
+
92
+ return Object.assign(o, a);
93
+ }