@t3foundry/tasra-sdk 0.3.1-next.1 → 0.4.0
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 +1067 -28
- package/README.md +15 -0
- package/dist/app/approvals.d.ts +66 -4
- package/dist/app/approvals.js +153 -16
- package/dist/app/authorization.d.ts +1 -1
- package/dist/app/authorization.js +1 -1
- package/dist/app/browser.d.ts +63 -0
- package/dist/app/browser.js +242 -0
- package/dist/app/client.d.ts +114 -13
- package/dist/app/client.js +302 -76
- package/dist/app/identity.js +21 -9
- package/dist/app/index.d.ts +28 -3
- package/dist/app/index.js +16 -1
- package/dist/app/memory-store.d.ts +7 -0
- package/dist/app/memory-store.js +55 -0
- package/dist/app/node.d.ts +34 -6
- package/dist/app/node.js +387 -64
- package/dist/app/paid-allowance.d.ts +325 -0
- package/dist/app/paid-allowance.js +1290 -0
- package/dist/app/paid-approval.d.ts +122 -0
- package/dist/app/paid-approval.js +379 -0
- package/dist/app/paid-journal.d.ts +51 -0
- package/dist/app/paid-journal.js +117 -0
- package/dist/app/paid-keys.d.ts +12 -0
- package/dist/app/paid-keys.js +20 -0
- package/dist/app/paid-operation.d.ts +821 -0
- package/dist/app/paid-operation.js +1720 -0
- package/dist/app/paid-reservation.d.ts +157 -0
- package/dist/app/paid-reservation.js +323 -0
- package/dist/app/paid-session.d.ts +147 -0
- package/dist/app/paid-session.js +578 -0
- package/dist/app/payer-queue.d.ts +11 -0
- package/dist/app/payer-queue.js +27 -0
- package/dist/app/policy-bootstrap.d.ts +342 -0
- package/dist/app/policy-bootstrap.js +365 -0
- package/dist/app/policy-operation.d.ts +47 -0
- package/dist/app/policy-operation.js +101 -0
- package/dist/app/ready-slot.d.ts +12 -0
- package/dist/app/ready-slot.js +5 -2
- package/dist/app/rotation-lock.d.ts +38 -0
- package/dist/app/rotation-lock.js +42 -0
- package/dist/app/session-decrypt.d.ts +135 -0
- package/dist/app/session-decrypt.js +335 -0
- package/dist/app/session-grants.d.ts +349 -0
- package/dist/app/session-grants.js +811 -0
- package/dist/app/signing-intents.d.ts +78 -0
- package/dist/app/signing-intents.js +95 -0
- package/dist/app/store-commit.d.ts +63 -0
- package/dist/app/store-commit.js +126 -0
- package/dist/app/store-errors.d.ts +8 -0
- package/dist/app/store-errors.js +11 -0
- package/dist/app/tasra-client.d.ts +2 -0
- package/dist/app/tasra-client.js +1 -1
- package/dist/app/wallet.js +4 -1
- package/dist/auth/holderProof.d.ts +3 -0
- package/dist/auth/holderProof.js +8 -7
- package/dist/auth/verifier.d.ts +4 -2
- package/dist/auth/verifier.js +5 -2
- package/dist/chain/abis/index.d.ts +6178 -4101
- package/dist/chain/abis/index.js +3 -0
- package/dist/chain/abis/keyRegistry.d.ts +256 -0
- package/dist/chain/abis/keyRegistry.js +333 -0
- package/dist/chain/abis/prepaidFaultOracle.d.ts +321 -0
- package/dist/chain/abis/prepaidFaultOracle.js +413 -0
- package/dist/chain/abis/settlement.d.ts +2203 -703
- package/dist/chain/abis/settlement.js +2685 -764
- package/dist/chain/allowance.d.ts +358 -0
- package/dist/chain/allowance.js +800 -0
- package/dist/chain/billing.d.ts +85 -0
- package/dist/chain/billing.js +197 -0
- package/dist/chain/chainId.d.ts +13 -0
- package/dist/chain/chainId.js +26 -0
- package/dist/chain/client.d.ts +15 -1
- package/dist/chain/client.js +57 -1
- package/dist/chain/commitmentRecovery.d.ts +51 -0
- package/dist/chain/commitmentRecovery.js +38 -0
- package/dist/chain/committeeClient.d.ts +17 -10
- package/dist/chain/committeeClient.js +64 -16
- package/dist/chain/committeeDraw.d.ts +12 -0
- package/dist/chain/committeeDraw.js +56 -0
- package/dist/chain/custodyHealth.d.ts +212 -0
- package/dist/chain/custodyHealth.js +365 -0
- package/dist/chain/deployments.js +2 -0
- package/dist/chain/events.js +2 -0
- package/dist/chain/headFollower.d.ts +77 -0
- package/dist/chain/headFollower.js +219 -0
- package/dist/chain/headPrefetch.d.ts +23 -0
- package/dist/chain/headPrefetch.js +67 -0
- package/dist/chain/index.d.ts +50 -8
- package/dist/chain/index.js +28 -3
- package/dist/chain/keeperExecution.d.ts +44 -0
- package/dist/chain/keeperExecution.js +127 -0
- package/dist/chain/keeperRecovery.d.ts +78 -0
- package/dist/chain/keeperRecovery.js +91 -0
- package/dist/chain/node.d.ts +24 -1
- package/dist/chain/node.js +88 -43
- package/dist/chain/offchain.d.ts +2 -0
- package/dist/chain/paidAgentTransport.d.ts +38 -0
- package/dist/chain/paidAgentTransport.js +1 -0
- package/dist/chain/paidEpoch.d.ts +66 -0
- package/dist/chain/paidEpoch.js +139 -0
- package/dist/chain/paidPacing.d.ts +81 -0
- package/dist/chain/paidPacing.js +136 -0
- package/dist/chain/pinnedReads.d.ts +49 -0
- package/dist/chain/pinnedReads.js +218 -0
- package/dist/chain/prepaid.d.ts +383 -0
- package/dist/chain/prepaid.js +1046 -0
- package/dist/chain/prepaidEscape.d.ts +274 -0
- package/dist/chain/prepaidEscape.js +432 -0
- package/dist/chain/prepaidFault.d.ts +94 -0
- package/dist/chain/prepaidFault.js +148 -0
- package/dist/chain/receipt.d.ts +24 -0
- package/dist/chain/receipt.js +82 -0
- package/dist/chain/registeredAgent.d.ts +68 -3
- package/dist/chain/registeredAgent.js +143 -42
- package/dist/chain/registeredOperation.d.ts +10 -1
- package/dist/chain/registeredOperation.js +18 -3
- package/dist/chain/registeredPaidAgent.d.ts +254 -0
- package/dist/chain/registeredPaidAgent.js +889 -0
- package/dist/chain/registeredRelay.d.ts +31 -1
- package/dist/chain/registeredRelay.js +164 -43
- package/dist/chain/relayVoucher.d.ts +64 -0
- package/dist/chain/relayVoucher.js +69 -0
- package/dist/chain/reservation.d.ts +239 -0
- package/dist/chain/reservation.js +746 -0
- package/dist/chain/reshareRoster.d.ts +64 -0
- package/dist/chain/reshareRoster.js +85 -0
- package/dist/chain/revertReason.d.ts +8 -0
- package/dist/chain/revertReason.js +2 -1
- package/dist/chain/rotationLock.d.ts +130 -0
- package/dist/chain/rotationLock.js +168 -0
- package/dist/chain/serviceRequestPolicy.d.ts +47 -0
- package/dist/chain/serviceRequestPolicy.js +86 -0
- package/dist/chain/services.d.ts +7 -0
- package/dist/chain/services.js +44 -4
- package/dist/chain/slotClient.js +11 -2
- package/dist/chain/slotHandoff.d.ts +81 -0
- package/dist/chain/slotHandoff.js +166 -0
- package/dist/chain/slotSeed.d.ts +47 -5
- package/dist/chain/slotSeed.js +173 -42
- package/dist/chain/verifierSnapshot.d.ts +36 -0
- package/dist/chain/verifierSnapshot.js +197 -0
- package/dist/chain/write.d.ts +111 -21
- package/dist/chain/write.js +378 -146
- package/dist/committee/blsSharing.d.ts +53 -0
- package/dist/committee/blsSharing.js +90 -0
- package/dist/committee/client.d.ts +62 -23
- package/dist/committee/client.js +288 -72
- package/dist/committee/dual-operation.d.ts +165 -0
- package/dist/committee/dual-operation.js +229 -0
- package/dist/committee/dual-sign.d.ts +26 -1
- package/dist/committee/dual-sign.js +85 -7
- package/dist/committee/ecdsa.d.ts +38 -0
- package/dist/committee/ecdsa.js +87 -15
- package/dist/committee/evidence.d.ts +30 -0
- package/dist/committee/evidence.js +65 -0
- package/dist/committee/execution.d.ts +414 -0
- package/dist/committee/execution.js +446 -0
- package/dist/committee/extraction.js +33 -26
- package/dist/committee/index.d.ts +30 -7
- package/dist/committee/index.js +18 -6
- package/dist/committee/keeperExecution.d.ts +37 -0
- package/dist/committee/keeperExecution.js +141 -0
- package/dist/committee/oauth.d.ts +14 -4
- package/dist/committee/oauth.js +17 -5
- package/dist/committee/payment.d.ts +369 -0
- package/dist/committee/payment.js +339 -0
- package/dist/committee/recovery.d.ts +136 -0
- package/dist/committee/recovery.js +204 -0
- package/dist/committee/recoveryJson.d.ts +2 -0
- package/dist/committee/recoveryJson.js +87 -0
- package/dist/committee/request.d.ts +63 -27
- package/dist/committee/request.js +77 -66
- package/dist/committee/session-grant.d.ts +320 -0
- package/dist/committee/session-grant.js +397 -0
- package/dist/committee/status-refresh.d.ts +257 -0
- package/dist/committee/status-refresh.js +619 -0
- package/dist/committee/token.d.ts +163 -15
- package/dist/committee/token.js +247 -10
- package/dist/crypto/bn254.d.ts +70 -0
- package/dist/crypto/bn254.js +222 -0
- package/dist/crypto/ibe.d.ts +32 -0
- package/dist/crypto/ibe.js +56 -8
- package/dist/crypto/index.d.ts +1 -1
- package/dist/crypto/index.js +1 -1
- package/dist/crypto/kem.d.ts +31 -0
- package/dist/crypto/kem.js +113 -7
- package/dist/crypto/recipient.d.ts +75 -0
- package/dist/crypto/recipient.js +107 -0
- package/dist/decryption/client.d.ts +10 -16
- package/dist/decryption/client.js +8 -76
- package/dist/decryption/index.d.ts +2 -0
- package/dist/decryption/index.js +2 -0
- package/dist/decryption/sealed.d.ts +72 -0
- package/dist/decryption/sealed.js +147 -0
- package/dist/decryption/session.d.ts +115 -0
- package/dist/decryption/session.js +358 -0
- package/dist/errors.d.ts +406 -1
- package/dist/errors.js +400 -5
- package/dist/index.d.ts +9 -2
- package/dist/index.js +6 -1
- package/dist/internal/bounded.d.ts +66 -0
- package/dist/internal/bounded.js +126 -0
- package/dist/internal/network.d.ts +4 -0
- package/dist/internal/network.js +40 -0
- package/dist/internal/retryAfter.d.ts +8 -0
- package/dist/internal/retryAfter.js +28 -0
- package/dist/internal/sleep.d.ts +6 -0
- package/dist/internal/sleep.js +15 -0
- package/dist/internal/typedData.d.ts +13 -0
- package/dist/internal/typedData.js +77 -0
- package/dist/internal/viemErrors.d.ts +26 -0
- package/dist/internal/viemErrors.js +49 -0
- package/dist/oid4vp/binding.d.ts +27 -1
- package/dist/oid4vp/binding.js +36 -1
- package/dist/oid4vp/jose.d.ts +4 -2
- package/dist/oid4vp/jose.js +39 -7
- package/dist/oid4vp/request-object.d.ts +61 -4
- package/dist/oid4vp/request-object.js +140 -5
- package/dist/oid4vp/verifier-agent.d.ts +18 -8
- package/dist/oid4vp/verifier-agent.js +48 -6
- package/dist/oid4vp/wallet.d.ts +2 -4
- package/dist/oid4vp/wallet.js +1 -0
- package/dist/retry.d.ts +78 -0
- package/dist/retry.js +137 -0
- package/dist/signing/frost.d.ts +1 -1
- package/dist/verifier-agent/index.d.ts +41 -5
- package/dist/verifier-agent/index.js +124 -9
- package/dist/verifier-agent/payment.d.ts +7 -0
- package/dist/verifier-agent/payment.js +17 -0
- package/docs/api.md +8 -2
- package/docs/application-api.md +25 -2
- package/docs/chain.md +155 -9
- package/docs/consumer-migrations.md +61 -0
- package/docs/encrypted-notes.md +3 -2
- package/docs/encryption.md +4 -2
- package/docs/errors.md +14 -1
- package/docs/flows.md +2 -2
- package/docs/funding.md +902 -0
- package/docs/native-approvals.md +63 -0
- package/docs/reference/README.md +9 -8
- package/docs/reference/app-browser.md +232 -0
- package/docs/reference/app-node.md +38 -8
- package/docs/reference/app.md +4621 -805
- package/docs/reference/chain-node.md +74 -16
- package/docs/reference/chain.md +6740 -1592
- package/docs/reference/committee.md +5630 -987
- package/docs/reference/main.md +1439 -140
- package/docs/reference/oid4vp.md +297 -144
- package/docs/reference/verifier-agent.md +77 -35
- package/docs/signing.md +6 -0
- package/examples/encrypted-notes.ts +21 -6
- package/examples/getting-started.ts +2 -7
- package/examples/live-config.ts +24 -17
- package/package.json +17 -5
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
// ADR-0090 D7 session grants: one presentation, many KEM decrypts. The client half of the
|
|
2
|
+
// reference `keykeeper_committee::session_grant` (design: docs/plans/session-grants.md in the
|
|
3
|
+
// network repo). Canonical bytes, hashes and thumbprints must agree byte-for-byte with it;
|
|
4
|
+
// `test/specs/committee.session-grant.test.ts` pins them against the reference vectors.
|
|
5
|
+
//
|
|
6
|
+
// Pure: no HTTP, no chain. The caller supplies the anchored verifier set and each slot's policy.
|
|
7
|
+
import { keccak_256 } from '@noble/hashes/sha3';
|
|
8
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
9
|
+
import { concatBytes } from '@noble/hashes/utils';
|
|
10
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
11
|
+
import { p256 } from '@noble/curves/p256';
|
|
12
|
+
import { bytesToHex, hexToBytes } from '../crypto/hex.js';
|
|
13
|
+
import { TasraError } from '../errors.js';
|
|
14
|
+
import { BINDING_HOLDER_KEY, BINDING_ISSUER_ASSERTED, BINDING_UNBOUND, MAX_STATUS_WINDOW_SECS, DEFAULT_STATUS_WINDOW_SECS, STATUS_SKEW_SECS, selectVerifierCommittee } from './token.js';
|
|
15
|
+
const utf8 = (s) => new TextEncoder().encode(s);
|
|
16
|
+
/** Domain of the quorum-signed grant bytes. */
|
|
17
|
+
export const SESSION_GRANT_DOMAIN = 'keykeeper-committee/session-grant/v1';
|
|
18
|
+
/** Domain of a single-operation session-key proof. */
|
|
19
|
+
export const SESSION_PROOF_DOMAIN = 'keykeeper-committee/session-proof/v1';
|
|
20
|
+
/** Domain of a batch session-key proof; a one-item batch proof never verifies as a single proof. */
|
|
21
|
+
export const SESSION_BATCH_PROOF_DOMAIN = 'keykeeper-committee/session-batch-proof/v1';
|
|
22
|
+
/** Domain of a refresh request signed by the session key (plan: phase 1's own domain). */
|
|
23
|
+
export const SESSION_REFRESH_DOMAIN = 'keykeeper-committee/session-refresh/v1';
|
|
24
|
+
/** Domain of a revoke request signed by the session key. */
|
|
25
|
+
export const SESSION_REVOKE_DOMAIN = 'keykeeper-committee/session-revoke/v1';
|
|
26
|
+
/** Most slots one grant may name. */
|
|
27
|
+
export const MAX_SESSION_SLOTS = 16;
|
|
28
|
+
/** Most items one batch proof may cover. */
|
|
29
|
+
export const MAX_BATCH_ITEMS = 64;
|
|
30
|
+
/** Longest `exp - iat`: the status attestation's longest window (the grant's `exp` is its status deadline). */
|
|
31
|
+
export const MAX_ACCESS_TTL_SECS = MAX_STATUS_WINDOW_SECS;
|
|
32
|
+
/** Default `exp - iat` when the SDK requests none. */
|
|
33
|
+
export const DEFAULT_ACCESS_TTL_SECS = DEFAULT_STATUS_WINDOW_SECS;
|
|
34
|
+
/** Longest idle period between two refreshes (30 min). */
|
|
35
|
+
export const MAX_IDLE_TIMEOUT_SECS = 30 * 60;
|
|
36
|
+
/** Longest a session may live from its opening presentation (12 h). */
|
|
37
|
+
export const MAX_ABSOLUTE_TTL_SECS = 12 * 3600;
|
|
38
|
+
/** `|proof.iat - now|` a keeper accepts. */
|
|
39
|
+
export const SESSION_PROOF_FRESHNESS_SECS = 30;
|
|
40
|
+
/** Bounds of a proof's `jti` (URL-safe characters only). */
|
|
41
|
+
export const MIN_JTI_LEN = 16;
|
|
42
|
+
/** Longest accepted proof `jti`, in characters. */
|
|
43
|
+
export const MAX_JTI_LEN = 64;
|
|
44
|
+
/** Most signatures a grant may carry (one per possible committee member). */
|
|
45
|
+
const MAX_GRANT_SIGNATURES = 4096;
|
|
46
|
+
const HEX32 = /^0x[0-9a-fA-F]{64}$/;
|
|
47
|
+
const HEX64B = /^0x[0-9a-fA-F]{128}$/;
|
|
48
|
+
const JTI = /^[A-Za-z0-9_-]+$/;
|
|
49
|
+
/** A refused grant, key or proof. `code` names the failed check. */
|
|
50
|
+
export class SessionGrantError extends TasraError {
|
|
51
|
+
/** Stable reason (`slot-not-granted`, `expired`, `quorum-not-met`, ...). */
|
|
52
|
+
code;
|
|
53
|
+
constructor(code, message) {
|
|
54
|
+
super(message);
|
|
55
|
+
this.code = code;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const refuse = (code, message) => { throw new SessionGrantError(code, message); };
|
|
59
|
+
function equal(a, b) {
|
|
60
|
+
if (a.length !== b.length)
|
|
61
|
+
return false;
|
|
62
|
+
let diff = 0;
|
|
63
|
+
for (let i = 0; i < a.length; i++)
|
|
64
|
+
diff |= a[i] ^ b[i];
|
|
65
|
+
return diff === 0;
|
|
66
|
+
}
|
|
67
|
+
function compare(a, b) {
|
|
68
|
+
for (let i = 0; i < Math.min(a.length, b.length); i++)
|
|
69
|
+
if (a[i] !== b[i])
|
|
70
|
+
return a[i] < b[i] ? -1 : 1;
|
|
71
|
+
return a.length - b.length;
|
|
72
|
+
}
|
|
73
|
+
function u32be(n) { const b = new Uint8Array(4); new DataView(b.buffer).setUint32(0, n >>> 0, false); return b; }
|
|
74
|
+
function u64be(n) { const b = new Uint8Array(8); new DataView(b.buffer).setBigUint64(0, BigInt(n), false); return b; }
|
|
75
|
+
function i64be(n) { const b = new Uint8Array(8); new DataView(b.buffer).setBigInt64(0, BigInt(n), false); return b; }
|
|
76
|
+
function safeInt(n, field, min = Number.MIN_SAFE_INTEGER) {
|
|
77
|
+
if (typeof n !== 'number' || !Number.isSafeInteger(n) || n < min)
|
|
78
|
+
refuse('malformed', `malformed session grant field ${field}`);
|
|
79
|
+
return n;
|
|
80
|
+
}
|
|
81
|
+
function hex32(s, field) {
|
|
82
|
+
if (typeof s !== 'string' || !HEX32.test(s))
|
|
83
|
+
refuse('malformed', `malformed session grant field ${field}`);
|
|
84
|
+
return hexToBytes(s);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Unpadded base64url (RFC 4648 §5), as JWK members and jtis use.
|
|
88
|
+
* @param bytes - Bytes to encode.
|
|
89
|
+
*/
|
|
90
|
+
export function toBase64Url(bytes) {
|
|
91
|
+
let s = '';
|
|
92
|
+
for (const b of bytes)
|
|
93
|
+
s += String.fromCharCode(b);
|
|
94
|
+
return btoa(s).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
95
|
+
}
|
|
96
|
+
function unb64url(s) {
|
|
97
|
+
if (typeof s !== 'string' || !/^[A-Za-z0-9_-]*$/.test(s))
|
|
98
|
+
refuse('bad-key', 'JWK coordinate is not base64url');
|
|
99
|
+
const padded = s.replace(/-/g, '+').replace(/_/g, '/') + '='.repeat((4 - (s.length % 4)) % 4);
|
|
100
|
+
const raw = atob(padded);
|
|
101
|
+
return Uint8Array.from(raw, c => c.charCodeAt(0));
|
|
102
|
+
}
|
|
103
|
+
const BINDING_WIRE = { unbound: BINDING_UNBOUND, holder_key: BINDING_HOLDER_KEY, issuer_asserted: BINDING_ISSUER_ASSERTED };
|
|
104
|
+
const BINDING_NAME = { [BINDING_UNBOUND]: 'unbound', [BINDING_HOLDER_KEY]: 'holder_key', [BINDING_ISSUER_ASSERTED]: 'issuer_asserted' };
|
|
105
|
+
/**
|
|
106
|
+
* Validate and build a grant payload with the reference bounds: KEM decrypt only, 1..16 strictly
|
|
107
|
+
* ascending slots, `0 < exp - iat <= 45`, `1 <= idle <= 1800`, `exp <= absoluteExp <= iat + 12 h`.
|
|
108
|
+
* @param p - Every payload field.
|
|
109
|
+
*/
|
|
110
|
+
export function sessionGrantPayload(p) {
|
|
111
|
+
if (p.action !== 'decrypt')
|
|
112
|
+
refuse('unsupported-action', `session grant action ${String(p.action)} is not supported (KEM decrypt only)`);
|
|
113
|
+
if (!Array.isArray(p.slots) || p.slots.length === 0 || p.slots.length > MAX_SESSION_SLOTS)
|
|
114
|
+
refuse('slot-count', `session grant names ${p.slots?.length} slots (need 1..=${MAX_SESSION_SLOTS})`);
|
|
115
|
+
for (const s of p.slots)
|
|
116
|
+
if (s.slotId.length !== 32 || s.ruleHash.length !== 32)
|
|
117
|
+
refuse('malformed', 'grant slot fields must be 32 bytes');
|
|
118
|
+
for (let i = 1; i < p.slots.length; i++)
|
|
119
|
+
if (compare(p.slots[i - 1].slotId, p.slots[i].slotId) >= 0)
|
|
120
|
+
refuse('slot-order', `session grant slots are not strictly ascending at index ${i}`);
|
|
121
|
+
for (const [name, v] of [['session_id', p.sessionId], ['holder_hash', p.holderHash], ['session_key_thumbprint', p.sessionKeyThumbprint], ['seed', p.seed]]) {
|
|
122
|
+
if (!(v instanceof Uint8Array) || v.length !== 32)
|
|
123
|
+
refuse('malformed', `malformed session grant field ${name}`);
|
|
124
|
+
}
|
|
125
|
+
if (!(p.binding in BINDING_NAME))
|
|
126
|
+
refuse('malformed', 'malformed session grant field binding');
|
|
127
|
+
if (p.chainId < 0n || p.chainId > 0xffffffffffffffffn || p.epoch < 0n || p.epoch > 0xffffffffffffffffn)
|
|
128
|
+
refuse('malformed', 'grant chain id or epoch out of range');
|
|
129
|
+
const ttl = p.exp - p.iat;
|
|
130
|
+
if (!Number.isSafeInteger(p.iat) || !Number.isSafeInteger(p.exp) || !(ttl > 0 && ttl <= MAX_ACCESS_TTL_SECS))
|
|
131
|
+
refuse('lifetime', `session grant lifetime invalid: iat ${p.iat}, exp ${p.exp}`);
|
|
132
|
+
if (!Number.isSafeInteger(p.idleTimeoutSecs) || p.idleTimeoutSecs < 1 || p.idleTimeoutSecs > MAX_IDLE_TIMEOUT_SECS)
|
|
133
|
+
refuse('idle-timeout', `session grant idle timeout ${p.idleTimeoutSecs} s is invalid`);
|
|
134
|
+
if (!Number.isSafeInteger(p.absoluteExp) || p.absoluteExp < p.exp || p.absoluteExp > p.iat + MAX_ABSOLUTE_TTL_SECS)
|
|
135
|
+
refuse('absolute-exp', `session grant absolute expiry ${p.absoluteExp} is out of bounds`);
|
|
136
|
+
return { ...p, slots: p.slots.map(s => ({ slotId: s.slotId.slice(), ruleHash: s.ruleHash.slice() })) };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The canonical grant bytes (reference module docs give the layout).
|
|
140
|
+
* @param p - Grant payload.
|
|
141
|
+
*/
|
|
142
|
+
export function sessionGrantCanonicalBytes(p) {
|
|
143
|
+
const action = utf8(p.action);
|
|
144
|
+
return concatBytes(utf8(SESSION_GRANT_DOMAIN), u64be(p.chainId), p.sessionId, p.holderHash, p.sessionKeyThumbprint, Uint8Array.of(p.binding), u32be(action.length), action, p.seed, u64be(p.epoch), u32be(p.slots.length), ...p.slots.flatMap(s => [s.slotId, s.ruleHash]), i64be(p.iat), i64be(p.exp), u32be(p.idleTimeoutSecs), i64be(p.absoluteExp));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* keccak256 of the canonical bytes: what each verifier signs and every session proof binds to.
|
|
148
|
+
* @param p - Grant payload.
|
|
149
|
+
*/
|
|
150
|
+
export function sessionGrantHash(p) {
|
|
151
|
+
return keccak_256(sessionGrantCanonicalBytes(p));
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Decode a wire grant through the payload bounds. Refuses unknown fields and malformed hex.
|
|
155
|
+
* @param wire - Grant as received on the wire.
|
|
156
|
+
*/
|
|
157
|
+
export function decodeSessionGrant(wire) {
|
|
158
|
+
const w = wire;
|
|
159
|
+
if (!w || typeof w !== 'object' || Array.isArray(w))
|
|
160
|
+
refuse('malformed', 'session grant is not an object');
|
|
161
|
+
const allowed = new Set(['chain_id', 'session_id', 'holder_hash', 'session_key_thumbprint', 'binding', 'action', 'seed', 'epoch', 'slots', 'iat', 'exp', 'idle_timeout_secs', 'absolute_exp', 'signatures']);
|
|
162
|
+
for (const k of Object.keys(w))
|
|
163
|
+
if (!allowed.has(k))
|
|
164
|
+
refuse('malformed', `unknown session grant field ${k}`);
|
|
165
|
+
if (!Array.isArray(w.slots) || w.slots.length > MAX_SESSION_SLOTS)
|
|
166
|
+
refuse('slot-count', 'session grant slots missing or too many');
|
|
167
|
+
if (!Array.isArray(w.signatures) || w.signatures.length > MAX_GRANT_SIGNATURES)
|
|
168
|
+
refuse('malformed', 'session grant signatures missing or too many');
|
|
169
|
+
const binding = BINDING_WIRE[w.binding];
|
|
170
|
+
if (binding === undefined)
|
|
171
|
+
refuse('malformed', 'malformed session grant field binding');
|
|
172
|
+
if (w.action !== 'decrypt')
|
|
173
|
+
refuse('unsupported-action', 'session grants are KEM decrypt only');
|
|
174
|
+
const slots = w.slots.map(s => {
|
|
175
|
+
const o = s;
|
|
176
|
+
if (!o || typeof o !== 'object' || Object.keys(o).some(k => k !== 'slot_id' && k !== 'rule_hash'))
|
|
177
|
+
refuse('malformed', 'malformed grant slot');
|
|
178
|
+
return { slotId: hex32(o.slot_id, 'slot_id'), ruleHash: hex32(o.rule_hash, 'rule_hash') };
|
|
179
|
+
});
|
|
180
|
+
const payload = sessionGrantPayload({
|
|
181
|
+
chainId: BigInt(safeInt(w.chain_id, 'chain_id', 0)), sessionId: hex32(w.session_id, 'session_id'), holderHash: hex32(w.holder_hash, 'holder_hash'),
|
|
182
|
+
sessionKeyThumbprint: hex32(w.session_key_thumbprint, 'session_key_thumbprint'), binding: binding, action: 'decrypt',
|
|
183
|
+
seed: hex32(w.seed, 'seed'), epoch: BigInt(safeInt(w.epoch, 'epoch', 0)), slots, iat: safeInt(w.iat, 'iat'), exp: safeInt(w.exp, 'exp'),
|
|
184
|
+
idleTimeoutSecs: safeInt(w.idle_timeout_secs, 'idle_timeout_secs', 0), absoluteExp: safeInt(w.absolute_exp, 'absolute_exp'),
|
|
185
|
+
});
|
|
186
|
+
const signatures = w.signatures.map(s => {
|
|
187
|
+
const o = s;
|
|
188
|
+
if (!o || typeof o !== 'object' || Object.keys(o).some(k => k !== 'verifier_index' && k !== 'signature'))
|
|
189
|
+
refuse('malformed', 'malformed grant signature');
|
|
190
|
+
if (typeof o.signature !== 'string' || !HEX64B.test(o.signature))
|
|
191
|
+
refuse('malformed', 'malformed grant signature');
|
|
192
|
+
return { verifierIndex: safeInt(o.verifier_index, 'verifier_index', 0), signature: hexToBytes(o.signature) };
|
|
193
|
+
});
|
|
194
|
+
return { payload, signatures };
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Encode a grant to its wire form (the inverse of {@link decodeSessionGrant}).
|
|
198
|
+
* @param grant - Decoded grant.
|
|
199
|
+
*/
|
|
200
|
+
export function encodeSessionGrant(grant) {
|
|
201
|
+
const p = grant.payload;
|
|
202
|
+
return {
|
|
203
|
+
chain_id: Number(p.chainId), session_id: bytesToHex(p.sessionId), holder_hash: bytesToHex(p.holderHash), session_key_thumbprint: bytesToHex(p.sessionKeyThumbprint),
|
|
204
|
+
binding: BINDING_NAME[p.binding], action: p.action, seed: bytesToHex(p.seed), epoch: Number(p.epoch),
|
|
205
|
+
slots: p.slots.map(s => ({ slot_id: bytesToHex(s.slotId), rule_hash: bytesToHex(s.ruleHash) })),
|
|
206
|
+
iat: p.iat, exp: p.exp, idle_timeout_secs: p.idleTimeoutSecs, absolute_exp: p.absoluteExp,
|
|
207
|
+
signatures: grant.signatures.map(s => ({ verifier_index: s.verifierIndex, signature: bytesToHex(s.signature) })),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Verify a grant for serving one slot, with the reference steps in their order: the slot is
|
|
212
|
+
* granted with the slot's own rule; the quorum is non-zero; `now <= exp + 5` and `now + 5 >= iat`
|
|
213
|
+
* (the status deadline, never a token leeway); the grant's seed is the beacon seed of its epoch;
|
|
214
|
+
* the slot's OWN draw `(slot, epoch, seed)`; every
|
|
215
|
+
* signature verifies strictly (a forged one refuses the grant); a quorum of the slot's drawn
|
|
216
|
+
* committee signed. Signatures from outside the draw are checked but do not count.
|
|
217
|
+
* @param o - Grant, verifier set, slot policy and time.
|
|
218
|
+
*/
|
|
219
|
+
export function verifySessionGrant(o) {
|
|
220
|
+
const p = o.grant.payload;
|
|
221
|
+
const slot = p.slots.find(s => equal(s.slotId, o.slotId)) ?? refuse('slot-not-granted', `slot ${bytesToHex(o.slotId)} is not named by the session grant`);
|
|
222
|
+
if (!equal(slot.ruleHash, o.slotRuleHash))
|
|
223
|
+
refuse('rule-mismatch', 'grant rule_hash does not match the slot rule hash');
|
|
224
|
+
if (!Number.isInteger(o.quorum) || o.quorum < 1)
|
|
225
|
+
refuse('zero-quorum', 'invalid policy: quorum must be >= 1');
|
|
226
|
+
if (o.now > p.exp + STATUS_SKEW_SECS)
|
|
227
|
+
refuse('expired', `session grant expired at ${p.exp}`);
|
|
228
|
+
if (o.now + STATUS_SKEW_SECS < p.iat)
|
|
229
|
+
refuse('not-yet-valid', `session grant not yet valid (iat ${p.iat})`);
|
|
230
|
+
if (!(o.seed instanceof Uint8Array) || o.seed.length !== 32 || !equal(p.seed, o.seed))
|
|
231
|
+
refuse('seed-mismatch', `session grant seed is not the beacon seed of epoch ${p.epoch}`);
|
|
232
|
+
const expected = selectVerifierCommittee(o.slotId, p.epoch, p.seed, o.set.registrySize, o.committee);
|
|
233
|
+
const hash = sessionGrantHash(p);
|
|
234
|
+
const verified = [];
|
|
235
|
+
for (const { verifierIndex, signature } of o.grant.signatures) {
|
|
236
|
+
const pk = o.set.pubkeys.get(verifierIndex) ?? refuse('unknown-verifier', `signer index ${verifierIndex} is not in the active verifier set`);
|
|
237
|
+
// RFC 8032 strict verification, the counterpart of the reference `verify_strict`.
|
|
238
|
+
let ok;
|
|
239
|
+
try {
|
|
240
|
+
ok = signature.length === 64 && ed25519.verify(signature, hash, pk, { zip215: false });
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
ok = false;
|
|
244
|
+
}
|
|
245
|
+
if (!ok)
|
|
246
|
+
refuse('bad-signature', `invalid grant signature from verifier index ${verifierIndex}`);
|
|
247
|
+
if (expected.includes(verifierIndex) && !verified.includes(verifierIndex))
|
|
248
|
+
verified.push(verifierIndex);
|
|
249
|
+
}
|
|
250
|
+
if (verified.length < o.quorum)
|
|
251
|
+
refuse('quorum-not-met', `quorum not met for the slot: ${verified.length} of its drawn committee signed, need ${o.quorum}`);
|
|
252
|
+
return { grantHash: hash, slotId: o.slotId.slice(), chainId: p.chainId, holderHash: p.holderHash.slice(), sessionKeyThumbprint: p.sessionKeyThumbprint.slice(),
|
|
253
|
+
binding: p.binding, iat: p.iat, exp: p.exp, verifiedSigners: verified };
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Parse a public session JWK; any private member or unsupported curve is refused.
|
|
257
|
+
* @param jwk - Candidate JWK.
|
|
258
|
+
*/
|
|
259
|
+
export function parseSessionKeyJwk(jwk) {
|
|
260
|
+
const o = jwk;
|
|
261
|
+
if (!o || typeof o !== 'object' || Array.isArray(o))
|
|
262
|
+
refuse('bad-key', 'JWK is not an object');
|
|
263
|
+
if ('d' in o)
|
|
264
|
+
refuse('bad-key', 'JWK carries a private member');
|
|
265
|
+
if (o.kty === 'OKP' && o.crv === 'Ed25519') {
|
|
266
|
+
const x = unb64url(o.x);
|
|
267
|
+
if (x.length !== 32)
|
|
268
|
+
refuse('bad-key', 'Ed25519 x is not 32 bytes');
|
|
269
|
+
try {
|
|
270
|
+
ed25519.ExtendedPoint.fromHex(x);
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
refuse('bad-key', 'not an Ed25519 point');
|
|
274
|
+
}
|
|
275
|
+
return { kty: 'OKP', crv: 'Ed25519', x: toBase64Url(x) };
|
|
276
|
+
}
|
|
277
|
+
if (o.kty === 'EC' && o.crv === 'P-256') {
|
|
278
|
+
const x = unb64url(o.x), y = unb64url(o.y);
|
|
279
|
+
if (x.length !== 32 || y.length !== 32)
|
|
280
|
+
refuse('bad-key', 'P-256 coordinates are not 32 bytes');
|
|
281
|
+
try {
|
|
282
|
+
p256.ProjectivePoint.fromHex(concatBytes(Uint8Array.of(4), x, y)).assertValidity();
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
refuse('bad-key', 'not a P-256 point');
|
|
286
|
+
}
|
|
287
|
+
return { kty: 'EC', crv: 'P-256', x: toBase64Url(x), y: toBase64Url(y) };
|
|
288
|
+
}
|
|
289
|
+
return refuse('bad-key', 'unsupported kty/crv');
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* RFC 7638 thumbprint: SHA-256 of the required members in lexicographic order, no whitespace.
|
|
293
|
+
* @param jwk - Public session key.
|
|
294
|
+
*/
|
|
295
|
+
export function sessionKeyThumbprint(jwk) {
|
|
296
|
+
const k = parseSessionKeyJwk(jwk);
|
|
297
|
+
const canonical = k.kty === 'OKP' ? `{"crv":"Ed25519","kty":"OKP","x":"${k.x}"}` : `{"crv":"P-256","kty":"EC","x":"${k.x}","y":"${k.y}"}`;
|
|
298
|
+
return sha256(utf8(canonical));
|
|
299
|
+
}
|
|
300
|
+
function lenPrefixedJti(jti) {
|
|
301
|
+
checkJti(jti);
|
|
302
|
+
const b = utf8(jti);
|
|
303
|
+
return concatBytes(Uint8Array.of(b.length), b);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Refuse a jti outside 16..64 URL-safe characters.
|
|
307
|
+
* @param jti - Proof identifier.
|
|
308
|
+
*/
|
|
309
|
+
export function checkJti(jti) {
|
|
310
|
+
if (typeof jti !== 'string' || jti.length < MIN_JTI_LEN || jti.length > MAX_JTI_LEN || !JTI.test(jti))
|
|
311
|
+
refuse('bad-jti', `proof jti invalid (need ${MIN_JTI_LEN}..=${MAX_JTI_LEN} URL-safe characters)`);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* `keccak256(SESSION_PROOF_DOMAIN ‖ grant_hash ‖ request_hash ‖ iat i64 BE ‖ len(jti) u8 ‖ jti)`.
|
|
315
|
+
* @param grantHash - 32-byte grant hash.
|
|
316
|
+
* @param requestHash - 32-byte request hash of the operation.
|
|
317
|
+
* @param iat - Proof issued-at, Unix seconds.
|
|
318
|
+
* @param jti - Proof identifier.
|
|
319
|
+
*/
|
|
320
|
+
export function sessionProofHash(grantHash, requestHash, iat, jti) {
|
|
321
|
+
if (grantHash.length !== 32 || requestHash.length !== 32)
|
|
322
|
+
refuse('malformed', 'grant and request hashes must be 32 bytes');
|
|
323
|
+
return keccak_256(concatBytes(utf8(SESSION_PROOF_DOMAIN), grantHash, requestHash, i64be(iat), lenPrefixedJti(jti)));
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* `keccak256(SESSION_BATCH_PROOF_DOMAIN ‖ grant_hash ‖ count u32 BE ‖ request_hash* ‖ iat ‖ len(jti) ‖ jti)`, items in order.
|
|
327
|
+
* @param grantHash - 32-byte grant hash.
|
|
328
|
+
* @param requestHashes - 1..64 32-byte request hashes, in batch order.
|
|
329
|
+
* @param iat - Proof issued-at, Unix seconds.
|
|
330
|
+
* @param jti - Proof identifier.
|
|
331
|
+
*/
|
|
332
|
+
export function sessionBatchProofHash(grantHash, requestHashes, iat, jti) {
|
|
333
|
+
if (requestHashes.length === 0 || requestHashes.length > MAX_BATCH_ITEMS)
|
|
334
|
+
refuse('batch-size', `batch proof covers ${requestHashes.length} items (need 1..=${MAX_BATCH_ITEMS})`);
|
|
335
|
+
if (grantHash.length !== 32 || requestHashes.some(h => h.length !== 32))
|
|
336
|
+
refuse('malformed', 'grant and request hashes must be 32 bytes');
|
|
337
|
+
return keccak_256(concatBytes(utf8(SESSION_BATCH_PROOF_DOMAIN), grantHash, u32be(requestHashes.length), ...requestHashes, i64be(iat), lenPrefixedJti(jti)));
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* The 32-byte hash a session key signs to refresh (or revoke) a grant:
|
|
341
|
+
* `keccak256(domain ‖ grant_hash ‖ seq u64 BE ‖ issued_at i64 BE)`.
|
|
342
|
+
* Integer inputs are encoded big-endian as u64/i64, as in the rest of the session-grant module.
|
|
343
|
+
* @param kind - Which control domain to use.
|
|
344
|
+
* @param grantHash - 32-byte grant hash.
|
|
345
|
+
* @param seq - Request sequence number (u64).
|
|
346
|
+
* @param issuedAt - Request time, Unix seconds.
|
|
347
|
+
*/
|
|
348
|
+
export function sessionControlHash(kind, grantHash, seq, issuedAt) {
|
|
349
|
+
if (grantHash.length !== 32 || !Number.isSafeInteger(seq) || seq < 0 || !Number.isSafeInteger(issuedAt))
|
|
350
|
+
refuse('malformed', 'invalid session control inputs');
|
|
351
|
+
return keccak_256(concatBytes(utf8(kind === 'refresh' ? SESSION_REFRESH_DOMAIN : SESSION_REVOKE_DOMAIN), grantHash, u64be(seq), i64be(issuedAt)));
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Verify a 64-byte session-key signature over a 32-byte hash exactly as a keeper does: Ed25519
|
|
355
|
+
* strict, or ES256 (ECDSA over SHA-256 of the hash, `r ‖ s`; high-S accepted like WebCrypto).
|
|
356
|
+
* @param jwk - Public session key.
|
|
357
|
+
* @param hash - 32-byte proof or control hash.
|
|
358
|
+
* @param signature - 64-byte signature.
|
|
359
|
+
*/
|
|
360
|
+
export function verifySessionSignature(jwk, hash, signature) {
|
|
361
|
+
const k = parseSessionKeyJwk(jwk);
|
|
362
|
+
if (signature.length !== 64 || hash.length !== 32)
|
|
363
|
+
return false;
|
|
364
|
+
try {
|
|
365
|
+
if (k.kty === 'OKP')
|
|
366
|
+
return ed25519.verify(signature, hash, unb64url(k.x), { zip215: false });
|
|
367
|
+
return p256.verify(signature, sha256(hash), concatBytes(Uint8Array.of(4), unb64url(k.x), unb64url(k.y)), { lowS: false, prehash: false });
|
|
368
|
+
}
|
|
369
|
+
catch {
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* A client-side check that a proof would pass a keeper's freshness and key checks.
|
|
375
|
+
* @param grant - Verified grant the proof binds to.
|
|
376
|
+
* @param jwk - Session key that signs the proof.
|
|
377
|
+
* @param proof - The proof's `iat` and `jti`.
|
|
378
|
+
* @param now - Current time, Unix seconds.
|
|
379
|
+
*/
|
|
380
|
+
export function checkProofWindow(grant, jwk, proof, now) {
|
|
381
|
+
if (!equal(sessionKeyThumbprint(jwk), grant.sessionKeyThumbprint))
|
|
382
|
+
refuse('wrong-key', 'session key thumbprint does not match the grant');
|
|
383
|
+
checkJti(proof.jti);
|
|
384
|
+
const fresh = Math.abs(proof.iat - now) <= SESSION_PROOF_FRESHNESS_SECS && proof.iat + STATUS_SKEW_SECS >= grant.iat && proof.iat <= grant.exp + STATUS_SKEW_SECS;
|
|
385
|
+
if (!fresh)
|
|
386
|
+
refuse('proof-stale', `proof iat ${proof.iat} is outside the accepted window`);
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Encode a proof for the wire.
|
|
390
|
+
* @param proof - Proof with its 64-byte signature.
|
|
391
|
+
*/
|
|
392
|
+
export function encodeSessionProof(proof) {
|
|
393
|
+
checkJti(proof.jti);
|
|
394
|
+
if (proof.signature.length !== 64)
|
|
395
|
+
refuse('malformed', 'proof signature must be 64 bytes');
|
|
396
|
+
return { iat: proof.iat, jti: proof.jti, signature: bytesToHex(proof.signature) };
|
|
397
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { type StatusRefreshCode, type StatusRefreshFailure } from '../errors.js';
|
|
2
|
+
import { type HolderKey, type Jwk, type JwsAlg } from '../oid4vp/jose.js';
|
|
3
|
+
import type { ClientExecutionSigner } from './execution.js';
|
|
4
|
+
import { type CompoundTokenWire, type StatusAttestationWire, type VerifierSet } from './token.js';
|
|
5
|
+
/** Domain separator of the status-refresh proof (new name, `/v1`). */
|
|
6
|
+
export declare const STATUS_REFRESH_DOMAIN = "keykeeper/committee-status-refresh/v1";
|
|
7
|
+
/** JOSE `typ` of a `holder_key` refresh proof. */
|
|
8
|
+
export declare const STATUS_REFRESH_JWT_TYP = "keykeeper-status-refresh+jwt";
|
|
9
|
+
/** Verifier route (direct mode). */
|
|
10
|
+
export declare const STATUS_REFRESH_PATH = "/v1/committee/status-refresh";
|
|
11
|
+
/** Largest request body a verifier accepts, in bytes. */
|
|
12
|
+
export declare const MAX_STATUS_REFRESH_BODY_BYTES = 4096;
|
|
13
|
+
/** A verifier accepts `|issued_at - now| <= 30 s`. */
|
|
14
|
+
export declare const STATUS_REFRESH_FRESHNESS_SECS = 30;
|
|
15
|
+
/** Who proves the refresh: the principal the verifier recorded at issuance. */
|
|
16
|
+
export type StatusRefreshPrincipal = 'holder_key' | 'client_execution';
|
|
17
|
+
/** Inputs of {@link statusRefreshHash}: exactly the signed list, nothing else. */
|
|
18
|
+
export interface StatusRefreshHashInput {
|
|
19
|
+
/** Chain ID (u64). */
|
|
20
|
+
chainId: number | bigint;
|
|
21
|
+
/** 32-byte canonical token hash ({@link compoundTokenHash}), unchanged by the refresh. */
|
|
22
|
+
tokenHash: Uint8Array;
|
|
23
|
+
/** 32-byte ORIGINAL payment hash of a paid grant; omit (all-zero) for an unpaid grant. */
|
|
24
|
+
paymentHash?: Uint8Array;
|
|
25
|
+
/** Principal kind. */
|
|
26
|
+
principal: StatusRefreshPrincipal;
|
|
27
|
+
/** Strictly increasing per token per verifier; positive u64. */
|
|
28
|
+
seq: number | bigint;
|
|
29
|
+
/** Unix seconds (i64); a verifier accepts it within 30 s of its clock. */
|
|
30
|
+
issuedAt: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The 32-byte digest a refresh proof signs:
|
|
34
|
+
* `keccak256(DOMAIN || chainId(u64 BE) || tokenHash || paymentHash|zero32 || principalKind(u8)
|
|
35
|
+
* || seq(u64 BE) || issuedAt(i64 BE))`.
|
|
36
|
+
*
|
|
37
|
+
* @param input - Chain, token hash, original payment hash (paid only), principal, seq and issue time.
|
|
38
|
+
*/
|
|
39
|
+
export declare function statusRefreshHash(input: StatusRefreshHashInput): Uint8Array;
|
|
40
|
+
/** Holder-key signer for a `holder_key` refresh proof. The key must be the one recorded at issuance. */
|
|
41
|
+
export interface StatusRefreshHolderSigner {
|
|
42
|
+
/** `ES256` for a P-256 key, `EdDSA` for an Ed25519 key (the recorded algorithm). */
|
|
43
|
+
alg: JwsAlg;
|
|
44
|
+
/** Public JWK placed in the JWS header (its thumbprint must equal the recorded one). */
|
|
45
|
+
publicJwk: Jwk;
|
|
46
|
+
/** Raw JWS signature over the signing input (ES256: 64-byte r||s; EdDSA: 64 bytes). */
|
|
47
|
+
sign(signingInput: Uint8Array): Uint8Array | Promise<Uint8Array>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A {@link StatusRefreshHolderSigner} over a local holder key (P-256 signs ES256, Ed25519 signs EdDSA).
|
|
51
|
+
* The returned signer keeps a reference to `holder.privateKey`; the caller owns its lifetime.
|
|
52
|
+
*
|
|
53
|
+
* @param holder - Holder key the credential was bound to at issuance.
|
|
54
|
+
*/
|
|
55
|
+
export declare function statusRefreshHolderSigner(holder: HolderKey): StatusRefreshHolderSigner;
|
|
56
|
+
/** The principal and signer of one refresh proof. */
|
|
57
|
+
export type StatusRefreshProver = {
|
|
58
|
+
/** Principal kind: a paid grant proves with its ORIGINAL creator-certified execution key. */
|
|
59
|
+
kind: 'client_execution';
|
|
60
|
+
/** Signer over the original execution key. */
|
|
61
|
+
signer: ClientExecutionSigner;
|
|
62
|
+
} | {
|
|
63
|
+
/** Principal kind: the holder key recorded at issuance. */
|
|
64
|
+
kind: 'holder_key';
|
|
65
|
+
/** Signer over the holder key recorded at issuance. */
|
|
66
|
+
signer: StatusRefreshHolderSigner;
|
|
67
|
+
};
|
|
68
|
+
/** One refresh proof on the wire. */
|
|
69
|
+
export interface StatusRefreshProofWire {
|
|
70
|
+
/** Principal kind. */
|
|
71
|
+
kind: StatusRefreshPrincipal;
|
|
72
|
+
/** Positive integer, strictly increasing per token per verifier. */
|
|
73
|
+
seq: number;
|
|
74
|
+
/** Unix seconds. */
|
|
75
|
+
issued_at: number;
|
|
76
|
+
/** `client_execution`: `0x` + 128 hex; `holder_key`: compact JWS. */
|
|
77
|
+
signature: string;
|
|
78
|
+
}
|
|
79
|
+
/** Inputs of {@link createStatusRefreshProof}. */
|
|
80
|
+
export interface CreateStatusRefreshProofOptions {
|
|
81
|
+
/** Chain ID. */
|
|
82
|
+
chainId: number | bigint;
|
|
83
|
+
/** 32-byte canonical token hash. */
|
|
84
|
+
tokenHash: Uint8Array;
|
|
85
|
+
/** ORIGINAL payment hash of a paid grant (omit when unpaid). */
|
|
86
|
+
paymentHash?: Uint8Array;
|
|
87
|
+
/** Journaled sequence number (positive safe integer). Never reuse one. */
|
|
88
|
+
seq: number;
|
|
89
|
+
/** Unix seconds (default now). */
|
|
90
|
+
issuedAt?: number;
|
|
91
|
+
/** Principal and signer. */
|
|
92
|
+
prover: StatusRefreshProver;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Sign one refresh proof. `client_execution` signs the hash with the execution key
|
|
96
|
+
* (`0x`-hex Ed25519); `holder_key` signs a compact JWS (`typ: keykeeper-status-refresh+jwt`,
|
|
97
|
+
* header `jwk`, payload `{"refresh":"0x<hash>"}`).
|
|
98
|
+
*
|
|
99
|
+
* @param opts - Chain, token hash, original payment hash, seq, issue time and prover.
|
|
100
|
+
*/
|
|
101
|
+
export declare function createStatusRefreshProof(opts: CreateStatusRefreshProofOptions): Promise<StatusRefreshProofWire>;
|
|
102
|
+
/**
|
|
103
|
+
* The exact verifier request body `{chain_id, token_hash, proof}` (≤ 4096 bytes).
|
|
104
|
+
*
|
|
105
|
+
* @param chainId - Chain ID (a JavaScript safe integer).
|
|
106
|
+
* @param tokenHash - 32-byte canonical token hash.
|
|
107
|
+
* @param proof - Signed proof.
|
|
108
|
+
*/
|
|
109
|
+
export declare function statusRefreshRequestBody(chainId: number, tokenHash: Uint8Array, proof: StatusRefreshProofWire): Uint8Array;
|
|
110
|
+
/** One verifier's refresh reply - exactly these four keys. */
|
|
111
|
+
export interface StatusRefreshReplyWire {
|
|
112
|
+
/** `0x` + 64 hex: the unchanged token hash. */
|
|
113
|
+
token_hash: string;
|
|
114
|
+
/** The verifier's registry index (u32). */
|
|
115
|
+
verifier_index: number;
|
|
116
|
+
/** Unix seconds the verifier now vouches until. */
|
|
117
|
+
status_until: number;
|
|
118
|
+
/** 64-byte Ed25519 signature over the status-attestation hash, hex. */
|
|
119
|
+
status_signature: string;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Strictly validate an untrusted verifier refresh reply. Throws {@link StatusRefreshError}
|
|
123
|
+
* (`status_refresh_invalid_response`). Validates the SHAPE only; the signature is checked against
|
|
124
|
+
* the original roster by the refresh functions.
|
|
125
|
+
*
|
|
126
|
+
* @param raw - Parsed JSON reply.
|
|
127
|
+
*/
|
|
128
|
+
export declare function parseStatusRefreshReply(raw: unknown): StatusRefreshReplyWire;
|
|
129
|
+
/**
|
|
130
|
+
* Classify a non-200 refresh answer by its HTTP status and the stable code in its body. A code
|
|
131
|
+
* that does not belong to the status is ignored; an unclassified answer is `status_refresh_http`.
|
|
132
|
+
*
|
|
133
|
+
* @param status - HTTP status.
|
|
134
|
+
* @param body - Response body text.
|
|
135
|
+
*/
|
|
136
|
+
export declare function classifyStatusRefreshResponse(status: number, body: string): StatusRefreshCode;
|
|
137
|
+
/** The grant a refresh re-attests and the ORIGINAL trust it was verified under. */
|
|
138
|
+
export interface StatusRefreshGrant {
|
|
139
|
+
/** The grant as journaled (its signatures and every hashed field are never changed). */
|
|
140
|
+
token: CompoundTokenWire;
|
|
141
|
+
/** Keys of the token epoch's anchored roster: the keys the token signatures verified under. */
|
|
142
|
+
set: VerifierSet;
|
|
143
|
+
/** The token's quorum. */
|
|
144
|
+
quorum: number;
|
|
145
|
+
/** Further hard caps, Unix seconds (e.g. the payment expiry). The token `exp` is always one. */
|
|
146
|
+
caps?: readonly number[];
|
|
147
|
+
}
|
|
148
|
+
/** A successful refresh. */
|
|
149
|
+
export interface StatusRefreshResult {
|
|
150
|
+
/** The grant with `status_attestations` replaced by the merge; signatures and hashed fields unchanged. */
|
|
151
|
+
token: CompoundTokenWire;
|
|
152
|
+
/** Merged attestations, one per counted verifier index (the later `status_until` wins), by index. */
|
|
153
|
+
statusAttestations: StatusAttestationWire[];
|
|
154
|
+
/** Indexes whose NEW attestation was accepted. */
|
|
155
|
+
refreshed: number[];
|
|
156
|
+
/** Effective status deadline over the merged list (the quorum-th largest), Unix seconds. */
|
|
157
|
+
statusUntil: number;
|
|
158
|
+
/** Verifiers that contributed nothing, and why. */
|
|
159
|
+
failures: StatusRefreshFailure[];
|
|
160
|
+
/**
|
|
161
|
+
* Original signers whose ORIGINAL attestation must not rejoin this result: withdrawn by their own
|
|
162
|
+
* typed negative outcome (direct), or left out of the relay's answer (relay: the relay's set is
|
|
163
|
+
* the whole answer). A caller merging with a journaled list drops these indexes from it.
|
|
164
|
+
*/
|
|
165
|
+
ineligible: number[];
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Whether a verifier's typed refresh outcome withdraws its ORIGINAL attestation from the merged
|
|
169
|
+
* result: the verifier, answering for this token, said its status statement does not stand now
|
|
170
|
+
* (revoked, past a cap, the principal does not verify, its context moved, it cannot read status,
|
|
171
|
+
* or the proof replays). Transport, unknown or malformed outcomes withdraw nothing.
|
|
172
|
+
*
|
|
173
|
+
* @param code - A failure's code.
|
|
174
|
+
*/
|
|
175
|
+
export declare function withdrawsOriginal(code: StatusRefreshCode): boolean;
|
|
176
|
+
/** A bounded HTTP answer. */
|
|
177
|
+
export interface StatusRefreshHttpResponse {
|
|
178
|
+
/** HTTP status. */
|
|
179
|
+
status: number;
|
|
180
|
+
/** Body bytes or text. */
|
|
181
|
+
body: Uint8Array | string;
|
|
182
|
+
/** Parsed `Retry-After`, ms. */
|
|
183
|
+
retryAfterMs?: number;
|
|
184
|
+
}
|
|
185
|
+
/** Options of {@link refreshStatusDirect}. */
|
|
186
|
+
export interface RefreshStatusDirectOptions extends StatusRefreshGrant {
|
|
187
|
+
/** Chain ID (the one the proof signs). */
|
|
188
|
+
chainId: number;
|
|
189
|
+
/** One signed proof; fanned out unchanged to the original signers. */
|
|
190
|
+
proof: StatusRefreshProofWire;
|
|
191
|
+
/** Verifier base URLs by registry index. Only indexes that verifiably signed the token are called. */
|
|
192
|
+
verifiers: readonly {
|
|
193
|
+
index: number;
|
|
194
|
+
url: string;
|
|
195
|
+
}[];
|
|
196
|
+
/** Fetch implementation (default global fetch). */
|
|
197
|
+
fetch?: typeof fetch;
|
|
198
|
+
/** Permit `http:` endpoints (local development only). */
|
|
199
|
+
allowHttp?: boolean;
|
|
200
|
+
/** Cancellation. */
|
|
201
|
+
signal?: AbortSignal;
|
|
202
|
+
/** Per-verifier deadline, ms (default 15000). */
|
|
203
|
+
timeoutMs?: number;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Refresh a grant's status directly at its original signers' verifiers
|
|
207
|
+
* (`POST /v1/committee/status-refresh`), verify every reply under the original roster, and merge.
|
|
208
|
+
* Sends the one proof once per original signer; never retries.
|
|
209
|
+
*
|
|
210
|
+
* @param opts - Grant, roster, quorum, caps, chain, proof and verifier endpoints.
|
|
211
|
+
* @returns The merged grant and its new effective deadline.
|
|
212
|
+
* @throws StatusRefreshError when fewer than a quorum of fresh, valid, capped attestations arrive.
|
|
213
|
+
*/
|
|
214
|
+
export declare function refreshStatusDirect(opts: RefreshStatusDirectOptions): Promise<StatusRefreshResult>;
|
|
215
|
+
/**
|
|
216
|
+
* Verify a verifier-agent relay answer (`POST /v1/sessions/:id/status-refresh`). A 200 body must
|
|
217
|
+
* be exactly `{token_hash, status_attestations}`; each attestation is re-verified here under the
|
|
218
|
+
* original roster (the agent is not trusted). A non-200 answer becomes a typed
|
|
219
|
+
* {@link StatusRefreshError}, with the relay's per-index `reasons` when it sent them.
|
|
220
|
+
*
|
|
221
|
+
* @param grant - Grant, original roster, quorum and caps.
|
|
222
|
+
* @param response - The relay's bounded HTTP answer.
|
|
223
|
+
*/
|
|
224
|
+
export declare function acceptStatusRefreshRelay(grant: StatusRefreshGrant, response: StatusRefreshHttpResponse): StatusRefreshResult;
|
|
225
|
+
/** Options of {@link refreshStatusViaAgent}. */
|
|
226
|
+
export interface RefreshStatusViaAgentOptions extends StatusRefreshGrant {
|
|
227
|
+
/** Verifier-agent base URL (the one that ran the session). */
|
|
228
|
+
agentUrl: string;
|
|
229
|
+
/** Session identity. */
|
|
230
|
+
sessionId: string;
|
|
231
|
+
/** The session's owner bearer (`poll_secret`). Never logged. */
|
|
232
|
+
pollSecret: string;
|
|
233
|
+
/** One signed proof. */
|
|
234
|
+
proof: StatusRefreshProofWire;
|
|
235
|
+
/** Fetch implementation (default global fetch). */
|
|
236
|
+
fetch?: typeof fetch;
|
|
237
|
+
/** Permit an `http:` agent URL (local development only). */
|
|
238
|
+
allowHttp?: boolean;
|
|
239
|
+
/** Cancellation. */
|
|
240
|
+
signal?: AbortSignal;
|
|
241
|
+
/** Deadline, ms (default 15000). */
|
|
242
|
+
timeoutMs?: number;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Refresh a grant's status through the verifier-agent relay. The agent fans the proof out to the
|
|
246
|
+
* original signers; its answer is re-verified here exactly as in {@link refreshStatusDirect}.
|
|
247
|
+
*
|
|
248
|
+
* @param opts - Grant, roster, quorum, caps, agent session and proof.
|
|
249
|
+
* @throws StatusRefreshError on a typed refusal, an unsigned or forged reply, or no fresh quorum.
|
|
250
|
+
*/
|
|
251
|
+
export declare function refreshStatusViaAgent(opts: RefreshStatusViaAgentOptions): Promise<StatusRefreshResult>;
|
|
252
|
+
/**
|
|
253
|
+
* The relay request body `{proof}` (for a caller-owned transport).
|
|
254
|
+
*
|
|
255
|
+
* @param proof - Refresh proof, validated before it is encoded.
|
|
256
|
+
*/
|
|
257
|
+
export declare function statusRefreshRelayBody(proof: StatusRefreshProofWire): Uint8Array;
|