zklicensing 0.1.0-beta.2 → 0.1.0-beta.21

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 (317) hide show
  1. package/README.md +417 -201
  2. package/build/agent.d.ts +371 -0
  3. package/build/agent.d.ts.map +1 -0
  4. package/build/agent.js +553 -0
  5. package/build/agent.js.map +1 -0
  6. package/build/agentActions.d.ts +841 -0
  7. package/build/agentActions.d.ts.map +1 -0
  8. package/build/agentActions.js +581 -0
  9. package/build/agentActions.js.map +1 -0
  10. package/build/agentRecord.d.ts +83 -0
  11. package/build/agentRecord.d.ts.map +1 -0
  12. package/build/agentRecord.js +69 -0
  13. package/build/agentRecord.js.map +1 -0
  14. package/build/agentVendor.d.ts +220 -0
  15. package/build/agentVendor.d.ts.map +1 -0
  16. package/build/agentVendor.js +306 -0
  17. package/build/agentVendor.js.map +1 -0
  18. package/build/appsLookup.d.ts +14 -0
  19. package/build/appsLookup.d.ts.map +1 -0
  20. package/build/appsLookup.js +12 -0
  21. package/build/appsLookup.js.map +1 -0
  22. package/build/archiveBootstrap.d.ts +31 -5
  23. package/build/archiveBootstrap.d.ts.map +1 -0
  24. package/build/archiveBootstrap.js +140 -47
  25. package/build/archiveBootstrap.js.map +1 -0
  26. package/build/atomicJson.d.ts +5 -0
  27. package/build/atomicJson.d.ts.map +1 -0
  28. package/build/atomicJson.js +55 -0
  29. package/build/atomicJson.js.map +1 -0
  30. package/build/bindClient.d.ts +85 -0
  31. package/build/bindClient.d.ts.map +1 -0
  32. package/build/bindClient.js +187 -0
  33. package/build/bindClient.js.map +1 -0
  34. package/build/buyClient.d.ts +27 -4
  35. package/build/buyClient.d.ts.map +1 -0
  36. package/build/buyClient.js +72 -26
  37. package/build/buyClient.js.map +1 -0
  38. package/build/chainHead.d.ts +61 -0
  39. package/build/chainHead.d.ts.map +1 -0
  40. package/build/chainHead.js +88 -0
  41. package/build/chainHead.js.map +1 -0
  42. package/build/compactMerkleMap.d.ts +72 -0
  43. package/build/compactMerkleMap.d.ts.map +1 -0
  44. package/build/compactMerkleMap.js +224 -0
  45. package/build/compactMerkleMap.js.map +1 -0
  46. package/build/contractInterface.d.ts +191 -51
  47. package/build/contractInterface.d.ts.map +1 -0
  48. package/build/contractInterface.js +234 -43
  49. package/build/contractInterface.js.map +1 -0
  50. package/build/contractTypes.d.ts +9 -0
  51. package/build/contractTypes.d.ts.map +1 -0
  52. package/build/contractTypes.js +28 -0
  53. package/build/contractTypes.js.map +1 -0
  54. package/build/countryStats.d.ts +12 -3
  55. package/build/countryStats.d.ts.map +1 -0
  56. package/build/countryStats.js +87 -23
  57. package/build/countryStats.js.map +1 -0
  58. package/build/deployClient.d.ts +138 -24
  59. package/build/deployClient.d.ts.map +1 -0
  60. package/build/deployClient.js +91 -31
  61. package/build/deployClient.js.map +1 -0
  62. package/build/deviceIdentity.d.ts +68 -0
  63. package/build/deviceIdentity.d.ts.map +1 -0
  64. package/build/deviceIdentity.js +209 -0
  65. package/build/deviceIdentity.js.map +1 -0
  66. package/build/freezeClient.d.ts +26 -6
  67. package/build/freezeClient.d.ts.map +1 -0
  68. package/build/freezeClient.js +30 -6
  69. package/build/freezeClient.js.map +1 -0
  70. package/build/generationClient.d.ts +6 -3
  71. package/build/generationClient.d.ts.map +1 -0
  72. package/build/generationClient.js +25 -38
  73. package/build/generationClient.js.map +1 -0
  74. package/build/holderSecret.d.ts +70 -0
  75. package/build/holderSecret.d.ts.map +1 -0
  76. package/build/holderSecret.js +76 -0
  77. package/build/holderSecret.js.map +1 -0
  78. package/build/index.d.ts +38 -38
  79. package/build/index.d.ts.map +1 -0
  80. package/build/index.js +97 -121
  81. package/build/index.js.map +1 -0
  82. package/build/keysClient.d.ts +142 -0
  83. package/build/keysClient.d.ts.map +1 -0
  84. package/build/keysClient.js +109 -0
  85. package/build/keysClient.js.map +1 -0
  86. package/build/licenseLeaf.d.ts +76 -0
  87. package/build/licenseLeaf.d.ts.map +1 -0
  88. package/build/licenseLeaf.js +189 -0
  89. package/build/licenseLeaf.js.map +1 -0
  90. package/build/licenseRecord.d.ts +68 -5
  91. package/build/licenseRecord.d.ts.map +1 -0
  92. package/build/licenseRecord.js +26 -1
  93. package/build/licenseRecord.js.map +1 -0
  94. package/build/licenseStore.d.ts +81 -4
  95. package/build/licenseStore.d.ts.map +1 -0
  96. package/build/licenseStore.js +400 -30
  97. package/build/licenseStore.js.map +1 -0
  98. package/build/licenseTrustless.d.ts +43 -0
  99. package/build/licenseTrustless.d.ts.map +1 -0
  100. package/build/licenseTrustless.js +180 -0
  101. package/build/licenseTrustless.js.map +1 -0
  102. package/build/manifestVerify.d.ts +29 -1
  103. package/build/manifestVerify.d.ts.map +1 -0
  104. package/build/manifestVerify.js +46 -4
  105. package/build/manifestVerify.js.map +1 -0
  106. package/build/minaPriceHistory.d.ts +9 -0
  107. package/build/minaPriceHistory.d.ts.map +1 -0
  108. package/build/minaPriceHistory.js +152 -0
  109. package/build/minaPriceHistory.js.map +1 -0
  110. package/build/onChainRoot.d.ts +60 -0
  111. package/build/onChainRoot.d.ts.map +1 -0
  112. package/build/onChainRoot.js +74 -0
  113. package/build/onChainRoot.js.map +1 -0
  114. package/build/ownershipLicenseHash.d.ts +25 -0
  115. package/build/ownershipLicenseHash.d.ts.map +1 -0
  116. package/build/ownershipLicenseHash.js +29 -0
  117. package/build/ownershipLicenseHash.js.map +1 -0
  118. package/build/ownershipSignature.d.ts +153 -0
  119. package/build/ownershipSignature.d.ts.map +1 -0
  120. package/build/ownershipSignature.js +297 -0
  121. package/build/ownershipSignature.js.map +1 -0
  122. package/build/permitCheckFlow.d.ts +155 -0
  123. package/build/permitCheckFlow.d.ts.map +1 -0
  124. package/build/permitCheckFlow.js +196 -0
  125. package/build/permitCheckFlow.js.map +1 -0
  126. package/build/permitDescribe.d.ts +124 -0
  127. package/build/permitDescribe.d.ts.map +1 -0
  128. package/build/permitDescribe.js +324 -0
  129. package/build/permitDescribe.js.map +1 -0
  130. package/build/permitInterface.d.ts +85 -0
  131. package/build/permitInterface.d.ts.map +1 -0
  132. package/build/permitInterface.js +121 -0
  133. package/build/permitInterface.js.map +1 -0
  134. package/build/permitMessages.d.ts +313 -0
  135. package/build/permitMessages.d.ts.map +1 -0
  136. package/build/permitMessages.js +462 -0
  137. package/build/permitMessages.js.map +1 -0
  138. package/build/permitProof.d.ts +108 -0
  139. package/build/permitProof.d.ts.map +1 -0
  140. package/build/permitProof.js +202 -0
  141. package/build/permitProof.js.map +1 -0
  142. package/build/permitRegistryRebuild.d.ts +166 -0
  143. package/build/permitRegistryRebuild.d.ts.map +1 -0
  144. package/build/permitRegistryRebuild.js +347 -0
  145. package/build/permitRegistryRebuild.js.map +1 -0
  146. package/build/permitTree.d.ts +88 -0
  147. package/build/permitTree.d.ts.map +1 -0
  148. package/build/permitTree.js +230 -0
  149. package/build/permitTree.js.map +1 -0
  150. package/build/permitsClient.d.ts +680 -0
  151. package/build/permitsClient.d.ts.map +1 -0
  152. package/build/permitsClient.js +597 -0
  153. package/build/permitsClient.js.map +1 -0
  154. package/build/proveErrors.d.ts +50 -0
  155. package/build/proveErrors.d.ts.map +1 -0
  156. package/build/proveErrors.js +76 -0
  157. package/build/proveErrors.js.map +1 -0
  158. package/build/receipt.d.ts +95 -0
  159. package/build/receipt.d.ts.map +1 -0
  160. package/build/receipt.js +84 -0
  161. package/build/receipt.js.map +1 -0
  162. package/build/refundClient.d.ts +7 -12
  163. package/build/refundClient.d.ts.map +1 -0
  164. package/build/refundClient.js +52 -21
  165. package/build/refundClient.js.map +1 -0
  166. package/build/registryTrustVerify.d.ts +78 -0
  167. package/build/registryTrustVerify.d.ts.map +1 -0
  168. package/build/registryTrustVerify.js +129 -0
  169. package/build/registryTrustVerify.js.map +1 -0
  170. package/build/releaseClient.d.ts +26 -0
  171. package/build/releaseClient.d.ts.map +1 -0
  172. package/build/releaseClient.js +65 -0
  173. package/build/releaseClient.js.map +1 -0
  174. package/build/renewClient.d.ts +28 -2
  175. package/build/renewClient.d.ts.map +1 -0
  176. package/build/renewClient.js +31 -27
  177. package/build/renewClient.js.map +1 -0
  178. package/build/seatName.d.ts +8 -0
  179. package/build/seatName.d.ts.map +1 -0
  180. package/build/seatName.js +136 -0
  181. package/build/seatName.js.map +1 -0
  182. package/build/slotConstants.d.ts +15 -0
  183. package/build/slotConstants.d.ts.map +1 -0
  184. package/build/slotConstants.js +17 -0
  185. package/build/slotConstants.js.map +1 -0
  186. package/build/statsClient.d.ts +61 -7
  187. package/build/statsClient.d.ts.map +1 -0
  188. package/build/statsClient.js +44 -5
  189. package/build/statsClient.js.map +1 -0
  190. package/build/successorChain.d.ts +35 -0
  191. package/build/successorChain.d.ts.map +1 -0
  192. package/build/successorChain.js +69 -0
  193. package/build/successorChain.js.map +1 -0
  194. package/build/taxId.d.ts +44 -0
  195. package/build/taxId.d.ts.map +1 -0
  196. package/build/taxId.js +212 -0
  197. package/build/taxId.js.map +1 -0
  198. package/build/trustAnchorVerify.d.ts +49 -0
  199. package/build/trustAnchorVerify.d.ts.map +1 -0
  200. package/build/trustAnchorVerify.js +432 -0
  201. package/build/trustAnchorVerify.js.map +1 -0
  202. package/build/trustAnchorVerifyCli.d.ts +45 -0
  203. package/build/trustAnchorVerifyCli.d.ts.map +1 -0
  204. package/build/trustAnchorVerifyCli.js +235 -0
  205. package/build/trustAnchorVerifyCli.js.map +1 -0
  206. package/build/txBroadcastClient.d.ts +26 -0
  207. package/build/txBroadcastClient.d.ts.map +1 -0
  208. package/build/txBroadcastClient.js +20 -0
  209. package/build/txBroadcastClient.js.map +1 -0
  210. package/build/unbindClient.d.ts +77 -0
  211. package/build/unbindClient.d.ts.map +1 -0
  212. package/build/unbindClient.js +169 -0
  213. package/build/unbindClient.js.map +1 -0
  214. package/build/units.d.ts +28 -0
  215. package/build/units.d.ts.map +1 -0
  216. package/build/units.js +65 -0
  217. package/build/units.js.map +1 -0
  218. package/build/vatRates.d.ts +54 -0
  219. package/build/vatRates.d.ts.map +1 -0
  220. package/build/vatRates.js +82 -0
  221. package/build/vatRates.js.map +1 -0
  222. package/build/vatRatesData.d.ts +5 -0
  223. package/build/vatRatesData.d.ts.map +1 -0
  224. package/build/vatRatesData.js +42 -0
  225. package/build/vatRatesData.js.map +1 -0
  226. package/build/vendorAccountClient.d.ts +66 -0
  227. package/build/vendorAccountClient.d.ts.map +1 -0
  228. package/build/vendorAccountClient.js +47 -0
  229. package/build/vendorAccountClient.js.map +1 -0
  230. package/build/verifyCore.d.ts +4 -1
  231. package/build/verifyCore.d.ts.map +1 -0
  232. package/build/verifyCore.js +21 -10
  233. package/build/verifyCore.js.map +1 -0
  234. package/build/verifyOffline.d.ts +85 -0
  235. package/build/verifyOffline.d.ts.map +1 -0
  236. package/build/verifyOffline.js +139 -0
  237. package/build/verifyOffline.js.map +1 -0
  238. package/build/verifyService.d.ts.map +1 -0
  239. package/build/verifyService.js +571 -501
  240. package/build/verifyService.js.map +1 -0
  241. package/build/verifyWithAncestors.d.ts +19 -0
  242. package/build/verifyWithAncestors.d.ts.map +1 -0
  243. package/build/verifyWithAncestors.js +127 -0
  244. package/build/verifyWithAncestors.js.map +1 -0
  245. package/build/zipToState.d.ts +2 -0
  246. package/build/zipToState.d.ts.map +1 -0
  247. package/build/zipToState.js +94 -0
  248. package/build/zipToState.js.map +1 -0
  249. package/package.json +141 -16
  250. package/src/agent.ts +682 -0
  251. package/src/agentActions.ts +637 -0
  252. package/src/agentRecord.ts +121 -0
  253. package/src/agentVendor.ts +356 -0
  254. package/src/appsLookup.ts +48 -0
  255. package/src/archiveBootstrap.ts +386 -0
  256. package/src/atomicJson.ts +53 -0
  257. package/src/bindClient.ts +297 -0
  258. package/src/buyClient.ts +344 -0
  259. package/src/chainHead.ts +106 -0
  260. package/src/compactMerkleMap.ts +222 -0
  261. package/src/contractInterface.ts +465 -0
  262. package/src/contractTypes.ts +31 -0
  263. package/src/countryStats.ts +200 -0
  264. package/src/deployClient.ts +517 -0
  265. package/src/deviceIdentity.ts +249 -0
  266. package/src/freezeClient.ts +162 -0
  267. package/src/generationClient.ts +145 -0
  268. package/src/holderSecret.ts +91 -0
  269. package/src/index.ts +362 -0
  270. package/src/keysClient.ts +135 -0
  271. package/src/licenseLeaf.ts +221 -0
  272. package/src/licenseRecord.ts +190 -0
  273. package/src/licenseStore.ts +767 -0
  274. package/src/licenseTrustless.ts +207 -0
  275. package/src/manifestVerify.ts +164 -0
  276. package/src/minaPriceHistory.ts +166 -0
  277. package/src/onChainRoot.ts +90 -0
  278. package/src/ownershipLicenseHash.ts +31 -0
  279. package/src/ownershipSignature.ts +421 -0
  280. package/src/permitCheckFlow.ts +227 -0
  281. package/src/permitDescribe.ts +362 -0
  282. package/src/permitInterface.ts +146 -0
  283. package/src/permitMessages.ts +572 -0
  284. package/src/permitProof.ts +230 -0
  285. package/src/permitRegistryRebuild.ts +423 -0
  286. package/src/permitTree.ts +232 -0
  287. package/src/permitsClient.ts +746 -0
  288. package/src/proveErrors.ts +98 -0
  289. package/src/receipt.ts +156 -0
  290. package/src/refundClient.ts +177 -0
  291. package/src/registryTrustVerify.ts +192 -0
  292. package/src/releaseClient.ts +101 -0
  293. package/src/renewClient.ts +234 -0
  294. package/src/seatName.ts +165 -0
  295. package/src/slotConstants.ts +18 -0
  296. package/src/statsClient.ts +249 -0
  297. package/src/successorChain.ts +87 -0
  298. package/src/taxId.ts +227 -0
  299. package/src/trustAnchorVerify.ts +550 -0
  300. package/src/trustAnchorVerifyCli.ts +234 -0
  301. package/src/txBroadcastClient.ts +48 -0
  302. package/src/unbindClient.ts +233 -0
  303. package/src/units.ts +74 -0
  304. package/src/vatRates.ts +100 -0
  305. package/src/vatRatesData.ts +43 -0
  306. package/src/vendorAccountClient.ts +120 -0
  307. package/src/verifyCore.ts +100 -0
  308. package/src/verifyOffline.ts +212 -0
  309. package/src/verifyService.ts +982 -0
  310. package/src/verifyWithAncestors.ts +163 -0
  311. package/src/zipToState.ts +94 -0
  312. package/build/licenseProof.d.ts +0 -26
  313. package/build/licenseProof.js +0 -41
  314. package/build/migrateClient.d.ts +0 -43
  315. package/build/migrateClient.js +0 -47
  316. package/build/ownership.d.ts +0 -34
  317. package/build/ownership.js +0 -147
