@btc-vision/bitcoin 7.0.0-alpha.0 → 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 (296) hide show
  1. package/browser/address.d.ts +6 -2
  2. package/browser/address.d.ts.map +1 -1
  3. package/browser/block.d.ts.map +1 -1
  4. package/browser/branded.d.ts +3 -14
  5. package/browser/branded.d.ts.map +1 -1
  6. package/browser/crypto.d.ts +1 -1
  7. package/browser/ecc/context.d.ts +4 -4
  8. package/browser/ecc/context.d.ts.map +1 -1
  9. package/browser/ecc/types.d.ts +1 -1
  10. package/browser/ecc/types.d.ts.map +1 -1
  11. package/browser/index.d.ts +3 -2
  12. package/browser/index.d.ts.map +1 -1
  13. package/browser/index.js +3579 -3539
  14. package/browser/io/BinaryReader.d.ts +15 -15
  15. package/browser/io/BinaryReader.d.ts.map +1 -1
  16. package/browser/io/BinaryWriter.d.ts +17 -17
  17. package/browser/io/BinaryWriter.d.ts.map +1 -1
  18. package/browser/io/MemoryPool.d.ts +20 -20
  19. package/browser/io/MemoryPool.d.ts.map +1 -1
  20. package/browser/opcodes.d.ts +11 -0
  21. package/browser/opcodes.d.ts.map +1 -1
  22. package/browser/payments/bip341.d.ts +1 -1
  23. package/browser/payments/bip341.d.ts.map +1 -1
  24. package/browser/payments/embed.d.ts +1 -1
  25. package/browser/payments/embed.d.ts.map +1 -1
  26. package/browser/payments/p2ms.d.ts.map +1 -1
  27. package/browser/payments/p2op.d.ts +1 -1
  28. package/browser/payments/p2op.d.ts.map +1 -1
  29. package/browser/payments/p2pk.d.ts +1 -1
  30. package/browser/payments/p2pk.d.ts.map +1 -1
  31. package/browser/payments/p2pkh.d.ts +1 -1
  32. package/browser/payments/p2pkh.d.ts.map +1 -1
  33. package/browser/payments/p2sh.d.ts.map +1 -1
  34. package/browser/payments/p2tr.d.ts +2 -2
  35. package/browser/payments/p2tr.d.ts.map +1 -1
  36. package/browser/payments/p2wpkh.d.ts +1 -1
  37. package/browser/payments/p2wpkh.d.ts.map +1 -1
  38. package/browser/payments/p2wsh.d.ts.map +1 -1
  39. package/browser/payments/types.d.ts +1 -1
  40. package/browser/payments/types.d.ts.map +1 -1
  41. package/browser/psbt/PsbtCache.d.ts +54 -0
  42. package/browser/psbt/PsbtCache.d.ts.map +1 -0
  43. package/browser/psbt/PsbtFinalizer.d.ts +21 -0
  44. package/browser/psbt/PsbtFinalizer.d.ts.map +1 -0
  45. package/browser/psbt/PsbtSigner.d.ts +32 -0
  46. package/browser/psbt/PsbtSigner.d.ts.map +1 -0
  47. package/browser/psbt/PsbtTransaction.d.ts +25 -0
  48. package/browser/psbt/PsbtTransaction.d.ts.map +1 -0
  49. package/browser/psbt/bip371.d.ts.map +1 -1
  50. package/browser/psbt/types.d.ts +14 -14
  51. package/browser/psbt/types.d.ts.map +1 -1
  52. package/browser/psbt/validation.d.ts +1 -1
  53. package/browser/psbt/validation.d.ts.map +1 -1
  54. package/browser/psbt.d.ts +27 -39
  55. package/browser/psbt.d.ts.map +1 -1
  56. package/browser/script.d.ts.map +1 -1
  57. package/browser/transaction.d.ts +4 -4
  58. package/browser/transaction.d.ts.map +1 -1
  59. package/browser/types.d.ts +4 -2
  60. package/browser/types.d.ts.map +1 -1
  61. package/browser/workers/WorkerSigningPool.d.ts +17 -17
  62. package/browser/workers/WorkerSigningPool.d.ts.map +1 -1
  63. package/browser/workers/WorkerSigningPool.node.d.ts +12 -12
  64. package/browser/workers/WorkerSigningPool.node.d.ts.map +1 -1
  65. package/browser/workers/index.d.ts +3 -50
  66. package/browser/workers/index.d.ts.map +1 -1
  67. package/browser/workers/index.node.d.ts +24 -0
  68. package/browser/workers/index.node.d.ts.map +1 -0
  69. package/browser/workers/psbt-parallel.d.ts +1 -1
  70. package/browser/workers/psbt-parallel.d.ts.map +1 -1
  71. package/browser/workers/types.d.ts.map +1 -1
  72. package/build/address.d.ts +6 -2
  73. package/build/address.d.ts.map +1 -1
  74. package/build/address.js +32 -19
  75. package/build/address.js.map +1 -1
  76. package/build/block.d.ts.map +1 -1
  77. package/build/block.js +2 -4
  78. package/build/block.js.map +1 -1
  79. package/build/branded.d.ts +3 -14
  80. package/build/branded.d.ts.map +1 -1
  81. package/build/branded.js +0 -5
  82. package/build/branded.js.map +1 -1
  83. package/build/crypto.d.ts +1 -1
  84. package/build/ecc/context.d.ts +4 -4
  85. package/build/ecc/context.d.ts.map +1 -1
  86. package/build/ecc/context.js +75 -52
  87. package/build/ecc/context.js.map +1 -1
  88. package/build/ecc/types.d.ts +1 -1
  89. package/build/ecc/types.d.ts.map +1 -1
  90. package/build/index.d.ts +3 -2
  91. package/build/index.d.ts.map +1 -1
  92. package/build/index.js +3 -3
  93. package/build/index.js.map +1 -1
  94. package/build/io/BinaryReader.d.ts +15 -15
  95. package/build/io/BinaryReader.d.ts.map +1 -1
  96. package/build/io/BinaryReader.js +17 -17
  97. package/build/io/BinaryReader.js.map +1 -1
  98. package/build/io/BinaryWriter.d.ts +17 -17
  99. package/build/io/BinaryWriter.d.ts.map +1 -1
  100. package/build/io/BinaryWriter.js +39 -39
  101. package/build/io/BinaryWriter.js.map +1 -1
  102. package/build/io/MemoryPool.d.ts +20 -20
  103. package/build/io/MemoryPool.d.ts.map +1 -1
  104. package/build/io/MemoryPool.js +28 -28
  105. package/build/io/MemoryPool.js.map +1 -1
  106. package/build/opcodes.d.ts +11 -0
  107. package/build/opcodes.d.ts.map +1 -1
  108. package/build/opcodes.js +19 -4
  109. package/build/opcodes.js.map +1 -1
  110. package/build/payments/bip341.d.ts +1 -2
  111. package/build/payments/bip341.d.ts.map +1 -1
  112. package/build/payments/bip341.js +1 -2
  113. package/build/payments/bip341.js.map +1 -1
  114. package/build/payments/embed.d.ts +1 -1
  115. package/build/payments/embed.d.ts.map +1 -1
  116. package/build/payments/embed.js +14 -14
  117. package/build/payments/embed.js.map +1 -1
  118. package/build/payments/p2ms.d.ts.map +1 -1
  119. package/build/payments/p2ms.js +21 -21
  120. package/build/payments/p2ms.js.map +1 -1
  121. package/build/payments/p2op.d.ts +1 -1
  122. package/build/payments/p2op.d.ts.map +1 -1
  123. package/build/payments/p2op.js +18 -18
  124. package/build/payments/p2op.js.map +1 -1
  125. package/build/payments/p2pk.d.ts +1 -1
  126. package/build/payments/p2pk.d.ts.map +1 -1
  127. package/build/payments/p2pk.js +17 -17
  128. package/build/payments/p2pk.js.map +1 -1
  129. package/build/payments/p2pkh.d.ts +1 -1
  130. package/build/payments/p2pkh.d.ts.map +1 -1
  131. package/build/payments/p2pkh.js +20 -20
  132. package/build/payments/p2pkh.js.map +1 -1
  133. package/build/payments/p2sh.d.ts.map +1 -1
  134. package/build/payments/p2sh.js +22 -20
  135. package/build/payments/p2sh.js.map +1 -1
  136. package/build/payments/p2tr.d.ts +2 -2
  137. package/build/payments/p2tr.d.ts.map +1 -1
  138. package/build/payments/p2tr.js +23 -23
  139. package/build/payments/p2tr.js.map +1 -1
  140. package/build/payments/p2wpkh.d.ts +1 -1
  141. package/build/payments/p2wpkh.d.ts.map +1 -1
  142. package/build/payments/p2wpkh.js +20 -20
  143. package/build/payments/p2wpkh.js.map +1 -1
  144. package/build/payments/p2wsh.d.ts.map +1 -1
  145. package/build/payments/p2wsh.js +22 -22
  146. package/build/payments/p2wsh.js.map +1 -1
  147. package/build/payments/types.d.ts +1 -1
  148. package/build/payments/types.d.ts.map +1 -1
  149. package/build/psbt/PsbtCache.d.ts +54 -0
  150. package/build/psbt/PsbtCache.d.ts.map +1 -0
  151. package/build/psbt/PsbtCache.js +249 -0
  152. package/build/psbt/PsbtCache.js.map +1 -0
  153. package/build/psbt/PsbtFinalizer.d.ts +21 -0
  154. package/build/psbt/PsbtFinalizer.d.ts.map +1 -0
  155. package/build/psbt/PsbtFinalizer.js +157 -0
  156. package/build/psbt/PsbtFinalizer.js.map +1 -0
  157. package/build/psbt/PsbtSigner.d.ts +32 -0
  158. package/build/psbt/PsbtSigner.d.ts.map +1 -0
  159. package/build/psbt/PsbtSigner.js +192 -0
  160. package/build/psbt/PsbtSigner.js.map +1 -0
  161. package/build/psbt/PsbtTransaction.d.ts +25 -0
  162. package/build/psbt/PsbtTransaction.d.ts.map +1 -0
  163. package/build/psbt/PsbtTransaction.js +61 -0
  164. package/build/psbt/PsbtTransaction.js.map +1 -0
  165. package/build/psbt/bip371.d.ts.map +1 -1
  166. package/build/psbt/bip371.js +6 -2
  167. package/build/psbt/bip371.js.map +1 -1
  168. package/build/psbt/psbtutils.js +1 -1
  169. package/build/psbt/psbtutils.js.map +1 -1
  170. package/build/psbt/types.d.ts +14 -14
  171. package/build/psbt/types.d.ts.map +1 -1
  172. package/build/psbt/validation.d.ts +1 -1
  173. package/build/psbt/validation.d.ts.map +1 -1
  174. package/build/psbt/validation.js +1 -1
  175. package/build/psbt/validation.js.map +1 -1
  176. package/build/psbt.d.ts +27 -39
  177. package/build/psbt.d.ts.map +1 -1
  178. package/build/psbt.js +142 -755
  179. package/build/psbt.js.map +1 -1
  180. package/build/script.d.ts.map +1 -1
  181. package/build/script.js +4 -4
  182. package/build/script.js.map +1 -1
  183. package/build/transaction.d.ts +4 -4
  184. package/build/transaction.d.ts.map +1 -1
  185. package/build/transaction.js +6 -5
  186. package/build/transaction.js.map +1 -1
  187. package/build/tsconfig.build.tsbuildinfo +1 -1
  188. package/build/types.d.ts +4 -2
  189. package/build/types.d.ts.map +1 -1
  190. package/build/types.js +12 -9
  191. package/build/types.js.map +1 -1
  192. package/build/workers/WorkerSigningPool.d.ts +17 -17
  193. package/build/workers/WorkerSigningPool.d.ts.map +1 -1
  194. package/build/workers/WorkerSigningPool.js +25 -25
  195. package/build/workers/WorkerSigningPool.js.map +1 -1
  196. package/build/workers/WorkerSigningPool.node.d.ts +12 -12
  197. package/build/workers/WorkerSigningPool.node.d.ts.map +1 -1
  198. package/build/workers/WorkerSigningPool.node.js +23 -23
  199. package/build/workers/WorkerSigningPool.node.js.map +1 -1
  200. package/build/workers/index.d.ts +3 -3
  201. package/build/workers/index.d.ts.map +1 -1
  202. package/build/workers/index.js +0 -3
  203. package/build/workers/index.js.map +1 -1
  204. package/build/workers/index.node.d.ts +24 -0
  205. package/build/workers/index.node.d.ts.map +1 -0
  206. package/build/workers/index.node.js +26 -0
  207. package/build/workers/index.node.js.map +1 -0
  208. package/build/workers/psbt-parallel.d.ts +1 -1
  209. package/build/workers/psbt-parallel.d.ts.map +1 -1
  210. package/build/workers/psbt-parallel.js.map +1 -1
  211. package/build/workers/types.d.ts.map +1 -1
  212. package/build/workers/types.js.map +1 -1
  213. package/package.json +30 -10
  214. package/src/address.ts +53 -21
  215. package/src/block.ts +15 -8
  216. package/src/branded.ts +15 -13
  217. package/src/crypto.ts +1 -1
  218. package/src/ecc/context.ts +85 -64
  219. package/src/ecc/types.ts +1 -8
  220. package/src/index.ts +48 -14
  221. package/src/io/BinaryReader.ts +18 -18
  222. package/src/io/BinaryWriter.ts +43 -43
  223. package/src/io/MemoryPool.ts +32 -32
  224. package/src/opcodes.ts +21 -4
  225. package/src/payments/bip341.ts +2 -4
  226. package/src/payments/embed.ts +18 -18
  227. package/src/payments/p2ms.ts +32 -25
  228. package/src/payments/p2op.ts +22 -22
  229. package/src/payments/p2pk.ts +20 -20
  230. package/src/payments/p2pkh.ts +25 -25
  231. package/src/payments/p2sh.ts +30 -27
  232. package/src/payments/p2tr.ts +31 -31
  233. package/src/payments/p2wpkh.ts +25 -25
  234. package/src/payments/p2wsh.ts +27 -27
  235. package/src/payments/types.ts +1 -1
  236. package/src/psbt/PsbtCache.ts +325 -0
  237. package/src/psbt/PsbtFinalizer.ts +213 -0
  238. package/src/psbt/PsbtSigner.ts +302 -0
  239. package/src/psbt/PsbtTransaction.ts +82 -0
  240. package/src/psbt/bip371.ts +7 -3
  241. package/src/psbt/psbtutils.ts +1 -1
  242. package/src/psbt/types.ts +14 -21
  243. package/src/psbt/validation.ts +5 -12
  244. package/src/psbt.ts +363 -1130
  245. package/src/script.ts +6 -9
  246. package/src/transaction.ts +18 -14
  247. package/src/types.ts +28 -17
  248. package/src/workers/WorkerSigningPool.node.ts +31 -31
  249. package/src/workers/WorkerSigningPool.ts +35 -39
  250. package/src/workers/index.node.ts +27 -0
  251. package/src/workers/index.ts +7 -9
  252. package/src/workers/psbt-parallel.ts +2 -7
  253. package/src/workers/types.ts +5 -1
  254. package/test/address.spec.ts +2 -2
  255. package/test/bitcoin.core.spec.ts +5 -2
  256. package/test/browser/payments.spec.ts +151 -0
  257. package/test/browser/psbt.spec.ts +1510 -0
  258. package/test/browser/script.spec.ts +223 -0
  259. package/test/browser/setup.ts +13 -0
  260. package/test/browser/workers-signing.spec.ts +537 -0
  261. package/test/crypto.spec.ts +2 -2
  262. package/test/fixtures/core/base58_encode_decode.json +12 -48
  263. package/test/fixtures/core/base58_keys_invalid.json +50 -150
  264. package/test/fixtures/core/sighash.json +1 -3
  265. package/test/fixtures/core/tx_valid.json +133 -501
  266. package/test/fixtures/embed.json +3 -11
  267. package/test/fixtures/p2ms.json +21 -91
  268. package/test/fixtures/p2pk.json +5 -24
  269. package/test/fixtures/p2pkh.json +7 -36
  270. package/test/fixtures/p2sh.json +8 -54
  271. package/test/fixtures/p2tr.json +2 -6
  272. package/test/fixtures/p2wpkh.json +7 -36
  273. package/test/fixtures/p2wsh.json +14 -59
  274. package/test/fixtures/psbt.json +2 -6
  275. package/test/fixtures/script.json +12 -48
  276. package/test/integration/addresses.spec.ts +11 -5
  277. package/test/integration/bip32.spec.ts +1 -1
  278. package/test/integration/cltv.spec.ts +10 -6
  279. package/test/integration/csv.spec.ts +10 -9
  280. package/test/integration/payments.spec.ts +8 -4
  281. package/test/integration/taproot.spec.ts +26 -6
  282. package/test/integration/transactions.spec.ts +22 -8
  283. package/test/payments.spec.ts +1 -1
  284. package/test/payments.utils.ts +1 -1
  285. package/test/psbt.spec.ts +250 -64
  286. package/test/script_signature.spec.ts +1 -1
  287. package/test/transaction.spec.ts +18 -5
  288. package/test/tsconfig.json +6 -20
  289. package/test/workers-pool.spec.ts +22 -23
  290. package/test/workers-signing.spec.ts +7 -3
  291. package/test/workers.spec.ts +6 -7
  292. package/typedoc.json +39 -0
  293. package/vitest.config.browser.ts +68 -0
  294. package/browser/ecpair.d.ts +0 -99
  295. package/src/ecpair.d.ts +0 -99
  296. package/test/taproot-cache.spec.ts +0 -694
