@btc-vision/bitcoin 6.4.1 → 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 (55) hide show
  1. package/browser/address.d.ts +1 -0
  2. package/browser/index.js +1 -1
  3. package/browser/payments/embed.d.ts +2 -2
  4. package/browser/payments/index.d.ts +61 -12
  5. package/browser/payments/lazy.d.ts +1 -1
  6. package/browser/payments/p2ms.d.ts +2 -2
  7. package/browser/payments/p2op.d.ts +25 -0
  8. package/browser/payments/p2pk.d.ts +2 -2
  9. package/browser/payments/p2pkh.d.ts +2 -2
  10. package/browser/payments/p2sh.d.ts +2 -2
  11. package/browser/payments/p2tr.d.ts +2 -2
  12. package/browser/payments/p2wpkh.d.ts +2 -2
  13. package/browser/payments/p2wsh.d.ts +2 -2
  14. package/browser/psbt/psbtutils.d.ts +1 -0
  15. package/build/address.js +4 -5
  16. package/build/payments/embed.d.ts +2 -2
  17. package/build/payments/embed.js +6 -2
  18. package/build/payments/index.d.ts +61 -12
  19. package/build/payments/index.js +1 -0
  20. package/build/payments/lazy.d.ts +1 -1
  21. package/build/payments/p2ms.d.ts +2 -2
  22. package/build/payments/p2ms.js +8 -2
  23. package/build/payments/p2op.d.ts +25 -0
  24. package/build/payments/p2op.js +112 -0
  25. package/build/payments/p2pk.d.ts +2 -2
  26. package/build/payments/p2pk.js +5 -1
  27. package/build/payments/p2pkh.d.ts +2 -2
  28. package/build/payments/p2pkh.js +5 -1
  29. package/build/payments/p2sh.d.ts +2 -2
  30. package/build/payments/p2sh.js +5 -1
  31. package/build/payments/p2tr.d.ts +2 -2
  32. package/build/payments/p2tr.js +5 -2
  33. package/build/payments/p2wpkh.d.ts +2 -2
  34. package/build/payments/p2wpkh.js +5 -1
  35. package/build/payments/p2wsh.d.ts +2 -2
  36. package/build/payments/p2wsh.js +5 -1
  37. package/build/psbt/psbtutils.d.ts +1 -0
  38. package/build/psbt/psbtutils.js +2 -0
  39. package/build/psbt.js +3 -1
  40. package/package.json +1 -1
  41. package/src/address.ts +283 -287
  42. package/src/payments/embed.ts +61 -55
  43. package/src/payments/index.ts +110 -13
  44. package/src/payments/lazy.ts +28 -28
  45. package/src/payments/p2ms.ts +11 -5
  46. package/src/payments/p2op.ts +170 -0
  47. package/src/payments/p2pk.ts +93 -85
  48. package/src/payments/p2pkh.ts +214 -210
  49. package/src/payments/p2sh.ts +211 -206
  50. package/src/payments/p2tr.ts +9 -4
  51. package/src/payments/p2wpkh.ts +7 -3
  52. package/src/payments/p2wsh.ts +7 -3
  53. package/src/psbt/psbtutils.ts +322 -320
  54. package/src/psbt.ts +5 -3
  55. package/test/payments.spec.ts +2 -2
