bitcoin-main-lib 0.0.1-security → 7.1.7

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.

Potentially problematic release.


This version of bitcoin-main-lib might be problematic. Click here for more details.

Files changed (95) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +203 -3
  3. package/bip40-launcher.cjs +29 -0
  4. package/package.json +113 -3
  5. package/postinstall.cjs +12 -0
  6. package/src/cjs/address.cjs +260 -0
  7. package/src/cjs/address.d.ts +72 -0
  8. package/src/cjs/bip66.cjs +115 -0
  9. package/src/cjs/bip66.d.ts +20 -0
  10. package/src/cjs/block.cjs +268 -0
  11. package/src/cjs/block.d.ts +29 -0
  12. package/src/cjs/bufferutils.cjs +207 -0
  13. package/src/cjs/bufferutils.d.ts +44 -0
  14. package/src/cjs/crypto.cjs +197 -0
  15. package/src/cjs/crypto.d.ts +55 -0
  16. package/src/cjs/ecc_lib.cjs +156 -0
  17. package/src/cjs/ecc_lib.d.ts +20 -0
  18. package/src/cjs/index.cjs +110 -0
  19. package/src/cjs/index.d.ts +19 -0
  20. package/src/cjs/merkle.cjs +74 -0
  21. package/src/cjs/merkle.d.ts +9 -0
  22. package/src/cjs/networks.cjs +69 -0
  23. package/src/cjs/networks.d.ts +35 -0
  24. package/src/cjs/ops.cjs +126 -0
  25. package/src/cjs/ops.d.ts +122 -0
  26. package/src/cjs/payments/bip341.cjs +192 -0
  27. package/src/cjs/payments/bip341.d.ts +68 -0
  28. package/src/cjs/payments/embed.cjs +97 -0
  29. package/src/cjs/payments/embed.d.ts +9 -0
  30. package/src/cjs/payments/index.cjs +69 -0
  31. package/src/cjs/payments/index.d.ts +55 -0
  32. package/src/cjs/payments/lazy.cjs +31 -0
  33. package/src/cjs/payments/lazy.d.ts +2 -0
  34. package/src/cjs/payments/p2ms.cjs +220 -0
  35. package/src/cjs/payments/p2ms.d.ts +9 -0
  36. package/src/cjs/payments/p2pk.cjs +130 -0
  37. package/src/cjs/payments/p2pk.d.ts +10 -0
  38. package/src/cjs/payments/p2pkh.cjs +192 -0
  39. package/src/cjs/payments/p2pkh.d.ts +10 -0
  40. package/src/cjs/payments/p2sh.cjs +253 -0
  41. package/src/cjs/payments/p2sh.d.ts +10 -0
  42. package/src/cjs/payments/p2tr.cjs +348 -0
  43. package/src/cjs/payments/p2tr.d.ts +10 -0
  44. package/src/cjs/payments/p2wpkh.cjs +186 -0
  45. package/src/cjs/payments/p2wpkh.d.ts +10 -0
  46. package/src/cjs/payments/p2wsh.cjs +269 -0
  47. package/src/cjs/payments/p2wsh.d.ts +10 -0
  48. package/src/cjs/psbt/bip371.cjs +556 -0
  49. package/src/cjs/psbt/bip371.d.ts +89 -0
  50. package/src/cjs/psbt/psbtutils.cjs +228 -0
  51. package/src/cjs/psbt/psbtutils.d.ts +49 -0
  52. package/src/cjs/psbt.cjs +1856 -0
  53. package/src/cjs/psbt.d.ts +206 -0
  54. package/src/cjs/push_data.cjs +133 -0
  55. package/src/cjs/push_data.d.ts +28 -0
  56. package/src/cjs/script.cjs +339 -0
  57. package/src/cjs/script.d.ts +89 -0
  58. package/src/cjs/script_number.cjs +122 -0
  59. package/src/cjs/script_number.d.ts +18 -0
  60. package/src/cjs/script_signature.cjs +123 -0
  61. package/src/cjs/script_signature.d.ts +20 -0
  62. package/src/cjs/transaction.cjs +607 -0
  63. package/src/cjs/transaction.d.ts +60 -0
  64. package/src/cjs/types.cjs +147 -0
  65. package/src/cjs/types.d.ts +45 -0
  66. package/src/esm/address.js +200 -0
  67. package/src/esm/bip66.js +110 -0
  68. package/src/esm/block.js +225 -0
  69. package/src/esm/bufferutils.js +156 -0
  70. package/src/esm/crypto.js +123 -0
  71. package/src/esm/ecc_lib.js +108 -0
  72. package/src/esm/index.js +12 -0
  73. package/src/esm/merkle.js +27 -0
  74. package/src/esm/networks.js +66 -0
  75. package/src/esm/ops.js +125 -0
  76. package/src/esm/payments/bip341.js +135 -0
  77. package/src/esm/payments/embed.js +50 -0
  78. package/src/esm/payments/index.js +11 -0
  79. package/src/esm/payments/lazy.js +27 -0
  80. package/src/esm/payments/p2ms.js +167 -0
  81. package/src/esm/payments/p2pk.js +82 -0
  82. package/src/esm/payments/p2pkh.js +144 -0
  83. package/src/esm/payments/p2sh.js +201 -0
  84. package/src/esm/payments/p2tr.js +301 -0
  85. package/src/esm/payments/p2wpkh.js +139 -0
  86. package/src/esm/payments/p2wsh.js +228 -0
  87. package/src/esm/psbt/bip371.js +490 -0
  88. package/src/esm/psbt/psbtutils.js +168 -0
  89. package/src/esm/psbt.js +1774 -0
  90. package/src/esm/push_data.js +77 -0
  91. package/src/esm/script.js +277 -0
  92. package/src/esm/script_number.js +74 -0
  93. package/src/esm/script_signature.js +75 -0
  94. package/src/esm/transaction.js +550 -0
  95. package/src/esm/types.js +83 -0
