@btc-vision/bitcoin 6.4.9 → 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.
@@ -1,144 +1,144 @@
1
- import { bech32 } from 'bech32';
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 { isPoint, typeforce as typef } from '../types.js';
6
- import { P2WPKHPayment, PaymentOpts, PaymentType } from './index.js';
7
- import * as lazy from './lazy.js';
8
-
9
- const OPS = bscript.OPS;
10
-
11
- const EMPTY_BUFFER = Buffer.alloc(0);
12
-
13
- // witness: {signature} {pubKey}
14
- // input: <>
15
- // output: OP_0 {pubKeyHash}
16
- /**
17
- * Creates a pay-to-witness-public-key-hash (p2wpkh) payment object.
18
- *
19
- * @param a - The payment object containing the necessary data.
20
- * @param opts - Optional payment options.
21
- * @returns The p2wpkh payment object.
22
- * @throws {TypeError} If the required data is missing or invalid.
23
- */
24
- export function p2wpkh(a: Omit<P2WPKHPayment, 'name'>, opts?: PaymentOpts): P2WPKHPayment {
25
- if (!a.address && !a.hash && !a.output && !a.pubkey && !a.witness)
26
- throw new TypeError('Not enough data');
27
- opts = Object.assign({ validate: true }, opts || {});
28
-
29
- typef(
30
- {
31
- address: typef.maybe(typef.String),
32
- hash: typef.maybe(typef.BufferN(20)),
33
- input: typef.maybe(typef.BufferN(0)),
34
- network: typef.maybe(typef.Object),
35
- output: typef.maybe(typef.BufferN(22)),
36
- pubkey: typef.maybe(isPoint),
37
- signature: typef.maybe(bscript.isCanonicalScriptSignature),
38
- witness: typef.maybe(typef.arrayOf(typef.Buffer)),
39
- },
40
- a,
41
- );
42
-
43
- const _address = lazy.value(() => {
44
- const result = bech32.decode(a.address!);
45
- const version = result.words.shift();
46
- const data = bech32.fromWords(result.words);
47
- return {
48
- version,
49
- prefix: result.prefix,
50
- data: Buffer.from(data),
51
- };
52
- });
53
-
54
- const network = a.network || BITCOIN_NETWORK;
55
- const o: P2WPKHPayment = {
56
- name: PaymentType.P2WPKH,
57
- network
58
- };
59
-
60
- lazy.prop(o, 'address', () => {
61
- if (!o.hash) return;
62
-
63
- const words = bech32.toWords(o.hash);
64
- words.unshift(0x00);
65
- return bech32.encode(network.bech32, words);
66
- });
67
- lazy.prop(o, 'hash', () => {
68
- if (a.output) return a.output.slice(2, 22);
69
- if (a.address) return _address().data;
70
- if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!);
71
- });
72
- lazy.prop(o, 'output', () => {
73
- if (!o.hash) return;
74
- return bscript.compile([OPS.OP_0, o.hash]);
75
- });
76
- lazy.prop(o, 'pubkey', () => {
77
- if (a.pubkey) return a.pubkey;
78
- if (!a.witness) return;
79
- return a.witness[1];
80
- });
81
- lazy.prop(o, 'signature', () => {
82
- if (!a.witness) return;
83
- return a.witness[0];
84
- });
85
- lazy.prop(o, 'input', () => {
86
- if (!o.witness) return;
87
- return EMPTY_BUFFER;
88
- });
89
- lazy.prop(o, 'witness', () => {
90
- if (!a.pubkey) return;
91
- if (!a.signature) return;
92
- return [a.signature, a.pubkey];
93
- });
94
-
95
- // extended validation
96
- if (opts.validate) {
97
- let hash: Buffer = Buffer.from([]);
98
- if (a.address) {
99
- if (network && network.bech32 !== _address().prefix)
100
- throw new TypeError('Invalid prefix or Network mismatch');
101
- if (_address().version !== 0x00) throw new TypeError('Invalid address version');
102
- if (_address().data.length !== 20) throw new TypeError('Invalid address data');
103
- hash = _address().data;
104
- }
105
-
106
- if (a.hash) {
107
- if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch');
108
- else hash = a.hash;
109
- }
110
-
111
- if (a.output) {
112
- if (a.output.length !== 22 || a.output[0] !== OPS.OP_0 || a.output[1] !== 0x14)
113
- throw new TypeError('Output is invalid');
114
- if (hash.length > 0 && !hash.equals(a.output.slice(2)))
115
- throw new TypeError('Hash mismatch');
116
- else hash = a.output.slice(2);
117
- }
118
-
119
- if (a.pubkey) {
120
- const pkh = bcrypto.hash160(a.pubkey);
121
- if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch');
122
- else hash = pkh;
123
- if (!isPoint(a.pubkey) || a.pubkey.length !== 33)
124
- throw new TypeError('Invalid pubkey for p2wpkh');
125
- }
126
-
127
- if (a.witness) {
128
- if (a.witness.length !== 2) throw new TypeError('Witness is invalid');
129
- if (!bscript.isCanonicalScriptSignature(a.witness[0]))
130
- throw new TypeError('Witness has invalid signature');
131
- if (!isPoint(a.witness[1]) || a.witness[1].length !== 33)
132
- throw new TypeError('Witness has invalid pubkey');
133
-
134
- if (a.signature && !a.signature.equals(a.witness[0]))
135
- throw new TypeError('Signature mismatch');
136
- if (a.pubkey && !a.pubkey.equals(a.witness[1])) throw new TypeError('Pubkey mismatch');
137
-
138
- const pkh = bcrypto.hash160(a.witness[1]);
139
- if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch');
140
- }
141
- }
142
-
143
- return Object.assign(o, a);
144
- }
1
+ import { bech32 } from 'bech32';
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 { isPoint, typeforce as typef } from '../types.js';
6
+ import { P2WPKHPayment, PaymentOpts, PaymentType } from './index.js';
7
+ import * as lazy from './lazy.js';
8
+
9
+ const OPS = bscript.opcodes;
10
+
11
+ const EMPTY_BUFFER = Buffer.alloc(0);
12
+
13
+ // witness: {signature} {pubKey}
14
+ // input: <>
15
+ // output: OP_0 {pubKeyHash}
16
+ /**
17
+ * Creates a pay-to-witness-public-key-hash (p2wpkh) payment object.
18
+ *
19
+ * @param a - The payment object containing the necessary data.
20
+ * @param opts - Optional payment options.
21
+ * @returns The p2wpkh payment object.
22
+ * @throws {TypeError} If the required data is missing or invalid.
23
+ */
24
+ export function p2wpkh(a: Omit<P2WPKHPayment, 'name'>, opts?: PaymentOpts): P2WPKHPayment {
25
+ if (!a.address && !a.hash && !a.output && !a.pubkey && !a.witness)
26
+ throw new TypeError('Not enough data');
27
+ opts = Object.assign({ validate: true }, opts || {});
28
+
29
+ typef(
30
+ {
31
+ address: typef.maybe(typef.String),
32
+ hash: typef.maybe(typef.BufferN(20)),
33
+ input: typef.maybe(typef.BufferN(0)),
34
+ network: typef.maybe(typef.Object),
35
+ output: typef.maybe(typef.BufferN(22)),
36
+ pubkey: typef.maybe(isPoint),
37
+ signature: typef.maybe(bscript.isCanonicalScriptSignature),
38
+ witness: typef.maybe(typef.arrayOf(typef.Buffer)),
39
+ },
40
+ a,
41
+ );
42
+
43
+ const _address = lazy.value(() => {
44
+ const result = bech32.decode(a.address!);
45
+ const version = result.words.shift();
46
+ const data = bech32.fromWords(result.words);
47
+ return {
48
+ version,
49
+ prefix: result.prefix,
50
+ data: Buffer.from(data),
51
+ };
52
+ });
53
+
54
+ const network = a.network || BITCOIN_NETWORK;
55
+ const o: P2WPKHPayment = {
56
+ name: PaymentType.P2WPKH,
57
+ network,
58
+ };
59
+
60
+ lazy.prop(o, 'address', () => {
61
+ if (!o.hash) return;
62
+
63
+ const words = bech32.toWords(o.hash);
64
+ words.unshift(0x00);
65
+ return bech32.encode(network.bech32, words);
66
+ });
67
+ lazy.prop(o, 'hash', () => {
68
+ if (a.output) return a.output.slice(2, 22);
69
+ if (a.address) return _address().data;
70
+ if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!);
71
+ });
72
+ lazy.prop(o, 'output', () => {
73
+ if (!o.hash) return;
74
+ return bscript.compile([OPS.OP_0, o.hash]);
75
+ });
76
+ lazy.prop(o, 'pubkey', () => {
77
+ if (a.pubkey) return a.pubkey;
78
+ if (!a.witness) return;
79
+ return a.witness[1];
80
+ });
81
+ lazy.prop(o, 'signature', () => {
82
+ if (!a.witness) return;
83
+ return a.witness[0];
84
+ });
85
+ lazy.prop(o, 'input', () => {
86
+ if (!o.witness) return;
87
+ return EMPTY_BUFFER;
88
+ });
89
+ lazy.prop(o, 'witness', () => {
90
+ if (!a.pubkey) return;
91
+ if (!a.signature) return;
92
+ return [a.signature, a.pubkey];
93
+ });
94
+
95
+ // extended validation
96
+ if (opts.validate) {
97
+ let hash: Buffer = Buffer.from([]);
98
+ if (a.address) {
99
+ if (network && network.bech32 !== _address().prefix)
100
+ throw new TypeError('Invalid prefix or Network mismatch');
101
+ if (_address().version !== 0x00) throw new TypeError('Invalid address version');
102
+ if (_address().data.length !== 20) throw new TypeError('Invalid address data');
103
+ hash = _address().data;
104
+ }
105
+
106
+ if (a.hash) {
107
+ if (hash.length > 0 && !hash.equals(a.hash)) throw new TypeError('Hash mismatch');
108
+ else hash = a.hash;
109
+ }
110
+
111
+ if (a.output) {
112
+ if (a.output.length !== 22 || a.output[0] !== OPS.OP_0 || a.output[1] !== 0x14)
113
+ throw new TypeError('Output is invalid');
114
+ if (hash.length > 0 && !hash.equals(a.output.slice(2)))
115
+ throw new TypeError('Hash mismatch');
116
+ else hash = a.output.slice(2);
117
+ }
118
+
119
+ if (a.pubkey) {
120
+ const pkh = bcrypto.hash160(a.pubkey);
121
+ if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch');
122
+ else hash = pkh;
123
+ if (!isPoint(a.pubkey) || a.pubkey.length !== 33)
124
+ throw new TypeError('Invalid pubkey for p2wpkh');
125
+ }
126
+
127
+ if (a.witness) {
128
+ if (a.witness.length !== 2) throw new TypeError('Witness is invalid');
129
+ if (!bscript.isCanonicalScriptSignature(a.witness[0]))
130
+ throw new TypeError('Witness has invalid signature');
131
+ if (!isPoint(a.witness[1]) || a.witness[1].length !== 33)
132
+ throw new TypeError('Witness has invalid pubkey');
133
+
134
+ if (a.signature && !a.signature.equals(a.witness[0]))
135
+ throw new TypeError('Signature mismatch');
136
+ if (a.pubkey && !a.pubkey.equals(a.witness[1])) throw new TypeError('Pubkey mismatch');
137
+
138
+ const pkh = bcrypto.hash160(a.witness[1]);
139
+ if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch');
140
+ }
141
+ }
142
+
143
+ return Object.assign(o, a);
144
+ }