@btc-vision/bitcoin 7.0.0-alpha.0 → 7.0.0-alpha.10

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 (369) hide show
  1. package/README.md +455 -155
  2. package/browser/address.d.ts +6 -2
  3. package/browser/address.d.ts.map +1 -1
  4. package/browser/block.d.ts.map +1 -1
  5. package/browser/branded.d.ts +3 -14
  6. package/browser/branded.d.ts.map +1 -1
  7. package/browser/chunks/psbt-parallel-BBFlkmiv.js +10717 -0
  8. package/browser/crypto.d.ts +1 -1
  9. package/browser/ecc/context.d.ts +25 -24
  10. package/browser/ecc/context.d.ts.map +1 -1
  11. package/browser/ecc/index.d.ts +1 -1
  12. package/browser/ecc/index.d.ts.map +1 -1
  13. package/browser/ecc/types.d.ts +10 -123
  14. package/browser/ecc/types.d.ts.map +1 -1
  15. package/browser/env.d.ts +13 -0
  16. package/browser/env.d.ts.map +1 -0
  17. package/browser/index.d.ts +7 -7
  18. package/browser/index.d.ts.map +1 -1
  19. package/browser/index.js +2497 -11686
  20. package/browser/io/BinaryReader.d.ts +15 -15
  21. package/browser/io/BinaryReader.d.ts.map +1 -1
  22. package/browser/io/BinaryWriter.d.ts +17 -17
  23. package/browser/io/BinaryWriter.d.ts.map +1 -1
  24. package/browser/io/hex.d.ts.map +1 -1
  25. package/browser/io/index.d.ts +0 -1
  26. package/browser/io/index.d.ts.map +1 -1
  27. package/browser/opcodes.d.ts +11 -0
  28. package/browser/opcodes.d.ts.map +1 -1
  29. package/browser/payments/bip341.d.ts +1 -1
  30. package/browser/payments/bip341.d.ts.map +1 -1
  31. package/browser/payments/embed.d.ts +1 -1
  32. package/browser/payments/embed.d.ts.map +1 -1
  33. package/browser/payments/p2ms.d.ts.map +1 -1
  34. package/browser/payments/p2op.d.ts +1 -1
  35. package/browser/payments/p2op.d.ts.map +1 -1
  36. package/browser/payments/p2pk.d.ts +1 -1
  37. package/browser/payments/p2pk.d.ts.map +1 -1
  38. package/browser/payments/p2pkh.d.ts +1 -1
  39. package/browser/payments/p2pkh.d.ts.map +1 -1
  40. package/browser/payments/p2sh.d.ts.map +1 -1
  41. package/browser/payments/p2tr.d.ts +2 -2
  42. package/browser/payments/p2tr.d.ts.map +1 -1
  43. package/browser/payments/p2wpkh.d.ts +1 -1
  44. package/browser/payments/p2wpkh.d.ts.map +1 -1
  45. package/browser/payments/p2wsh.d.ts.map +1 -1
  46. package/browser/payments/types.d.ts +1 -1
  47. package/browser/payments/types.d.ts.map +1 -1
  48. package/browser/psbt/PsbtCache.d.ts +54 -0
  49. package/browser/psbt/PsbtCache.d.ts.map +1 -0
  50. package/browser/psbt/PsbtFinalizer.d.ts +21 -0
  51. package/browser/psbt/PsbtFinalizer.d.ts.map +1 -0
  52. package/browser/psbt/PsbtSigner.d.ts +32 -0
  53. package/browser/psbt/PsbtSigner.d.ts.map +1 -0
  54. package/browser/psbt/PsbtTransaction.d.ts +25 -0
  55. package/browser/psbt/PsbtTransaction.d.ts.map +1 -0
  56. package/browser/psbt/bip371.d.ts.map +1 -1
  57. package/browser/psbt/types.d.ts +5 -71
  58. package/browser/psbt/types.d.ts.map +1 -1
  59. package/browser/psbt/validation.d.ts +1 -1
  60. package/browser/psbt/validation.d.ts.map +1 -1
  61. package/browser/psbt.d.ts +26 -40
  62. package/browser/psbt.d.ts.map +1 -1
  63. package/browser/script.d.ts.map +1 -1
  64. package/browser/transaction.d.ts +4 -4
  65. package/browser/transaction.d.ts.map +1 -1
  66. package/browser/types.d.ts +5 -3
  67. package/browser/types.d.ts.map +1 -1
  68. package/browser/workers/WorkerSigningPool.d.ts +24 -17
  69. package/browser/workers/WorkerSigningPool.d.ts.map +1 -1
  70. package/browser/workers/WorkerSigningPool.node.d.ts +19 -12
  71. package/browser/workers/WorkerSigningPool.node.d.ts.map +1 -1
  72. package/browser/workers/WorkerSigningPool.sequential.d.ts +67 -0
  73. package/browser/workers/WorkerSigningPool.sequential.d.ts.map +1 -0
  74. package/browser/workers/WorkerSigningPool.worklet.d.ts +64 -0
  75. package/browser/workers/WorkerSigningPool.worklet.d.ts.map +1 -0
  76. package/browser/workers/index.browser.d.ts +16 -0
  77. package/browser/workers/index.browser.d.ts.map +1 -0
  78. package/browser/workers/index.d.ts +4 -64
  79. package/browser/workers/index.d.ts.map +1 -1
  80. package/browser/workers/index.js +28 -0
  81. package/browser/workers/index.node.d.ts +17 -0
  82. package/browser/workers/index.node.d.ts.map +1 -0
  83. package/browser/workers/index.react-native.d.ts +28 -0
  84. package/browser/workers/index.react-native.d.ts.map +1 -0
  85. package/browser/workers/index.shared.d.ts +15 -0
  86. package/browser/workers/index.shared.d.ts.map +1 -0
  87. package/browser/workers/psbt-parallel.d.ts +2 -3
  88. package/browser/workers/psbt-parallel.d.ts.map +1 -1
  89. package/browser/workers/types.d.ts +17 -0
  90. package/browser/workers/types.d.ts.map +1 -1
  91. package/build/address.d.ts +6 -2
  92. package/build/address.d.ts.map +1 -1
  93. package/build/address.js +32 -19
  94. package/build/address.js.map +1 -1
  95. package/build/bech32utils.js.map +1 -1
  96. package/build/block.d.ts.map +1 -1
  97. package/build/block.js +2 -4
  98. package/build/block.js.map +1 -1
  99. package/build/branded.d.ts +3 -14
  100. package/build/branded.d.ts.map +1 -1
  101. package/build/branded.js +0 -5
  102. package/build/branded.js.map +1 -1
  103. package/build/crypto.d.ts +1 -1
  104. package/build/ecc/context.d.ts +25 -24
  105. package/build/ecc/context.d.ts.map +1 -1
  106. package/build/ecc/context.js +29 -101
  107. package/build/ecc/context.js.map +1 -1
  108. package/build/ecc/index.d.ts +1 -1
  109. package/build/ecc/index.d.ts.map +1 -1
  110. package/build/ecc/types.d.ts +7 -126
  111. package/build/ecc/types.d.ts.map +1 -1
  112. package/build/ecc/types.js +4 -1
  113. package/build/ecc/types.js.map +1 -1
  114. package/build/env.d.ts +13 -0
  115. package/build/env.d.ts.map +1 -0
  116. package/build/env.js +198 -0
  117. package/build/env.js.map +1 -0
  118. package/build/index.d.ts +8 -7
  119. package/build/index.d.ts.map +1 -1
  120. package/build/index.js +9 -7
  121. package/build/index.js.map +1 -1
  122. package/build/io/BinaryReader.d.ts +15 -15
  123. package/build/io/BinaryReader.d.ts.map +1 -1
  124. package/build/io/BinaryReader.js +17 -17
  125. package/build/io/BinaryReader.js.map +1 -1
  126. package/build/io/BinaryWriter.d.ts +17 -17
  127. package/build/io/BinaryWriter.d.ts.map +1 -1
  128. package/build/io/BinaryWriter.js +39 -39
  129. package/build/io/BinaryWriter.js.map +1 -1
  130. package/build/io/hex.d.ts.map +1 -1
  131. package/build/io/hex.js +2 -1
  132. package/build/io/hex.js.map +1 -1
  133. package/build/io/index.d.ts +0 -1
  134. package/build/io/index.d.ts.map +1 -1
  135. package/build/io/index.js +0 -2
  136. package/build/io/index.js.map +1 -1
  137. package/build/opcodes.d.ts +11 -0
  138. package/build/opcodes.d.ts.map +1 -1
  139. package/build/opcodes.js +19 -4
  140. package/build/opcodes.js.map +1 -1
  141. package/build/payments/bip341.d.ts +1 -2
  142. package/build/payments/bip341.d.ts.map +1 -1
  143. package/build/payments/bip341.js +1 -2
  144. package/build/payments/bip341.js.map +1 -1
  145. package/build/payments/embed.d.ts +1 -1
  146. package/build/payments/embed.d.ts.map +1 -1
  147. package/build/payments/embed.js +14 -14
  148. package/build/payments/embed.js.map +1 -1
  149. package/build/payments/p2ms.d.ts.map +1 -1
  150. package/build/payments/p2ms.js +21 -21
  151. package/build/payments/p2ms.js.map +1 -1
  152. package/build/payments/p2op.d.ts +1 -1
  153. package/build/payments/p2op.d.ts.map +1 -1
  154. package/build/payments/p2op.js +18 -18
  155. package/build/payments/p2op.js.map +1 -1
  156. package/build/payments/p2pk.d.ts +1 -1
  157. package/build/payments/p2pk.d.ts.map +1 -1
  158. package/build/payments/p2pk.js +17 -17
  159. package/build/payments/p2pk.js.map +1 -1
  160. package/build/payments/p2pkh.d.ts +1 -1
  161. package/build/payments/p2pkh.d.ts.map +1 -1
  162. package/build/payments/p2pkh.js +20 -20
  163. package/build/payments/p2pkh.js.map +1 -1
  164. package/build/payments/p2sh.d.ts.map +1 -1
  165. package/build/payments/p2sh.js +22 -20
  166. package/build/payments/p2sh.js.map +1 -1
  167. package/build/payments/p2tr.d.ts +2 -2
  168. package/build/payments/p2tr.d.ts.map +1 -1
  169. package/build/payments/p2tr.js +25 -26
  170. package/build/payments/p2tr.js.map +1 -1
  171. package/build/payments/p2wpkh.d.ts +1 -1
  172. package/build/payments/p2wpkh.d.ts.map +1 -1
  173. package/build/payments/p2wpkh.js +20 -20
  174. package/build/payments/p2wpkh.js.map +1 -1
  175. package/build/payments/p2wsh.d.ts.map +1 -1
  176. package/build/payments/p2wsh.js +22 -22
  177. package/build/payments/p2wsh.js.map +1 -1
  178. package/build/payments/types.d.ts +1 -1
  179. package/build/payments/types.d.ts.map +1 -1
  180. package/build/psbt/PsbtCache.d.ts +54 -0
  181. package/build/psbt/PsbtCache.d.ts.map +1 -0
  182. package/build/psbt/PsbtCache.js +249 -0
  183. package/build/psbt/PsbtCache.js.map +1 -0
  184. package/build/psbt/PsbtFinalizer.d.ts +21 -0
  185. package/build/psbt/PsbtFinalizer.d.ts.map +1 -0
  186. package/build/psbt/PsbtFinalizer.js +157 -0
  187. package/build/psbt/PsbtFinalizer.js.map +1 -0
  188. package/build/psbt/PsbtSigner.d.ts +32 -0
  189. package/build/psbt/PsbtSigner.d.ts.map +1 -0
  190. package/build/psbt/PsbtSigner.js +192 -0
  191. package/build/psbt/PsbtSigner.js.map +1 -0
  192. package/build/psbt/PsbtTransaction.d.ts +25 -0
  193. package/build/psbt/PsbtTransaction.d.ts.map +1 -0
  194. package/build/psbt/PsbtTransaction.js +61 -0
  195. package/build/psbt/PsbtTransaction.js.map +1 -0
  196. package/build/psbt/bip371.d.ts.map +1 -1
  197. package/build/psbt/bip371.js +6 -2
  198. package/build/psbt/bip371.js.map +1 -1
  199. package/build/psbt/psbtutils.js +1 -1
  200. package/build/psbt/psbtutils.js.map +1 -1
  201. package/build/psbt/types.d.ts +5 -71
  202. package/build/psbt/types.d.ts.map +1 -1
  203. package/build/psbt/validation.d.ts +1 -1
  204. package/build/psbt/validation.d.ts.map +1 -1
  205. package/build/psbt/validation.js +1 -1
  206. package/build/psbt/validation.js.map +1 -1
  207. package/build/psbt.d.ts +26 -40
  208. package/build/psbt.d.ts.map +1 -1
  209. package/build/psbt.js +180 -808
  210. package/build/psbt.js.map +1 -1
  211. package/build/script.d.ts.map +1 -1
  212. package/build/script.js +4 -4
  213. package/build/script.js.map +1 -1
  214. package/build/transaction.d.ts +4 -4
  215. package/build/transaction.d.ts.map +1 -1
  216. package/build/transaction.js +6 -5
  217. package/build/transaction.js.map +1 -1
  218. package/build/tsconfig.build.tsbuildinfo +1 -1
  219. package/build/types.d.ts +5 -3
  220. package/build/types.d.ts.map +1 -1
  221. package/build/types.js +14 -25
  222. package/build/types.js.map +1 -1
  223. package/build/workers/WorkerSigningPool.d.ts +24 -17
  224. package/build/workers/WorkerSigningPool.d.ts.map +1 -1
  225. package/build/workers/WorkerSigningPool.js +36 -25
  226. package/build/workers/WorkerSigningPool.js.map +1 -1
  227. package/build/workers/WorkerSigningPool.node.d.ts +19 -12
  228. package/build/workers/WorkerSigningPool.node.d.ts.map +1 -1
  229. package/build/workers/WorkerSigningPool.node.js +60 -28
  230. package/build/workers/WorkerSigningPool.node.js.map +1 -1
  231. package/build/workers/WorkerSigningPool.sequential.d.ts +76 -0
  232. package/build/workers/WorkerSigningPool.sequential.d.ts.map +1 -0
  233. package/build/workers/WorkerSigningPool.sequential.js +160 -0
  234. package/build/workers/WorkerSigningPool.sequential.js.map +1 -0
  235. package/build/workers/WorkerSigningPool.worklet.d.ts +79 -0
  236. package/build/workers/WorkerSigningPool.worklet.d.ts.map +1 -0
  237. package/build/workers/WorkerSigningPool.worklet.js +390 -0
  238. package/build/workers/WorkerSigningPool.worklet.js.map +1 -0
  239. package/build/workers/index.browser.d.ts +24 -0
  240. package/build/workers/index.browser.d.ts.map +1 -0
  241. package/build/workers/index.browser.js +30 -0
  242. package/build/workers/index.browser.js.map +1 -0
  243. package/build/workers/index.d.ts +6 -18
  244. package/build/workers/index.d.ts.map +1 -1
  245. package/build/workers/index.js +12 -14
  246. package/build/workers/index.js.map +1 -1
  247. package/build/workers/index.node.d.ts +38 -0
  248. package/build/workers/index.node.d.ts.map +1 -0
  249. package/build/workers/index.node.js +45 -0
  250. package/build/workers/index.node.js.map +1 -0
  251. package/build/workers/index.react-native.d.ts +28 -0
  252. package/build/workers/index.react-native.d.ts.map +1 -0
  253. package/build/workers/index.react-native.js +67 -0
  254. package/build/workers/index.react-native.js.map +1 -0
  255. package/build/workers/index.shared.d.ts +15 -0
  256. package/build/workers/index.shared.d.ts.map +1 -0
  257. package/build/workers/index.shared.js +20 -0
  258. package/build/workers/index.shared.js.map +1 -0
  259. package/build/workers/psbt-parallel.d.ts +2 -3
  260. package/build/workers/psbt-parallel.d.ts.map +1 -1
  261. package/build/workers/psbt-parallel.js +4 -4
  262. package/build/workers/psbt-parallel.js.map +1 -1
  263. package/build/workers/types.d.ts +17 -0
  264. package/build/workers/types.d.ts.map +1 -1
  265. package/build/workers/types.js.map +1 -1
  266. package/package.json +48 -9
  267. package/src/address.ts +53 -21
  268. package/src/bech32utils.ts +3 -3
  269. package/src/block.ts +17 -10
  270. package/src/branded.ts +15 -13
  271. package/src/crypto.ts +1 -1
  272. package/src/ecc/context.ts +36 -136
  273. package/src/ecc/index.ts +2 -2
  274. package/src/ecc/types.ts +7 -145
  275. package/src/env.ts +239 -0
  276. package/src/index.ts +57 -22
  277. package/src/io/BinaryReader.ts +18 -18
  278. package/src/io/BinaryWriter.ts +43 -43
  279. package/src/io/hex.ts +2 -1
  280. package/src/io/index.ts +0 -3
  281. package/src/opcodes.ts +21 -4
  282. package/src/payments/bip341.ts +5 -7
  283. package/src/payments/embed.ts +19 -19
  284. package/src/payments/p2ms.ts +34 -27
  285. package/src/payments/p2op.ts +22 -22
  286. package/src/payments/p2pk.ts +22 -22
  287. package/src/payments/p2pkh.ts +28 -28
  288. package/src/payments/p2sh.ts +33 -30
  289. package/src/payments/p2tr.ts +40 -40
  290. package/src/payments/p2wpkh.ts +30 -30
  291. package/src/payments/p2wsh.ts +29 -29
  292. package/src/payments/types.ts +1 -1
  293. package/src/psbt/PsbtCache.ts +325 -0
  294. package/src/psbt/PsbtFinalizer.ts +213 -0
  295. package/src/psbt/PsbtSigner.ts +302 -0
  296. package/src/psbt/PsbtTransaction.ts +82 -0
  297. package/src/psbt/bip371.ts +7 -3
  298. package/src/psbt/psbtutils.ts +1 -1
  299. package/src/psbt/types.ts +5 -94
  300. package/src/psbt/validation.ts +5 -12
  301. package/src/psbt.ts +376 -1201
  302. package/src/script.ts +6 -9
  303. package/src/transaction.ts +19 -15
  304. package/src/types.ts +33 -45
  305. package/src/workers/WorkerSigningPool.node.ts +72 -36
  306. package/src/workers/WorkerSigningPool.sequential.ts +191 -0
  307. package/src/workers/WorkerSigningPool.ts +48 -39
  308. package/src/workers/WorkerSigningPool.worklet.ts +522 -0
  309. package/src/workers/index.browser.ts +34 -0
  310. package/src/workers/index.node.ts +50 -0
  311. package/src/workers/index.react-native.ts +110 -0
  312. package/src/workers/index.shared.ts +58 -0
  313. package/src/workers/index.ts +14 -65
  314. package/src/workers/psbt-parallel.ts +8 -13
  315. package/src/workers/types.ts +26 -1
  316. package/test/address.spec.ts +2 -2
  317. package/test/bitcoin.core.spec.ts +5 -2
  318. package/test/browser/payments.spec.ts +151 -0
  319. package/test/browser/psbt.spec.ts +1510 -0
  320. package/test/browser/script.spec.ts +223 -0
  321. package/test/browser/setup.ts +13 -0
  322. package/test/browser/workers-signing.spec.ts +537 -0
  323. package/test/crypto.spec.ts +2 -2
  324. package/test/env.spec.ts +418 -0
  325. package/test/fixtures/core/base58_encode_decode.json +12 -48
  326. package/test/fixtures/core/base58_keys_invalid.json +50 -150
  327. package/test/fixtures/core/sighash.json +1 -3
  328. package/test/fixtures/core/tx_valid.json +133 -501
  329. package/test/fixtures/embed.json +3 -11
  330. package/test/fixtures/p2ms.json +21 -91
  331. package/test/fixtures/p2pk.json +5 -24
  332. package/test/fixtures/p2pkh.json +7 -36
  333. package/test/fixtures/p2sh.json +8 -54
  334. package/test/fixtures/p2tr.json +2 -6
  335. package/test/fixtures/p2wpkh.json +7 -36
  336. package/test/fixtures/p2wsh.json +14 -59
  337. package/test/fixtures/psbt.json +2 -6
  338. package/test/fixtures/script.json +12 -48
  339. package/test/integration/addresses.spec.ts +11 -5
  340. package/test/integration/bip32.spec.ts +1 -1
  341. package/test/integration/cltv.spec.ts +10 -6
  342. package/test/integration/csv.spec.ts +10 -9
  343. package/test/integration/payments.spec.ts +8 -4
  344. package/test/integration/taproot.spec.ts +26 -6
  345. package/test/integration/transactions.spec.ts +22 -8
  346. package/test/payments.spec.ts +1 -1
  347. package/test/payments.utils.ts +1 -1
  348. package/test/psbt.spec.ts +250 -64
  349. package/test/script_signature.spec.ts +1 -1
  350. package/test/transaction.spec.ts +18 -5
  351. package/test/tsconfig.json +6 -20
  352. package/test/workers-pool.spec.ts +65 -23
  353. package/test/workers-sequential.spec.ts +669 -0
  354. package/test/workers-signing.spec.ts +7 -3
  355. package/test/workers-worklet.spec.ts +500 -0
  356. package/test/workers.spec.ts +6 -7
  357. package/typedoc.json +39 -0
  358. package/vite.config.browser.ts +31 -6
  359. package/vitest.config.browser.ts +68 -0
  360. package/browser/ecpair.d.ts +0 -99
  361. package/browser/io/MemoryPool.d.ts +0 -220
  362. package/browser/io/MemoryPool.d.ts.map +0 -1
  363. package/build/io/MemoryPool.d.ts +0 -220
  364. package/build/io/MemoryPool.d.ts.map +0 -1
  365. package/build/io/MemoryPool.js +0 -309
  366. package/build/io/MemoryPool.js.map +0 -1
  367. package/src/ecpair.d.ts +0 -99
  368. package/src/io/MemoryPool.ts +0 -343
  369. package/test/taproot-cache.spec.ts +0 -694
