@btc-vision/bitcoin 6.3.3 → 6.3.5

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.
@@ -76,11 +76,13 @@ export function p2pkh(a, opts) {
76
76
  let pubKey = a.pubkey;
77
77
  if (a.useHybrid || a.useUncompressed) {
78
78
  const decompressed = decompressPublicKey(a.pubkey);
79
- if (a.useUncompressed) {
80
- pubKey = decompressed.uncompressed;
81
- }
82
- else {
83
- pubKey = decompressed.hybrid;
79
+ if (decompressed) {
80
+ if (a.useUncompressed) {
81
+ pubKey = decompressed.uncompressed;
82
+ }
83
+ else {
84
+ pubKey = decompressed.hybrid;
85
+ }
84
86
  }
85
87
  }
86
88
  return bscript.compile([a.signature, pubKey]);
@@ -131,17 +133,19 @@ export function p2pkh(a, opts) {
131
133
  if ((a.pubkey.length === 33 && (a.pubkey[0] === 0x02 || a.pubkey[0] === 0x03)) ||
132
134
  (a.pubkey.length === 65 && a.pubkey[0] === 0x04)) {
133
135
  const uncompressed = decompressPublicKey(a.pubkey);
134
- const pkh2 = bcrypto.hash160(uncompressed.uncompressed);
135
- if (!hash.equals(pkh2)) {
136
- const pkh3 = bcrypto.hash160(uncompressed.hybrid);
137
- badHash = !hash.equals(pkh3);
138
- if (!badHash) {
139
- a.useHybrid = true;
136
+ if (uncompressed) {
137
+ const pkh2 = bcrypto.hash160(uncompressed.uncompressed);
138
+ if (!hash.equals(pkh2)) {
139
+ const pkh3 = bcrypto.hash160(uncompressed.hybrid);
140
+ badHash = !hash.equals(pkh3);
141
+ if (!badHash) {
142
+ a.useHybrid = true;
143
+ }
144
+ }
145
+ else {
146
+ badHash = false;
147
+ a.useUncompressed = true;
140
148
  }
141
- }
142
- else {
143
- badHash = false;
144
- a.useUncompressed = true;
145
149
  }
146
150
  }
147
151
  }
@@ -11,7 +11,7 @@ export interface UncompressedPublicKey {
11
11
  hybrid: Buffer;
12
12
  uncompressed: Buffer;
13
13
  }
14
- export declare function decompressPublicKey(realPubKey: Uint8Array | Buffer): UncompressedPublicKey;
14
+ export declare function decompressPublicKey(realPubKey: Uint8Array | Buffer): UncompressedPublicKey | undefined;
15
15
  export declare function bigIntTo32Bytes(num: bigint): Buffer;
16
16
  export declare function pubkeysMatch(a: Buffer, b: Buffer): boolean;
17
17
  export declare function pubkeyPositionInScript(pubkey: Buffer, script: Buffer): number;
@@ -52,7 +52,11 @@ export function witnessStackToScriptWitness(witness) {
52
52
  return buffer;
53
53
  }
54
54
  export function decompressPublicKey(realPubKey) {
55
+ if (realPubKey.length === 32) {
56
+ return;
57
+ }
55
58
  if (![33, 65].includes(realPubKey.length)) {
59
+ console.trace(`Unsupported key length=${realPubKey.length}. Must be 33 (compressed) or 65 (uncompressed).`);
56
60
  throw new Error(`Unsupported key length=${realPubKey.length}. Must be 33 (compressed) or 65 (uncompressed).`);
57
61
  }
58
62
  let point;
@@ -105,8 +109,10 @@ export function pubkeyPositionInScript(pubkey, script) {
105
109
  const pubkeyHash = hash160(pubkey);
106
110
  const pubkeyXOnly = toXOnly(pubkey);
107
111
  const uncompressed = decompressPublicKey(pubkey);
108
- const pubkeyHybridHash = hash160(uncompressed.hybrid);
109
- const pubkeyUncompressedHash = hash160(uncompressed.uncompressed);
112
+ const pubkeyHybridHash = uncompressed?.hybrid ? hash160(uncompressed.hybrid) : undefined;
113
+ const pubkeyUncompressedHash = uncompressed?.uncompressed
114
+ ? hash160(uncompressed.uncompressed)
115
+ : undefined;
110
116
  return decompiled.findIndex((element) => {
111
117
  if (typeof element === 'number')
112
118
  return false;
@@ -117,12 +123,15 @@ export function pubkeyPositionInScript(pubkey, script) {
117
123
  if (element.equals(pubkeyHash)) {
118
124
  return true;
119
125
  }
120
- if (pubkeysMatch(element, uncompressed.uncompressed))
121
- return true;
122
- if (pubkeysMatch(element, uncompressed.hybrid))
123
- return true;
124
- if (element.equals(pubkeyHybridHash) || element.equals(pubkeyUncompressedHash)) {
125
- return true;
126
+ if (uncompressed) {
127
+ if (pubkeysMatch(element, uncompressed.uncompressed))
128
+ return true;
129
+ if (pubkeysMatch(element, uncompressed.hybrid))
130
+ return true;
131
+ if ((pubkeyHybridHash && element.equals(pubkeyHybridHash)) ||
132
+ (pubkeyUncompressedHash && element.equals(pubkeyUncompressedHash))) {
133
+ return true;
134
+ }
126
135
  }
127
136
  });
128
137
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@btc-vision/bitcoin",
3
3
  "type": "module",
4
- "version": "6.3.3",
4
+ "version": "6.3.5",
5
5
  "description": "Client-side Bitcoin JavaScript library",
6
6
  "engines": {
7
7
  "node": ">=16.0.0"
@@ -91,7 +91,6 @@
91
91
  "@types/proxyquire": "^1.3.31",
92
92
  "@types/randombytes": "^2.0.3",
93
93
  "better-npm-audit": "^3.11.0",
94
- "bip32": "^4.0.0",
95
94
  "bip39": "^3.1.0",
96
95
  "bip65": "^1.0.3",
97
96
  "bip68": "^1.0.4",
@@ -135,6 +134,7 @@
135
134
  "babel-plugin-transform-import-meta": "^2.2.1",
136
135
  "babel-preset-react": "^6.24.1",
137
136
  "babelify": "^10.0.0",
137
+ "bip32": "^4.0.0",
138
138
  "bech32": "^2.0.0",
139
139
  "bip174": "^2.1.1",
140
140
  "browserify-zlib": "^0.2.0",
@@ -1,206 +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 { 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 (a.useUncompressed) {
101
- pubKey = decompressed.uncompressed;
102
- } else {
103
- pubKey = decompressed.hybrid;
104
- }
105
- }
106
-
107
- return bscript.compile([a.signature, pubKey]);
108
- });
109
-
110
- lazy.prop(o, 'witness', () => {
111
- if (!o.input) return;
112
- return [];
113
- });
114
-
115
- // extended validation
116
- if (opts.validate) {
117
- let hash: Buffer = Buffer.from([]);
118
- if (a.address) {
119
- if (_address().version !== network.pubKeyHash) {
120
- throw new TypeError('Invalid version or Network mismatch');
121
- }
122
-
123
- if (_address().hash.length !== 20) {
124
- throw new TypeError('Invalid address');
125
- }
126
-
127
- hash = _address().hash;
128
- }
129
-
130
- if (a.hash) {
131
- if (hash.length > 0 && !hash.equals(a.hash)) {
132
- throw new TypeError('Hash mismatch');
133
- } else {
134
- hash = a.hash;
135
- }
136
- }
137
-
138
- if (a.output) {
139
- if (
140
- a.output.length !== 25 ||
141
- a.output[0] !== OPS.OP_DUP ||
142
- a.output[1] !== OPS.OP_HASH160 ||
143
- a.output[2] !== 0x14 ||
144
- a.output[23] !== OPS.OP_EQUALVERIFY ||
145
- a.output[24] !== OPS.OP_CHECKSIG
146
- ) {
147
- throw new TypeError('Output is invalid');
148
- }
149
-
150
- const hash2 = a.output.slice(3, 23);
151
- if (hash.length > 0 && !hash.equals(hash2)) throw new TypeError('Hash mismatch');
152
- else hash = hash2;
153
- }
154
-
155
- if (a.pubkey) {
156
- const pkh = bcrypto.hash160(a.pubkey);
157
-
158
- let badHash = hash.length > 0 && !hash.equals(pkh);
159
- if (badHash) {
160
- if (
161
- (a.pubkey.length === 33 && (a.pubkey[0] === 0x02 || a.pubkey[0] === 0x03)) ||
162
- (a.pubkey.length === 65 && a.pubkey[0] === 0x04)
163
- ) {
164
- const uncompressed = decompressPublicKey(a.pubkey);
165
- const pkh2 = bcrypto.hash160(uncompressed.uncompressed);
166
-
167
- if (!hash.equals(pkh2)) {
168
- const pkh3 = bcrypto.hash160(uncompressed.hybrid);
169
- badHash = !hash.equals(pkh3);
170
-
171
- if (!badHash) {
172
- a.useHybrid = true;
173
- }
174
- } else {
175
- badHash = false;
176
- a.useUncompressed = true;
177
- }
178
- }
179
- }
180
-
181
- if (badHash) {
182
- throw new TypeError('Hash mismatch');
183
- } else {
184
- hash = pkh;
185
- }
186
- }
187
-
188
- if (a.input) {
189
- const chunks = _chunks();
190
- if (chunks.length !== 2) throw new TypeError('Input is invalid');
191
- if (!bscript.isCanonicalScriptSignature(chunks[0] as Buffer))
192
- throw new TypeError('Input has invalid signature');
193
- if (!isPoint(chunks[1])) throw new TypeError('Input has invalid pubkey');
194
-
195
- if (a.signature && !a.signature.equals(chunks[0] as Buffer))
196
- throw new TypeError('Signature mismatch');
197
- if (a.pubkey && !a.pubkey.equals(chunks[1] as Buffer))
198
- throw new TypeError('Pubkey mismatch');
199
-
200
- const pkh = bcrypto.hash160(chunks[1] as Buffer);
201
- if (hash.length > 0 && !hash.equals(pkh)) throw new TypeError('Hash mismatch (input)');
202
- }
203
- }
204
-
205
- return Object.assign(o, a);
206
- }
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
+ }