@unlink-xyz/core 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (369) hide show
  1. package/README.md +9 -0
  2. package/dist/account/{zkAccount.d.ts → account.d.ts} +36 -5
  3. package/dist/account/account.d.ts.map +1 -0
  4. package/dist/account/accounts.d.ts +42 -0
  5. package/dist/account/accounts.d.ts.map +1 -0
  6. package/dist/account/seed.d.ts +45 -0
  7. package/dist/account/seed.d.ts.map +1 -0
  8. package/dist/account/serialization.d.ts +6 -0
  9. package/dist/account/serialization.d.ts.map +1 -0
  10. package/dist/browser/index.js +56221 -0
  11. package/dist/browser/index.js.map +1 -0
  12. package/dist/browser/wallet/index.js +55942 -0
  13. package/dist/browser/wallet/index.js.map +1 -0
  14. package/dist/clients/broadcaster.d.ts +8 -2
  15. package/dist/clients/broadcaster.d.ts.map +1 -1
  16. package/dist/clients/http.d.ts +6 -0
  17. package/dist/clients/http.d.ts.map +1 -1
  18. package/dist/clients/indexer.d.ts +16 -0
  19. package/dist/clients/indexer.d.ts.map +1 -1
  20. package/dist/config.d.ts +30 -9
  21. package/dist/config.d.ts.map +1 -1
  22. package/dist/constants.d.ts +6 -0
  23. package/dist/constants.d.ts.map +1 -0
  24. package/dist/core.d.ts.map +1 -1
  25. package/dist/crypto/adapters/index.d.ts +17 -0
  26. package/dist/crypto/adapters/index.d.ts.map +1 -0
  27. package/dist/crypto/adapters/polyfills.d.ts +5 -0
  28. package/dist/crypto/adapters/polyfills.d.ts.map +1 -0
  29. package/dist/crypto/encrypt.d.ts +33 -0
  30. package/dist/crypto/encrypt.d.ts.map +1 -0
  31. package/dist/crypto/secure-memory.d.ts +25 -0
  32. package/dist/crypto/secure-memory.d.ts.map +1 -0
  33. package/dist/errors.d.ts +17 -0
  34. package/dist/errors.d.ts.map +1 -1
  35. package/dist/history/index.d.ts +3 -0
  36. package/dist/history/index.d.ts.map +1 -0
  37. package/dist/history/service.d.ts +46 -0
  38. package/dist/history/service.d.ts.map +1 -0
  39. package/dist/history/types.d.ts +21 -0
  40. package/dist/history/types.d.ts.map +1 -0
  41. package/dist/index.d.ts +16 -7
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +6721 -19
  44. package/dist/index.js.map +1 -0
  45. package/dist/keys/address.d.ts +13 -0
  46. package/dist/keys/address.d.ts.map +1 -0
  47. package/dist/keys/derive.d.ts +37 -0
  48. package/dist/keys/derive.d.ts.map +1 -0
  49. package/dist/keys/hex.d.ts +14 -0
  50. package/dist/keys/hex.d.ts.map +1 -0
  51. package/dist/keys/index.d.ts +5 -0
  52. package/dist/keys/index.d.ts.map +1 -0
  53. package/dist/keys/mnemonic.d.ts +6 -0
  54. package/dist/keys/mnemonic.d.ts.map +1 -0
  55. package/dist/keys.d.ts +5 -1
  56. package/dist/keys.d.ts.map +1 -1
  57. package/dist/prover/config.d.ts +53 -22
  58. package/dist/prover/config.d.ts.map +1 -1
  59. package/dist/prover/integrity.d.ts +20 -0
  60. package/dist/prover/integrity.d.ts.map +1 -0
  61. package/dist/prover/prover.d.ts +16 -20
  62. package/dist/prover/prover.d.ts.map +1 -1
  63. package/dist/prover/registry.d.ts +3 -30
  64. package/dist/prover/registry.d.ts.map +1 -1
  65. package/dist/state/merkle/hydrator.d.ts +21 -19
  66. package/dist/state/merkle/hydrator.d.ts.map +1 -1
  67. package/dist/state/merkle/index.d.ts +2 -2
  68. package/dist/state/merkle/index.d.ts.map +1 -1
  69. package/dist/state/merkle/merkle-tree.d.ts +8 -0
  70. package/dist/state/merkle/merkle-tree.d.ts.map +1 -1
  71. package/dist/state/store/ciphertext-store.d.ts +11 -0
  72. package/dist/state/store/ciphertext-store.d.ts.map +1 -1
  73. package/dist/state/store/history-store.d.ts +24 -0
  74. package/dist/state/store/history-store.d.ts.map +1 -0
  75. package/dist/state/store/index.d.ts +3 -2
  76. package/dist/state/store/index.d.ts.map +1 -1
  77. package/dist/state/store/job-store.d.ts +7 -7
  78. package/dist/state/store/job-store.d.ts.map +1 -1
  79. package/dist/state/store/jobs.d.ts +70 -25
  80. package/dist/state/store/jobs.d.ts.map +1 -1
  81. package/dist/state/store/leaf-store.d.ts +4 -0
  82. package/dist/state/store/leaf-store.d.ts.map +1 -1
  83. package/dist/state/store/note-store.d.ts +7 -7
  84. package/dist/state/store/note-store.d.ts.map +1 -1
  85. package/dist/state/store/nullifier-store.d.ts +9 -0
  86. package/dist/state/store/nullifier-store.d.ts.map +1 -1
  87. package/dist/state/store/records.d.ts +39 -2
  88. package/dist/state/store/records.d.ts.map +1 -1
  89. package/dist/state/store/root-store.d.ts.map +1 -1
  90. package/dist/state/store/store.d.ts +79 -27
  91. package/dist/state/store/store.d.ts.map +1 -1
  92. package/dist/storage/indexeddb.d.ts.map +1 -1
  93. package/dist/storage/memory.d.ts.map +1 -1
  94. package/dist/transactions/adapter.d.ts +31 -0
  95. package/dist/transactions/adapter.d.ts.map +1 -0
  96. package/dist/transactions/deposit.d.ts +12 -15
  97. package/dist/transactions/deposit.d.ts.map +1 -1
  98. package/dist/transactions/index.d.ts +9 -4
  99. package/dist/transactions/index.d.ts.map +1 -1
  100. package/dist/transactions/note-selection.d.ts +17 -0
  101. package/dist/transactions/note-selection.d.ts.map +1 -0
  102. package/dist/transactions/note-sync.d.ts +5 -33
  103. package/dist/transactions/note-sync.d.ts.map +1 -1
  104. package/dist/transactions/reconcile.d.ts +9 -11
  105. package/dist/transactions/reconcile.d.ts.map +1 -1
  106. package/dist/transactions/transact.d.ts +30 -22
  107. package/dist/transactions/transact.d.ts.map +1 -1
  108. package/dist/transactions/transaction-planner.d.ts +34 -0
  109. package/dist/transactions/transaction-planner.d.ts.map +1 -0
  110. package/dist/transactions/transfer-planner.d.ts +37 -0
  111. package/dist/transactions/transfer-planner.d.ts.map +1 -0
  112. package/dist/transactions/types/deposit.d.ts +67 -0
  113. package/dist/transactions/types/deposit.d.ts.map +1 -0
  114. package/dist/transactions/types/domain.d.ts +70 -0
  115. package/dist/transactions/types/domain.d.ts.map +1 -0
  116. package/dist/transactions/types/index.d.ts +18 -0
  117. package/dist/transactions/types/index.d.ts.map +1 -0
  118. package/dist/transactions/types/options.d.ts +54 -0
  119. package/dist/transactions/types/options.d.ts.map +1 -0
  120. package/dist/transactions/types/planning.d.ts +82 -0
  121. package/dist/transactions/types/planning.d.ts.map +1 -0
  122. package/dist/transactions/types/state-stores.d.ts +151 -0
  123. package/dist/transactions/types/state-stores.d.ts.map +1 -0
  124. package/dist/transactions/types/transact.d.ts +83 -0
  125. package/dist/transactions/types/transact.d.ts.map +1 -0
  126. package/dist/transactions/withdrawal-planner.d.ts +58 -0
  127. package/dist/transactions/withdrawal-planner.d.ts.map +1 -0
  128. package/dist/tsconfig.tsbuildinfo +1 -1
  129. package/dist/tsup.browser.config.d.ts +7 -0
  130. package/dist/tsup.browser.config.d.ts.map +1 -0
  131. package/dist/tsup.config.d.ts +8 -0
  132. package/dist/tsup.config.d.ts.map +1 -0
  133. package/dist/types.d.ts +1 -0
  134. package/dist/types.d.ts.map +1 -1
  135. package/dist/utils/amounts.d.ts +26 -0
  136. package/dist/utils/amounts.d.ts.map +1 -0
  137. package/dist/utils/async.d.ts +9 -0
  138. package/dist/utils/async.d.ts.map +1 -1
  139. package/dist/utils/async.js +38 -11
  140. package/dist/utils/async.js.map +1 -0
  141. package/dist/utils/bigint.d.ts +0 -2
  142. package/dist/utils/bigint.d.ts.map +1 -1
  143. package/dist/utils/format.d.ts +25 -0
  144. package/dist/utils/format.d.ts.map +1 -0
  145. package/dist/utils/notes.d.ts +15 -0
  146. package/dist/utils/notes.d.ts.map +1 -0
  147. package/dist/utils/polling.d.ts +5 -0
  148. package/dist/utils/polling.d.ts.map +1 -1
  149. package/dist/utils/random.d.ts +18 -0
  150. package/dist/utils/random.d.ts.map +1 -0
  151. package/dist/utils/signature.d.ts +6 -0
  152. package/dist/utils/signature.d.ts.map +1 -1
  153. package/dist/utils/validators.d.ts +21 -10
  154. package/dist/utils/validators.d.ts.map +1 -1
  155. package/dist/vitest.config.d.ts +3 -0
  156. package/dist/vitest.config.d.ts.map +1 -0
  157. package/dist/wallet/adapter.d.ts +21 -0
  158. package/dist/wallet/adapter.d.ts.map +1 -0
  159. package/dist/wallet/burner/service.d.ts +32 -0
  160. package/dist/wallet/burner/service.d.ts.map +1 -0
  161. package/dist/wallet/burner/types.d.ts +47 -0
  162. package/dist/wallet/burner/types.d.ts.map +1 -0
  163. package/dist/wallet/index.d.ts +20 -0
  164. package/dist/wallet/index.d.ts.map +1 -0
  165. package/dist/wallet/index.js +6462 -0
  166. package/dist/wallet/index.js.map +1 -0
  167. package/dist/wallet/sdk.d.ts +48 -0
  168. package/dist/wallet/sdk.d.ts.map +1 -0
  169. package/dist/wallet/types.d.ts +457 -0
  170. package/dist/wallet/types.d.ts.map +1 -0
  171. package/dist/wallet/unlink-wallet.d.ts +187 -0
  172. package/dist/wallet/unlink-wallet.d.ts.map +1 -0
  173. package/package.json +38 -15
  174. package/.eslintrc.json +0 -4
  175. package/account/zkAccount.test.ts +0 -316
  176. package/account/zkAccount.ts +0 -222
  177. package/circuits.json +0 -26
  178. package/clients/broadcaster.ts +0 -67
  179. package/clients/http.ts +0 -94
  180. package/clients/indexer.ts +0 -150
  181. package/config.ts +0 -39
  182. package/core.ts +0 -17
  183. package/dist/account/railgun-imports-prototype.d.ts +0 -12
  184. package/dist/account/railgun-imports-prototype.d.ts.map +0 -1
  185. package/dist/account/railgun-imports-prototype.js +0 -30
  186. package/dist/account/zkAccount.d.ts.map +0 -1
  187. package/dist/account/zkAccount.js +0 -128
  188. package/dist/circuits.json +0 -26
  189. package/dist/clients/broadcaster.js +0 -23
  190. package/dist/clients/http.js +0 -57
  191. package/dist/clients/indexer.js +0 -67
  192. package/dist/config.js +0 -29
  193. package/dist/core.js +0 -12
  194. package/dist/errors.js +0 -18
  195. package/dist/key-derivation/babyjubjub.d.ts +0 -9
  196. package/dist/key-derivation/babyjubjub.d.ts.map +0 -1
  197. package/dist/key-derivation/babyjubjub.js +0 -9
  198. package/dist/key-derivation/bech32.d.ts +0 -22
  199. package/dist/key-derivation/bech32.d.ts.map +0 -1
  200. package/dist/key-derivation/bech32.js +0 -86
  201. package/dist/key-derivation/bip32.d.ts +0 -17
  202. package/dist/key-derivation/bip32.d.ts.map +0 -1
  203. package/dist/key-derivation/bip32.js +0 -41
  204. package/dist/key-derivation/bip39.d.ts +0 -22
  205. package/dist/key-derivation/bip39.d.ts.map +0 -1
  206. package/dist/key-derivation/bip39.js +0 -56
  207. package/dist/key-derivation/bytes.d.ts +0 -19
  208. package/dist/key-derivation/bytes.d.ts.map +0 -1
  209. package/dist/key-derivation/bytes.js +0 -92
  210. package/dist/key-derivation/hash.d.ts +0 -3
  211. package/dist/key-derivation/hash.d.ts.map +0 -1
  212. package/dist/key-derivation/hash.js +0 -10
  213. package/dist/key-derivation/index.d.ts +0 -8
  214. package/dist/key-derivation/index.d.ts.map +0 -1
  215. package/dist/key-derivation/index.js +0 -7
  216. package/dist/key-derivation/wallet-node.d.ts +0 -45
  217. package/dist/key-derivation/wallet-node.d.ts.map +0 -1
  218. package/dist/key-derivation/wallet-node.js +0 -109
  219. package/dist/keys.js +0 -41
  220. package/dist/prover/config.js +0 -80
  221. package/dist/prover/index.js +0 -1
  222. package/dist/prover/prover.js +0 -274
  223. package/dist/prover/registry.js +0 -57
  224. package/dist/schema.js +0 -14
  225. package/dist/state/ciphertext-store.d.ts +0 -12
  226. package/dist/state/ciphertext-store.d.ts.map +0 -1
  227. package/dist/state/ciphertext-store.js +0 -25
  228. package/dist/state/hydrator.d.ts +0 -16
  229. package/dist/state/hydrator.d.ts.map +0 -1
  230. package/dist/state/hydrator.js +0 -18
  231. package/dist/state/index.js +0 -2
  232. package/dist/state/job-store.d.ts +0 -12
  233. package/dist/state/job-store.d.ts.map +0 -1
  234. package/dist/state/job-store.js +0 -118
  235. package/dist/state/jobs.d.ts +0 -50
  236. package/dist/state/jobs.d.ts.map +0 -1
  237. package/dist/state/jobs.js +0 -1
  238. package/dist/state/leaf-store.d.ts +0 -17
  239. package/dist/state/leaf-store.d.ts.map +0 -1
  240. package/dist/state/leaf-store.js +0 -35
  241. package/dist/state/merkle/hydrator.js +0 -36
  242. package/dist/state/merkle/index.js +0 -2
  243. package/dist/state/merkle/merkle-tree.js +0 -104
  244. package/dist/state/merkle-tree.d.ts +0 -34
  245. package/dist/state/merkle-tree.d.ts.map +0 -1
  246. package/dist/state/merkle-tree.js +0 -104
  247. package/dist/state/note-store.d.ts +0 -37
  248. package/dist/state/note-store.d.ts.map +0 -1
  249. package/dist/state/note-store.js +0 -133
  250. package/dist/state/nullifier-store.d.ts +0 -13
  251. package/dist/state/nullifier-store.d.ts.map +0 -1
  252. package/dist/state/nullifier-store.js +0 -21
  253. package/dist/state/records.d.ts +0 -57
  254. package/dist/state/records.d.ts.map +0 -1
  255. package/dist/state/records.js +0 -1
  256. package/dist/state/root-store.d.ts +0 -13
  257. package/dist/state/root-store.d.ts.map +0 -1
  258. package/dist/state/root-store.js +0 -30
  259. package/dist/state/store/ciphertext-store.js +0 -25
  260. package/dist/state/store/index.js +0 -8
  261. package/dist/state/store/job-store.js +0 -118
  262. package/dist/state/store/jobs.js +0 -1
  263. package/dist/state/store/leaf-store.js +0 -35
  264. package/dist/state/store/note-store.js +0 -142
  265. package/dist/state/store/nullifier-store.js +0 -30
  266. package/dist/state/store/records.js +0 -1
  267. package/dist/state/store/root-store.js +0 -30
  268. package/dist/state/store/store.js +0 -22
  269. package/dist/state/store.d.ts +0 -26
  270. package/dist/state/store.d.ts.map +0 -1
  271. package/dist/state/store.js +0 -19
  272. package/dist/state.d.ts +0 -83
  273. package/dist/state.d.ts.map +0 -1
  274. package/dist/state.js +0 -171
  275. package/dist/storage/index.js +0 -2
  276. package/dist/storage/indexeddb.js +0 -205
  277. package/dist/storage/memory.js +0 -87
  278. package/dist/transactions/deposit.js +0 -169
  279. package/dist/transactions/index.js +0 -4
  280. package/dist/transactions/note-sync.js +0 -320
  281. package/dist/transactions/reconcile.js +0 -39
  282. package/dist/transactions/shield.d.ts +0 -5
  283. package/dist/transactions/shield.d.ts.map +0 -1
  284. package/dist/transactions/shield.js +0 -93
  285. package/dist/transactions/transact.js +0 -561
  286. package/dist/transactions/types.d.ts +0 -114
  287. package/dist/transactions/types.d.ts.map +0 -1
  288. package/dist/transactions/types.js +0 -1
  289. package/dist/transactions/utils.d.ts +0 -10
  290. package/dist/transactions/utils.d.ts.map +0 -1
  291. package/dist/transactions/utils.js +0 -17
  292. package/dist/types.js +0 -1
  293. package/dist/utils/bigint.js +0 -29
  294. package/dist/utils/crypto.d.ts +0 -12
  295. package/dist/utils/crypto.d.ts.map +0 -1
  296. package/dist/utils/crypto.js +0 -39
  297. package/dist/utils/json-codec.js +0 -25
  298. package/dist/utils/polling.js +0 -6
  299. package/dist/utils/signature.js +0 -12
  300. package/dist/utils/time.d.ts +0 -2
  301. package/dist/utils/time.d.ts.map +0 -1
  302. package/dist/utils/time.js +0 -3
  303. package/dist/utils/validators.js +0 -70
  304. package/dist/utils/witness.d.ts +0 -11
  305. package/dist/utils/witness.d.ts.map +0 -1
  306. package/dist/utils/witness.js +0 -19
  307. package/errors.ts +0 -20
  308. package/index.ts +0 -21
  309. package/key-derivation/babyjubjub.ts +0 -11
  310. package/key-derivation/bech32.test.ts +0 -90
  311. package/key-derivation/bech32.ts +0 -124
  312. package/key-derivation/bip32.ts +0 -56
  313. package/key-derivation/bip39.ts +0 -76
  314. package/key-derivation/bytes.ts +0 -118
  315. package/key-derivation/hash.ts +0 -13
  316. package/key-derivation/index.ts +0 -7
  317. package/key-derivation/wallet-node.ts +0 -155
  318. package/keys.ts +0 -47
  319. package/prover/config.ts +0 -104
  320. package/prover/index.ts +0 -1
  321. package/prover/prover.integration.test.ts +0 -162
  322. package/prover/prover.test.ts +0 -309
  323. package/prover/prover.ts +0 -405
  324. package/prover/registry.test.ts +0 -90
  325. package/prover/registry.ts +0 -82
  326. package/schema.ts +0 -17
  327. package/setup-artifacts.sh +0 -57
  328. package/state/index.ts +0 -2
  329. package/state/merkle/hydrator.ts +0 -69
  330. package/state/merkle/index.ts +0 -12
  331. package/state/merkle/merkle-tree.test.ts +0 -50
  332. package/state/merkle/merkle-tree.ts +0 -163
  333. package/state/store/ciphertext-store.ts +0 -28
  334. package/state/store/index.ts +0 -24
  335. package/state/store/job-store.ts +0 -162
  336. package/state/store/jobs.ts +0 -64
  337. package/state/store/leaf-store.ts +0 -39
  338. package/state/store/note-store.ts +0 -177
  339. package/state/store/nullifier-store.ts +0 -39
  340. package/state/store/records.ts +0 -61
  341. package/state/store/root-store.ts +0 -34
  342. package/state/store/store.ts +0 -25
  343. package/state.test.ts +0 -235
  344. package/storage/index.ts +0 -3
  345. package/storage/indexeddb.test.ts +0 -99
  346. package/storage/indexeddb.ts +0 -235
  347. package/storage/memory.test.ts +0 -59
  348. package/storage/memory.ts +0 -93
  349. package/transactions/deposit.test.ts +0 -160
  350. package/transactions/deposit.ts +0 -227
  351. package/transactions/index.ts +0 -20
  352. package/transactions/note-sync.test.ts +0 -155
  353. package/transactions/note-sync.ts +0 -452
  354. package/transactions/reconcile.ts +0 -73
  355. package/transactions/transact.test.ts +0 -451
  356. package/transactions/transact.ts +0 -811
  357. package/transactions/types.ts +0 -141
  358. package/tsconfig.json +0 -15
  359. package/types/global.d.ts +0 -15
  360. package/types.ts +0 -24
  361. package/utils/async.ts +0 -15
  362. package/utils/bigint.ts +0 -34
  363. package/utils/crypto.test.ts +0 -69
  364. package/utils/crypto.ts +0 -58
  365. package/utils/json-codec.ts +0 -38
  366. package/utils/polling.ts +0 -6
  367. package/utils/signature.ts +0 -16
  368. package/utils/validators.test.ts +0 -64
  369. package/utils/validators.ts +0 -86
