@bsv/sdk 1.0.0

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 (464) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +40 -0
  2. package/.github/ISSUE_TEMPLATE/discussion.md +24 -0
  3. package/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md +23 -0
  4. package/CHANGELOG.md +72 -0
  5. package/CONTRIBUTING.md +85 -0
  6. package/LICENSE.txt +28 -0
  7. package/README.md +87 -0
  8. package/ROADMAP.md +3 -0
  9. package/dist/cjs/mod.js +25 -0
  10. package/dist/cjs/mod.js.map +1 -0
  11. package/dist/cjs/package.json +42 -0
  12. package/dist/cjs/src/compat/BIP39.js +272 -0
  13. package/dist/cjs/src/compat/BIP39.js.map +1 -0
  14. package/dist/cjs/src/compat/BSM.js +77 -0
  15. package/dist/cjs/src/compat/BSM.js.map +1 -0
  16. package/dist/cjs/src/compat/ECIES.js +483 -0
  17. package/dist/cjs/src/compat/ECIES.js.map +1 -0
  18. package/dist/cjs/src/compat/HD.js +326 -0
  19. package/dist/cjs/src/compat/HD.js.map +1 -0
  20. package/dist/cjs/src/compat/Mnemonic.js +298 -0
  21. package/dist/cjs/src/compat/Mnemonic.js.map +1 -0
  22. package/dist/cjs/src/compat/bip-39-wordlist-en.js +2057 -0
  23. package/dist/cjs/src/compat/bip-39-wordlist-en.js.map +1 -0
  24. package/dist/cjs/src/compat/index.js +37 -0
  25. package/dist/cjs/src/compat/index.js.map +1 -0
  26. package/dist/cjs/src/messages/EncryptedMessage.js +69 -0
  27. package/dist/cjs/src/messages/EncryptedMessage.js.map +1 -0
  28. package/dist/cjs/src/messages/SignedMessage.js +85 -0
  29. package/dist/cjs/src/messages/SignedMessage.js.map +1 -0
  30. package/dist/cjs/src/messages/index.js +29 -0
  31. package/dist/cjs/src/messages/index.js.map +1 -0
  32. package/dist/cjs/src/primitives/AESGCM.js +384 -0
  33. package/dist/cjs/src/primitives/AESGCM.js.map +1 -0
  34. package/dist/cjs/src/primitives/BasePoint.js +19 -0
  35. package/dist/cjs/src/primitives/BasePoint.js.map +1 -0
  36. package/dist/cjs/src/primitives/BigNumber.js +4269 -0
  37. package/dist/cjs/src/primitives/BigNumber.js.map +1 -0
  38. package/dist/cjs/src/primitives/Curve.js +1126 -0
  39. package/dist/cjs/src/primitives/Curve.js.map +1 -0
  40. package/dist/cjs/src/primitives/DRBG.js +99 -0
  41. package/dist/cjs/src/primitives/DRBG.js.map +1 -0
  42. package/dist/cjs/src/primitives/ECDSA.js +169 -0
  43. package/dist/cjs/src/primitives/ECDSA.js.map +1 -0
  44. package/dist/cjs/src/primitives/Hash.js +1332 -0
  45. package/dist/cjs/src/primitives/Hash.js.map +1 -0
  46. package/dist/cjs/src/primitives/JacobianPoint.js +400 -0
  47. package/dist/cjs/src/primitives/JacobianPoint.js.map +1 -0
  48. package/dist/cjs/src/primitives/K256.js +111 -0
  49. package/dist/cjs/src/primitives/K256.js.map +1 -0
  50. package/dist/cjs/src/primitives/Mersenne.js +118 -0
  51. package/dist/cjs/src/primitives/Mersenne.js.map +1 -0
  52. package/dist/cjs/src/primitives/MontgomoryMethod.js +150 -0
  53. package/dist/cjs/src/primitives/MontgomoryMethod.js.map +1 -0
  54. package/dist/cjs/src/primitives/Point.js +819 -0
  55. package/dist/cjs/src/primitives/Point.js.map +1 -0
  56. package/dist/cjs/src/primitives/PrivateKey.js +190 -0
  57. package/dist/cjs/src/primitives/PrivateKey.js.map +1 -0
  58. package/dist/cjs/src/primitives/PublicKey.js +151 -0
  59. package/dist/cjs/src/primitives/PublicKey.js.map +1 -0
  60. package/dist/cjs/src/primitives/Random.js +57 -0
  61. package/dist/cjs/src/primitives/Random.js.map +1 -0
  62. package/dist/cjs/src/primitives/ReductionContext.js +490 -0
  63. package/dist/cjs/src/primitives/ReductionContext.js.map +1 -0
  64. package/dist/cjs/src/primitives/Signature.js +220 -0
  65. package/dist/cjs/src/primitives/Signature.js.map +1 -0
  66. package/dist/cjs/src/primitives/SymmetricKey.js +69 -0
  67. package/dist/cjs/src/primitives/SymmetricKey.js.map +1 -0
  68. package/dist/cjs/src/primitives/TransactionSignature.js +172 -0
  69. package/dist/cjs/src/primitives/TransactionSignature.js.map +1 -0
  70. package/dist/cjs/src/primitives/index.js +45 -0
  71. package/dist/cjs/src/primitives/index.js.map +1 -0
  72. package/dist/cjs/src/primitives/utils.js +615 -0
  73. package/dist/cjs/src/primitives/utils.js.map +1 -0
  74. package/dist/cjs/src/script/LockingScript.js +35 -0
  75. package/dist/cjs/src/script/LockingScript.js.map +1 -0
  76. package/dist/cjs/src/script/OP.js +208 -0
  77. package/dist/cjs/src/script/OP.js.map +1 -0
  78. package/dist/cjs/src/script/Script.js +429 -0
  79. package/dist/cjs/src/script/Script.js.map +1 -0
  80. package/dist/cjs/src/script/ScriptChunk.js +3 -0
  81. package/dist/cjs/src/script/ScriptChunk.js.map +1 -0
  82. package/dist/cjs/src/script/ScriptTemplate.js +3 -0
  83. package/dist/cjs/src/script/ScriptTemplate.js.map +1 -0
  84. package/dist/cjs/src/script/Spend.js +1252 -0
  85. package/dist/cjs/src/script/Spend.js.map +1 -0
  86. package/dist/cjs/src/script/UnlockingScript.js +35 -0
  87. package/dist/cjs/src/script/UnlockingScript.js.map +1 -0
  88. package/dist/cjs/src/script/index.js +32 -0
  89. package/dist/cjs/src/script/index.js.map +1 -0
  90. package/dist/cjs/src/script/templates/P2PKH.js +98 -0
  91. package/dist/cjs/src/script/templates/P2PKH.js.map +1 -0
  92. package/dist/cjs/src/script/templates/RPuzzle.js +125 -0
  93. package/dist/cjs/src/script/templates/RPuzzle.js.map +1 -0
  94. package/dist/cjs/src/script/templates/index.js +11 -0
  95. package/dist/cjs/src/script/templates/index.js.map +1 -0
  96. package/dist/cjs/src/transaction/Broadcaster.js +3 -0
  97. package/dist/cjs/src/transaction/Broadcaster.js.map +1 -0
  98. package/dist/cjs/src/transaction/ChainTracker.js +3 -0
  99. package/dist/cjs/src/transaction/ChainTracker.js.map +1 -0
  100. package/dist/cjs/src/transaction/FeeModel.js +3 -0
  101. package/dist/cjs/src/transaction/FeeModel.js.map +1 -0
  102. package/dist/cjs/src/transaction/MerklePath.js +239 -0
  103. package/dist/cjs/src/transaction/MerklePath.js.map +1 -0
  104. package/dist/cjs/src/transaction/Transaction.js +557 -0
  105. package/dist/cjs/src/transaction/Transaction.js.map +1 -0
  106. package/dist/cjs/src/transaction/TransactionInput.js +3 -0
  107. package/dist/cjs/src/transaction/TransactionInput.js.map +1 -0
  108. package/dist/cjs/src/transaction/TransactionOutput.js +3 -0
  109. package/dist/cjs/src/transaction/TransactionOutput.js.map +1 -0
  110. package/dist/cjs/src/transaction/broadcasters/ARC.js +101 -0
  111. package/dist/cjs/src/transaction/broadcasters/ARC.js.map +1 -0
  112. package/dist/cjs/src/transaction/broadcasters/index.js +9 -0
  113. package/dist/cjs/src/transaction/broadcasters/index.js.map +1 -0
  114. package/dist/cjs/src/transaction/fee-models/SatoshisPerKilobyte.js +69 -0
  115. package/dist/cjs/src/transaction/fee-models/SatoshisPerKilobyte.js.map +1 -0
  116. package/dist/cjs/src/transaction/fee-models/index.js +9 -0
  117. package/dist/cjs/src/transaction/fee-models/index.js.map +1 -0
  118. package/dist/cjs/src/transaction/index.js +11 -0
  119. package/dist/cjs/src/transaction/index.js.map +1 -0
  120. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -0
  121. package/dist/esm/mod.js +9 -0
  122. package/dist/esm/mod.js.map +1 -0
  123. package/dist/esm/src/compat/BIP39.js +272 -0
  124. package/dist/esm/src/compat/BIP39.js.map +1 -0
  125. package/dist/esm/src/compat/BSM.js +45 -0
  126. package/dist/esm/src/compat/BSM.js.map +1 -0
  127. package/dist/esm/src/compat/ECIES.js +454 -0
  128. package/dist/esm/src/compat/ECIES.js.map +1 -0
  129. package/dist/esm/src/compat/HD.js +304 -0
  130. package/dist/esm/src/compat/HD.js.map +1 -0
  131. package/dist/esm/src/compat/Mnemonic.js +272 -0
  132. package/dist/esm/src/compat/Mnemonic.js.map +1 -0
  133. package/dist/esm/src/compat/bip-39-wordlist-en.js +2054 -0
  134. package/dist/esm/src/compat/bip-39-wordlist-en.js.map +1 -0
  135. package/dist/esm/src/compat/index.js +5 -0
  136. package/dist/esm/src/compat/index.js.map +1 -0
  137. package/dist/esm/src/messages/EncryptedMessage.js +61 -0
  138. package/dist/esm/src/messages/EncryptedMessage.js.map +1 -0
  139. package/dist/esm/src/messages/SignedMessage.js +77 -0
  140. package/dist/esm/src/messages/SignedMessage.js.map +1 -0
  141. package/dist/esm/src/messages/index.js +3 -0
  142. package/dist/esm/src/messages/index.js.map +1 -0
  143. package/dist/esm/src/primitives/AESGCM.js +371 -0
  144. package/dist/esm/src/primitives/AESGCM.js.map +1 -0
  145. package/dist/esm/src/primitives/BasePoint.js +16 -0
  146. package/dist/esm/src/primitives/BasePoint.js.map +1 -0
  147. package/dist/esm/src/primitives/BigNumber.js +4304 -0
  148. package/dist/esm/src/primitives/BigNumber.js.map +1 -0
  149. package/dist/esm/src/primitives/Curve.js +1141 -0
  150. package/dist/esm/src/primitives/Curve.js.map +1 -0
  151. package/dist/esm/src/primitives/DRBG.js +98 -0
  152. package/dist/esm/src/primitives/DRBG.js.map +1 -0
  153. package/dist/esm/src/primitives/ECDSA.js +161 -0
  154. package/dist/esm/src/primitives/ECDSA.js.map +1 -0
  155. package/dist/esm/src/primitives/Hash.js +1336 -0
  156. package/dist/esm/src/primitives/Hash.js.map +1 -0
  157. package/dist/esm/src/primitives/JacobianPoint.js +398 -0
  158. package/dist/esm/src/primitives/JacobianPoint.js.map +1 -0
  159. package/dist/esm/src/primitives/K256.js +105 -0
  160. package/dist/esm/src/primitives/K256.js.map +1 -0
  161. package/dist/esm/src/primitives/Mersenne.js +117 -0
  162. package/dist/esm/src/primitives/Mersenne.js.map +1 -0
  163. package/dist/esm/src/primitives/MontgomoryMethod.js +149 -0
  164. package/dist/esm/src/primitives/MontgomoryMethod.js.map +1 -0
  165. package/dist/esm/src/primitives/Point.js +816 -0
  166. package/dist/esm/src/primitives/Point.js.map +1 -0
  167. package/dist/esm/src/primitives/PrivateKey.js +184 -0
  168. package/dist/esm/src/primitives/PrivateKey.js.map +1 -0
  169. package/dist/esm/src/primitives/PublicKey.js +145 -0
  170. package/dist/esm/src/primitives/PublicKey.js.map +1 -0
  171. package/dist/esm/src/primitives/Random.js +56 -0
  172. package/dist/esm/src/primitives/Random.js.map +1 -0
  173. package/dist/esm/src/primitives/ReductionContext.js +486 -0
  174. package/dist/esm/src/primitives/ReductionContext.js.map +1 -0
  175. package/dist/esm/src/primitives/Signature.js +223 -0
  176. package/dist/esm/src/primitives/Signature.js.map +1 -0
  177. package/dist/esm/src/primitives/SymmetricKey.js +63 -0
  178. package/dist/esm/src/primitives/SymmetricKey.js.map +1 -0
  179. package/dist/esm/src/primitives/TransactionSignature.js +144 -0
  180. package/dist/esm/src/primitives/TransactionSignature.js.map +1 -0
  181. package/dist/esm/src/primitives/index.js +9 -0
  182. package/dist/esm/src/primitives/index.js.map +1 -0
  183. package/dist/esm/src/primitives/utils.js +601 -0
  184. package/dist/esm/src/primitives/utils.js.map +1 -0
  185. package/dist/esm/src/script/LockingScript.js +29 -0
  186. package/dist/esm/src/script/LockingScript.js.map +1 -0
  187. package/dist/esm/src/script/OP.js +206 -0
  188. package/dist/esm/src/script/OP.js.map +1 -0
  189. package/dist/esm/src/script/Script.js +424 -0
  190. package/dist/esm/src/script/Script.js.map +1 -0
  191. package/dist/esm/src/script/ScriptChunk.js +2 -0
  192. package/dist/esm/src/script/ScriptChunk.js.map +1 -0
  193. package/dist/esm/src/script/ScriptTemplate.js +2 -0
  194. package/dist/esm/src/script/ScriptTemplate.js.map +1 -0
  195. package/dist/esm/src/script/Spend.js +1240 -0
  196. package/dist/esm/src/script/Spend.js.map +1 -0
  197. package/dist/esm/src/script/UnlockingScript.js +29 -0
  198. package/dist/esm/src/script/UnlockingScript.js.map +1 -0
  199. package/dist/esm/src/script/index.js +7 -0
  200. package/dist/esm/src/script/index.js.map +1 -0
  201. package/dist/esm/src/script/templates/P2PKH.js +92 -0
  202. package/dist/esm/src/script/templates/P2PKH.js.map +1 -0
  203. package/dist/esm/src/script/templates/RPuzzle.js +119 -0
  204. package/dist/esm/src/script/templates/RPuzzle.js.map +1 -0
  205. package/dist/esm/src/script/templates/index.js +3 -0
  206. package/dist/esm/src/script/templates/index.js.map +1 -0
  207. package/dist/esm/src/transaction/Broadcaster.js +2 -0
  208. package/dist/esm/src/transaction/Broadcaster.js.map +1 -0
  209. package/dist/esm/src/transaction/ChainTracker.js +2 -0
  210. package/dist/esm/src/transaction/ChainTracker.js.map +1 -0
  211. package/dist/esm/src/transaction/FeeModel.js +2 -0
  212. package/dist/esm/src/transaction/FeeModel.js.map +1 -0
  213. package/dist/esm/src/transaction/MerklePath.js +237 -0
  214. package/dist/esm/src/transaction/MerklePath.js.map +1 -0
  215. package/dist/esm/src/transaction/Transaction.js +557 -0
  216. package/dist/esm/src/transaction/Transaction.js.map +1 -0
  217. package/dist/esm/src/transaction/TransactionInput.js +2 -0
  218. package/dist/esm/src/transaction/TransactionInput.js.map +1 -0
  219. package/dist/esm/src/transaction/TransactionOutput.js +2 -0
  220. package/dist/esm/src/transaction/TransactionOutput.js.map +1 -0
  221. package/dist/esm/src/transaction/broadcasters/ARC.js +100 -0
  222. package/dist/esm/src/transaction/broadcasters/ARC.js.map +1 -0
  223. package/dist/esm/src/transaction/broadcasters/index.js +2 -0
  224. package/dist/esm/src/transaction/broadcasters/index.js.map +1 -0
  225. package/dist/esm/src/transaction/fee-models/SatoshisPerKilobyte.js +71 -0
  226. package/dist/esm/src/transaction/fee-models/SatoshisPerKilobyte.js.map +1 -0
  227. package/dist/esm/src/transaction/fee-models/index.js +2 -0
  228. package/dist/esm/src/transaction/fee-models/index.js.map +1 -0
  229. package/dist/esm/src/transaction/index.js +3 -0
  230. package/dist/esm/src/transaction/index.js.map +1 -0
  231. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -0
  232. package/dist/types/mod.d.ts +9 -0
  233. package/dist/types/mod.d.ts.map +1 -0
  234. package/dist/types/src/compat/BIP39.d.ts +132 -0
  235. package/dist/types/src/compat/BIP39.d.ts.map +1 -0
  236. package/dist/types/src/compat/BSM.d.ts +28 -0
  237. package/dist/types/src/compat/BSM.d.ts.map +1 -0
  238. package/dist/types/src/compat/ECIES.d.ts +62 -0
  239. package/dist/types/src/compat/ECIES.d.ts.map +1 -0
  240. package/dist/types/src/compat/HD.d.ts +117 -0
  241. package/dist/types/src/compat/HD.d.ts.map +1 -0
  242. package/dist/types/src/compat/Mnemonic.d.ts +132 -0
  243. package/dist/types/src/compat/Mnemonic.d.ts.map +1 -0
  244. package/dist/types/src/compat/bip-39-wordlist-en.d.ts +5 -0
  245. package/dist/types/src/compat/bip-39-wordlist-en.d.ts.map +1 -0
  246. package/dist/types/src/compat/index.d.ts +5 -0
  247. package/dist/types/src/compat/index.d.ts.map +1 -0
  248. package/dist/types/src/messages/EncryptedMessage.d.ts +20 -0
  249. package/dist/types/src/messages/EncryptedMessage.d.ts.map +1 -0
  250. package/dist/types/src/messages/SignedMessage.d.ts +21 -0
  251. package/dist/types/src/messages/SignedMessage.d.ts.map +1 -0
  252. package/dist/types/src/messages/index.d.ts +3 -0
  253. package/dist/types/src/messages/index.d.ts.map +1 -0
  254. package/dist/types/src/primitives/AESGCM.d.ts +14 -0
  255. package/dist/types/src/primitives/AESGCM.d.ts.map +1 -0
  256. package/dist/types/src/primitives/BasePoint.d.ts +22 -0
  257. package/dist/types/src/primitives/BasePoint.d.ts.map +1 -0
  258. package/dist/types/src/primitives/BigNumber.d.ts +1895 -0
  259. package/dist/types/src/primitives/BigNumber.d.ts.map +1 -0
  260. package/dist/types/src/primitives/Curve.d.ts +55 -0
  261. package/dist/types/src/primitives/Curve.d.ts.map +1 -0
  262. package/dist/types/src/primitives/DRBG.d.ts +54 -0
  263. package/dist/types/src/primitives/DRBG.d.ts.map +1 -0
  264. package/dist/types/src/primitives/ECDSA.d.ts +39 -0
  265. package/dist/types/src/primitives/ECDSA.d.ts.map +1 -0
  266. package/dist/types/src/primitives/Hash.d.ts +411 -0
  267. package/dist/types/src/primitives/Hash.d.ts.map +1 -0
  268. package/dist/types/src/primitives/JacobianPoint.d.ts +164 -0
  269. package/dist/types/src/primitives/JacobianPoint.d.ts.map +1 -0
  270. package/dist/types/src/primitives/K256.d.ts +53 -0
  271. package/dist/types/src/primitives/K256.d.ts.map +1 -0
  272. package/dist/types/src/primitives/Mersenne.d.ts +72 -0
  273. package/dist/types/src/primitives/Mersenne.d.ts.map +1 -0
  274. package/dist/types/src/primitives/MontgomoryMethod.d.ts +96 -0
  275. package/dist/types/src/primitives/MontgomoryMethod.d.ts.map +1 -0
  276. package/dist/types/src/primitives/Point.d.ts +303 -0
  277. package/dist/types/src/primitives/Point.d.ts.map +1 -0
  278. package/dist/types/src/primitives/PrivateKey.d.ts +143 -0
  279. package/dist/types/src/primitives/PrivateKey.d.ts.map +1 -0
  280. package/dist/types/src/primitives/PublicKey.d.ts +108 -0
  281. package/dist/types/src/primitives/PublicKey.d.ts.map +1 -0
  282. package/dist/types/src/primitives/Random.d.ts +14 -0
  283. package/dist/types/src/primitives/Random.d.ts.map +1 -0
  284. package/dist/types/src/primitives/ReductionContext.d.ts +308 -0
  285. package/dist/types/src/primitives/ReductionContext.d.ts.map +1 -0
  286. package/dist/types/src/primitives/Signature.d.ts +100 -0
  287. package/dist/types/src/primitives/Signature.d.ts.map +1 -0
  288. package/dist/types/src/primitives/SymmetricKey.d.ts +44 -0
  289. package/dist/types/src/primitives/SymmetricKey.d.ts.map +1 -0
  290. package/dist/types/src/primitives/TransactionSignature.d.ts +36 -0
  291. package/dist/types/src/primitives/TransactionSignature.d.ts.map +1 -0
  292. package/dist/types/src/primitives/index.d.ts +9 -0
  293. package/dist/types/src/primitives/index.d.ts.map +1 -0
  294. package/dist/types/src/primitives/utils.d.ts +118 -0
  295. package/dist/types/src/primitives/utils.d.ts.map +1 -0
  296. package/dist/types/src/script/LockingScript.d.ts +25 -0
  297. package/dist/types/src/script/LockingScript.d.ts.map +1 -0
  298. package/dist/types/src/script/OP.d.ts +193 -0
  299. package/dist/types/src/script/OP.d.ts.map +1 -0
  300. package/dist/types/src/script/Script.d.ts +148 -0
  301. package/dist/types/src/script/Script.d.ts.map +1 -0
  302. package/dist/types/src/script/ScriptChunk.d.ts +8 -0
  303. package/dist/types/src/script/ScriptChunk.d.ts.map +1 -0
  304. package/dist/types/src/script/ScriptTemplate.d.ts +33 -0
  305. package/dist/types/src/script/ScriptTemplate.d.ts.map +1 -0
  306. package/dist/types/src/script/Spend.d.ts +103 -0
  307. package/dist/types/src/script/Spend.d.ts.map +1 -0
  308. package/dist/types/src/script/UnlockingScript.d.ts +25 -0
  309. package/dist/types/src/script/UnlockingScript.d.ts.map +1 -0
  310. package/dist/types/src/script/index.d.ts +8 -0
  311. package/dist/types/src/script/index.d.ts.map +1 -0
  312. package/dist/types/src/script/templates/P2PKH.d.ts +37 -0
  313. package/dist/types/src/script/templates/P2PKH.d.ts.map +1 -0
  314. package/dist/types/src/script/templates/RPuzzle.d.ts +47 -0
  315. package/dist/types/src/script/templates/RPuzzle.d.ts.map +1 -0
  316. package/dist/types/src/script/templates/index.d.ts +3 -0
  317. package/dist/types/src/script/templates/index.d.ts.map +1 -0
  318. package/dist/types/src/transaction/Broadcaster.d.ts +39 -0
  319. package/dist/types/src/transaction/Broadcaster.d.ts.map +1 -0
  320. package/dist/types/src/transaction/ChainTracker.d.ts +23 -0
  321. package/dist/types/src/transaction/ChainTracker.d.ts.map +1 -0
  322. package/dist/types/src/transaction/FeeModel.d.ts +12 -0
  323. package/dist/types/src/transaction/FeeModel.d.ts.map +1 -0
  324. package/dist/types/src/transaction/MerklePath.d.ts +91 -0
  325. package/dist/types/src/transaction/MerklePath.d.ts.map +1 -0
  326. package/dist/types/src/transaction/Transaction.d.ts +181 -0
  327. package/dist/types/src/transaction/Transaction.d.ts.map +1 -0
  328. package/dist/types/src/transaction/TransactionInput.d.ts +63 -0
  329. package/dist/types/src/transaction/TransactionInput.d.ts.map +1 -0
  330. package/dist/types/src/transaction/TransactionOutput.d.ts +36 -0
  331. package/dist/types/src/transaction/TransactionOutput.d.ts.map +1 -0
  332. package/dist/types/src/transaction/broadcasters/ARC.d.ts +28 -0
  333. package/dist/types/src/transaction/broadcasters/ARC.d.ts.map +1 -0
  334. package/dist/types/src/transaction/broadcasters/index.d.ts +2 -0
  335. package/dist/types/src/transaction/broadcasters/index.d.ts.map +1 -0
  336. package/dist/types/src/transaction/fee-models/SatoshisPerKilobyte.d.ts +26 -0
  337. package/dist/types/src/transaction/fee-models/SatoshisPerKilobyte.d.ts.map +1 -0
  338. package/dist/types/src/transaction/fee-models/index.d.ts +2 -0
  339. package/dist/types/src/transaction/fee-models/index.d.ts.map +1 -0
  340. package/dist/types/src/transaction/index.d.ts +7 -0
  341. package/dist/types/src/transaction/index.d.ts.map +1 -0
  342. package/dist/types/tsconfig.types.tsbuildinfo +1 -0
  343. package/docs/README.md +9 -0
  344. package/docs/compat.md +2856 -0
  345. package/docs/getting-started/COMMONJS.md +94 -0
  346. package/docs/getting-started/REACT-TS.md +131 -0
  347. package/docs/getting-started/TS-NODE.md +106 -0
  348. package/docs/getting-started/VUE.md +103 -0
  349. package/docs/messages.md +146 -0
  350. package/docs/primitives.md +7440 -0
  351. package/docs/script.md +766 -0
  352. package/docs/transaction.md +741 -0
  353. package/jest.config.js +6 -0
  354. package/mod.ts +8 -0
  355. package/package.json +137 -0
  356. package/src/compat/BSM.ts +51 -0
  357. package/src/compat/ECIES.ts +557 -0
  358. package/src/compat/HD.ts +348 -0
  359. package/src/compat/Mnemonic.ts +295 -0
  360. package/src/compat/__tests/BSM.test.ts +38 -0
  361. package/src/compat/__tests/ECIES.test.ts +90 -0
  362. package/src/compat/__tests/HD.test.ts +405 -0
  363. package/src/compat/__tests/Mnemonic.test.ts +177 -0
  364. package/src/compat/__tests/Mnemonic.vectors.ts +172 -0
  365. package/src/compat/bip-39-wordlist-en.ts +2053 -0
  366. package/src/compat/index.ts +4 -0
  367. package/src/messages/EncryptedMessage.ts +70 -0
  368. package/src/messages/SignedMessage.ts +87 -0
  369. package/src/messages/__tests/EncryptedMessage.test.ts +36 -0
  370. package/src/messages/__tests/SignedMessage.test.ts +53 -0
  371. package/src/messages/index.ts +2 -0
  372. package/src/primitives/AESGCM.ts +479 -0
  373. package/src/primitives/BasePoint.ts +21 -0
  374. package/src/primitives/BigNumber.ts +4619 -0
  375. package/src/primitives/Curve.ts +1163 -0
  376. package/src/primitives/DRBG.ts +102 -0
  377. package/src/primitives/ECDSA.ts +164 -0
  378. package/src/primitives/Hash.ts +1420 -0
  379. package/src/primitives/JacobianPoint.ts +410 -0
  380. package/src/primitives/K256.ts +116 -0
  381. package/src/primitives/Mersenne.ts +123 -0
  382. package/src/primitives/MontgomoryMethod.ts +160 -0
  383. package/src/primitives/Point.ts +852 -0
  384. package/src/primitives/PrivateKey.ts +195 -0
  385. package/src/primitives/PublicKey.ts +154 -0
  386. package/src/primitives/Random.ts +55 -0
  387. package/src/primitives/ReductionContext.ts +528 -0
  388. package/src/primitives/Signature.ts +235 -0
  389. package/src/primitives/SymmetricKey.ts +75 -0
  390. package/src/primitives/TransactionSignature.ts +189 -0
  391. package/src/primitives/__tests/AESGCM.test.ts +338 -0
  392. package/src/primitives/__tests/BRC42.private.vectors.ts +33 -0
  393. package/src/primitives/__tests/BRC42.public.vectors.ts +33 -0
  394. package/src/primitives/__tests/BigNumber.arithmatic.test.ts +572 -0
  395. package/src/primitives/__tests/BigNumber.binary.test.ts +203 -0
  396. package/src/primitives/__tests/BigNumber.constructor.test.ts +176 -0
  397. package/src/primitives/__tests/BigNumber.dhGroup.test.ts +18 -0
  398. package/src/primitives/__tests/BigNumber.fixtures.ts +264 -0
  399. package/src/primitives/__tests/BigNumber.serializers.test.ts +157 -0
  400. package/src/primitives/__tests/BigNumber.utils.test.ts +347 -0
  401. package/src/primitives/__tests/Curve.unit.test.ts +192 -0
  402. package/src/primitives/__tests/DRBG.test.ts +18 -0
  403. package/src/primitives/__tests/DRBG.vectors.ts +167 -0
  404. package/src/primitives/__tests/ECDH.test.ts +31 -0
  405. package/src/primitives/__tests/ECDSA.test.ts +58 -0
  406. package/src/primitives/__tests/HMAC.test.ts +59 -0
  407. package/src/primitives/__tests/Hash.test.ts +121 -0
  408. package/src/primitives/__tests/PBKDF2.vectors.ts +119 -0
  409. package/src/primitives/__tests/PrivateKey.test.ts +17 -0
  410. package/src/primitives/__tests/PublicKey.test.ts +66 -0
  411. package/src/primitives/__tests/Random.test.ts +14 -0
  412. package/src/primitives/__tests/Reader.test.ts +296 -0
  413. package/src/primitives/__tests/ReductionContext.test.ts +279 -0
  414. package/src/primitives/__tests/SymmetricKey.test.ts +58 -0
  415. package/src/primitives/__tests/SymmetricKey.vectors.ts +40 -0
  416. package/src/primitives/__tests/Writer.test.ts +198 -0
  417. package/src/primitives/__tests/sighash.vectors.ts +3503 -0
  418. package/src/primitives/__tests/utils.test.ts +108 -0
  419. package/src/primitives/index.ts +8 -0
  420. package/src/primitives/utils.ts +665 -0
  421. package/src/script/LockingScript.ts +30 -0
  422. package/src/script/OP.ts +219 -0
  423. package/src/script/Script.ts +426 -0
  424. package/src/script/ScriptChunk.ts +7 -0
  425. package/src/script/ScriptTemplate.ts +36 -0
  426. package/src/script/Spend.ts +1379 -0
  427. package/src/script/UnlockingScript.ts +30 -0
  428. package/src/script/__tests/Script.test.ts +369 -0
  429. package/src/script/__tests/Spend.test.ts +248 -0
  430. package/src/script/__tests/script.invalid.vectors.ts +925 -0
  431. package/src/script/__tests/script.valid.vectors.ts +1120 -0
  432. package/src/script/__tests/scriptFromVector.ts +42 -0
  433. package/src/script/__tests/spend.valid.vectors.ts +2288 -0
  434. package/src/script/index.ts +7 -0
  435. package/src/script/templates/P2PKH.ts +109 -0
  436. package/src/script/templates/RPuzzle.ts +140 -0
  437. package/src/script/templates/index.ts +2 -0
  438. package/src/transaction/Broadcaster.ts +42 -0
  439. package/src/transaction/ChainTracker.ts +22 -0
  440. package/src/transaction/FeeModel.ts +13 -0
  441. package/src/transaction/MerklePath.ts +259 -0
  442. package/src/transaction/Transaction.ts +602 -0
  443. package/src/transaction/TransactionInput.ts +63 -0
  444. package/src/transaction/TransactionOutput.ts +37 -0
  445. package/src/transaction/__tests/MerklePath.test.ts +181 -0
  446. package/src/transaction/__tests/Transaction.test.ts +413 -0
  447. package/src/transaction/__tests/bigtx.vectors.ts +4 -0
  448. package/src/transaction/__tests/bump.invalid.vectors.ts +8 -0
  449. package/src/transaction/__tests/bump.valid.vectors.ts +4 -0
  450. package/src/transaction/__tests/tx.invalid.vectors.ts +281 -0
  451. package/src/transaction/__tests/tx.valid.vectors.ts +364 -0
  452. package/src/transaction/broadcasters/ARC.ts +106 -0
  453. package/src/transaction/broadcasters/__tests/ARC.test.ts +115 -0
  454. package/src/transaction/broadcasters/index.ts +1 -0
  455. package/src/transaction/fee-models/SatoshisPerKilobyte.ts +71 -0
  456. package/src/transaction/fee-models/index.ts +1 -0
  457. package/src/transaction/index.ts +6 -0
  458. package/ts2md.json +5 -0
  459. package/tsconfig.base.json +26 -0
  460. package/tsconfig.cjs.json +11 -0
  461. package/tsconfig.eslint.json +12 -0
  462. package/tsconfig.esm.json +9 -0
  463. package/tsconfig.json +17 -0
  464. package/tsconfig.types.json +11 -0
