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

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 (456) hide show
  1. package/HOW_TO_WRITE_GOOD_CODE.md +2436 -0
  2. package/benchmark/psbt-2000-inputs.bench.ts +178 -0
  3. package/benchmark/signing.bench.ts +147 -0
  4. package/browser/address.d.ts +57 -10
  5. package/browser/address.d.ts.map +1 -0
  6. package/browser/bech32utils.d.ts +9 -1
  7. package/browser/bech32utils.d.ts.map +1 -0
  8. package/browser/bip66.d.ts +11 -6
  9. package/browser/bip66.d.ts.map +1 -0
  10. package/browser/block.d.ts +117 -11
  11. package/browser/block.d.ts.map +1 -0
  12. package/browser/branded.d.ts +20 -0
  13. package/browser/branded.d.ts.map +1 -0
  14. package/browser/crypto/crypto.d.ts +1 -0
  15. package/browser/crypto/crypto.d.ts.map +1 -0
  16. package/browser/crypto.d.ts +46 -7
  17. package/browser/crypto.d.ts.map +1 -0
  18. package/browser/ecc/context.d.ts +129 -0
  19. package/browser/ecc/context.d.ts.map +1 -0
  20. package/browser/ecc/index.d.ts +11 -0
  21. package/browser/ecc/index.d.ts.map +1 -0
  22. package/browser/ecc/types.d.ts +128 -0
  23. package/browser/ecc/types.d.ts.map +1 -0
  24. package/browser/ecpair.d.ts +99 -0
  25. package/browser/errors.d.ts +124 -0
  26. package/browser/errors.d.ts.map +1 -0
  27. package/browser/index.d.ts +32 -5
  28. package/browser/index.d.ts.map +1 -0
  29. package/browser/index.js +12477 -101
  30. package/browser/io/BinaryReader.d.ts +276 -0
  31. package/browser/io/BinaryReader.d.ts.map +1 -0
  32. package/browser/io/BinaryWriter.d.ts +391 -0
  33. package/browser/io/BinaryWriter.d.ts.map +1 -0
  34. package/browser/io/MemoryPool.d.ts +220 -0
  35. package/browser/io/MemoryPool.d.ts.map +1 -0
  36. package/browser/io/base64.d.ts +13 -0
  37. package/browser/io/base64.d.ts.map +1 -0
  38. package/browser/io/hex.d.ts +67 -0
  39. package/browser/io/hex.d.ts.map +1 -0
  40. package/browser/io/index.d.ts +17 -0
  41. package/browser/io/index.d.ts.map +1 -0
  42. package/browser/io/utils.d.ts +199 -0
  43. package/browser/io/utils.d.ts.map +1 -0
  44. package/browser/merkle.d.ts +10 -1
  45. package/browser/merkle.d.ts.map +1 -0
  46. package/browser/networks.d.ts +70 -9
  47. package/browser/networks.d.ts.map +1 -0
  48. package/browser/opcodes.d.ts +1 -0
  49. package/browser/opcodes.d.ts.map +1 -0
  50. package/browser/payments/bip341.d.ts +35 -9
  51. package/browser/payments/bip341.d.ts.map +1 -0
  52. package/browser/payments/embed.d.ts +112 -1
  53. package/browser/payments/embed.d.ts.map +1 -0
  54. package/browser/payments/index.d.ts +17 -10
  55. package/browser/payments/index.d.ts.map +1 -0
  56. package/browser/payments/p2ms.d.ts +150 -0
  57. package/browser/payments/p2ms.d.ts.map +1 -0
  58. package/browser/payments/p2op.d.ts +150 -24
  59. package/browser/payments/p2op.d.ts.map +1 -0
  60. package/browser/payments/p2pk.d.ts +154 -1
  61. package/browser/payments/p2pk.d.ts.map +1 -0
  62. package/browser/payments/p2pkh.d.ts +176 -1
  63. package/browser/payments/p2pkh.d.ts.map +1 -0
  64. package/browser/payments/p2sh.d.ts +150 -1
  65. package/browser/payments/p2sh.d.ts.map +1 -0
  66. package/browser/payments/p2tr.d.ts +185 -1
  67. package/browser/payments/p2tr.d.ts.map +1 -0
  68. package/browser/payments/p2wpkh.d.ts +161 -1
  69. package/browser/payments/p2wpkh.d.ts.map +1 -0
  70. package/browser/payments/p2wsh.d.ts +146 -1
  71. package/browser/payments/p2wsh.d.ts.map +1 -0
  72. package/browser/payments/types.d.ts +94 -64
  73. package/browser/payments/types.d.ts.map +1 -0
  74. package/browser/psbt/bip371.d.ts +34 -8
  75. package/browser/psbt/bip371.d.ts.map +1 -0
  76. package/browser/psbt/psbtutils.d.ts +56 -16
  77. package/browser/psbt/psbtutils.d.ts.map +1 -0
  78. package/browser/psbt/types.d.ts +245 -0
  79. package/browser/psbt/types.d.ts.map +1 -0
  80. package/browser/psbt/utils.d.ts +64 -0
  81. package/browser/psbt/utils.d.ts.map +1 -0
  82. package/browser/psbt/validation.d.ts +84 -0
  83. package/browser/psbt/validation.d.ts.map +1 -0
  84. package/browser/psbt.d.ts +82 -118
  85. package/browser/psbt.d.ts.map +1 -0
  86. package/browser/pubkey.d.ts +27 -6
  87. package/browser/pubkey.d.ts.map +1 -0
  88. package/browser/push_data.d.ts +24 -2
  89. package/browser/push_data.d.ts.map +1 -0
  90. package/browser/script.d.ts +33 -8
  91. package/browser/script.d.ts.map +1 -0
  92. package/browser/script_number.d.ts +17 -0
  93. package/browser/script_number.d.ts.map +1 -0
  94. package/browser/script_signature.d.ts +23 -5
  95. package/browser/script_signature.d.ts.map +1 -0
  96. package/browser/transaction.d.ts +160 -18
  97. package/browser/transaction.d.ts.map +1 -0
  98. package/browser/types.d.ts +36 -38
  99. package/browser/types.d.ts.map +1 -0
  100. package/browser/workers/WorkerSigningPool.d.ts +143 -0
  101. package/browser/workers/WorkerSigningPool.d.ts.map +1 -0
  102. package/browser/workers/WorkerSigningPool.node.d.ts +116 -0
  103. package/browser/workers/WorkerSigningPool.node.d.ts.map +1 -0
  104. package/browser/workers/ecc-bundle.d.ts +25 -0
  105. package/browser/workers/ecc-bundle.d.ts.map +1 -0
  106. package/browser/workers/index.d.ts +91 -0
  107. package/browser/workers/index.d.ts.map +1 -0
  108. package/browser/workers/psbt-parallel.d.ts +88 -0
  109. package/browser/workers/psbt-parallel.d.ts.map +1 -0
  110. package/browser/workers/signing-worker.d.ts +37 -0
  111. package/browser/workers/signing-worker.d.ts.map +1 -0
  112. package/browser/workers/types.d.ts +365 -0
  113. package/browser/workers/types.d.ts.map +1 -0
  114. package/build/address.d.ts +58 -11
  115. package/build/address.d.ts.map +1 -0
  116. package/build/address.js +82 -25
  117. package/build/address.js.map +1 -0
  118. package/build/bech32utils.d.ts +9 -1
  119. package/build/bech32utils.d.ts.map +1 -0
  120. package/build/bech32utils.js +10 -2
  121. package/build/bech32utils.js.map +1 -0
  122. package/build/bip66.d.ts +11 -6
  123. package/build/bip66.d.ts.map +1 -0
  124. package/build/bip66.js +32 -3
  125. package/build/bip66.js.map +1 -0
  126. package/build/block.d.ts +117 -11
  127. package/build/block.d.ts.map +1 -0
  128. package/build/block.js +202 -72
  129. package/build/block.js.map +1 -0
  130. package/build/branded.d.ts +20 -0
  131. package/build/branded.d.ts.map +1 -0
  132. package/build/branded.js +7 -0
  133. package/build/branded.js.map +1 -0
  134. package/build/crypto/crypto.d.ts +1 -0
  135. package/build/crypto/crypto.d.ts.map +1 -0
  136. package/build/crypto/crypto.js +1 -0
  137. package/build/crypto/crypto.js.map +1 -0
  138. package/build/crypto.d.ts +46 -7
  139. package/build/crypto.d.ts.map +1 -0
  140. package/build/crypto.js +65 -20
  141. package/build/crypto.js.map +1 -0
  142. package/build/ecc/context.d.ts +135 -0
  143. package/build/ecc/context.d.ts.map +1 -0
  144. package/build/ecc/context.js +232 -0
  145. package/build/ecc/context.js.map +1 -0
  146. package/build/ecc/index.d.ts +11 -0
  147. package/build/ecc/index.d.ts.map +1 -0
  148. package/build/ecc/index.js +11 -0
  149. package/build/ecc/index.js.map +1 -0
  150. package/build/ecc/types.d.ts +134 -0
  151. package/build/ecc/types.d.ts.map +1 -0
  152. package/build/ecc/types.js +8 -0
  153. package/build/ecc/types.js.map +1 -0
  154. package/build/errors.d.ts +124 -0
  155. package/build/errors.d.ts.map +1 -0
  156. package/build/errors.js +155 -0
  157. package/build/errors.js.map +1 -0
  158. package/build/index.d.ts +32 -5
  159. package/build/index.d.ts.map +1 -0
  160. package/build/index.js +26 -3
  161. package/build/index.js.map +1 -0
  162. package/build/io/BinaryReader.d.ts +276 -0
  163. package/build/io/BinaryReader.d.ts.map +1 -0
  164. package/build/io/BinaryReader.js +425 -0
  165. package/build/io/BinaryReader.js.map +1 -0
  166. package/build/io/BinaryWriter.d.ts +391 -0
  167. package/build/io/BinaryWriter.d.ts.map +1 -0
  168. package/build/io/BinaryWriter.js +611 -0
  169. package/build/io/BinaryWriter.js.map +1 -0
  170. package/build/io/MemoryPool.d.ts +220 -0
  171. package/build/io/MemoryPool.d.ts.map +1 -0
  172. package/build/io/MemoryPool.js +309 -0
  173. package/build/io/MemoryPool.js.map +1 -0
  174. package/build/io/base64.d.ts +13 -0
  175. package/build/io/base64.d.ts.map +1 -0
  176. package/build/io/base64.js +20 -0
  177. package/build/io/base64.js.map +1 -0
  178. package/build/io/hex.d.ts +67 -0
  179. package/build/io/hex.d.ts.map +1 -0
  180. package/build/io/hex.js +138 -0
  181. package/build/io/hex.js.map +1 -0
  182. package/build/io/index.d.ts +17 -0
  183. package/build/io/index.d.ts.map +1 -0
  184. package/build/io/index.js +23 -0
  185. package/build/io/index.js.map +1 -0
  186. package/build/io/utils.d.ts +199 -0
  187. package/build/io/utils.d.ts.map +1 -0
  188. package/build/io/utils.js +271 -0
  189. package/build/io/utils.js.map +1 -0
  190. package/build/merkle.d.ts +10 -1
  191. package/build/merkle.d.ts.map +1 -0
  192. package/build/merkle.js +12 -1
  193. package/build/merkle.js.map +1 -0
  194. package/build/networks.d.ts +70 -9
  195. package/build/networks.d.ts.map +1 -0
  196. package/build/networks.js +90 -4
  197. package/build/networks.js.map +1 -0
  198. package/build/opcodes.d.ts +1 -0
  199. package/build/opcodes.d.ts.map +1 -0
  200. package/build/opcodes.js +1 -0
  201. package/build/opcodes.js.map +1 -0
  202. package/build/payments/bip341.d.ts +35 -9
  203. package/build/payments/bip341.d.ts.map +1 -0
  204. package/build/payments/bip341.js +34 -15
  205. package/build/payments/bip341.js.map +1 -0
  206. package/build/payments/embed.d.ts +120 -1
  207. package/build/payments/embed.d.ts.map +1 -0
  208. package/build/payments/embed.js +215 -34
  209. package/build/payments/embed.js.map +1 -0
  210. package/build/payments/index.d.ts +17 -10
  211. package/build/payments/index.d.ts.map +1 -0
  212. package/build/payments/index.js +20 -10
  213. package/build/payments/index.js.map +1 -0
  214. package/build/payments/p2ms.d.ts +159 -1
  215. package/build/payments/p2ms.d.ts.map +1 -0
  216. package/build/payments/p2ms.js +427 -108
  217. package/build/payments/p2ms.js.map +1 -0
  218. package/build/payments/p2op.d.ts +158 -24
  219. package/build/payments/p2op.d.ts.map +1 -0
  220. package/build/payments/p2op.js +379 -93
  221. package/build/payments/p2op.js.map +1 -0
  222. package/build/payments/p2pk.d.ts +162 -1
  223. package/build/payments/p2pk.d.ts.map +1 -0
  224. package/build/payments/p2pk.js +327 -58
  225. package/build/payments/p2pk.js.map +1 -0
  226. package/build/payments/p2pkh.d.ts +185 -1
  227. package/build/payments/p2pkh.d.ts.map +1 -0
  228. package/build/payments/p2pkh.js +467 -114
  229. package/build/payments/p2pkh.js.map +1 -0
  230. package/build/payments/p2sh.d.ts +159 -1
  231. package/build/payments/p2sh.d.ts.map +1 -0
  232. package/build/payments/p2sh.js +500 -150
  233. package/build/payments/p2sh.js.map +1 -0
  234. package/build/payments/p2tr.d.ts +193 -1
  235. package/build/payments/p2tr.d.ts.map +1 -0
  236. package/build/payments/p2tr.js +592 -174
  237. package/build/payments/p2tr.js.map +1 -0
  238. package/build/payments/p2wpkh.d.ts +170 -1
  239. package/build/payments/p2wpkh.d.ts.map +1 -0
  240. package/build/payments/p2wpkh.js +428 -103
  241. package/build/payments/p2wpkh.js.map +1 -0
  242. package/build/payments/p2wsh.d.ts +155 -1
  243. package/build/payments/p2wsh.d.ts.map +1 -0
  244. package/build/payments/p2wsh.js +465 -143
  245. package/build/payments/p2wsh.js.map +1 -0
  246. package/build/payments/types.d.ts +98 -64
  247. package/build/payments/types.d.ts.map +1 -0
  248. package/build/payments/types.js +17 -13
  249. package/build/payments/types.js.map +1 -0
  250. package/build/psbt/bip371.d.ts +35 -9
  251. package/build/psbt/bip371.d.ts.map +1 -0
  252. package/build/psbt/bip371.js +117 -28
  253. package/build/psbt/bip371.js.map +1 -0
  254. package/build/psbt/psbtutils.d.ts +56 -16
  255. package/build/psbt/psbtutils.d.ts.map +1 -0
  256. package/build/psbt/psbtutils.js +71 -16
  257. package/build/psbt/psbtutils.js.map +1 -0
  258. package/build/psbt/types.d.ts +249 -0
  259. package/build/psbt/types.d.ts.map +1 -0
  260. package/build/psbt/types.js +6 -0
  261. package/build/psbt/types.js.map +1 -0
  262. package/build/psbt/utils.d.ts +68 -0
  263. package/build/psbt/utils.d.ts.map +1 -0
  264. package/build/psbt/utils.js +171 -0
  265. package/build/psbt/utils.js.map +1 -0
  266. package/build/psbt/validation.d.ts +88 -0
  267. package/build/psbt/validation.d.ts.map +1 -0
  268. package/build/psbt/validation.js +149 -0
  269. package/build/psbt/validation.js.map +1 -0
  270. package/build/psbt.d.ts +84 -120
  271. package/build/psbt.d.ts.map +1 -0
  272. package/build/psbt.js +406 -413
  273. package/build/psbt.js.map +1 -0
  274. package/build/pubkey.d.ts +27 -6
  275. package/build/pubkey.d.ts.map +1 -0
  276. package/build/pubkey.js +36 -12
  277. package/build/pubkey.js.map +1 -0
  278. package/build/push_data.d.ts +24 -2
  279. package/build/push_data.d.ts.map +1 -0
  280. package/build/push_data.js +44 -12
  281. package/build/push_data.js.map +1 -0
  282. package/build/script.d.ts +33 -8
  283. package/build/script.d.ts.map +1 -0
  284. package/build/script.js +101 -37
  285. package/build/script.js.map +1 -0
  286. package/build/script_number.d.ts +17 -0
  287. package/build/script_number.d.ts.map +1 -0
  288. package/build/script_number.js +19 -0
  289. package/build/script_number.js.map +1 -0
  290. package/build/script_signature.d.ts +23 -5
  291. package/build/script_signature.d.ts.map +1 -0
  292. package/build/script_signature.js +48 -15
  293. package/build/script_signature.js.map +1 -0
  294. package/build/transaction.d.ts +160 -18
  295. package/build/transaction.d.ts.map +1 -0
  296. package/build/transaction.js +443 -176
  297. package/build/transaction.js.map +1 -0
  298. package/build/tsconfig.build.tsbuildinfo +1 -0
  299. package/build/types.d.ts +36 -38
  300. package/build/types.d.ts.map +1 -0
  301. package/build/types.js +169 -57
  302. package/build/types.js.map +1 -0
  303. package/build/workers/WorkerSigningPool.d.ts +174 -0
  304. package/build/workers/WorkerSigningPool.d.ts.map +1 -0
  305. package/build/workers/WorkerSigningPool.js +553 -0
  306. package/build/workers/WorkerSigningPool.js.map +1 -0
  307. package/build/workers/WorkerSigningPool.node.d.ts +124 -0
  308. package/build/workers/WorkerSigningPool.node.d.ts.map +1 -0
  309. package/build/workers/WorkerSigningPool.node.js +753 -0
  310. package/build/workers/WorkerSigningPool.node.js.map +1 -0
  311. package/build/workers/ecc-bundle.d.ts +25 -0
  312. package/build/workers/ecc-bundle.d.ts.map +1 -0
  313. package/build/workers/ecc-bundle.js +25 -0
  314. package/build/workers/ecc-bundle.js.map +1 -0
  315. package/build/workers/index.d.ts +91 -0
  316. package/build/workers/index.d.ts.map +1 -0
  317. package/build/workers/index.js +114 -0
  318. package/build/workers/index.js.map +1 -0
  319. package/build/workers/psbt-parallel.d.ts +117 -0
  320. package/build/workers/psbt-parallel.d.ts.map +1 -0
  321. package/build/workers/psbt-parallel.js +233 -0
  322. package/build/workers/psbt-parallel.js.map +1 -0
  323. package/build/workers/signing-worker.d.ts +37 -0
  324. package/build/workers/signing-worker.d.ts.map +1 -0
  325. package/build/workers/signing-worker.js +350 -0
  326. package/build/workers/signing-worker.js.map +1 -0
  327. package/build/workers/types.d.ts +365 -0
  328. package/build/workers/types.d.ts.map +1 -0
  329. package/build/workers/types.js +60 -0
  330. package/build/workers/types.js.map +1 -0
  331. package/package.json +68 -9
  332. package/scripts/bundle-ecc.ts +111 -0
  333. package/src/address.ts +91 -45
  334. package/src/bech32utils.ts +3 -3
  335. package/src/bip66.ts +34 -24
  336. package/src/block.ts +205 -86
  337. package/src/branded.ts +18 -0
  338. package/src/crypto.ts +64 -26
  339. package/src/ecc/context.ts +280 -0
  340. package/src/ecc/index.ts +14 -0
  341. package/src/ecc/types.ts +147 -0
  342. package/src/ecpair.d.ts +99 -0
  343. package/src/errors.ts +163 -0
  344. package/src/index.ts +112 -9
  345. package/src/io/BinaryReader.ts +461 -0
  346. package/src/io/BinaryWriter.ts +696 -0
  347. package/src/io/MemoryPool.ts +343 -0
  348. package/src/io/base64.ts +20 -0
  349. package/src/io/hex.ts +155 -0
  350. package/src/io/index.ts +41 -0
  351. package/src/io/utils.ts +283 -0
  352. package/src/merkle.ts +14 -9
  353. package/src/networks.ts +9 -9
  354. package/src/payments/bip341.ts +32 -33
  355. package/src/payments/embed.ts +244 -41
  356. package/src/payments/index.ts +12 -10
  357. package/src/payments/p2ms.ts +497 -118
  358. package/src/payments/p2op.ts +432 -134
  359. package/src/payments/p2pk.ts +370 -72
  360. package/src/payments/p2pkh.ts +524 -130
  361. package/src/payments/p2sh.ts +572 -169
  362. package/src/payments/p2tr.ts +686 -194
  363. package/src/payments/p2wpkh.ts +482 -105
  364. package/src/payments/p2wsh.ts +524 -162
  365. package/src/payments/types.ts +80 -66
  366. package/src/psbt/bip371.ts +72 -51
  367. package/src/psbt/psbtutils.ts +39 -40
  368. package/src/psbt/types.ts +324 -0
  369. package/src/psbt/utils.ts +188 -0
  370. package/src/psbt/validation.ts +185 -0
  371. package/src/psbt.ts +608 -827
  372. package/src/pubkey.ts +22 -23
  373. package/src/push_data.ts +18 -16
  374. package/src/script.ts +81 -66
  375. package/src/script_number.ts +6 -6
  376. package/src/script_signature.ts +33 -36
  377. package/src/transaction.ts +462 -239
  378. package/src/types.ts +229 -100
  379. package/src/workers/WorkerSigningPool.node.ts +887 -0
  380. package/src/workers/WorkerSigningPool.ts +666 -0
  381. package/src/workers/ecc-bundle.ts +26 -0
  382. package/src/workers/index.ts +165 -0
  383. package/src/workers/psbt-parallel.ts +327 -0
  384. package/src/workers/signing-worker.ts +353 -0
  385. package/src/workers/types.ts +417 -0
  386. package/test/address.spec.ts +9 -6
  387. package/test/bitcoin.core.spec.ts +16 -17
  388. package/test/block.spec.ts +8 -7
  389. package/test/bufferutils.spec.ts +228 -214
  390. package/test/crypto.spec.ts +19 -11
  391. package/test/fixtures/p2pk.json +0 -8
  392. package/test/fixtures/p2pkh.json +1 -1
  393. package/test/fixtures/p2sh.json +1 -1
  394. package/test/fixtures/script.json +1 -1
  395. package/test/fixtures/transaction.json +2 -2
  396. package/test/integration/_regtest.ts +25 -0
  397. package/test/integration/addresses.spec.ts +4 -3
  398. package/test/integration/bip32.spec.ts +2 -1
  399. package/test/integration/blocks.spec.ts +1 -1
  400. package/test/integration/cltv.spec.ts +18 -16
  401. package/test/integration/csv.spec.ts +37 -64
  402. package/test/integration/payments.spec.ts +5 -3
  403. package/test/integration/taproot.spec.ts +76 -83
  404. package/test/integration/transactions.spec.ts +38 -35
  405. package/test/payments.spec.ts +35 -13
  406. package/test/payments.utils.ts +17 -16
  407. package/test/psbt.spec.ts +111 -100
  408. package/test/script.spec.ts +11 -10
  409. package/test/script_signature.spec.ts +9 -11
  410. package/test/taproot-cache.spec.ts +694 -0
  411. package/test/transaction.spec.ts +32 -40
  412. package/test/types.spec.ts +74 -29
  413. package/test/workers-pool.spec.ts +963 -0
  414. package/test/workers-signing.spec.ts +635 -0
  415. package/test/workers.spec.ts +1390 -0
  416. package/tsconfig.base.json +34 -18
  417. package/tsconfig.browser.json +15 -0
  418. package/tsconfig.build.json +5 -0
  419. package/tsconfig.json +5 -14
  420. package/typedoc.json +29 -0
  421. package/vite.config.browser.ts +3 -42
  422. package/vitest.config.integration.ts +2 -0
  423. package/browser/bufferutils.d.ts +0 -34
  424. package/browser/chunks/crypto-BhCpKpek.js +0 -2033
  425. package/browser/chunks/payments-B1wlSccx.js +0 -1089
  426. package/browser/chunks/psbt-BCNk7JUx.js +0 -4055
  427. package/browser/chunks/script-DyPItFEl.js +0 -318
  428. package/browser/chunks/transaction-C_UbhMGn.js +0 -432
  429. package/browser/chunks/utils-DNZi-T5W.js +0 -761
  430. package/browser/ecc_lib.d.ts +0 -3
  431. package/browser/hooks/AdvancedSignatureManager.d.ts +0 -16
  432. package/browser/hooks/HookedSigner.d.ts +0 -4
  433. package/browser/hooks/SignatureManager.d.ts +0 -13
  434. package/browser/payments/lazy.d.ts +0 -2
  435. package/browser/typeforce.d.ts +0 -38
  436. package/build/bufferutils.d.ts +0 -34
  437. package/build/bufferutils.js +0 -141
  438. package/build/ecc_lib.d.ts +0 -3
  439. package/build/ecc_lib.js +0 -61
  440. package/build/hooks/AdvancedSignatureManager.d.ts +0 -16
  441. package/build/hooks/AdvancedSignatureManager.js +0 -52
  442. package/build/hooks/HookedSigner.d.ts +0 -4
  443. package/build/hooks/HookedSigner.js +0 -64
  444. package/build/hooks/SignatureManager.d.ts +0 -13
  445. package/build/hooks/SignatureManager.js +0 -45
  446. package/build/payments/lazy.d.ts +0 -2
  447. package/build/payments/lazy.js +0 -28
  448. package/build/tsconfig.tsbuildinfo +0 -1
  449. package/src/bufferutils.ts +0 -188
  450. package/src/ecc_lib.ts +0 -94
  451. package/src/hooks/AdvancedSignatureManager.ts +0 -104
  452. package/src/hooks/HookedSigner.ts +0 -108
  453. package/src/hooks/SignatureManager.ts +0 -84
  454. package/src/payments/lazy.ts +0 -28
  455. package/src/typeforce.d.ts +0 -38
  456. package/tsconfig.webpack.json +0 -18
