@t3foundry/tasra-sdk 0.0.0-stage → 0.3.0-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.
- package/CHANGELOG.md +148 -0
- package/LICENSE +202 -0
- package/NOTICE +94 -0
- package/README.md +137 -2
- package/SECURITY.md +115 -0
- package/dist/app/approvals.d.ts +50 -0
- package/dist/app/approvals.js +90 -0
- package/dist/app/authorization.d.ts +19 -0
- package/dist/app/authorization.js +20 -0
- package/dist/app/client.d.ts +231 -0
- package/dist/app/client.js +272 -0
- package/dist/app/creation.d.ts +73 -0
- package/dist/app/creation.js +77 -0
- package/dist/app/deployment.d.ts +36 -0
- package/dist/app/deployment.js +18 -0
- package/dist/app/identity.d.ts +211 -0
- package/dist/app/identity.js +287 -0
- package/dist/app/index.d.ts +22 -0
- package/dist/app/index.js +12 -0
- package/dist/app/manifest.d.ts +74 -0
- package/dist/app/manifest.js +120 -0
- package/dist/app/node.d.ts +30 -0
- package/dist/app/node.js +159 -0
- package/dist/app/ready-slot.d.ts +157 -0
- package/dist/app/ready-slot.js +199 -0
- package/dist/app/tasra-client.d.ts +125 -0
- package/dist/app/tasra-client.js +79 -0
- package/dist/app/viem.d.ts +10 -0
- package/dist/app/viem.js +32 -0
- package/dist/app/wallet.d.ts +126 -0
- package/dist/app/wallet.js +137 -0
- package/dist/auth/dpop.d.ts +86 -0
- package/dist/auth/dpop.js +142 -0
- package/dist/auth/holderProof.d.ts +102 -0
- package/dist/auth/holderProof.js +161 -0
- package/dist/auth/identityScope.d.ts +19 -0
- package/dist/auth/identityScope.js +82 -0
- package/dist/auth/index.d.ts +11 -0
- package/dist/auth/index.js +13 -0
- package/dist/auth/oid4vp.d.ts +230 -0
- package/dist/auth/oid4vp.js +780 -0
- package/dist/auth/rego/wasm/PINS.json +11 -0
- package/dist/auth/rego/wasm/keykeeper_rego_wasm.d.ts +76 -0
- package/dist/auth/rego/wasm/keykeeper_rego_wasm.js +379 -0
- package/dist/auth/rego/wasm/keykeeper_rego_wasm_bg.wasm +0 -0
- package/dist/auth/rego.d.ts +192 -0
- package/dist/auth/rego.js +492 -0
- package/dist/auth/verifier.d.ts +166 -0
- package/dist/auth/verifier.js +211 -0
- package/dist/chain/abis/accountantSetRegistry.d.ts +441 -0
- package/dist/chain/abis/accountantSetRegistry.js +574 -0
- package/dist/chain/abis/accountantSlashing.d.ts +2071 -0
- package/dist/chain/abis/accountantSlashing.js +2670 -0
- package/dist/chain/abis/bondingCurve.d.ts +398 -0
- package/dist/chain/abis/bondingCurve.js +521 -0
- package/dist/chain/abis/equivocationSlasher.d.ts +292 -0
- package/dist/chain/abis/equivocationSlasher.js +379 -0
- package/dist/chain/abis/fixedTasraPriceOracle.d.ts +52 -0
- package/dist/chain/abis/fixedTasraPriceOracle.js +72 -0
- package/dist/chain/abis/index.d.ts +12801 -0
- package/dist/chain/abis/index.js +64 -0
- package/dist/chain/abis/keeperShareRegistry.d.ts +519 -0
- package/dist/chain/abis/keeperShareRegistry.js +675 -0
- package/dist/chain/abis/keyRegistry.d.ts +2421 -0
- package/dist/chain/abis/keyRegistry.js +3114 -0
- package/dist/chain/abis/livenessRegistry.d.ts +402 -0
- package/dist/chain/abis/livenessRegistry.js +524 -0
- package/dist/chain/abis/mockEurc.d.ts +210 -0
- package/dist/chain/abis/mockEurc.js +276 -0
- package/dist/chain/abis/nodeRegistry.d.ts +1630 -0
- package/dist/chain/abis/nodeRegistry.js +2113 -0
- package/dist/chain/abis/platformExecutor.d.ts +328 -0
- package/dist/chain/abis/platformExecutor.js +429 -0
- package/dist/chain/abis/prevrandaoSaltBeacon.d.ts +16 -0
- package/dist/chain/abis/prevrandaoSaltBeacon.js +24 -0
- package/dist/chain/abis/serviceRegistry.d.ts +556 -0
- package/dist/chain/abis/serviceRegistry.js +719 -0
- package/dist/chain/abis/settlement.d.ts +1624 -0
- package/dist/chain/abis/settlement.js +2096 -0
- package/dist/chain/abis/tasraSwapRouter.d.ts +92 -0
- package/dist/chain/abis/tasraSwapRouter.js +122 -0
- package/dist/chain/abis/tasraToken.d.ts +455 -0
- package/dist/chain/abis/tasraToken.js +599 -0
- package/dist/chain/abis/tasraVestingVault.d.ts +165 -0
- package/dist/chain/abis/tasraVestingVault.js +215 -0
- package/dist/chain/abis/thresholdRandomBeacon.d.ts +378 -0
- package/dist/chain/abis/thresholdRandomBeacon.js +494 -0
- package/dist/chain/abis/treasury.d.ts +301 -0
- package/dist/chain/abis/treasury.js +390 -0
- package/dist/chain/abis/verifierSetRegistry.d.ts +445 -0
- package/dist/chain/abis/verifierSetRegistry.js +579 -0
- package/dist/chain/client.d.ts +212 -0
- package/dist/chain/client.js +576 -0
- package/dist/chain/commitmentRecovery.d.ts +21 -0
- package/dist/chain/commitmentRecovery.js +44 -0
- package/dist/chain/committeeClient.d.ts +102 -0
- package/dist/chain/committeeClient.js +137 -0
- package/dist/chain/deployments.d.ts +56 -0
- package/dist/chain/deployments.js +269 -0
- package/dist/chain/discovery.d.ts +49 -0
- package/dist/chain/discovery.js +107 -0
- package/dist/chain/events.d.ts +58 -0
- package/dist/chain/events.js +125 -0
- package/dist/chain/format.d.ts +25 -0
- package/dist/chain/format.js +51 -0
- package/dist/chain/index.d.ts +41 -0
- package/dist/chain/index.js +56 -0
- package/dist/chain/manifest.d.ts +165 -0
- package/dist/chain/manifest.js +132 -0
- package/dist/chain/network-profiles.json +75 -0
- package/dist/chain/networks.d.ts +73 -0
- package/dist/chain/networks.js +62 -0
- package/dist/chain/node.d.ts +44 -0
- package/dist/chain/node.js +176 -0
- package/dist/chain/offchain.d.ts +149 -0
- package/dist/chain/offchain.js +73 -0
- package/dist/chain/provisionRule.d.ts +164 -0
- package/dist/chain/provisionRule.js +212 -0
- package/dist/chain/registeredAgent.d.ts +64 -0
- package/dist/chain/registeredAgent.js +140 -0
- package/dist/chain/registeredOperation.d.ts +49 -0
- package/dist/chain/registeredOperation.js +136 -0
- package/dist/chain/registeredRelay.d.ts +206 -0
- package/dist/chain/registeredRelay.js +312 -0
- package/dist/chain/revertReason.d.ts +14 -0
- package/dist/chain/revertReason.js +62 -0
- package/dist/chain/ruleAmendment.d.ts +80 -0
- package/dist/chain/ruleAmendment.js +195 -0
- package/dist/chain/ruleAmendmentCore.d.ts +129 -0
- package/dist/chain/ruleAmendmentCore.js +152 -0
- package/dist/chain/ruleInput.d.ts +10 -0
- package/dist/chain/ruleInput.js +13 -0
- package/dist/chain/serviceIdentity.d.ts +176 -0
- package/dist/chain/serviceIdentity.js +237 -0
- package/dist/chain/serviceProfiles.d.ts +39 -0
- package/dist/chain/serviceProfiles.js +74 -0
- package/dist/chain/services.d.ts +141 -0
- package/dist/chain/services.js +79 -0
- package/dist/chain/slotClient.d.ts +55 -0
- package/dist/chain/slotClient.js +62 -0
- package/dist/chain/slotSeed.d.ts +31 -0
- package/dist/chain/slotSeed.js +98 -0
- package/dist/chain/write.d.ts +431 -0
- package/dist/chain/write.js +958 -0
- package/dist/client/client.d.ts +112 -0
- package/dist/client/client.js +111 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +2 -0
- package/dist/client/session.d.ts +67 -0
- package/dist/client/session.js +238 -0
- package/dist/committee/client.d.ts +284 -0
- package/dist/committee/client.js +351 -0
- package/dist/committee/dual-sign.d.ts +110 -0
- package/dist/committee/dual-sign.js +202 -0
- package/dist/committee/ecdsa.d.ts +36 -0
- package/dist/committee/ecdsa.js +45 -0
- package/dist/committee/extraction.d.ts +82 -0
- package/dist/committee/extraction.js +96 -0
- package/dist/committee/index.d.ts +19 -0
- package/dist/committee/index.js +13 -0
- package/dist/committee/oauth.d.ts +51 -0
- package/dist/committee/oauth.js +90 -0
- package/dist/committee/receipts.d.ts +51 -0
- package/dist/committee/receipts.js +59 -0
- package/dist/committee/request.d.ts +264 -0
- package/dist/committee/request.js +305 -0
- package/dist/committee/token.d.ts +274 -0
- package/dist/committee/token.js +418 -0
- package/dist/crypto/constants.d.ts +10 -0
- package/dist/crypto/constants.js +32 -0
- package/dist/crypto/detect.d.ts +20 -0
- package/dist/crypto/detect.js +44 -0
- package/dist/crypto/envelope.d.ts +60 -0
- package/dist/crypto/envelope.js +243 -0
- package/dist/crypto/frost.d.ts +49 -0
- package/dist/crypto/frost.js +152 -0
- package/dist/crypto/hex.d.ts +7 -0
- package/dist/crypto/hex.js +24 -0
- package/dist/crypto/ibe-blob.d.ts +114 -0
- package/dist/crypto/ibe-blob.js +208 -0
- package/dist/crypto/ibe.d.ts +88 -0
- package/dist/crypto/ibe.js +262 -0
- package/dist/crypto/index.d.ts +12 -0
- package/dist/crypto/index.js +12 -0
- package/dist/crypto/kem.d.ts +55 -0
- package/dist/crypto/kem.js +241 -0
- package/dist/decryption/client.d.ts +64 -0
- package/dist/decryption/client.js +82 -0
- package/dist/decryption/index.d.ts +2 -0
- package/dist/decryption/index.js +2 -0
- package/dist/errors.d.ts +124 -0
- package/dist/errors.js +167 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +98 -0
- package/dist/keys/index.d.ts +1 -0
- package/dist/keys/index.js +4 -0
- package/dist/keys/node-client.d.ts +27 -0
- package/dist/keys/node-client.js +97 -0
- package/dist/oid4vp/binding.d.ts +76 -0
- package/dist/oid4vp/binding.js +118 -0
- package/dist/oid4vp/did-web.d.ts +49 -0
- package/dist/oid4vp/did-web.js +57 -0
- package/dist/oid4vp/index.d.ts +9 -0
- package/dist/oid4vp/index.js +10 -0
- package/dist/oid4vp/jose.d.ts +166 -0
- package/dist/oid4vp/jose.js +242 -0
- package/dist/oid4vp/jwe.d.ts +30 -0
- package/dist/oid4vp/jwe.js +93 -0
- package/dist/oid4vp/oid4vci.d.ts +84 -0
- package/dist/oid4vp/oid4vci.js +85 -0
- package/dist/oid4vp/request-object.d.ts +109 -0
- package/dist/oid4vp/request-object.js +96 -0
- package/dist/oid4vp/sd-jwt.d.ts +177 -0
- package/dist/oid4vp/sd-jwt.js +208 -0
- package/dist/oid4vp/verifier-agent.d.ts +161 -0
- package/dist/oid4vp/verifier-agent.js +132 -0
- package/dist/oid4vp/wallet.d.ts +116 -0
- package/dist/oid4vp/wallet.js +108 -0
- package/dist/recipient/store.d.ts +67 -0
- package/dist/recipient/store.js +98 -0
- package/dist/signing/ecdsa.d.ts +49 -0
- package/dist/signing/ecdsa.js +79 -0
- package/dist/signing/frost.d.ts +88 -0
- package/dist/signing/frost.js +145 -0
- package/dist/signing/index.d.ts +4 -0
- package/dist/signing/index.js +3 -0
- package/dist/slots/faucet.d.ts +23 -0
- package/dist/slots/faucet.js +35 -0
- package/dist/slots/index.d.ts +2 -0
- package/dist/slots/index.js +4 -0
- package/dist/verifier-agent/index.d.ts +241 -0
- package/dist/verifier-agent/index.js +411 -0
- package/docs/DEVELOPER-EXPERIENCE.md +62 -0
- package/docs/README.md +33 -0
- package/docs/access-control.md +76 -0
- package/docs/ai-development.md +292 -0
- package/docs/api.md +338 -0
- package/docs/application-api.md +335 -0
- package/docs/architecture.md +65 -0
- package/docs/basics.md +48 -0
- package/docs/capabilities.md +86 -0
- package/docs/chain.md +153 -0
- package/docs/compatibility.md +36 -0
- package/docs/consumer-migrations.md +80 -0
- package/docs/create-slot.md +104 -0
- package/docs/document-signing.md +243 -0
- package/docs/encrypted-notes.md +129 -0
- package/docs/encryption.md +100 -0
- package/docs/errors.md +76 -0
- package/docs/flows.md +289 -0
- package/docs/fuji.md +127 -0
- package/docs/funding.md +126 -0
- package/docs/getting-started.md +94 -0
- package/docs/glossary.md +44 -0
- package/docs/installation.md +132 -0
- package/docs/manage-slot.md +95 -0
- package/docs/native-approvals.md +124 -0
- package/docs/prerequisites.md +137 -0
- package/docs/read-slot.md +41 -0
- package/docs/reference/README.md +30 -0
- package/docs/reference/app-node.md +102 -0
- package/docs/reference/app.md +1737 -0
- package/docs/reference/chain-node.md +159 -0
- package/docs/reference/chain.md +3609 -0
- package/docs/reference/committee.md +2176 -0
- package/docs/reference/main.md +4068 -0
- package/docs/reference/oid4vp.md +2547 -0
- package/docs/reference/verifier-agent.md +498 -0
- package/docs/shared-account.md +168 -0
- package/docs/signing-and-encryption.md +52 -0
- package/docs/signing.md +172 -0
- package/examples/committee-slot.ts +16 -0
- package/examples/connect-fuji.ts +41 -0
- package/examples/connect-network.ts +14 -0
- package/examples/credential-approvals.ts +46 -0
- package/examples/document-signing/README.md +59 -0
- package/examples/document-signing/backend.ts +132 -0
- package/examples/document-signing/build.mjs +19 -0
- package/examples/document-signing/config.ts +34 -0
- package/examples/document-signing/gitignore.template +9 -0
- package/examples/document-signing/model.ts +140 -0
- package/examples/document-signing/network.ts +58 -0
- package/examples/document-signing/package.json +25 -0
- package/examples/document-signing/public/index.html +27 -0
- package/examples/document-signing/public/sample.pdf +31 -0
- package/examples/document-signing/public/styles.css +531 -0
- package/examples/document-signing/server.ts +358 -0
- package/examples/document-signing/setup.ts +111 -0
- package/examples/document-signing/store.ts +105 -0
- package/examples/document-signing/tsconfig.json +24 -0
- package/examples/document-signing/verify.ts +24 -0
- package/examples/document-signing/web/app.ts +312 -0
- package/examples/document-signing/web/preview.ts +67 -0
- package/examples/document-signing/web/wallet.ts +146 -0
- package/examples/document-signing/workflow.test.ts +228 -0
- package/examples/document-signing/workflow.ts +241 -0
- package/examples/document-signing.ts +96 -0
- package/examples/encrypted-notes.ts +86 -0
- package/examples/ethers-signer.ts +204 -0
- package/examples/fund-slot.ts +73 -0
- package/examples/getting-started.ts +25 -0
- package/examples/learning-authorize.ts +23 -0
- package/examples/learning-cancel-slot.ts +11 -0
- package/examples/learning-context.ts +28 -0
- package/examples/learning-create-slot.ts +25 -0
- package/examples/learning-read-slot.ts +8 -0
- package/examples/learning-renew-slot.ts +12 -0
- package/examples/learning-sign.ts +8 -0
- package/examples/live-config.ts +71 -0
- package/examples/minimal.ts +6 -0
- package/examples/native-approvals.ts +57 -0
- package/examples/network.ts +58 -0
- package/examples/offline.ts +16 -0
- package/examples/personal-vault.ts +20 -0
- package/examples/provision-slot.ts +47 -0
- package/examples/shared-account.ts +46 -0
- package/examples/slot-address.ts +13 -0
- package/examples/slot-creation.ts +9 -0
- package/examples/tutorial-negative.ts +26 -0
- package/examples/tutorial-support.ts +76 -0
- package/examples/viem-account.ts +203 -0
- package/package.json +303 -4
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// OpenID4VCI pre-authorized credential issuance with holder-key proofs.
|
|
2
|
+
import { holderSigner, signCompactJws } from './jose.js';
|
|
3
|
+
/** URI scheme for a wallet credential offer. */
|
|
4
|
+
export const CREDENTIAL_OFFER_SCHEME = 'openid-credential-offer://';
|
|
5
|
+
/** OAuth grant identifier for credential issuance using a pre-authorized code. */
|
|
6
|
+
export const PRE_AUTHORIZED_GRANT = 'urn:ietf:params:oauth:grant-type:pre-authorized_code';
|
|
7
|
+
/** JOSE type identifier for an OpenID4VCI proof of key possession. */
|
|
8
|
+
export const PROOF_TYP = 'openid4vci-proof+jwt';
|
|
9
|
+
/**
|
|
10
|
+
* Parse an `openid-credential-offer://?credential_offer=...` or `...?credential_offer_uri=...` URI.
|
|
11
|
+
*
|
|
12
|
+
* @param uri - OpenID4VCI credential-offer URI containing an offer or offer URL.
|
|
13
|
+
*/
|
|
14
|
+
export function parseCredentialOfferUri(uri) {
|
|
15
|
+
const q = uri.includes('?') ? uri.slice(uri.indexOf('?') + 1) : '';
|
|
16
|
+
const params = new URLSearchParams(q);
|
|
17
|
+
const byValue = params.get('credential_offer');
|
|
18
|
+
const byRef = params.get('credential_offer_uri');
|
|
19
|
+
if (byValue)
|
|
20
|
+
return { offer: JSON.parse(byValue) };
|
|
21
|
+
if (byRef)
|
|
22
|
+
return { offerUri: byRef };
|
|
23
|
+
throw new Error('credential offer URI carries neither credential_offer nor credential_offer_uri');
|
|
24
|
+
}
|
|
25
|
+
async function getJson(f, url) {
|
|
26
|
+
const res = await f(url, { headers: { Accept: 'application/json' } });
|
|
27
|
+
if (!res.ok)
|
|
28
|
+
throw new Error(`${url} → HTTP ${res.status}`);
|
|
29
|
+
return (await res.json());
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Run the pre-authorized code flow end to end and return the issued credential.
|
|
33
|
+
*
|
|
34
|
+
* @param opts - Credential offer, holder key, optional transaction code and transport settings.
|
|
35
|
+
*/
|
|
36
|
+
export async function receiveCredential(opts) {
|
|
37
|
+
const f = opts.fetchImpl ?? fetch;
|
|
38
|
+
let offer = opts.offer;
|
|
39
|
+
if (!offer) {
|
|
40
|
+
if (!opts.offerUri)
|
|
41
|
+
throw new Error('receiveCredential needs an offer or an offerUri');
|
|
42
|
+
const parsed = parseCredentialOfferUri(opts.offerUri);
|
|
43
|
+
offer = parsed.offer ?? (await getJson(f, parsed.offerUri));
|
|
44
|
+
}
|
|
45
|
+
const grant = offer.grants?.[PRE_AUTHORIZED_GRANT];
|
|
46
|
+
const code = grant?.['pre-authorized_code'];
|
|
47
|
+
if (!code)
|
|
48
|
+
throw new Error('credential offer carries no pre-authorized code grant');
|
|
49
|
+
if (grant.tx_code && !opts.txCode)
|
|
50
|
+
throw new Error('this offer requires a transaction code (tx_code)');
|
|
51
|
+
const issuerUrl = offer.credential_issuer.replace(/\/$/, '');
|
|
52
|
+
const meta = await getJson(f, `${issuerUrl}/.well-known/openid-credential-issuer`);
|
|
53
|
+
const configId = opts.credentialConfigurationId ?? offer.credential_configuration_ids[0];
|
|
54
|
+
if (!configId)
|
|
55
|
+
throw new Error('credential offer names no credential configuration');
|
|
56
|
+
const config = meta.credential_configurations_supported[configId];
|
|
57
|
+
if (!config)
|
|
58
|
+
throw new Error(`issuer does not advertise configuration ${configId}`);
|
|
59
|
+
const asUrl = (grant.authorization_server ?? meta.authorization_servers?.[0] ?? issuerUrl).replace(/\/$/, '');
|
|
60
|
+
const asMeta = await getJson(f, `${asUrl}/.well-known/oauth-authorization-server`).catch(() => getJson(f, `${asUrl}/.well-known/openid-configuration`));
|
|
61
|
+
const tokenForm = new URLSearchParams({ grant_type: PRE_AUTHORIZED_GRANT, 'pre-authorized_code': code, ...(opts.txCode ? { tx_code: opts.txCode } : {}) });
|
|
62
|
+
const tokenRes = await f(asMeta.token_endpoint, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json' }, body: tokenForm.toString() });
|
|
63
|
+
if (!tokenRes.ok)
|
|
64
|
+
throw new Error(`token endpoint → HTTP ${tokenRes.status}: ${(await tokenRes.text().catch(() => '')).slice(0, 200)}`);
|
|
65
|
+
const token = (await tokenRes.json());
|
|
66
|
+
let nonce = token.c_nonce;
|
|
67
|
+
if (!nonce && meta.nonce_endpoint) {
|
|
68
|
+
const nres = await f(meta.nonce_endpoint, { method: 'POST' });
|
|
69
|
+
if (nres.ok)
|
|
70
|
+
nonce = (await nres.json()).c_nonce;
|
|
71
|
+
}
|
|
72
|
+
const proof = signCompactJws({ typ: PROOF_TYP, kid: `${opts.holder.did}#0` }, { aud: meta.credential_issuer, iat: opts.nowSecs ?? Math.floor(Date.now() / 1000), ...(nonce ? { nonce } : {}) }, holderSigner(opts.holder));
|
|
73
|
+
const credRes = await f(meta.credential_endpoint, {
|
|
74
|
+
method: 'POST',
|
|
75
|
+
headers: { 'Content-Type': 'application/json', Accept: 'application/json', Authorization: `Bearer ${token.access_token}` },
|
|
76
|
+
body: JSON.stringify({ credential_configuration_id: configId, proofs: { jwt: [proof] } }),
|
|
77
|
+
});
|
|
78
|
+
if (!credRes.ok)
|
|
79
|
+
throw new Error(`credential endpoint → HTTP ${credRes.status}: ${(await credRes.text().catch(() => '')).slice(0, 200)}`);
|
|
80
|
+
const body = (await credRes.json());
|
|
81
|
+
const credential = body.credentials?.[0]?.credential ?? body.credential;
|
|
82
|
+
if (!credential)
|
|
83
|
+
throw new Error('credential response carries no credential');
|
|
84
|
+
return { credential, configurationId: configId, format: config.format, issuer: meta.credential_issuer };
|
|
85
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { type ResolveOpts } from './did-web.js';
|
|
2
|
+
import { type EcJwk, type Jwk } from './jose.js';
|
|
3
|
+
import type { Query as DcqlQuery } from '../auth/oid4vp.js';
|
|
4
|
+
/** JOSE type identifier for a signed authorization request object. */
|
|
5
|
+
export declare const REQUEST_OBJECT_TYP = "oauth-authz-req+jwt";
|
|
6
|
+
/** OpenID4VP client identifier prefix for a DID-based verifier. */
|
|
7
|
+
export declare const CLIENT_ID_PREFIX_DID = "decentralized_identifier:";
|
|
8
|
+
/** OpenID4VP request claims carrying the credential query and wallet response context. */
|
|
9
|
+
export interface RequestObjectClaims {
|
|
10
|
+
/** DID of the request issuer. */
|
|
11
|
+
iss: string;
|
|
12
|
+
/** Verifier client identifier, optionally prefixed with decentralized_identifier:. */
|
|
13
|
+
client_id: string;
|
|
14
|
+
/** Requested OpenID4VP response type. */
|
|
15
|
+
response_type?: string;
|
|
16
|
+
/** `direct_post.jwt` (the verifier-agent always) or `direct_post`. */
|
|
17
|
+
response_mode?: string;
|
|
18
|
+
/** Endpoint receiving the wallet response. */
|
|
19
|
+
response_uri: string;
|
|
20
|
+
/** Challenge nonce for this operation. */
|
|
21
|
+
nonce: string;
|
|
22
|
+
/** Opaque state echoed in the wallet response. */
|
|
23
|
+
state: string;
|
|
24
|
+
/** Issue time in Unix seconds. */
|
|
25
|
+
iat?: number;
|
|
26
|
+
/** Expiration time in Unix seconds. */
|
|
27
|
+
exp?: number;
|
|
28
|
+
/** Credential requirements presented to the wallet. */
|
|
29
|
+
dcql_query: DcqlQuery;
|
|
30
|
+
/** Verifier metadata, including optional response encryption keys. */
|
|
31
|
+
client_metadata?: {
|
|
32
|
+
jwks?: {
|
|
33
|
+
keys: Jwk[];
|
|
34
|
+
};
|
|
35
|
+
vp_formats_supported?: Record<string, unknown>;
|
|
36
|
+
encrypted_response_enc_values_supported?: string[];
|
|
37
|
+
[k: string]: unknown;
|
|
38
|
+
};
|
|
39
|
+
/** Encoded operation context displayed or bound by the wallet. */
|
|
40
|
+
transaction_data?: string[];
|
|
41
|
+
[k: string]: unknown;
|
|
42
|
+
}
|
|
43
|
+
/** Request JWT, decoded claims and signing identity after request-object validation. */
|
|
44
|
+
export interface VerifiedRequestObject {
|
|
45
|
+
/** Original compact request JWT. */
|
|
46
|
+
jwt: string;
|
|
47
|
+
/** Verified request JOSE header. */
|
|
48
|
+
header: {
|
|
49
|
+
alg: string;
|
|
50
|
+
typ?: string;
|
|
51
|
+
kid?: string;
|
|
52
|
+
};
|
|
53
|
+
/** Validated request claims. */
|
|
54
|
+
claims: RequestObjectClaims;
|
|
55
|
+
/** The DID `iss` names, after the `client_id` prefix agreed with it. */
|
|
56
|
+
signerDid: string;
|
|
57
|
+
/** Public key used to verify the request signature. */
|
|
58
|
+
signerKey: Jwk;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* `openid4vp://?client_id=...&request_uri=...` (a QR payload or deep link) to its two parameters.
|
|
62
|
+
*
|
|
63
|
+
* @param uri - Wallet deep link or QR payload containing request_uri.
|
|
64
|
+
*/
|
|
65
|
+
export declare function parseOpenid4vpUri(uri: string): {
|
|
66
|
+
clientId?: string;
|
|
67
|
+
requestUri: string;
|
|
68
|
+
};
|
|
69
|
+
/** Resolve the signing key a JAR's `kid` names: `did:web` documents online, `did:key`/`did:jwk` offline. */
|
|
70
|
+
export type KeyResolver = (did: string, kid: string | undefined) => Promise<Jwk>;
|
|
71
|
+
/**
|
|
72
|
+
* Create a signing-key resolver that fetches did:web keys and decodes did:key or did:jwk keys.
|
|
73
|
+
*
|
|
74
|
+
* @param opts - Fetch override and optional loopback HTTP permission.
|
|
75
|
+
*/
|
|
76
|
+
export declare function defaultKeyResolver(opts?: ResolveOpts): KeyResolver;
|
|
77
|
+
/** Signing-key resolver, evaluation time and expiration tolerance for request validation. */
|
|
78
|
+
export interface VerifyRequestObjectOpts {
|
|
79
|
+
/** Resolver supplying a trusted public key for the request issuer and key identifier. */
|
|
80
|
+
resolveKey?: KeyResolver;
|
|
81
|
+
/** Current time override in Unix seconds. */
|
|
82
|
+
nowSecs?: number;
|
|
83
|
+
/** Seconds of clock skew tolerated on `exp`. */
|
|
84
|
+
leewaySecs?: number;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Verify the request signature, JOSE type and agreement between client_id and the
|
|
88
|
+
* issuer DID. Check expiration only when exp is a number; absence or a nonnumeric
|
|
89
|
+
* value is not rejected by this helper. Require a nonce, response URI and DCQL query.
|
|
90
|
+
*
|
|
91
|
+
* @param jwt - Signed OpenID4VP request JWT.
|
|
92
|
+
* @param opts - Trusted key resolver, current time and expiration tolerance.
|
|
93
|
+
*/
|
|
94
|
+
export declare function verifyRequestObject(jwt: string, opts?: VerifyRequestObjectOpts): Promise<VerifiedRequestObject>;
|
|
95
|
+
/**
|
|
96
|
+
* The ephemeral P-256 key the wallet must encrypt its response to, when the verifier-agent served one.
|
|
97
|
+
*
|
|
98
|
+
* @param ro - Verified request claims containing optional recipient encryption keys.
|
|
99
|
+
*/
|
|
100
|
+
export declare function responseEncryptionKey(ro: Pick<VerifiedRequestObject, 'claims'>): EcJwk | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Fetch a JAR from `request_uri` (`Accept: application/oauth-authz-req+jwt`) and verify it.
|
|
103
|
+
*
|
|
104
|
+
* @param requestUri - URL from the OpenID4VP request_uri parameter.
|
|
105
|
+
* @param opts - Request signature verification settings and optional fetch implementation.
|
|
106
|
+
*/
|
|
107
|
+
export declare function fetchRequestObject(requestUri: string, opts?: VerifyRequestObjectOpts & {
|
|
108
|
+
fetchImpl?: typeof fetch;
|
|
109
|
+
}): Promise<VerifiedRequestObject>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// The signed Request Object (JAR) the Verifier Agent serves at `request_uri`, verified the way a
|
|
2
|
+
// standards wallet verifies it: `iss` is the platform's `did:web`, the JWS `kid` names one of that
|
|
3
|
+
// document's verification methods, `client_id` is the DID under the `decentralized_identifier:`
|
|
4
|
+
// prefix, `typ` is `oauth-authz-req+jwt`. Nothing here is Tasra-specific; the committee
|
|
5
|
+
// binding rides in the `nonce`, which the wallet copies into its KB-JWT without interpreting it.
|
|
6
|
+
import { resolveDidWeb, verificationKey } from './did-web.js';
|
|
7
|
+
import { decodeJson, jwkFromDid, verifyCompactJws } from './jose.js';
|
|
8
|
+
/** JOSE type identifier for a signed authorization request object. */
|
|
9
|
+
export const REQUEST_OBJECT_TYP = 'oauth-authz-req+jwt';
|
|
10
|
+
/** OpenID4VP client identifier prefix for a DID-based verifier. */
|
|
11
|
+
export const CLIENT_ID_PREFIX_DID = 'decentralized_identifier:';
|
|
12
|
+
/**
|
|
13
|
+
* `openid4vp://?client_id=...&request_uri=...` (a QR payload or deep link) to its two parameters.
|
|
14
|
+
*
|
|
15
|
+
* @param uri - Wallet deep link or QR payload containing request_uri.
|
|
16
|
+
*/
|
|
17
|
+
export function parseOpenid4vpUri(uri) {
|
|
18
|
+
const q = uri.includes('?') ? uri.slice(uri.indexOf('?') + 1) : '';
|
|
19
|
+
const params = new URLSearchParams(q);
|
|
20
|
+
const requestUri = params.get('request_uri');
|
|
21
|
+
if (!requestUri)
|
|
22
|
+
throw new Error('openid4vp URI carries no request_uri');
|
|
23
|
+
return { clientId: params.get('client_id') ?? undefined, requestUri };
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create a signing-key resolver that fetches did:web keys and decodes did:key or did:jwk keys.
|
|
27
|
+
*
|
|
28
|
+
* @param opts - Fetch override and optional loopback HTTP permission.
|
|
29
|
+
*/
|
|
30
|
+
export function defaultKeyResolver(opts = {}) {
|
|
31
|
+
return async (did, kid) => {
|
|
32
|
+
if (did.startsWith('did:web:')) {
|
|
33
|
+
const doc = await resolveDidWeb(did, opts);
|
|
34
|
+
return verificationKey(doc, kid ?? `${did}#`);
|
|
35
|
+
}
|
|
36
|
+
return jwkFromDid(did);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Verify the request signature, JOSE type and agreement between client_id and the
|
|
41
|
+
* issuer DID. Check expiration only when exp is a number; absence or a nonnumeric
|
|
42
|
+
* value is not rejected by this helper. Require a nonce, response URI and DCQL query.
|
|
43
|
+
*
|
|
44
|
+
* @param jwt - Signed OpenID4VP request JWT.
|
|
45
|
+
* @param opts - Trusted key resolver, current time and expiration tolerance.
|
|
46
|
+
*/
|
|
47
|
+
export async function verifyRequestObject(jwt, opts = {}) {
|
|
48
|
+
const [h, p, s] = jwt.split('.');
|
|
49
|
+
if (!h || !p || !s)
|
|
50
|
+
throw new Error('request object is not a compact JWS');
|
|
51
|
+
const header = decodeJson(h);
|
|
52
|
+
if (header.typ !== REQUEST_OBJECT_TYP)
|
|
53
|
+
throw new Error(`request object typ must be ${REQUEST_OBJECT_TYP}`);
|
|
54
|
+
const claims = decodeJson(p);
|
|
55
|
+
if (typeof claims.iss !== 'string' || !claims.iss.startsWith('did:'))
|
|
56
|
+
throw new Error('request object iss is not a DID');
|
|
57
|
+
const bareClientId = claims.client_id.startsWith(CLIENT_ID_PREFIX_DID) ? claims.client_id.slice(CLIENT_ID_PREFIX_DID.length) : claims.client_id;
|
|
58
|
+
if (bareClientId !== claims.iss)
|
|
59
|
+
throw new Error('request object client_id does not name its iss');
|
|
60
|
+
if (header.kid && !header.kid.startsWith(claims.iss))
|
|
61
|
+
throw new Error('request object kid belongs to a different DID than iss');
|
|
62
|
+
const signerKey = await (opts.resolveKey ?? defaultKeyResolver())(claims.iss, header.kid);
|
|
63
|
+
verifyCompactJws(jwt, signerKey);
|
|
64
|
+
const now = opts.nowSecs ?? Math.floor(Date.now() / 1000);
|
|
65
|
+
if (typeof claims.exp === 'number' && claims.exp + (opts.leewaySecs ?? 60) < now)
|
|
66
|
+
throw new Error('request object expired');
|
|
67
|
+
if (typeof claims.nonce !== 'string' || !claims.nonce)
|
|
68
|
+
throw new Error('request object has no nonce');
|
|
69
|
+
if (typeof claims.response_uri !== 'string')
|
|
70
|
+
throw new Error('request object has no response_uri');
|
|
71
|
+
if (!claims.dcql_query)
|
|
72
|
+
throw new Error('request object has no dcql_query');
|
|
73
|
+
return { jwt, header, claims, signerDid: claims.iss, signerKey };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The ephemeral P-256 key the wallet must encrypt its response to, when the verifier-agent served one.
|
|
77
|
+
*
|
|
78
|
+
* @param ro - Verified request claims containing optional recipient encryption keys.
|
|
79
|
+
*/
|
|
80
|
+
export function responseEncryptionKey(ro) {
|
|
81
|
+
const k = ro.claims.client_metadata?.jwks?.keys?.[0];
|
|
82
|
+
return k && k.kty === 'EC' && k.crv === 'P-256' ? k : undefined;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Fetch a JAR from `request_uri` (`Accept: application/oauth-authz-req+jwt`) and verify it.
|
|
86
|
+
*
|
|
87
|
+
* @param requestUri - URL from the OpenID4VP request_uri parameter.
|
|
88
|
+
* @param opts - Request signature verification settings and optional fetch implementation.
|
|
89
|
+
*/
|
|
90
|
+
export async function fetchRequestObject(requestUri, opts = {}) {
|
|
91
|
+
const f = opts.fetchImpl ?? fetch;
|
|
92
|
+
const res = await f(requestUri, { headers: { Accept: `application/${REQUEST_OBJECT_TYP}` } });
|
|
93
|
+
if (!res.ok)
|
|
94
|
+
throw new Error(`request_uri ${requestUri} → HTTP ${res.status}: ${(await res.text().catch(() => '')).slice(0, 200)}`);
|
|
95
|
+
return verifyRequestObject((await res.text()).trim(), opts);
|
|
96
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { type CredentialView } from '../auth/oid4vp.js';
|
|
2
|
+
import { type HolderKey, type JwsSigner, type Jwk } from './jose.js';
|
|
3
|
+
/** JOSE type identifier for a selectively disclosable verifiable credential. */
|
|
4
|
+
export declare const SD_JWT_TYP = "dc+sd-jwt";
|
|
5
|
+
/** JOSE type identifier for the holder key-binding JWT. */
|
|
6
|
+
export declare const KB_JWT_TYP = "kb+jwt";
|
|
7
|
+
/** One top-level SD-JWT claim disclosure with its encoded value and digest. */
|
|
8
|
+
export interface Disclosure {
|
|
9
|
+
/** base64url(JSON([salt, name, value])) - what travels on the wire. */
|
|
10
|
+
encoded: string;
|
|
11
|
+
/** Random salt decoded from the disclosure. */
|
|
12
|
+
salt: string;
|
|
13
|
+
/** Top-level claim name. */
|
|
14
|
+
name: string;
|
|
15
|
+
/** Disclosed claim value. */
|
|
16
|
+
value: unknown;
|
|
17
|
+
/** base64url(sha256(encoded)) - what the issuer JWT's `_sd` array holds. */
|
|
18
|
+
digest: string;
|
|
19
|
+
}
|
|
20
|
+
/** Decoded SD-JWT components with disclosure hashes checked; issuer signature verification is separate. */
|
|
21
|
+
export interface ParsedSdJwt {
|
|
22
|
+
/** Original compact SD-JWT credential or presentation. */
|
|
23
|
+
compact: string;
|
|
24
|
+
/** The issuer-signed JWT (first `~`-segment). */
|
|
25
|
+
issuerJwt: string;
|
|
26
|
+
/** Decoded issuer JWT header; not authenticated by parsing alone. */
|
|
27
|
+
header: Record<string, unknown>;
|
|
28
|
+
/** Decoded issuer JWT claims; not authenticated by parsing alone. */
|
|
29
|
+
payload: Record<string, unknown>;
|
|
30
|
+
/** Decoded claim disclosures whose hashes match the issuer payload. */
|
|
31
|
+
disclosures: Disclosure[];
|
|
32
|
+
/** The Key Binding JWT, when the compact carries one (a presentation). */
|
|
33
|
+
kbJwt?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Compute the base64url SHA-256 digest of an encoded SD-JWT disclosure.
|
|
37
|
+
*
|
|
38
|
+
* @param encoded - Base64url-encoded SD-JWT disclosure string.
|
|
39
|
+
*/
|
|
40
|
+
export declare function disclosureDigest(encoded: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* Parse a compact SD-JWT and check each disclosed claim against the issuer payload's disclosure hashes. This does not verify the issuer signature.
|
|
43
|
+
*
|
|
44
|
+
* @param compact - Compact SD-JWT credential or presentation.
|
|
45
|
+
*/
|
|
46
|
+
export declare function parseSdJwt(compact: string): ParsedSdJwt;
|
|
47
|
+
/**
|
|
48
|
+
* The credential's claims as the verifier sees them: plain payload claims + disclosed ones.
|
|
49
|
+
*
|
|
50
|
+
* @param parsed - Parsed credential with disclosure hashes already checked.
|
|
51
|
+
*/
|
|
52
|
+
export declare function sdJwtClaims(parsed: ParsedSdJwt): Record<string, unknown>;
|
|
53
|
+
/**
|
|
54
|
+
* A {@link CredentialView} for the DCQL evaluator: format `dc+sd-jwt`, `types` = [`vct`].
|
|
55
|
+
*
|
|
56
|
+
* @param parsed - Parsed credential to expose for advisory DCQL matching.
|
|
57
|
+
*/
|
|
58
|
+
export declare function sdJwtCredentialView(parsed: ParsedSdJwt): CredentialView;
|
|
59
|
+
/**
|
|
60
|
+
* `base64url(sha256(prefix))` where `prefix` is everything before the KB-JWT, trailing `~` included.
|
|
61
|
+
*
|
|
62
|
+
* @param prefix - Presentation prefix before the key-binding JWT, including the trailing tilde.
|
|
63
|
+
*/
|
|
64
|
+
export declare function sdHash(prefix: string): string;
|
|
65
|
+
/** Issuer DID, optional signing key identifier and compact JWS signer. */
|
|
66
|
+
export interface SdJwtIssuer {
|
|
67
|
+
/** The issuer DID the credential's `iss` names; `kid` = `${did}#${fragment}` unless given. */
|
|
68
|
+
did: string;
|
|
69
|
+
/** Optional signing key identifier. */
|
|
70
|
+
kid?: string;
|
|
71
|
+
/** Private-key signer for the issuer JWT. */
|
|
72
|
+
signer: JwsSigner;
|
|
73
|
+
}
|
|
74
|
+
/** Issuer, claims, holder binding and lifetime for a top-level selective-disclosure credential. */
|
|
75
|
+
export interface IssueSdJwtVcOpts {
|
|
76
|
+
/** Issuer identity and signing key. */
|
|
77
|
+
issuer: SdJwtIssuer;
|
|
78
|
+
/** Verifiable credential type identifier. */
|
|
79
|
+
vct: string;
|
|
80
|
+
/** Every claim goes into a disclosure unless named in `plain` (which then travels in the clear). */
|
|
81
|
+
claims: Record<string, unknown>;
|
|
82
|
+
/** Claim names to include directly in the issuer JWT instead of selective disclosures. */
|
|
83
|
+
plain?: string[];
|
|
84
|
+
/** The holder's key binding: `{kid: did:jwk...#0}` (Hovi's shape) or `{jwk}`. */
|
|
85
|
+
cnf: {
|
|
86
|
+
kid: string;
|
|
87
|
+
} | {
|
|
88
|
+
jwk: Jwk;
|
|
89
|
+
};
|
|
90
|
+
/** Subject DID, when the credential names one (`sub`); the verifier derives the holder from it first. */
|
|
91
|
+
sub?: string;
|
|
92
|
+
/** Current time override in Unix seconds. */
|
|
93
|
+
nowSecs?: number;
|
|
94
|
+
/** Credential lifetime in seconds; defaults to 365 days. */
|
|
95
|
+
ttlSecs?: number;
|
|
96
|
+
/** Extra header members (e.g. a `typ` override); `alg` and `kid` are set here. */
|
|
97
|
+
header?: Record<string, unknown>;
|
|
98
|
+
/** Secure random bytes used to salt each disclosure; defaults to 16 Web Crypto bytes. */
|
|
99
|
+
random?: () => Uint8Array;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Mint a compact SD-JWT VC `issuer~d1~...~` with one disclosure per selectively disclosable claim.
|
|
103
|
+
*
|
|
104
|
+
* @param opts - Issuer key, claim values, holder binding and credential lifetime.
|
|
105
|
+
*/
|
|
106
|
+
export declare function issueSdJwtVc(opts: IssueSdJwtVcOpts): string;
|
|
107
|
+
/**
|
|
108
|
+
* The `cnf` a holder key binds to: `{kid: "<did:jwk>#0"}`, exactly as the Hovi wallet presents.
|
|
109
|
+
*
|
|
110
|
+
* @param holder - Holder did:jwk identifier to reference in the credential binding.
|
|
111
|
+
*/
|
|
112
|
+
export declare function holderCnf(holder: Pick<HolderKey, 'did'>): {
|
|
113
|
+
kid: string;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* A P-256 issuer as `did:key` (Hovi Studio's issuer shape) from a private scalar.
|
|
117
|
+
*
|
|
118
|
+
* @param privateKey - 32-byte P-256 private scalar.
|
|
119
|
+
* @param opts - Whether kid should contain only the DID fragment.
|
|
120
|
+
*/
|
|
121
|
+
export declare function p256DidKeyIssuer(privateKey: Uint8Array, opts?: {
|
|
122
|
+
fragmentKid?: boolean;
|
|
123
|
+
}): SdJwtIssuer;
|
|
124
|
+
/**
|
|
125
|
+
* A P-256/ES256 `did:jwk` issuer from a private scalar. Other issuer algorithms are not supported by this helper.
|
|
126
|
+
*
|
|
127
|
+
* @param privateKey - 32-byte P-256 private scalar.
|
|
128
|
+
* @param alg - Signing algorithm; this helper accepts only ES256.
|
|
129
|
+
*/
|
|
130
|
+
export declare function didJwkIssuer(privateKey: Uint8Array, alg?: 'ES256'): SdJwtIssuer;
|
|
131
|
+
/** Selected disclosures and holder key for a nonce-bound, audience-bound SD-JWT presentation. */
|
|
132
|
+
export interface PresentSdJwtOpts {
|
|
133
|
+
/** Parsed SD-JWT credential with disclosure hashes checked. */
|
|
134
|
+
parsed: ParsedSdJwt;
|
|
135
|
+
/** Which disclosures to reveal: claim names, or `'all'`. Undisclosed claims stay hidden. */
|
|
136
|
+
disclose: 'all' | readonly string[];
|
|
137
|
+
/** Holder key matching the credential confirmation claim. */
|
|
138
|
+
holder: HolderKey;
|
|
139
|
+
/** The JAR's `nonce`. */
|
|
140
|
+
nonce: string;
|
|
141
|
+
/** The JAR's `client_id`, VERBATIM (prefix included). */
|
|
142
|
+
aud: string;
|
|
143
|
+
/** Current time override in Unix seconds. */
|
|
144
|
+
nowSecs?: number;
|
|
145
|
+
/** Extra KB-JWT claims (e.g. `transaction_data_hashes`). */
|
|
146
|
+
extraKbClaims?: Record<string, unknown>;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Build the presentation `issuer~selected...~kb-jwt`, the KB-JWT signed by the holder's own key.
|
|
150
|
+
*
|
|
151
|
+
* @param opts - Parsed credential, disclosure selection, holder key, nonce and audience.
|
|
152
|
+
*/
|
|
153
|
+
export declare function presentSdJwt(opts: PresentSdJwtOpts): string;
|
|
154
|
+
/**
|
|
155
|
+
* Verify the holder signature, presentation hash, nonce and audience of a key-binding JWT. This does not verify the issuer signature, credential lifetime or issuer trust.
|
|
156
|
+
*
|
|
157
|
+
* @param presentation - Compact SD-JWT presentation including its key-binding JWT.
|
|
158
|
+
* @param expected - Expected wallet challenge nonce and verifier audience.
|
|
159
|
+
*/
|
|
160
|
+
export declare function verifyKbJwt(presentation: string, expected: {
|
|
161
|
+
nonce: string;
|
|
162
|
+
aud: string;
|
|
163
|
+
}): {
|
|
164
|
+
holderJwk: Jwk;
|
|
165
|
+
claims: Record<string, unknown>;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Decode (no verification) the issuer JWT's payload of a compact SD-JWT - for display.
|
|
169
|
+
*
|
|
170
|
+
* @param compact - Compact SD-JWT credential or presentation to inspect without signature verification.
|
|
171
|
+
*/
|
|
172
|
+
export declare function peekSdJwt(compact: string): {
|
|
173
|
+
iss?: string;
|
|
174
|
+
vct?: string;
|
|
175
|
+
exp?: number;
|
|
176
|
+
sub?: string;
|
|
177
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// SD-JWT credentials with top-level selective disclosures and holder-key binding.
|
|
2
|
+
// Nested selective disclosures and array decoys are not supported.
|
|
3
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
4
|
+
import { jsonCredential, FORMAT_DC_SD_JWT } from '../auth/oid4vp.js';
|
|
5
|
+
import { b64url, b64urlDecode, decodeJson, didJwk, holderSigner, jwkFromDid, p256DidKey, p256PublicJwk, signCompactJws, utf8, verifyCompactJws } from './jose.js';
|
|
6
|
+
/** JOSE type identifier for a selectively disclosable verifiable credential. */
|
|
7
|
+
export const SD_JWT_TYP = 'dc+sd-jwt';
|
|
8
|
+
/** JOSE type identifier for the holder key-binding JWT. */
|
|
9
|
+
export const KB_JWT_TYP = 'kb+jwt';
|
|
10
|
+
/**
|
|
11
|
+
* Compute the base64url SHA-256 digest of an encoded SD-JWT disclosure.
|
|
12
|
+
*
|
|
13
|
+
* @param encoded - Base64url-encoded SD-JWT disclosure string.
|
|
14
|
+
*/
|
|
15
|
+
export function disclosureDigest(encoded) {
|
|
16
|
+
return b64url(sha256(utf8(encoded)));
|
|
17
|
+
}
|
|
18
|
+
function decodeDisclosure(encoded) {
|
|
19
|
+
const arr = decodeJson(encoded);
|
|
20
|
+
if (!Array.isArray(arr) || arr.length !== 3 || typeof arr[0] !== 'string' || typeof arr[1] !== 'string') {
|
|
21
|
+
throw new Error('SD-JWT disclosure is not [salt, name, value]');
|
|
22
|
+
}
|
|
23
|
+
return { encoded, salt: arr[0], name: arr[1], value: arr[2], digest: disclosureDigest(encoded) };
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Parse a compact SD-JWT and check each disclosed claim against the issuer payload's disclosure hashes. This does not verify the issuer signature.
|
|
27
|
+
*
|
|
28
|
+
* @param compact - Compact SD-JWT credential or presentation.
|
|
29
|
+
*/
|
|
30
|
+
export function parseSdJwt(compact) {
|
|
31
|
+
const parts = compact.split('~');
|
|
32
|
+
const issuerJwt = parts[0];
|
|
33
|
+
if (!issuerJwt || issuerJwt.split('.').length !== 3)
|
|
34
|
+
throw new Error('SD-JWT: first segment is not a compact JWS');
|
|
35
|
+
const [h, p] = issuerJwt.split('.');
|
|
36
|
+
const header = decodeJson(h);
|
|
37
|
+
const payload = decodeJson(p);
|
|
38
|
+
const trailing = compact.endsWith('~');
|
|
39
|
+
const middle = trailing ? parts.slice(1, -1) : parts.slice(1, -1);
|
|
40
|
+
const kbJwt = trailing ? undefined : parts[parts.length - 1];
|
|
41
|
+
if (kbJwt !== undefined && kbJwt.split('.').length !== 3)
|
|
42
|
+
throw new Error('SD-JWT: trailing segment is neither empty nor a KB-JWT');
|
|
43
|
+
const sd = new Set(Array.isArray(payload._sd) ? payload._sd : []);
|
|
44
|
+
const disclosures = middle.filter(d => d !== '').map(decodeDisclosure);
|
|
45
|
+
for (const d of disclosures) {
|
|
46
|
+
if (!sd.has(d.digest))
|
|
47
|
+
throw new Error(`SD-JWT: disclosure ${JSON.stringify(d.name)} is not in the issuer's _sd`);
|
|
48
|
+
}
|
|
49
|
+
return { compact, issuerJwt, header, payload, disclosures, kbJwt };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The credential's claims as the verifier sees them: plain payload claims + disclosed ones.
|
|
53
|
+
*
|
|
54
|
+
* @param parsed - Parsed credential with disclosure hashes already checked.
|
|
55
|
+
*/
|
|
56
|
+
export function sdJwtClaims(parsed) {
|
|
57
|
+
const out = {};
|
|
58
|
+
for (const [k, v] of Object.entries(parsed.payload)) {
|
|
59
|
+
if (k === '_sd' || k === '_sd_alg')
|
|
60
|
+
continue;
|
|
61
|
+
out[k] = v;
|
|
62
|
+
}
|
|
63
|
+
for (const d of parsed.disclosures)
|
|
64
|
+
out[d.name] = d.value;
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* A {@link CredentialView} for the DCQL evaluator: format `dc+sd-jwt`, `types` = [`vct`].
|
|
69
|
+
*
|
|
70
|
+
* @param parsed - Parsed credential to expose for advisory DCQL matching.
|
|
71
|
+
*/
|
|
72
|
+
export function sdJwtCredentialView(parsed) {
|
|
73
|
+
const claims = sdJwtClaims(parsed);
|
|
74
|
+
const vct = typeof claims.vct === 'string' ? [claims.vct] : [];
|
|
75
|
+
return jsonCredential({ format: FORMAT_DC_SD_JWT, types: vct, body: claims });
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* `base64url(sha256(prefix))` where `prefix` is everything before the KB-JWT, trailing `~` included.
|
|
79
|
+
*
|
|
80
|
+
* @param prefix - Presentation prefix before the key-binding JWT, including the trailing tilde.
|
|
81
|
+
*/
|
|
82
|
+
export function sdHash(prefix) {
|
|
83
|
+
return b64url(sha256(utf8(prefix)));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Mint a compact SD-JWT VC `issuer~d1~...~` with one disclosure per selectively disclosable claim.
|
|
87
|
+
*
|
|
88
|
+
* @param opts - Issuer key, claim values, holder binding and credential lifetime.
|
|
89
|
+
*/
|
|
90
|
+
export function issueSdJwtVc(opts) {
|
|
91
|
+
const now = opts.nowSecs ?? Math.floor(Date.now() / 1000);
|
|
92
|
+
const random = opts.random ?? (() => crypto.getRandomValues(new Uint8Array(16)));
|
|
93
|
+
const plain = new Set(opts.plain ?? []);
|
|
94
|
+
const disclosures = [];
|
|
95
|
+
const clear = {};
|
|
96
|
+
for (const [name, value] of Object.entries(opts.claims)) {
|
|
97
|
+
if (plain.has(name)) {
|
|
98
|
+
clear[name] = value;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const encoded = b64url(JSON.stringify([b64url(random()), name, value]));
|
|
102
|
+
disclosures.push({ encoded, salt: '', name, value, digest: disclosureDigest(encoded) });
|
|
103
|
+
}
|
|
104
|
+
const payload = {
|
|
105
|
+
iss: opts.issuer.did,
|
|
106
|
+
iat: now,
|
|
107
|
+
exp: now + (opts.ttlSecs ?? 365 * 86400),
|
|
108
|
+
vct: opts.vct,
|
|
109
|
+
cnf: opts.cnf,
|
|
110
|
+
...(opts.sub !== undefined ? { sub: opts.sub } : {}),
|
|
111
|
+
...clear,
|
|
112
|
+
_sd_alg: 'sha-256',
|
|
113
|
+
_sd: disclosures.map(d => d.digest).sort(),
|
|
114
|
+
};
|
|
115
|
+
const kid = opts.issuer.kid ?? `${opts.issuer.did}#${opts.issuer.did.split(':').pop()}`;
|
|
116
|
+
const jwt = signCompactJws({ typ: SD_JWT_TYP, kid, ...(opts.header ?? {}) }, payload, opts.issuer.signer);
|
|
117
|
+
return `${jwt}~${disclosures.map(d => d.encoded).join('~')}${disclosures.length ? '~' : ''}`;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The `cnf` a holder key binds to: `{kid: "<did:jwk>#0"}`, exactly as the Hovi wallet presents.
|
|
121
|
+
*
|
|
122
|
+
* @param holder - Holder did:jwk identifier to reference in the credential binding.
|
|
123
|
+
*/
|
|
124
|
+
export function holderCnf(holder) {
|
|
125
|
+
return { kid: `${holder.did}#0` };
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* A P-256 issuer as `did:key` (Hovi Studio's issuer shape) from a private scalar.
|
|
129
|
+
*
|
|
130
|
+
* @param privateKey - 32-byte P-256 private scalar.
|
|
131
|
+
* @param opts - Whether kid should contain only the DID fragment.
|
|
132
|
+
*/
|
|
133
|
+
export function p256DidKeyIssuer(privateKey, opts = {}) {
|
|
134
|
+
const pub = p256PublicJwk(privateKey);
|
|
135
|
+
const raw = new Uint8Array([0x04, ...b64urlDecode(pub.x), ...b64urlDecode(pub.y)]);
|
|
136
|
+
const did = p256DidKey(raw);
|
|
137
|
+
const fragment = did.slice('did:key:'.length);
|
|
138
|
+
return { did, kid: opts.fragmentKid ? `#${fragment}` : `${did}#${fragment}`, signer: { alg: 'ES256', privateKey } };
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* A P-256/ES256 `did:jwk` issuer from a private scalar. Other issuer algorithms are not supported by this helper.
|
|
142
|
+
*
|
|
143
|
+
* @param privateKey - 32-byte P-256 private scalar.
|
|
144
|
+
* @param alg - Signing algorithm; this helper accepts only ES256.
|
|
145
|
+
*/
|
|
146
|
+
export function didJwkIssuer(privateKey, alg = 'ES256') {
|
|
147
|
+
if (alg !== 'ES256')
|
|
148
|
+
throw new Error('didJwkIssuer: only P-256 / ES256 is supported');
|
|
149
|
+
const did = didJwk(p256PublicJwk(privateKey));
|
|
150
|
+
return { did, kid: `${did}#0`, signer: { alg, privateKey } };
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Build the presentation `issuer~selected...~kb-jwt`, the KB-JWT signed by the holder's own key.
|
|
154
|
+
*
|
|
155
|
+
* @param opts - Parsed credential, disclosure selection, holder key, nonce and audience.
|
|
156
|
+
*/
|
|
157
|
+
export function presentSdJwt(opts) {
|
|
158
|
+
const want = opts.disclose === 'all' ? null : new Set(opts.disclose);
|
|
159
|
+
const selected = opts.parsed.disclosures.filter(d => want === null || want.has(d.name));
|
|
160
|
+
const prefix = `${opts.parsed.issuerJwt}~${selected.map(d => d.encoded).join('~')}${selected.length ? '~' : ''}`;
|
|
161
|
+
const kb = signCompactJws({ typ: KB_JWT_TYP }, {
|
|
162
|
+
nonce: opts.nonce,
|
|
163
|
+
aud: opts.aud,
|
|
164
|
+
iat: opts.nowSecs ?? Math.floor(Date.now() / 1000),
|
|
165
|
+
sd_hash: sdHash(prefix),
|
|
166
|
+
...(opts.extraKbClaims ?? {}),
|
|
167
|
+
}, holderSigner(opts.holder));
|
|
168
|
+
return `${prefix}${kb}`;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Verify the holder signature, presentation hash, nonce and audience of a key-binding JWT. This does not verify the issuer signature, credential lifetime or issuer trust.
|
|
172
|
+
*
|
|
173
|
+
* @param presentation - Compact SD-JWT presentation including its key-binding JWT.
|
|
174
|
+
* @param expected - Expected wallet challenge nonce and verifier audience.
|
|
175
|
+
*/
|
|
176
|
+
export function verifyKbJwt(presentation, expected) {
|
|
177
|
+
const parsed = parseSdJwt(presentation);
|
|
178
|
+
if (!parsed.kbJwt)
|
|
179
|
+
throw new Error('presentation carries no KB-JWT');
|
|
180
|
+
const cnf = parsed.payload.cnf;
|
|
181
|
+
if (!cnf)
|
|
182
|
+
throw new Error('issuer JWT has no cnf claim');
|
|
183
|
+
if (!cnf.jwk && !cnf.kid)
|
|
184
|
+
throw new Error('cnf has neither jwk nor kid');
|
|
185
|
+
const holderJwk = cnf.jwk ?? jwkFromDid(cnf.kid);
|
|
186
|
+
const { header, payload } = verifyCompactJws(parsed.kbJwt, holderJwk);
|
|
187
|
+
if (header.typ !== KB_JWT_TYP)
|
|
188
|
+
throw new Error(`KB-JWT typ must be ${KB_JWT_TYP}`);
|
|
189
|
+
if (payload.nonce !== expected.nonce)
|
|
190
|
+
throw new Error('KB-JWT nonce mismatch');
|
|
191
|
+
const aud = payload.aud;
|
|
192
|
+
const audOk = typeof aud === 'string' ? aud === expected.aud : Array.isArray(aud) && aud.includes(expected.aud);
|
|
193
|
+
if (!audOk)
|
|
194
|
+
throw new Error('KB-JWT aud mismatch');
|
|
195
|
+
const prefix = presentation.slice(0, presentation.length - parsed.kbJwt.length);
|
|
196
|
+
if (payload.sd_hash !== sdHash(prefix))
|
|
197
|
+
throw new Error('KB-JWT sd_hash mismatch');
|
|
198
|
+
return { holderJwk, claims: payload };
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Decode (no verification) the issuer JWT's payload of a compact SD-JWT - for display.
|
|
202
|
+
*
|
|
203
|
+
* @param compact - Compact SD-JWT credential or presentation to inspect without signature verification.
|
|
204
|
+
*/
|
|
205
|
+
export function peekSdJwt(compact) {
|
|
206
|
+
const p = parseSdJwt(compact).payload;
|
|
207
|
+
return { iss: p.iss, vct: p.vct, exp: p.exp, sub: p.sub };
|
|
208
|
+
}
|