@@ -1,162 +0,0 @@
1
- import { keys } from "../../keys.js";
2
- import type { Storage } from "../../types.js";
3
- import { decodeJson, encodeJson, getJson } from "../../utils/json-codec.js";
4
- import {
5
- ensureAddress,
6
- ensureChainId,
7
- ensurePositiveInt,
8
- } from "../../utils/validators.js";
9
- import type {
10
- JobStatus,
11
- PendingDepositJob,
12
- PendingJobKind,
13
- PendingJobRecord,
14
- PendingTransactContext,
15
- PendingTransactJob,
16
- } from "../store/jobs.js";
17
- import { DEFAULT_JOB_TIMEOUT_MS } from "../store/jobs.js";
18
-
19
- const VALID_STATUSES: JobStatus[] = [
20
- "pending",
21
- "submitted",
22
- "broadcasting",
23
- "succeeded",
24
- "failed",
25
- "dead",
26
- ] as const;
27
-
28
- function assertStatus(status: string): asserts status is JobStatus {
29
- if (!VALID_STATUSES.includes(status as JobStatus)) {
30
- throw new Error(`invalid job status: ${status}`);
31
- }
32
- }
33
-
34
- function assertKind(kind: string): asserts kind is PendingJobKind {
35
- if (kind !== "deposit" && kind !== "transact") {
36
- throw new Error(`invalid job kind: ${kind}`);
37
- }
38
- }
39
-
40
- function normalizeTimestamps<T extends PendingJobRecord>(job: T): T {
41
- const createdAt =
42
- typeof job.createdAt === "number" && Number.isFinite(job.createdAt)
43
- ? job.createdAt
44
- : Date.now();
45
- const timeoutMs =
46
- typeof job.timeoutMs === "number" && job.timeoutMs > 0
47
- ? job.timeoutMs
48
- : DEFAULT_JOB_TIMEOUT_MS;
49
-
50
- return {
51
- ...job,
52
- createdAt,
53
- timeoutMs,
54
- };
55
- }
56
-
57
- function validateTransactContext(context: PendingTransactContext) {
58
- ensurePositiveInt("context index", context.index);
59
- if (!context.nullifier) {
60
- throw new Error("context nullifier is required");
61
- }
62
- const { witness } = context;
63
- if (!witness) {
64
- throw new Error("context witness is required");
65
- }
66
- ensurePositiveInt("witness leafIndex", witness.leafIndex);
67
- if (
68
- !Array.isArray(witness.pathElements) ||
69
- !Array.isArray(witness.pathIndices)
70
- ) {
71
- throw new Error("witness pathElements and pathIndices are required");
72
- }
73
- }
74
-
75
- function validateTransactJob(job: PendingTransactJob) {
76
- ensureAddress("pool address", job.poolAddress);
77
- if (!job.calldata) {
78
- throw new Error("calldata is required for transact job");
79
- }
80
- job.contexts.forEach(validateTransactContext);
81
- job.predictedOutputs.forEach((output) => {
82
- if (!output.hex) {
83
- throw new Error("predicted output hex is required");
84
- }
85
- ensurePositiveInt("predicted output index", output.index);
86
- });
87
- }
88
-
89
- function validateDepositJob(job: PendingDepositJob) {
90
- if (!job.predictedCommitment?.hex) {
91
- throw new Error("predicted commitment hex is required");
92
- }
93
- if (job.predictedCommitment.index !== undefined) {
94
- ensurePositiveInt(
95
- "predicted commitment index",
96
- job.predictedCommitment.index,
97
- );
98
- }
99
- }
100
-
101
- function validateJob(job: PendingJobRecord) {
102
- if (!job.relayId) {
103
- throw new Error("relayId is required");
104
- }
105
- ensureChainId(job.chainId);
106
- assertStatus(job.status);
107
- assertKind(job.kind);
108
- ensurePositiveInt("job createdAt", job.createdAt);
109
- ensurePositiveInt("job timeoutMs", job.timeoutMs);
110
- if (job.lastCheckedAt !== undefined) {
111
- ensurePositiveInt("job lastCheckedAt", job.lastCheckedAt);
112
- }
113
- if (job.kind === "deposit") {
114
- validateDepositJob(job);
115
- } else {
116
- validateTransactJob(job);
117
- }
118
- }
119
-
120
- function buildJobKey(relayId: string) {
121
- return keys.job(relayId);
122
- }
123
-
124
- export function createJobStore(storage: Storage) {
125
- return {
126
- async putPendingJob(job: PendingJobRecord) {
127
- const normalized = normalizeTimestamps(job);
128
- validateJob(normalized);
129
- await storage.put(
130
- buildJobKey(normalized.relayId),
131
- encodeJson(normalized),
132
- );
133
- },
134
-
135
- async getPendingJob(relayId: string) {
136
- return getJson<PendingJobRecord>(storage, buildJobKey(relayId));
137
- },
138
-
139
- async listPendingJobs(
140
- filter: {
141
- kind?: PendingJobKind;
142
- statuses?: JobStatus[];
143
- } = {},
144
- ) {
145
- const entries = await storage.iter({ prefix: "jobs:" });
146
- const statuses = filter.statuses ?? VALID_STATUSES;
147
- const filtered = entries
148
- .map(({ value }) => decodeJson<PendingJobRecord>(value))
149
- .filter(
150
- (job) =>
151
- (filter.kind === undefined || job.kind === filter.kind) &&
152
- statuses.includes(job.status),
153
- );
154
- filtered.sort((a, b) => a.createdAt - b.createdAt);
155
- return filtered;
156
- },
157
-
158
- async deletePendingJob(relayId: string) {
159
- await storage.delete(buildJobKey(relayId));
160
- },
161
- };
162
- }
@@ -1,64 +0,0 @@
1
- export type JobStatus =
2
- | "pending"
3
- | "submitted"
4
- | "broadcasting"
5
- | "succeeded"
6
- | "failed"
7
- | "dead";
8
-
9
- export type PendingJobKind = "deposit" | "transact";
10
-
11
- export type PendingJobBase = {
12
- relayId: string;
13
- kind: PendingJobKind;
14
- chainId: number;
15
- status: JobStatus;
16
- broadcasterRelayId?: string | null;
17
- txHash?: string | null;
18
- createdAt: number;
19
- lastCheckedAt?: number;
20
- timeoutMs: number;
21
- error?: string | null;
22
- };
23
-
24
- export type PendingDepositJob = PendingJobBase & {
25
- kind: "deposit";
26
- predictedCommitment: {
27
- hex: string;
28
- index?: number;
29
- root?: string;
30
- };
31
- };
32
-
33
- export type PendingTransactContext = {
34
- index: number;
35
- nullifier: string;
36
- // Serialized Merkle proof with hex-encoded nodes to keep it JSON friendly.
37
- witness: {
38
- root: string;
39
- leaf: string;
40
- pathElements: string[][];
41
- pathIndices: number[];
42
- leafIndex: number;
43
- };
44
- root: string;
45
- };
46
-
47
- export type PendingTransactOutput = {
48
- hex: string;
49
- index: number;
50
- root?: string;
51
- };
52
-
53
- export type PendingTransactJob = PendingJobBase & {
54
- kind: "transact";
55
- poolAddress: string;
56
- calldata: string;
57
- contexts: PendingTransactContext[];
58
- predictedOutputs: PendingTransactOutput[];
59
- expectedRoot?: string;
60
- };
61
-
62
- export type PendingJobRecord = PendingDepositJob | PendingTransactJob;
63
-
64
- export const DEFAULT_JOB_TIMEOUT_MS = 5 * 60 * 1000;
@@ -1,39 +0,0 @@
1
- import { keys } from "../../keys.js";
2
- import type { Storage } from "../../types.js";
3
- import { getJson, putJson } from "../../utils/json-codec.js";
4
- import { ensureChainId, ensurePositiveInt } from "../../utils/validators.js";
5
- import type { LeafRecord } from "../store/records.js";
6
-
7
- export function createLeafStore(storage: Storage) {
8
- return {
9
- /**
10
- * Cache public leaf data pulled from the on-chain Merkle tree.
11
- */
12
- async putLeaf(leaf: LeafRecord) {
13
- ensureChainId(leaf.chainId);
14
- ensurePositiveInt("leaf index", leaf.index);
15
- await putJson(storage, keys.leaf(leaf.chainId, leaf.index), leaf);
16
- },
17
-
18
- /**
19
- * Load a leaf commitment by tree index.
20
- */
21
- async getLeaf(chainId: number, index: number) {
22
- ensureChainId(chainId);
23
- ensurePositiveInt("leaf index", index);
24
- return getJson<LeafRecord>(storage, keys.leaf(chainId, index));
25
- },
26
-
27
- /**
28
- * Remove all cached leaves for a chain.
29
- */
30
- async clearLeaves(chainId: number) {
31
- ensureChainId(chainId);
32
- const prefix = `leaves:${chainId}:`;
33
- const entries = await storage.iter({ prefix });
34
- if (entries.length === 0) return;
35
- const deletions = entries.map(({ key }) => ({ del: key }));
36
- await storage.batch(deletions);
37
- },
38
- };
39
- }
@@ -1,177 +0,0 @@
1
- import { CoreError } from "../../errors.js";
2
- import { keys } from "../../keys.js";
3
- import type { BatchOp, Storage } from "../../types.js";
4
- import {
5
- decodeJson,
6
- encodeJson,
7
- getJson,
8
- type JsonRecord,
9
- } from "../../utils/json-codec.js";
10
- import {
11
- ensureBigIntString,
12
- ensureChainId,
13
- ensureMpk,
14
- ensurePositiveInt,
15
- } from "../../utils/validators.js";
16
- import type { NoteInsert, NoteRecord } from "../store/records.js";
17
-
18
- export const emptyBytes = () => new Uint8Array(0);
19
-
20
- export function createNoteStore(storage: Storage) {
21
- const persistNote = async (record: NoteRecord) => {
22
- ensureChainId(record.chainId);
23
- ensurePositiveInt("note index", record.index);
24
- ensureMpk(record.mpk);
25
- ensureBigIntString("note value", record.value);
26
- if (record.spentAt !== undefined) {
27
- ensurePositiveInt("note spentAt", record.spentAt);
28
- }
29
-
30
- const noteKey = keys.note(record.chainId, record.index);
31
- const previous = await getJson<NoteRecord>(storage, noteKey);
32
-
33
- const ops: BatchOp[] = [
34
- { put: [noteKey, encodeJson(record as JsonRecord)] },
35
- ];
36
-
37
- if (previous && previous.spentAt === undefined) {
38
- ops.push({
39
- del: keys.unspent(previous.chainId, previous.mpk, previous.index),
40
- });
41
- }
42
-
43
- if (record.spentAt === undefined) {
44
- // idx:notes:unspent functions as a set, so we only persist the sentinel bytes.
45
- ops.push({
46
- put: [
47
- keys.unspent(record.chainId, record.mpk, record.index),
48
- emptyBytes(),
49
- ],
50
- });
51
- } else {
52
- ops.push({
53
- del: keys.unspent(record.chainId, record.mpk, record.index),
54
- });
55
- }
56
-
57
- await storage.batch(ops);
58
- };
59
-
60
- const store = {
61
- /**
62
- * Upsert a note record and maintain the unspent-note index for fast balance queries.
63
- * If the note already exists and is marked as spent, preserve the spentAt field.
64
- */
65
- async putNote(note: NoteInsert) {
66
- const noteKey = keys.note(note.chainId, note.index);
67
- const existing = await getJson<NoteRecord>(storage, noteKey);
68
- if (existing?.spentAt !== undefined) {
69
- // Preserve spentAt if note was already marked as spent
70
- await persistNote({ ...note, spentAt: existing.spentAt });
71
- } else {
72
- await persistNote({ ...note });
73
- }
74
- },
75
-
76
- /**
77
- * Fetch a note by (chainId, index) if it exists in local storage.
78
- */
79
- async getNote(chainId: number, index: number) {
80
- ensureChainId(chainId);
81
- ensurePositiveInt("note index", index);
82
- return getJson<NoteRecord>(storage, keys.note(chainId, index));
83
- },
84
-
85
- /**
86
- * List notes matching optional filters (by chainId, mpk) and spent state.
87
- */
88
- async listNotes(
89
- options: {
90
- chainId?: number;
91
- mpk?: string;
92
- token?: string;
93
- includeSpent?: boolean;
94
- } = {},
95
- ) {
96
- const { chainId, mpk, token, includeSpent = true } = options;
97
- if (chainId !== undefined) {
98
- ensureChainId(chainId);
99
- }
100
- if (mpk !== undefined) {
101
- ensureMpk(mpk);
102
- }
103
-
104
- const prefix = chainId !== undefined ? `notes:${chainId}:` : "notes:";
105
- const entries = await storage.iter({ prefix });
106
- const filtered = entries
107
- .map(({ value }) => decodeJson<NoteRecord>(value))
108
- .filter(
109
- (note) =>
110
- (chainId === undefined || note.chainId === chainId) &&
111
- (mpk === undefined || note.mpk === mpk) &&
112
- (token === undefined || note.token === token) &&
113
- (includeSpent || note.spentAt === undefined),
114
- );
115
- filtered.sort((a, b) => a.index - b.index);
116
- return filtered;
117
- },
118
-
119
- /**
120
- * Mark a cached note as spent by setting its timestamp and re-indexing metadata.
121
- */
122
- async markNoteSpent(chainId: number, index: number, spentAt = Date.now()) {
123
- ensureChainId(chainId);
124
- ensurePositiveInt("note index", index);
125
- ensurePositiveInt("note spentAt", spentAt);
126
- const existing = await store.getNote(chainId, index);
127
- if (!existing) {
128
- throw new CoreError("note not found");
129
- }
130
- if (existing.spentAt === spentAt) {
131
- return existing;
132
- }
133
- const updated: NoteRecord = { ...existing, spentAt };
134
- await persistNote(updated);
135
- return updated;
136
- },
137
-
138
- /**
139
- * Drop the spent marker from a cached note, returning it to the unspent index.
140
- */
141
- async markNoteUnspent(chainId: number, index: number) {
142
- ensureChainId(chainId);
143
- ensurePositiveInt("note index", index);
144
- const existing = await store.getNote(chainId, index);
145
- if (!existing) {
146
- throw new CoreError("note not found");
147
- }
148
- if (existing.spentAt === undefined) {
149
- return existing;
150
- }
151
- const updated: NoteRecord = { ...existing };
152
- delete updated.spentAt;
153
- await persistNote(updated);
154
- return updated;
155
- },
156
-
157
- /**
158
- * Aggregate unspent note balances per token for a given master public key.
159
- */
160
- async getZkBalances(mpk: string, options: { chainId?: number } = {}) {
161
- ensureMpk(mpk);
162
- const notes = await store.listNotes({
163
- chainId: options.chainId,
164
- mpk,
165
- includeSpent: false,
166
- });
167
- const totals: Record<string, bigint> = {};
168
- for (const note of notes) {
169
- const amount = BigInt(note.value);
170
- totals[note.token] = (totals[note.token] ?? 0n) + amount;
171
- }
172
- return totals;
173
- },
174
- };
175
-
176
- return store;
177
- }
@@ -1,39 +0,0 @@
1
- import { keys } from "../../keys.js";
2
- import type { Storage } from "../../types.js";
3
- import { getJson, putJson } from "../../utils/json-codec.js";
4
- import { ensureChainId } from "../../utils/validators.js";
5
- import type { NullifierRecord } from "../store/records.js";
6
-
7
- export function createNullifierStore(storage: Storage) {
8
- return {
9
- /**
10
- * Record a nullifier observation so later syncs can de-duplicate work.
11
- */
12
- async putNullifier(nullifier: NullifierRecord) {
13
- ensureChainId(nullifier.chainId);
14
- await putJson(
15
- storage,
16
- keys.nullifier(nullifier.chainId, nullifier.nullifier),
17
- nullifier,
18
- );
19
- },
20
-
21
- /**
22
- * Lookup a previously observed nullifier by value.
23
- */
24
- async getNullifier(chainId: number, value: string) {
25
- ensureChainId(chainId);
26
- return getJson<NullifierRecord>(storage, keys.nullifier(chainId, value));
27
- },
28
-
29
- /**
30
- * Count all nullifiers stored locally for a given chain.
31
- */
32
- async countNullifiers(chainId: number): Promise<number> {
33
- ensureChainId(chainId);
34
- const prefix = `nullifiers:${chainId}:`;
35
- const entries = await storage.iter({ prefix });
36
- return entries.length;
37
- },
38
- };
39
- }
@@ -1,61 +0,0 @@
1
- /**
2
- * Locally cached metadata for a deposit note we consider owned.
3
- */
4
- export type NoteRecord = {
5
- chainId: number;
6
- /** Leaf index within the on-chain tree (zero-based, monotonic). */
7
- index: number;
8
- /** Canonical asset identifier, typically the ERC-20 address lowercased. */
9
- token: string;
10
- /** Amount expressed as an unsigned BigInt serialized to a base-10 string. */
11
- value: string;
12
- /** Commitment stored on-chain (Poseidon(note)). */
13
- commitment: string;
14
- /** Note public key derived from the master public key. */
15
- npk: string;
16
- /**
17
- * Master public key for the account (Poseidon(spendingPk.public, nullifyingKey)).
18
- * Acts as the stable account identifier when grouping notes locally.
19
- */
20
- mpk: string;
21
- /** Randomizer used when committing the note. */
22
- random: string;
23
- /** Nullifier that will be revealed when the note is spent. */
24
- nullifier: string;
25
- /** Optional timestamp when the note was marked as spent. */
26
- spentAt?: number;
27
- };
28
-
29
- // prettier-ignore
30
- export type NoteInsert = Omit<NoteRecord, 'spentAt'>;
31
-
32
- /**
33
- * Details about a nullifier observed on-chain, whether or not we own the note.
34
- */
35
- export type NullifierRecord = {
36
- chainId: number;
37
- /** Nullifier value (expected to match NoteRecord.nullifier). */
38
- nullifier: string;
39
- /** Leaf index of the corresponding note when known. */
40
- noteIndex?: number;
41
- };
42
-
43
- /**
44
- * Public information about a leaf commitment inserted into the pool's tree.
45
- */
46
- export type LeafRecord = {
47
- chainId: number;
48
- /** Leaf index inside the tree (zero-based). */
49
- index: number;
50
- /** Commitment value stored in the tree. */
51
- commitment: string;
52
- };
53
-
54
- /**
55
- * Historical merkle root snapshot used for proof construction.
56
- */
57
- export type RootRecord = {
58
- chainId: number;
59
- /** Merkle root value committed on-chain. */
60
- root: string;
61
- };
@@ -1,34 +0,0 @@
1
- import { CoreError } from "../../errors.js";
2
- import { keys, validateKey } from "../../keys.js";
3
- import type { Storage } from "../../types.js";
4
- import { getJson, putJson } from "../../utils/json-codec.js";
5
- import { ensureChainId } from "../../utils/validators.js";
6
- import type { RootRecord } from "../store/records.js";
7
-
8
- export function createRootStore(storage: Storage) {
9
- return {
10
- /**
11
- * Persist a historical root snapshot for proof generation.
12
- */
13
- async putRoot(root: RootRecord) {
14
- ensureChainId(root.chainId);
15
- if (typeof root.root !== "string" || root.root.length === 0) {
16
- throw new CoreError("root value must be a non-empty string");
17
- }
18
- const key = keys.root(root.chainId, root.root);
19
- validateKey(key);
20
- await putJson(storage, key, root);
21
- },
22
-
23
- /**
24
- * Retrieve a root snapshot by Merkle root value.
25
- */
26
- async getRoot(chainId: number, value: string) {
27
- ensureChainId(chainId);
28
- if (typeof value !== "string" || value.length === 0) {
29
- throw new CoreError("root value must be a non-empty string");
30
- }
31
- return getJson<RootRecord>(storage, keys.root(chainId, value));
32
- },
33
- };
34
- }
@@ -1,25 +0,0 @@
1
- import type { Storage } from "../../types.js";
2
- import { createCiphertextStore } from "./ciphertext-store.js";
3
- import { createJobStore } from "./job-store.js";
4
- import { createLeafStore } from "./leaf-store.js";
5
- import { createNoteStore } from "./note-store.js";
6
- import { createNullifierStore } from "./nullifier-store.js";
7
- import { createRootStore } from "./root-store.js";
8
-
9
- export function createStateStore(storage: Storage) {
10
- const notes = createNoteStore(storage);
11
- const nullifiers = createNullifierStore(storage);
12
- const leaves = createLeafStore(storage);
13
- const roots = createRootStore(storage);
14
- const ciphertexts = createCiphertextStore(storage);
15
- const jobs = createJobStore(storage);
16
-
17
- return {
18
- ...notes,
19
- ...nullifiers,
20
- ...leaves,
21
- ...roots,
22
- ...ciphertexts,
23
- ...jobs,
24
- };
25
- }