package/src/pubkey.ts CHANGED
@@ -3,32 +3,33 @@
3
3
  * @packageDocumentation
4
4
  */
5
5
  import { Point } from '@noble/secp256k1';
6
+ import { alloc, concat, equals, fromHex, toHex } from './io/index.js';
7
+ import type { PublicKey, XOnlyPublicKey } from './types.js';
6
8
 
7
9
  /**
8
10
  * Converts a public key to x-only format (32 bytes).
9
11
  * @param pubKey - The public key buffer (33 or 65 bytes)
10
12
  * @returns The x-only public key (32 bytes)
11
13
  */
12
- export const toXOnly = (pubKey: Buffer | Uint8Array): Buffer => {
13
- const buffer = pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);
14
- return Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
14
+ export const toXOnly = (pubKey: PublicKey | XOnlyPublicKey): XOnlyPublicKey => {
15
+ return (pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33)) as XOnlyPublicKey;
15
16
  };
16
17
 
17
18
  export interface UncompressedPublicKey {
18
- hybrid: Buffer;
19
- uncompressed: Buffer;
19
+ hybrid: Uint8Array;
20
+ uncompressed: Uint8Array;
20
21
  }
21
22
 
22
23
  /**
23
- * Converts bigint to 32-byte Buffer.
24
+ * Converts bigint to 32-byte Uint8Array.
24
25
  */
