ox 1.0.5 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (337) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/dist/core/Base64.d.ts +10 -21
  3. package/dist/core/Base64.d.ts.map +1 -1
  4. package/dist/core/Base64.js +8 -127
  5. package/dist/core/Base64.js.map +1 -1
  6. package/dist/core/BinaryStateTree.d.ts.map +1 -1
  7. package/dist/core/BinaryStateTree.js +2 -2
  8. package/dist/core/BinaryStateTree.js.map +1 -1
  9. package/dist/core/Bls.d.ts.map +1 -1
  10. package/dist/core/Bls.js +19 -44
  11. package/dist/core/Bls.js.map +1 -1
  12. package/dist/core/Ed25519.d.ts.map +1 -1
  13. package/dist/core/Ed25519.js +7 -6
  14. package/dist/core/Ed25519.js.map +1 -1
  15. package/dist/core/Engine.d.ts +156 -0
  16. package/dist/core/Engine.d.ts.map +1 -0
  17. package/dist/core/Engine.js +210 -0
  18. package/dist/core/Engine.js.map +1 -0
  19. package/dist/core/Hash.d.ts +4 -4
  20. package/dist/core/Hash.d.ts.map +1 -1
  21. package/dist/core/Hash.js +9 -12
  22. package/dist/core/Hash.js.map +1 -1
  23. package/dist/core/Keystore.d.ts.map +1 -1
  24. package/dist/core/Keystore.js +15 -10
  25. package/dist/core/Keystore.js.map +1 -1
  26. package/dist/core/Mnemonic.d.ts.map +1 -1
  27. package/dist/core/Mnemonic.js +3 -2
  28. package/dist/core/Mnemonic.js.map +1 -1
  29. package/dist/core/P256.d.ts.map +1 -1
  30. package/dist/core/P256.js +8 -9
  31. package/dist/core/P256.js.map +1 -1
  32. package/dist/core/Secp256k1.d.ts.map +1 -1
  33. package/dist/core/Secp256k1.js +8 -9
  34. package/dist/core/Secp256k1.js.map +1 -1
  35. package/dist/core/WebCryptoP256.d.ts.map +1 -1
  36. package/dist/core/WebCryptoP256.js +2 -2
  37. package/dist/core/WebCryptoP256.js.map +1 -1
  38. package/dist/core/X25519.d.ts.map +1 -1
  39. package/dist/core/X25519.js +4 -3
  40. package/dist/core/X25519.js.map +1 -1
  41. package/dist/core/internal/bls.d.ts +12 -0
  42. package/dist/core/internal/bls.d.ts.map +1 -0
  43. package/dist/core/internal/bls.js +79 -0
  44. package/dist/core/internal/bls.js.map +1 -0
  45. package/dist/core/internal/codec/base64.d.ts +84 -0
  46. package/dist/core/internal/codec/base64.d.ts.map +1 -0
  47. package/dist/core/internal/codec/base64.js +177 -0
  48. package/dist/core/internal/codec/base64.js.map +1 -0
  49. package/dist/core/internal/codec/hex.d.ts +49 -4
  50. package/dist/core/internal/codec/hex.d.ts.map +1 -1
  51. package/dist/core/internal/codec/hex.js +109 -35
  52. package/dist/core/internal/codec/hex.js.map +1 -1
  53. package/dist/core/internal/curves.d.ts +17 -0
  54. package/dist/core/internal/curves.d.ts.map +1 -0
  55. package/dist/core/internal/curves.js +17 -0
  56. package/dist/core/internal/curves.js.map +1 -0
  57. package/dist/core/internal/ed25519.d.ts +14 -0
  58. package/dist/core/internal/ed25519.d.ts.map +1 -0
  59. package/dist/core/internal/ed25519.js +29 -0
  60. package/dist/core/internal/ed25519.js.map +1 -0
  61. package/dist/core/internal/engine.d.ts +278 -0
  62. package/dist/core/internal/engine.d.ts.map +1 -0
  63. package/dist/core/internal/engine.js +131 -0
  64. package/dist/core/internal/engine.js.map +1 -0
  65. package/dist/core/internal/hash.d.ts +12 -0
  66. package/dist/core/internal/hash.d.ts.map +1 -0
  67. package/dist/core/internal/hash.js +30 -0
  68. package/dist/core/internal/hash.js.map +1 -0
  69. package/dist/core/internal/keystore.d.ts +14 -0
  70. package/dist/core/internal/keystore.d.ts.map +1 -0
  71. package/dist/core/internal/keystore.js +32 -0
  72. package/dist/core/internal/keystore.js.map +1 -0
  73. package/dist/core/internal/mnemonic.d.ts +4 -0
  74. package/dist/core/internal/mnemonic.d.ts.map +1 -0
  75. package/dist/core/internal/mnemonic.js +14 -0
  76. package/dist/core/internal/mnemonic.js.map +1 -0
  77. package/dist/core/internal/p256.d.ts +14 -0
  78. package/dist/core/internal/p256.d.ts.map +1 -0
  79. package/dist/core/internal/p256.js +36 -0
  80. package/dist/core/internal/p256.js.map +1 -0
  81. package/dist/core/internal/secp256k1.d.ts +14 -0
  82. package/dist/core/internal/secp256k1.d.ts.map +1 -0
  83. package/dist/core/internal/secp256k1.js +36 -0
  84. package/dist/core/internal/secp256k1.js.map +1 -0
  85. package/dist/core/internal/x25519.d.ts +8 -0
  86. package/dist/core/internal/x25519.d.ts.map +1 -0
  87. package/dist/core/internal/x25519.js +20 -0
  88. package/dist/core/internal/x25519.js.map +1 -0
  89. package/dist/index.d.ts +38 -0
  90. package/dist/index.d.ts.map +1 -1
  91. package/dist/index.docs.d.ts +2 -0
  92. package/dist/index.docs.d.ts.map +1 -1
  93. package/dist/index.docs.js +2 -0
  94. package/dist/index.docs.js.map +1 -1
  95. package/dist/index.js +38 -0
  96. package/dist/index.js.map +1 -1
  97. package/dist/node/Ed25519.d.ts +33 -0
  98. package/dist/node/Ed25519.d.ts.map +1 -0
  99. package/dist/node/Ed25519.js +67 -0
  100. package/dist/node/Ed25519.js.map +1 -0
  101. package/dist/node/Engine.d.ts +68 -0
  102. package/dist/node/Engine.d.ts.map +1 -0
  103. package/dist/node/Engine.js +75 -0
  104. package/dist/node/Engine.js.map +1 -0
  105. package/dist/node/Hash.d.ts +37 -0
  106. package/dist/node/Hash.d.ts.map +1 -0
  107. package/dist/node/Hash.js +35 -0
  108. package/dist/node/Hash.js.map +1 -0
  109. package/dist/node/Keystore.d.ts +36 -0
  110. package/dist/node/Keystore.d.ts.map +1 -0
  111. package/dist/node/Keystore.js +72 -0
  112. package/dist/node/Keystore.js.map +1 -0
  113. package/dist/node/Mnemonic.d.ts +35 -0
  114. package/dist/node/Mnemonic.d.ts.map +1 -0
  115. package/dist/node/Mnemonic.js +46 -0
  116. package/dist/node/Mnemonic.js.map +1 -0
  117. package/dist/node/P256.d.ts +32 -0
  118. package/dist/node/P256.d.ts.map +1 -0
  119. package/dist/node/P256.js +34 -0
  120. package/dist/node/P256.js.map +1 -0
  121. package/dist/node/X25519.d.ts +31 -0
  122. package/dist/node/X25519.d.ts.map +1 -0
  123. package/dist/node/X25519.js +71 -0
  124. package/dist/node/X25519.js.map +1 -0
  125. package/dist/node/index.d.ts +126 -0
  126. package/dist/node/index.d.ts.map +1 -0
  127. package/dist/node/index.js +126 -0
  128. package/dist/node/index.js.map +1 -0
  129. package/dist/tempo/VirtualMaster.d.ts +2 -0
  130. package/dist/tempo/VirtualMaster.d.ts.map +1 -1
  131. package/dist/tempo/VirtualMaster.js +19 -5
  132. package/dist/tempo/VirtualMaster.js.map +1 -1
  133. package/dist/tempo/internal/mine.wasm.d.ts +2 -4
  134. package/dist/tempo/internal/mine.wasm.d.ts.map +1 -1
  135. package/dist/tempo/internal/mine.wasm.js +7 -12
  136. package/dist/tempo/internal/mine.wasm.js.map +1 -1
  137. package/dist/tempo/internal/virtualMasterPool.js +4 -1
  138. package/dist/tempo/internal/virtualMasterPool.js.map +1 -1
  139. package/dist/wasm/Ed25519.d.ts +35 -0
  140. package/dist/wasm/Ed25519.d.ts.map +1 -0
  141. package/dist/wasm/Ed25519.js +88 -0
  142. package/dist/wasm/Ed25519.js.map +1 -0
  143. package/dist/wasm/Engine.d.ts +71 -0
  144. package/dist/wasm/Engine.d.ts.map +1 -0
  145. package/dist/wasm/Engine.js +76 -0
  146. package/dist/wasm/Engine.js.map +1 -0
  147. package/dist/wasm/Hash.d.ts +43 -0
  148. package/dist/wasm/Hash.d.ts.map +1 -0
  149. package/dist/wasm/Hash.js +64 -0
  150. package/dist/wasm/Hash.js.map +1 -0
  151. package/dist/wasm/Keystore.d.ts +37 -0
  152. package/dist/wasm/Keystore.d.ts.map +1 -0
  153. package/dist/wasm/Keystore.js +34 -0
  154. package/dist/wasm/Keystore.js.map +1 -0
  155. package/dist/wasm/Mnemonic.d.ts +33 -0
  156. package/dist/wasm/Mnemonic.d.ts.map +1 -0
  157. package/dist/wasm/Mnemonic.js +30 -0
  158. package/dist/wasm/Mnemonic.js.map +1 -0
  159. package/dist/wasm/X25519.d.ts +33 -0
  160. package/dist/wasm/X25519.d.ts.map +1 -0
  161. package/dist/wasm/X25519.js +53 -0
  162. package/dist/wasm/X25519.js.map +1 -0
  163. package/dist/wasm/index.d.ts +115 -0
  164. package/dist/wasm/index.d.ts.map +1 -0
  165. package/dist/wasm/index.js +115 -0
  166. package/dist/wasm/index.js.map +1 -0
  167. package/dist/wasm/internal/blake3.d.ts +11 -0
  168. package/dist/wasm/internal/blake3.d.ts.map +1 -0
  169. package/dist/wasm/internal/blake3.js +31 -0
  170. package/dist/wasm/internal/blake3.js.map +1 -0
  171. package/dist/wasm/internal/blake3.wasm.d.ts +3 -0
  172. package/dist/wasm/internal/blake3.wasm.d.ts.map +1 -0
  173. package/dist/wasm/internal/blake3.wasm.js +10 -0
  174. package/dist/wasm/internal/blake3.wasm.js.map +1 -0
  175. package/dist/wasm/internal/crypto25519.d.ts +16 -0
  176. package/dist/wasm/internal/crypto25519.d.ts.map +1 -0
  177. package/dist/wasm/internal/crypto25519.js +5 -0
  178. package/dist/wasm/internal/crypto25519.js.map +1 -0
  179. package/dist/wasm/internal/crypto25519.wasm.d.ts +3 -0
  180. package/dist/wasm/internal/crypto25519.wasm.d.ts.map +1 -0
  181. package/dist/wasm/internal/crypto25519.wasm.js +10 -0
  182. package/dist/wasm/internal/crypto25519.wasm.js.map +1 -0
  183. package/dist/wasm/internal/ed25519.d.ts +4 -0
  184. package/dist/wasm/internal/ed25519.d.ts.map +1 -0
  185. package/dist/wasm/internal/ed25519.js +21 -0
  186. package/dist/wasm/internal/ed25519.js.map +1 -0
  187. package/dist/wasm/internal/hash.d.ts +9 -0
  188. package/dist/wasm/internal/hash.d.ts.map +1 -0
  189. package/dist/wasm/internal/hash.js +26 -0
  190. package/dist/wasm/internal/hash.js.map +1 -0
  191. package/dist/wasm/internal/hashes.d.ts +12 -0
  192. package/dist/wasm/internal/hashes.d.ts.map +1 -0
  193. package/dist/wasm/internal/hashes.js +8 -0
  194. package/dist/wasm/internal/hashes.js.map +1 -0
  195. package/dist/wasm/internal/hashes.wasm.d.ts +7 -0
  196. package/dist/wasm/internal/hashes.wasm.d.ts.map +1 -0
  197. package/dist/wasm/internal/hashes.wasm.js +14 -0
  198. package/dist/wasm/internal/hashes.wasm.js.map +1 -0
  199. package/dist/wasm/internal/instantiate.d.ts +56 -0
  200. package/dist/wasm/internal/instantiate.d.ts.map +1 -0
  201. package/dist/wasm/internal/instantiate.js +92 -0
  202. package/dist/wasm/internal/instantiate.js.map +1 -0
  203. package/dist/wasm/internal/mnemonic.d.ts +4 -0
  204. package/dist/wasm/internal/mnemonic.d.ts.map +1 -0
  205. package/dist/wasm/internal/mnemonic.js +45 -0
  206. package/dist/wasm/internal/mnemonic.js.map +1 -0
  207. package/dist/wasm/internal/pbkdf2.d.ts +12 -0
  208. package/dist/wasm/internal/pbkdf2.d.ts.map +1 -0
  209. package/dist/wasm/internal/pbkdf2.js +51 -0
  210. package/dist/wasm/internal/pbkdf2.js.map +1 -0
  211. package/dist/wasm/internal/x25519.d.ts +4 -0
  212. package/dist/wasm/internal/x25519.d.ts.map +1 -0
  213. package/dist/wasm/internal/x25519.js +21 -0
  214. package/dist/wasm/internal/x25519.js.map +1 -0
  215. package/dist/zod/TypedData.d.ts +12 -12
  216. package/package.json +81 -1
  217. package/src/core/Base64.ts +19 -166
  218. package/src/core/BinaryStateTree.ts +2 -3
  219. package/src/core/Bls.ts +27 -77
  220. package/src/core/Ed25519.ts +7 -7
  221. package/src/core/Engine.ts +308 -0
  222. package/src/core/Hash.ts +9 -12
  223. package/src/core/Keystore.ts +17 -17
  224. package/src/core/Mnemonic.ts +3 -6
  225. package/src/core/P256.ts +15 -24
  226. package/src/core/Rlp_tx.bench.ts +2 -4
  227. package/src/core/Secp256k1.ts +9 -14
  228. package/src/core/TxEnvelopeEip1559.bench.ts +0 -5
  229. package/src/core/WebCryptoP256.ts +2 -2
  230. package/src/core/X25519.ts +4 -3
  231. package/src/core/_test/AbiParameters.adversarial.fuzz.ts +1 -2
  232. package/src/core/_test/AbiParameters.fuzz.ts +1 -2
  233. package/src/core/_test/Base.fuzz.ts +1 -2
  234. package/src/core/_test/Cbor.fuzz.ts +1 -2
  235. package/src/core/_test/Engine.fuzz.ts +293 -0
  236. package/src/core/_test/Engine.test-d.ts +140 -0
  237. package/src/core/_test/Engine.test.ts +618 -0
  238. package/src/core/_test/Hash.vectors.test.ts +64 -0
  239. package/src/core/_test/Hex.fuzz.ts +1 -2
  240. package/src/core/_test/Rlp.fuzz.ts +1 -2
  241. package/src/core/_test/Signature.fuzz.ts +1 -2
  242. package/src/core/_test/curves.test.ts +7 -0
  243. package/src/core/_test/index.test.ts +1 -0
  244. package/src/core/internal/bls.ts +121 -0
  245. package/src/core/internal/codec/_test/base64.conformance.ts +156 -0
  246. package/src/core/internal/codec/_test/hex.conformance.ts +204 -0
  247. package/src/core/internal/codec/_test/hex.fuzz.ts +234 -0
  248. package/src/core/internal/codec/_test/hex.test.ts +124 -0
  249. package/src/core/internal/codec/base64.ts +222 -0
  250. package/src/core/internal/codec/hex.ts +115 -34
  251. package/src/core/internal/curves.ts +18 -0
  252. package/src/core/internal/ed25519.ts +64 -0
  253. package/src/core/internal/engine.ts +410 -0
  254. package/src/core/internal/hash.ts +48 -0
  255. package/src/core/internal/keystore.ts +107 -0
  256. package/src/core/internal/mnemonic.ts +18 -0
  257. package/src/core/internal/p256.ts +91 -0
  258. package/src/core/internal/secp256k1.ts +91 -0
  259. package/src/core/internal/x25519.ts +40 -0
  260. package/src/index.docs.ts +2 -0
  261. package/src/index.ts +39 -0
  262. package/src/node/Ed25519.ts +87 -0
  263. package/src/node/Engine.ts +125 -0
  264. package/src/node/Hash.ts +51 -0
  265. package/src/node/Keystore.ts +103 -0
  266. package/src/node/Mnemonic.ts +68 -0
  267. package/src/node/P256.ts +48 -0
  268. package/src/node/X25519.ts +93 -0
  269. package/src/node/_test/Curves.fuzz.ts +76 -0
  270. package/src/node/_test/Ed25519.test-d.ts +35 -0
  271. package/src/node/_test/Ed25519.test.ts +220 -0
  272. package/src/node/_test/Engine.test-d.ts +28 -0
  273. package/src/node/_test/Engine.test.ts +102 -0
  274. package/src/node/_test/Hash.test-d.ts +32 -0
  275. package/src/node/_test/Hash.test.ts +101 -0
  276. package/src/node/_test/Keystore.fuzz.ts +58 -0
  277. package/src/node/_test/Keystore.test-d.ts +43 -0
  278. package/src/node/_test/Keystore.test.ts +198 -0
  279. package/src/node/_test/Mnemonic.fuzz.ts +31 -0
  280. package/src/node/_test/Mnemonic.test-d.ts +21 -0
  281. package/src/node/_test/Mnemonic.test.ts +98 -0
  282. package/src/node/_test/P256.test-d.ts +29 -0
  283. package/src/node/_test/P256.test.ts +102 -0
  284. package/src/node/_test/X25519.test-d.ts +28 -0
  285. package/src/node/_test/X25519.test.ts +239 -0
  286. package/src/node/index.ts +132 -0
  287. package/src/tempo/VirtualMaster.test.ts +75 -0
  288. package/src/tempo/VirtualMaster.ts +28 -6
  289. package/src/tempo/internal/mine.wasm.ts +7 -13
  290. package/src/tempo/internal/virtualMasterPool.ts +4 -1
  291. package/src/tsconfig.json +2 -1
  292. package/src/version.ts +1 -1
  293. package/src/wasm/Ed25519.ts +114 -0
  294. package/src/wasm/Engine.ts +121 -0
  295. package/src/wasm/Hash.bench.ts +60 -0
  296. package/src/wasm/Hash.ts +88 -0
  297. package/src/wasm/Keystore.ts +48 -0
  298. package/src/wasm/Mnemonic.ts +44 -0
  299. package/src/wasm/X25519.ts +70 -0
  300. package/src/wasm/_test/Crypto25519.conformance.ts +57 -0
  301. package/src/wasm/_test/Crypto25519.fuzz.ts +187 -0
  302. package/src/wasm/_test/Ed25519.test-d.ts +28 -0
  303. package/src/wasm/_test/Ed25519.test.ts +168 -0
  304. package/src/wasm/_test/Ed25519.vectors.test.ts +73 -0
  305. package/src/wasm/_test/Engine.test-d.ts +28 -0
  306. package/src/wasm/_test/Engine.test.ts +96 -0
  307. package/src/wasm/_test/Hash.browser.test.ts +54 -0
  308. package/src/wasm/_test/Hash.fuzz.ts +223 -0
  309. package/src/wasm/_test/Hash.test-d.ts +33 -0
  310. package/src/wasm/_test/Hash.test.ts +311 -0
  311. package/src/wasm/_test/Hash.vectors.test.ts +124 -0
  312. package/src/wasm/_test/Keystore.browser.test.ts +42 -0
  313. package/src/wasm/_test/Keystore.fuzz.ts +113 -0
  314. package/src/wasm/_test/Keystore.test-d.ts +33 -0
  315. package/src/wasm/_test/Keystore.test.ts +192 -0
  316. package/src/wasm/_test/Keystore.vectors.test.ts +21 -0
  317. package/src/wasm/_test/Mnemonic.test-d.ts +17 -0
  318. package/src/wasm/_test/Mnemonic.test.ts +110 -0
  319. package/src/wasm/_test/Runtime.test.ts +21 -0
  320. package/src/wasm/_test/X25519.test-d.ts +21 -0
  321. package/src/wasm/_test/X25519.test.ts +194 -0
  322. package/src/wasm/_test/fixtures.ts +4 -0
  323. package/src/wasm/_test/runtime.wasm.ts +11 -0
  324. package/src/wasm/index.ts +120 -0
  325. package/src/wasm/internal/blake3.ts +45 -0
  326. package/src/wasm/internal/blake3.wasm.ts +11 -0
  327. package/src/wasm/internal/crypto25519.ts +43 -0
  328. package/src/wasm/internal/crypto25519.wasm.ts +11 -0
  329. package/src/wasm/internal/ed25519.ts +31 -0
  330. package/src/wasm/internal/hash.ts +50 -0
  331. package/src/wasm/internal/hashes.ts +29 -0
  332. package/src/wasm/internal/hashes.wasm.ts +17 -0
  333. package/src/wasm/internal/instantiate.ts +135 -0
  334. package/src/wasm/internal/mnemonic.ts +56 -0
  335. package/src/wasm/internal/pbkdf2.ts +86 -0
  336. package/src/wasm/internal/x25519.ts +29 -0
  337. package/src/tempo/internal/mine.c +0 -182
