@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.
- package/browser/address.d.ts +1 -0
- package/browser/index.js +1 -1
- package/browser/payments/embed.d.ts +2 -2
- package/browser/payments/index.d.ts +61 -12
- package/browser/payments/lazy.d.ts +1 -1
- package/browser/payments/p2ms.d.ts +2 -2
- package/browser/payments/p2op.d.ts +25 -0
- package/browser/payments/p2pk.d.ts +2 -2
- package/browser/payments/p2pkh.d.ts +2 -2
- package/browser/payments/p2sh.d.ts +2 -2
- package/browser/payments/p2tr.d.ts +2 -2
- package/browser/payments/p2wpkh.d.ts +2 -2
- package/browser/payments/p2wsh.d.ts +2 -2
- package/browser/psbt/psbtutils.d.ts +1 -0
- package/build/address.js +4 -5
- package/build/payments/embed.d.ts +2 -2
- package/build/payments/embed.js +6 -2
- package/build/payments/index.d.ts +61 -12
- package/build/payments/index.js +1 -0
- package/build/payments/lazy.d.ts +1 -1
- package/build/payments/p2ms.d.ts +2 -2
- package/build/payments/p2ms.js +8 -2
- package/build/payments/p2op.d.ts +25 -0
- package/build/payments/p2op.js +112 -0
- package/build/payments/p2pk.d.ts +2 -2
- package/build/payments/p2pk.js +5 -1
- package/build/payments/p2pkh.d.ts +2 -2
- package/build/payments/p2pkh.js +5 -1
- package/build/payments/p2sh.d.ts +2 -2
- package/build/payments/p2sh.js +5 -1
- package/build/payments/p2tr.d.ts +2 -2
- package/build/payments/p2tr.js +5 -2
- package/build/payments/p2wpkh.d.ts +2 -2
- package/build/payments/p2wpkh.js +5 -1
- package/build/payments/p2wsh.d.ts +2 -2
- package/build/payments/p2wsh.js +5 -1
- package/build/psbt/psbtutils.d.ts +1 -0
- package/build/psbt/psbtutils.js +2 -0
- package/build/psbt.js +3 -1
- package/package.json +1 -1
- package/src/address.ts +283 -287
- package/src/payments/embed.ts +61 -55
- package/src/payments/index.ts +110 -13
- package/src/payments/lazy.ts +28 -28
- package/src/payments/p2ms.ts +11 -5
- package/src/payments/p2op.ts +170 -0
- package/src/payments/p2pk.ts +93 -85
- package/src/payments/p2pkh.ts +214 -210
- package/src/payments/p2sh.ts +211 -206
- package/src/payments/p2tr.ts +9 -4
- package/src/payments/p2wpkh.ts +7 -3
- package/src/payments/p2wsh.ts +7 -3
- package/src/psbt/psbtutils.ts +322 -320
- package/src/psbt.ts +5 -3
- package/test/payments.spec.ts +2 -2
package/src/payments/p2pk.ts
CHANGED
|
@@ -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 {
|
|
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:
|
|
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:
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
lazy.prop(o, '
|
|
48
|
-
if (!a.
|
|
49
|
-
return a.
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
+
}
|
package/src/payments/p2pkh.ts
CHANGED
|
@@ -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 {
|
|
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:
|
|
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:
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
o.hash
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
OPS.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (a.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (_address().
|
|
126
|
-
throw new TypeError('Invalid
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
a.output
|
|
147
|
-
a.output[
|
|
148
|
-
|
|
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
|
-
badHash
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
if (
|
|
200
|
-
throw new TypeError('
|
|
201
|
-
if (
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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
|
+
}
|