@@ -0,0 +1,219 @@
1
+ /**
2
+ * An object mapping opcode names (such as OP_DUP) to their corresponding numbers (such as 0x76), and vice versa.
3
+ */
4
+ const OP = {
5
+ // push value
6
+ OP_FALSE: 0x00,
7
+ OP_0: 0x00,
8
+ OP_PUSHDATA1: 0x4c,
9
+ OP_PUSHDATA2: 0x4d,
10
+ OP_PUSHDATA4: 0x4e,
11
+ OP_1NEGATE: 0x4f,
12
+ OP_RESERVED: 0x50,
13
+ OP_TRUE: 0x51,
14
+ OP_1: 0x51,
15
+ OP_2: 0x52,
16
+ OP_3: 0x53,
17
+ OP_4: 0x54,
18
+ OP_5: 0x55,
19
+ OP_6: 0x56,
20
+ OP_7: 0x57,
21
+ OP_8: 0x58,
22
+ OP_9: 0x59,
23
+ OP_10: 0x5a,
24
+ OP_11: 0x5b,
25
+ OP_12: 0x5c,
26
+ OP_13: 0x5d,
27
+ OP_14: 0x5e,
28
+ OP_15: 0x5f,
29
+ OP_16: 0x60,
30
+
31
+ // control
32
+ OP_NOP: 0x61,
33
+ OP_VER: 0x62,
34
+ OP_IF: 0x63,
35
+ OP_NOTIF: 0x64,
36
+ OP_VERIF: 0x65,
37
+ OP_VERNOTIF: 0x66,
38
+ OP_ELSE: 0x67,
39
+ OP_ENDIF: 0x68,
40
+ OP_VERIFY: 0x69,
41
+ OP_RETURN: 0x6a,
42
+
43
+ // stack ops
44
+ OP_TOALTSTACK: 0x6b,
45
+ OP_FROMALTSTACK: 0x6c,
46
+ OP_2DROP: 0x6d,
47
+ OP_2DUP: 0x6e,
48
+ OP_3DUP: 0x6f,
49
+ OP_2OVER: 0x70,
50
+ OP_2ROT: 0x71,
51
+ OP_2SWAP: 0x72,
52
+ OP_IFDUP: 0x73,
53
+ OP_DEPTH: 0x74,
54
+ OP_DROP: 0x75,
55
+ OP_DUP: 0x76,
56
+ OP_NIP: 0x77,
57
+ OP_OVER: 0x78,
58
+ OP_PICK: 0x79,
59
+ OP_ROLL: 0x7a,
60
+ OP_ROT: 0x7b,
61
+ OP_SWAP: 0x7c,
62
+ OP_TUCK: 0x7d,
63
+
64
+ // data manipulation ops
65
+ OP_CAT: 0x7e,
66
+ OP_SUBSTR: 0x7f, // Replaced in BSV
67
+ OP_SPLIT: 0x7f,
68
+ OP_LEFT: 0x80, // Replaced in BSV
69
+ OP_NUM2BIN: 0x80,
70
+ OP_RIGHT: 0x81, // Replaced in BSV
71
+ OP_BIN2NUM: 0x81,
72
+ OP_SIZE: 0x82,
73
+
74
+ // bit logic
75
+ OP_INVERT: 0x83,
76
+ OP_AND: 0x84,
77
+ OP_OR: 0x85,
78
+ OP_XOR: 0x86,
79
+ OP_EQUAL: 0x87,
80
+ OP_EQUALVERIFY: 0x88,
81
+ OP_RESERVED1: 0x89,
82
+ OP_RESERVED2: 0x8a,
83
+
84
+ // numeric
85
+ OP_1ADD: 0x8b,
86
+ OP_1SUB: 0x8c,
87
+ OP_2MUL: 0x8d,
88
+ OP_2DIV: 0x8e,
89
+ OP_NEGATE: 0x8f,
90
+ OP_ABS: 0x90,
91
+ OP_NOT: 0x91,
92
+ OP_0NOTEQUAL: 0x92,
93
+
94
+ OP_ADD: 0x93,
95
+ OP_SUB: 0x94,
96
+ OP_MUL: 0x95,
97
+ OP_DIV: 0x96,
98
+ OP_MOD: 0x97,
99
+ OP_LSHIFT: 0x98,
100
+ OP_RSHIFT: 0x99,
101
+
102
+ OP_BOOLAND: 0x9a,
103
+ OP_BOOLOR: 0x9b,
104
+ OP_NUMEQUAL: 0x9c,
105
+ OP_NUMEQUALVERIFY: 0x9d,
106
+ OP_NUMNOTEQUAL: 0x9e,
107
+ OP_LESSTHAN: 0x9f,
108
+ OP_GREATERTHAN: 0xa0,
109
+ OP_LESSTHANOREQUAL: 0xa1,
110
+ OP_GREATERTHANOREQUAL: 0xa2,
111
+ OP_MIN: 0xa3,
112
+ OP_MAX: 0xa4,
113
+
114
+ OP_WITHIN: 0xa5,
115
+
116
+ // crypto
117
+ OP_RIPEMD160: 0xa6,
118
+ OP_SHA1: 0xa7,
119
+ OP_SHA256: 0xa8,
120
+ OP_HASH160: 0xa9,
121
+ OP_HASH256: 0xaa,
122
+ OP_CODESEPARATOR: 0xab,
123
+ OP_CHECKSIG: 0xac,
124
+ OP_CHECKSIGVERIFY: 0xad,
125
+ OP_CHECKMULTISIG: 0xae,
126
+ OP_CHECKMULTISIGVERIFY: 0xaf,
127
+
128
+ // expansion
129
+ OP_NOP1: 0xb0,
130
+ OP_NOP2: 0xb1,
131
+ OP_NOP3: 0xb2,
132
+ OP_NOP4: 0xb3,
133
+ OP_NOP5: 0xb4,
134
+ OP_NOP6: 0xb5,
135
+ OP_NOP7: 0xb6,
136
+ OP_NOP8: 0xb7,
137
+ OP_NOP9: 0xb8,
138
+ OP_NOP10: 0xb9,
139
+ OP_NOP11: 0xba,
140
+ OP_NOP12: 0xbb,
141
+ OP_NOP13: 0xbc,
142
+ OP_NOP14: 0xbd,
143
+ OP_NOP15: 0xbe,
144
+ OP_NOP16: 0xbf,
145
+ OP_NOP17: 0xc0,
146
+ OP_NOP18: 0xc1,
147
+ OP_NOP19: 0xc2,
148
+ OP_NOP20: 0xc3,
149
+ OP_NOP21: 0xc4,
150
+ OP_NOP22: 0xc5,
151
+ OP_NOP23: 0xc6,
152
+ OP_NOP24: 0xc7,
153
+ OP_NOP25: 0xc8,
154
+ OP_NOP26: 0xc9,
155
+ OP_NOP27: 0xca,
156
+ OP_NOP28: 0xcb,
157
+ OP_NOP29: 0xcc,
158
+ OP_NOP30: 0xcd,
159
+ OP_NOP31: 0xce,
160
+ OP_NOP32: 0xcf,
161
+ OP_NOP33: 0xd0,
162
+ OP_NOP34: 0xd1,
163
+ OP_NOP35: 0xd2,
164
+ OP_NOP36: 0xd3,
165
+ OP_NOP37: 0xd4,
166
+ OP_NOP38: 0xd5,
167
+ OP_NOP39: 0xd6,
168
+ OP_NOP40: 0xd7,
169
+ OP_NOP41: 0xd8,
170
+ OP_NOP42: 0xd9,
171
+ OP_NOP43: 0xda,
172
+ OP_NOP44: 0xdb,
173
+ OP_NOP45: 0xdc,
174
+ OP_NOP46: 0xdd,
175
+ OP_NOP47: 0xde,
176
+ OP_NOP48: 0xdf,
177
+ OP_NOP49: 0xe0,
178
+ OP_NOP50: 0xe1,
179
+ OP_NOP51: 0xe2,
180
+ OP_NOP52: 0xe3,
181
+ OP_NOP53: 0xe4,
182
+ OP_NOP54: 0xe5,
183
+ OP_NOP55: 0xe6,
184
+ OP_NOP56: 0xe7,
185
+ OP_NOP57: 0xe8,
186
+ OP_NOP58: 0xe9,
187
+ OP_NOP59: 0xea,
188
+ OP_NOP60: 0xeb,
189
+ OP_NOP61: 0xec,
190
+ OP_NOP62: 0xed,
191
+ OP_NOP63: 0xee,
192
+ OP_NOP64: 0xef,
193
+ OP_NOP65: 0xf0,
194
+ OP_NOP66: 0xf1,
195
+ OP_NOP67: 0xf2,
196
+ OP_NOP68: 0xf3,
197
+ OP_NOP69: 0xf4,
198
+ OP_NOP70: 0xf5,
199
+ OP_NOP71: 0xf6,
200
+ OP_NOP72: 0xf7,
201
+ OP_NOP73: 0xf8,
202
+ OP_NOP77: 0xfc,
203
+
204
+ // template matching params
205
+ OP_SMALLDATA: 0xf9,
206
+ OP_SMALLINTEGER: 0xfa,
207
+ OP_PUBKEYS: 0xfb,
208
+ OP_PUBKEYHASH: 0xfd,
209
+ OP_PUBKEY: 0xfe,
210
+
211
+ OP_INVALIDOPCODE: 0xff
212
+ }
213
+
214
+ for (const name in OP) {
215
+ OP[OP[name]] = name
216
+ OP[String(OP[name])] = name
217
+ }
218
+
219
+ export default OP
@@ -0,0 +1,426 @@
1
+ import ScriptChunk from './ScriptChunk.js'
2
+ import OP from './OP.js'
3
+ import { encode, toHex, Reader, Writer, toArray } from '../primitives/utils.js'
4
+ import BigNumber from '../primitives/BigNumber.js'
5
+
6
+ /**
7
+ * The Script class represents a script in a Bitcoin SV transaction,
8
+ * encapsulating the functionality to construct, parse, and serialize
9
+ * scripts used in both locking (output) and unlocking (input) scripts.
10
+ *
11
+ * @property {ScriptChunk[]} chunks - An array of script chunks that make up the script.
12
+ */
13
+ export default class Script {
14
+ chunks: ScriptChunk[]
15
+
16
+ /**
17
+ * @method fromASM
18
+ * Static method to construct a Script instance from an ASM (Assembly) formatted string.
19
+ * @param asm - The script in ASM string format.
20
+ * @returns A new Script instance.
21
+ * @example
22
+ * const script = Script.fromASM("OP_DUP OP_HASH160 abcd... OP_EQUALVERIFY OP_CHECKSIG")
23
+ */
24
+ static fromASM (asm: string): Script {
25
+ const chunks: ScriptChunk[] = []
26
+ const tokens = asm.split(' ')
27
+ let i = 0
28
+ while (i < tokens.length) {
29
+ const token = tokens[i]
30
+ let opCode
31
+ let opCodeNum: number
32
+ if (typeof OP[token] !== 'undefined') {
33
+ opCode = token
34
+ opCodeNum = OP[token]
35
+ }
36
+
37
+ // we start with two special cases, 0 and -1, which are handled specially in
38
+ // toASM. see _chunkToString.
39
+ if (token === '0') {
40
+ opCodeNum = 0
41
+ chunks.push({
42
+ op: opCodeNum
43
+ })
44
+ i = i + 1
45
+ } else if (token === '-1') {
46
+ opCodeNum = OP.OP_1NEGATE
47
+ chunks.push({
48
+ op: opCodeNum
49
+ })
50
+ i = i + 1
51
+ } else if (opCode === undefined) {
52
+ let hex = tokens[i]
53
+ if (hex.length % 2 !== 0) {
54
+ hex = '0' + hex
55
+ }
56
+ const arr = toArray(hex, 'hex')
57
+ if (encode(arr, 'hex') !== hex) {
58
+ throw new Error('invalid hex string in script')
59
+ }
60
+ const len = arr.length
61
+ if (len >= 0 && len < OP.OP_PUSHDATA1) {
62
+ opCodeNum = len
63
+ } else if (len < Math.pow(2, 8)) {
64
+ opCodeNum = OP.OP_PUSHDATA1
65
+ } else if (len < Math.pow(2, 16)) {
66
+ opCodeNum = OP.OP_PUSHDATA2
67
+ } else if (len < Math.pow(2, 32)) {
68
+ opCodeNum = OP.OP_PUSHDATA4
69
+ }
70
+ chunks.push({
71
+ data: arr,
72
+ op: opCodeNum
73
+ })
74
+ i = i + 1
75
+ } else if (
76
+ opCodeNum === OP.OP_PUSHDATA1 ||
77
+ opCodeNum === OP.OP_PUSHDATA2 ||
78
+ opCodeNum === OP.OP_PUSHDATA4
79
+ ) {
80
+ chunks.push({
81
+ data: toArray(tokens[i + 2], 'hex'),
82
+ op: opCodeNum
83
+ })
84
+ i = i + 3
85
+ } else {
86
+ chunks.push({
87
+ op: opCodeNum
88
+ })
89
+ i = i + 1
90
+ }
91
+ }
92
+ return new Script(chunks)
93
+ }
94
+
95
+ /**
96
+ * @method fromHex
97
+ * Static method to construct a Script instance from a hexadecimal string.
98
+ * @param hex - The script in hexadecimal format.
99
+ * @returns A new Script instance.
100
+ * @example
101
+ * const script = Script.fromHex("76a9...");
102
+ */
103
+ static fromHex (hex: string): Script {
104
+ return Script.fromBinary(toArray(hex, 'hex'))
105
+ }
106
+
107
+ /**
108
+ * @method fromBinary
109
+ * Static method to construct a Script instance from a binary array.
110
+ * @param bin - The script in binary array format.
111
+ * @returns A new Script instance.
112
+ * @example
113
+ * const script = Script.fromBinary([0x76, 0xa9, ...])
114
+ */
115
+ static fromBinary (bin: number[]): Script {
116
+ bin = [...bin]
117
+ const chunks: ScriptChunk[] = []
118
+
119
+ const br = new Reader(bin)
120
+ while (!br.eof()) {
121
+ const op = br.readUInt8()
122
+
123
+ let len = 0
124
+ // eslint-disable-next-line @typescript-eslint/no-shadow
125
+ let data: number[] = []
126
+ if (op > 0 && op < OP.OP_PUSHDATA1) {
127
+ len = op
128
+ chunks.push({
129
+ data: br.read(len),
130
+ op
131
+ })
132
+ } else if (op === OP.OP_PUSHDATA1) {
133
+ try {
134
+ len = br.readUInt8()
135
+ data = br.read(len)
136
+ } catch (err) {
137
+ br.read()
138
+ }
139
+ chunks.push({
140
+ data,
141
+ op
142
+ })
143
+ } else if (op === OP.OP_PUSHDATA2) {
144
+ try {
145
+ len = br.readUInt16LE()
146
+ data = br.read(len)
147
+ } catch (err) {
148
+ br.read()
149
+ }
150
+ chunks.push({
151
+ data,
152
+ op
153
+ })
154
+ } else if (op === OP.OP_PUSHDATA4) {
155
+ try {
156
+ len = br.readUInt32LE()
157
+ data = br.read(len)
158
+ } catch (err) {
159
+ br.read()
160
+ }
161
+ chunks.push({
162
+ data,
163
+ op
164
+ })
165
+ } else {
166
+ chunks.push({
167
+ op
168
+ })
169
+ }
170
+ }
171
+ return new Script(chunks)
172
+ }
173
+
174
+ /**
175
+ * @constructor
176
+ * Constructs a new Script object.
177
+ * @param chunks=[] - An array of script chunks to directly initialize the script.
178
+ */
179
+ constructor (chunks: ScriptChunk[] = []) {
180
+ this.chunks = chunks
181
+ }
182
+
183
+ /**
184
+ * @method toASM
185
+ * Serializes the script to an ASM formatted string.
186
+ * @returns The script in ASM string format.
187
+ */
188
+ toASM (): string {
189
+ let str = ''
190
+ for (let i = 0; i < this.chunks.length; i++) {
191
+ const chunk = this.chunks[i]
192
+ str += this._chunkToString(chunk)
193
+ }
194
+
195
+ return str.slice(1)
196
+ }
197
+
198
+ /**
199
+ * @method toHex
200
+ * Serializes the script to a hexadecimal string.
201
+ * @returns The script in hexadecimal format.
202
+ */
203
+ toHex (): string {
204
+ return encode(this.toBinary(), 'hex') as string
205
+ }
206
+
207
+ /**
208
+ * @method toBinary
209
+ * Serializes the script to a binary array.
210
+ * @returns The script in binary array format.
211
+ */
212
+ toBinary (): number[] {
213
+ const writer = new Writer()
214
+
215
+ for (let i = 0; i < this.chunks.length; i++) {
216
+ const chunk = this.chunks[i]
217
+ const op = chunk.op
218
+ writer.writeUInt8(op)
219
+ if (chunk.data) {
220
+ if (op < OP.OP_PUSHDATA1) {
221
+ writer.write(chunk.data)
222
+ } else if (op === OP.OP_PUSHDATA1) {
223
+ writer.writeUInt8(chunk.data.length)
224
+ writer.write(chunk.data)
225
+ } else if (op === OP.OP_PUSHDATA2) {
226
+ writer.writeUInt16LE(chunk.data.length)
227
+ writer.write(chunk.data)
228
+ } else if (op === OP.OP_PUSHDATA4) {
229
+ writer.writeUInt32LE(chunk.data.length)
230
+ writer.write(chunk.data)
231
+ }
232
+ }
233
+ }
234
+
235
+ return writer.toArray()
236
+ }
237
+
238
+ /**
239
+ * @method writeScript
240
+ * Appends another script to this script.
241
+ * @param script - The script to append.
242
+ * @returns This script instance for chaining.
243
+ */
244
+ writeScript (script: Script): Script {
245
+ this.chunks = this.chunks.concat(script.chunks)
246
+ return this
247
+ }
248
+
249
+ /**
250
+ * @method writeOpCode
251
+ * Appends an opcode to the script.
252
+ * @param op - The opcode to append.
253
+ * @returns This script instance for chaining.
254
+ */
255
+ writeOpCode (op: number): Script {
256
+ this.chunks.push({ op })
257
+ return this
258
+ }
259
+
260
+ /**
261
+ * @method setChunkOpCode
262
+ * Sets the opcode of a specific chunk in the script.
263
+ * @param i - The index of the chunk.
264
+ * @param op - The opcode to set.
265
+ * @returns This script instance for chaining.
266
+ */
267
+ setChunkOpCode (i: number, op: number): Script {
268
+ this.chunks[i] = { op }
269
+ return this
270
+ }
271
+
272
+ /**
273
+ * @method writeBn
274
+ * Appends a BigNumber to the script as an opcode.
275
+ * @param bn - The BigNumber to append.
276
+ * @returns This script instance for chaining.
277
+ */
278
+ writeBn (bn: BigNumber): Script {
279
+ if (bn.cmpn(0) === OP.OP_0) {
280
+ this.chunks.push({
281
+ op: OP.OP_0
282
+ })
283
+ } else if (bn.cmpn(-1) === 0) {
284
+ this.chunks.push({
285
+ op: OP.OP_1NEGATE
286
+ })
287
+ } else if (bn.cmpn(1) >= 0 && bn.cmpn(16) <= 0) {
288
+ // see OP_1 - OP_16
289
+ this.chunks.push({
290
+ op: bn.toNumber() + OP.OP_1 - 1
291
+ })
292
+ } else {
293
+ const buf = bn.toSm('little')
294
+ this.writeBin(buf)
295
+ }
296
+ return this
297
+ }
298
+
299
+ /**
300
+ * @method writeBin
301
+ * Appends binary data to the script, determining the appropriate opcode based on length.
302
+ * @param bin - The binary data to append.
303
+ * @returns This script instance for chaining.
304
+ * @throws {Error} Throws an error if the data is too large to be pushed.
305
+ */
306
+ writeBin (bin: number[]): Script {
307
+ let op
308
+ if (bin.length > 0 && bin.length < OP.OP_PUSHDATA1) {
309
+ op = bin.length
310
+ } else if (bin.length === 0) {
311
+ op = OP.OP_0
312
+ } else if (bin.length < Math.pow(2, 8)) {
313
+ op = OP.OP_PUSHDATA1
314
+ } else if (bin.length < Math.pow(2, 16)) {
315
+ op = OP.OP_PUSHDATA2
316
+ } else if (bin.length < Math.pow(2, 32)) {
317
+ op = OP.OP_PUSHDATA4
318
+ } else {
319
+ throw new Error("You can't push that much data")
320
+ }
321
+ this.chunks.push({
322
+ data: bin,
323
+ op
324
+ })
325
+ return this
326
+ }
327
+
328
+ /**
329
+ * @method writeNumber
330
+ * Appends a number to the script.
331
+ * @param num - The number to append.
332
+ * @returns This script instance for chaining.
333
+ */
334
+ writeNumber (num: number): Script {
335
+ this.writeBn(new BigNumber(num))
336
+ return this
337
+ }
338
+
339
+ /**
340
+ * @method removeCodeseparators
341
+ * Removes all OP_CODESEPARATOR opcodes from the script.
342
+ * @returns This script instance for chaining.
343
+ */
344
+ removeCodeseparators (): Script {
345
+ const chunks = []
346
+ for (let i = 0; i < this.chunks.length; i++) {
347
+ if (this.chunks[i].op !== OP.OP_CODESEPARATOR) {
348
+ chunks.push(this.chunks[i])
349
+ }
350
+ }
351
+ this.chunks = chunks
352
+ return this
353
+ }
354
+
355
+ /**
356
+ * Deletes the given item wherever it appears in the current script.
357
+ *
358
+ * @param script - The script containing the item to delete from the current script.
359
+ *
360
+ * @returns This script instance for chaining.
361
+ */
362
+ findAndDelete (script: Script): Script {
363
+ const buf = script.toHex()
364
+ for (let i = 0; i < this.chunks.length; i++) {
365
+ const script2 = new Script([this.chunks[i]])
366
+ const buf2 = script2.toHex()
367
+ if (buf === buf2) {
368
+ this.chunks.splice(i, 1)
369
+ }
370
+ }
371
+ return this
372
+ }
373
+
374
+ /**
375
+ * @method isPushOnly
376
+ * Checks if the script contains only push data operations.
377
+ * @returns True if the script is push-only, otherwise false.
378
+ */
379
+ isPushOnly (): boolean {
380
+ for (let i = 0; i < this.chunks.length; i++) {
381
+ const chunk = this.chunks[i]
382
+ const opCodeNum = chunk.op
383
+ if (opCodeNum > OP.OP_16) {
384
+ return false
385
+ }
386
+ }
387
+ return true
388
+ }
389
+
390
+ /**
391
+ * @method isLockingScript
392
+ * Determines if the script is a locking script.
393
+ * @returns True if the script is a locking script, otherwise false.
394
+ */
395
+ isLockingScript (): boolean {
396
+ throw new Error('Not implemented')
397
+ }
398
+
399
+ /**
400
+ * @method isUnlockingScript
401
+ * Determines if the script is an unlocking script.
402
+ * @returns True if the script is an unlocking script, otherwise false.
403
+ */
404
+ isUnlockingScript (): boolean {
405
+ throw new Error('Not implemented')
406
+ }
407
+
408
+ /**
409
+ * @private
410
+ * @method _chunkToString
411
+ * Converts a script chunk to its string representation.
412
+ * @param chunk - The script chunk.
413
+ * @returns The string representation of the chunk.
414
+ */
415
+ private _chunkToString (chunk: ScriptChunk): string {
416
+ const op = chunk.op
417
+ let str = ''
418
+ if (typeof chunk.data === 'undefined') {
419
+ const val = (OP[op] as string)
420
+ str = `${str} ${val}`
421
+ } else {
422
+ str = `${str} ${toHex(chunk.data)}`
423
+ }
424
+ return str
425
+ }
426
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * A representation of a chunk of a script, which includes an opcode. For push operations, the associated data to push onto the stack is also included.
3
+ */
4
+ export default interface ScriptChunk {
5
+ op: number
6
+ data?: number[]
7
+ }
@@ -0,0 +1,36 @@
1
+ import LockingScript from './LockingScript.js'
2
+ import UnlockingScript from './UnlockingScript.js'
3
+ import Transaction from '../transaction/Transaction.js'
4
+
5
+ /**
6
+ * @interface
7
+ *
8
+ * This interface defines the structure for script templates used in Bitcoin transactions.
9
+ * It provides methods for creating locking scripts, unlocking scripts, and estimating the length of unlocking scripts.
10
+ */
11
+ export default interface ScriptTemplate {
12
+ /**
13
+ * Creates a locking script with the given parameters.
14
+ *
15
+ * @param {...any} params - The parameters required to create the locking script.
16
+ * @returns {LockingScript} - An instance of LockingScript.
17
+ */
18
+ lock: (...params: any) => LockingScript
19
+
20
+ /**
21
+ * Creates a function that generates an unlocking script along with its signature and length estimation.
22
+ *
23
+ * This method returns an object containing two functions:
24
+ * 1. `sign` - A function that, when called with a transaction and an input index, returns an UnlockingScript instance.
25
+ * 2. `estimateLength` - A function that returns the estimated length of the unlocking script in bytes.
26
+ *
27
+ * @param {...any} params - The parameters required to create the unlocking script.
28
+ * @returns {Object} - An object containing the `sign` and `estimateLength` functions.
29
+ */
30
+ unlock: (...params: any) =>
31
+ {
32
+ sign: (tx: Transaction, inputIndex: number) =>
33
+ Promise<UnlockingScript>
34
+ estimateLength: (tx: Transaction, inputIndex: number) => Promise<number>
35
+ }
36
+ }