@t3foundry/tasra-sdk 0.0.0-stage → 0.3.1-next.1

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 (322) hide show
  1. package/CHANGELOG.md +148 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +94 -0
  4. package/README.md +137 -2
  5. package/SECURITY.md +115 -0
  6. package/dist/app/approvals.d.ts +50 -0
  7. package/dist/app/approvals.js +90 -0
  8. package/dist/app/authorization.d.ts +19 -0
  9. package/dist/app/authorization.js +20 -0
  10. package/dist/app/client.d.ts +231 -0
  11. package/dist/app/client.js +272 -0
  12. package/dist/app/creation.d.ts +73 -0
  13. package/dist/app/creation.js +77 -0
  14. package/dist/app/deployment.d.ts +36 -0
  15. package/dist/app/deployment.js +18 -0
  16. package/dist/app/identity.d.ts +211 -0
  17. package/dist/app/identity.js +287 -0
  18. package/dist/app/index.d.ts +22 -0
  19. package/dist/app/index.js +12 -0
  20. package/dist/app/manifest.d.ts +74 -0
  21. package/dist/app/manifest.js +120 -0
  22. package/dist/app/node.d.ts +30 -0
  23. package/dist/app/node.js +159 -0
  24. package/dist/app/ready-slot.d.ts +157 -0
  25. package/dist/app/ready-slot.js +199 -0
  26. package/dist/app/tasra-client.d.ts +125 -0
  27. package/dist/app/tasra-client.js +79 -0
  28. package/dist/app/viem.d.ts +10 -0
  29. package/dist/app/viem.js +32 -0
  30. package/dist/app/wallet.d.ts +126 -0
  31. package/dist/app/wallet.js +137 -0
  32. package/dist/auth/dpop.d.ts +86 -0
  33. package/dist/auth/dpop.js +142 -0
  34. package/dist/auth/holderProof.d.ts +102 -0
  35. package/dist/auth/holderProof.js +161 -0
  36. package/dist/auth/identityScope.d.ts +19 -0
  37. package/dist/auth/identityScope.js +82 -0
  38. package/dist/auth/index.d.ts +11 -0
  39. package/dist/auth/index.js +13 -0
  40. package/dist/auth/oid4vp.d.ts +230 -0
  41. package/dist/auth/oid4vp.js +780 -0
  42. package/dist/auth/rego/wasm/PINS.json +11 -0
  43. package/dist/auth/rego/wasm/keykeeper_rego_wasm.d.ts +76 -0
  44. package/dist/auth/rego/wasm/keykeeper_rego_wasm.js +379 -0
  45. package/dist/auth/rego/wasm/keykeeper_rego_wasm_bg.wasm +0 -0
  46. package/dist/auth/rego.d.ts +192 -0
  47. package/dist/auth/rego.js +492 -0
  48. package/dist/auth/verifier.d.ts +166 -0
  49. package/dist/auth/verifier.js +211 -0
  50. package/dist/chain/abis/accountantSetRegistry.d.ts +441 -0
  51. package/dist/chain/abis/accountantSetRegistry.js +574 -0
  52. package/dist/chain/abis/accountantSlashing.d.ts +2071 -0
  53. package/dist/chain/abis/accountantSlashing.js +2670 -0
  54. package/dist/chain/abis/bondingCurve.d.ts +398 -0
  55. package/dist/chain/abis/bondingCurve.js +521 -0
  56. package/dist/chain/abis/equivocationSlasher.d.ts +292 -0
  57. package/dist/chain/abis/equivocationSlasher.js +379 -0
  58. package/dist/chain/abis/fixedTasraPriceOracle.d.ts +52 -0
  59. package/dist/chain/abis/fixedTasraPriceOracle.js +72 -0
  60. package/dist/chain/abis/index.d.ts +12801 -0
  61. package/dist/chain/abis/index.js +64 -0
  62. package/dist/chain/abis/keeperShareRegistry.d.ts +519 -0
  63. package/dist/chain/abis/keeperShareRegistry.js +675 -0
  64. package/dist/chain/abis/keyRegistry.d.ts +2421 -0
  65. package/dist/chain/abis/keyRegistry.js +3114 -0
  66. package/dist/chain/abis/livenessRegistry.d.ts +402 -0
  67. package/dist/chain/abis/livenessRegistry.js +524 -0
  68. package/dist/chain/abis/mockEurc.d.ts +210 -0
  69. package/dist/chain/abis/mockEurc.js +276 -0
  70. package/dist/chain/abis/nodeRegistry.d.ts +1630 -0
  71. package/dist/chain/abis/nodeRegistry.js +2113 -0
  72. package/dist/chain/abis/platformExecutor.d.ts +328 -0
  73. package/dist/chain/abis/platformExecutor.js +429 -0
  74. package/dist/chain/abis/prevrandaoSaltBeacon.d.ts +16 -0
  75. package/dist/chain/abis/prevrandaoSaltBeacon.js +24 -0
  76. package/dist/chain/abis/serviceRegistry.d.ts +556 -0
  77. package/dist/chain/abis/serviceRegistry.js +719 -0
  78. package/dist/chain/abis/settlement.d.ts +1624 -0
  79. package/dist/chain/abis/settlement.js +2096 -0
  80. package/dist/chain/abis/tasraSwapRouter.d.ts +92 -0
  81. package/dist/chain/abis/tasraSwapRouter.js +122 -0
  82. package/dist/chain/abis/tasraToken.d.ts +455 -0
  83. package/dist/chain/abis/tasraToken.js +599 -0
  84. package/dist/chain/abis/tasraVestingVault.d.ts +165 -0
  85. package/dist/chain/abis/tasraVestingVault.js +215 -0
  86. package/dist/chain/abis/thresholdRandomBeacon.d.ts +378 -0
  87. package/dist/chain/abis/thresholdRandomBeacon.js +494 -0
  88. package/dist/chain/abis/treasury.d.ts +301 -0
  89. package/dist/chain/abis/treasury.js +390 -0
  90. package/dist/chain/abis/verifierSetRegistry.d.ts +445 -0
  91. package/dist/chain/abis/verifierSetRegistry.js +579 -0
  92. package/dist/chain/client.d.ts +212 -0
  93. package/dist/chain/client.js +576 -0
  94. package/dist/chain/commitmentRecovery.d.ts +21 -0
  95. package/dist/chain/commitmentRecovery.js +44 -0
  96. package/dist/chain/committeeClient.d.ts +102 -0
  97. package/dist/chain/committeeClient.js +137 -0
  98. package/dist/chain/deployments.d.ts +56 -0
  99. package/dist/chain/deployments.js +269 -0
  100. package/dist/chain/discovery.d.ts +49 -0
  101. package/dist/chain/discovery.js +107 -0
  102. package/dist/chain/events.d.ts +58 -0
  103. package/dist/chain/events.js +125 -0
  104. package/dist/chain/format.d.ts +25 -0
  105. package/dist/chain/format.js +51 -0
  106. package/dist/chain/index.d.ts +41 -0
  107. package/dist/chain/index.js +56 -0
  108. package/dist/chain/manifest.d.ts +165 -0
  109. package/dist/chain/manifest.js +132 -0
  110. package/dist/chain/network-profiles.json +75 -0
  111. package/dist/chain/networks.d.ts +73 -0
  112. package/dist/chain/networks.js +62 -0
  113. package/dist/chain/node.d.ts +44 -0
  114. package/dist/chain/node.js +176 -0
  115. package/dist/chain/offchain.d.ts +149 -0
  116. package/dist/chain/offchain.js +73 -0
  117. package/dist/chain/provisionRule.d.ts +164 -0
  118. package/dist/chain/provisionRule.js +212 -0
  119. package/dist/chain/registeredAgent.d.ts +64 -0
  120. package/dist/chain/registeredAgent.js +140 -0
  121. package/dist/chain/registeredOperation.d.ts +49 -0
  122. package/dist/chain/registeredOperation.js +136 -0
  123. package/dist/chain/registeredRelay.d.ts +206 -0
  124. package/dist/chain/registeredRelay.js +312 -0
  125. package/dist/chain/revertReason.d.ts +14 -0
  126. package/dist/chain/revertReason.js +62 -0
  127. package/dist/chain/ruleAmendment.d.ts +80 -0
  128. package/dist/chain/ruleAmendment.js +195 -0
  129. package/dist/chain/ruleAmendmentCore.d.ts +129 -0
  130. package/dist/chain/ruleAmendmentCore.js +152 -0
  131. package/dist/chain/ruleInput.d.ts +10 -0
  132. package/dist/chain/ruleInput.js +13 -0
  133. package/dist/chain/serviceIdentity.d.ts +176 -0
  134. package/dist/chain/serviceIdentity.js +237 -0
  135. package/dist/chain/serviceProfiles.d.ts +39 -0
  136. package/dist/chain/serviceProfiles.js +74 -0
  137. package/dist/chain/services.d.ts +141 -0
  138. package/dist/chain/services.js +79 -0
  139. package/dist/chain/slotClient.d.ts +55 -0
  140. package/dist/chain/slotClient.js +62 -0
  141. package/dist/chain/slotSeed.d.ts +31 -0
  142. package/dist/chain/slotSeed.js +98 -0
  143. package/dist/chain/write.d.ts +431 -0
  144. package/dist/chain/write.js +958 -0
  145. package/dist/client/client.d.ts +112 -0
  146. package/dist/client/client.js +111 -0
  147. package/dist/client/index.d.ts +3 -0
  148. package/dist/client/index.js +2 -0
  149. package/dist/client/session.d.ts +67 -0
  150. package/dist/client/session.js +238 -0
  151. package/dist/committee/client.d.ts +284 -0
  152. package/dist/committee/client.js +351 -0
  153. package/dist/committee/dual-sign.d.ts +110 -0
  154. package/dist/committee/dual-sign.js +202 -0
  155. package/dist/committee/ecdsa.d.ts +36 -0
  156. package/dist/committee/ecdsa.js +45 -0
  157. package/dist/committee/extraction.d.ts +82 -0
  158. package/dist/committee/extraction.js +96 -0
  159. package/dist/committee/index.d.ts +19 -0
  160. package/dist/committee/index.js +13 -0
  161. package/dist/committee/oauth.d.ts +51 -0
  162. package/dist/committee/oauth.js +90 -0
  163. package/dist/committee/receipts.d.ts +51 -0
  164. package/dist/committee/receipts.js +59 -0
  165. package/dist/committee/request.d.ts +264 -0
  166. package/dist/committee/request.js +305 -0
  167. package/dist/committee/token.d.ts +274 -0
  168. package/dist/committee/token.js +418 -0
  169. package/dist/crypto/constants.d.ts +10 -0
  170. package/dist/crypto/constants.js +32 -0
  171. package/dist/crypto/detect.d.ts +20 -0
  172. package/dist/crypto/detect.js +44 -0
  173. package/dist/crypto/envelope.d.ts +60 -0
  174. package/dist/crypto/envelope.js +243 -0
  175. package/dist/crypto/frost.d.ts +49 -0
  176. package/dist/crypto/frost.js +152 -0
  177. package/dist/crypto/hex.d.ts +7 -0
  178. package/dist/crypto/hex.js +24 -0
  179. package/dist/crypto/ibe-blob.d.ts +114 -0
  180. package/dist/crypto/ibe-blob.js +208 -0
  181. package/dist/crypto/ibe.d.ts +88 -0
  182. package/dist/crypto/ibe.js +262 -0
  183. package/dist/crypto/index.d.ts +12 -0
  184. package/dist/crypto/index.js +12 -0
  185. package/dist/crypto/kem.d.ts +55 -0
  186. package/dist/crypto/kem.js +241 -0
  187. package/dist/decryption/client.d.ts +64 -0
  188. package/dist/decryption/client.js +82 -0
  189. package/dist/decryption/index.d.ts +2 -0
  190. package/dist/decryption/index.js +2 -0
  191. package/dist/errors.d.ts +124 -0
  192. package/dist/errors.js +167 -0
  193. package/dist/index.d.ts +43 -0
  194. package/dist/index.js +98 -0
  195. package/dist/keys/index.d.ts +1 -0
  196. package/dist/keys/index.js +4 -0
  197. package/dist/keys/node-client.d.ts +27 -0
  198. package/dist/keys/node-client.js +97 -0
  199. package/dist/oid4vp/binding.d.ts +76 -0
  200. package/dist/oid4vp/binding.js +118 -0
  201. package/dist/oid4vp/did-web.d.ts +49 -0
  202. package/dist/oid4vp/did-web.js +57 -0
  203. package/dist/oid4vp/index.d.ts +9 -0
  204. package/dist/oid4vp/index.js +10 -0
  205. package/dist/oid4vp/jose.d.ts +166 -0
  206. package/dist/oid4vp/jose.js +242 -0
  207. package/dist/oid4vp/jwe.d.ts +30 -0
  208. package/dist/oid4vp/jwe.js +93 -0
  209. package/dist/oid4vp/oid4vci.d.ts +84 -0
  210. package/dist/oid4vp/oid4vci.js +85 -0
  211. package/dist/oid4vp/request-object.d.ts +109 -0
  212. package/dist/oid4vp/request-object.js +96 -0
  213. package/dist/oid4vp/sd-jwt.d.ts +177 -0
  214. package/dist/oid4vp/sd-jwt.js +208 -0
  215. package/dist/oid4vp/verifier-agent.d.ts +161 -0
  216. package/dist/oid4vp/verifier-agent.js +132 -0
  217. package/dist/oid4vp/wallet.d.ts +116 -0
  218. package/dist/oid4vp/wallet.js +108 -0
  219. package/dist/recipient/store.d.ts +67 -0
  220. package/dist/recipient/store.js +98 -0
  221. package/dist/signing/ecdsa.d.ts +49 -0
  222. package/dist/signing/ecdsa.js +79 -0
  223. package/dist/signing/frost.d.ts +88 -0
  224. package/dist/signing/frost.js +145 -0
  225. package/dist/signing/index.d.ts +4 -0
  226. package/dist/signing/index.js +3 -0
  227. package/dist/slots/faucet.d.ts +23 -0
  228. package/dist/slots/faucet.js +35 -0
  229. package/dist/slots/index.d.ts +2 -0
  230. package/dist/slots/index.js +4 -0
  231. package/dist/verifier-agent/index.d.ts +241 -0
  232. package/dist/verifier-agent/index.js +411 -0
  233. package/docs/DEVELOPER-EXPERIENCE.md +62 -0
  234. package/docs/README.md +33 -0
  235. package/docs/access-control.md +76 -0
  236. package/docs/ai-development.md +292 -0
  237. package/docs/api.md +338 -0
  238. package/docs/application-api.md +335 -0
  239. package/docs/architecture.md +65 -0
  240. package/docs/basics.md +48 -0
  241. package/docs/capabilities.md +86 -0
  242. package/docs/chain.md +153 -0
  243. package/docs/compatibility.md +36 -0
  244. package/docs/consumer-migrations.md +80 -0
  245. package/docs/create-slot.md +104 -0
  246. package/docs/document-signing.md +243 -0
  247. package/docs/encrypted-notes.md +129 -0
  248. package/docs/encryption.md +100 -0
  249. package/docs/errors.md +76 -0
  250. package/docs/flows.md +289 -0
  251. package/docs/fuji.md +127 -0
  252. package/docs/funding.md +126 -0
  253. package/docs/getting-started.md +94 -0
  254. package/docs/glossary.md +44 -0
  255. package/docs/installation.md +132 -0
  256. package/docs/manage-slot.md +95 -0
  257. package/docs/native-approvals.md +124 -0
  258. package/docs/prerequisites.md +137 -0
  259. package/docs/read-slot.md +41 -0
  260. package/docs/reference/README.md +30 -0
  261. package/docs/reference/app-node.md +102 -0
  262. package/docs/reference/app.md +1737 -0
  263. package/docs/reference/chain-node.md +159 -0
  264. package/docs/reference/chain.md +3609 -0
  265. package/docs/reference/committee.md +2176 -0
  266. package/docs/reference/main.md +4068 -0
  267. package/docs/reference/oid4vp.md +2547 -0
  268. package/docs/reference/verifier-agent.md +498 -0
  269. package/docs/shared-account.md +168 -0
  270. package/docs/signing-and-encryption.md +52 -0
  271. package/docs/signing.md +172 -0
  272. package/examples/committee-slot.ts +16 -0
  273. package/examples/connect-fuji.ts +41 -0
  274. package/examples/connect-network.ts +14 -0
  275. package/examples/credential-approvals.ts +46 -0
  276. package/examples/document-signing/README.md +59 -0
  277. package/examples/document-signing/backend.ts +132 -0
  278. package/examples/document-signing/build.mjs +19 -0
  279. package/examples/document-signing/config.ts +34 -0
  280. package/examples/document-signing/gitignore.template +9 -0
  281. package/examples/document-signing/model.ts +140 -0
  282. package/examples/document-signing/network.ts +58 -0
  283. package/examples/document-signing/package.json +25 -0
  284. package/examples/document-signing/public/index.html +27 -0
  285. package/examples/document-signing/public/sample.pdf +31 -0
  286. package/examples/document-signing/public/styles.css +531 -0
  287. package/examples/document-signing/server.ts +358 -0
  288. package/examples/document-signing/setup.ts +111 -0
  289. package/examples/document-signing/store.ts +105 -0
  290. package/examples/document-signing/tsconfig.json +24 -0
  291. package/examples/document-signing/verify.ts +24 -0
  292. package/examples/document-signing/web/app.ts +312 -0
  293. package/examples/document-signing/web/preview.ts +67 -0
  294. package/examples/document-signing/web/wallet.ts +146 -0
  295. package/examples/document-signing/workflow.test.ts +228 -0
  296. package/examples/document-signing/workflow.ts +241 -0
  297. package/examples/document-signing.ts +96 -0
  298. package/examples/encrypted-notes.ts +86 -0
  299. package/examples/ethers-signer.ts +204 -0
  300. package/examples/fund-slot.ts +73 -0
  301. package/examples/getting-started.ts +25 -0
  302. package/examples/learning-authorize.ts +23 -0
  303. package/examples/learning-cancel-slot.ts +11 -0
  304. package/examples/learning-context.ts +28 -0
  305. package/examples/learning-create-slot.ts +25 -0
  306. package/examples/learning-read-slot.ts +8 -0
  307. package/examples/learning-renew-slot.ts +12 -0
  308. package/examples/learning-sign.ts +8 -0
  309. package/examples/live-config.ts +71 -0
  310. package/examples/minimal.ts +6 -0
  311. package/examples/native-approvals.ts +57 -0
  312. package/examples/network.ts +58 -0
  313. package/examples/offline.ts +16 -0
  314. package/examples/personal-vault.ts +20 -0
  315. package/examples/provision-slot.ts +47 -0
  316. package/examples/shared-account.ts +46 -0
  317. package/examples/slot-address.ts +13 -0
  318. package/examples/slot-creation.ts +9 -0
  319. package/examples/tutorial-negative.ts +26 -0
  320. package/examples/tutorial-support.ts +76 -0
  321. package/examples/viem-account.ts +203 -0
  322. package/package.json +303 -4
