@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,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encode text as UTF-8 bytes.
|
|
3
|
+
*
|
|
4
|
+
* @param s - Text to encode.
|
|
5
|
+
*/
|
|
6
|
+
export declare const utf8: (s: string) => Uint8Array;
|
|
7
|
+
/**
|
|
8
|
+
* Decode UTF-8 bytes into text using replacement characters for invalid sequences.
|
|
9
|
+
*
|
|
10
|
+
* @param b - UTF-8 bytes to decode.
|
|
11
|
+
*/
|
|
12
|
+
export declare const fromUtf8: (b: Uint8Array) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Encode bytes or UTF-8 text as unpadded base64url.
|
|
15
|
+
*
|
|
16
|
+
* @param bytes - Raw bytes, or text encoded as UTF-8 before conversion.
|
|
17
|
+
*/
|
|
18
|
+
export declare function b64url(bytes: Uint8Array | string): string;
|
|
19
|
+
/**
|
|
20
|
+
* Decode base64url text into bytes.
|
|
21
|
+
*
|
|
22
|
+
* @param s - Base64url-encoded text, with optional padding.
|
|
23
|
+
*/
|
|
24
|
+
export declare function b64urlDecode(s: string): Uint8Array;
|
|
25
|
+
/**
|
|
26
|
+
* Decode a base64url JSON value. The generic type is a caller assertion, not runtime validation.
|
|
27
|
+
*
|
|
28
|
+
* @param b64 - Base64url-encoded JSON text.
|
|
29
|
+
*/
|
|
30
|
+
export declare const decodeJson: <T = unknown>(b64: string) => T;
|
|
31
|
+
/** Public P-256 JSON Web Key with base64url coordinates and optional JOSE metadata. */
|
|
32
|
+
export interface EcJwk {
|
|
33
|
+
/** EC key type discriminator. */
|
|
34
|
+
kty: 'EC';
|
|
35
|
+
/** P-256 curve identifier. */
|
|
36
|
+
crv: 'P-256';
|
|
37
|
+
/** Base64url-encoded public x coordinate. */
|
|
38
|
+
x: string;
|
|
39
|
+
/** Base64url-encoded public y coordinate. */
|
|
40
|
+
y: string;
|
|
41
|
+
/** Optional signing key identifier. */
|
|
42
|
+
kid?: string;
|
|
43
|
+
/** JOSE signing algorithm identifier. */
|
|
44
|
+
alg?: string;
|
|
45
|
+
/** Optional JOSE key-use hint. */
|
|
46
|
+
use?: string;
|
|
47
|
+
}
|
|
48
|
+
/** Public Ed25519 JSON Web Key with a base64url public key and optional key identifier. */
|
|
49
|
+
export interface OkpJwk {
|
|
50
|
+
/** OKP key type discriminator. */
|
|
51
|
+
kty: 'OKP';
|
|
52
|
+
/** Ed25519 curve identifier. */
|
|
53
|
+
crv: 'Ed25519';
|
|
54
|
+
/** Base64url-encoded Ed25519 public key. */
|
|
55
|
+
x: string;
|
|
56
|
+
/** Optional signing key identifier. */
|
|
57
|
+
kid?: string;
|
|
58
|
+
}
|
|
59
|
+
/** Supported public JSON Web Key: P-256 or Ed25519. */
|
|
60
|
+
export type Jwk = EcJwk | OkpJwk;
|
|
61
|
+
/** A holder key in the shape the Hovi profile presents: `cnf.kid = did:jwk:...#0`. */
|
|
62
|
+
export interface HolderKey {
|
|
63
|
+
/** 32 bytes: a P-256 private scalar, or an Ed25519 seed. */
|
|
64
|
+
privateKey: Uint8Array;
|
|
65
|
+
/** Public JSON Web Key for this holder. */
|
|
66
|
+
publicJwk: Jwk;
|
|
67
|
+
/** `did:jwk:<base64url(JSON(publicJwk))>` */
|
|
68
|
+
did: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* How to sign for this holder. P-256 keys sign ES256, Ed25519 keys EdDSA - a credential is bound to
|
|
72
|
+
* one key, and the KB-JWT it is presented with has to be signed by that key's own algorithm. Issuers
|
|
73
|
+
* outside the P-256 profile exist: `tasra-cli vc issue-sd-jwt` binds an Ed25519 holder key.
|
|
74
|
+
*
|
|
75
|
+
* @param holder - Holder private key and public JWK identifying its signing algorithm.
|
|
76
|
+
*/
|
|
77
|
+
export declare function holderSigner(holder: HolderKey): JwsSigner;
|
|
78
|
+
/**
|
|
79
|
+
* Derive a public P-256 JSON Web Key from a private scalar.
|
|
80
|
+
*
|
|
81
|
+
* @param privateKey - 32-byte P-256 private scalar.
|
|
82
|
+
*/
|
|
83
|
+
export declare function p256PublicJwk(privateKey: Uint8Array): EcJwk;
|
|
84
|
+
/**
|
|
85
|
+
* `did:jwk` of a JWK - the JSON is serialised in `kty, crv, x, y` order, the vault's convention.
|
|
86
|
+
*
|
|
87
|
+
* @param jwk - Public P-256 or Ed25519 JSON Web Key.
|
|
88
|
+
*/
|
|
89
|
+
export declare function didJwk(jwk: Jwk): string;
|
|
90
|
+
/**
|
|
91
|
+
* Construct a holder identity from a 32-byte P-256 private scalar.
|
|
92
|
+
*
|
|
93
|
+
* @param privateKey - 32-byte P-256 private scalar.
|
|
94
|
+
*/
|
|
95
|
+
export declare function p256HolderKey(privateKey: Uint8Array): HolderKey;
|
|
96
|
+
/**
|
|
97
|
+
* An Ed25519 holder key from a 32-byte seed - the shape `tasra-cli vc issue-sd-jwt` binds.
|
|
98
|
+
*
|
|
99
|
+
* @param seed - 32-byte Ed25519 secret seed.
|
|
100
|
+
*/
|
|
101
|
+
export declare function ed25519HolderKey(seed: Uint8Array): HolderKey;
|
|
102
|
+
/** Generate a random P-256 holder key and its did:jwk identifier. */
|
|
103
|
+
export declare function randomHolderKey(): HolderKey;
|
|
104
|
+
/**
|
|
105
|
+
* Encode bytes as base58btc, preserving leading zero bytes.
|
|
106
|
+
*
|
|
107
|
+
* @param bytes - Bytes to encode, including any leading zeros.
|
|
108
|
+
*/
|
|
109
|
+
export declare function base58Encode(bytes: Uint8Array): string;
|
|
110
|
+
/**
|
|
111
|
+
* Decode base58btc text, preserving leading zero bytes and rejecting invalid characters.
|
|
112
|
+
*
|
|
113
|
+
* @param s - Base58btc text to decode.
|
|
114
|
+
*/
|
|
115
|
+
export declare function base58Decode(s: string): Uint8Array;
|
|
116
|
+
/**
|
|
117
|
+
* `did:key` of an Ed25519 public key (multicodec 0xed01).
|
|
118
|
+
*
|
|
119
|
+
* @param publicKey - 32-byte Ed25519 public key.
|
|
120
|
+
*/
|
|
121
|
+
export declare function ed25519DidKey(publicKey: Uint8Array): string;
|
|
122
|
+
/**
|
|
123
|
+
* `did:key` of a P-256 public key (multicodec 0x1200 to varint `80 24`, compressed point) - Hovi's issuer shape.
|
|
124
|
+
*
|
|
125
|
+
* @param publicKeyUncompressedOrCompressed - SEC1-encoded P-256 public key in compressed or uncompressed form.
|
|
126
|
+
*/
|
|
127
|
+
export declare function p256DidKey(publicKeyUncompressedOrCompressed: Uint8Array): string;
|
|
128
|
+
/**
|
|
129
|
+
* The 32-byte Ed25519 key inside a `did:key:z6Mk...`; throws for any other key type.
|
|
130
|
+
*
|
|
131
|
+
* @param did - Ed25519 did:key identifier.
|
|
132
|
+
*/
|
|
133
|
+
export declare function ed25519FromDidKey(did: string): Uint8Array;
|
|
134
|
+
/**
|
|
135
|
+
* The public JWK inside a `did:jwk` or a `did:key` (Ed25519 / P-256); a `#fragment` is ignored.
|
|
136
|
+
*
|
|
137
|
+
* @param did - P-256 or Ed25519 did:key or did:jwk identifier; an optional fragment is ignored.
|
|
138
|
+
*/
|
|
139
|
+
export declare function jwkFromDid(did: string): Jwk;
|
|
140
|
+
/** Supported compact JWS algorithms: ES256 for P-256 or EdDSA for Ed25519. */
|
|
141
|
+
export type JwsAlg = 'ES256' | 'EdDSA';
|
|
142
|
+
/** A signer for a compact JWS: a raw private key of the named curve. */
|
|
143
|
+
export interface JwsSigner {
|
|
144
|
+
/** JOSE signing algorithm identifier. */
|
|
145
|
+
alg: JwsAlg;
|
|
146
|
+
/** 32-byte P-256 private scalar or Ed25519 secret seed, matching alg. */
|
|
147
|
+
privateKey: Uint8Array;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Sign a JSON payload as compact JWS. Set the header algorithm from the supplied signer.
|
|
151
|
+
*
|
|
152
|
+
* @param header - JOSE header fields; the signer determines alg.
|
|
153
|
+
* @param payload - JSON claims to sign.
|
|
154
|
+
* @param signer - Private key and supported JWS algorithm.
|
|
155
|
+
*/
|
|
156
|
+
export declare function signCompactJws(header: Record<string, unknown>, payload: Record<string, unknown>, signer: JwsSigner): string;
|
|
157
|
+
/**
|
|
158
|
+
* Verify a compact JWS under `jwk` and return its decoded payload; throws on any failure.
|
|
159
|
+
*
|
|
160
|
+
* @param jws - Compact JWS to verify.
|
|
161
|
+
* @param jwk - Public JSON Web Key trusted by the caller for this signature.
|
|
162
|
+
*/
|
|
163
|
+
export declare function verifyCompactJws<T = Record<string, unknown>>(jws: string, jwk: Jwk): {
|
|
164
|
+
header: Record<string, unknown>;
|
|
165
|
+
payload: T;
|
|
166
|
+
};
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// Minimal JOSE plumbing shared by the wallet modules: base64url, compact JWS over ES256 (P-256)
|
|
2
|
+
// and EdDSA (Ed25519), JWK and DID helpers (`did:jwk`, `did:key` for both curves), and the
|
|
3
|
+
// holder key shape the Hovi profile uses (a P-256 `did:jwk`, `#0` fragment). Pure noble; runs in
|
|
4
|
+
// a browser extension, a PWA and Node alike.
|
|
5
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
6
|
+
import { p256 } from '@noble/curves/p256';
|
|
7
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
8
|
+
/**
|
|
9
|
+
* Encode text as UTF-8 bytes.
|
|
10
|
+
*
|
|
11
|
+
* @param s - Text to encode.
|
|
12
|
+
*/
|
|
13
|
+
export const utf8 = (s) => new TextEncoder().encode(s);
|
|
14
|
+
/**
|
|
15
|
+
* Decode UTF-8 bytes into text using replacement characters for invalid sequences.
|
|
16
|
+
*
|
|
17
|
+
* @param b - UTF-8 bytes to decode.
|
|
18
|
+
*/
|
|
19
|
+
export const fromUtf8 = (b) => new TextDecoder().decode(b);
|
|
20
|
+
/**
|
|
21
|
+
* Encode bytes or UTF-8 text as unpadded base64url.
|
|
22
|
+
*
|
|
23
|
+
* @param bytes - Raw bytes, or text encoded as UTF-8 before conversion.
|
|
24
|
+
*/
|
|
25
|
+
export function b64url(bytes) {
|
|
26
|
+
const b = typeof bytes === 'string' ? utf8(bytes) : bytes;
|
|
27
|
+
let bin = '';
|
|
28
|
+
for (const x of b)
|
|
29
|
+
bin += String.fromCharCode(x);
|
|
30
|
+
return btoa(bin).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Decode base64url text into bytes.
|
|
34
|
+
*
|
|
35
|
+
* @param s - Base64url-encoded text, with optional padding.
|
|
36
|
+
*/
|
|
37
|
+
export function b64urlDecode(s) {
|
|
38
|
+
const pad = s.length % 4 === 0 ? '' : '='.repeat(4 - (s.length % 4));
|
|
39
|
+
return Uint8Array.from(atob(s.replace(/-/g, '+').replace(/_/g, '/') + pad), c => c.charCodeAt(0));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Decode a base64url JSON value. The generic type is a caller assertion, not runtime validation.
|
|
43
|
+
*
|
|
44
|
+
* @param b64 - Base64url-encoded JSON text.
|
|
45
|
+
*/
|
|
46
|
+
export const decodeJson = (b64) => JSON.parse(fromUtf8(b64urlDecode(b64)));
|
|
47
|
+
/**
|
|
48
|
+
* How to sign for this holder. P-256 keys sign ES256, Ed25519 keys EdDSA - a credential is bound to
|
|
49
|
+
* one key, and the KB-JWT it is presented with has to be signed by that key's own algorithm. Issuers
|
|
50
|
+
* outside the P-256 profile exist: `tasra-cli vc issue-sd-jwt` binds an Ed25519 holder key.
|
|
51
|
+
*
|
|
52
|
+
* @param holder - Holder private key and public JWK identifying its signing algorithm.
|
|
53
|
+
*/
|
|
54
|
+
export function holderSigner(holder) {
|
|
55
|
+
return { alg: holder.publicJwk.kty === 'OKP' ? 'EdDSA' : 'ES256', privateKey: holder.privateKey };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Derive a public P-256 JSON Web Key from a private scalar.
|
|
59
|
+
*
|
|
60
|
+
* @param privateKey - 32-byte P-256 private scalar.
|
|
61
|
+
*/
|
|
62
|
+
export function p256PublicJwk(privateKey) {
|
|
63
|
+
const pub = p256.getPublicKey(privateKey, false);
|
|
64
|
+
return { kty: 'EC', crv: 'P-256', x: b64url(pub.slice(1, 33)), y: b64url(pub.slice(33, 65)) };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* `did:jwk` of a JWK - the JSON is serialised in `kty, crv, x, y` order, the vault's convention.
|
|
68
|
+
*
|
|
69
|
+
* @param jwk - Public P-256 or Ed25519 JSON Web Key.
|
|
70
|
+
*/
|
|
71
|
+
export function didJwk(jwk) {
|
|
72
|
+
const canon = jwk.kty === 'EC' ? { kty: jwk.kty, crv: jwk.crv, x: jwk.x, y: jwk.y } : { kty: jwk.kty, crv: jwk.crv, x: jwk.x };
|
|
73
|
+
return `did:jwk:${b64url(JSON.stringify(canon))}`;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Construct a holder identity from a 32-byte P-256 private scalar.
|
|
77
|
+
*
|
|
78
|
+
* @param privateKey - 32-byte P-256 private scalar.
|
|
79
|
+
*/
|
|
80
|
+
export function p256HolderKey(privateKey) {
|
|
81
|
+
if (privateKey.length !== 32)
|
|
82
|
+
throw new Error('P-256 private key must be 32 bytes');
|
|
83
|
+
const publicJwk = p256PublicJwk(privateKey);
|
|
84
|
+
return { privateKey, publicJwk, did: didJwk(publicJwk) };
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* An Ed25519 holder key from a 32-byte seed - the shape `tasra-cli vc issue-sd-jwt` binds.
|
|
88
|
+
*
|
|
89
|
+
* @param seed - 32-byte Ed25519 secret seed.
|
|
90
|
+
*/
|
|
91
|
+
export function ed25519HolderKey(seed) {
|
|
92
|
+
const publicJwk = { kty: 'OKP', crv: 'Ed25519', x: b64url(ed25519.getPublicKey(seed)) };
|
|
93
|
+
return { privateKey: seed, publicJwk, did: didJwk(publicJwk) };
|
|
94
|
+
}
|
|
95
|
+
/** Generate a random P-256 holder key and its did:jwk identifier. */
|
|
96
|
+
export function randomHolderKey() {
|
|
97
|
+
return p256HolderKey(p256.utils.randomPrivateKey());
|
|
98
|
+
}
|
|
99
|
+
// base58btc (did:key)
|
|
100
|
+
const B58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
101
|
+
/**
|
|
102
|
+
* Encode bytes as base58btc, preserving leading zero bytes.
|
|
103
|
+
*
|
|
104
|
+
* @param bytes - Bytes to encode, including any leading zeros.
|
|
105
|
+
*/
|
|
106
|
+
export function base58Encode(bytes) {
|
|
107
|
+
let n = 0n;
|
|
108
|
+
for (const b of bytes)
|
|
109
|
+
n = (n << 8n) | BigInt(b);
|
|
110
|
+
let out = '';
|
|
111
|
+
while (n > 0n) {
|
|
112
|
+
out = B58[Number(n % 58n)] + out;
|
|
113
|
+
n /= 58n;
|
|
114
|
+
}
|
|
115
|
+
for (const b of bytes) {
|
|
116
|
+
if (b !== 0)
|
|
117
|
+
break;
|
|
118
|
+
out = '1' + out;
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Decode base58btc text, preserving leading zero bytes and rejecting invalid characters.
|
|
124
|
+
*
|
|
125
|
+
* @param s - Base58btc text to decode.
|
|
126
|
+
*/
|
|
127
|
+
export function base58Decode(s) {
|
|
128
|
+
let n = 0n;
|
|
129
|
+
for (const c of s) {
|
|
130
|
+
const i = B58.indexOf(c);
|
|
131
|
+
if (i < 0)
|
|
132
|
+
throw new Error('bad base58');
|
|
133
|
+
n = n * 58n + BigInt(i);
|
|
134
|
+
}
|
|
135
|
+
const bytes = [];
|
|
136
|
+
while (n > 0n) {
|
|
137
|
+
bytes.unshift(Number(n & 0xffn));
|
|
138
|
+
n >>= 8n;
|
|
139
|
+
}
|
|
140
|
+
let zeros = 0;
|
|
141
|
+
for (const c of s) {
|
|
142
|
+
if (c !== '1')
|
|
143
|
+
break;
|
|
144
|
+
zeros++;
|
|
145
|
+
}
|
|
146
|
+
return new Uint8Array([...new Array(zeros).fill(0), ...bytes]);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* `did:key` of an Ed25519 public key (multicodec 0xed01).
|
|
150
|
+
*
|
|
151
|
+
* @param publicKey - 32-byte Ed25519 public key.
|
|
152
|
+
*/
|
|
153
|
+
export function ed25519DidKey(publicKey) {
|
|
154
|
+
return `did:key:z${base58Encode(new Uint8Array([0xed, 0x01, ...publicKey]))}`;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* `did:key` of a P-256 public key (multicodec 0x1200 to varint `80 24`, compressed point) - Hovi's issuer shape.
|
|
158
|
+
*
|
|
159
|
+
* @param publicKeyUncompressedOrCompressed - SEC1-encoded P-256 public key in compressed or uncompressed form.
|
|
160
|
+
*/
|
|
161
|
+
export function p256DidKey(publicKeyUncompressedOrCompressed) {
|
|
162
|
+
const compressed = p256.ProjectivePoint.fromHex(publicKeyUncompressedOrCompressed).toRawBytes(true);
|
|
163
|
+
return `did:key:z${base58Encode(new Uint8Array([0x80, 0x24, ...compressed]))}`;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* The 32-byte Ed25519 key inside a `did:key:z6Mk...`; throws for any other key type.
|
|
167
|
+
*
|
|
168
|
+
* @param did - Ed25519 did:key identifier.
|
|
169
|
+
*/
|
|
170
|
+
export function ed25519FromDidKey(did) {
|
|
171
|
+
const m = /^did:key:z([1-9A-HJ-NP-Za-km-z]+)$/.exec(did);
|
|
172
|
+
if (!m)
|
|
173
|
+
throw new Error('not a base58btc did:key');
|
|
174
|
+
const raw = base58Decode(m[1]);
|
|
175
|
+
if (raw.length !== 34 || raw[0] !== 0xed || raw[1] !== 0x01)
|
|
176
|
+
throw new Error('did:key is not an Ed25519 key');
|
|
177
|
+
return raw.slice(2);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The public JWK inside a `did:jwk` or a `did:key` (Ed25519 / P-256); a `#fragment` is ignored.
|
|
181
|
+
*
|
|
182
|
+
* @param did - P-256 or Ed25519 did:key or did:jwk identifier; an optional fragment is ignored.
|
|
183
|
+
*/
|
|
184
|
+
export function jwkFromDid(did) {
|
|
185
|
+
const bare = did.split('#')[0];
|
|
186
|
+
if (bare.startsWith('did:jwk:'))
|
|
187
|
+
return decodeJson(bare.slice('did:jwk:'.length));
|
|
188
|
+
if (bare.startsWith('did:key:z')) {
|
|
189
|
+
const raw = base58Decode(bare.slice('did:key:z'.length));
|
|
190
|
+
if (raw[0] === 0xed && raw[1] === 0x01)
|
|
191
|
+
return { kty: 'OKP', crv: 'Ed25519', x: b64url(raw.slice(2)) };
|
|
192
|
+
if (raw[0] === 0x80 && raw[1] === 0x24) {
|
|
193
|
+
const u = p256.ProjectivePoint.fromHex(raw.slice(2)).toRawBytes(false);
|
|
194
|
+
return { kty: 'EC', crv: 'P-256', x: b64url(u.slice(1, 33)), y: b64url(u.slice(33, 65)) };
|
|
195
|
+
}
|
|
196
|
+
throw new Error(`unsupported did:key multicodec 0x${raw[0]?.toString(16)}${raw[1]?.toString(16)}`);
|
|
197
|
+
}
|
|
198
|
+
throw new Error(`cannot derive a key offline from ${bare.split(':').slice(0, 2).join(':')}`);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Sign a JSON payload as compact JWS. Set the header algorithm from the supplied signer.
|
|
202
|
+
*
|
|
203
|
+
* @param header - JOSE header fields; the signer determines alg.
|
|
204
|
+
* @param payload - JSON claims to sign.
|
|
205
|
+
* @param signer - Private key and supported JWS algorithm.
|
|
206
|
+
*/
|
|
207
|
+
export function signCompactJws(header, payload, signer) {
|
|
208
|
+
const h = { ...header, alg: signer.alg };
|
|
209
|
+
const input = `${b64url(JSON.stringify(h))}.${b64url(JSON.stringify(payload))}`;
|
|
210
|
+
const sig = signer.alg === 'ES256'
|
|
211
|
+
? p256.sign(sha256(utf8(input)), signer.privateKey).toCompactRawBytes()
|
|
212
|
+
: ed25519.sign(utf8(input), signer.privateKey);
|
|
213
|
+
return `${input}.${b64url(sig)}`;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Verify a compact JWS under `jwk` and return its decoded payload; throws on any failure.
|
|
217
|
+
*
|
|
218
|
+
* @param jws - Compact JWS to verify.
|
|
219
|
+
* @param jwk - Public JSON Web Key trusted by the caller for this signature.
|
|
220
|
+
*/
|
|
221
|
+
export function verifyCompactJws(jws, jwk) {
|
|
222
|
+
const [h, p, s] = jws.split('.');
|
|
223
|
+
if (!h || !p || !s)
|
|
224
|
+
throw new Error('not a compact JWS');
|
|
225
|
+
const header = decodeJson(h);
|
|
226
|
+
const input = utf8(`${h}.${p}`);
|
|
227
|
+
const sig = b64urlDecode(s);
|
|
228
|
+
let ok;
|
|
229
|
+
if (header.alg === 'ES256' && jwk.kty === 'EC') {
|
|
230
|
+
const pub = new Uint8Array([0x04, ...b64urlDecode(jwk.x), ...b64urlDecode(jwk.y)]);
|
|
231
|
+
ok = p256.verify(sig, sha256(input), pub);
|
|
232
|
+
}
|
|
233
|
+
else if (header.alg === 'EdDSA' && jwk.kty === 'OKP') {
|
|
234
|
+
ok = ed25519.verify(sig, input, b64urlDecode(jwk.x));
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
throw new Error(`JWS alg ${String(header.alg)} does not match the key type ${jwk.kty}`);
|
|
238
|
+
}
|
|
239
|
+
if (!ok)
|
|
240
|
+
throw new Error('JWS signature does not verify');
|
|
241
|
+
return { header, payload: decodeJson(p) };
|
|
242
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type EcJwk } from './jose.js';
|
|
2
|
+
/** Supported AES-GCM content encryption algorithms for compact JWE. */
|
|
3
|
+
export type JweEnc = 'A256GCM' | 'A128GCM';
|
|
4
|
+
/**
|
|
5
|
+
* Concat KDF (NIST SP 800-56A, single-pass SHA-256) - AlgorithmID = `enc` for ECDH-ES direct.
|
|
6
|
+
*
|
|
7
|
+
* @param z - ECDH shared secret bytes.
|
|
8
|
+
* @param alg - Algorithm identifier included in the KDF context.
|
|
9
|
+
* @param apu - Producer party information.
|
|
10
|
+
* @param apv - Recipient party information.
|
|
11
|
+
* @param keyLen - Derived key length in bytes.
|
|
12
|
+
*/
|
|
13
|
+
export declare function concatKdf(z: Uint8Array, alg: string, apu: Uint8Array, apv: Uint8Array, keyLen: number): Uint8Array;
|
|
14
|
+
/**
|
|
15
|
+
* Encrypt `plaintext` to the recipient's ephemeral P-256 JWK (the JAR's `client_metadata.jwks.keys[0]`)
|
|
16
|
+
* as `header..iv.ciphertext.tag`. A fresh sender key per call; `kid` echoed when the recipient key has one.
|
|
17
|
+
*
|
|
18
|
+
* @param plaintext - UTF-8 plaintext to encrypt.
|
|
19
|
+
* @param recipient - Recipient public P-256 key.
|
|
20
|
+
* @param enc - AES-GCM content encryption algorithm.
|
|
21
|
+
* @param random - Cryptographically secure random byte generator; defaults to Web Crypto.
|
|
22
|
+
*/
|
|
23
|
+
export declare function encryptJwe(plaintext: string, recipient: EcJwk, enc?: JweEnc, random?: (n: number) => Uint8Array): string;
|
|
24
|
+
/**
|
|
25
|
+
* Decrypt a compact JWE produced by {@link encryptJwe} (or a wallet) with the recipient's private scalar.
|
|
26
|
+
*
|
|
27
|
+
* @param compact - Compact JWE using ECDH-ES and a supported AES-GCM algorithm.
|
|
28
|
+
* @param recipientPrivateKey - Recipient 32-byte P-256 private scalar.
|
|
29
|
+
*/
|
|
30
|
+
export declare function decryptJwe(compact: string, recipientPrivateKey: Uint8Array): string;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Compact JWE using P-256 ECDH-ES and AES-GCM for encrypted wallet responses.
|
|
2
|
+
import { gcm } from '@noble/ciphers/aes';
|
|
3
|
+
import { p256 } from '@noble/curves/p256';
|
|
4
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
5
|
+
import { b64url, b64urlDecode, decodeJson, utf8 } from './jose.js';
|
|
6
|
+
function be32(n) {
|
|
7
|
+
return new Uint8Array([(n >>> 24) & 255, (n >>> 16) & 255, (n >>> 8) & 255, n & 255]);
|
|
8
|
+
}
|
|
9
|
+
function cat(...parts) {
|
|
10
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
11
|
+
let o = 0;
|
|
12
|
+
for (const p of parts) {
|
|
13
|
+
out.set(p, o);
|
|
14
|
+
o += p.length;
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Concat KDF (NIST SP 800-56A, single-pass SHA-256) - AlgorithmID = `enc` for ECDH-ES direct.
|
|
20
|
+
*
|
|
21
|
+
* @param z - ECDH shared secret bytes.
|
|
22
|
+
* @param alg - Algorithm identifier included in the KDF context.
|
|
23
|
+
* @param apu - Producer party information.
|
|
24
|
+
* @param apv - Recipient party information.
|
|
25
|
+
* @param keyLen - Derived key length in bytes.
|
|
26
|
+
*/
|
|
27
|
+
export function concatKdf(z, alg, apu, apv, keyLen) {
|
|
28
|
+
const reps = Math.ceil(keyLen / 32);
|
|
29
|
+
const out = [];
|
|
30
|
+
for (let counter = 1; counter <= reps; counter++) {
|
|
31
|
+
const algB = utf8(alg);
|
|
32
|
+
out.push(sha256(cat(be32(counter), z, be32(algB.length), algB, be32(apu.length), apu, be32(apv.length), apv, be32(keyLen * 8))));
|
|
33
|
+
}
|
|
34
|
+
return cat(...out).slice(0, keyLen);
|
|
35
|
+
}
|
|
36
|
+
function ecdhZ(privateKey, peer) {
|
|
37
|
+
const peerPub = new Uint8Array([0x04, ...b64urlDecode(peer.x), ...b64urlDecode(peer.y)]);
|
|
38
|
+
// The shared secret is the x-coordinate of the ECDH point.
|
|
39
|
+
return p256.getSharedSecret(privateKey, peerPub, true).slice(1, 33);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Encrypt `plaintext` to the recipient's ephemeral P-256 JWK (the JAR's `client_metadata.jwks.keys[0]`)
|
|
43
|
+
* as `header..iv.ciphertext.tag`. A fresh sender key per call; `kid` echoed when the recipient key has one.
|
|
44
|
+
*
|
|
45
|
+
* @param plaintext - UTF-8 plaintext to encrypt.
|
|
46
|
+
* @param recipient - Recipient public P-256 key.
|
|
47
|
+
* @param enc - AES-GCM content encryption algorithm.
|
|
48
|
+
* @param random - Cryptographically secure random byte generator; defaults to Web Crypto.
|
|
49
|
+
*/
|
|
50
|
+
export function encryptJwe(plaintext, recipient, enc = 'A256GCM', random = n => crypto.getRandomValues(new Uint8Array(n))) {
|
|
51
|
+
if (recipient.kty !== 'EC' || recipient.crv !== 'P-256')
|
|
52
|
+
throw new Error('JWE recipient must be an EC P-256 key');
|
|
53
|
+
const keyLen = enc === 'A256GCM' ? 32 : 16;
|
|
54
|
+
const senderPriv = p256.utils.randomPrivateKey();
|
|
55
|
+
const senderPub = p256.getPublicKey(senderPriv, false);
|
|
56
|
+
const epk = { kty: 'EC', crv: 'P-256', x: b64url(senderPub.slice(1, 33)), y: b64url(senderPub.slice(33, 65)) };
|
|
57
|
+
const header = { alg: 'ECDH-ES', enc, epk, ...(recipient.kid ? { kid: recipient.kid } : {}) };
|
|
58
|
+
const headerB64 = b64url(JSON.stringify(header));
|
|
59
|
+
const cek = concatKdf(ecdhZ(senderPriv, recipient), enc, new Uint8Array(), new Uint8Array(), keyLen);
|
|
60
|
+
const iv = random(12);
|
|
61
|
+
const sealed = gcm(cek, iv, utf8(headerB64)).encrypt(utf8(plaintext));
|
|
62
|
+
const ct = sealed.slice(0, sealed.length - 16);
|
|
63
|
+
const tag = sealed.slice(sealed.length - 16);
|
|
64
|
+
return `${headerB64}..${b64url(iv)}.${b64url(ct)}.${b64url(tag)}`;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Decrypt a compact JWE produced by {@link encryptJwe} (or a wallet) with the recipient's private scalar.
|
|
68
|
+
*
|
|
69
|
+
* @param compact - Compact JWE using ECDH-ES and a supported AES-GCM algorithm.
|
|
70
|
+
* @param recipientPrivateKey - Recipient 32-byte P-256 private scalar.
|
|
71
|
+
*/
|
|
72
|
+
export function decryptJwe(compact, recipientPrivateKey) {
|
|
73
|
+
const parts = compact.split('.');
|
|
74
|
+
if (parts.length !== 5)
|
|
75
|
+
throw new Error(`JWE must have 5 parts, got ${parts.length}`);
|
|
76
|
+
const [headerB64, encKey, ivB64, ctB64, tagB64] = parts;
|
|
77
|
+
if (encKey !== '')
|
|
78
|
+
throw new Error('JWE encrypted-key part must be empty for ECDH-ES');
|
|
79
|
+
const header = decodeJson(headerB64);
|
|
80
|
+
if (header.alg !== 'ECDH-ES')
|
|
81
|
+
throw new Error(`unsupported JWE alg ${String(header.alg)}`);
|
|
82
|
+
if (header.enc !== 'A256GCM' && header.enc !== 'A128GCM')
|
|
83
|
+
throw new Error(`unsupported JWE enc ${String(header.enc)}`);
|
|
84
|
+
if (!header.epk)
|
|
85
|
+
throw new Error('JWE header missing epk');
|
|
86
|
+
const keyLen = header.enc === 'A256GCM' ? 32 : 16;
|
|
87
|
+
const apu = header.apu ? b64urlDecode(header.apu) : new Uint8Array();
|
|
88
|
+
const apv = header.apv ? b64urlDecode(header.apv) : new Uint8Array();
|
|
89
|
+
const cek = concatKdf(ecdhZ(recipientPrivateKey, header.epk), header.enc, apu, apv, keyLen);
|
|
90
|
+
const iv = b64urlDecode(ivB64);
|
|
91
|
+
const sealed = cat(b64urlDecode(ctB64), b64urlDecode(tagB64));
|
|
92
|
+
return new TextDecoder().decode(gcm(cek, iv, utf8(headerB64)).decrypt(sealed));
|
|
93
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type HolderKey } from './jose.js';
|
|
2
|
+
/** URI scheme for a wallet credential offer. */
|
|
3
|
+
export declare const CREDENTIAL_OFFER_SCHEME = "openid-credential-offer://";
|
|
4
|
+
/** OAuth grant identifier for credential issuance using a pre-authorized code. */
|
|
5
|
+
export declare const PRE_AUTHORIZED_GRANT = "urn:ietf:params:oauth:grant-type:pre-authorized_code";
|
|
6
|
+
/** JOSE type identifier for an OpenID4VCI proof of key possession. */
|
|
7
|
+
export declare const PROOF_TYP = "openid4vci-proof+jwt";
|
|
8
|
+
/** Credential issuer, offered configurations and optional pre-authorized grant details. */
|
|
9
|
+
export interface CredentialOffer {
|
|
10
|
+
/** Issuer identifier and metadata base URL. */
|
|
11
|
+
credential_issuer: string;
|
|
12
|
+
/** Credential configurations available in the offer. */
|
|
13
|
+
credential_configuration_ids: string[];
|
|
14
|
+
/** Grant details, including any pre-authorized code and transaction code requirements. */
|
|
15
|
+
grants?: Record<string, {
|
|
16
|
+
'pre-authorized_code'?: string;
|
|
17
|
+
tx_code?: {
|
|
18
|
+
input_mode?: string;
|
|
19
|
+
length?: number;
|
|
20
|
+
description?: string;
|
|
21
|
+
};
|
|
22
|
+
authorization_server?: string;
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Parse an `openid-credential-offer://?credential_offer=...` or `...?credential_offer_uri=...` URI.
|
|
27
|
+
*
|
|
28
|
+
* @param uri - OpenID4VCI credential-offer URI containing an offer or offer URL.
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseCredentialOfferUri(uri: string): {
|
|
31
|
+
offer?: CredentialOffer;
|
|
32
|
+
offerUri?: string;
|
|
33
|
+
};
|
|
34
|
+
/** Issuer endpoints and credential configurations used during credential issuance. */
|
|
35
|
+
export interface IssuerMetadata {
|
|
36
|
+
/** Issuer identifier used as the proof audience. */
|
|
37
|
+
credential_issuer: string;
|
|
38
|
+
/** Endpoint accepting credential issuance requests. */
|
|
39
|
+
credential_endpoint: string;
|
|
40
|
+
/** Optional endpoint for obtaining a proof nonce. */
|
|
41
|
+
nonce_endpoint?: string;
|
|
42
|
+
/** Authorization servers associated with this issuer. */
|
|
43
|
+
authorization_servers?: string[];
|
|
44
|
+
/** Configuration identifiers mapped to credential formats and metadata. */
|
|
45
|
+
credential_configurations_supported: Record<string, {
|
|
46
|
+
format: string;
|
|
47
|
+
vct?: string;
|
|
48
|
+
[k: string]: unknown;
|
|
49
|
+
}>;
|
|
50
|
+
}
|
|
51
|
+
/** Credential offer, holder key and optional transaction code for pre-authorized issuance. */
|
|
52
|
+
export interface ReceiveCredentialOpts {
|
|
53
|
+
/** The scanned URI, or an already-parsed offer. */
|
|
54
|
+
offerUri?: string;
|
|
55
|
+
/** Already parsed credential offer; takes precedence over offerUri. */
|
|
56
|
+
offer?: CredentialOffer;
|
|
57
|
+
/** Holder key used to prove possession during issuance. */
|
|
58
|
+
holder: HolderKey;
|
|
59
|
+
/** The transaction code the issuer displayed, when the grant asks for one. */
|
|
60
|
+
txCode?: string;
|
|
61
|
+
/** Which configuration to request (default: the offer's first). */
|
|
62
|
+
credentialConfigurationId?: string;
|
|
63
|
+
/** HTTP transport override; defaults to the global fetch implementation. */
|
|
64
|
+
fetchImpl?: typeof fetch;
|
|
65
|
+
/** Current time override in Unix seconds. */
|
|
66
|
+
nowSecs?: number;
|
|
67
|
+
}
|
|
68
|
+
/** Issued compact credential with its selected configuration, format and issuer identifier. */
|
|
69
|
+
export interface ReceivedCredential {
|
|
70
|
+
/** The compact `dc+sd-jwt` (or whatever the configuration's format is). */
|
|
71
|
+
credential: string;
|
|
72
|
+
/** Credential configuration selected for issuance. */
|
|
73
|
+
configurationId: string;
|
|
74
|
+
/** Credential format identifier. */
|
|
75
|
+
format: string;
|
|
76
|
+
/** Credential issuer identifier returned by metadata. */
|
|
77
|
+
issuer: string;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Run the pre-authorized code flow end to end and return the issued credential.
|
|
81
|
+
*
|
|
82
|
+
* @param opts - Credential offer, holder key, optional transaction code and transport settings.
|
|
83
|
+
*/
|
|
84
|
+
export declare function receiveCredential(opts: ReceiveCredentialOpts): Promise<ReceivedCredential>;
|