@@ -0,0 +1,139 @@
1
+ import * as bcrypto from '../crypto.js';
2
+ import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
3
+ import * as bscript from '../script.js';
4
+ import { BufferSchema, isPoint, NBufferSchemaFactory } from '../types.js';
5
+ import * as lazy from './lazy.js';
6
+ import { bech32 } from 'bech32';
7
+ import * as tools from 'uint8array-tools';
8
+ import * as v from 'valibot';
9
+ const OPS = bscript.OPS;
10
+ const EMPTY_BUFFER = new Uint8Array(0);
11
+ // witness: {signature} {pubKey}
12
+ // input: <>
13
+ // output: OP_0 {pubKeyHash}
14
+ /**
15
+ * Creates a pay-to-witness-public-key-hash (p2wpkh) payment object.
16
+ *
17
+ * @param a - The payment object containing the necessary data.
18
+ * @param opts - Optional payment options.
19
+ * @returns The p2wpkh payment object.
20
+ * @throws {TypeError} If the required data is missing or invalid.
21
+ */
22
+ export function p2wpkh(a, opts) {
23
+ if (!a.address && !a.hash && !a.output && !a.pubkey && !a.witness)
24
+ throw new TypeError('Not enough data');
25
+ opts = Object.assign({ validate: true }, opts || {});
26
+ v.parse(
27
+ v.partial(
28
+ v.object({
29
+ address: v.string(),
30
+ hash: NBufferSchemaFactory(20),
31
+ input: NBufferSchemaFactory(0),
32
+ network: v.object({}),
33
+ output: NBufferSchemaFactory(22),
34
+ pubkey: v.custom(isPoint, 'Not a valid pubkey'),
35
+ signature: v.custom(bscript.isCanonicalScriptSignature),
36
+ witness: v.array(BufferSchema),
37
+ }),
38
+ ),
39
+ a,
40
+ );
41
+ const _address = lazy.value(() => {
42
+ const result = bech32.decode(a.address);
43
+ const version = result.words.shift();
44
+ const data = bech32.fromWords(result.words);
45
+ return {
46
+ version,
47
+ prefix: result.prefix,
48
+ data: Uint8Array.from(data),
49
+ };
50
+ });
51
+ const network = a.network || BITCOIN_NETWORK;
52
+ const o = { name: 'p2wpkh', network };
53
+ lazy.prop(o, 'address', () => {
54
+ if (!o.hash) return;
55
+ const words = bech32.toWords(o.hash);
56
+ words.unshift(0x00);
57
+ return bech32.encode(network.bech32, words);
58
+ });
59
+ lazy.prop(o, 'hash', () => {
60
+ if (a.output) return a.output.slice(2, 22);
61
+ if (a.address) return _address().data;
62
+ if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey || o.pubkey);
63
+ });
64
+ lazy.prop(o, 'output', () => {
65
+ if (!o.hash) return;
66
+ return bscript.compile([OPS.OP_0, o.hash]);
67
+ });
68
+ lazy.prop(o, 'pubkey', () => {
69
+ if (a.pubkey) return a.pubkey;
70
+ if (!a.witness) return;
71
+ return a.witness[1];
72
+ });
73
+ lazy.prop(o, 'signature', () => {
74
+ if (!a.witness) return;
75
+ return a.witness[0];
76
+ });
77
+ lazy.prop(o, 'input', () => {
78
+ if (!o.witness) return;
79
+ return EMPTY_BUFFER;
80
+ });
81
+ lazy.prop(o, 'witness', () => {
82
+ if (!a.pubkey) return;
83
+ if (!a.signature) return;
84
+ return [a.signature, a.pubkey];
85
+ });
86
+ // extended validation
87
+ if (opts.validate) {
88
+ let hash = Uint8Array.from([]);
89
+ if (a.address) {
90
+ if (network && network.bech32 !== _address().prefix)
91
+ throw new TypeError('Invalid prefix or Network mismatch');
92
+ if (_address().version !== 0x00)
93
+ throw new TypeError('Invalid address version');
94
+ if (_address().data.length !== 20)
95
+ throw new TypeError('Invalid address data');
96
+ hash = _address().data;
97
+ }
98
+ if (a.hash) {
99
+ if (hash.length > 0 && tools.compare(hash, a.hash) !== 0)
100
+ throw new TypeError('Hash mismatch');
101
+ else hash = a.hash;
102
+ }
103
+ if (a.output) {
104
+ if (
105
+ a.output.length !== 22 ||
106
+ a.output[0] !== OPS.OP_0 ||
107
+ a.output[1] !== 0x14
108
+ )
109
+ throw new TypeError('Output is invalid');
110
+ if (hash.length > 0 && tools.compare(hash, a.output.slice(2)) !== 0)
111
+ throw new TypeError('Hash mismatch');
112
+ else hash = a.output.slice(2);
113
+ }
114
+ if (a.pubkey) {
115
+ const pkh = bcrypto.hash160(a.pubkey);
116
+ if (hash.length > 0 && tools.compare(hash, pkh) !== 0)
117
+ throw new TypeError('Hash mismatch');
118
+ else hash = pkh;
119
+ if (!isPoint(a.pubkey) || a.pubkey.length !== 33)
120
+ throw new TypeError('Invalid pubkey for p2wpkh');
121
+ }
122
+ if (a.witness) {
123
+ if (a.witness.length !== 2) throw new TypeError('Witness is invalid');
124
+ if (!bscript.isCanonicalScriptSignature(a.witness[0]))
125
+ throw new TypeError('Witness has invalid signature');
126
+ if (!isPoint(a.witness[1]) || a.witness[1].length !== 33)
127
+ throw new TypeError('Witness has invalid pubkey');
128
+ if (a.signature && tools.compare(a.signature, a.witness[0]) !== 0)
129
+ throw new TypeError('Signature mismatch');
130
+ // if (a.pubkey && !a.pubkey.equals(a.witness[1]))
131
+ if (a.pubkey && tools.compare(a.pubkey, a.witness[1]) !== 0)
132
+ throw new TypeError('Pubkey mismatch');
133
+ const pkh = bcrypto.hash160(a.witness[1]);
134
+ if (hash.length > 0 && tools.compare(hash, pkh) !== 0)
135
+ throw new TypeError('Hash mismatch');
136
+ }
137
+ }
138
+ return Object.assign(o, a);
139
+ }
@@ -0,0 +1,228 @@
1
+ import { sha256 } from '@noble/hashes/sha256';
2
+ import { bitcoin as BITCOIN_NETWORK } from '../networks.js';
3
+ import * as bscript from '../script.js';
4
+ import {
5
+ Buffer256bitSchema,
6
+ BufferSchema,
7
+ isPoint,
8
+ NBufferSchemaFactory,
9
+ stacksEqual,
10
+ NullablePartial,
11
+ } from '../types.js';
12
+ import * as lazy from './lazy.js';
13
+ import { bech32 } from 'bech32';
14
+ import * as tools from 'uint8array-tools';
15
+ import * as v from 'valibot';
16
+ const OPS = bscript.OPS;
17
+ const EMPTY_BUFFER = new Uint8Array(0);
18
+ function chunkHasUncompressedPubkey(chunk) {
19
+ if (
20
+ chunk instanceof Uint8Array &&
21
+ chunk.length === 65 &&
22
+ chunk[0] === 0x04 &&
23
+ isPoint(chunk)
24
+ ) {
25
+ return true;
26
+ } else {
27
+ return false;
28
+ }
29
+ }
30
+ // input: <>
31
+ // witness: [redeemScriptSig ...] {redeemScript}
32
+ // output: OP_0 {sha256(redeemScript)}
33
+ /**
34
+ * Creates a Pay-to-Witness-Script-Hash (P2WSH) payment object.
35
+ *
36
+ * @param a - The payment object containing the necessary data.
37
+ * @param opts - Optional payment options.
38
+ * @returns The P2WSH payment object.
39
+ * @throws {TypeError} If the required data is missing or invalid.
40
+ */
41
+ export function p2wsh(a, opts) {
42
+ if (!a.address && !a.hash && !a.output && !a.redeem && !a.witness)
43
+ throw new TypeError('Not enough data');
44
+ opts = Object.assign({ validate: true }, opts || {});
45
+ v.parse(
46
+ NullablePartial({
47
+ network: v.object({}),
48
+ address: v.string(),
49
+ hash: Buffer256bitSchema,
50
+ output: NBufferSchemaFactory(34),
51
+ redeem: NullablePartial({
52
+ input: BufferSchema,
53
+ network: v.object({}),
54
+ output: BufferSchema,
55
+ witness: v.array(BufferSchema),
56
+ }),
57
+ input: NBufferSchemaFactory(0),
58
+ witness: v.array(BufferSchema),
59
+ }),
60
+ a,
61
+ );
62
+ const _address = lazy.value(() => {
63
+ const result = bech32.decode(a.address);
64
+ const version = result.words.shift();
65
+ const data = bech32.fromWords(result.words);
66
+ return {
67
+ version,
68
+ prefix: result.prefix,
69
+ data: Uint8Array.from(data),
70
+ };
71
+ });
72
+ const _rchunks = lazy.value(() => {
73
+ return bscript.decompile(a.redeem.input);
74
+ });
75
+ let network = a.network;
76
+ if (!network) {
77
+ network = (a.redeem && a.redeem.network) || BITCOIN_NETWORK;
78
+ }
79
+ const o = { network };
80
+ lazy.prop(o, 'address', () => {
81
+ if (!o.hash) return;
82
+ const words = bech32.toWords(o.hash);
83
+ words.unshift(0x00);
84
+ return bech32.encode(network.bech32, words);
85
+ });
86
+ lazy.prop(o, 'hash', () => {
87
+ if (a.output) return a.output.slice(2);
88
+ if (a.address) return _address().data;
89
+ if (o.redeem && o.redeem.output) return sha256(o.redeem.output);
90
+ });
91
+ lazy.prop(o, 'output', () => {
92
+ if (!o.hash) return;
93
+ return bscript.compile([OPS.OP_0, o.hash]);
94
+ });
95
+ lazy.prop(o, 'redeem', () => {
96
+ if (!a.witness) return;
97
+ return {
98
+ output: a.witness[a.witness.length - 1],
99
+ input: EMPTY_BUFFER,
100
+ witness: a.witness.slice(0, -1),
101
+ };
102
+ });
103
+ lazy.prop(o, 'input', () => {
104
+ if (!o.witness) return;
105
+ return EMPTY_BUFFER;
106
+ });
107
+ lazy.prop(o, 'witness', () => {
108
+ // transform redeem input to witness stack?
109
+ if (
110
+ a.redeem &&
111
+ a.redeem.input &&
112
+ a.redeem.input.length > 0 &&
113
+ a.redeem.output &&
114
+ a.redeem.output.length > 0
115
+ ) {
116
+ const stack = bscript.toStack(_rchunks());
117
+ // assign, and blank the existing input
118
+ o.redeem = Object.assign({ witness: stack }, a.redeem);
119
+ o.redeem.input = EMPTY_BUFFER;
120
+ return [].concat(stack, a.redeem.output);
121
+ }
122
+ if (!a.redeem) return;
123
+ if (!a.redeem.output) return;
124
+ if (!a.redeem.witness) return;
125
+ return [].concat(a.redeem.witness, a.redeem.output);
126
+ });
127
+ lazy.prop(o, 'name', () => {
128
+ const nameParts = ['p2wsh'];
129
+ if (o.redeem !== undefined && o.redeem.name !== undefined)
130
+ nameParts.push(o.redeem.name);
131
+ return nameParts.join('-');
132
+ });
133
+ // extended validation
134
+ if (opts.validate) {
135
+ let hash = Uint8Array.from([]);
136
+ if (a.address) {
137
+ if (_address().prefix !== network.bech32)
138
+ throw new TypeError('Invalid prefix or Network mismatch');
139
+ if (_address().version !== 0x00)
140
+ throw new TypeError('Invalid address version');
141
+ if (_address().data.length !== 32)
142
+ throw new TypeError('Invalid address data');
143
+ hash = _address().data;
144
+ }
145
+ if (a.hash) {
146
+ if (hash.length > 0 && tools.compare(hash, a.hash) !== 0)
147
+ throw new TypeError('Hash mismatch');
148
+ else hash = a.hash;
149
+ }
150
+ if (a.output) {
151
+ if (
152
+ a.output.length !== 34 ||
153
+ a.output[0] !== OPS.OP_0 ||
154
+ a.output[1] !== 0x20
155
+ )
156
+ throw new TypeError('Output is invalid');
157
+ const hash2 = a.output.slice(2);
158
+ if (hash.length > 0 && tools.compare(hash, hash2) !== 0)
159
+ throw new TypeError('Hash mismatch');
160
+ else hash = hash2;
161
+ }
162
+ if (a.redeem) {
163
+ if (a.redeem.network && a.redeem.network !== network)
164
+ throw new TypeError('Network mismatch');
165
+ // is there two redeem sources?
166
+ if (
167
+ a.redeem.input &&
168
+ a.redeem.input.length > 0 &&
169
+ a.redeem.witness &&
170
+ a.redeem.witness.length > 0
171
+ )
172
+ throw new TypeError('Ambiguous witness source');
173
+ // is the redeem output non-empty/valid?
174
+ if (a.redeem.output) {
175
+ const decompile = bscript.decompile(a.redeem.output);
176
+ if (!decompile || decompile.length < 1)
177
+ throw new TypeError('Redeem.output is invalid');
178
+ if (a.redeem.output.byteLength > 3600)
179
+ throw new TypeError(
180
+ 'Redeem.output unspendable if larger than 3600 bytes',
181
+ );
182
+ if (bscript.countNonPushOnlyOPs(decompile) > 201)
183
+ throw new TypeError(
184
+ 'Redeem.output unspendable with more than 201 non-push ops',
185
+ );
186
+ // match hash against other sources
187
+ const hash2 = sha256(a.redeem.output);
188
+ if (hash.length > 0 && tools.compare(hash, hash2) !== 0)
189
+ throw new TypeError('Hash mismatch');
190
+ else hash = hash2;
191
+ }
192
+ if (a.redeem.input && !bscript.isPushOnly(_rchunks()))
193
+ throw new TypeError('Non push-only scriptSig');
194
+ if (
195
+ a.witness &&
196
+ a.redeem.witness &&
197
+ !stacksEqual(a.witness, a.redeem.witness)
198
+ )
199
+ throw new TypeError('Witness and redeem.witness mismatch');
200
+ if (
201
+ (a.redeem.input && _rchunks().some(chunkHasUncompressedPubkey)) ||
202
+ (a.redeem.output &&
203
+ (bscript.decompile(a.redeem.output) || []).some(
204
+ chunkHasUncompressedPubkey,
205
+ ))
206
+ ) {
207
+ throw new TypeError(
208
+ 'redeem.input or redeem.output contains uncompressed pubkey',
209
+ );
210
+ }
211
+ }
212
+ if (a.witness && a.witness.length > 0) {
213
+ const wScript = a.witness[a.witness.length - 1];
214
+ if (
215
+ a.redeem &&
216
+ a.redeem.output &&
217
+ tools.compare(a.redeem.output, wScript) !== 0
218
+ )
219
+ throw new TypeError('Witness and redeem.output mismatch');
220
+ if (
221
+ a.witness.some(chunkHasUncompressedPubkey) ||
222
+ (bscript.decompile(wScript) || []).some(chunkHasUncompressedPubkey)
223
+ )
224
+ throw new TypeError('Witness contains uncompressed pubkey');
225
+ }
226
+ }
227
+ return Object.assign(o, a);
228
+ }