@@ -0,0 +1,187 @@
1
+ // Copyright (c) 2025-2026 zkLicensing project developers
2
+ // SPDX-License-Identifier: BUSL-1.1
3
+ /**
4
+ * bindClient.ts
5
+ *
6
+ * Browser-safe client for the non-custodial device-binding flow.
7
+ * Mirrors `buyClient.ts` in shape: the caller drives the wallet step
8
+ * between `proveBind` and `confirmBind`, so the SDK stays
9
+ * wallet-agnostic.
10
+ *
11
+ * Binding is a root-mutating tx on the same per-app in-flight slot
12
+ * that buy / renew / refund use — a concurrent request
13
+ * against the same app returns 409 `ProveQueueBusyError`, which the
14
+ * caller must catch and retry (see the retry loop in
15
+ * examples/src/bind.ts).
16
+ *
17
+ * The signature scheme is Pallas Schnorr and is re-verified INSIDE
18
+ * the on-chain circuit via `Signature.verify()` — a compromised
19
+ * backend cannot forge a bind because it can't produce a signature
20
+ * that satisfies the in-circuit check.
21
+ */
22
+ import { Field } from 'o1js';
23
+ import { readErrorBody, tryQueueBusyFromBody, keeperError } from './proveErrors.js';
24
+ import { encodePublicKey, encodeSignature, signBinding, slotStateOf } from './ownershipSignature.js';
25
+ function baseUrl(url) {
26
+ return url.replace(/\/+$/, '');
27
+ }
28
+ async function readJsonError(resp) {
29
+ try {
30
+ const body = await resp.json();
31
+ if (typeof body.error === 'string')
32
+ return body.error;
33
+ }
34
+ catch { /* body wasn't JSON */ }
35
+ return `HTTP ${resp.status}`;
36
+ }
37
+ // Sample a fresh replay-defense nonce. WebCrypto is required for
38
+ // `crypto.getRandomValues` — same runtime floor as the rest of the
39
+ // signing surface (Node ≥ 20, modern browsers).
40
+ function freshNonce() {
41
+ const crypto = globalThis.crypto;
42
+ if (!crypto?.getRandomValues) {
43
+ throw new Error('crypto.getRandomValues unavailable — bindClient requires Node ≥ 20 or a modern browser');
44
+ }
45
+ const bytes = new Uint8Array(16); // 128 bits
46
+ crypto.getRandomValues(bytes);
47
+ let n = 0n;
48
+ for (const b of bytes)
49
+ n = (n << 8n) | BigInt(b);
50
+ // Field(n) is safe: 128 bits << the Pallas base field size, so no
51
+ // modular reduction ever fires here.
52
+ return Field(n).toString();
53
+ }
54
+ // Read the target slot's current packed state and the license's expiry from
55
+ // the keeper's license mirror. Shared with unbindClient.
56
+ export async function fetchBindingState(opts) {
57
+ const fetcher = opts.fetcher ?? fetch;
58
+ const url = `${baseUrl(opts.keeperUrl)}/license/${encodeURIComponent(opts.zkAppAddress)}/`
59
+ + `${encodeURIComponent(opts.licenseHash)}?network=${encodeURIComponent(opts.network)}`;
60
+ const resp = await fetcher(url, { signal: opts.signal });
61
+ if (!resp.ok)
62
+ throw new Error(`License lookup failed: ${await readJsonError(resp)}`);
63
+ const body = await resp.json();
64
+ if (!body.found || !body.expirySlot)
65
+ throw new Error('License not found on the keeper.');
66
+ return {
67
+ slotState: slotStateOf((body.devices ?? [])[opts.slotIndex]),
68
+ expirySlot: body.expirySlot,
69
+ };
70
+ }
71
+ // POST /prove/bind — backend compiles + proves the bind tx.
72
+ //
73
+ // Retry contract:
74
+ // - 409 `proving queue busy` → `ProveQueueBusyError`; caller catches
75
+ // and retries with `retryAfterMs` cadence (see examples/src/bind.ts).
76
+ // - Any other non-2xx surfaces the body's `error` field verbatim.
77
+ export async function proveBind(req) {
78
+ const fetcher = req.fetcher ?? fetch;
79
+ const nonce = req.nonce ?? freshNonce();
80
+ const { slotState, expirySlot } = (req.slotState !== undefined && req.expirySlot !== undefined)
81
+ ? { slotState: req.slotState, expirySlot: req.expirySlot }
82
+ : await fetchBindingState({ ...req, fetcher });
83
+ const ownerPubKey = req.ownerPrivateKey.toPublicKey();
84
+ const ownerSig = signBinding(req.ownerPrivateKey, {
85
+ licenseHash: req.licenseHash,
86
+ deviceCommitment: req.deviceCommitment,
87
+ slotIndex: req.slotIndex,
88
+ nonce,
89
+ slotState,
90
+ expirySlot,
91
+ zkAppAddress: req.zkAppAddress,
92
+ });
93
+ const resp = await fetcher(`${baseUrl(req.keeperUrl)}/prove/bind`, {
94
+ method: 'POST',
95
+ headers: { 'Content-Type': 'application/json' },
96
+ signal: req.signal,
97
+ body: JSON.stringify({
98
+ licenseHash: req.licenseHash,
99
+ buyerAddress: req.buyerAddress,
100
+ zkAppAddress: req.zkAppAddress,
101
+ network: req.network,
102
+ ownerPubKey: encodePublicKey(ownerPubKey),
103
+ ownerSig: encodeSignature(ownerSig),
104
+ deviceCommitment: req.deviceCommitment,
105
+ slotIndex: req.slotIndex,
106
+ nonce,
107
+ }),
108
+ });
109
+ if (!resp.ok) {
110
+ const parsed = await readErrorBody(resp);
111
+ const queueBusy = tryQueueBusyFromBody(parsed);
112
+ if (queueBusy)
113
+ throw queueBusy;
114
+ throw keeperError(parsed);
115
+ }
116
+ return await resp.json();
117
+ }
118
+ // POST /confirm/bind — pass-through no-op that clears the staged
119
+ // intent on the backend. Call this only after the wallet has returned
120
+ // a real txHash; a cancelled or dropped signature leaves the intent
121
+ // to expire on its own.
122
+ export async function confirmBind(req) {
123
+ const fetcher = req.fetcher ?? fetch;
124
+ const resp = await fetcher(`${baseUrl(req.keeperUrl)}/confirm/bind`, {
125
+ method: 'POST',
126
+ headers: { 'Content-Type': 'application/json' },
127
+ signal: req.signal,
128
+ body: JSON.stringify({
129
+ zkAppAddress: req.zkAppAddress,
130
+ licenseHash: req.licenseHash,
131
+ txHash: req.txHash,
132
+ }),
133
+ });
134
+ if (!resp.ok)
135
+ throw new Error(await readJsonError(resp));
136
+ return { ok: true };
137
+ }
138
+ // Polls GET /license/:zkAppAddress/:licenseHash until the bind has
139
+ // landed on-chain (the target slot's commitment matches the expected
140
+ // value) or the timeout fires. Mirrors pollRenewStatus in shape.
141
+ export async function pollBindStatus(opts) {
142
+ const fetcher = opts.fetcher ?? fetch;
143
+ const interval = opts.intervalMs ?? 10_000;
144
+ const timeout = opts.timeoutMs ?? 20 * 60 * 1000;
145
+ const startedAt = Date.now();
146
+ const deadline = startedAt + timeout;
147
+ const url = `${baseUrl(opts.keeperUrl)}/license/${encodeURIComponent(opts.zkAppAddress)}/${encodeURIComponent(opts.licenseHash)}?network=${opts.network}`;
148
+ while (true) {
149
+ if (opts.signal?.aborted)
150
+ throw new Error('pollBindStatus aborted');
151
+ let status = { status: 'unknown', elapsedMs: Date.now() - startedAt };
152
+ try {
153
+ const resp = await fetcher(url, { signal: opts.signal });
154
+ if (resp.ok) {
155
+ const body = await resp.json();
156
+ if (body.found) {
157
+ const slot = (body.devices ?? []).find(d => d.slotIndex === opts.slotIndex);
158
+ const commitment = slot?.deviceCommitment ?? '0';
159
+ if (commitment === opts.deviceCommitment) {
160
+ status = {
161
+ status: 'confirmed',
162
+ slotIndex: opts.slotIndex,
163
+ deviceCommitment: commitment,
164
+ elapsedMs: Date.now() - startedAt,
165
+ };
166
+ }
167
+ else {
168
+ status = {
169
+ status: 'pending',
170
+ slotIndex: opts.slotIndex,
171
+ deviceCommitment: commitment,
172
+ elapsedMs: Date.now() - startedAt,
173
+ };
174
+ }
175
+ }
176
+ }
177
+ }
178
+ catch { /* transient network — try again on next tick */ }
179
+ opts.onTick?.(status);
180
+ if (status.status === 'confirmed')
181
+ return status;
182
+ if (Date.now() >= deadline)
183
+ return status;
184
+ await new Promise((r) => setTimeout(r, interval));
185
+ }
186
+ }
187
+ //# sourceMappingURL=bindClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bindClient.js","sourceRoot":"","sources":["../src/bindClient.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,oCAAoC;AAEpC;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,KAAK,EAAoC,MAAM,MAAM,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpF,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAiErG,SAAS,OAAO,C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,SAAS,EAAE,IAAI,CAAC,SAAS;4BACzB,gBAAgB,EAAE,UAAU;4BAC5B,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS;yBAClC,CAAC;oBACJ,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;QAAC,MAAM,CAAC,CAAC,gDAAgD,CAAC,CAAC;QAC5D,IAAI,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC;QACtB,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW;YAAE,OAAO,MAAM,CAAC;QACjD,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,QAAQ;YAAE,OAAO,MAAM,CAAC;QAC1C,MAAM,IAAI,OAAO,CAAO,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;IAC1D,CAAC;AACH,CAAC"}
@@ -1,15 +1,23 @@
1
+ import type { PurchaseReceipt } from './receipt.js';
2
+ import type { PrivateKey, PublicKey } from 'o1js';
1
3
  export type BuyIdentity = {
2
- secretHash: string;
4
+ publicKey: PublicKey;
5
+ publicKeyBase58: string;
6
+ privateKey: PrivateKey;
3
7
  licenseHash: string;
4
8
  };