@@ -1,309 +0,0 @@
1
- /**
2
- * Memory pool using SharedArrayBuffer for zero-allocation operations.
3
- *
4
- * Provides thread-safe memory allocation using Atomics for concurrent access.
5
- * Ideal for parsing operations where temporary buffers are needed.
6
- *
7
- * @packageDocumentation
8
- */
9
- /**
10
- * Thread-safe memory pool using SharedArrayBuffer.
11
- *
12
- * Allocates memory from a pre-allocated SharedArrayBuffer pool,
13
- * eliminating garbage collection pressure from repeated allocations.
14
- *
15
- * Uses Atomics for thread-safe allocation when used with Workers.
16
- *
17
- * @example
18
- * ```typescript
19
- * import { MemoryPool } from '@btc-vision/bitcoin';
20
- *
21
- * // Create a 1MB pool
22
- * const pool = new MemoryPool(1024 * 1024);
23
- *
24
- * // Allocate memory for transaction parsing
25
- * const txBuffer = pool.alloc(500);
26
- * // ... use txBuffer ...
27
- *
28
- * // Reset pool when done (all allocations become invalid)
29
- * pool.reset();
30
- * ```
31
- */
32
- export class MemoryPool {
33
- /**
34
- * The underlying SharedArrayBuffer.
35
- */
36
- #buffer;
37
- /**
38
- * View for data allocations.
39
- */
40
- #data;
41
- /**
42
- * Control array for atomic offset management.
43
- * Located at the end of the buffer.
44
- */
45
- #control;
46
- /**
47
- * Maximum allocatable size (buffer size minus control bytes).
48
- */
49
- #maxSize;
50
- /**
51
- * Creates a new MemoryPool.
52
- *
53
- * @param size - Size of the pool in bytes (default 4MB)
54
- *
55
- * @example
56
- * ```typescript
57
- * import { MemoryPool } from '@btc-vision/bitcoin';
58
- *
59
- * // 4MB pool (default)
60
- * const pool = new MemoryPool();
61
- *
62
- * // 16MB pool
63
- * const largePool = new MemoryPool(16 * 1024 * 1024);
64
- * ```
65
- */
66
- constructor(size = 4 * 1024 * 1024) {
67
- // Add 4 bytes for atomic control (current offset)
68
- const totalSize = size + 4;
69
- this.#buffer = new SharedArrayBuffer(totalSize);
70
- this.#data = new Uint8Array(this.#buffer, 0, size);
71
- this.#control = new Int32Array(this.#buffer, size, 1);
72
- this.#maxSize = size;
73
- }
74
- /**
75
- * Creates a MemoryPool from an existing SharedArrayBuffer.
76
- *
77
- * Useful for sharing a pool between Workers.
78
- *
79
- * @param buffer - Existing SharedArrayBuffer
80
- * @returns A new MemoryPool instance wrapping the buffer
81
- *
82
- * @example
83
- * ```typescript
84
- * // Main thread
85
- * const pool = new MemoryPool(1024 * 1024);
86
- * worker.postMessage({ buffer: pool.sharedBuffer });
87
- *
88
- * // Worker thread
89
- * self.onmessage = (e) => {
90
- * const pool = MemoryPool.fromSharedBuffer(e.data.buffer);
91
- * const mem = pool.alloc(100);
92
- * };
93
- * ```
94
- */
95
- static fromSharedBuffer(buffer) {
96
- const pool = Object.create(MemoryPool.prototype);
97
- const size = buffer.byteLength - 4;
98
- // Use Object.defineProperty to set private fields on the created object
99
- Object.defineProperty(pool, '#buffer', { value: buffer });
100
- Object.defineProperty(pool, '#data', { value: new Uint8Array(buffer, 0, size) });
101
- Object.defineProperty(pool, '#control', { value: new Int32Array(buffer, size, 1) });
102
- Object.defineProperty(pool, '#maxSize', { value: size });
103
- return pool;
104
- }
105
- /**
106
- * Total capacity of the pool in bytes.
107
- */
108
- get capacity() {
109
- return this.#maxSize;
110
- }
111
- /**
112
- * Current allocation offset (bytes used).
113
- *
114
- * Uses Atomics for thread-safe reading.
115
- */
116
- get used() {
117
- return Atomics.load(this.#control, 0);
118
- }
119
- /**
120
- * Remaining available bytes.
121
- */
122
- get available() {
123
- return this.#maxSize - this.used;
124
- }
125
- /**
126
- * The underlying SharedArrayBuffer.
127
- *
128
- * Can be transferred to Workers for shared memory access.
129
- */
130
- get sharedBuffer() {
131
- return this.#buffer;
132
- }
133
- /**
134
- * Allocates memory from the pool.
135
- *
136
- * Thread-safe using Atomics.add for concurrent access.
137
- *
138
- * @param size - Number of bytes to allocate
139
- * @returns Uint8Array view into the pool
140
- * @throws RangeError if pool is exhausted
141
- *
142
- * @example
143
- * ```typescript
144
- * const pool = new MemoryPool(1024);
145
- * const buf1 = pool.alloc(100); // First 100 bytes
146
- * const buf2 = pool.alloc(200); // Next 200 bytes
147
- * ```
148
- */
149
- alloc(size) {
150
- if (size <= 0) {
151
- throw new RangeError('Allocation size must be positive');
152
- }
153
- // Atomically reserve space
154
- const oldOffset = Atomics.add(this.#control, 0, size);
155
- if (oldOffset + size > this.#maxSize) {
156
- // Undo the allocation
157
- Atomics.sub(this.#control, 0, size);
158
- throw new RangeError(`MemoryPool exhausted: requested ${size} bytes, only ${this.#maxSize - oldOffset} available`);
159
- }
160
- return this.#data.subarray(oldOffset, oldOffset + size);
161
- }
162
- /**
163
- * Allocates memory and fills it with zeros.
164
- *
165
- * @param size - Number of bytes to allocate
166
- * @returns Uint8Array view into the pool, filled with zeros
167
- * @throws RangeError if pool is exhausted
168
- */
169
- allocZeroed(size) {
170
- const mem = this.alloc(size);
171
- mem.fill(0);
172
- return mem;
173
- }
174
- /**
175
- * Allocates memory for a specific typed array.
176
- *
177
- * @param length - Number of elements
178
- * @param bytesPerElement - Size of each element in bytes
179
- * @returns Uint8Array view (use constructor of target type on underlying buffer)
180
- * @throws RangeError if pool is exhausted
181
- *
182
- * @example
183
- * ```typescript
184
- * const pool = new MemoryPool(1024);
185
- * const bytes = pool.allocTyped(10, 4); // 40 bytes for 10 Uint32
186
- * const u32View = new Uint32Array(bytes.buffer, bytes.byteOffset, 10);
187
- * ```
188
- */
189
- allocTyped(length, bytesPerElement) {
190
- return this.alloc(length * bytesPerElement);
191
- }
192
- /**
193
- * Resets the pool, making all memory available again.
194
- *
195
- * WARNING: All previously allocated views become invalid.
196
- * Only call when you're sure no references are held.
197
- *
198
- * Thread-safe using Atomics.store.
199
- *
200
- * @example
201
- * ```typescript
202
- * const pool = new MemoryPool(1024);
203
- * const buf = pool.alloc(100);
204
- * // ... use buf ...
205
- * pool.reset(); // buf is now invalid!
206
- * ```
207
- */
208
- reset() {
209
- Atomics.store(this.#control, 0, 0);
210
- }
211
- /**
212
- * Checks if the pool can accommodate an allocation.
213
- *
214
- * @param size - Number of bytes needed
215
- * @returns True if allocation would succeed
216
- */
217
- canAlloc(size) {
218
- return this.available >= size;
219
- }
220
- }
221
- /**
222
- * Non-shared memory pool using regular ArrayBuffer.
223
- *
224
- * Use when SharedArrayBuffer is not available (e.g., browsers without
225
- * proper COOP/COEP headers) or when thread-safety is not needed.
226
- *
227
- * @example
228
- * ```typescript
229
- * import { SimpleMemoryPool } from '@btc-vision/bitcoin';
230
- *
231
- * const pool = new SimpleMemoryPool(1024 * 1024);
232
- * const buf = pool.alloc(100);
233
- * ```
234
- */
235
- export class SimpleMemoryPool {
236
- #data;
237
- #offset = 0;
238
- /**
239
- * Creates a new SimpleMemoryPool.
240
- *
241
- * @param size - Size of the pool in bytes
242
- */
243
- constructor(size) {
244
- this.#data = new Uint8Array(size);
245
- }
246
- /**
247
- * Total capacity of the pool in bytes.
248
- */
249
- get capacity() {
250
- return this.#data.length;
251
- }
252
- /**
253
- * Current allocation offset (bytes used).
254
- */
255
- get used() {
256
- return this.#offset;
257
- }
258
- /**
259
- * Remaining available bytes.
260
- */
261
- get available() {
262
- return this.#data.length - this.#offset;
263
- }
264
- /**
265
- * Allocates memory from the pool.
266
- *
267
- * @param size - Number of bytes to allocate
268
- * @returns Uint8Array view into the pool
269
- * @throws RangeError if pool is exhausted
270
- */
271
- alloc(size) {
272
- if (size <= 0) {
273
- throw new RangeError('Allocation size must be positive');
274
- }
275
- if (this.#offset + size > this.#data.length) {
276
- throw new RangeError(`SimpleMemoryPool exhausted: requested ${size} bytes, only ${this.available} available`);
277
- }
278
- const start = this.#offset;
279
- this.#offset += size;
280
- return this.#data.subarray(start, this.#offset);
281
- }
282
- /**
283
- * Allocates memory and fills it with zeros.
284
- *
285
- * @param size - Number of bytes to allocate
286
- * @returns Uint8Array view into the pool, filled with zeros
287
- */
288
- allocZeroed(size) {
289
- const mem = this.alloc(size);
290
- mem.fill(0);
291
- return mem;
292
- }
293
- /**
294
- * Resets the pool, making all memory available again.
295
- */
296
- reset() {
297
- this.#offset = 0;
298
- }
299
- /**
300
- * Checks if the pool can accommodate an allocation.
301
- *
302
- * @param size - Number of bytes needed
303
- * @returns True if allocation would succeed
304
- */
305
- canAlloc(size) {
306
- return this.available >= size;
307
- }
308
- }
309
- //# sourceMappingURL=MemoryPool.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"MemoryPool.js","sourceRoot":"","sources":["../../src/io/MemoryPool.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,UAAU;IACnB;;OAEG;IACM,OAAO,CAAoB;IAEpC;;OAEG;IACM,KAAK,CAAa;IAE3B;;;OAGG;IACM,QAAQ,CAAa;IAE9B;;OAEG;IACM,QAAQ,CAAS;IAE1B;;;;;;;;;;;;;;;OAeG;IACH,YAAmB,OAAe,CAAC,GAAG,IAAI,GAAG,IAAI;QAC7C,kDAAkD;QAClD,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC;QAC3B,IAAI,CAAC,OAAO,GAAG,IAAI,iBAAiB,CAAC,SAAS,CAAC,CAAC;QAChD,IAAI,CAAC,KAAK,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;QACnD,IAAI,CAAC,QAAQ,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QACtD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACzB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;OAoBG;IACI,MAAM,CAAC,gBAAgB,CAAC,MAAyB;QACpD,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,SAAS,CAAe,CAAC;QAC/D,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,GAAG,CAAC,CAAC;QAEnC,wEAAwE;QACxE,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;QAC1D,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,IAA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;;;OAMG;IACI,KAAK,CAAC,IAAY;QACrB,IAAI,IAAI,IAAI,CAAC,EAAE,CAAC;YACZ,MAAM,IAAI,UAAU,CAAC,kCAAkC,CAAC,CAAC;QAC7D,CAAC;QAED,IAAI,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;YAC1C,MAAM,IAAI,UAAU,CAChB,yCAAyC,IAAI,gBAAgB,IAAI,CAAC,SAAS,YAAY,CAC1F,CAAC;QACN,CAAC;QAED,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC;QAC3B,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAED;;;;;OAKG;IACI,WAAW,CAAC,IAAY;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC7B,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACZ,OAAO,GAAG,CAAC;IACf,CAAC;IAED;;OAEG;IACI,KAAK;QACR,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;IACrB,CAAC;IAED;;;;;OAKG;IACI,QAAQ,CAAC,IAAY;QACxB,OAAO,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC;IAClC,CAAC;CACJ"}
package/src/ecpair.d.ts DELETED
@@ -1,99 +0,0 @@
1
- /**
2
- * Ambient module declaration for ecpair.
3
- *
4
- * The upstream ecpair@3 .d.ts files depend on valibot@^0.37 type-level APIs.
5
- * When a different valibot major is hoisted (e.g. v1.x required by bip32),
6
- * @typescript-eslint resolves valibot from the project root instead of
7
- * ecpair's nested node_modules, turning every ecpair type into an error type.
8
- *
9
- * This local declaration re-exports the subset of ecpair's public API that
10
- * this project actually uses, without any valibot dependency.
11
- */
12
- declare module 'ecpair' {
13
- interface Network {
14
- messagePrefix: string | Uint8Array;
15
- bech32: string;
16
- bip32: {
17
- public: number;
18
- private: number;
19
- };
20
- pubKeyHash: number;
21
- scriptHash: number;
22
- wif: number;
23
- }
24
-
25
- interface Signer {
26
- publicKey: Uint8Array;
27
- network?: unknown;
28
- sign(hash: Uint8Array, lowR?: boolean): Uint8Array;
29
- }
30
-
31
- interface SignerAsync {
32
- publicKey: Uint8Array;
33
- network?: unknown;
34
- sign(hash: Uint8Array, lowR?: boolean): Promise<Uint8Array>;
35
- }
36
-
37
- interface ECPairInterface extends Signer {
38
- compressed: boolean;
39
- network: Network;
40
- lowR: boolean;
41
- privateKey?: Uint8Array;
42
- toWIF(): string;
43
- tweak(t: Uint8Array): ECPairInterface;
44
- verify(hash: Uint8Array, signature: Uint8Array): boolean;
45
- verifySchnorr(hash: Uint8Array, signature: Uint8Array): boolean;
46
- signSchnorr(hash: Uint8Array): Uint8Array;
47
- }
48
-
49
- interface ECPairOptions {
50
- compressed?: boolean;
51
- network?: Network;
52
- rng?: (arg?: number) => Uint8Array;
53
- }
54
-
55
- interface ECPairAPI {
56
- isPoint(maybePoint: unknown): boolean;
57
- fromPrivateKey(buffer: Uint8Array, options?: ECPairOptions): ECPairInterface;
58
- fromPublicKey(buffer: Uint8Array, options?: ECPairOptions): ECPairInterface;
59
- fromWIF(wifString: string, network?: Network | Network[]): ECPairInterface;
60
- makeRandom(options?: ECPairOptions): ECPairInterface;
61
- }
62
-
63
- interface TinySecp256k1Interface {
64
- isPoint(p: Uint8Array): boolean;
65
- pointCompress(p: Uint8Array, compressed?: boolean): Uint8Array;
66
- isPrivate(d: Uint8Array): boolean;
67
- pointFromScalar(d: Uint8Array, compressed?: boolean): Uint8Array | null;
68
- xOnlyPointAddTweak(
69
- p: Uint8Array,
70
- tweak: Uint8Array,
71
- ): { parity: 1 | 0; xOnlyPubkey: Uint8Array } | null;
72
- privateAdd(d: Uint8Array, tweak: Uint8Array): Uint8Array | null;
73
- privateNegate(d: Uint8Array): Uint8Array;
74
- sign(h: Uint8Array, d: Uint8Array, e?: Uint8Array): Uint8Array;
75
- signSchnorr?(h: Uint8Array, d: Uint8Array, e?: Uint8Array): Uint8Array;
76
- verify(h: Uint8Array, Q: Uint8Array, signature: Uint8Array, strict?: boolean): boolean;
77
- verifySchnorr?(h: Uint8Array, Q: Uint8Array, signature: Uint8Array): boolean;
78
- }
79
-
80
- const networks: {
81
- bitcoin: Network;
82
- testnet: Network;
83
- };
84
-
85
- function ECPairFactory(ecc: TinySecp256k1Interface): ECPairAPI;
86
- export default ECPairFactory;
87
-
88
- export {
89
- ECPairFactory,
90
- Signer,
91
- SignerAsync,
92
- ECPairAPI,
93
- ECPairInterface,
94
- ECPairOptions,
95
- TinySecp256k1Interface,
96
- Network,
97
- networks,
98
- };
99
- }