@@ -0,0 +1,48 @@
1
+ import { blake3 as noble_blake3 } from '@noble/hashes/blake3.js'
2
+ import { hmac } from '@noble/hashes/hmac.js'
3
+ import { ripemd160 as noble_ripemd160 } from '@noble/hashes/legacy.js'
4
+ import { sha256 as noble_sha256 } from '@noble/hashes/sha2.js'
5
+ import { keccak_256 as noble_keccak256 } from '@noble/hashes/sha3.js'
6
+ import { type Complete, type Hash, overrides } from './engine.js'
7
+
8
+ /**
9
+ * Resolvers for the `Hash` slot, and ox's defaults for it, backed by
10
+ * `@noble/hashes`.
11
+ *
12
+ * Declaring the defaults against the slot contract is what keeps them honest: a
13
+ * default that goes missing, or whose signature drifts, fails to compile rather
14
+ * than failing at the call site.
15
+ */
16
+
17
+ const blake3Default: Complete<Hash>['blake3'] = (input) => noble_blake3(input)
18
+
19
+ const hmacSha256Default: Complete<Hash>['hmacSha256'] = (key, message) =>
20
+ hmac(noble_sha256, key, message)
21
+
22
+ const keccak256Default: Complete<Hash>['keccak256'] = (input) =>
23
+ noble_keccak256(input)
24
+
25
+ const ripemd160Default: Complete<Hash>['ripemd160'] = (input) =>
26
+ noble_ripemd160(input)
27
+
28
+ const sha256Default: Complete<Hash>['sha256'] = (input) => noble_sha256(input)
29
+
30
+ /** @internal */
31
+ export const blake3: Complete<Hash>['blake3'] = (input) =>
32
+ (overrides.Hash?.blake3 ?? blake3Default)(input)
33
+
34
+ /** @internal */
35
+ export const hmacSha256: Complete<Hash>['hmacSha256'] = (key, message) =>
36
+ (overrides.Hash?.hmacSha256 ?? hmacSha256Default)(key, message)
37
+
38
+ /** @internal */
39
+ export const keccak256: Complete<Hash>['keccak256'] = (input) =>
40
+ (overrides.Hash?.keccak256 ?? keccak256Default)(input)
41
+
42
+ /** @internal */
43
+ export const ripemd160: Complete<Hash>['ripemd160'] = (input) =>
44
+ (overrides.Hash?.ripemd160 ?? ripemd160Default)(input)
45
+
46
+ /** @internal */
47
+ export const sha256: Complete<Hash>['sha256'] = (input) =>
48
+ (overrides.Hash?.sha256 ?? sha256Default)(input)
@@ -0,0 +1,107 @@
1
+ import { ctr } from '@noble/ciphers/aes.js'
2
+ import {
3
+ pbkdf2 as pbkdf2_noble,
4
+ pbkdf2Async as pbkdf2Async_noble,
5
+ } from '@noble/hashes/pbkdf2.js'
6
+ import {
7
+ scrypt as scrypt_noble,
8
+ scryptAsync as scryptAsync_noble,
9
+ } from '@noble/hashes/scrypt.js'
10
+ import { sha256 } from '@noble/hashes/sha2.js'
11
+ import { type Complete, type Keystore, overrides } from './engine.js'
12
+
13
+ /**
14
+ * Resolvers for the `Keystore` slot, and ox's defaults for it, backed by
15
+ * `@noble/ciphers` and `@noble/hashes`.
16
+ *
17
+ * Declaring the defaults against the slot contract is what keeps them honest: a
18
+ * default that goes missing, or whose signature drifts, fails to compile rather
19
+ * than failing at the call site.
20
+ */
21
+
22
+ const aesCtrDecryptDefault: Complete<Keystore>['aesCtrDecrypt'] = (
23
+ key,
24
+ iv,
25
+ data,
26
+ ) => ctr(key, iv).decrypt(data)
27
+
28
+ const aesCtrEncryptDefault: Complete<Keystore>['aesCtrEncrypt'] = (
29
+ key,
30
+ iv,
31
+ data,
32
+ ) => ctr(key, iv).encrypt(data)
33
+
34
+ const pbkdf2Sha256Default: Complete<Keystore>['pbkdf2Sha256'] = (
35
+ password,
36
+ salt,
37
+ options,
38
+ ) => pbkdf2_noble(sha256, password, salt, options)
39
+
40
+ const pbkdf2Sha256AsyncDefault: Complete<Keystore>['pbkdf2Sha256Async'] = (
41
+ password,
42
+ salt,
43
+ options,
44
+ ) => pbkdf2Async_noble(sha256, password, salt, options)
45
+
46
+ const scryptDefault: Complete<Keystore>['scrypt'] = (password, salt, options) =>
47
+ scrypt_noble(password, salt, options)
48
+
49
+ const scryptAsyncDefault: Complete<Keystore>['scryptAsync'] = (
50
+ password,
51
+ salt,
52
+ options,
53
+ ) => scryptAsync_noble(password, salt, options)
54
+
55
+ /** @internal */
56
+ export const aesCtrDecrypt: Complete<Keystore>['aesCtrDecrypt'] = (
57
+ key,
58
+ iv,
59
+ data,
60
+ ) => (overrides.Keystore?.aesCtrDecrypt ?? aesCtrDecryptDefault)(key, iv, data)
61
+
62
+ /** @internal */
63
+ export const aesCtrEncrypt: Complete<Keystore>['aesCtrEncrypt'] = (
64
+ key,
65
+ iv,
66
+ data,
67
+ ) => (overrides.Keystore?.aesCtrEncrypt ?? aesCtrEncryptDefault)(key, iv, data)
68
+
69
+ /** @internal */
70
+ export const pbkdf2Sha256: Complete<Keystore>['pbkdf2Sha256'] = (
71
+ password,
72
+ salt,
73
+ options,
74
+ ) =>
75
+ (overrides.Keystore?.pbkdf2Sha256 ?? pbkdf2Sha256Default)(
76
+ password,
77
+ salt,
78
+ options,
79
+ )
80
+
81
+ /** @internal */
82
+ export const pbkdf2Sha256Async: Complete<Keystore>['pbkdf2Sha256Async'] = (
83
+ password,
84
+ salt,
85
+ options,
86
+ ) =>
87
+ (overrides.Keystore?.pbkdf2Sha256Async ?? pbkdf2Sha256AsyncDefault)(
88
+ password,
89
+ salt,
90
+ options,
91
+ )
92
+
93
+ /** @internal */
94
+ export const scrypt: Complete<Keystore>['scrypt'] = (password, salt, options) =>
95
+ (overrides.Keystore?.scrypt ?? scryptDefault)(password, salt, options)
96
+
97
+ /** @internal */
98
+ export const scryptAsync: Complete<Keystore>['scryptAsync'] = (
99
+ password,
100
+ salt,
101
+ options,
102
+ ) =>
103
+ (overrides.Keystore?.scryptAsync ?? scryptAsyncDefault)(
104
+ password,
105
+ salt,
106
+ options,
107
+ )
@@ -0,0 +1,18 @@
1
+ import { mnemonicToSeedSync } from '@scure/bip39'
2
+ import { type Complete, type Mnemonic, overrides } from './engine.js'
3
+
4
+ /**
5
+ * Resolvers for the `Mnemonic` slot, and ox's defaults for it, backed by
6
+ * `@scure/bip39`.
7
+ *
8
+ * Declaring the defaults against the slot contract is what keeps them honest: a
9
+ * default that goes missing, or whose signature drifts, fails to compile rather
10
+ * than failing at the call site.
11
+ */
12
+
13
+ const toSeedDefault: Complete<Mnemonic>['toSeed'] = (mnemonic, passphrase) =>
14
+ mnemonicToSeedSync(mnemonic, passphrase)
15
+
16
+ /** @internal */
17
+ export const toSeed: Complete<Mnemonic>['toSeed'] = (mnemonic, passphrase) =>
18
+ (overrides.Mnemonic?.toSeed ?? toSeedDefault)(mnemonic, passphrase)
@@ -0,0 +1,91 @@
1
+ import { p256 } from '@noble/curves/nist.js'
2
+ import { type Complete, type Ecdsa, overrides } from './engine.js'
3
+
4
+ /**
5
+ * Resolvers for the `P256` slot, and ox's defaults for it, backed by
6
+ * `@noble/curves`.
7
+ *
8
+ * Declaring the defaults against the slot contract is what keeps them honest: a
9
+ * default that goes missing, or whose signature drifts, fails to compile rather
10
+ * than failing at the call site. `lowS` and the recovered signature format are
11
+ * pinned here rather than in the contract, so an engine cannot opt out of them.
12
+ */
13
+
14
+ const getPublicKeyDefault: Complete<Ecdsa>['getPublicKey'] = (privateKey) =>
15
+ p256.getPublicKey(privateKey, false)
16
+
17
+ const getSharedSecretDefault: Complete<Ecdsa>['getSharedSecret'] = (
18
+ privateKey,
19
+ publicKey,
20
+ ) => p256.getSharedSecret(privateKey, publicKey, true)
21
+
22
+ const randomSecretKeyDefault: Complete<Ecdsa>['randomSecretKey'] = () =>
23
+ p256.utils.randomSecretKey()
24
+
25
+ const recoverPublicKeyDefault: Complete<Ecdsa>['recoverPublicKey'] = (
26
+ signature,
27
+ payload,
28
+ ) =>
29
+ p256.Signature.fromBytes(signature, 'recovered')
30
+ .recoverPublicKey(payload)
31
+ .toBytes(false)
32
+
33
+ const signDefault: Complete<Ecdsa>['sign'] = (payload, privateKey, options) =>
34
+ p256.sign(payload, privateKey, {
35
+ ...options,
36
+ format: 'recovered',
37
+ lowS: true,
38
+ })
39
+
40
+ const verifyDefault: Complete<Ecdsa>['verify'] = (
41
+ signature,
42
+ payload,
43
+ publicKey,
44
+ options,
45
+ ) => p256.verify(signature, payload, publicKey, { ...options, lowS: true })
46
+
47
+ /** @internal */
48
+ export const getPublicKey: Complete<Ecdsa>['getPublicKey'] = (privateKey) =>
49
+ (overrides.P256?.getPublicKey ?? getPublicKeyDefault)(privateKey)
50
+
51
+ /** @internal */
52
+ export const getSharedSecret: Complete<Ecdsa>['getSharedSecret'] = (
53
+ privateKey,
54
+ publicKey,
55
+ ) =>
56
+ (overrides.P256?.getSharedSecret ?? getSharedSecretDefault)(
57
+ privateKey,
58
+ publicKey,
59
+ )
60
+
61
+ /** @internal */
62
+ export const randomSecretKey: Complete<Ecdsa>['randomSecretKey'] = () =>
63
+ (overrides.P256?.randomSecretKey ?? randomSecretKeyDefault)()
64
+
65
+ /** @internal */
66
+ export const recoverPublicKey: Complete<Ecdsa>['recoverPublicKey'] = (
67
+ signature,
68
+ payload,
69
+ ) =>
70
+ (overrides.P256?.recoverPublicKey ?? recoverPublicKeyDefault)(
71
+ signature,
72
+ payload,
73
+ )
74
+
75
+ /** @internal */
76
+ export const sign: Complete<Ecdsa>['sign'] = (payload, privateKey, options) =>
77
+ (overrides.P256?.sign ?? signDefault)(payload, privateKey, options)
78
+
79
+ /** @internal */
80
+ export const verify: Complete<Ecdsa>['verify'] = (
81
+ signature,
82
+ payload,
83
+ publicKey,
84
+ options,
85
+ ) =>
86
+ (overrides.P256?.verify ?? verifyDefault)(
87
+ signature,
88
+ payload,
89
+ publicKey,
90
+ options,
91
+ )
@@ -0,0 +1,91 @@
1
+ import { secp256k1 } from '@noble/curves/secp256k1.js'
2
+ import { type Complete, type Ecdsa, overrides } from './engine.js'
3
+
4
+ /**
5
+ * Resolvers for the `Secp256k1` slot, and ox's defaults for it, backed by
6
+ * `@noble/curves`.
7
+ *
8
+ * Declaring the defaults against the slot contract is what keeps them honest: a
9
+ * default that goes missing, or whose signature drifts, fails to compile rather
10
+ * than failing at the call site. `lowS` and the recovered signature format are
11
+ * pinned here rather than in the contract, so an engine cannot opt out of them.
12
+ */
13
+
14
+ const getPublicKeyDefault: Complete<Ecdsa>['getPublicKey'] = (privateKey) =>
15
+ secp256k1.getPublicKey(privateKey, false)
16
+
17
+ const getSharedSecretDefault: Complete<Ecdsa>['getSharedSecret'] = (
18
+ privateKey,
19
+ publicKey,
20
+ ) => secp256k1.getSharedSecret(privateKey, publicKey, true)
21
+
22
+ const randomSecretKeyDefault: Complete<Ecdsa>['randomSecretKey'] = () =>
23
+ secp256k1.utils.randomSecretKey()
24
+
25
+ const recoverPublicKeyDefault: Complete<Ecdsa>['recoverPublicKey'] = (
26
+ signature,
27
+ payload,
28
+ ) =>
29
+ secp256k1.Signature.fromBytes(signature, 'recovered')
30
+ .recoverPublicKey(payload)
31
+ .toBytes(false)
32
+
33
+ const signDefault: Complete<Ecdsa>['sign'] = (payload, privateKey, options) =>
34
+ secp256k1.sign(payload, privateKey, {
35
+ ...options,
36
+ format: 'recovered',
37
+ lowS: true,
38
+ })
39
+
40
+ const verifyDefault: Complete<Ecdsa>['verify'] = (
41
+ signature,
42
+ payload,
43
+ publicKey,
44
+ options,
45
+ ) => secp256k1.verify(signature, payload, publicKey, { ...options, lowS: true })
46
+
47
+ /** @internal */
48
+ export const getPublicKey: Complete<Ecdsa>['getPublicKey'] = (privateKey) =>
49
+ (overrides.Secp256k1?.getPublicKey ?? getPublicKeyDefault)(privateKey)
50
+
51
+ /** @internal */
52
+ export const getSharedSecret: Complete<Ecdsa>['getSharedSecret'] = (
53
+ privateKey,
54
+ publicKey,
55
+ ) =>
56
+ (overrides.Secp256k1?.getSharedSecret ?? getSharedSecretDefault)(
57
+ privateKey,
58
+ publicKey,
59
+ )
60
+
61
+ /** @internal */
62
+ export const randomSecretKey: Complete<Ecdsa>['randomSecretKey'] = () =>
63
+ (overrides.Secp256k1?.randomSecretKey ?? randomSecretKeyDefault)()
64
+
65
+ /** @internal */
66
+ export const recoverPublicKey: Complete<Ecdsa>['recoverPublicKey'] = (
67
+ signature,
68
+ payload,
69
+ ) =>
70
+ (overrides.Secp256k1?.recoverPublicKey ?? recoverPublicKeyDefault)(
71
+ signature,
72
+ payload,
73
+ )
74
+
75
+ /** @internal */
76
+ export const sign: Complete<Ecdsa>['sign'] = (payload, privateKey, options) =>
77
+ (overrides.Secp256k1?.sign ?? signDefault)(payload, privateKey, options)
78
+
79
+ /** @internal */
80
+ export const verify: Complete<Ecdsa>['verify'] = (
81
+ signature,
82
+ payload,
83
+ publicKey,
84
+ options,
85
+ ) =>
86
+ (overrides.Secp256k1?.verify ?? verifyDefault)(
87
+ signature,
88
+ payload,
89
+ publicKey,
90
+ options,
91
+ )
@@ -0,0 +1,40 @@
1
+ import { x25519 } from '@noble/curves/ed25519.js'
2
+ import { type Complete, type Ecdh, overrides } from './engine.js'
3
+
4
+ /**
5
+ * Resolvers for the `X25519` slot, and ox's defaults for it, backed by
6
+ * `@noble/curves`.
7
+ *
8
+ * Declaring the defaults against the slot contract is what keeps them honest: a
9
+ * default that goes missing, or whose signature drifts, fails to compile rather
10
+ * than failing at the call site.
11
+ */
12
+
13
+ const getPublicKeyDefault: Complete<Ecdh>['getPublicKey'] = (privateKey) =>
14
+ x25519.getPublicKey(privateKey)
15
+
16
+ const getSharedSecretDefault: Complete<Ecdh>['getSharedSecret'] = (
17
+ privateKey,
18
+ publicKey,
19
+ ) => x25519.getSharedSecret(privateKey, publicKey)
20
+
21
+ const randomSecretKeyDefault: Complete<Ecdh>['randomSecretKey'] = () =>
22
+ x25519.utils.randomSecretKey()
23
+
24
+ /** @internal */
25
+ export const getPublicKey: Complete<Ecdh>['getPublicKey'] = (privateKey) =>
26
+ (overrides.X25519?.getPublicKey ?? getPublicKeyDefault)(privateKey)
27
+
28
+ /** @internal */
29
+ export const getSharedSecret: Complete<Ecdh>['getSharedSecret'] = (
30
+ privateKey,
31
+ publicKey,
32
+ ) =>
33
+ (overrides.X25519?.getSharedSecret ?? getSharedSecretDefault)(
34
+ privateKey,
35
+ publicKey,
36
+ )
37
+
38
+ /** @internal */
39
+ export const randomSecretKey: Complete<Ecdh>['randomSecretKey'] = () =>
40
+ (overrides.X25519?.randomSecretKey ?? randomSecretKeyDefault)()
package/src/index.docs.ts CHANGED
@@ -7,6 +7,8 @@ export * from './erc6492/index.js'
7
7
  export * from './erc7821/index.js'