5
- export declare function deriveBuyIdentity(passphrase: string): BuyIdentity;
9
+ export declare function deriveBuyIdentity(passphrase: string, zkAppAddress: string): Promise<BuyIdentity>;
6
10
  export type ProveBuyRequest = {
11
+ agentConsent?: unknown;
7
12
  keeperUrl: string;
8
13
  licenseHash: string;
9
14
  buyerAddress: string;
10
15
  zkAppAddress: string;
11
- duration: 0 | 1 | 2;
16
+ ownerPubKey: string;
17
+ network: 'devnet' | 'mainnet';
18
+ duration: 0 | 1 | 2 | 3;
12
19
  buyerCountry: string;
20
+ buyerZip?: string;
13
21
  refundWaiverAccepted?: boolean;
14
22
  freezeAfterSlot?: number | null;
15
23
  fetcher?: typeof fetch;
@@ -25,6 +33,7 @@ export type ProveBuyResponse = {
25
33
  duration: number;
26
34
  durationLabel: string;
27
35
  generation: number;
36
+ appKey: string;
28
37
  };
29
38
  export type ConfirmBuyRequest = {
30
39
  keeperUrl: string;
@@ -35,15 +44,29 @@ export type ConfirmBuyRequest = {
35
44
  signal?: AbortSignal;
36
45
  };
37
46
  export type BuyStatus = {
38
- status: 'unknown' | 'pending' | 'confirmed' | 'failed';
47
+ status: 'unknown' | 'pending' | 'landed' | 'timeout' | 'failed';
48
+ failureReason?: string;
39
49
  txHash?: string;
40
50
  landedAtBlockHeight?: number;
41
51
  landedAtSlot?: number;
42
52
  elapsedMs?: number;
43
53
  };
54
+ /**
55
+ * The buyer's country guessed from their IP address (keeper `GET /geo/country`),
56
+ * to pre-fill the country field. Null when the keeper has no geolocation
57
+ * database, the address has no country, or the keeper is unreachable. The
58
+ * buyer's own choice is what `proveBuy` sends; the keeper separately records
59
+ * the country it sees as VAT evidence, and never the address.
60
+ */
61
+ export declare function detectBuyerCountry(keeperUrl: string, fetcher?: typeof fetch): Promise<string | null>;
44
62
  export declare function proveBuy(req: ProveBuyRequest): Promise<ProveBuyResponse>;
63
+ /**
64
+ * Returns the buyer's receipt (zklicensing/receipt) when the keeper issued
65
+ * one: put it in the license file as `receipt` (Vendor Terms §7.1).
66
+ */
45
67
  export declare function confirmBuy(req: ConfirmBuyRequest): Promise<{
46
68
  ok: true;
69
+ receipt?: PurchaseReceipt;
47
70
  }>;
48
71
  export type PollBuyStatusOptions = {
49
72
  keeperUrl: string;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"buyClient.d.ts","sourceRoot":"","sources":["../src/buyClient.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAKpD,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAElD,MAAM,MAAM,WAAW,GAAG;IAMxB,SAAS,EAAQ,SAAS,CAAC;IAG3B,eAAe,EAAE,MAAM,CAAC;IAKxB,UAAU,EAAO,UAAU,CAAC;IA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-1,31 +1,42 @@
1
1
  // Copyright (c) 2025-2026 zkLicensing project developers
2
- // All rights reserved.
2
+ // SPDX-License-Identifier: BUSL-1.1
3
3
  /**
4
4
  * buyClient.ts
5
5
  *
6
- * Browser-safe client for the buy flow. Exposes the three keeper roundtrips
6
+ * Browser-safe client for the buy flow. Exposes the three backend roundtrips
7
7
  * (derive identity → prove → confirm) as small, individually testable
8
8
  * functions plus a `pollBuyStatus` helper. Deliberately does NOT talk to a
9
9
  * wallet — the caller drives the wallet step between `proveBuy` and
10
10
  * `confirmBuy`, so the SDK stays wallet-agnostic and can be reused by CLI
11
11
  * tools, alternate UIs, and third-party integrators.
12
12
  *
13
- * Peer dep: o1js (for Poseidon + Encoding). Consumers of ./buy must have
14
- * o1js in their dependency graph.
13
+ * Peer dep: o1js (for Poseidon + Pallas PrivateKey/PublicKey/Signature).
14
+ * The ownership-signing surface (ownershipSignature.ts) uses Mina-native
15
+ * Schnorr so the passphrase-derived keypair also authorizes /prove/bind
16
+ * and /prove/unbind — verified IN-CIRCUIT there via Signature.verify(),
17
+ * which requires o1js. Every buy-flow consumer already ships o1js for the
18
+ * buy-witness path, so this is not new cost.
15
19
  */
16
- import { Encoding, Poseidon } from 'o1js';
17
20
  import { resolveFreezeState } from './freezeClient.js';
18
21
  import { REFUND_WINDOW_N, TOLERANCE_WINDOW_N } from './contractInterface.js';
19
- // Derive the two hashes the buy flow keys off. Deterministic in the
20
- // passphrase — same passphrase always maps to the same licenseHash on a
21
- // given zkApp, so a buyer's passphrase choice locks in their license
22
- // identity from day one. Only `licenseHash` is sent to the keeper (see
23
- // proveBuy); `secretHash` stays on the buyer's device and is used only by
24
- // the client-side LicenseOwnershipProgram challenge/response.
25
- export function deriveBuyIdentity(passphrase) {
26
- const secretHash = Poseidon.hash(Encoding.stringToFields(passphrase));
27
- const licenseHash = Poseidon.hash([secretHash]);
28
- return { secretHash: secretHash.toString(), licenseHash: licenseHash.toString() };
22
+ import { readErrorBody, tryQueueBusyFromBody, keeperError } from './proveErrors.js';
23
+ import { deriveOwnershipKeypair } from './ownershipSignature.js';
24
+ import { licenseHashFromPubkey } from './ownershipLicenseHash.js';
25
+ // Derive the ownership keypair + licenseHash the buy flow keys off.
26
+ // Deterministic in (passphrase, zkAppAddress) — same inputs always map
27
+ // to the same keypair (via HKDF-SHA256) and therefore the same
28
+ // licenseHash on every device, so a buyer can re-derive their license
29
+ // identity anywhere from the passphrase and the app address. Binding
30
+ // the derivation to the app address means a leaked passphrase for app A
31
+ // cannot be replayed as a valid activation for app B.
32
+ //
33
+ // HKDF still needs WebCrypto (Node ≥20, Chromium 137+, Safari 17+,
34
+ // Firefox 130+); the scalar reduction and Pallas group operations are
35
+ // pure JS through o1js. Async because subtle.deriveBits is async.
36
+ export async function deriveBuyIdentity(passphrase, zkAppAddress) {
37
+ const { privateKey, publicKey, publicKeyBase58 } = await deriveOwnershipKeypair(passphrase, zkAppAddress);
38
+ const licenseHash = licenseHashFromPubkey(publicKey);
39
+ return { publicKey, publicKeyBase58, privateKey, licenseHash };
29
40
  }
30
41
  function baseUrl(url) {
31
42
  return url.replace(/\/+$/, '');
@@ -39,11 +50,11 @@ async function readJsonError(resp) {
39
50
  catch { /* body wasn't JSON */ }
40
51
  return `HTTP ${resp.status}`;
41
52
  }
42
- // POST /prove/buy — keeper compiles + proves the buy tx.
53
+ // POST /prove/buy — backend compiles + proves the buy tx.
43
54
  // The buyer's wallet then signs the returned provenTxJson.
44
55
  //
45
56
  // Pre-network guards:
46
- // - salesFrozen === true → refuse immediately (keeper would 423 anyway).
57
+ // - salesFrozen === true → refuse immediately (backend would 423 anyway).
47
58
  // - freezeAfterSlot announced AND this buy's 14-day refund window would
48
59
  // extend past the freeze slot AND refundWaiverAccepted !== true →
49
60
  // refuse (buyer must explicitly accept that the scheduled hardfork
@@ -58,16 +69,36 @@ async function readJsonError(resp) {
58
69
  // delivery) or forced by a freeze overlap.
59
70
  // - refundWaiverAccepted !== true → POST /prove/buy (standard escrow path
60
71
  // with a 14-day refund window).
72
+ /**
73
+ * The buyer's country guessed from their IP address (keeper `GET /geo/country`),
74
+ * to pre-fill the country field. Null when the keeper has no geolocation
75
+ * database, the address has no country, or the keeper is unreachable. The
76
+ * buyer's own choice is what `proveBuy` sends; the keeper separately records
77
+ * the country it sees as VAT evidence, and never the address.
78
+ */
79
+ export async function detectBuyerCountry(keeperUrl, fetcher = fetch) {
80
+ try {
81
+ const res = await fetcher(`${keeperUrl.replace(/\/$/, '')}/geo/country`, { cache: 'no-store' });
82
+ if (!res.ok)
83
+ return null;
84
+ const body = await res.json();
85
+ return typeof body.country === 'string' && /^[A-Z]{2}$/.test(body.country) ? body.country : null;
86
+ }
87
+ catch {
88
+ return null;
89
+ }
90
+ }
61
91
  export async function proveBuy(req) {
62
92
  const fetcher = req.fetcher ?? fetch;
63
93
  const freeze = await resolveFreezeState({
64
94
  keeperUrl: req.keeperUrl,
95
+ network: req.network,
65
96
  freezeAfterSlot: req.freezeAfterSlot,
66
97
  fetcher,
67
98
  signal: req.signal,
68
99
  });
69
100
  if (freeze.salesFrozen) {
70
- throw new Error('Sales frozen — new buys refused ahead of scheduled hardfork migration');
101
+ throw new Error('Sales frozen — new buys refused ahead of a scheduled hardfork');
71
102
  }
72
103
  let windowOverlapsFreeze = false;
73
104
  if (freeze.freezeAfterSlot !== null) {
@@ -95,20 +126,28 @@ export async function proveBuy(req) {
95
126
  licenseHash: req.licenseHash,
96
127
  buyerAddress: req.buyerAddress,
97
128
  zkAppAddress: req.zkAppAddress,
129
+ ownerPubKey: req.ownerPubKey,
130
+ network: req.network,
98
131
  duration: req.duration,
99
132
  buyerCountry: req.buyerCountry,
133
+ ...(req.buyerZip ? { buyerZip: req.buyerZip } : {}),
100
134
  ...(req.refundWaiverAccepted === true ? { refundWaiverAccepted: true } : {}),
135
+ ...(req.refundWaiverAccepted === true && req.agentConsent ? { agentConsent: req.agentConsent } : {}),
101
136
  }),
102
137
  });
103
138
  if (!resp.ok) {
104
139
  // Distinct guidance for the no-escrow path: a 404 here means the
105
140
  // vendor's zkApp was deployed before the no-escrow method existed. Fix
106
- // is a redeploy through the hardfork-migration flow (§12.13); until
141
+ // is a redeploy (§12.13); until
107
142
  // then the escrowless (waiver) path is unavailable on this vendor.
108
143
  if (useNoEscrow && resp.status === 404) {
109
- throw new Error('Keeper does not offer /prove/buy-no-escrow — this vendor\'s zkApp does not support the escrowless (waiver) path; vendor must redeploy via the hardfork-migration flow');
144
+ throw new Error('Backend does not offer /prove/buy-no-escrow — this vendor\'s zkApp does not support the escrowless (waiver) path; vendor must redeploy');
110
145
  }
111
- throw new Error(await readJsonError(resp));
146
+ const parsed = await readErrorBody(resp);
147
+ const queueBusy = tryQueueBusyFromBody(parsed);
148
+ if (queueBusy)
149
+ throw queueBusy;
150
+ throw keeperError(parsed);
112
151
  }
113
152
  return await resp.json();
114
153
  }
@@ -116,6 +155,10 @@ export async function proveBuy(req) {
116
155
  // into a persisted (pending: true) LicenseRecord. Call this only after the
117
156
  // wallet has actually returned a txHash — a cancelled or dropped signature
118
157
  // leaves the intent to expire on its own.
158
+ /**
159
+ * Returns the buyer's receipt (zklicensing/receipt) when the keeper issued
160
+ * one: put it in the license file as `receipt` (Vendor Terms §7.1).
161
+ */
119
162
  export async function confirmBuy(req) {
120
163
  const fetcher = req.fetcher ?? fetch;
121
164
  const resp = await fetcher(`${baseUrl(req.keeperUrl)}/confirm/buy`, {
@@ -130,11 +173,14 @@ export async function confirmBuy(req) {
130
173
  });
131
174
  if (!resp.ok)
132
175
  throw new Error(await readJsonError(resp));
133
- return { ok: true };
176
+ const body = await resp.json().catch(() => ({}));
177
+ return { ok: true, ...(body?.receipt ? { receipt: body.receipt } : {}) };
134
178
  }
135
- // Polls GET /buy/status until it returns a terminal state ('confirmed' or
136
- // 'failed') or the timeout fires. Terminal state is resolved via the
137
- // returned status; 'unknown' just means the keeper hasn't seen the tx yet.
179
+ // Polls GET /buy/status until it returns a terminal state ('landed' — success,
180
+ // tx included on-chain; 'failed' — included but failed on chain; or 'timeout'
181
+ // — watch deadline passed without inclusion)
182
+ // or the local poll timeout fires. 'unknown' just means the backend hasn't seen
183
+ // a watch record yet (confirmBuy still landing, or retention window expired).
138
184
  export async function pollBuyStatus(opts) {
139
185
  const fetcher = opts.fetcher ?? fetch;
140
186
  const interval = opts.intervalMs ?? 3_000;
@@ -152,7 +198,7 @@ export async function pollBuyStatus(opts) {
152
198
  }
153
199
  catch { /* transient network — try again on next tick */ }
154
200
  opts.onTick?.(status);
155
- if (status.status === 'confirmed' || status.status === 'failed')
201
+ if (status.status === 'landed' || status.status === 'timeout' || status.status === 'failed')
156
202
  return status;
157
203
  if (Date.now() >= deadline)
158
204
  return status;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"buyClient.js","sourceRoot":"","sources":["../src/buyClient.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,oCAAoC;AAEpC;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpF,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAyBlE,oEAAoE;AACpE,uEAAuE;AACvE,+DAA+D;AAC/D,sEAAsE;AACtE,qEAAqE;AACrE,wEAAwE;AACxE,sDAAsD;AACtD,EAAE;AACF,mEAAmE;AACnE,sEAAsE;AACtE,kEAAkE;AAClE,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,UAAoB,EACpB,YAAoB;IAEpB,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,GAAG,MAAM,sBAAsB,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;IAC1G,MAAM,WAAW,GAAG,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,OAAO,MAAM,CAAC;QAC3G,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,QAAQ;YAAE,OAAO,MAAM,CAAC;QAC1C,MAAM,IAAI,OAAO,CAAO,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;IAC1D,CAAC;AACH,CAAC"}
@@ -0,0 +1,61 @@
1
+ /** `state`: the account's whole app state (decimal strings). */
2
+ export type ChainHead = {
3
+ root: string;
4
+ slot: number;
5
+ state: string[];
6
+ };
7
+ export declare function readChainHead(args: {
8
+ address: string;
9
+ network: 'devnet' | 'mainnet';
10
+ graphqlUrl?: string;
11
+ fetcher?: typeof fetch;
12
+ }): Promise<ChainHead>;
13
+ /** Permission names as a Mina node reports them: 'Proof', 'Signature', 'Either', 'None', 'Impossible'. */
14
+ export type ZkappAccount = {
15
+ found: boolean;
16
+ zkappUri?: string;
17
+ vkHash?: string;
18
+ appState?: string[];
19
+ permissions?: {
20
+ editState?: string;
21
+ send?: string;
22
+ setZkappUri?: string;
23
+ setPermissions?: string;
24
+ setDelegate?: string;
25
+ setTiming?: string;
26
+ setVerificationKey?: {
27
+ auth?: string;
28
+ txnVersion?: number;
29
+ };
30
+ };
31
+ };
32
+ /** A Mina node that couldn't be reached or answered with an error; `status` 0 when there was no HTTP answer. */
33
+ export declare class MinaNodeError extends Error {
34
+ readonly status: number;
35
+ constructor(message: string, status: number);
36
+ }
37
+ /**
38
+ * Reads a zkApp account (state, verification-key hash, URI, permissions)
39
+ * from a Mina node over GraphQL — the fields o1js's fetchAccount reads,
40
+ * without o1js and its global Mina instance. `found: false` when the node
41
+ * has no such account.
42
+ */
43
+ export declare function readZkappAccount(args: {
44
+ address: string;
45
+ graphqlUrl: string;
46
+ fetcher?: typeof fetch;
47
+ }): Promise<ZkappAccount>;
48
+ /**
49
+ * The one GraphQL read behind readZkappAccount and readChainHead: the
50
+ * account and the chain's current slot (`undefined` when the node didn't
51
+ * report it) in one query. Throws MinaNodeError.
52
+ */
53
+ export declare function readZkappHead(args: {
54
+ address: string;
55
+ graphqlUrl: string;
56
+ fetcher?: typeof fetch;
57
+ }): Promise<{
58
+ account: ZkappAccount;
59
+ slot?: number;
60
+ }>;
61
+ //# sourceMappingURL=chainHead.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chainHead.d.ts","sourceRoot":"","sources":["../src/chainHead.ts"],"names":[],"mappings":"AAWA,gEAAgE;AAChE,MAAM,MAAM,SAAS,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAExE,wBAAsB,aAAa,CAAC,IAAI,EAAE;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,QAAQ,GAAG,SAAS,CAAC;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,KAAK,CAAC;CACxB,GAAG,OAAO,CAAC,SAAS,CAAC,CAOrB;AAED,0GAA0G;AAC1G,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,OAAO,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,WAAW,CAAC,EAAE;QACZ,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAC;QACjF,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAC;QACzC,kBAAkB,CAAC,EAAE;YAAE,IAAI,CAAC,EAAE,MAAM,CAAC;YAAC,UAAU,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC;KAC7D,CAAC;CACH,CAAC;AAEF,gHAAgH;AAChH,qBAAa,aAAc,SAAQ,KAAK;IACT,QAAQ,CAAC,MAAM,EAAE,MAAM;gBAAxC,OAAO,EAAE,MAAM,EAAW,MAAM,EAAE,MAAM;CACrD;AAED;;;;;GAKG;AACH,wBAAsB,gBAAgB,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,KAAK,CAAA;CAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAEnI;AAED;;;;GAIG;AACH,wBAAsB,aAAa,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,KAAK,CAAA;CAAE,GAAG,OAAO,CAAC;IAAE,OAAO,EAAE,YAAY,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CA4C5J"}
@@ -0,0 +1,88 @@
1
+ // Copyright (c) 2025-2026 zkLicensing project developers
2
+ // SPDX-License-Identifier: BUSL-1.1
3
+ /**
4
+ * chainHead.ts — reads a zkApp's first state field (its tree root) and the
5
+ * chain's current slot from a Mina node over GraphQL, in one query, without
6
+ * o1js and without touching o1js's global Mina instance.
7
+ */
8
+ import { DEFAULT_MINA_GRAPHQL_URLS } from './onChainRoot.js';
9
+ export async function readChainHead(args) {
10
+ const url = args.graphqlUrl ?? DEFAULT_MINA_GRAPHQL_URLS[args.network];
11
+ const { account, slot } = await readZkappHead({ address: args.address, graphqlUrl: url, fetcher: args.fetcher });
12
+ const root = account.appState?.[0];
13
+ if (!root)
14
+ throw new Error(`${args.address} is not a zkApp on ${args.network}`);
15
+ if (slot === undefined)
16
+ throw new MinaNodeError(`Mina node ${url} did not report the current slot`, 0);
17
+ return { root, slot, state: account.appState };
18
+ }
19
+ /** A Mina node that couldn't be reached or answered with an error; `status` 0 when there was no HTTP answer. */
20
+ export class MinaNodeError extends Error {
21
+ status;
22
+ constructor(message, status) {
23
+ super(message);
24
+ this.status = status;
25
+ }
26
+ }
27
+ /**
28
+ * Reads a zkApp account (state, verification-key hash, URI, permissions)
29
+ * from a Mina node over GraphQL — the fields o1js's fetchAccount reads,
30
+ * without o1js and its global Mina instance. `found: false` when the node
31
+ * has no such account.
32
+ */
33
+ export async function readZkappAccount(args) {
34
+ return (await readZkappHead(args)).account;
35
+ }
36
+ /**
37
+ * The one GraphQL read behind readZkappAccount and readChainHead: the
38
+ * account and the chain's current slot (`undefined` when the node didn't
39
+ * report it) in one query. Throws MinaNodeError.
40
+ */
41
+ export async function readZkappHead(args) {
42
+ const query = `query($pk: PublicKey!) {
43
+ account(publicKey: $pk) {
44
+ zkappUri
45
+ zkappState
46
+ verificationKey { hash }
47
+ permissions { editState send setZkappUri setPermissions setDelegate setTiming setVerificationKey { auth txnVersion } }
48
+ }
49
+ bestChain(maxLength: 1) { protocolState { consensusState { slotSinceGenesis } } }
50
+ }`;
51
+ let resp;
52
+ try {
53
+ resp = await (args.fetcher ?? fetch)(args.graphqlUrl, {
54
+ method: 'POST',
55
+ headers: { 'Content-Type': 'application/json' },
56
+ body: JSON.stringify({ query, variables: { pk: args.address } }),
57
+ });
58
+ }
59
+ catch (e) {
60
+ throw new MinaNodeError(`Mina node ${args.graphqlUrl}: ${e?.message ?? e}`, 0);
61
+ }
62
+ const body = await resp.json().catch(() => null);
63
+ if (!resp.ok || !body?.data) {
64
+ throw new MinaNodeError(`Mina node ${args.graphqlUrl}: ${body?.errors?.map(e => e.message).join('; ') || `HTTP ${resp.status}`}`, resp.status);
65
+ }
66
+ const rawSlot = body.data.bestChain?.[0]?.protocolState?.consensusState?.slotSinceGenesis;
67
+ const slot = rawSlot === undefined || rawSlot === null || !Number.isFinite(Number(rawSlot)) ? undefined : Number(rawSlot);
68
+ const a = body.data.account;
69
+ if (!a)
70
+ return { account: { found: false }, slot };
71
+ const p = a.permissions ?? {};
72
+ const txn = p.setVerificationKey?.txnVersion;
73
+ return {
74
+ slot,
75
+ account: {
76
+ found: true,
77
+ zkappUri: a.zkappUri ?? undefined,
78
+ vkHash: a.verificationKey?.hash != null ? String(a.verificationKey.hash) : undefined,
79
+ appState: Array.isArray(a.zkappState) ? a.zkappState.map(String) : undefined,
80
+ permissions: {
81
+ editState: p.editState, send: p.send, setZkappUri: p.setZkappUri, setPermissions: p.setPermissions,
82
+ setDelegate: p.setDelegate, setTiming: p.setTiming,
83
+ setVerificationKey: p.setVerificationKey ? { auth: p.setVerificationKey.auth, txnVersion: txn === undefined || txn === null ? undefined : Number(txn) } : undefined,
84
+ },
85
+ },
86
+ };
87
+ }
88
+ //# sourceMappingURL=chainHead.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chainHead.js","sourceRoot":"","sources":["../src/chainHead.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,oCAAoC;AAEpC;;;;GAIG;AAEH,OAAO,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAC;AAK7D,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,IAKnC;IACC,MAAM,GAAG,GAAG,IAAI,CAAC,UAAU,IAAI,yBAAyB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACvE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,MAAM,aAAa,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;IACjH,MAAM,IAAI,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC;IACnC,IAAI,CAAC,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,CAAC,OAAO,sBAAsB,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;IAChF,IAAI,IAAI,KAAK,SAAS;QAAE,MAAM,IAAI,aAAa,CAAC,aAAa,GAAG,kCAAkC,EAAE,CAAC,CAAC,CAA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