@@ -0,0 +1,142 @@
1
+ // DPoP (RFC 9449) proofs for the Keykeeper OAuth authorization flow.
2
+ //
3
+ // TWO CALL SHAPES, and which one you need is decided by WHERE YOUR TOKEN'S KEY LIVES.
4
+ // A DPoP proof is only worth anything if its key is the key the access token is bound to
5
+ // (`cnf.jkt`), so the proof must be signed by whatever holds that key:
6
+ //
7
+ // 1. **Your app obtained the token with a key this helper owns** — a plain OAuth client,
8
+ // `oidc-client-ts`, or the fleet test client. Use `createDpopKey()` to mint the
9
+ // non-extractable key, obtain the token with `key.tokenRequestProof(...)` in the
10
+ // token request's `DPoP` header, then hand `key.signer` to `submitOauthResponse`.
11
+ // The two differ in exactly what RFC 9449 says they must: a token request has no
12
+ // access token yet, so its proof carries no `ath`.
13
+ //
14
+ // 2. **An SDK holds the key internally** — `auth0-spa-js` with `useDpop`. It will not
15
+ // hand you the key, but it WILL mint a proof for a URL and nonce you choose
16
+ // (`auth0.generateDpopProof({url, method, nonce, accessToken})`). Wrap that call as a
17
+ // {@link DpopSigner} and pass it through; this file never sees the key.
18
+ //
19
+ // Note: `keycloak-js` has NO released DPoP API (`dpopConfig` exists only in an unreleased PR).
20
+ // A Keycloak tenant therefore uses shape 1: obtain the token with `tokenRequestProof` from
21
+ // `createDpopKey()` and present with the same key's `signer`.
22
+ //
23
+ // Note: the key MUST be non-extractable where the platform allows it. `createDpopKey` asks
24
+ // WebCrypto for `extractable: false`, which is what makes a stolen token inert: an attacker
25
+ // who exfiltrates the token cannot exfiltrate the key that spends it.
26
+ import { TasraError } from '../errors.js';
27
+ const B64URL_ALPHABET_SAFE = /^[A-Za-z0-9\-_]*$/;
28
+ function b64url(bytes) {
29
+ let s = '';
30
+ for (const b of bytes)
31
+ s += String.fromCharCode(b);
32
+ return btoa(s).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
33
+ }
34
+ function b64urlJson(value) {
35
+ return b64url(new TextEncoder().encode(JSON.stringify(value)));
36
+ }
37
+ /**
38
+ * Compute the base64url SHA-256 access-token hash for the DPoP ath claim.
39
+ *
40
+ * @param accessToken - Exact access token string to bind into the DPoP proof.
41
+ */
42
+ export async function accessTokenHash(accessToken) {
43
+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(accessToken));
44
+ return b64url(new Uint8Array(digest));
45
+ }
46
+ /**
47
+ * Generate an ES256 DPoP key pair with a non-extractable private key.
48
+ * The returned signer can create proofs without exposing the private key bytes.
49
+ */
50
+ export async function createDpopKey() {
51
+ const pair = await crypto.subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, false, [
52
+ 'sign',
53
+ ]);
54
+ const publicJwk = await crypto.subtle.exportKey('jwk', pair.publicKey);
55
+ // A JWK carrying anything beyond the public EC parameters would leak into every proof
56
+ // header; keep exactly what RFC 7638 hashes.
57
+ const header = {
58
+ kty: 'EC',
59
+ crv: 'P-256',
60
+ x: publicJwk.x,
61
+ y: publicJwk.y,
62
+ };
63
+ const signer = {
64
+ async proof({ htm, htu, nonce, accessToken }) {
65
+ const protectedHeader = { alg: 'ES256', typ: 'dpop+jwt', jwk: header };
66
+ // Absent claims are OMITTED, not sent empty: an IdP refuses a token-request proof that
67
+ // carries `ath`, and a `nonce: ""` is a nonce, just a wrong one.
68
+ const payload = {
69
+ htm,
70
+ htu,
71
+ ...(nonce !== undefined ? { nonce } : {}),
72
+ ...(accessToken !== undefined ? { ath: await accessTokenHash(accessToken) } : {}),
73
+ jti: randomJti(),
74
+ iat: Math.floor(Date.now() / 1000),
75
+ };
76
+ const signingInput = `${b64urlJson(protectedHeader)}.${b64urlJson(payload)}`;
77
+ const raw = await crypto.subtle.sign({ name: 'ECDSA', hash: 'SHA-256' }, pair.privateKey, new TextEncoder().encode(signingInput));
78
+ return `${signingInput}.${b64url(new Uint8Array(raw))}`;
79
+ },
80
+ };
81
+ return {
82
+ publicJwk: header,
83
+ async thumbprint() {
84
+ return jwkThumbprint(header);
85
+ },
86
+ async tokenRequestProof({ htm, htu, nonce }) {
87
+ return signer.proof({ htm, htu, nonce });
88
+ },
89
+ signer,
90
+ };
91
+ }
92
+ /**
93
+ * RFC 7638 JWK thumbprint of an EC P-256 public key.
94
+ *
95
+ * The member order is LEXICOGRAPHIC and the JSON has no whitespace - the RFC hashes an
96
+ * exactly specified string, so `JSON.stringify` over an object literal in a different order
97
+ * yields a different thumbprint and the token's `cnf.jkt` would never match.
98
+ *
99
+ * @param jwk - Public JSON Web Key whose required members form the thumbprint.
100
+ */
101
+ export async function jwkThumbprint(jwk) {
102
+ if (jwk.kty !== 'EC' || !jwk.crv || !jwk.x || !jwk.y) {
103
+ throw new TasraError('jwkThumbprint: only EC public keys are supported here', {
104
+ retryable: false,
105
+ });
106
+ }
107
+ const canonical = `{"crv":"${jwk.crv}","kty":"EC","x":"${jwk.x}","y":"${jwk.y}"}`;
108
+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(canonical));
109
+ return b64url(new Uint8Array(digest));
110
+ }
111
+ function randomJti() {
112
+ const b = new Uint8Array(16);
113
+ crypto.getRandomValues(b);
114
+ return b64url(b);
115
+ }
116
+ /**
117
+ * Wrap `auth0-spa-js`'s own proof minter as a {@link DpopSigner}.
118
+ *
119
+ * The SDK holds the key, so this is the ONLY way an Auth0 app can produce a proof whose
120
+ * `jkt` matches its token's `cnf.jkt`.
121
+ *
122
+ * ```ts
123
+ * const signer = auth0DpopSigner((args) => auth0.generateDpopProof(args))
124
+ * ```
125
+ *
126
+ * @param generate - Callback that creates a DPoP proof using the identity provider client's bound key.
127
+ */
128
+ export function auth0DpopSigner(generate) {
129
+ return {
130
+ async proof({ htm, htu, nonce, accessToken }) {
131
+ return generate({ url: htu, method: htm, nonce, accessToken });
132
+ },
133
+ };
134
+ }
135
+ /**
136
+ * Guard for a nonce that can ride in a header (the agent's challenge carries it back).
137
+ *
138
+ * @param nonce - Nonce text to check before using it as an HTTP header value.
139
+ */
140
+ export function isHeaderSafeNonce(nonce) {
141
+ return nonce.length > 0 && B64URL_ALPHABET_SAFE.test(nonce);
142
+ }
@@ -0,0 +1,102 @@
1
+ /**
2
+ * `base64url(sha256(credentials joined by "\n"))`.
3
+ *
4
+ * MUST byte-for-byte match the verifier's `credentials_commitment`
5
+ * (the reference holder-proof implementation). Order-sensitive and delimiter-framed so both
6
+ * sides agree without JSON canonicalization. Binds a holder proof to the exact set of
7
+ * compact-JWS credentials being presented.
8
+ *
9
+ * @param credentials - Ordered compact credential strings; order is part of the commitment.
10
+ */
11
+ export declare function credentialsCommitment(credentials: string[]): string;
12
+ /** Single-use verifier challenge, expiry and optional disclosed slot policy. */
13
+ export interface HolderNonce {
14
+ /** Single-use verifier challenge. */
15
+ nonce: string;
16
+ /** Expiry, Unix seconds. */
17
+ expiresAt: number;
18
+ /** The slot's DCQL rule (present only for public-disclosure slots). */
19
+ dcqlRule?: string;
20
+ /** The salt used in the rule's on-chain commitment (present with dcqlRule). */
21
+ dcqlSalt?: string;
22
+ /** Rule version counter (present with dcqlRule). */
23
+ ruleVersion?: number;
24
+ }
25
+ /**
26
+ * Mint a single-use challenge nonce, optionally bound to a slot id + action (F4).
27
+ * POST {verifier}/v1/nonce
28
+ *
29
+ * @param verifierUrl - Verifier HTTP base URL.
30
+ * @param opts - Optional slot and action binding for the single-use challenge.
31
+ */
32
+ export declare function fetchHolderNonce(verifierUrl: string, opts?: {
33
+ slotId?: string;
34
+ action?: string;
35
+ }): Promise<HolderNonce>;
36
+ /**
37
+ * A signer for the holder DID's `authentication` key. Either the SDK holds the raw
38
+ * Ed25519 secret, or the caller supplies a `sign` callback (HSM / wallet / WebCrypto)
39
+ * that returns the raw JWS signature bytes for the given signing input.
40
+ */
41
+ export type HolderSigner = {
42
+ alg: 'EdDSA'; /** Holder DID identifying the authentication key. */
43
+ did: string;
44
+ secretKey: Uint8Array; /** Optional JOSE key identifier included in the proof header. */
45
+ kid?: string;
46
+ } | {
47
+ /** JWS algorithm used by the holder authentication key. */
48
+ alg: 'EdDSA' | 'ES256';
49
+ /** Holder DID identifying the authentication key. */
50
+ did: string;
51
+ sign: (signingInput: Uint8Array) => Uint8Array | Promise<Uint8Array>;
52
+ /** Optional JOSE key identifier included in the proof header. */
53
+ kid?: string;
54
+ };
55
+ /** Holder signing key, verifier challenge and credentials to bind into a proof. */
56
+ export interface BuildHolderProofOpts {
57
+ /** Holder DID authentication key or signing callback. */
58
+ signer: HolderSigner;
59
+ /** The verifier's expected audience (its token `iss`). */
60
+ audience: string;
61
+ /** The challenge from {@link fetchHolderNonce}. */
62
+ nonce: string;
63
+ /** The exact compact-JWS credentials being presented, in order. */
64
+ credentials: string[];
65
+ /** Echo the nonce's slot binding (when the nonce was slot-bound). */
66
+ slotId?: string;
67
+ /** Echo the nonce's action binding. */
68
+ action?: string;
69
+ /** Proof lifetime, seconds (default 300). */
70
+ ttlSecs?: number;
71
+ /** Override `iat` (Unix seconds) - for tests. */
72
+ nowSecs?: number;
73
+ }
74
+ /**
75
+ * Build a holder-proof compact-JWS (header.payload.signature).
76
+ *
77
+ * @param opts - Holder signer, challenge, audience and ordered credentials to bind.
78
+ */
79
+ export declare function buildHolderProof(opts: BuildHolderProofOpts): Promise<string>;
80
+ /**
81
+ * Convenience: fetch a nonce and build the holder proof in one step. Returns the
82
+ * compact-JWS to put in the `holder_proof` field of a `verify-vp-jwt` /
83
+ * `committee-authorize` request.
84
+ *
85
+ * @param verifierUrl - Verifier HTTP base URL.
86
+ * @param opts - Holder signer, verifier audience, credentials and optional operation binding.
87
+ */
88
+ export declare function createHolderProof(verifierUrl: string, opts: {
89
+ signer: HolderSigner;
90
+ audience: string;
91
+ credentials: string[];
92
+ slotId?: string;
93
+ action?: string;
94
+ ttlSecs?: number;
95
+ }): Promise<string>;
96
+ /**
97
+ * A did:key identifier for an Ed25519 public key (multicodec 0xed01, base58btc). Useful
98
+ * when the holder is identified by a self-certifying did:key.
99
+ *
100
+ * @param publicKey - 32-byte Ed25519 public key.
101
+ */
102
+ export declare function ed25519DidKey(publicKey: Uint8Array): string;
@@ -0,0 +1,161 @@
1
+ // Holder proof-of-possession (F1) + nonce/replay binding (F4) for the verifier.
2
+ //
3
+ // When a verifier runs with `require_holder_binding`, presenting credentials is not
4
+ // enough: the presenter must prove control of the holder DID's `authentication` key over
5
+ // * a fresh single-use server nonce (POST /v1/nonce) - anti-replay,
6
+ // * this verifier's audience (its token `iss`) - anti cross-verifier replay,
7
+ // * a commitment to the exact ordered credentials - so a captured proof can't be
8
+ // reused with a different credential set,
9
+ // * optionally the slot id + action the nonce was bound to (per-resource / action).
10
+ //
11
+ // This stays product-agnostic: the consumer brings the holder key (or a sign callback -
12
+ // e.g. an HSM / browser wallet). The SDK never persists it. Browser-safe (no Buffer).
13
+ import { ed25519 } from '@noble/curves/ed25519';
14
+ import { sha256 } from '@noble/hashes/sha256';
15
+ import { httpError } from '../errors.js';
16
+ function base(url) {
17
+ return url.replace(/\/$/, '');
18
+ }
19
+ /** base64url (no padding) of raw bytes - browser + Node safe via btoa. */
20
+ function b64urlBytes(bytes) {
21
+ let bin = '';
22
+ for (const b of bytes)
23
+ bin += String.fromCharCode(b);
24
+ return btoa(bin).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
25
+ }
26
+ function b64urlString(s) {
27
+ return b64urlBytes(new TextEncoder().encode(s));
28
+ }
29
+ /**
30
+ * `base64url(sha256(credentials joined by "\n"))`.
31
+ *
32
+ * MUST byte-for-byte match the verifier's `credentials_commitment`
33
+ * (the reference holder-proof implementation). Order-sensitive and delimiter-framed so both
34
+ * sides agree without JSON canonicalization. Binds a holder proof to the exact set of
35
+ * compact-JWS credentials being presented.
36
+ *
37
+ * @param credentials - Ordered compact credential strings; order is part of the commitment.
38
+ */
39
+ export function credentialsCommitment(credentials) {
40
+ return b64urlBytes(sha256(new TextEncoder().encode(credentials.join('\n'))));
41
+ }
42
+ /**
43
+ * Mint a single-use challenge nonce, optionally bound to a slot id + action (F4).
44
+ * POST {verifier}/v1/nonce
45
+ *
46
+ * @param verifierUrl - Verifier HTTP base URL.
47
+ * @param opts - Optional slot and action binding for the single-use challenge.
48
+ */
49
+ export async function fetchHolderNonce(verifierUrl, opts) {
50
+ const url = `${base(verifierUrl)}/v1/nonce`;
51
+ const res = await fetch(url, {
52
+ method: 'POST',
53
+ headers: { 'Content-Type': 'application/json' },
54
+ body: JSON.stringify({ slot_id: opts?.slotId, action: opts?.action }),
55
+ });
56
+ if (!res.ok)
57
+ throw await httpError(res, url, 'v1/nonce');
58
+ const b = (await res.json());
59
+ const out = { nonce: b.nonce, expiresAt: b.expires_at };
60
+ if (b.dcql_rule !== undefined)
61
+ out.dcqlRule = b.dcql_rule;
62
+ if (b.dcql_salt !== undefined)
63
+ out.dcqlSalt = b.dcql_salt;
64
+ if (b.rule_version !== undefined)
65
+ out.ruleVersion = b.rule_version;
66
+ return out;
67
+ }
68
+ /**
69
+ * Build a holder-proof compact-JWS (header.payload.signature).
70
+ *
71
+ * @param opts - Holder signer, challenge, audience and ordered credentials to bind.
72
+ */
73
+ export async function buildHolderProof(opts) {
74
+ const now = opts.nowSecs ?? Math.floor(Date.now() / 1000);
75
+ const header = { alg: opts.signer.alg, typ: 'JWT' };
76
+ if (opts.signer.kid)
77
+ header.kid = opts.signer.kid;
78
+ const payload = {
79
+ iss: opts.signer.did,
80
+ aud: opts.audience,
81
+ iat: now,
82
+ exp: now + (opts.ttlSecs ?? 300),
83
+ nonce: opts.nonce,
84
+ vp_hash: credentialsCommitment(opts.credentials),
85
+ };
86
+ if (opts.slotId)
87
+ payload.slot_id = opts.slotId;
88
+ if (opts.action)
89
+ payload.action = opts.action;
90
+ const signingInput = `${b64urlString(JSON.stringify(header))}.${b64urlString(JSON.stringify(payload))}`;
91
+ const inputBytes = new TextEncoder().encode(signingInput);
92
+ let sig;
93
+ if ('secretKey' in opts.signer) {
94
+ if (opts.signer.alg !== 'EdDSA') {
95
+ throw new Error('buildHolderProof: a raw secretKey signer only supports EdDSA');
96
+ }
97
+ sig = ed25519.sign(inputBytes, opts.signer.secretKey);
98
+ }
99
+ else {
100
+ sig = await opts.signer.sign(inputBytes);
101
+ }
102
+ return `${signingInput}.${b64urlBytes(sig)}`;
103
+ }
104
+ /**
105
+ * Convenience: fetch a nonce and build the holder proof in one step. Returns the
106
+ * compact-JWS to put in the `holder_proof` field of a `verify-vp-jwt` /
107
+ * `committee-authorize` request.
108
+ *
109
+ * @param verifierUrl - Verifier HTTP base URL.
110
+ * @param opts - Holder signer, verifier audience, credentials and optional operation binding.
111
+ */
112
+ export async function createHolderProof(verifierUrl, opts) {
113
+ const { nonce } = await fetchHolderNonce(verifierUrl, { slotId: opts.slotId, action: opts.action });
114
+ return buildHolderProof({
115
+ signer: opts.signer,
116
+ audience: opts.audience,
117
+ nonce,
118
+ credentials: opts.credentials,
119
+ slotId: opts.slotId,
120
+ action: opts.action,
121
+ ttlSecs: opts.ttlSecs,
122
+ });
123
+ }
124
+ /**
125
+ * A did:key identifier for an Ed25519 public key (multicodec 0xed01, base58btc). Useful
126
+ * when the holder is identified by a self-certifying did:key.
127
+ *
128
+ * @param publicKey - 32-byte Ed25519 public key.
129
+ */
130
+ export function ed25519DidKey(publicKey) {
131
+ // multibase base58btc('z') prefix; multicodec 0xed 0x01 || 32-byte key.
132
+ const data = new Uint8Array(2 + publicKey.length);
133
+ data[0] = 0xed;
134
+ data[1] = 0x01;
135
+ data.set(publicKey, 2);
136
+ return `did:key:z${base58btc(data)}`;
137
+ }
138
+ // Minimal base58btc (Bitcoin alphabet) for did:key. Kept local + dependency-free.
139
+ const B58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
140
+ function base58btc(bytes) {
141
+ let zeros = 0;
142
+ while (zeros < bytes.length && bytes[zeros] === 0)
143
+ zeros++;
144
+ const digits = [];
145
+ for (let i = zeros; i < bytes.length; i++) {
146
+ let carry = bytes[i];
147
+ for (let j = 0; j < digits.length; j++) {
148
+ carry += digits[j] << 8;
149
+ digits[j] = carry % 58;
150
+ carry = (carry / 58) | 0;
151
+ }
152
+ while (carry > 0) {
153
+ digits.push(carry % 58);
154
+ carry = (carry / 58) | 0;
155
+ }
156
+ }
157
+ let out = '1'.repeat(zeros);
158
+ for (let i = digits.length - 1; i >= 0; i--)
159
+ out += B58[digits[i]];
160
+ return out;
161
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Max BYTE length of a scope grant or an identity (the reference implementation's cap). Anything
3
+ * longer fails closed - a grant or identity this large is a bug or an attack, not a
4
+ * real subject path.
5
+ */
6
+ export declare const MAX_IDENTITY_LEN = 1024;
7
+ /**
8
+ * Whether the scope `grant` (from a verified credential) covers the requested
9
+ * `identity`. Fail-closed on oversize or NUL-bearing inputs.
10
+ *
11
+ * Implemented over UTF-8 BYTES, not UTF-16 code units, deliberately: the reference implementation
12
+ * compares raw bytes, the length cap is in bytes, and the `/`-boundary check indexes a
13
+ * byte position. Operating on `.length`/`charAt` would diverge for any non-ASCII
14
+ * segment.
15
+ *
16
+ * @param grant - Slash-delimited scope grant, optionally ending in a wildcard.
17
+ * @param identity - Requested identity string to compare with the grant.
18
+ */
19
+ export declare function scopeCovers(grant: string, identity: string): boolean;
@@ -0,0 +1,82 @@
1
+ // Identity-scope matching for (identity-scoped threshold decryption).
2
+ //
3
+ // THIS FILE MUST AGREE WITH THE RUST CRATE EXACTLY - it mirrors
4
+ // the reference scope matcher, and the shared corpus
5
+ // (test/oid4vp-vectors.json, `scope_matcher` family) pins the two on every run.
6
+ //
7
+ // A DCQL rule answers WHO may operate; `scopeCovers` answers WHICH identity a caller
8
+ // may operate *on*, by testing a scope grant found in a verified credential against the
9
+ // requested identity string. The grammar is deliberately minimal - a pattern language
10
+ // is an attack surface - and every path fails CLOSED: an oversize or NUL-bearing input
11
+ // matches nothing rather than erroring or being coerced.
12
+ //
13
+ // Grammar (raw-byte, case-sensitive; identities are `/`-segmented UTF-8):
14
+ // - exact: `g` covers exactly `g`.
15
+ // - segment-prefix: `p/*` covers `p` itself and anything strictly under `p/`. The
16
+ // segment boundary is load-bearing: `patient:12/*` covers `patient:12` and
17
+ // `patient:12/labs` but MUST NOT cover the sibling `patient:123` (the classic
18
+ // prefix-match bug - its own corpus vector).
19
+ // - explicit-open: bare `"*"` covers everything (visible in the committed rule,
20
+ // never a default).
21
+ //
22
+ // No mid-path wildcards, no regex. Higher layers (the namespace binding in
23
+ // `oid4vp.ts#evaluateIdentityScoped`) decide *whose* grant is honoured; this function
24
+ // only decides whether a given grant string reaches a given identity string.
25
+ /**
26
+ * Max BYTE length of a scope grant or an identity (the reference implementation's cap). Anything
27
+ * longer fails closed - a grant or identity this large is a bug or an attack, not a
28
+ * real subject path.
29
+ */
30
+ export const MAX_IDENTITY_LEN = 1024;
31
+ const encoder = new TextEncoder();
32
+ /**
33
+ * Whether the scope `grant` (from a verified credential) covers the requested
34
+ * `identity`. Fail-closed on oversize or NUL-bearing inputs.
35
+ *
36
+ * Implemented over UTF-8 BYTES, not UTF-16 code units, deliberately: the reference implementation
37
+ * compares raw bytes, the length cap is in bytes, and the `/`-boundary check indexes a
38
+ * byte position. Operating on `.length`/`charAt` would diverge for any non-ASCII
39
+ * segment.
40
+ *
41
+ * @param grant - Slash-delimited scope grant, optionally ending in a wildcard.
42
+ * @param identity - Requested identity string to compare with the grant.
43
+ */
44
+ export function scopeCovers(grant, identity) {
45
+ const g = encoder.encode(grant);
46
+ const id = encoder.encode(identity);
47
+ if (g.length > MAX_IDENTITY_LEN || id.length > MAX_IDENTITY_LEN)
48
+ return false;
49
+ if (g.includes(0) || id.includes(0))
50
+ return false;
51
+ // Explicit-open: the whole namespace, written in the rule on purpose.
52
+ if (grant === '*')
53
+ return true;
54
+ // Segment-prefix: `p/*` covers `p` and anything strictly beneath `p/`. The next byte
55
+ // after the prefix MUST be `/`, so a longer sibling segment (`patient:123` vs
56
+ // `patient:12`) is NOT covered - that boundary check is the whole point.
57
+ // (`/` and `*` are ASCII, so the string-level suffix test equals the byte-level one.)
58
+ if (grant.endsWith('/*')) {
59
+ const prefix = g.subarray(0, g.length - 2);
60
+ if (bytesEqual(id, prefix))
61
+ return true;
62
+ return id.length > prefix.length && id[prefix.length] === 0x2f && startsWith(id, prefix);
63
+ }
64
+ // Exact match - a grant naming one identity.
65
+ return bytesEqual(g, id);
66
+ }
67
+ function bytesEqual(a, b) {
68
+ if (a.length !== b.length)
69
+ return false;
70
+ for (let i = 0; i < a.length; i++)
71
+ if (a[i] !== b[i])
72
+ return false;
73
+ return true;
74
+ }
75
+ function startsWith(bytes, prefix) {
76
+ if (bytes.length < prefix.length)
77
+ return false;
78
+ for (let i = 0; i < prefix.length; i++)
79
+ if (bytes[i] !== prefix[i])
80
+ return false;
81
+ return true;
82
+ }
@@ -0,0 +1,11 @@
1
+ export { redeemRenewalToken, createRenewal, revokeRenewal, redeemCredential, issueAdminCredential, revokeSlotUser, verifyPresentation, verifyVpJwt, decodeJwtClaims, jwtExpMs, isJwtExpiringSoon, } from './verifier.js';
2
+ export type { RenewalGrant, IssuedToken, JwtClaims, RedemptionGrant } from './verifier.js';
3
+ export { credentialsCommitment, fetchHolderNonce, buildHolderProof, createHolderProof, ed25519DidKey, } from './holderProof.js';
4
+ export type { HolderSigner, HolderNonce, BuildHolderProofOpts } from './holderProof.js';
5
+ export { validate, evaluate, evaluateIdentityScoped, select, canonicalize, isOid4vpRule, jsonCredential, FORMAT_JWT_VC_JSON, FORMAT_DC_SD_JWT, FORMAT_OAUTH_AT_DPOP, ACCEPTED_FORMATS, isOauthFormat, resolvePath, DcqlMalformedError, MAX_RULE_LEN, MAX_AGE_SECS_MIN, MAX_AGE_SECS_MAX, } from './oid4vp.js';
6
+ export type { Query, CredentialQuery, CredentialSetQuery, ClaimQuery, ClaimPathSegment, Meta, ScopeNamespace, CredentialView, ClaimResult, Selection, } from './oid4vp.js';
7
+ export { createDpopKey, auth0DpopSigner, jwkThumbprint, accessTokenHash, isHeaderSafeNonce, } from './dpop.js';
8
+ export type { DpopSigner, DpopKey } from './dpop.js';
9
+ export { scopeCovers, MAX_IDENTITY_LEN } from './identityScope.js';
10
+ export { isOauthEnvelope, loadRego, packEnvelope, unpackEnvelope, lintEnvelope, evaluateEnvelope, RegoEnvelopeError, OAUTH_ENVELOPE_KEY, DEFAULT_REGO_ENTRYPOINT, REGO_POLICY_REFUSED, REGO_WASM_SHA256, } from './rego.js';
11
+ export type { OauthTokenSpec, OauthTokens, OauthPolicyInput, UnpackedEnvelope, EnvelopeGateReport, EnvelopeLint, OauthPresentation, EnvelopeVerdict, RegoEngine, LoadRegoOptions, } from './rego.js';
@@ -0,0 +1,13 @@
1
+ // Verifier auth (the DCQL-gated JWT lifecycle) + the DCQL policy grammar that gates it.
2
+ export { redeemRenewalToken, createRenewal, revokeRenewal, redeemCredential, issueAdminCredential, revokeSlotUser, verifyPresentation, verifyVpJwt, decodeJwtClaims, jwtExpMs, isJwtExpiringSoon, } from './verifier.js';
3
+ // F1/F4 holder proof-of-possession.
4
+ export { credentialsCommitment, fetchHolderNonce, buildHolderProof, createHolderProof, ed25519DidKey, } from './holderProof.js';
5
+ // OID4VP-DCQL - the ONLY rule language
6
+ export { validate, evaluate, evaluateIdentityScoped, select, canonicalize, isOid4vpRule, jsonCredential, FORMAT_JWT_VC_JSON, FORMAT_DC_SD_JWT, FORMAT_OAUTH_AT_DPOP, ACCEPTED_FORMATS, isOauthFormat, resolvePath, DcqlMalformedError, MAX_RULE_LEN, MAX_AGE_SECS_MIN, MAX_AGE_SECS_MAX, } from './oid4vp.js';
7
+ // DPoP (RFC 9449)
8
+ export { createDpopKey, auth0DpopSigner, jwkThumbprint, accessTokenHash, isHeaderSafeNonce, } from './dpop.js';
9
+ // identity-scope matcher
10
+ export { scopeCovers, MAX_IDENTITY_LEN } from './identityScope.js';
11
+ // OAuth policy envelopes (`kk_oauth`, ADR-0078): Rego over an organization's own access token.
12
+ // The grammar test is pure; pack/unpack/lint/evaluate run the network's engine (WebAssembly).
13
+ export { isOauthEnvelope, loadRego, packEnvelope, unpackEnvelope, lintEnvelope, evaluateEnvelope, RegoEnvelopeError, OAUTH_ENVELOPE_KEY, DEFAULT_REGO_ENTRYPOINT, REGO_POLICY_REFUSED, REGO_WASM_SHA256, } from './rego.js';