@@ -77,6 +77,38 @@ export class MemoryPool {
77
77
  this.#maxSize = size;
78
78
  }
79
79
 
80
+ /**
81
+ * Total capacity of the pool in bytes.
82
+ */
83
+ public get capacity(): number {
84
+ return this.#maxSize;
85
+ }
86
+
87
+ /**
88
+ * Current allocation offset (bytes used).
89
+ *
90
+ * Uses Atomics for thread-safe reading.
91
+ */
92
+ public get used(): number {
93
+ return Atomics.load(this.#control, 0);
94
+ }
95
+
96
+ /**
97
+ * Remaining available bytes.
98
+ */
99
+ public get available(): number {
100
+ return this.#maxSize - this.used;
101
+ }
102
+
103
+ /**
104
+ * The underlying SharedArrayBuffer.
105
+ *
106
+ * Can be transferred to Workers for shared memory access.
107
+ */
108
+ public get sharedBuffer(): SharedArrayBuffer {
109
+ return this.#buffer;
110
+ }
111
+
80
112
  /**
81
113
  * Creates a MemoryPool from an existing SharedArrayBuffer.
82
114
  *
@@ -111,38 +143,6 @@ export class MemoryPool {
111
143
  return pool;
112
144
  }
113
145
 
114
- /**
115
- * Total capacity of the pool in bytes.
116
- */
117
- public get capacity(): number {
118
- return this.#maxSize;
119
- }
120
-
121
- /**
122
- * Current allocation offset (bytes used).
123
- *
124
- * Uses Atomics for thread-safe reading.
125
- */
126
- public get used(): number {
127
- return Atomics.load(this.#control, 0);
128
- }
129
-
130
- /**
131
- * Remaining available bytes.
132
- */
133
- public get available(): number {
134
- return this.#maxSize - this.used;
135
- }
136
-
137
- /**
138
- * The underlying SharedArrayBuffer.
139
- *
140
- * Can be transferred to Workers for shared memory access.
141
- */
142
- public get sharedBuffer(): SharedArrayBuffer {
143
- return this.#buffer;
144
- }
145
-
146
146
  /**
147
147
  * Allocates memory from the pool.
148
148
  *
package/src/opcodes.ts CHANGED
@@ -273,8 +273,25 @@ export const opcodes: Opcodes = {
273
273
  OP_INVALIDOPCODE: 255,
274
274
  };
275
275
 
276
- export const REVERSE_OPS: { [key: number]: string } = {};
277
- for (const op of Object.keys(opcodes)) {
278
- const code = opcodes[op as keyof Opcodes];
279
- REVERSE_OPS[code] = op;
276
+ let _reverseOps: { [key: number]: string } | undefined;
277
+
278
+ /**
279
+ * Returns the reverse mapping from opcode number to opcode name.
280
+ * Lazily computed on first call.
281
+ */
282
+ export function getReverseOps(): { [key: number]: string } {
283
+ if (!_reverseOps) {
284
+ _reverseOps = {};
285
+ for (const op of Object.keys(opcodes)) {
286
+ const code = opcodes[op as keyof Opcodes];
287
+ _reverseOps[code] = op;
288
+ }
289
+ }
290
+ return _reverseOps;
280
291
  }
292
+
293
+ /**
294
+ * @deprecated Use {@link getReverseOps}() for lazy initialization.
295
+ * This eagerly-initialized alias exists for backward compatibility.
296
+ */
297
+ export const REVERSE_OPS: { [key: number]: string } = getReverseOps();
@@ -1,10 +1,8 @@
1
1
  import * as bcrypto from '../crypto.js';
2
2
  import { getEccLib } from '../ecc/context.js';
3
- import { concat, compare, equals, alloc } from '../io/index.js';
4
- import { varuint } from '../io/index.js';
3
+ import { alloc, compare, concat, equals, varuint } from '../io/index.js';
4
+ import type { Bytes32, Tapleaf, Taptree, XOnlyPublicKey } from '../types.js';
5
5
  import { isTapleaf } from '../types.js';
6
- import type { Tapleaf, Taptree } from '../types.js';
7
- import type { Bytes32, XOnlyPublicKey } from '../types.js';
8
6
 
9
7
  export const LEAF_VERSION_TAPSCRIPT = 0xc0;
10
8
  export const MAX_TAPTREE_DEPTH = 128;
@@ -9,9 +9,9 @@
9
9
 
10
10
  import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
11
11
  import * as bscript from '../script.js';
12
- import { stacksEqual, type Stack } from '../types.js';
13
12
  import type { Script } from '../types.js';
14
- import { PaymentType, type EmbedPayment, type PaymentOpts } from './types.js';
13
+ import { type Stack, stacksEqual } from '../types.js';
14
+ import { type EmbedPayment, type PaymentOpts, PaymentType } from './types.js';
15
15
 
16
16
  const OPS = bscript.opcodes;
17
17
 
@@ -162,6 +162,20 @@ export class Embed {
162
162
 
163
163
  // Private computation methods
164
164
 
165
+ /**
166
+ * Converts to a plain EmbedPayment object for backwards compatibility.
167
+ *
168
+ * @returns An EmbedPayment object
169
+ */
170
+ toPayment(): EmbedPayment {
171
+ return {
172
+ name: this.name,
173
+ network: this.network,
174
+ data: this.data,
175
+ output: this.output,
176
+ };
177
+ }
178
+
165
179
  #computeData(): Uint8Array[] | undefined {
166
180
  if (this.#inputData) {
167
181
  return this.#inputData;
@@ -176,6 +190,8 @@ export class Embed {
176
190
  return undefined;
177
191
  }
178
192
 
193
+ // Validation
194
+
179
195
  #computeOutput(): Uint8Array | undefined {
180
196
  if (this.#inputOutput) {
181
197
  return this.#inputOutput;
@@ -186,8 +202,6 @@ export class Embed {
186
202
  return bscript.compile(([OPS.OP_RETURN] as Stack).concat(this.#inputData)) as Script;
187
203
  }
188
204
 
189
- // Validation
190
-
191
205
  #validate(): void {
192
206
  if (this.#inputOutput) {
193
207
  const chunks = bscript.decompile(this.#inputOutput);
@@ -206,20 +220,6 @@ export class Embed {
206
220
  }
207
221
  }
208
222
  }
209
-
210
- /**
211
- * Converts to a plain EmbedPayment object for backwards compatibility.
212
- *
213
- * @returns An EmbedPayment object
214
- */
215
- toPayment(): EmbedPayment {
216
- return {
217
- name: this.name,
218
- network: this.network,
219
- data: this.data,
220
- output: this.output,
221
- };
222
- }
223
223
  }
224
224
 
225
225
  /**
@@ -9,8 +9,15 @@
9
9
 
10
10
  import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
11
11
  import * as bscript from '../script.js';
12
- import { isPoint, stacksEqual, type PublicKey, type Script, type Signature, type Stack } from '../types.js';
13
- import { PaymentType, type P2MSPayment, type PaymentOpts } from './types.js';
12
+ import {
13
+ isPoint,
14
+ type PublicKey,
15
+ type Script,
16
+ type Signature,
17
+ type Stack,
18
+ stacksEqual,
19
+ } from '../types.js';
20
+ import { type P2MSPayment, type PaymentOpts, PaymentType } from './types.js';
14
21
 
15
22
  const OPS = bscript.opcodes;
16
23
  const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
@@ -274,6 +281,27 @@ export class P2MS {
274
281
 
275
282
  // Private helper methods
276
283
 
284
+ /**
285
+ * Converts to a plain P2MSPayment object for backwards compatibility.
286
+ *
287
+ * @returns A P2MSPayment object
288
+ */
289
+ toPayment(): P2MSPayment {
290
+ return {
291
+ name: this.name,
292
+ network: this.network,
293
+ m: this.m,
294
+ n: this.n,
295
+ pubkeys: this.pubkeys,
296
+ signatures: this.signatures,
297
+ output: this.output,
298
+ input: this.input,
299
+ witness: this.witness,
300
+ };
301
+ }
302
+
303
+ // Private computation methods
304
+
277
305
  #decode(output: Uint8Array | Stack): void {
278
306
  if (this.#decoded) return;
279
307
  this.#decoded = true;
@@ -286,8 +314,6 @@ export class P2MS {
286
314
  this.#pubkeysComputed = true;
287
315
  }
288
316
 
289
- // Private computation methods
290
-
291
317
  #computeM(): number | undefined {
292
318
  if (this.#inputM !== undefined) {
293
319
  return this.#inputM;
@@ -364,6 +390,8 @@ export class P2MS {
364
390
  return bscript.compile(([OPS.OP_0] as Stack).concat(this.#inputSignatures)) as Script;
365
391
  }
366
392
 
393
+ // Validation
394
+
367
395
  #computeWitness(): Uint8Array[] | undefined {
368
396
  if (this.input) {
369
397
  return [];
@@ -371,8 +399,6 @@ export class P2MS {
371
399
  return undefined;
372
400
  }
373
401
 
374
- // Validation
375
-
376
402
  #isAcceptableSignature(x: Uint8Array | number): boolean {
377
403
  return (
378
404
  bscript.isCanonicalScriptSignature(x as Uint8Array) ||
@@ -465,25 +491,6 @@ export class P2MS {
465
491
  }
466
492
  }
467
493
  }
468
-
469
- /**
470
- * Converts to a plain P2MSPayment object for backwards compatibility.
471
- *
472
- * @returns A P2MSPayment object
473
- */
474
- toPayment(): P2MSPayment {
475
- return {
476
- name: this.name,
477
- network: this.network,
478
- m: this.m,
479
- n: this.n,
480
- pubkeys: this.pubkeys,
481
- signatures: this.signatures,
482
- output: this.output,
483
- input: this.input,
484
- witness: this.witness,
485
- };
486
- }
487
494
  }
488
495
 
489
496
  /**
@@ -13,7 +13,7 @@ import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
13
13
  import * as bscript from '../script.js';
14
14
  import { alloc, concat, equals } from '../io/index.js';
15
15
  import type { Bytes20, Script } from '../types.js';
16
- import { PaymentType, type P2OPPayment, type PaymentOpts } from './types.js';
16
+ import { type P2OPPayment, type PaymentOpts, PaymentType } from './types.js';
17
17
 
18
18
  const OPS = bscript.opcodes;
19
19
  const P2OP_WITNESS_VERSION = 0x10;
@@ -241,6 +241,23 @@ export class P2OP {
241
241
 
242
242
  // Private helper methods
243
243
 
244
+ /**
245
+ * Converts to a plain P2OPPayment object for backwards compatibility.
246
+ *
247
+ * @returns A P2OPPayment object
248
+ */
249
+ toPayment(): P2OPPayment {
250
+ return {
251
+ name: this.name,
252
+ network: this.network,
253
+ address: this.address,
254
+ program: this.program,
255
+ deploymentVersion: this.deploymentVersion,
256
+ hash160: this.hash160,
257
+ output: this.output as Script | undefined,
258
+ };
259
+ }
260
+
244
261
  #getDecodedAddress(): { version: number; prefix: string; data: Uint8Array } | undefined {
245
262
  if (!this.#decodedAddressComputed) {
246
263
  if (this.#inputAddress) {
@@ -258,6 +275,8 @@ export class P2OP {
258
275
  return this.#decodedAddress;
259
276
  }
260
277
 
278
+ // Private computation methods
279
+
261
280
  #makeProgramFromParts(): Uint8Array | undefined {
262
281
  if (
263
282
  typeof this.#inputDeploymentVersion !== 'undefined' &&
@@ -274,8 +293,6 @@ export class P2OP {
274
293
  return undefined;
275
294
  }
276
295
 
277
- // Private computation methods
278
-
279
296
  #computeAddress(): string | undefined {
280
297
  if (this.#inputAddress) {
281
298
  return this.#inputAddress;
@@ -346,6 +363,8 @@ export class P2OP {
346
363
  return prog.subarray(1) as Bytes20;
347
364
  }
348
365
 
366
+ // Validation
367
+
349
368
  #computeOutput(): Uint8Array | undefined {
350
369
  if (this.#inputOutput) {
351
370
  return this.#inputOutput;
@@ -356,8 +375,6 @@ export class P2OP {
356
375
  return bscript.compile([OPS.OP_16, prog]);
357
376
  }
358
377
 
359
- // Validation
360
-
361
378
  #validate(): void {
362
379
  let prog: Uint8Array = alloc(0);
363
380
 
@@ -416,23 +433,6 @@ export class P2OP {
416
433
  throw new TypeError('hash160 mismatch');
417
434
  }
418
435
  }
419
-
420
- /**
421
- * Converts to a plain P2OPPayment object for backwards compatibility.
422
- *
423
- * @returns A P2OPPayment object
424
- */
425
- toPayment(): P2OPPayment {
426
- return {
427
- name: this.name,
428
- network: this.network,
429
- address: this.address,
430
- program: this.program,
431
- deploymentVersion: this.deploymentVersion,
432
- hash160: this.hash160,
433
- output: this.output as Script | undefined,
434
- };
435
- }
436
436
  }
437
437
 
438
438
  /**
@@ -11,7 +11,7 @@ import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
11
11
  import * as bscript from '../script.js';
12
12
  import { isPoint, type PublicKey, type Script, type Signature } from '../types.js';
13
13
  import { equals } from '../io/index.js';
14
- import { PaymentType, type P2PKPayment, type PaymentOpts } from './types.js';
14
+ import { type P2PKPayment, type PaymentOpts, PaymentType } from './types.js';
15
15
 
16
16
  const OPS = bscript.opcodes;
17
17
 
@@ -238,6 +238,23 @@ export class P2PK {
238
238
 
239
239
  // Private computation methods
240
240
 
241
+ /**
242
+ * Converts to a plain P2PKPayment object for backwards compatibility.
243
+ *
244
+ * @returns A P2PKPayment object
245
+ */
246
+ toPayment(): P2PKPayment {
247
+ return {
248
+ name: this.name,
249
+ network: this.network,
250
+ pubkey: this.pubkey,
251
+ signature: this.signature,
252
+ output: this.output,
253
+ input: this.input,
254
+ witness: this.witness,
255
+ };
256
+ }
257
+
241
258
  #computePubkey(): PublicKey | undefined {
242
259
  if (this.#inputPubkey) {
243
260
  return this.#inputPubkey as PublicKey;
@@ -284,6 +301,8 @@ export class P2PK {
284
301
  return undefined;
285
302
  }
286
303
 
304
+ // Validation
305
+
287
306
  #computeWitness(): Uint8Array[] | undefined {
288
307
  if (this.input) {
289
308
  return [];
@@ -291,8 +310,6 @@ export class P2PK {
291
310
  return undefined;
292
311
  }
293
312
 
294
- // Validation
295
-
296
313
  #validate(): void {
297
314
  if (this.#inputOutput) {
298
315
  if (this.#inputOutput[this.#inputOutput.length - 1] !== OPS.OP_CHECKSIG) {
@@ -329,23 +346,6 @@ export class P2PK {
329
346
  }
330
347
  }
331
348
  }
332
-
333
- /**
334
- * Converts to a plain P2PKPayment object for backwards compatibility.
335
- *
336
- * @returns A P2PKPayment object
337
- */
338
- toPayment(): P2PKPayment {
339
- return {
340
- name: this.name,
341
- network: this.network,
342
- pubkey: this.pubkey,
343
- signature: this.signature,
344
- output: this.output,
345
- input: this.input,
346
- witness: this.witness,
347
- };
348
- }
349
349
  }
350
350
 
351
351
  /**
@@ -13,9 +13,9 @@ import * as bcrypto from '../crypto.js';
13
13
  import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
14
14
  import { decompressPublicKey } from '../pubkey.js';
15
15
  import * as bscript from '../script.js';
16
- import { isPoint, type Bytes20, type PublicKey, type Script, type Signature } from '../types.js';
16
+ import { type Bytes20, isPoint, type PublicKey, type Script, type Signature } from '../types.js';
17
17
  import { alloc, equals } from '../io/index.js';
18
- import { PaymentType, type P2PKHPayment, type PaymentOpts } from './types.js';
18
+ import { type P2PKHPayment, type PaymentOpts, PaymentType } from './types.js';
19
19
 
20
20
  const OPS = bscript.opcodes;
21
21
 
@@ -303,6 +303,25 @@ export class P2PKH {
303
303
 
304
304
  // Private helper methods
305
305
 
306
+ /**
307
+ * Converts to a plain P2PKHPayment object for backwards compatibility.
308
+ *
309
+ * @returns A P2PKHPayment object
310
+ */
311
+ toPayment(): P2PKHPayment {
312
+ return {
313
+ name: this.name,
314
+ network: this.network,
315
+ address: this.address,
316
+ hash: this.hash,
317
+ pubkey: this.pubkey,
318
+ signature: this.signature,
319
+ output: this.output,
320
+ input: this.input,
321
+ witness: this.witness,
322
+ };
323
+ }
324
+
306
325
  #getDecodedAddress(): { version: number; hash: Uint8Array } | undefined {
307
326
  if (!this.#decodedAddressComputed) {
308
327
  if (this.#inputAddress) {
@@ -317,6 +336,8 @@ export class P2PKH {
317
336
  return this.#decodedAddress;
318
337
  }
319
338
 
339
+ // Private computation methods
340
+
320
341
  #getInputChunks(): (Uint8Array | number)[] | undefined {
321
342
  if (!this.#inputChunksComputed) {
322
343
  if (this.#inputInput) {
@@ -327,8 +348,6 @@ export class P2PKH {
327
348
  return this.#inputChunks;
328
349
  }
329
350
 
330
- // Private computation methods
331
-
332
351
  #computeAddress(): string | undefined {
333
352
  if (this.#inputAddress) {
334
353
  return this.#inputAddress;
@@ -425,6 +444,8 @@ export class P2PKH {
425
444
  return bscript.compile([this.#inputSignature, pubKey]) as Script;
426
445
  }
427
446
 
447
+ // Validation
448
+
428
449
  #computeWitness(): Uint8Array[] | undefined {
429
450
  if (this.input) {
430
451
  return [];
@@ -432,8 +453,6 @@ export class P2PKH {
432
453
  return undefined;
433
454
  }
434
455
 
435
- // Validation
436
-
437
456
  #validate(): void {
438
457
  let hash: Uint8Array = new Uint8Array(0);
439
458
 
@@ -540,25 +559,6 @@ export class P2PKH {
540
559
  }
541
560
  }
542
561
  }
543
-
544
- /**
545
- * Converts to a plain P2PKHPayment object for backwards compatibility.
546
- *
547
- * @returns A P2PKHPayment object
548
- */
549
- toPayment(): P2PKHPayment {
550
- return {
551
- name: this.name,
552
- network: this.network,
553
- address: this.address,
554
- hash: this.hash,
555
- pubkey: this.pubkey,
556
- signature: this.signature,
557
- output: this.output,
558
- input: this.input,
559
- witness: this.witness,
560
- };
561
- }
562
562
  }
563
563
 
564
564
  /**
@@ -12,13 +12,13 @@ import * as bs58check from 'bs58check';
12
12
  import * as bcrypto from '../crypto.js';
13
13
  import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
14
14
  import * as bscript from '../script.js';
15
- import { stacksEqual, type Bytes20, type Script, type Stack } from '../types.js';
15
+ import { type Bytes20, type Script, type Stack, stacksEqual } from '../types.js';
16
16
  import { alloc, equals } from '../io/index.js';
17
17
  import {
18
- PaymentType,
19
18
  type P2SHPayment,
20
19
  type Payment,
21
20
  type PaymentOpts,
21
+ PaymentType,
22
22
  type ScriptRedeem,
23
23
  } from './types.js';
24
24
 
@@ -282,6 +282,24 @@ export class P2SH {
282
282
 
283
283
  // Private helper methods
284
284
 
285
+ /**
286
+ * Converts to a plain P2SHPayment object for backwards compatibility.
287
+ *
288
+ * @returns A P2SHPayment object
289
+ */
290
+ toPayment(): P2SHPayment {
291
+ return {
292
+ name: this.name,
293
+ network: this.network,
294
+ address: this.address,
295
+ hash: this.hash,
296
+ output: this.output,
297
+ input: this.input,
298
+ redeem: this.redeem,
299
+ witness: this.witness,
300
+ };
301
+ }
302
+
285
303
  #getDecodedAddress(): { version: number; hash: Uint8Array } | undefined {
286
304
  if (!this.#decodedAddressComputed) {
287
305
  if (this.#inputAddress) {
@@ -306,6 +324,8 @@ export class P2SH {
306
324
  return this.#inputChunks;
307
325
  }
308
326
 
327
+ // Private computation methods
328
+
309
329
  #getDerivedRedeem(): ScriptRedeem | undefined {
310
330
  if (!this.#derivedRedeemComputed) {
311
331
  const chunks = this.#getInputChunks();
@@ -313,8 +333,9 @@ export class P2SH {
313
333
  const lastChunk = chunks[chunks.length - 1];
314
334
  this.#derivedRedeem = {
315
335
  network: this.#network,
316
- output:
317
- (lastChunk === OPS.OP_FALSE ? new Uint8Array(0) : (lastChunk as Uint8Array)) as Script,
336
+ output: (lastChunk === OPS.OP_FALSE
337
+ ? new Uint8Array(0)
338
+ : (lastChunk as Uint8Array)) as Script,
318
339
  input: bscript.compile(chunks.slice(0, -1)) as Script,
319
340
  witness: this.#inputWitness || [],
320
341
  };
@@ -324,8 +345,6 @@ export class P2SH {
324
345
  return this.#derivedRedeem;
325
346
  }
326
347
 
327
- // Private computation methods
328
-
329
348
  #computeAddress(): string | undefined {
330
349
  if (this.#inputAddress) {
331
350
  return this.#inputAddress;
@@ -374,7 +393,9 @@ export class P2SH {
374
393
  if (!r || !r.input || !r.output) {
375
394
  return undefined;
376
395
  }
377
- return bscript.compile(([] as Stack).concat(bscript.decompile(r.input) as Stack, r.output)) as Script;
396
+ return bscript.compile(
397
+ ([] as Stack).concat(bscript.decompile(r.input) as Stack, r.output),
398
+ ) as Script;
378
399
  }
379
400
 
380
401
  #computeRedeem(): ScriptRedeem | undefined {
@@ -387,6 +408,8 @@ export class P2SH {
387
408
  return undefined;
388
409
  }
389
410
 
411
+ // Validation
412
+
390
413
  #computeWitness(): Uint8Array[] | undefined {
391
414
  if (this.#inputWitness) {
392
415
  return this.#inputWitness;
@@ -401,8 +424,6 @@ export class P2SH {
401
424
  return undefined;
402
425
  }
403
426
 
404
- // Validation
405
-
406
427
  #checkRedeem(redeem: Payment): void {
407
428
  // Is the redeem output empty/invalid?
408
429
  if (redeem.output) {
@@ -547,24 +568,6 @@ export class P2SH {
547
568
  }
548
569
  }
549
570
  }
550
-
551
- /**
552
- * Converts to a plain P2SHPayment object for backwards compatibility.
553
- *
554
- * @returns A P2SHPayment object
555
- */
556
- toPayment(): P2SHPayment {
557
- return {
558
- name: this.name,
559
- network: this.network,
560
- address: this.address,
561
- hash: this.hash,
562
- output: this.output,
563
- input: this.input,
564
- redeem: this.redeem,
565
- witness: this.witness,
566
- };
567
- }
568
571
  }
569
572
 
570
573
  /**