@btc-vision/bitcoin 7.0.0-alpha.1 → 7.0.0-alpha.2

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 (160) hide show
  1. package/browser/address.d.ts +5 -1
  2. package/browser/address.d.ts.map +1 -1
  3. package/browser/branded.d.ts +3 -14
  4. package/browser/branded.d.ts.map +1 -1
  5. package/browser/ecc/context.d.ts.map +1 -1
  6. package/browser/index.d.ts +2 -1
  7. package/browser/index.d.ts.map +1 -1
  8. package/browser/index.js +2964 -2919
  9. package/browser/opcodes.d.ts +11 -0
  10. package/browser/opcodes.d.ts.map +1 -1
  11. package/browser/psbt/PsbtCache.d.ts +54 -0
  12. package/browser/psbt/PsbtCache.d.ts.map +1 -0
  13. package/browser/psbt/PsbtFinalizer.d.ts +21 -0
  14. package/browser/psbt/PsbtFinalizer.d.ts.map +1 -0
  15. package/browser/psbt/PsbtSigner.d.ts +32 -0
  16. package/browser/psbt/PsbtSigner.d.ts.map +1 -0
  17. package/browser/psbt/PsbtTransaction.d.ts +25 -0
  18. package/browser/psbt/PsbtTransaction.d.ts.map +1 -0
  19. package/browser/psbt/types.d.ts +13 -13
  20. package/browser/psbt/types.d.ts.map +1 -1
  21. package/browser/psbt/validation.d.ts +1 -1
  22. package/browser/psbt/validation.d.ts.map +1 -1
  23. package/browser/psbt.d.ts +27 -39
  24. package/browser/psbt.d.ts.map +1 -1
  25. package/browser/script.d.ts.map +1 -1
  26. package/browser/transaction.d.ts +4 -4
  27. package/browser/transaction.d.ts.map +1 -1
  28. package/browser/types.d.ts +4 -2
  29. package/browser/types.d.ts.map +1 -1
  30. package/browser/workers/index.d.ts +3 -50
  31. package/browser/workers/index.d.ts.map +1 -1
  32. package/browser/workers/index.node.d.ts +24 -0
  33. package/browser/workers/index.node.d.ts.map +1 -0
  34. package/build/address.d.ts +5 -1
  35. package/build/address.d.ts.map +1 -1
  36. package/build/address.js +29 -17
  37. package/build/address.js.map +1 -1
  38. package/build/branded.d.ts +3 -14
  39. package/build/branded.d.ts.map +1 -1
  40. package/build/branded.js +0 -5
  41. package/build/branded.js.map +1 -1
  42. package/build/ecc/context.d.ts.map +1 -1
  43. package/build/ecc/context.js +68 -45
  44. package/build/ecc/context.js.map +1 -1
  45. package/build/index.d.ts +2 -1
  46. package/build/index.d.ts.map +1 -1
  47. package/build/index.js +1 -1
  48. package/build/index.js.map +1 -1
  49. package/build/opcodes.d.ts +11 -0
  50. package/build/opcodes.d.ts.map +1 -1
  51. package/build/opcodes.js +19 -4
  52. package/build/opcodes.js.map +1 -1
  53. package/build/psbt/PsbtCache.d.ts +54 -0
  54. package/build/psbt/PsbtCache.d.ts.map +1 -0
  55. package/build/psbt/PsbtCache.js +249 -0
  56. package/build/psbt/PsbtCache.js.map +1 -0
  57. package/build/psbt/PsbtFinalizer.d.ts +21 -0
  58. package/build/psbt/PsbtFinalizer.d.ts.map +1 -0
  59. package/build/psbt/PsbtFinalizer.js +157 -0
  60. package/build/psbt/PsbtFinalizer.js.map +1 -0
  61. package/build/psbt/PsbtSigner.d.ts +32 -0
  62. package/build/psbt/PsbtSigner.d.ts.map +1 -0
  63. package/build/psbt/PsbtSigner.js +192 -0
  64. package/build/psbt/PsbtSigner.js.map +1 -0
  65. package/build/psbt/PsbtTransaction.d.ts +25 -0
  66. package/build/psbt/PsbtTransaction.d.ts.map +1 -0
  67. package/build/psbt/PsbtTransaction.js +61 -0
  68. package/build/psbt/PsbtTransaction.js.map +1 -0
  69. package/build/psbt/types.d.ts +13 -13
  70. package/build/psbt/types.d.ts.map +1 -1
  71. package/build/psbt/validation.d.ts +1 -1
  72. package/build/psbt/validation.d.ts.map +1 -1
  73. package/build/psbt.d.ts +27 -39
  74. package/build/psbt.d.ts.map +1 -1
  75. package/build/psbt.js +139 -746
  76. package/build/psbt.js.map +1 -1
  77. package/build/script.d.ts.map +1 -1
  78. package/build/script.js +2 -2
  79. package/build/script.js.map +1 -1
  80. package/build/transaction.d.ts +4 -4
  81. package/build/transaction.d.ts.map +1 -1
  82. package/build/transaction.js +5 -4
  83. package/build/transaction.js.map +1 -1
  84. package/build/tsconfig.build.tsbuildinfo +1 -1
  85. package/build/types.d.ts +4 -2
  86. package/build/types.d.ts.map +1 -1
  87. package/build/types.js +9 -0
  88. package/build/types.js.map +1 -1
  89. package/build/workers/WorkerSigningPool.js +1 -1
  90. package/build/workers/WorkerSigningPool.js.map +1 -1
  91. package/build/workers/index.d.ts +3 -3
  92. package/build/workers/index.d.ts.map +1 -1
  93. package/build/workers/index.js +0 -3
  94. package/build/workers/index.js.map +1 -1
  95. package/build/workers/index.node.d.ts +24 -0
  96. package/build/workers/index.node.d.ts.map +1 -0
  97. package/build/workers/index.node.js +26 -0
  98. package/build/workers/index.node.js.map +1 -0
  99. package/package.json +28 -9
  100. package/src/address.ts +41 -18
  101. package/src/branded.ts +15 -13
  102. package/src/ecc/context.ts +75 -57
  103. package/src/index.ts +36 -1
  104. package/src/opcodes.ts +21 -4
  105. package/src/psbt/PsbtCache.ts +325 -0
  106. package/src/psbt/PsbtFinalizer.ts +213 -0
  107. package/src/psbt/PsbtSigner.ts +302 -0
  108. package/src/psbt/PsbtTransaction.ts +82 -0
  109. package/src/psbt/types.ts +13 -13
  110. package/src/psbt/validation.ts +1 -1
  111. package/src/psbt.ts +299 -1090
  112. package/src/script.ts +2 -2
  113. package/src/transaction.ts +9 -8
  114. package/src/types.ts +13 -0
  115. package/src/workers/WorkerSigningPool.ts +1 -1
  116. package/src/workers/index.node.ts +27 -0
  117. package/src/workers/index.ts +7 -9
  118. package/test/address.spec.ts +2 -2
  119. package/test/bitcoin.core.spec.ts +5 -2
  120. package/test/browser/payments.spec.ts +151 -0
  121. package/test/browser/psbt.spec.ts +1510 -0
  122. package/test/browser/script.spec.ts +223 -0
  123. package/test/browser/setup.ts +13 -0
  124. package/test/browser/workers-signing.spec.ts +537 -0
  125. package/test/crypto.spec.ts +2 -2
  126. package/test/fixtures/core/base58_encode_decode.json +12 -48
  127. package/test/fixtures/core/base58_keys_invalid.json +50 -150
  128. package/test/fixtures/core/sighash.json +1 -3
  129. package/test/fixtures/core/tx_valid.json +133 -501
  130. package/test/fixtures/embed.json +3 -11
  131. package/test/fixtures/p2ms.json +21 -91
  132. package/test/fixtures/p2pk.json +5 -24
  133. package/test/fixtures/p2pkh.json +7 -36
  134. package/test/fixtures/p2sh.json +8 -54
  135. package/test/fixtures/p2tr.json +2 -6
  136. package/test/fixtures/p2wpkh.json +7 -36
  137. package/test/fixtures/p2wsh.json +14 -59
  138. package/test/fixtures/psbt.json +2 -6
  139. package/test/fixtures/script.json +12 -48
  140. package/test/integration/addresses.spec.ts +11 -5
  141. package/test/integration/bip32.spec.ts +1 -1
  142. package/test/integration/cltv.spec.ts +10 -6
  143. package/test/integration/csv.spec.ts +10 -9
  144. package/test/integration/payments.spec.ts +8 -4
  145. package/test/integration/taproot.spec.ts +26 -6
  146. package/test/integration/transactions.spec.ts +22 -8
  147. package/test/payments.spec.ts +1 -1
  148. package/test/payments.utils.ts +1 -1
  149. package/test/psbt.spec.ts +250 -64
  150. package/test/script_signature.spec.ts +1 -1
  151. package/test/transaction.spec.ts +18 -5
  152. package/test/tsconfig.json +6 -20
  153. package/test/workers-pool.spec.ts +22 -23
  154. package/test/workers-signing.spec.ts +7 -3
  155. package/test/workers.spec.ts +6 -7
  156. package/typedoc.json +11 -1
  157. package/vitest.config.browser.ts +68 -0
  158. package/browser/ecpair.d.ts +0 -99
  159. package/src/ecpair.d.ts +0 -99
  160. package/test/taproot-cache.spec.ts +0 -694
