@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,348 @@
1
+ import { fromBase58Check, toBase58Check, Writer, Reader, toArray, toHex } from '../primitives/utils.js'
2
+ import * as Hash from '../primitives/Hash.js'
3
+ import Curve from '../primitives/Curve.js'
4
+ import PrivateKey from '../primitives/PrivateKey.js'
5
+ import PublicKey from '../primitives/PublicKey.js'
6
+ import Random from '../primitives/Random.js'
7
+ import BigNumber from '../primitives/BigNumber.js'
8
+
9
+ /**
10
+ * @deprecated
11
+ * The HD class implements the Bitcoin Improvement Proposal 32 (BIP32) hierarchical deterministic wallets.
12
+ * It allows the generation of child keys from a master key, ensuring a tree-like structure of keys and addresses.
13
+ * This class is deprecated due to the introduction of BRC-42, which offers an enhanced key derivation scheme.
14
+ * BRC-42 uses invoice numbers for key derivation, improving privacy and scalability compared to BIP32.
15
+ *
16
+ * @class HD
17
+ * @deprecated Replaced by BRC-42 which uses invoice numbers and supports private derivation.
18
+ */
19
+ export default class HD {
20
+ versionBytesNum: number
21
+ depth: number
22
+ parentFingerPrint: number[]
23
+ childIndex: number
24
+ chainCode: number[]
25
+ privKey: PrivateKey
26
+ pubKey: PublicKey
27
+ constants = {
28
+ pubKey: 0x0488b21e,
29
+ privKey: 0x0488ade4
30
+ }
31
+
32
+ /**
33
+ * Constructor for the BIP32 HD wallet.
34
+ * Initializes an HD wallet with optional parameters for version bytes, depth, parent fingerprint, child index, chain code, private key, and public key.
35
+ * @param versionBytesNum - Version bytes number for the wallet.
36
+ * @param depth - Depth of the key in the hierarchy.
37
+ * @param parentFingerPrint - Fingerprint of the parent key.
38
+ * @param childIndex - Index of the child key.
39
+ * @param chainCode - Chain code for key derivation.
40
+ * @param privKey - Private key of the wallet.
41
+ * @param pubKey - Public key of the wallet.
42
+ */
43
+ constructor(
44
+ versionBytesNum?: number,
45
+ depth?: number,
46
+ parentFingerPrint?: number[],
47
+ childIndex?: number,
48
+ chainCode?: number[],
49
+ privKey?: PrivateKey,
50
+ pubKey?: PublicKey
51
+ ) {
52
+ this.versionBytesNum = versionBytesNum
53
+ this.depth = depth
54
+ this.parentFingerPrint = parentFingerPrint
55
+ this.childIndex = childIndex
56
+ this.chainCode = chainCode
57
+ this.privKey = privKey
58
+ this.pubKey = pubKey
59
+ }
60
+
61
+ /**
62
+ * Generates a new HD wallet with random keys.
63
+ * This method creates a root HD wallet with randomly generated private and public keys.
64
+ * @returns {HD} The current HD instance with generated keys.
65
+ */
66
+ public fromRandom(): this {
67
+ this.versionBytesNum = this.constants.privKey
68
+ this.depth = 0x00
69
+ this.parentFingerPrint = [0, 0, 0, 0]
70
+ this.childIndex = 0
71
+ this.chainCode = Random(32)
72
+ this.privKey = PrivateKey.fromRandom()
73
+ this.pubKey = this.privKey.toPublicKey()
74
+ return this
75
+ }
76
+
77
+ /**
78
+ * Generates a new HD wallet with random keys.
79
+ * This method creates a root HD wallet with randomly generated private and public keys.
80
+ * @returns {HD} A new HD instance with generated keys.
81
+ * @static
82
+ */
83
+ public static fromRandom(): HD {
84
+ return new this().fromRandom()
85
+ }
86
+
87
+ /**
88
+ * Initializes the HD wallet from a given base58 encoded string.
89
+ * This method decodes a provided string to set up the HD wallet's properties.
90
+ * @param str - A base58 encoded string representing the wallet.
91
+ * @returns {HD} The current instance with properties set from the string.
92
+ */
93
+ public fromString(str: string): this {
94
+ const decoded = fromBase58Check(str)
95
+ return this.fromBinary([...decoded.prefix, ...decoded.data] as number[])
96
+ }
97
+
98
+ /**
99
+ * Converts the HD wallet to a base58 encoded string.
100
+ * This method provides a string representation of the HD wallet's current state.
101
+ * @returns {string} A base58 encoded string of the HD wallet.
102
+ */
103
+ public toString(): string {
104
+ const bin = this.toBinary()
105
+ return toBase58Check(bin, [])
106
+ }
107
+
108
+ /**
109
+ * Initializes the HD wallet from a seed.
110
+ * This method generates keys and other properties from a given seed, conforming to the BIP32 specification.
111
+ * @param bytes - An array of bytes representing the seed.
112
+ * @returns {HD} The current instance with properties set from the seed.
113
+ */
114
+ public fromSeed(bytes: number[]): this {
115
+ if (bytes.length < 128 / 8) {
116
+ throw new Error('Need more than 128 bits of entropy')
117
+ }
118
+ if (bytes.length > 512 / 8) {
119
+ throw new Error('More than 512 bits of entropy is nonstandard')
120
+ }
121
+ const hash: number[] = Hash.sha512hmac(toArray('Bitcoin seed', 'utf8'), bytes) as number[]
122
+
123
+ this.depth = 0x00
124
+ this.parentFingerPrint = [0, 0, 0, 0]
125
+ this.childIndex = 0
126
+ this.chainCode = hash.slice(32, 64)
127
+ this.versionBytesNum = this.constants.privKey
128
+ this.privKey = new PrivateKey(hash.slice(0, 32))
129
+ this.pubKey = this.privKey.toPublicKey()
130
+
131
+ return this
132
+ }
133
+
134
+ /**
135
+ * Initializes the HD wallet from a seed.
136
+ * This method generates keys and other properties from a given seed, conforming to the BIP32 specification.
137
+ * @param bytes - An array of bytes representing the seed.
138
+ * @returns {HD} The current instance with properties set from the seed.
139
+ */
140
+ public static fromSeed(bytes: number[]): HD {
141
+ return new this().fromSeed(bytes)
142
+ }
143
+
144
+ /**
145
+ * Initializes the HD wallet from a binary buffer.
146
+ * Parses a binary buffer to set up the wallet's properties.
147
+ * @param buf - A buffer containing the wallet data.
148
+ * @returns {HD} The current instance with properties set from the buffer.
149
+ */
150
+ public fromBinary(buf: number[]): this {
151
+ // Both pub and private extended keys are 78 buf
152
+ if (buf.length !== 78) {
153
+ throw new Error('incorrect bip32 data length')
154
+ }
155
+ const reader = new Reader(buf)
156
+
157
+ this.versionBytesNum = reader.readUInt32BE()
158
+ this.depth = reader.readUInt8()
159
+ this.parentFingerPrint = reader.read(4)
160
+ this.childIndex = reader.readUInt32BE()
161
+ this.chainCode = reader.read(32)
162
+ const keyBytes = reader.read(33)
163
+
164
+ const isPrivate = this.versionBytesNum === this.constants.privKey
165
+ const isPublic = this.versionBytesNum === this.constants.pubKey
166
+
167
+ if (isPrivate && keyBytes[0] === 0) {
168
+ this.privKey = new PrivateKey(keyBytes.slice(1, 33))
169
+ this.pubKey = this.privKey.toPublicKey()
170
+ } else if (isPublic && (keyBytes[0] === 0x02 || keyBytes[0] === 0x03)) {
171
+ this.pubKey = PublicKey.fromString(toHex(keyBytes))
172
+ } else {
173
+ throw new Error('Invalid key')
174
+ }
175
+
176
+ return this
177
+ }
178
+
179
+ /**
180
+ * Derives a child HD wallet based on a given path.
181
+ * The path specifies the hierarchy of the child key to be derived.
182
+ * @param path - A string representing the derivation path (e.g., 'm/0'/1).
183
+ * @returns {HD} A new HD instance representing the derived child wallet.
184
+ */
185
+ public derive(path: string): HD {
186
+ if (path === 'm') {
187
+ return this
188
+ }
189
+
190
+ const e = path.split('/')
191
+
192
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
193
+ let bip32: HD = this
194
+ for (const i in e) {
195
+ const c = e[i]
196
+
197
+ if (i === '0') {
198
+ if (c !== 'm') {
199
+ throw new Error('invalid path')
200
+ }
201
+ continue
202
+ }
203
+
204
+ if (parseInt(c.replace("'", ''), 10).toString() !== c.replace("'", '')) {
205
+ throw new Error('invalid path')
206
+ }
207
+
208
+ const usePrivate = c.length > 1 && c[c.length - 1] === "'"
209
+ let childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c, 10) & 0x7fffffff
210
+
211
+ if (usePrivate) {
212
+ childIndex += 0x80000000
213
+ }
214
+
215
+ bip32 = bip32.deriveChild(childIndex)
216
+ }
217
+
218
+ return bip32
219
+ }
220
+
221
+ /**
222
+ * Derives a child HD wallet from the current wallet based on an index.
223
+ * This method generates either a private or public child key depending on the current wallet's state.
224
+ * @param i - The index of the child key to derive.
225
+ * @returns {HD} A new HD instance representing the derived child wallet.
226
+ */
227
+ public deriveChild(i: number): HD {
228
+ if (typeof i !== 'number') {
229
+ throw new Error('i must be a number')
230
+ }
231
+
232
+ const ibc: number[] = []
233
+ ibc.push((i >> 24) & 0xff)
234
+ ibc.push((i >> 16) & 0xff)
235
+ ibc.push((i >> 8) & 0xff)
236
+ ibc.push(i & 0xff)
237
+ const ib = [...ibc]
238
+
239
+ const usePrivate = (i & 0x80000000) !== 0
240
+
241
+ const isPrivate = this.versionBytesNum === this.constants.privKey
242
+
243
+ if (usePrivate && (!this.privKey || !isPrivate)) {
244
+ throw new Error('Cannot do private key derivation without private key')
245
+ }
246
+
247
+ let ret = null
248
+ if (this.privKey) {
249
+ let data = null
250
+
251
+ if (usePrivate) {
252
+ data = [0, ...this.privKey.toArray('be', 32), ...ib]
253
+ } else {
254
+ data = [...this.pubKey.encode(true) as number[], ...ib]
255
+ }
256
+
257
+ const hash = Hash.sha512hmac(this.chainCode, data)
258
+ const il = new BigNumber(hash.slice(0, 32))
259
+ const ir = hash.slice(32, 64)
260
+
261
+ // ki = IL + kpar (mod n).
262
+ const k = il.add(this.privKey).mod(new Curve().n)
263
+
264
+ ret = new HD()
265
+ ret.chainCode = ir
266
+
267
+ ret.privKey = new PrivateKey(k.toArray())
268
+ ret.pubKey = ret.privKey.toPublicKey()
269
+ } else {
270
+ const data = [...this.pubKey.encode(true) as number[], ...ib]
271
+ const hash = Hash.sha512hmac(this.chainCode, data)
272
+ const il = new BigNumber(hash.slice(0, 32))
273
+ const ir = hash.slice(32, 64)
274
+
275
+ // Ki = (IL + kpar)*G = IL*G + Kpar
276
+ const ilG = new Curve().g.mul(il)
277
+ const Kpar = this.pubKey
278
+ const Ki = ilG.add(Kpar)
279
+ const newpub = new PublicKey(Ki.x, Ki.y)
280
+
281
+ ret = new HD()
282
+ ret.chainCode = ir
283
+
284
+ ret.pubKey = newpub
285
+ }
286
+
287
+ ret.childIndex = i
288
+ const pubKeyhash = Hash.hash160(this.pubKey.encode(true))
289
+ ret.parentFingerPrint = pubKeyhash.slice(0, 4)
290
+ ret.versionBytesNum = this.versionBytesNum
291
+ ret.depth = this.depth + 1
292
+
293
+ return ret
294
+ }
295
+
296
+ /**
297
+ * Converts the current HD wallet to a public-only wallet.
298
+ * This method strips away the private key information, leaving only the public part.
299
+ * @returns {HD} A new HD instance representing the public-only wallet.
300
+ */
301
+ public toPublic(): HD {
302
+ const bip32 = new HD(this.versionBytesNum, this.depth, this.parentFingerPrint, this.childIndex, this.chainCode, this.privKey, this.pubKey)
303
+ bip32.versionBytesNum = this.constants.pubKey
304
+ bip32.privKey = undefined
305
+ return bip32
306
+ }
307
+
308
+ /**
309
+ * Converts the HD wallet into a binary representation.
310
+ * This method serializes the wallet's properties into a binary format.
311
+ * @returns {number[]} An array of numbers representing the binary data of the wallet.
312
+ */
313
+ public toBinary(): number[] {
314
+ const isPrivate = this.versionBytesNum === this.constants.privKey
315
+ const isPublic = this.versionBytesNum === this.constants.pubKey
316
+ if (isPrivate) {
317
+ return new Writer()
318
+ .writeUInt32BE(this.versionBytesNum)
319
+ .writeUInt8(this.depth)
320
+ .write(this.parentFingerPrint)
321
+ .writeUInt32BE(this.childIndex)
322
+ .write(this.chainCode)
323
+ .writeUInt8(0)
324
+ .write(this.privKey.toArray('be', 32))
325
+ .toArray()
326
+ } else if (isPublic) {
327
+ return new Writer()
328
+ .writeUInt32BE(this.versionBytesNum)
329
+ .writeUInt8(this.depth)
330
+ .write(this.parentFingerPrint)
331
+ .writeUInt32BE(this.childIndex)
332
+ .write(this.chainCode)
333
+ .write(this.pubKey.encode(true) as number[])
334
+ .toArray()
335
+ } else {
336
+ throw new Error('bip32: invalid versionBytesNum byte')
337
+ }
338
+ }
339
+
340
+ /**
341
+ * Checks if the HD wallet contains a private key.
342
+ * This method determines whether the wallet is a private key wallet or a public key only wallet.
343
+ * @returns {boolean} A boolean value indicating whether the wallet has a private key (true) or not (false).
344
+ */
345
+ public isPrivate(): boolean {
346
+ return this.versionBytesNum === this.constants.privKey
347
+ }
348
+ }
@@ -0,0 +1,295 @@
1
+ import { wordList } from './bip-39-wordlist-en.js'
2
+ import { encode, toArray, Reader, Writer } from '../primitives/utils.js'
3
+ import * as Hash from '../primitives/Hash.js'
4
+ import Random from '../primitives/Random.js'
5
+
6
+ /**
7
+ * @class Mnemonic
8
+ *
9
+ * @description
10
+ * Class representing Mnemonic functionality.
11
+ * This class provides methods for generating, converting, and validating mnemonic phrases
12
+ * according to the BIP39 standard. It supports creating mnemonics from random entropy,
13
+ * converting mnemonics to seeds, and validating mnemonic phrases.
14
+ */
15
+ export default class Mnemonic {
16
+ public mnemonic: string
17
+ public seed: number[]
18
+ public Wordlist: { value: string[]; space: string }
19
+
20
+ /**
21
+ * Constructs a Mnemonic object.
22
+ * @param {string} [mnemonic] - An optional mnemonic phrase.
23
+ * @param {number[]} [seed] - An optional seed derived from the mnemonic.
24
+ * @param {object} [wordlist=wordList] - An object containing a list of words and space character used in the mnemonic.
25
+ */
26
+ constructor(mnemonic?: string, seed?: number[], wordlist = wordList) {
27
+ this.mnemonic = mnemonic
28
+ this.seed = seed
29
+ this.Wordlist = wordlist
30
+ }
31
+
32
+ /**
33
+ * Converts the mnemonic and seed into a binary representation.
34
+ * @returns {number[]} The binary representation of the mnemonic and seed.
35
+ */
36
+ public toBinary(): number[] {
37
+ const bw = new Writer()
38
+ if (this.mnemonic) {
39
+ const buf = toArray(this.mnemonic, 'utf8')
40
+ bw.writeVarIntNum(buf.length)
41
+ bw.write(buf)
42
+ } else {
43
+ bw.writeVarIntNum(0)
44
+ }
45
+ if (this.seed) {
46
+ bw.writeVarIntNum(this.seed.length)
47
+ bw.write(this.seed)
48
+ } else {
49
+ bw.writeVarIntNum(0)
50
+ }
51
+ return bw.toArray()
52
+ }
53
+
54
+ /**
55
+ * Loads a mnemonic and seed from a binary representation.
56
+ * @param {number[]} bin - The binary representation of a mnemonic and seed.
57
+ * @returns {this} The Mnemonic instance with loaded mnemonic and seed.
58
+ */
59
+ public fromBinary(bin: number[]): this {
60
+ const br = new Reader(bin)
61
+ const mnemoniclen = br.readVarIntNum()
62
+ if (mnemoniclen > 0) {
63
+ this.mnemonic = encode(br.read(mnemoniclen), 'utf8') as string
64
+ }
65
+ const seedlen = br.readVarIntNum()
66
+ if (seedlen > 0) {
67
+ this.seed = br.read(seedlen)
68
+ }
69
+ return this
70
+ }
71
+
72
+ /**
73
+ * Generates a random mnemonic from a given bit length.
74
+ * @param {number} [bits=128] - The bit length for the random mnemonic (must be a multiple of 32 and at least 128).
75
+ * @returns {this} The Mnemonic instance with the new random mnemonic.
76
+ * @throws {Error} If the bit length is not a multiple of 32 or is less than 128.
77
+ */
78
+ public fromRandom(bits?: number): this {
79
+ if (!bits) {
80
+ bits = 128
81
+ }
82
+ if (bits % 32 !== 0) {
83
+ throw new Error('bits must be multiple of 32')
84
+ }
85
+ if (bits < 128) {
86
+ throw new Error('bits must be at least 128')
87
+ }
88
+ const buf = Random(bits / 8)
89
+ this.entropy2Mnemonic(buf)
90
+ this.mnemonic2Seed()
91
+ return this
92
+ }
93
+
94
+ /**
95
+ * Static method to generate a Mnemonic instance with a random mnemonic.
96
+ * @param {number} [bits=128] - The bit length for the random mnemonic.
97
+ * @returns {Mnemonic} A new Mnemonic instance.
98
+ */
99
+ public static fromRandom(bits?: number): Mnemonic {
100
+ return new this().fromRandom(bits)
101
+ }
102
+
103
+ /**
104
+ * Converts given entropy into a mnemonic phrase.
105
+ * This method is used to generate a mnemonic from a specific entropy source.
106
+ * @param {number[]} buf - The entropy buffer, must be at least 128 bits.
107
+ * @returns {this} The Mnemonic instance with the mnemonic set from the given entropy.
108
+ * @throws {Error} If the entropy is less than 128 bits.
109
+ */
110
+ public fromEntropy(buf: number[]): this {
111
+ this.entropy2Mnemonic(buf)
112
+ return this
113
+ }
114
+
115
+ /**
116
+ * Static method to create a Mnemonic instance from a given entropy.
117
+ * @param {number[]} buf - The entropy buffer.
118
+ * @returns {Mnemonic} A new Mnemonic instance.
119
+ */
120
+ public static fromEntropy(buf: number[]): Mnemonic {
121
+ return new this().fromEntropy(buf)
122
+ }
123
+
124
+ /**
125
+ * Sets the mnemonic for the instance from a string.
126
+ * @param {string} mnemonic - The mnemonic phrase as a string.
127
+ * @returns {this} The Mnemonic instance with the set mnemonic.
128
+ */
129
+ public fromString(mnemonic: string): this {
130
+ this.mnemonic = mnemonic
131
+ return this
132
+ }
133
+
134
+ /**
135
+ * Static method to create a Mnemonic instance from a mnemonic string.
136
+ * @param {string} str - The mnemonic phrase.
137
+ * @returns {Mnemonic} A new Mnemonic instance.
138
+ */
139
+ public static fromString(str: string): Mnemonic {
140
+ return new this().fromString(str)
141
+ }
142
+
143
+ /**
144
+ * Converts the instance's mnemonic to a string representation.
145
+ * @returns {string} The mnemonic phrase as a string.
146
+ */
147
+ public toString(): string {
148
+ return this.mnemonic
149
+ }
150
+
151
+ /**
152
+ * Converts the mnemonic to a seed.
153
+ * The mnemonic must pass the validity check before conversion.
154
+ * @param {string} [passphrase=''] - An optional passphrase for additional security.
155
+ * @returns {number[]} The generated seed.
156
+ * @throws {Error} If the mnemonic is invalid.
157
+ */
158
+ public toSeed(passphrase?: string): number[] {
159
+ this.mnemonic2Seed(passphrase)
160
+ return this.seed
161
+ }
162
+
163
+ /**
164
+ * Converts entropy to a mnemonic phrase.
165
+ * This method takes a buffer of entropy and converts it into a corresponding
166
+ * mnemonic phrase based on the Mnemonic wordlist. The entropy should be at least 128 bits.
167
+ * The method applies a checksum and maps the entropy to words in the wordlist.
168
+ * @param {number[]} buf - The entropy buffer to convert. Must be at least 128 bits.
169
+ * @returns {this} The Mnemonic instance with the mnemonic set from the entropy.
170
+ * @throws {Error} If the entropy is less than 128 bits or if it's not an even multiple of 11 bits.
171
+ */
172
+ public entropy2Mnemonic(buf: number[]): this {
173
+ if (buf.length < 128 / 8) {
174
+ throw new Error('Entropy is less than 128 bits. It must be 128 bits or more.')
175
+ }
176
+
177
+ const hash = Hash.sha256(buf)
178
+ let bin = ''
179
+ const bits = buf.length * 8
180
+ for (let i = 0; i < buf.length; i++) {
181
+ bin = bin + ('00000000' + buf[i].toString(2)).slice(-8)
182
+ }
183
+ let hashbits = hash[0].toString(2)
184
+ hashbits = ('00000000' + hashbits).slice(-8).slice(0, bits / 32)
185
+ bin = bin + hashbits
186
+
187
+ if (bin.length % 11 !== 0) {
188
+ throw new Error('internal error - entropy not an even multiple of 11 bits - ' + bin.length)
189
+ }
190
+
191
+ let mnemonic = ''
192
+ for (let i = 0; i < bin.length / 11; i++) {
193
+ if (mnemonic !== '') {
194
+ mnemonic = mnemonic + this.Wordlist.space
195
+ }
196
+ const wi = parseInt(bin.slice(i * 11, (i + 1) * 11), 2)
197
+ mnemonic = mnemonic + this.Wordlist.value[wi]
198
+ }
199
+
200
+ this.mnemonic = mnemonic
201
+ return this
202
+ }
203
+
204
+ /**
205
+ * Validates the mnemonic phrase.
206
+ * Checks for correct length, absence of invalid words, and proper checksum.
207
+ * @returns {boolean} True if the mnemonic is valid, false otherwise.
208
+ * @throws {Error} If the mnemonic is not an even multiple of 11 bits.
209
+ */
210
+ public check(): boolean {
211
+ const mnemonic = this.mnemonic
212
+
213
+ // confirm no invalid words
214
+ const words = mnemonic.split(this.Wordlist.space)
215
+ let bin = ''
216
+ for (let i = 0; i < words.length; i++) {
217
+ const ind = this.Wordlist.value.indexOf(words[i])
218
+ if (ind < 0) {
219
+ return false
220
+ }
221
+ bin = bin + ('00000000000' + ind.toString(2)).slice(-11)
222
+ }
223
+
224
+ if (bin.length % 11 !== 0) {
225
+ throw new Error('internal error - entropy not an even multiple of 11 bits - ' + bin.length)
226
+ }
227
+
228
+ // confirm checksum
229
+ const cs = bin.length / 33
230
+ const hashBits = bin.slice(-cs)
231
+ const nonhashBits = bin.slice(0, bin.length - cs)
232
+ const buf = []
233
+ for (let i = 0; i < nonhashBits.length / 8; i++) {
234
+ buf.push(parseInt(bin.slice(i * 8, (i + 1) * 8), 2))
235
+ }
236
+ const hash = Hash.sha256(buf.slice(0, nonhashBits.length / 8))
237
+ let expectedHashBits = hash[0].toString(2)
238
+ expectedHashBits = ('00000000' + expectedHashBits).slice(-8).slice(0, cs)
239
+
240
+ return expectedHashBits === hashBits
241
+ }
242
+
243
+ /**
244
+ * Converts a mnemonic to a seed.
245
+ * This method takes the instance's mnemonic phrase, combines it with a passphrase (if provided),
246
+ * and uses PBKDF2 to generate a seed. It also validates the mnemonic before conversion.
247
+ * This seed can then be used for generating deterministic keys.
248
+ * @param {string} [passphrase=''] - An optional passphrase for added security.
249
+ * @returns {this} The Mnemonic instance with the seed generated from the mnemonic.
250
+ * @throws {Error} If the mnemonic does not pass validation or if the passphrase is not a string.
251
+ */
252
+ public mnemonic2Seed(passphrase = ''): this {
253
+ let mnemonic = this.mnemonic
254
+ if (!this.check()) {
255
+ throw new Error(
256
+ 'Mnemonic does not pass the check - was the mnemonic typed incorrectly? Are there extra spaces?'
257
+ )
258
+ }
259
+ if (typeof passphrase !== 'string') {
260
+ throw new Error('passphrase must be a string or undefined')
261
+ }
262
+ mnemonic = mnemonic.normalize('NFKD')
263
+ passphrase = passphrase.normalize('NFKD')
264
+ const mbuf = toArray(mnemonic, 'utf8')
265
+ const pbuf = [...toArray('mnemonic', 'utf8'), ...toArray(passphrase, 'utf8')]
266
+ this.seed = Hash.pbkdf2(mbuf, pbuf, 2048, 64, 'sha512')
267
+ return this
268
+ }
269
+
270
+ /**
271
+ * Determines the validity of a given passphrase with the mnemonic.
272
+ * This method is useful for checking if a passphrase matches with the mnemonic.
273
+ * @param {string} [passphrase=''] - The passphrase to validate.
274
+ * @returns {boolean} True if the mnemonic and passphrase combination is valid, false otherwise.
275
+ */
276
+ public isValid(passphrase = ''): boolean {
277
+ let isValid
278
+ try {
279
+ isValid = !!this.mnemonic2Seed(passphrase)
280
+ } catch (err) {
281
+ isValid = false
282
+ }
283
+ return isValid
284
+ }
285
+
286
+ /**
287
+ * Static method to check the validity of a given mnemonic and passphrase combination.
288
+ * @param {string} mnemonic - The mnemonic phrase.
289
+ * @param {string} [passphrase=''] - The passphrase to validate.
290
+ * @returns {boolean} True if the combination is valid, false otherwise.
291
+ */
292
+ public static isValid(mnemonic: string, passphrase = ''): boolean {
293
+ return new Mnemonic(mnemonic).isValid(passphrase)
294
+ }
295
+ }
@@ -0,0 +1,38 @@
1
+ import { sign, verify, magicHash } from '../../../dist/cjs/src/compat/BSM'
2
+ import { toArray } from '../../../dist/cjs/src/primitives/utils'
3
+ import PrivateKey from '../../../dist/cjs/src/primitives/PrivateKey'
4
+ import PublicKey from '../../../dist/cjs/src/primitives/PublicKey'
5
+ import Signature from '../../../dist/cjs/src/primitives/Signature'
6
+
7
+ describe('BSM', () => {
8
+ describe('magicHash', () => {
9
+ it('should return a hash', () => {
10
+ const buf = toArray('001122', 'hex')
11
+ const hashBuf = magicHash(buf)
12
+ expect(hashBuf.length).toEqual(32)
13
+ })
14
+ })
15
+ describe('sign', () => {
16
+ const messageBuf = toArray('this is my message', 'utf8')
17
+ const privateKey = new PrivateKey(42)
18
+ it('should return a signature', () => {
19
+ const sig = sign(messageBuf, privateKey).toDER()
20
+ expect(sig.length).toEqual(70)
21
+ })
22
+ })
23
+ describe('verify', () => {
24
+ const messageBuf = toArray('this is my message', 'utf8')
25
+ const privateKey = new PrivateKey(42)
26
+
27
+ it('should verify a signed message', () => {
28
+ const sig = sign(messageBuf, privateKey)
29
+ expect(verify(messageBuf, sig, privateKey.toPublicKey())).toEqual(true)
30
+ })
31
+ it('Should verify a signed message in base64', () => {
32
+ const message = toArray("Texas", 'utf8')
33
+ const signature = Signature.fromDER("IAV89EkfHSzAIA8cEWbbKHUYzJqcShkpWaXGJ5+mf4+YIlf3XNlr0bj9X60sNe1A7+x9qyk+zmXropMDY4370n8=", 'base64')
34
+ const publicKey = PublicKey.fromString("03d4d1a6c5d8c03b0e671bc1891b69afaecb40c0686188fe9019f93581b43e8334")
35
+ expect(verify(message, signature, publicKey)).toBe(true)
36
+ })
37
+ })
38
+ })