8
8
  export * from './erc8010/index.js'
9
9
  export * from './erc8021/index.js'
10
+ export * from './node/index.js'
10
11
  export * from './webauthn/index.js'
11
12
  export * from './tempo/index.js'
13
+ export * from './wasm/index.js'
12
14
  export * from './zod/index.js'
package/src/index.ts CHANGED
@@ -1520,6 +1520,45 @@ export * as CoseKey from './core/CoseKey.js'
1520
1520
  */
1521
1521
  export * as Ed25519 from './core/Ed25519.js'
1522
1522
 
1523
+ /**
1524
+ * Functions for delegating ox's cryptography to a different implementation.
1525
+ *
1526
+ * Ox uses [`@noble`](https://github.com/paulmillr/noble-hashes) and
1527
+ * [`@scure`](https://github.com/paulmillr/scure-bip32) by default. An engine
1528
+ * replaces those implementations with your own — for example a WASM build of an
1529
+ * audited C library.
1530
+ *
1531
+ * @example
1532
+ * ### Installing an Engine
1533
+ *
1534
+ * Engine slots are named after ox modules, and everything is optional: any slot
1535
+ * or function you leave out keeps using ox's default implementation.
1536
+ *
1537
+ * ```ts twoslash
1538
+ * // @noErrors
1539
+ * import { Engine } from 'ox'
1540
+ * import { Hash } from 'ox/wasm'
1541
+ *
1542
+ * await Engine.install({
1543
+ * Hash: Hash.engine()
1544
+ * })
1545
+ * ```
1546
+ *
1547
+ * @example
1548
+ * ### Overriding a Single Primitive
1549
+ *
1550
+ * ```ts twoslash
1551
+ * import { Engine } from 'ox'
1552
+ *
1553
+ * Engine.set({
1554
+ * Hash: { keccak256: () => new Uint8Array(32) }
1555
+ * })
1556
+ * ```
1557
+ *
1558
+ * @category Crypto
1559
+ */
1560
+ export * as Engine from './core/Engine.js'
1561
+
1523
1562
  /**
1524
1563
  * Utility functions for working with ENS names.
1525
1564
  *
@@ -0,0 +1,87 @@
1
+ import * as crypto from 'node:crypto'
2
+ import type * as Engine from '../core/Engine.js'
3
+ import type * as Errors from '../core/Errors.js'
4
+
5
+ export * from '../core/Ed25519.js'
6
+
7
+ const privateKeyPrefix = Buffer.from('302e020100300506032b657004220420', 'hex')
8
+
9
+ /**
10
+ * Creates a Node.js implementation of the [`Ed25519`](/api/Ed25519) engine
11
+ * slot, without installing it.
12
+ *
13
+ * Node's verifier does not implement Ox's ZIP-215 verification semantics, so
14
+ * this engine deliberately omits `verify`. Public-key conversion and random
15
+ * key generation also remain on Ox's default implementation.
16
+ *
17
+ * @example
18
+ * ```ts twoslash
19
+ * // @noErrors
20
+ * import { Engine } from 'ox'
21
+ * import { Ed25519 } from 'ox/node'
22
+ *
23
+ * await Engine.install({ Ed25519: Ed25519.engine() })
24
+ *
25
+ * Ed25519.getPublicKey({ privateKey: '0x...' })
26
+ * ```
27
+ *
28
+ * @returns The raw `Ed25519` engine slot.
29
+ */
30
+ export function engine(): Promise<engine.ReturnType> {
31
+ return Promise.resolve({
32
+ getPublicKey: (privateKey) =>
33
+ rawPublicKey(crypto.createPublicKey(toPrivateKey(privateKey))),
34
+ sign: (payload, privateKey) =>
35
+ new Uint8Array(crypto.sign(null, payload, toPrivateKey(privateKey))),
36
+ toMontgomerySecret: (privateKey) => {
37
+ assertLength(privateKey)
38
+ const digest = crypto.hash('sha512', privateKey, 'buffer')
39
+ try {
40
+ const secretKey = Uint8Array.from(digest.subarray(0, 32))
41
+ secretKey[0]! &= 248
42
+ secretKey[31]! &= 127
43
+ secretKey[31]! |= 64
44
+ return secretKey
45
+ } finally {
46
+ digest.fill(0)
47
+ }
48
+ },
49
+ })
50
+ }
51
+
52
+ export declare namespace engine {
53
+ /** Every `Ed25519` primitive this module implements. */
54
+ type ReturnType = {
55
+ [key in 'getPublicKey' | 'sign' | 'toMontgomerySecret']-?: NonNullable<
56
+ Engine.Eddsa[key]
57
+ >
58
+ }
59
+
60
+ type ErrorType = Errors.GlobalErrorType
61
+ }
62
+
63
+ function assertLength(key: Uint8Array): void {
64
+ if (key.length !== 32)
65
+ throw new RangeError(
66
+ `Ed25519 private key must be 32 bytes, got ${key.length}`,
67
+ )
68
+ }
69
+
70
+ function rawPublicKey(publicKey: crypto.KeyObject): Uint8Array {
71
+ const der = publicKey.export({ format: 'der', type: 'spki' })
72
+ return Uint8Array.from(der.subarray(-32))
73
+ }
74
+
75
+ function toPrivateKey(privateKey: Uint8Array): crypto.KeyObject {
76
+ assertLength(privateKey)
77
+ const der = Buffer.concat([privateKeyPrefix, privateKey])
78
+ try {
79
+ return crypto.createPrivateKey({
80
+ format: 'der',
81
+ key: der,
82
+ type: 'pkcs8',
83
+ })
84
+ } finally {
85
+ der.fill(0)
86
+ }
87
+ }
@@ -0,0 +1,125 @@
1
+ import * as CoreEngine from '../core/Engine.js'
2
+ import type * as Errors from '../core/Errors.js'
3
+ import * as Ed25519 from './Ed25519.js'
4
+ import * as Hash from './Hash.js'
5
+ import * as Keystore from './Keystore.js'
6
+ import * as Mnemonic from './Mnemonic.js'
7
+ import * as P256 from './P256.js'
8
+ import * as X25519 from './X25519.js'
9
+
10
+ export {
11
+ AsyncScopeError,
12
+ get,
13
+ InvalidSlotValueError,
14
+ reset,
15
+ set,
16
+ UnknownPrimitiveError,
17
+ UnknownSlotError,
18
+ with,
19
+ } from '../core/Engine.js'
20
+ export type {
21
+ Bls,
22
+ Ecdh,
23
+ Ecdsa,
24
+ Eddsa,
25
+ Engine,
26
+ Hash,
27
+ Keystore,
28
+ Mnemonic,
29
+ } from '../core/Engine.js'
30
+
31
+ /**
32
+ * Installs every Node.js implementation this entrypoint provides.
33
+ *
34
+ * Slots initialize in parallel and are installed atomically. Call this once
35
+ * during startup, before any cryptographic operation.
36
+ *
37
+ * @example
38
+ * ```ts twoslash
39
+ * // @noErrors
40
+ * import { Engine, Hash } from 'ox/node'
41
+ *
42
+ * await Engine.install()
43
+ *
44
+ * Hash.sha256('0xdeadbeef')
45
+ * ```
46
+ *
47
+ * @returns The engine that was installed.
48
+ */
49
+ export function install(): Promise<install.ReturnType> {
50
+ return CoreEngine.install({
51
+ Ed25519: Ed25519.engine(),
52
+ Hash: Hash.engine(),
53
+ Keystore: Keystore.engine(),
54
+ Mnemonic: Mnemonic.engine(),
55
+ P256: P256.engine(),
56
+ X25519: X25519.engine(),
57
+ })
58
+ }
59
+
60
+ export declare namespace install {
61
+ /** Every slot this entrypoint installs, each with its primitives present. */
62
+ type ReturnType = engine.ReturnType
63
+
64
+ type ErrorType = engine.ErrorType | CoreEngine.install.ErrorType
65
+ }
66
+
67
+ /**
68
+ * Creates every Node.js implementation this entrypoint provides, without
69
+ * installing it.
70
+ *
71
+ * Reach for this where an engine has to exist as a value, such as a benchmark
72
+ * or an [`Engine.with`](/api/Engine/with) scope.
73
+ *
74
+ * @example
75
+ * ```ts twoslash
76
+ * // @noErrors
77
+ * import { Engine, Hash } from 'ox'
78
+ * import { Engine as NodeEngine } from 'ox/node'
79
+ *
80
+ * const node = await NodeEngine.engine()
81
+ *
82
+ * Engine.with(node, () => Hash.sha256('0xdeadbeef'))
83
+ * ```
84
+ *
85
+ * @returns An engine, ready to install.
86
+ */
87
+ export async function engine(): Promise<engine.ReturnType> {
88
+ const [ed25519, hash, keystore, mnemonic, p256, x25519] = await Promise.all([
89
+ Ed25519.engine(),
90
+ Hash.engine(),
91
+ Keystore.engine(),
92
+ Mnemonic.engine(),
93
+ P256.engine(),
94
+ X25519.engine(),
95
+ ])
96
+ return {
97
+ Ed25519: ed25519,
98
+ Hash: hash,
99
+ Keystore: keystore,
100
+ Mnemonic: mnemonic,
101
+ P256: p256,
102
+ X25519: x25519,
103
+ }
104
+ }
105
+
106
+ export declare namespace engine {
107
+ /** Every slot this entrypoint supplies, each with its primitives present. */
108
+ type ReturnType = {
109
+ Ed25519: Ed25519.engine.ReturnType
110
+ Hash: Hash.engine.ReturnType
111
+ Keystore: Keystore.engine.ReturnType
112
+ Mnemonic: Mnemonic.engine.ReturnType
113
+ P256: P256.engine.ReturnType
114
+ X25519: X25519.engine.ReturnType
115
+ }
116
+
117
+ type ErrorType =
118
+ | Ed25519.engine.ErrorType
119
+ | Hash.engine.ErrorType
120
+ | Keystore.engine.ErrorType
121
+ | Mnemonic.engine.ErrorType
122
+ | P256.engine.ErrorType
123
+ | X25519.engine.ErrorType
124
+ | Errors.GlobalErrorType
125
+ }