@@ -0,0 +1,223 @@
1
+ /**
2
+ * Browser-adapted version of test/script.spec.ts
3
+ * Removes minimaldata native module dependency and its test section.
4
+ */
5
+ import assert from 'assert';
6
+ import { describe, it } from 'vitest';
7
+ import * as bscript from '../../src/script.js';
8
+ import { toHex } from '../../src/io/index.js';
9
+ import fixtures from '../fixtures/script.json' with { type: 'json' };
10
+
11
+ describe('script', () => {
12
+ describe('isCanonicalPubKey', () => {
13
+ it('rejects if not provided a Buffer', () => {
14
+ assert.strictEqual(bscript.isCanonicalPubKey(0 as any), false);
15
+ assert.strictEqual(bscript.isCanonicalPubKey('string' as any), false);
16
+ assert.strictEqual(bscript.isCanonicalPubKey(null as any), false);
17
+ });
18
+
19
+ it('rejects smaller than 33 bytes', () => {
20
+ for (let i = 0; i < 33; i++) {
21
+ assert.strictEqual(bscript.isCanonicalPubKey(Buffer.allocUnsafe(i)), false);
22
+ }
23
+ });
24
+
25
+ it('accepts valid compressed pubkey (33 bytes, starts with 0x02)', () => {
26
+ const compressedEven = Buffer.from(
27
+ '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
28
+ 'hex',
29
+ );
30
+ assert.strictEqual(bscript.isCanonicalPubKey(compressedEven), true);
31
+ });
32
+
33
+ it('accepts valid compressed pubkey (33 bytes, starts with 0x03)', () => {
34
+ const compressedOdd = Buffer.from(
35
+ '03df51984d6b8b8b1cc693e239491f77a36c9e9dfe4a486e9972a18e03610a0d22',
36
+ 'hex',
37
+ );
38
+ assert.strictEqual(bscript.isCanonicalPubKey(compressedOdd), true);
39
+ });
40
+
41
+ it('accepts valid uncompressed pubkey (65 bytes, starts with 0x04)', () => {
42
+ const uncompressed = Buffer.from(
43
+ '0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
44
+ 'hex',
45
+ );
46
+ assert.strictEqual(bscript.isCanonicalPubKey(uncompressed), true);
47
+ });
48
+
49
+ it('rejects invalid format indicator', () => {
50
+ const invalidFormat = Buffer.from(
51
+ '0579be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
52
+ 'hex',
53
+ );
54
+ assert.strictEqual(bscript.isCanonicalPubKey(invalidFormat), false);
55
+ });
56
+ });
57
+
58
+ describe('isCanonicalScriptSignature', () => {
59
+ it('rejects if not provided a Buffer', () => {
60
+ assert.strictEqual(bscript.isCanonicalScriptSignature(0 as any), false);
61
+ assert.strictEqual(bscript.isCanonicalScriptSignature('string' as any), false);
62
+ assert.strictEqual(bscript.isCanonicalScriptSignature(null as any), false);
63
+ });
64
+
65
+ it('rejects if hash type is invalid', () => {
66
+ const sigWithBadHashType = Buffer.from(
67
+ '3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb00',
68
+ 'hex',
69
+ );
70
+ assert.strictEqual(bscript.isCanonicalScriptSignature(sigWithBadHashType), false);
71
+ });
72
+
73
+ it('accepts valid canonical signature with SIGHASH_ALL', () => {
74
+ const validSig = Buffer.from(
75
+ '3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01',
76
+ 'hex',
77
+ );
78
+ assert.strictEqual(bscript.isCanonicalScriptSignature(validSig), true);
79
+ });
80
+
81
+ it('rejects signature with invalid DER encoding', () => {
82
+ const invalidDer = Buffer.from('3045022047ac8e8701', 'hex');
83
+ assert.strictEqual(bscript.isCanonicalScriptSignature(invalidDer), false);
84
+ });
85
+ });
86
+
87
+ describe('fromASM/toASM', () => {
88
+ fixtures.valid.forEach((f) => {
89
+ it('encodes/decodes ' + f.asm, () => {
90
+ const script = bscript.fromASM(f.asm);
91
+ assert.strictEqual(bscript.toASM(script), f.asm);
92
+ });
93
+ });
94
+
95
+ fixtures.invalid.fromASM.forEach((f) => {
96
+ it('throws ' + f.description, () => {
97
+ assert.throws(() => {
98
+ bscript.fromASM(f.script);
99
+ }, new RegExp(f.description));
100
+ });
101
+ });
102
+ });
103
+
104
+ describe('toASM', () => {
105
+ const OP_RETURN = bscript.opcodes.OP_RETURN;
106
+ it('encodes empty buffer as OP_0', () => {
107
+ const chunks = [OP_RETURN, Buffer.from([])];
108
+ assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_0');
109
+ });
110
+
111
+ for (let i = 1; i <= 16; i++) {
112
+ it(`encodes one byte buffer [${i}] as OP_${i}`, () => {
113
+ const chunks = [OP_RETURN, Buffer.from([i])];
114
+ assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_' + i);
115
+ });
116
+ }
117
+ });
118
+
119
+ describe('fromASM/toASM (templates)', () => {
120
+ fixtures.valid2.forEach((f) => {
121
+ if (f.inputHex) {
122
+ const ih = bscript.toASM(Buffer.from(f.inputHex, 'hex'));
123
+
124
+ it('encodes/decodes ' + ih, () => {
125
+ const script = bscript.fromASM(f.input);
126
+ assert.strictEqual(toHex(script), f.inputHex);
127
+ assert.strictEqual(bscript.toASM(script), f.input);
128
+ });
129
+ }
130
+
131
+ if (f.outputHex) {
132
+ it('encodes/decodes ' + f.output, () => {
133
+ const script = bscript.fromASM(f.output);
134
+ assert.strictEqual(toHex(script), f.outputHex);
135
+ assert.strictEqual(bscript.toASM(script), f.output);
136
+ });
137
+ }
138
+ });
139
+ });
140
+
141
+ describe('isPushOnly', () => {
142
+ fixtures.valid.forEach((f) => {
143
+ it('returns ' + !!f.stack + ' for ' + f.asm, () => {
144
+ const script = bscript.fromASM(f.asm);
145
+ const chunks = bscript.decompile(script);
146
+
147
+ assert.strictEqual(bscript.isPushOnly(chunks!), !!f.stack);
148
+ });
149
+ });
150
+ });
151
+
152
+ describe('toStack', () => {
153
+ fixtures.valid.forEach((f) => {
154
+ it('returns ' + !!f.stack + ' for ' + f.asm, () => {
155
+ if (!f.stack || !f.asm) return;
156
+
157
+ const script = bscript.fromASM(f.asm);
158
+
159
+ const stack = bscript.toStack(script);
160
+ assert.deepStrictEqual(
161
+ stack.map((x) => {
162
+ return toHex(x);
163
+ }),
164
+ f.stack,
165
+ );
166
+
167
+ assert.strictEqual(
168
+ bscript.toASM(bscript.compile(stack)),
169
+ f.asm,
170
+ 'should rebuild same script from stack',
171
+ );
172
+ });
173
+ });
174
+ });
175
+
176
+ describe('compile (via fromASM)', () => {
177
+ fixtures.valid.forEach((f) => {
178
+ it('compiles ' + f.asm, () => {
179
+ const scriptSig = bscript.fromASM(f.asm);
180
+
181
+ assert.strictEqual(toHex(scriptSig), f.script);
182
+
183
+ if (f.nonstandard) {
184
+ const scriptSigNS = bscript.fromASM(f.nonstandard.scriptSig);
185
+
186
+ assert.strictEqual(toHex(scriptSigNS), f.script);
187
+ }
188
+ });
189
+ });
190
+ });
191
+
192
+ describe('decompile', () => {
193
+ fixtures.valid.forEach((f) => {
194
+ it('decompiles ' + f.asm, () => {
195
+ const chunks = bscript.decompile(Buffer.from(f.script, 'hex'));
196
+
197
+ assert.strictEqual(toHex(bscript.compile(chunks!)), f.script);
198
+ assert.strictEqual(bscript.toASM(chunks!), f.asm);
199
+
200
+ if (f.nonstandard) {
201
+ const chunksNS = bscript.decompile(
202
+ Buffer.from(f.nonstandard.scriptSigHex, 'hex'),
203
+ );
204
+
205
+ assert.strictEqual(toHex(bscript.compile(chunksNS!)), f.script);
206
+
207
+ assert.strictEqual(bscript.toASM(chunksNS!), f.nonstandard.scriptSig);
208
+ }
209
+ });
210
+ });
211
+
212
+ fixtures.invalid.decompile.forEach((f) => {
213
+ it('fails to decompile ' + f.script + ', because "' + f.description + '"', () => {
214
+ const chunks = bscript.decompile(Buffer.from(f.script, 'hex'));
215
+
216
+ assert.strictEqual(chunks, null);
217
+ });
218
+ });
219
+ });
220
+
221
+ // NOTE: SCRIPT_VERIFY_MINIMALDATA tests excluded in browser build
222
+ // because the 'minimaldata' package is a native Node.js module
223
+ });
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Browser-safe utilities for tests that need crypto.randomBytes().
3
+ */
4
+
5
+ /**
6
+ * Browser-safe replacement for Node.js `crypto.randomBytes()`.
7
+ * Uses the Web Crypto API (`crypto.getRandomValues()`).
8
+ */
9
+ export function randomBytes(size: number): Uint8Array {
10
+ const buf = new Uint8Array(size);
11
+ globalThis.crypto.getRandomValues(buf);
12
+ return buf;
13
+ }