25
- export function bigIntTo32Bytes(num: bigint): Buffer {
26
+ export function bigIntTo32Bytes(num: bigint): Uint8Array {
26
27
  let hex = num.toString(16);
27
28
  hex = hex.padStart(64, '0');
28
29
  if (hex.length > 64) {
29
30
  hex = hex.slice(-64);
30
31
  }
31
- return Buffer.from(hex, 'hex');
32
+ return fromHex(hex);
32
33
  }
33
34
 
34
35
  /**
@@ -36,11 +37,9 @@ export function bigIntTo32Bytes(num: bigint): Buffer {
36
37
  * to its "hybrid" form (prefix 0x06/0x07), then derives a P2PKH address from it.
37
38
  *
38
39
  * @param realPubKey - 33-byte compressed (0x02/0x03) or 65-byte uncompressed (0x04) pubkey
39
- * @returns Buffer | undefined
40
+ * @returns UncompressedPublicKey | undefined
40
41
  */
41
- export function decompressPublicKey(
42
- realPubKey: Uint8Array | Buffer,
43
- ): UncompressedPublicKey | undefined {
42
+ export function decompressPublicKey(realPubKey: PublicKey): UncompressedPublicKey | undefined {
44
43
  if (realPubKey.length === 32) {
45
44
  return;
46
45
  }
@@ -54,7 +53,7 @@ export function decompressPublicKey(
54
53
 
55
54
  let point: Point;
56
55
  try {
57
- point = Point.fromHex(Buffer.from(realPubKey).toString('hex'));
56
+ point = Point.fromHex(toHex(realPubKey));
58
57
  } catch (err) {
59
58
  throw new Error('Invalid secp256k1 public key bytes. Cannot parse.');
60
59
  }
@@ -65,12 +64,12 @@ export function decompressPublicKey(
65
64
  const isEven = point.y % 2n === 0n;
66
65
  const prefix = isEven ? 0x06 : 0x07;
67
66
 
68
- const hybridPubKey = Buffer.alloc(65);
67
+ const hybridPubKey = alloc(65);
69
68
  hybridPubKey[0] = prefix;
70
- xBuf.copy(hybridPubKey, 1);
71
- yBuf.copy(hybridPubKey, 33);
69
+ hybridPubKey.set(xBuf, 1);
70
+ hybridPubKey.set(yBuf, 33);
72
71
 
73
- const uncompressedPubKey = Buffer.concat([Buffer.from([0x04]), xBuf, yBuf]);
72
+ const uncompressedPubKey = concat([new Uint8Array([0x04]), xBuf, yBuf]);
74
73
 
75
74
  return {
76
75
  hybrid: hybridPubKey,
@@ -79,20 +78,20 @@ export function decompressPublicKey(
79
78
  }
80
79
 
81
80
  /**
82
- * Compare two potential pubkey Buffers, treating hybrid keys (0x06/0x07)
81
+ * Compare two potential pubkey Uint8Arrays, treating hybrid keys (0x06/0x07)
83
82
  * as equivalent to uncompressed (0x04).
84
83
  */
85
- export function pubkeysMatch(a: Buffer, b: Buffer): boolean {
86
- if (a.equals(b)) return true;
84
+ export function pubkeysMatch(a: Uint8Array, b: Uint8Array): boolean {
85
+ if (equals(a, b)) return true;
87
86
 
88
87
  if (a.length === 65 && b.length === 65) {
89
- const aCopy = Buffer.from(a);
90
- const bCopy = Buffer.from(b);
88
+ const aCopy = new Uint8Array(a);
89
+ const bCopy = new Uint8Array(b);
91
90
 
92
91
  if (aCopy[0] === 0x06 || aCopy[0] === 0x07) aCopy[0] = 0x04;
93
92
  if (bCopy[0] === 0x06 || bCopy[0] === 0x07) bCopy[0] = 0x04;
94
93
 
95
- return aCopy.equals(bCopy);
94
+ return equals(aCopy, bCopy);
96
95
  }
97
96
 
98
97
  return false;
package/src/push_data.ts CHANGED
@@ -19,27 +19,28 @@ export function encodingLength(i: number): number {
19
19
  * @param offset - The offset at which to start writing the encoded buffer.
20
20
  * @returns The size of the encoded buffer.
21
21
  */
22
- export function encode(buffer: Buffer, num: number, offset: number): number {
22
+ export function encode(buffer: Uint8Array, num: number, offset: number): number {
23
23
  const size = encodingLength(num);
24
+ const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
24
25
 
25
26
  // ~6 bit
26
27
  if (size === 1) {
27
- buffer.writeUInt8(num, offset);
28
+ buffer[offset] = num;
28
29
 
29
30
  // 8 bit
30
31
  } else if (size === 2) {
31
- buffer.writeUInt8(opcodes.OP_PUSHDATA1, offset);
32
- buffer.writeUInt8(num, offset + 1);
32
+ buffer[offset] = opcodes.OP_PUSHDATA1;
33
+ buffer[offset + 1] = num;
33
34
 
34
35
  // 16 bit
35
36
  } else if (size === 3) {
36
- buffer.writeUInt8(opcodes.OP_PUSHDATA2, offset);
37
- buffer.writeUInt16LE(num, offset + 1);
37
+ buffer[offset] = opcodes.OP_PUSHDATA2;
38
+ view.setUint16(offset + 1, num, true);
38
39
 
39
40
  // 32 bit
40
41
  } else {
41
- buffer.writeUInt8(opcodes.OP_PUSHDATA4, offset);
42
- buffer.writeUInt32LE(num, offset + 1);
42
+ buffer[offset] = opcodes.OP_PUSHDATA4;
43
+ view.setUint32(offset + 1, num, true);
43
44
  }
44
45
 
45
46
  return size;
@@ -52,32 +53,33 @@ export function encode(buffer: Buffer, num: number, offset: number): number {
52
53
  * @returns An object containing the opcode, number, and size, or null if decoding fails.
53
54
  */
54
55
  export function decode(
55
- buffer: Buffer,
56
+ buffer: Uint8Array,
56
57
  offset: number,
57
58
  ): {
58
59
  opcode: number;
59
60
  number: number;
60
61
  size: number;
61
62
  } | null {
62
- const opcode = buffer.readUInt8(offset);
63
+ const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
64
+ const opcode = buffer[offset];
63
65
  let num: number;
64
66
  let size: number;
65
67
 
66
68
  // ~6 bit
67
- if (opcode < opcodes.OP_PUSHDATA1) {
68
- num = opcode;
69
+ if (opcode! < opcodes.OP_PUSHDATA1) {
70
+ num = opcode!;
69
71
  size = 1;
70
72
 
71
73
  // 8 bit
72
74
  } else if (opcode === opcodes.OP_PUSHDATA1) {
73
75
  if (offset + 2 > buffer.length) return null;
74
- num = buffer.readUInt8(offset + 1);
76
+ num = buffer[offset + 1]!;
75
77
  size = 2;
76
78
 
77
79
  // 16 bit
78
80
  } else if (opcode === opcodes.OP_PUSHDATA2) {
79
81
  if (offset + 3 > buffer.length) return null;
80
- num = buffer.readUInt16LE(offset + 1);
82
+ num = view.getUint16(offset + 1, true);
81
83
  size = 3;
82
84
 
83
85
  // 32 bit
@@ -85,12 +87,12 @@ export function decode(
85
87
  if (offset + 5 > buffer.length) return null;
86
88
  if (opcode !== opcodes.OP_PUSHDATA4) throw new Error('Unexpected opcode');
87
89
 
88
- num = buffer.readUInt32LE(offset + 1);
90
+ num = view.getUint32(offset + 1, true);
89
91
  size = 5;
90
92
  }
91
93
 
92
94
  return {
93
- opcode,
95
+ opcode: opcode!,
94
96
  number: num,
95
97
  size,
96
98
  };
package/src/script.ts CHANGED
@@ -3,74 +3,79 @@
3
3
  * @packageDocumentation
4
4
  */
5
5
  import * as bip66 from './bip66.js';
6
- import { Opcodes, opcodes, REVERSE_OPS } from './opcodes.js';
6
+ import { alloc, fromHex, toHex } from './io/index.js';
7
+ import type { Opcodes } from './opcodes.js';
8
+ import { opcodes, REVERSE_OPS } from './opcodes.js';
7
9
  import * as pushdata from './push_data.js';
8
10
  import * as scriptNumber from './script_number.js';
9
11
  import * as scriptSignature from './script_signature.js';
12
+ import { isDefinedHashType } from './script_signature.js';
13
+ import type { Script, Stack } from './types.js';
10
14
  import * as types from './types.js';
11
- import type { Stack } from './types.js';
12
-
13
- const { typeforce } = types;
14
15
 
15
16
  const OP_INT_BASE = opcodes.OP_RESERVED; // OP_1 - 1
16
17
  export { opcodes };
17
18
 
18
19
  function isOPInt(value: number): boolean {
19
20
  return (
20
- types.Number(value) &&
21
+ types.isNumber(value) &&
21
22
  (value === opcodes.OP_0 ||
22
23
  (value >= opcodes.OP_1 && value <= opcodes.OP_16) ||
23
24
  value === opcodes.OP_1NEGATE)
24
25
  );
25
26
  }
26
27
 
27
- function isPushOnlyChunk(value: number | Buffer): boolean {
28
- return types.Buffer(value) || isOPInt(value as number);
28
+ function isPushOnlyChunk(value: number | Uint8Array): boolean {
29
+ if (value instanceof Uint8Array) return true;
30
+ return isOPInt(value);
29
31
  }
30
32
 
31
33
  export function isPushOnly(value: Stack): boolean {
32
- return types.Array(value) && value.every(isPushOnlyChunk);
34
+ return types.isArray(value) && value.every(isPushOnlyChunk);
33
35
  }
34
36
 
35
37
  export function countNonPushOnlyOPs(value: Stack): number {
36
38
  return value.length - value.filter(isPushOnlyChunk).length;
37
39
  }
38
40
 
39
- function asMinimalOP(buffer: Buffer): number | undefined {
41
+ function asMinimalOP(buffer: Uint8Array): number | undefined {
40
42
  if (buffer.length === 0) return opcodes.OP_0;
41
- if (buffer.length !== 1) return;
42
- if (buffer[0] >= 1 && buffer[0] <= 16) return OP_INT_BASE + buffer[0];
43
+ if (buffer.length !== 1) return undefined;
44
+ if (buffer[0]! >= 1 && buffer[0]! <= 16) return OP_INT_BASE + buffer[0]!;
43
45
  if (buffer[0] === 0x81) return opcodes.OP_1NEGATE;
46
+ return undefined;
44
47
  }
45
48
 
46
- function chunksIsBuffer(buf: Buffer | Stack): buf is Buffer {
47
- return Buffer.isBuffer(buf);
49
+ function chunksIsUint8Array(buf: Uint8Array | Stack): buf is Uint8Array {
50
+ return buf instanceof Uint8Array;
48
51
  }
49
52
 
50
- function chunksIsArray(buf: Buffer | Stack): buf is Stack {
51
- return types.Array(buf);
53
+ function chunksIsArray(buf: Uint8Array | Stack): buf is Stack {
54
+ return types.isArray(buf);
52
55
  }
53
56
 
54
- function singleChunkIsBuffer(buf: number | Buffer): buf is Buffer {
55
- return Buffer.isBuffer(buf);
57
+ function singleChunkIsUint8Array(buf: number | Uint8Array): buf is Uint8Array {
58
+ return buf instanceof Uint8Array;
56
59
  }
57
60
 
58
61
  /**
59
- * Compiles an array of chunks into a Buffer.
62
+ * Compiles an array of chunks into a Uint8Array.
60
63
  *
61
64
  * @param chunks - The array of chunks to compile.
62
- * @returns The compiled Buffer.
65
+ * @returns The compiled Uint8Array.
63
66
  * @throws Error if the compilation fails.
64
67
  */
65
- export function compile(chunks: Buffer | Stack): Buffer {
66
- // TODO: remove me
67
- if (chunksIsBuffer(chunks)) return chunks;
68
+ export function compile(chunks: Uint8Array | Stack): Script {
69
+ // Already compiled - return as-is
70
+ if (chunksIsUint8Array(chunks)) return chunks as Script;
68
71
 
69
- typeforce(types.Array, chunks);
72
+ if (!types.isArray(chunks)) {
73
+ throw new TypeError('Expected an array');
74
+ }
70
75
 
71
76
  const bufferSize = chunks.reduce((accum: number, chunk) => {
72
77
  // data chunk
73
- if (singleChunkIsBuffer(chunk)) {
78
+ if (singleChunkIsUint8Array(chunk)) {
74
79
  // adhere to BIP62.3, minimal push policy
75
80
  if (chunk.length === 1 && asMinimalOP(chunk) !== undefined) {
76
81
  return accum + 1;
@@ -83,46 +88,48 @@ export function compile(chunks: Buffer | Stack): Buffer {
83
88
  return accum + 1;
84
89
  }, 0.0);
85
90
 
86
- const buffer = Buffer.allocUnsafe(bufferSize);
91
+ const buffer = new Uint8Array(bufferSize);
87
92
  let offset = 0;
88
93
 
89
94
  chunks.forEach((chunk) => {
90
95
  // data chunk
91
- if (singleChunkIsBuffer(chunk)) {
96
+ if (singleChunkIsUint8Array(chunk)) {
92
97
  // adhere to BIP62.3, minimal push policy
93
98
  const opcode = asMinimalOP(chunk);
94
99
  if (opcode !== undefined) {
95
- buffer.writeUInt8(opcode, offset);
100
+ buffer[offset] = opcode;
96
101
  offset += 1;
97
102
  return;
98
103
  }
99
104
 
100
105
  offset += pushdata.encode(buffer, chunk.length, offset);
101
- chunk.copy(buffer, offset);
106
+ buffer.set(chunk, offset);
102
107
  offset += chunk.length;
103
108
 
104
109
  // opcode
105
110
  } else {
106
- buffer.writeUInt8(chunk, offset);
111
+ buffer[offset] = chunk;
107
112
  offset += 1;
108
113
  }
109
114
  });
110
115
 
111
116
  if (offset !== buffer.length) throw new Error('Could not decode chunks');
112
- return buffer;
117
+ return buffer as Script;
113
118
  }
114
119
 
115
- export function decompile(buffer: Buffer | Array<number | Buffer>): Array<number | Buffer> | null {
116
- // TODO: remove me
117
- if (chunksIsArray(buffer)) return buffer;
120
+ export function decompile(buffer: Uint8Array | Stack): Array<number | Uint8Array> | null {
121
+ // Already decompiled - return as-is
122
+ if (chunksIsArray(buffer)) return buffer as Array<number | Uint8Array>;
118
123
 
119
- typeforce(types.Buffer, buffer);
124
+ if (!(buffer instanceof Uint8Array)) {
125
+ throw new TypeError('Expected a Uint8Array');
126
+ }
120
127
 
121
- const chunks: Array<number | Buffer> = [];
128
+ const chunks: Array<number | Uint8Array> = [];
122
129
  let i = 0;
123
130
 
124
131
  while (i < buffer.length) {
125
- const opcode = buffer[i];
132
+ const opcode = buffer[i]!;
126
133
 
127
134
  // data chunk
128
135
  if (opcode > opcodes.OP_0 && opcode <= opcodes.OP_PUSHDATA4) {
@@ -159,23 +166,27 @@ export function decompile(buffer: Buffer | Array<number | Buffer>): Array<number
159
166
 
160
167
  /**
161
168
  * Converts the given chunks into an ASM (Assembly) string representation.
162
- * If the chunks parameter is a Buffer, it will be decompiled into a Stack before conversion.
169
+ * If the chunks parameter is a Uint8Array, it will be decompiled into a Stack before conversion.
163
170
  * @param chunks - The chunks to convert into ASM.
164
171
  * @returns The ASM string representation of the chunks.
165
172
  */
166
- export function toASM(chunks: Buffer | Array<number | Buffer>): string {
167
- if (chunksIsBuffer(chunks)) {
168
- chunks = decompile(chunks) as Stack;
169
- }
170
- if (!chunks) {
171
- throw new Error('Could not convert invalid chunks to ASM');
173
+ export function toASM(chunks: Uint8Array | Stack): string {
174
+ let resolved: Stack;
175
+ if (chunksIsUint8Array(chunks)) {
176
+ const decompiled = decompile(chunks);
177
+ if (!decompiled) {
178
+ throw new Error('Could not convert invalid chunks to ASM');
179
+ }
180
+ resolved = decompiled;
181
+ } else {
182
+ resolved = chunks;
172
183
  }
173
- return chunks
184
+ return resolved
174
185
  .map((chunk) => {
175
186
  // data?
176
- if (singleChunkIsBuffer(chunk)) {
187
+ if (singleChunkIsUint8Array(chunk)) {
177
188
  const op = asMinimalOP(chunk);
178
- if (op === undefined) return chunk.toString('hex');
189
+ if (op === undefined) return toHex(chunk);
179
190
  chunk = op;
180
191
  }
181
192
 
@@ -186,12 +197,14 @@ export function toASM(chunks: Buffer | Array<number | Buffer>): string {
186
197
  }
187
198
 
188
199
  /**
189
- * Converts an ASM string to a Buffer.
200
+ * Converts an ASM string to a Uint8Array.
190
201
  * @param asm The ASM string to convert.
191
- * @returns The converted Buffer.
202
+ * @returns The converted Uint8Array.
192
203
  */
193
- export function fromASM(asm: string): Buffer {
194
- typeforce(types.String, asm);
204
+ export function fromASM(asm: string): Script {
205
+ if (typeof asm !== 'string') {
206
+ throw new TypeError('Expected a string');
207
+ }
195
208
 
196
209
  return compile(
197
210
  asm.split(' ').map((chunkStr) => {
@@ -199,41 +212,43 @@ export function fromASM(asm: string): Buffer {
199
212
  if (opcodes[chunkStr as keyof Opcodes] !== undefined) {
200
213
  return opcodes[chunkStr as keyof Opcodes];
201
214
  }
202
- typeforce(types.Hex, chunkStr);
215
+ if (!types.isHex(chunkStr)) {
216
+ throw new TypeError('Expected hex string');
217
+ }
203
218
 
204
219
  // data!
205
- return Buffer.from(chunkStr, 'hex');
220
+ return fromHex(chunkStr);
206
221
  }),
207
222
  );
208
223
  }
209
224
 
210
225
  /**
211
- * Converts the given chunks into a stack of buffers.
226
+ * Converts the given chunks into a stack of Uint8Arrays.
212
227
  *
213
228
  * @param chunks - The chunks to convert.
214
- * @returns The stack of buffers.
229
+ * @returns The stack of Uint8Arrays.
215
230
  */
216
- export function toStack(chunks: Buffer | Array<number | Buffer>): Buffer[] {
217
- chunks = decompile(chunks) as Stack;
218
- typeforce(isPushOnly, chunks);
231
+ export function toStack(chunks: Uint8Array | Stack): Uint8Array[] {
232
+ const resolved = chunksIsUint8Array(chunks) ? decompile(chunks) : chunks;
233
+ if (!resolved || !isPushOnly(resolved)) {
234
+ throw new TypeError('Expected push-only script');
235
+ }
219
236
 
220
- return chunks.map((op) => {
221
- if (singleChunkIsBuffer(op)) return op;
222
- if (op === opcodes.OP_0) return Buffer.allocUnsafe(0);
237
+ return resolved.map((op) => {
238
+ if (singleChunkIsUint8Array(op)) return op;
239
+ if (op === opcodes.OP_0) return alloc(0);
223
240
 
224
241
  return scriptNumber.encode(op - OP_INT_BASE);
225
242
  });
226
243
  }
227
244
 
228
- export function isCanonicalPubKey(buffer: Buffer): boolean {
245
+ export function isCanonicalPubKey(buffer: Uint8Array): boolean {
229
246
  return types.isPoint(buffer);
230
247
  }
231
248
 
232
- import { isDefinedHashType } from './script_signature.js';
233
-
234
- export function isCanonicalScriptSignature(buffer: Buffer): boolean {
235
- if (!Buffer.isBuffer(buffer)) return false;
236
- if (!isDefinedHashType(buffer[buffer.length - 1])) return false;
249
+ export function isCanonicalScriptSignature(buffer: Uint8Array): boolean {
250
+ if (!(buffer instanceof Uint8Array)) return false;
251
+ if (!isDefinedHashType(buffer[buffer.length - 1]!)) return false;
237
252
 
238
253
  return bip66.check(buffer.subarray(0, -1));
239
254
  }
@@ -16,8 +16,8 @@ export function decode(buffer: Buffer, maxLength?: number, minimal?: boolean): n
16
16
  if (length === 0) return 0;
17
17
  if (length > maxLength) throw new TypeError('Script number overflow');
18
18
  if (minimal) {
19
- if ((buffer[length - 1] & 0x7f) === 0) {
20
- if (length <= 1 || (buffer[length - 2] & 0x80) === 0)
19
+ if ((buffer[length - 1]! & 0x7f) === 0) {
20
+ if (length <= 1 || (buffer[length - 2]! & 0x80) === 0)
21
21
  throw new Error('Non-minimally encoded script number');
22
22
  }
23
23
  }
@@ -34,10 +34,10 @@ export function decode(buffer: Buffer, maxLength?: number, minimal?: boolean): n
34
34
  // 32-bit / 24-bit / 16-bit / 8-bit
35
35
  let result = 0;
36
36
  for (let i = 0; i < length; ++i) {
37
- result |= buffer[i] << (8 * i);
37
+ result |= buffer[i]! << (8 * i);
38
38
  }
39
39
 
40
- if (buffer[length - 1] & 0x80) return -(result & ~(0x80 << (8 * (length - 1))));
40
+ if (buffer[length - 1]! & 0x80) return -(result & ~(0x80 << (8 * (length - 1))));
41
41
  return result;
42
42
  }
43
43
 
@@ -62,10 +62,10 @@ export function encode(_number: number): Buffer {
62
62
  value >>= 8;
63
63
  }
64
64
 
65
- if (buffer[size - 1] & 0x80) {
65
+ if (buffer[size - 1]! & 0x80) {
66
66
  buffer.writeUInt8(negative ? 0x80 : 0x00, size - 1);
67
67
  } else if (negative) {
68
- buffer[size - 1] |= 0x80;
68
+ buffer[size - 1]! |= 0x80;
69
69
  }
70
70
 
71
71
  return buffer;
@@ -1,9 +1,8 @@
1
1
  import * as bip66 from './bip66.js';
2
- import * as types from './types.js';
2
+ import { alloc, concat } from './io/index.js';
3
+ import { isUInt8, isUint8ArrayN } from './types.js';
3
4
 
4
- const { typeforce } = types;
5
-
6
- const ZERO = Buffer.alloc(1, 0);
5
+ const ZERO = new Uint8Array([0]);
7
6
 
8
7
  /**
9
8
  * Checks if a hash type is defined (valid for Bitcoin signatures).
@@ -16,48 +15,48 @@ export function isDefinedHashType(hashType: number): boolean {
16
15
  }
17
16
 
18
17
  /**
19
- * Converts a buffer to a DER-encoded buffer.
20
- * @param x - The buffer to be converted.
21
- * @returns The DER-encoded buffer.
18
+ * Converts a Uint8Array to a DER-encoded Uint8Array.
19
+ * @param x - The Uint8Array to be converted.
20
+ * @returns The DER-encoded Uint8Array.
22
21
  */
23
- function toDER(x: Buffer): Buffer {
22
+ function toDER(x: Uint8Array): Uint8Array {
24
23
  let i = 0;
25
24
  while (x[i] === 0) ++i;
26
25
  if (i === x.length) return ZERO;
27
26
  x = x.subarray(i);
28
- if (x[0] & 0x80) return Buffer.concat([ZERO, x], 1 + x.length);
27
+ if (x[0]! & 0x80) return concat([ZERO, x]);
29
28
  return x;
30
29
  }
31
30
 
32
31
  /**
33
- * Converts a DER-encoded signature to a buffer.
34
- * If the first byte of the input buffer is 0x00, it is skipped.
35
- * The resulting buffer is 32 bytes long, filled with zeros if necessary.
32
+ * Converts a DER-encoded signature to a Uint8Array.
33
+ * If the first byte of the input is 0x00, it is skipped.
34
+ * The resulting Uint8Array is 32 bytes long, filled with zeros if necessary.
36
35
  * @param x - The DER-encoded signature.
37
- * @returns The converted buffer.
36
+ * @returns The converted Uint8Array.
38
37
  */
39
- function fromDER(x: Buffer): Buffer {
38
+ function fromDER(x: Uint8Array): Uint8Array {
40
39
  if (x[0] === 0x00) x = x.subarray(1);
41
- const buffer = Buffer.alloc(32, 0);
40
+ const buffer = alloc(32);
42
41
  const bstart = Math.max(0, 32 - x.length);
43
- x.copy(buffer, bstart);
42
+ buffer.set(x, bstart);
44
43
  return buffer;
45
44
  }
46
45
 
47
- interface ScriptSignature {
48
- signature: Buffer;
46
+ export interface ScriptSignature {
47
+ signature: Uint8Array;
49
48
  hashType: number;
50
49
  }
51
50
 
52
51
  // BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed)
53
52
  /**
54
- * Decodes a buffer into a ScriptSignature object.
55
- * @param buffer - The buffer to decode.
53
+ * Decodes a Uint8Array into a ScriptSignature object.
54
+ * @param buffer - The Uint8Array to decode.
56
55
  * @returns The decoded ScriptSignature object.
57
56
  * @throws Error if the hashType is invalid.
58
57
  */
59
- export function decode(buffer: Buffer): ScriptSignature {
60
- const hashType = buffer.readUInt8(buffer.length - 1);
58
+ export function decode(buffer: Uint8Array): ScriptSignature {
59
+ const hashType = buffer[buffer.length - 1]!;
61
60
  if (!isDefinedHashType(hashType)) {
62
61
  throw new Error(`Invalid hashType ${hashType}`);
63
62
  }
@@ -65,36 +64,34 @@ export function decode(buffer: Buffer): ScriptSignature {
65
64
  const decoded = bip66.decode(buffer.subarray(0, -1));
66
65
  const r = fromDER(decoded.r);
67
66
  const s = fromDER(decoded.s);
68
- const signature = Buffer.concat([r, s], 64);
67
+ const signature = concat([r, s]);
69
68
 
70
69
  return { signature, hashType };
71
70
  }
72
71
 
73
72
  /**
74
- * Encodes a signature and hash type into a buffer.
73
+ * Encodes a signature and hash type into a Uint8Array.
75
74
  * @param signature - The signature to encode.
76
75
  * @param hashType - The hash type to encode.
77
- * @returns The encoded buffer.
76
+ * @returns The encoded Uint8Array.
78
77
  * @throws Error if the hashType is invalid.
79
78
  */
80
- export function encode(signature: Buffer, hashType: number): Buffer {
81
- typeforce(
82
- {
83
- signature: types.BufferN(64),
84
- hashType: types.UInt8,
85
- },
86
- { signature, hashType },
87
- );
79
+ export function encode(signature: Uint8Array, hashType: number): Uint8Array {
80
+ if (!isUint8ArrayN(signature, 64)) {
81
+ throw new TypeError('Expected signature to be a 64-byte Uint8Array');
82
+ }
83
+ if (!isUInt8(hashType)) {
84
+ throw new TypeError('Expected hashType to be a UInt8');
85
+ }
88
86
 
89
87
  if (!isDefinedHashType(hashType)) {
90
88
  throw new Error(`Invalid hashType ${hashType}`);
91
89
  }
92
90
 
93
- const hashTypeBuffer = Buffer.allocUnsafe(1);
94
- hashTypeBuffer.writeUInt8(hashType, 0);
91
+ const hashTypeBuffer = new Uint8Array([hashType]);
95
92
 
96
93
  const r = toDER(signature.subarray(0, 32));
97
94
  const s = toDER(signature.subarray(32, 64));
98
95
 
99
- return Buffer.concat([bip66.encode(r, s), hashTypeBuffer]);
96
+ return concat([bip66.encode(r, s), hashTypeBuffer]);
100
97
  }