@@ -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
+ }
@@ -1,210 +1,214 @@
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 { isPoint, typeforce as typef } from '../types.js';
6
- import { Payment, PaymentOpts, StackFunction } from './index.js';
7
- import * as lazy from './lazy.js';
8
- import { decompressPublicKey } from '../psbt/psbtutils.js';
9
-
10
- const OPS = bscript.OPS;
11
-
12
- // input: {signature} {pubkey}
13
- // output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
14
- /**
15
- * Creates a Pay-to-Public-Key-Hash (P2PKH) payment object.
16
- *
17
- * @param a - The payment object containing the necessary data.
18
- * @param opts - Optional payment options.
19
- * @returns The P2PKH payment object.
20
- * @throws {TypeError} If the required data is not provided or if the data is invalid.
21
- */
22
- export function p2pkh(a: Payment, opts?: PaymentOpts): Payment {
23
- if (!a.address && !a.hash && !a.output && !a.pubkey && !a.input) {
24
- throw new TypeError('Not enough data');
25
- }
26
-
27
- opts = Object.assign({ validate: true }, opts || {});
28
-
29
- typef(
30
- {
31
- network: typef.maybe(typef.Object),
32
- address: typef.maybe(typef.String),
33
- hash: typef.maybe(typef.BufferN(20)),
34
- output: typef.maybe(typef.BufferN(25)),
35
-
36
- pubkey: typef.maybe(isPoint),
37
- signature: typef.maybe(bscript.isCanonicalScriptSignature),
38
- input: typef.maybe(typef.Buffer),
39
- },
40
- a,
41
- );
42
-
43
- const _address = lazy.value(() => {
44
- const payload = Buffer.from(bs58check.default.decode(a.address!));
45
- const version = payload.readUInt8(0);
46
- const hash = payload.slice(1);
47
- return { version, hash };
48
- });
49
-
50
- const _chunks = lazy.value(() => {
51
- return bscript.decompile(a.input!);
52
- }) as StackFunction;
53
-
54
- const network = a.network || BITCOIN_NETWORK;
55
- const o: Payment = { name: 'p2pkh', network };
56
-
57
- lazy.prop(o, 'address', () => {
58
- if (!o.hash) return;
59
-
60
- const payload = Buffer.allocUnsafe(21);
61
- payload.writeUInt8(network.pubKeyHash, 0);
62
- o.hash.copy(payload, 1);
63
- return bs58check.default.encode(payload);
64
- });
65
-
66
- lazy.prop(o, 'hash', () => {
67
- if (a.output) return a.output.slice(3, 23);
68
- if (a.address) return _address().hash;
69
- if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!);
70
- });
71
-
72
- lazy.prop(o, 'output', () => {
73
- if (!o.hash) return;
74
- return bscript.compile([
75
- OPS.OP_DUP,
76
- OPS.OP_HASH160,
77
- o.hash,
78
- OPS.OP_EQUALVERIFY,
79
- OPS.OP_CHECKSIG,
80
- ]);
81
- });
82
-
83
- lazy.prop(o, 'pubkey', () => {
84
- if (!a.input) return;
85
- return _chunks()[1] as Buffer;
86
- });
87
-
88
- lazy.prop(o, 'signature', () => {
89
- if (!a.input) return;
90
- return _chunks()[0] as Buffer;
91
- });
92
-
93
- lazy.prop(o, 'input', () => {
94
- if (!a.pubkey) return;
95
- if (!a.signature) return;
96
-
97
- let pubKey: Buffer = a.pubkey;
98
- if (a.useHybrid || a.useUncompressed) {
99
- const decompressed = decompressPublicKey(a.pubkey);
100
- if (decompressed) {
101
- if (a.useUncompressed) {
102
- pubKey = decompressed.uncompressed;
103
- } else {
104
- pubKey = decompressed.hybrid;
105
- }
106
- }
107
- }
108
-
109
- return bscript.compile([a.signature, pubKey]);
110
- });
111
-
112
- lazy.prop(o, 'witness', () => {
113
- if (!o.input) return;
114
- return [];
115
- });
116
-
117
- // extended validation
118
- if (opts.validate) {
119
- let hash: Buffer = Buffer.from([]);
120
- if (a.address) {
121
- if (_address().version !== network.pubKeyHash) {
122
- throw new TypeError('Invalid version or Network mismatch');
123
- }
124
-
125
- if (_address().hash.length !== 20) {
126
- throw new TypeError('Invalid address');
127
- }
128
-
129
- hash = _address().hash;
130
- }
131
-
132
- if (a.hash) {
133
- if (hash.length > 0 && !hash.equals(a.hash)) {
134
- throw new TypeError('Hash mismatch');
135
- } else {
136
- hash = a.hash;
137
- }
138
- }
139
-
140
- if (a.output) {
141
- if (
142
- a.output.length !== 25 ||
143
- a.output[0] !== OPS.OP_DUP ||
144
- a.output[1] !== OPS.OP_HASH160 ||
145
- a.output[2] !== 0x14 ||
146
- a.output[23] !== OPS.OP_EQUALVERIFY ||
147
- a.output[24] !== OPS.OP_CHECKSIG
148
- ) {
149
- throw new TypeError('Output is invalid');
150
- }
151
-
152
- const hash2 = a.output.slice(3, 23);
153
- if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
154
- else hash = hash2;
155
- }
156
-
157
- if (a.pubkey) {
158
- const pkh = bcrypto.hash160(a.pubkey);
159
-
160
- let badHash = hash.length > 0 && !hash.equals(pkh);
161
- if (badHash) {
162
- if (
163
- (a.pubkey.length === 33 && (a.pubkey[0] === 0x02 || a.pubkey[0] === 0x03)) ||
164
- (a.pubkey.length === 65 && a.pubkey[0] === 0x04)
165
- ) {
166
- const uncompressed = decompressPublicKey(a.pubkey);
167
- if (uncompressed) {
168
- const pkh2 = bcrypto.hash160(uncompressed.uncompressed);
169
-
170
- if (!hash.equals(pkh2)) {
171
- const pkh3 = bcrypto.hash160(uncompressed.hybrid);
172
- badHash = !hash.equals(pkh3);
173
-
174
- if (!badHash) {
175
- a.useHybrid = true;
176
- }
177
- } else {
178
- badHash = false;
179
- a.useUncompressed = true;
180
- }
181
- }
182
- }
183
- }
184
-
185
- if (badHash) {
186
- throw new TypeError('Hash mismatch');
187
- } else {
188
- hash = pkh;
189
- }
190
- }
191
-
192
- if (a.input) {
193
- const chunks = _chunks();
194
- if (chunks.length !== 2) throw new TypeError('Input is invalid');
195
- if (!bscript.isCanonicalScriptSignature(chunks[0] as Buffer))
196
- throw new TypeError('Input has invalid signature');
197
- if (!isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey');
198
-
199
- if (a.signature && !a.signature.equals(chunks[0] as Buffer))
200
- throw new TypeError('Signature mismatch');
201
- if (a.pubkey && !a.pubkey.equals(chunks[1] as Buffer))
202
- throw new TypeError('Pubkey mismatch');
203
-
204
- const pkh = bcrypto.hash160(chunks[1] as Buffer);
205
- if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch (input)');
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 { isPoint, typeforce as typef } from '../types.js';
6
+ import { P2PKHPayment, PaymentOpts, StackFunction } from './index.js';
7
+ import * as lazy from './lazy.js';
8
+ import { decompressPublicKey } from '../psbt/psbtutils.js';
9
+
10
+ const OPS = bscript.OPS;
11
+
12
+ // input: {signature} {pubkey}
13
+ // output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
14
+ /**
15
+ * Creates a Pay-to-Public-Key-Hash (P2PKH) payment object.
16
+ *
17
+ * @param a - The payment object containing the necessary data.
18
+ * @param opts - Optional payment options.
19
+ * @returns The P2PKH payment object.
20
+ * @throws {TypeError} If the required data is not provided or if the data is invalid.
21
+ */
22
+ export function p2pkh(a: Omit<P2PKHPayment, 'name'>, opts?: PaymentOpts): P2PKHPayment {
23
+ if (!a.address && !a.hash && !a.output && !a.pubkey && !a.input) {
24
+ throw new TypeError('Not enough data');
25
+ }
26
+
27
+ opts = Object.assign({ validate: true }, opts || {});
28
+
29
+ typef(
30
+ {
31
+ network: typef.maybe(typef.Object),
32
+ address: typef.maybe(typef.String),
33
+ hash: typef.maybe(typef.BufferN(20)),
34
+ output: typef.maybe(typef.BufferN(25)),
35
+
36
+ pubkey: typef.maybe(isPoint),
37
+ signature: typef.maybe(bscript.isCanonicalScriptSignature),
38
+ input: typef.maybe(typef.Buffer),
39
+ },
40
+ a,
41
+ );
42
+
43
+ const _address = lazy.value(() => {
44
+ const payload = Buffer.from(bs58check.default.decode(a.address!));
45
+ const version = payload.readUInt8(0);
46
+ const hash = payload.slice(1);
47
+ return { version, hash };
48
+ });
49
+
50
+ const _chunks = lazy.value(() => {
51
+ return bscript.decompile(a.input!);
52
+ }) as StackFunction;
53
+
54
+ const network = a.network || BITCOIN_NETWORK;
55
+ const o: P2PKHPayment = {
56
+ name: 'p2pkh',
57
+ network,
58
+ hash: undefined,
59
+ };
60
+
61
+ lazy.prop(o, 'address', () => {
62
+ if (!o.hash) return;
63
+
64
+ const payload = Buffer.allocUnsafe(21);
65
+ payload.writeUInt8(network.pubKeyHash, 0);
66
+ o.hash.copy(payload, 1);
67
+ return bs58check.default.encode(payload);
68
+ });
69
+
70
+ lazy.prop(o, 'hash', () => {
71
+ if (a.output) return a.output.slice(3, 23);
72
+ if (a.address) return _address().hash;
73
+ if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!);
74
+ });
75
+
76
+ lazy.prop(o, 'output', () => {
77
+ if (!o.hash) return;
78
+ return bscript.compile([
79
+ OPS.OP_DUP,
80
+ OPS.OP_HASH160,
81
+ o.hash,
82
+ OPS.OP_EQUALVERIFY,
83
+ OPS.OP_CHECKSIG,
84
+ ]);
85
+ });
86
+
87
+ lazy.prop(o, 'pubkey', () => {
88
+ if (!a.input) return;
89
+ return _chunks()[1] as Buffer;
90
+ });
91
+
92
+ lazy.prop(o, 'signature', () => {
93
+ if (!a.input) return;
94
+ return _chunks()[0] as Buffer;
95
+ });
96
+
97
+ lazy.prop(o, 'input', () => {
98
+ if (!a.pubkey) return;
99
+ if (!a.signature) return;
100
+
101
+ let pubKey: Buffer = a.pubkey;
102
+ if (a.useHybrid || a.useUncompressed) {
103
+ const decompressed = decompressPublicKey(a.pubkey);
104
+ if (decompressed) {
105
+ if (a.useUncompressed) {
106
+ pubKey = decompressed.uncompressed;
107
+ } else {
108
+ pubKey = decompressed.hybrid;
109
+ }
110
+ }
111
+ }
112
+
113
+ return bscript.compile([a.signature, pubKey]);
114
+ });
115
+
116
+ lazy.prop(o, 'witness', () => {
117
+ if (!o.input) return;
118
+ return [];
119
+ });
120
+
121
+ // extended validation
122
+ if (opts.validate) {
123
+ let hash: Buffer = Buffer.from([]);
124
+ if (a.address) {
125
+ if (_address().version !== network.pubKeyHash) {
126
+ throw new TypeError('Invalid version or Network mismatch');
127
+ }
128
+
129
+ if (_address().hash.length !== 20) {
130
+ throw new TypeError('Invalid address');
131
+ }
132
+
133
+ hash = _address().hash;
134
+ }
135
+
136
+ if (a.hash) {
137
+ if (hash.length > 0 && !hash.equals(a.hash)) {
138
+ throw new TypeError('Hash mismatch');
139
+ } else {
140
+ hash = a.hash;
141
+ }
142
+ }
143
+
144
+ if (a.output) {
145
+ if (
146
+ a.output.length !== 25 ||
147
+ a.output[0] !== OPS.OP_DUP ||
148
+ a.output[1] !== OPS.OP_HASH160 ||
149
+ a.output[2] !== 0x14 ||
150
+ a.output[23] !== OPS.OP_EQUALVERIFY ||
151
+ a.output[24] !== OPS.OP_CHECKSIG
152
+ ) {
153
+ throw new TypeError('Output is invalid');
154
+ }
155
+
156
+ const hash2 = a.output.slice(3, 23);
157
+ if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
158
+ else hash = hash2;
159
+ }
160
+
161
+ if (a.pubkey) {
162
+ const pkh = bcrypto.hash160(a.pubkey);
163
+
164
+ let badHash = hash.length > 0 && !hash.equals(pkh);
165
+ if (badHash) {
166
+ if (
167
+ (a.pubkey.length === 33 && (a.pubkey[0] === 0x02 || a.pubkey[0] === 0x03)) ||
168
+ (a.pubkey.length === 65 && a.pubkey[0] === 0x04)
169
+ ) {
170
+ const uncompressed = decompressPublicKey(a.pubkey);
171
+ if (uncompressed) {
172
+ const pkh2 = bcrypto.hash160(uncompressed.uncompressed);
173
+
174
+ if (!hash.equals(pkh2)) {
175
+ const pkh3 = bcrypto.hash160(uncompressed.hybrid);
176
+ badHash = !hash.equals(pkh3);
177
+
178
+ if (!badHash) {
179
+ a.useHybrid = true;
180
+ }
181
+ } else {
182
+ badHash = false;
183
+ a.useUncompressed = true;
184
+ }
185
+ }
186
+ }
187
+ }
188
+
189
+ if (badHash) {
190
+ throw new TypeError('Hash mismatch');
191
+ } else {
192
+ hash = pkh;
193
+ }
194
+ }
195
+
196
+ if (a.input) {
197
+ const chunks = _chunks();
198
+ if (chunks.length !== 2) throw new TypeError('Input is invalid');
199
+ if (!bscript.isCanonicalScriptSignature(chunks[0] as Buffer))
200
+ throw new TypeError('Input has invalid signature');
201
+ if (!isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey');
202
+
203
+ if (a.signature && !a.signature.equals(chunks[0] as Buffer))
204
+ throw new TypeError('Signature mismatch');
205
+ if (a.pubkey && !a.pubkey.equals(chunks[1] as Buffer))
206
+ throw new TypeError('Pubkey mismatch');
207
+
208
+ const pkh = bcrypto.hash160(chunks[1] as Buffer);
209
+ if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch (input)');
210
+ }
211
+ }
212
+
213
+ return Object.assign(o, a);
214
+ }