@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,75 @@
|
|
|
1
|
+
import type { Ciphertext } from './kem.js';
|
|
2
|
+
import { type CommitteeAction } from '../oid4vp/binding.js';
|
|
3
|
+
/** RFC 9180 suite accepted for client-sealed decryption contributions. */
|
|
4
|
+
export declare const RECIPIENT_SUITE = "hpke-x25519-sha256-chacha20poly1305";
|
|
5
|
+
/** Public recipient metadata bound before holder and client execution authorization. */
|
|
6
|
+
export interface RecipientWire {
|
|
7
|
+
/** Exact supported RFC 9180 suite name. */
|
|
8
|
+
suite: string;
|
|
9
|
+
/** Hex-encoded 32-byte X25519 public key; never a private key or recovery seed. */
|
|
10
|
+
public_key: string;
|
|
11
|
+
}
|
|
12
|
+
/** Public decryption context included in the signed authorization session. */
|
|
13
|
+
export interface DecryptionBindingWire {
|
|
14
|
+
/** Client recipient to which every keeper seals its contribution. */
|
|
15
|
+
recipient: RecipientWire;
|
|
16
|
+
/** Nonnegative canonical key epoch of the ciphertext. */
|
|
17
|
+
ciphertext_epoch: number;
|
|
18
|
+
/** Hex-encoded content digest: complete KEM ciphertext and identity, or SHA-256 of the IBE identity. */
|
|
19
|
+
content_digest?: string;
|
|
20
|
+
}
|
|
21
|
+
/** Standard padded-base64 HPKE envelope carrying one encrypted contribution. */
|
|
22
|
+
export interface SealedShareWire {
|
|
23
|
+
/** RFC 9180 encapsulated X25519 key, encoded as padded base64. */
|
|
24
|
+
enc: string;
|
|
25
|
+
/** Authenticated encrypted contribution, encoded as padded base64. */
|
|
26
|
+
ciphertext: string;
|
|
27
|
+
}
|
|
28
|
+
/** Client-held opening capability; only its public recipient belongs in requests or journals. */
|
|
29
|
+
export interface ClientRecipient {
|
|
30
|
+
/** Public recipient metadata that authorization must bind before any keeper work. */
|
|
31
|
+
readonly recipient: RecipientWire;
|
|
32
|
+
/** Open one contribution locally using the exact authorized request and keeper context.
|
|
33
|
+
* The caller must pairing-verify the recovered contribution and clear it after use.
|
|
34
|
+
* @param sealed - Keeper's encrypted contribution.
|
|
35
|
+
* @param requestHash - Chain-, slot-, action- and recipient-bound request hash.
|
|
36
|
+
* @param identifier - Distinct keeper's BLS participant identifier.
|
|
37
|
+
* @param epoch - Authorized ciphertext key epoch.
|
|
38
|
+
* @param verifyingShare - Authenticated 144-byte G2 || G1 public verifying share.
|
|
39
|
+
*/
|
|
40
|
+
open(sealed: SealedShareWire, requestHash: Uint8Array, identifier: number, epoch: number, verifyingShare: Uint8Array): Promise<Uint8Array>;
|
|
41
|
+
}
|
|
42
|
+
/** Create BEFORE requesting authorization. The private key stays in this closure and
|
|
43
|
+
* is never included in session or keeper requests. Generate a fresh instance per operation.
|
|
44
|
+
* @param seed - Optional client-held 32-byte random recovery seed for RFC 9180 DeriveKeyPair.
|
|
45
|
+
* Store it only in the client's private storage; the caller owns and must clear its buffer.
|
|
46
|
+
*/
|
|
47
|
+
export declare function createDecryptionRecipient(seed?: Uint8Array): Promise<ClientRecipient>;
|
|
48
|
+
/** Hash the complete KEM content with unambiguous field lengths.
|
|
49
|
+
* @param ct - Ciphertext including its group element, nonce and authenticated ciphertext.
|
|
50
|
+
* @param identity - Exact associated-data identity bytes used during encryption.
|
|
51
|
+
*/
|
|
52
|
+
export declare function kemContentDigest(ct: Ciphertext, identity: Uint8Array): Uint8Array;
|
|
53
|
+
/** Bind a content digest to the ciphertext epoch and client recipient.
|
|
54
|
+
* @param content - Complete 32-byte KEM content digest or SHA-256 IBE identity digest.
|
|
55
|
+
* @param epoch - Nonnegative canonical ciphertext key epoch.
|
|
56
|
+
* @param recipient - Supported suite and client X25519 public key.
|
|
57
|
+
*/
|
|
58
|
+
export declare function recipientPayloadDigest(content: Uint8Array, epoch: number, recipient: RecipientWire): Uint8Array;
|
|
59
|
+
/** Prepare the digest and public session metadata before requesting authorization.
|
|
60
|
+
* @param content - Complete KEM content digest or SHA-256 IBE identity digest.
|
|
61
|
+
* @param epoch - Canonical ciphertext key epoch.
|
|
62
|
+
* @param client - Original client-held recipient, retained for opening and recovery.
|
|
63
|
+
*/
|
|
64
|
+
export declare function decryptionBinding(content: Uint8Array, epoch: number, client: ClientRecipient): {
|
|
65
|
+
payloadDigest: Uint8Array;
|
|
66
|
+
decryption: DecryptionBindingWire;
|
|
67
|
+
};
|
|
68
|
+
/** Check the token binding before transmitting; refuse old or substituted grants.
|
|
69
|
+
* @param chainId - Deployment chain identifier.
|
|
70
|
+
* @param slotId - Hex-encoded 32-byte slot identifier.
|
|
71
|
+
* @param action - Keeper operation named by the authorization.
|
|
72
|
+
* @param digest - Recipient-bound 32-byte operation payload digest.
|
|
73
|
+
* @param claimed - Request hash carried by the signed token; omission is refused.
|
|
74
|
+
*/
|
|
75
|
+
export declare function checkedDecryptionRequestHash(chainId: number | bigint, slotId: string, action: CommitteeAction, digest: Uint8Array, claimed: string | undefined): Uint8Array;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// RFC 9180 recipient protection for Tasra threshold contributions. No custom HPKE.
|
|
2
|
+
import { CipherSuite, HkdfSha256 } from '@hpke/core';
|
|
3
|
+
import { DhkemX25519HkdfSha256 } from '@hpke/dhkem-x25519';
|
|
4
|
+
import { Chacha20Poly1305 } from '@hpke/chacha20poly1305';
|
|
5
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
6
|
+
import { bytesToHex, hexToBytes } from './hex.js';
|
|
7
|
+
import { base64Decode } from './envelope.js';
|
|
8
|
+
import { requestHash } from '../oid4vp/binding.js';
|
|
9
|
+
/** RFC 9180 suite accepted for client-sealed decryption contributions. */
|
|
10
|
+
export const RECIPIENT_SUITE = 'hpke-x25519-sha256-chacha20poly1305';
|
|
11
|
+
const utf8 = (s) => new TextEncoder().encode(s);
|
|
12
|
+
const suite = new CipherSuite({ kem: new DhkemX25519HkdfSha256(), kdf: new HkdfSha256(), aead: new Chacha20Poly1305() });
|
|
13
|
+
function concat(...parts) {
|
|
14
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
15
|
+
let offset = 0;
|
|
16
|
+
for (const p of parts) {
|
|
17
|
+
out.set(p, offset);
|
|
18
|
+
offset += p.length;
|
|
19
|
+
}
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
function be64(n) {
|
|
23
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
24
|
+
throw new Error('epoch/length must be a nonnegative safe integer');
|
|
25
|
+
const out = new Uint8Array(8);
|
|
26
|
+
new DataView(out.buffer).setBigUint64(0, BigInt(n));
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
function aad(hash, identifier, epoch, vs) {
|
|
30
|
+
if (hash.length !== 32 || !Number.isInteger(identifier) || identifier < 1 || identifier > 65535 || vs.length !== 144)
|
|
31
|
+
throw new Error('invalid sealed share context');
|
|
32
|
+
return concat(hash, new Uint8Array([identifier >>> 8, identifier & 255]), be64(epoch), vs);
|
|
33
|
+
}
|
|
34
|
+
/** Create BEFORE requesting authorization. The private key stays in this closure and
|
|
35
|
+
* is never included in session or keeper requests. Generate a fresh instance per operation.
|
|
36
|
+
* @param seed - Optional client-held 32-byte random recovery seed for RFC 9180 DeriveKeyPair.
|
|
37
|
+
* Store it only in the client's private storage; the caller owns and must clear its buffer.
|
|
38
|
+
*/
|
|
39
|
+
export async function createDecryptionRecipient(seed) {
|
|
40
|
+
const derive = async (original) => {
|
|
41
|
+
if (original.length !== 32 || original.every(b => b === 0))
|
|
42
|
+
throw new Error('Recipient recovery seed must contain 32 random bytes');
|
|
43
|
+
const copy = new Uint8Array(original);
|
|
44
|
+
try {
|
|
45
|
+
return await suite.kem.deriveKeyPair(copy);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
copy.fill(0);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const keys = seed === undefined ? await suite.kem.generateKeyPair() : await derive(seed);
|
|
52
|
+
const publicKey = new Uint8Array(await suite.kem.serializePublicKey(keys.publicKey));
|
|
53
|
+
const recipient = Object.freeze({ suite: RECIPIENT_SUITE, public_key: bytesToHex(publicKey) });
|
|
54
|
+
return Object.freeze({
|
|
55
|
+
recipient,
|
|
56
|
+
async open(sealed, hash, identifier, epoch, vs) {
|
|
57
|
+
if (!sealed || typeof sealed.enc !== 'string' || typeof sealed.ciphertext !== 'string')
|
|
58
|
+
throw new Error('unsealed or malformed keeper reply refused');
|
|
59
|
+
const enc = base64Decode(sealed.enc);
|
|
60
|
+
if (enc.length !== 32)
|
|
61
|
+
throw new Error('invalid HPKE encapsulated key');
|
|
62
|
+
const ctx = await suite.createRecipientContext({ recipientKey: keys.privateKey, enc: enc.buffer, info: utf8('keykeeper:sealed-decryption-share:v1').buffer });
|
|
63
|
+
return new Uint8Array(await ctx.open(base64Decode(sealed.ciphertext).buffer, aad(hash, identifier, epoch, vs).buffer));
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** Hash the complete KEM content with unambiguous field lengths.
|
|
68
|
+
* @param ct - Ciphertext including its group element, nonce and authenticated ciphertext.
|
|
69
|
+
* @param identity - Exact associated-data identity bytes used during encryption.
|
|
70
|
+
*/
|
|
71
|
+
export function kemContentDigest(ct, identity) {
|
|
72
|
+
const parts = [ct.u, ct.nonce, ct.aeadCt, identity];
|
|
73
|
+
return sha256(concat(utf8('keykeeper:kem-content:v1'), ...parts.flatMap(p => [be64(p.length), p])));
|
|
74
|
+
}
|
|
75
|
+
/** Bind a content digest to the ciphertext epoch and client recipient.
|
|
76
|
+
* @param content - Complete 32-byte KEM content digest or SHA-256 IBE identity digest.
|
|
77
|
+
* @param epoch - Nonnegative canonical ciphertext key epoch.
|
|
78
|
+
* @param recipient - Supported suite and client X25519 public key.
|
|
79
|
+
*/
|
|
80
|
+
export function recipientPayloadDigest(content, epoch, recipient) {
|
|
81
|
+
const key = hexToBytes(recipient.public_key);
|
|
82
|
+
if (recipient.suite !== RECIPIENT_SUITE || content.length !== 32 || key.length !== 32 || key.every(b => b === 0))
|
|
83
|
+
throw new Error('invalid recipient binding');
|
|
84
|
+
return sha256(concat(utf8('keykeeper:recipient-bound-decryption:v1'), content, be64(epoch), new Uint8Array([0, 32, 0, 1, 0, 3]), key));
|
|
85
|
+
}
|
|
86
|
+
/** Prepare the digest and public session metadata before requesting authorization.
|
|
87
|
+
* @param content - Complete KEM content digest or SHA-256 IBE identity digest.
|
|
88
|
+
* @param epoch - Canonical ciphertext key epoch.
|
|
89
|
+
* @param client - Original client-held recipient, retained for opening and recovery.
|
|
90
|
+
*/
|
|
91
|
+
export function decryptionBinding(content, epoch, client) {
|
|
92
|
+
return { payloadDigest: recipientPayloadDigest(content, epoch, client.recipient), decryption: { recipient: client.recipient, ciphertext_epoch: epoch, content_digest: bytesToHex(content) } };
|
|
93
|
+
}
|
|
94
|
+
/** Check the token binding before transmitting; refuse old or substituted grants.
|
|
95
|
+
* @param chainId - Deployment chain identifier.
|
|
96
|
+
* @param slotId - Hex-encoded 32-byte slot identifier.
|
|
97
|
+
* @param action - Keeper operation named by the authorization.
|
|
98
|
+
* @param digest - Recipient-bound 32-byte operation payload digest.
|
|
99
|
+
* @param claimed - Request hash carried by the signed token; omission is refused.
|
|
100
|
+
*/
|
|
101
|
+
export function checkedDecryptionRequestHash(chainId, slotId, action, digest, claimed) {
|
|
102
|
+
const expected = requestHash(chainId, hexToBytes(slotId), action, digest);
|
|
103
|
+
const actual = claimed ? hexToBytes(claimed) : new Uint8Array();
|
|
104
|
+
if (actual.length !== 32 || !expected.every((b, i) => b === actual[i]))
|
|
105
|
+
throw new Error('token is not bound to this recipient and decryption request');
|
|
106
|
+
return expected;
|
|
107
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
/**
|
|
1
|
+
import type { Ciphertext } from '../crypto/kem.js';
|
|
2
|
+
/** Legacy input retained for explicit runtime refusal. */
|
|
3
3
|
export interface BlsPeer {
|
|
4
4
|
/** BLS threshold participant identifier. */
|
|
5
5
|
id: number;
|
|
6
6
|
/** Network peer identifier for that participant. */
|
|
7
7
|
peerId: string;
|
|
8
8
|
}
|
|
9
|
-
/**
|
|
9
|
+
/** Legacy input retained for explicit runtime refusal. */
|
|
10
10
|
export interface DecryptCustodyOpts {
|
|
11
11
|
/** Keeper HTTP base URL. */
|
|
12
12
|
nodeUrl: string;
|
|
@@ -31,14 +31,7 @@ export interface DecryptCustodyOpts {
|
|
|
31
31
|
/** Request identifier used to correlate or resume the operation. */
|
|
32
32
|
requestId?: string;
|
|
33
33
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Decrypt via the custody path: the node runs the whole k-of-n ceremony and
|
|
36
|
-
* returns the plaintext (one HTTP round-trip).
|
|
37
|
-
*
|
|
38
|
-
* @param opts - Keeper endpoint, JWT, ciphertext, associated data and decryption participants.
|
|
39
|
-
*/
|
|
40
|
-
export declare function decryptCustody(opts: DecryptCustodyOpts): Promise<Uint8Array>;
|
|
41
|
-
/** Keeper endpoints, JWT and ciphertext for combining partial decryptions in the caller. */
|
|
34
|
+
/** Legacy input retained for explicit runtime refusal. */
|
|
42
35
|
export interface ShardDecryptOpts {
|
|
43
36
|
/** Base URLs of at least k nodes to fetch partial decryptions from. */
|
|
44
37
|
nodeUrls: string[];
|
|
@@ -55,10 +48,11 @@ export interface ShardDecryptOpts {
|
|
|
55
48
|
/** Pairing-verify each share before combining (identifiable abort). Default false. */
|
|
56
49
|
verifyShares?: boolean;
|
|
57
50
|
}
|
|
58
|
-
/**
|
|
59
|
-
*
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
51
|
+
/** Server-side custody decryption is retired.
|
|
52
|
+
* @param opts - Historical request options, refused before any HTTP request.
|
|
53
|
+
*/
|
|
54
|
+
export declare function decryptCustody(opts: DecryptCustodyOpts): Promise<Uint8Array>;
|
|
55
|
+
/** Unsealed JWT share delivery is retired.
|
|
56
|
+
* @param opts - Historical request options, refused before any HTTP request.
|
|
63
57
|
*/
|
|
64
58
|
export declare function decryptWithShardDelivery(opts: ShardDecryptOpts): Promise<Uint8Array>;
|
|
@@ -1,82 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// - shard-delivery (POST /v1/shards/decrypt) - CLIENT fans out to k nodes and
|
|
4
|
-
// combines partial shares locally
|
|
5
|
-
//
|
|
6
|
-
// Ciphertext fields are base64 (standard, padded); key ids are bare hex; the
|
|
7
|
-
// `identity` AAD is sent as a UTF-8 string (the node uses its bytes as the AEAD
|
|
8
|
-
// AAD / audit key). Identity bytes that aren't valid UTF-8 are not supported on
|
|
9
|
-
// the wire.
|
|
10
|
-
import { combineDecryptShares } from '../crypto/kem.js';
|
|
11
|
-
import { base64Encode, base64Decode } from '../crypto/envelope.js';
|
|
12
|
-
import { bytesToHex as toHex } from '@noble/hashes/utils';
|
|
13
|
-
import { httpError } from '../errors.js';
|
|
14
|
-
const strip0x = (s) => (s.startsWith('0x') ? s.slice(2) : s);
|
|
15
|
-
const base = (u) => u.replace(/\/$/, '');
|
|
16
|
-
function ctToWire(ct) {
|
|
17
|
-
return { u: base64Encode(ct.u), nonce: base64Encode(ct.nonce), aead_ct: base64Encode(ct.aeadCt) };
|
|
18
|
-
}
|
|
19
|
-
async function nodePost(url, jwt, body, requestId) {
|
|
20
|
-
const headers = {
|
|
21
|
-
'Content-Type': 'application/json',
|
|
22
|
-
Authorization: `Bearer ${jwt}`,
|
|
23
|
-
};
|
|
24
|
-
if (requestId)
|
|
25
|
-
headers['x-request-id'] = requestId;
|
|
26
|
-
const res = await fetch(url, { method: 'POST', headers, body: JSON.stringify(body) });
|
|
27
|
-
if (!res.ok)
|
|
28
|
-
throw await httpError(res, url, `POST ${url}`);
|
|
29
|
-
return res.json();
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Decrypt via the custody path: the node runs the whole k-of-n ceremony and
|
|
33
|
-
* returns the plaintext (one HTTP round-trip).
|
|
34
|
-
*
|
|
35
|
-
* @param opts - Keeper endpoint, JWT, ciphertext, associated data and decryption participants.
|
|
1
|
+
/** Server-side custody decryption is retired.
|
|
2
|
+
* @param opts - Historical request options, refused before any HTTP request.
|
|
36
3
|
*/
|
|
37
4
|
export async function decryptCustody(opts) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
ciphertext: ctToWire(opts.ciphertext),
|
|
41
|
-
identity: new TextDecoder().decode(opts.identity),
|
|
42
|
-
decrypting_set: opts.decryptingSet,
|
|
43
|
-
bls_peers: opts.blsPeers.map(p => ({ id: p.id, peer_id: p.peerId })),
|
|
44
|
-
};
|
|
45
|
-
if (opts.userSignature)
|
|
46
|
-
body.user_signature = toHex(opts.userSignature);
|
|
47
|
-
if (opts.ciphertextEpoch !== undefined)
|
|
48
|
-
body.ciphertext_epoch = opts.ciphertextEpoch;
|
|
49
|
-
if (opts.targetKeykeeper)
|
|
50
|
-
body.target_keykeeper = strip0x(opts.targetKeykeeper);
|
|
51
|
-
const d = await nodePost(`${base(opts.nodeUrl)}/v1/decrypt`, opts.jwt, body, opts.requestId);
|
|
52
|
-
return base64Decode(d.plaintext);
|
|
53
|
-
}
|
|
54
|
-
async function partialDecrypt(nodeUrl, jwt, slotId, ct, identity, ciphertextEpoch) {
|
|
55
|
-
const body = {
|
|
56
|
-
key_slot_id: strip0x(slotId),
|
|
57
|
-
ciphertext: ctToWire(ct),
|
|
58
|
-
identity: new TextDecoder().decode(identity),
|
|
59
|
-
};
|
|
60
|
-
if (ciphertextEpoch !== undefined)
|
|
61
|
-
body.ciphertext_epoch = ciphertextEpoch;
|
|
62
|
-
const d = await nodePost(`${base(nodeUrl)}/v1/shards/decrypt`, jwt, body);
|
|
63
|
-
return {
|
|
64
|
-
id: Number(d.identifier),
|
|
65
|
-
decryptionShare: base64Decode(d.decryption_share),
|
|
66
|
-
verifyingShare: base64Decode(d.verifying_share),
|
|
67
|
-
};
|
|
5
|
+
void opts;
|
|
6
|
+
throw new Error('legacy decryption is retired; use recipient-bound decryptWithCommitteePartials');
|
|
68
7
|
}
|
|
69
|
-
/**
|
|
70
|
-
*
|
|
71
|
-
* node and combine the shares locally (the master key is never assembled).
|
|
72
|
-
*
|
|
73
|
-
* @param opts - Keeper endpoints, JWT, ciphertext and optional per-share verification.
|
|
8
|
+
/** Unsealed JWT share delivery is retired.
|
|
9
|
+
* @param opts - Historical request options, refused before any HTTP request.
|
|
74
10
|
*/
|
|
75
11
|
export async function decryptWithShardDelivery(opts) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const shares = await Promise.all(opts.nodeUrls.map(u => partialDecrypt(u, opts.jwt, opts.slotId, opts.ciphertext, opts.identity, opts.ciphertextEpoch)));
|
|
79
|
-
return combineDecryptShares(shares, opts.ciphertext, opts.identity, {
|
|
80
|
-
verify: opts.verifyShares ?? false,
|
|
81
|
-
});
|
|
12
|
+
void opts;
|
|
13
|
+
throw new Error('legacy decryption is retired; use recipient-bound decryptWithCommitteePartials');
|
|
82
14
|
}
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export { decryptCustody, decryptWithShardDelivery } from './client.js';
|
|
2
2
|
export type { DecryptCustodyOpts, ShardDecryptOpts, BlsPeer } from './client.js';
|
|
3
|
+
export { decryptWithSessionGrant, isStaleSessionRefusal } from './session.js';
|
|
4
|
+
export type { SessionDecryptItem, SessionDecryptOptions, SessionDecryptResult, SessionCombiner, SessionItemRefusal, SessionVerifierProofWire } from './session.js';
|
package/dist/decryption/index.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
// BLS threshold decryption over the node fleet (custody + shard-delivery).
|
|
2
2
|
export { decryptCustody, decryptWithShardDelivery } from './client.js';
|
|
3
|
+
// ADR-0090 D7/D8: per-message decrypts under a session grant (single or batch route).
|
|
4
|
+
export { decryptWithSessionGrant, isStaleSessionRefusal } from './session.js';
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { type KeeperExecutionApprover } from '../committee/keeperExecution.js';
|
|
2
|
+
import { type OperationReceipt } from '../committee/receipts.js';
|
|
3
|
+
import { type Ciphertext } from '../crypto/kem.js';
|
|
4
|
+
import { type ClientRecipient } from '../crypto/recipient.js';
|
|
5
|
+
import type { CompoundTokenWire } from '../committee/token.js';
|
|
6
|
+
import type { VerifierProof } from '../committee/client.js';
|
|
7
|
+
/** Recipient-bound authorization and independently authenticated keeper quorum for local decryption. */
|
|
8
|
+
export interface SealedRequestOpts {
|
|
9
|
+
/** Client-owned exact-body approval capability, required for paid contributions. */
|
|
10
|
+
approveExecution?: KeeperExecutionApprover;
|
|
11
|
+
/** Cancel the complete fan-out, including approvals and response bodies. */
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
/** Guarded HTTP transport; defaults to global fetch. */
|
|
14
|
+
fetchImpl?: typeof fetch;
|
|
15
|
+
/** Distinct keeper endpoints resolved from the assigned operator roster. */
|
|
16
|
+
nodeUrls: string[];
|
|
17
|
+
/** Deployment chain identifier included in the signed request binding. */
|
|
18
|
+
chainId: number | bigint;
|
|
19
|
+
/** Holder authorization bound to this content, recipient, slot, chain and action. */
|
|
20
|
+
committeeToken: CompoundTokenWire;
|
|
21
|
+
/** Original client-held recipient created before requesting authorization. */
|
|
22
|
+
recipient: ClientRecipient;
|
|
23
|
+
/** Canonical key epoch authorized for the ciphertext. */
|
|
24
|
+
ciphertextEpoch: number;
|
|
25
|
+
/** Authenticate this package against the slot's published commitment before use.
|
|
26
|
+
* Keys are keeper identifiers, values are 144-byte G2 || G1 verifying shares. */
|
|
27
|
+
verifyingShares: ReadonlyMap<number, Uint8Array>;
|
|
28
|
+
/** Required number of distinct authenticated and pairing-verified contributions. */
|
|
29
|
+
threshold: number;
|
|
30
|
+
/** Registry membership proofs for the token's anchored verifier snapshot. */
|
|
31
|
+
verifierProofs?: VerifierProof[];
|
|
32
|
+
/** Optional assigned operator address pinned by the operation policy. */
|
|
33
|
+
targetKeykeeper?: string;
|
|
34
|
+
/** Slot-owner signature over the canonical KEM request when required by slot policy. */
|
|
35
|
+
userSignature?: Uint8Array;
|
|
36
|
+
/** Original sealed credential evidence associated with the compound token. */
|
|
37
|
+
evidenceCt?: string;
|
|
38
|
+
/** Use the same id when producing the KEM owner signature. */
|
|
39
|
+
requestId?: string;
|
|
40
|
+
}
|
|
41
|
+
/** Locally opened and verified contribution. Clear its value after combining or discarding it. */
|
|
42
|
+
export interface SealedPartial {
|
|
43
|
+
/** Validated operation receipt when the keeper includes one. */
|
|
44
|
+
receipt?: OperationReceipt;
|
|
45
|
+
/** Distinct BLS participant identifier. */
|
|
46
|
+
identifier: number;
|
|
47
|
+
/** Sensitive unsealed contribution, held only on the client. */
|
|
48
|
+
value: Uint8Array;
|
|
49
|
+
/** Independently authenticated 144-byte G2 || G1 public verifying share. */
|
|
50
|
+
verifyingShare: Uint8Array;
|
|
51
|
+
/** Authorized canonical key epoch. */
|
|
52
|
+
epoch: number;
|
|
53
|
+
/** Endpoint from which this verified contribution was received. */
|
|
54
|
+
nodeUrl: string;
|
|
55
|
+
}
|
|
56
|
+
/** Collect the first threshold of distinct, locally opened and verified contributions.
|
|
57
|
+
* The whole fan-out is bounded to 15 seconds. Outstanding reads are cancelled at quorum;
|
|
58
|
+
* invalid, duplicate and late contributions are cleared. The caller owns returned values.
|
|
59
|
+
* @param opts - Original recipient-bound grant, trusted public shares and assigned endpoints.
|
|
60
|
+
* @param action - KEM decryption or IBE extraction; must match the signed token binding.
|
|
61
|
+
* @param identity - Original UTF-8 identity bytes bound by authorization.
|
|
62
|
+
* @param ciphertext - Complete KEM ciphertext, required for the decrypt action.
|
|
63
|
+
*/
|
|
64
|
+
export declare function collectSealedPartials(opts: SealedRequestOpts, action: 'decrypt' | 'ibe-extract', identity: Uint8Array, ciphertext?: Ciphertext): Promise<SealedPartial[]>;
|
|
65
|
+
/** Collect and combine recipient-sealed KEM contributions locally, then clear all partials.
|
|
66
|
+
* Servers receive only the authorized public recipient and never the recovered plaintext.
|
|
67
|
+
* @param opts - Recipient-bound quorum settings, complete ciphertext and original identity bytes.
|
|
68
|
+
*/
|
|
69
|
+
export declare function decryptWithCommitteePartials(opts: SealedRequestOpts & {
|
|
70
|
+
ciphertext: Ciphertext;
|
|
71
|
+
identity: Uint8Array;
|
|
72
|
+
}): Promise<Uint8Array>;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { evidenceBody } from '../committee/evidence.js';
|
|
2
|
+
import { withKeeperExecutionApprovals } from '../committee/keeperExecution.js';
|
|
3
|
+
import { readIbeReply } from '../committee/client.js';
|
|
4
|
+
import { decodeOperationReceipt } from '../committee/receipts.js';
|
|
5
|
+
import { withNetworkDeadline } from '../internal/network.js';
|
|
6
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
7
|
+
import { base64Decode, base64Encode } from '../crypto/envelope.js';
|
|
8
|
+
import { bytesToHex } from '../crypto/hex.js';
|
|
9
|
+
import { combineDecryptShares, decodeKemU, verifyDecryptShareAt } from '../crypto/kem.js';
|
|
10
|
+
import { ibeIdentityPoint, ibeVerifyShareAt } from '../crypto/ibe.js';
|
|
11
|
+
import { checkedDecryptionRequestHash, kemContentDigest, recipientPayloadDigest } from '../crypto/recipient.js';
|
|
12
|
+
import { httpError } from '../errors.js';
|
|
13
|
+
/** Collect the first threshold of distinct, locally opened and verified contributions.
|
|
14
|
+
* The whole fan-out is bounded to 15 seconds. Outstanding reads are cancelled at quorum;
|
|
15
|
+
* invalid, duplicate and late contributions are cleared. The caller owns returned values.
|
|
16
|
+
* @param opts - Original recipient-bound grant, trusted public shares and assigned endpoints.
|
|
17
|
+
* @param action - KEM decryption or IBE extraction; must match the signed token binding.
|
|
18
|
+
* @param identity - Original UTF-8 identity bytes bound by authorization.
|
|
19
|
+
* @param ciphertext - Complete KEM ciphertext, required for the decrypt action.
|
|
20
|
+
*/
|
|
21
|
+
export async function collectSealedPartials(opts, action, identity, ciphertext) {
|
|
22
|
+
return withNetworkDeadline(signal => collectWithinDeadline({ ...opts, signal }, action, identity, ciphertext), opts.signal);
|
|
23
|
+
}
|
|
24
|
+
async function collectWithinDeadline(opts, action, identity, ciphertext) {
|
|
25
|
+
if (!opts.recipient || !opts.verifyingShares || opts.nodeUrls.length === 0 || !Number.isInteger(opts.threshold) || opts.threshold < 1 || opts.threshold > opts.verifyingShares.size || opts.threshold > opts.nodeUrls.length)
|
|
26
|
+
throw new Error('recipient, authenticated verifying shares, keeper URLs and a valid threshold are required');
|
|
27
|
+
opts = { ...opts, nodeUrls: [...opts.nodeUrls], committeeToken: structuredClone(opts.committeeToken), verifyingShares: new Map([...opts.verifyingShares].map(([id, share]) => [id, share.slice()])) };
|
|
28
|
+
if (new Set(opts.nodeUrls.map(u => u.replace(/\/$/, ''))).size !== opts.nodeUrls.length)
|
|
29
|
+
throw new Error('duplicate sealed keeper endpoint');
|
|
30
|
+
opts.signal?.throwIfAborted();
|
|
31
|
+
const content = action === 'decrypt' ? kemContentDigest(ciphertext, identity) : sha256(identity);
|
|
32
|
+
const digest = recipientPayloadDigest(content, opts.ciphertextEpoch, opts.recipient.recipient);
|
|
33
|
+
const hash = checkedDecryptionRequestHash(opts.chainId, opts.committeeToken.slot_id, action, digest, opts.committeeToken.request_hash);
|
|
34
|
+
// Refuse lossy UTF-8 conversion: the request must bind the same identity bytes.
|
|
35
|
+
const textIdentity = new TextDecoder('utf-8', { fatal: true }).decode(identity);
|
|
36
|
+
// Decode U / hash Q_ID once per operation, not once per share.
|
|
37
|
+
const U = action === 'decrypt' ? decodeKemU(ciphertext.u) : undefined;
|
|
38
|
+
let qId;
|
|
39
|
+
const controller = new AbortController();
|
|
40
|
+
const requests = opts.nodeUrls.map(async (nodeUrl) => {
|
|
41
|
+
const url = `${nodeUrl.replace(/\/$/, '')}${action === 'decrypt' ? '/v1/committee/decrypt/partial' : '/v1/shards/ibe/extract'}`;
|
|
42
|
+
let body = {
|
|
43
|
+
committee_token: opts.committeeToken, identity: textIdentity,
|
|
44
|
+
recipient: opts.recipient.recipient, ciphertext_epoch: opts.ciphertextEpoch,
|
|
45
|
+
...(ciphertext ? { ciphertext: { u: base64Encode(ciphertext.u), nonce: base64Encode(ciphertext.nonce), aead_ct: base64Encode(ciphertext.aeadCt) } } : {}),
|
|
46
|
+
...evidenceBody(opts.committeeToken, opts.evidenceCt),
|
|
47
|
+
...(opts.targetKeykeeper ? { target_keykeeper: opts.targetKeykeeper.replace(/^0x/, '') } : {}),
|
|
48
|
+
...(opts.userSignature ? { user_signature: bytesToHex(opts.userSignature).replace(/^0x/, '') } : {}),
|
|
49
|
+
...(opts.verifierProofs ? { verifier_proofs: opts.verifierProofs.map(p => ({ verifier_index: p.verifierIndex, operator: p.operator.replace(/^0x/, ''), pubkey: p.pubkey.replace(/^0x/, ''), proof: p.proof.map(h => h.replace(/^0x/, '')) })) } : {}),
|
|
50
|
+
};
|
|
51
|
+
body = await withKeeperExecutionApprovals({ body, kind: action === 'decrypt' ? 'sealed-decrypt' : 'ibe-extract', nodeUrl, payment: opts.committeeToken.payment, approveExecution: opts.approveExecution, signal: opts.signal });
|
|
52
|
+
opts.signal?.throwIfAborted();
|
|
53
|
+
const res = await (opts.fetchImpl ?? fetch)(url, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(opts.requestId ? { 'x-request-id': opts.requestId } : {}) }, body: JSON.stringify(body), signal: AbortSignal.any([controller.signal, opts.signal]), ...(opts.committeeToken.payment ? { redirect: 'error', credentials: 'omit' } : {}) });
|
|
54
|
+
const text = await readIbeReply(res, opts.signal);
|
|
55
|
+
opts.signal?.throwIfAborted();
|
|
56
|
+
if (!res.ok)
|
|
57
|
+
throw await httpError(new Response(text, { status: res.status }), url, 'sealed partial');
|
|
58
|
+
const d = JSON.parse(text);
|
|
59
|
+
if (d.epoch !== opts.ciphertextEpoch || d.key_slot_id.replace(/^0x/, '').toLowerCase() !== opts.committeeToken.slot_id.replace(/^0x/, '').toLowerCase())
|
|
60
|
+
throw new Error('keeper returned another slot or epoch');
|
|
61
|
+
const vs = opts.verifyingShares.get(d.identifier);
|
|
62
|
+
const supplied = base64Decode(d.verifying_share);
|
|
63
|
+
if (!vs || vs.length !== 144 || supplied.length !== 144 || !vs.every((b, i) => b === supplied[i]))
|
|
64
|
+
throw new Error('keeper verifying share differs from authenticated package');
|
|
65
|
+
const value = await opts.recipient.open(d.sealed_share, hash, d.identifier, d.epoch, vs);
|
|
66
|
+
try {
|
|
67
|
+
if (action === 'decrypt') {
|
|
68
|
+
if (value.length !== 96 || !verifyDecryptShareAt({ id: d.identifier, decryptionShare: value, verifyingShare: vs }, U))
|
|
69
|
+
throw new Error('invalid KEM partial');
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
ibeVerifyShareAt(vs.slice(0, 96), qId ??= ibeIdentityPoint(identity), { identifier: d.identifier, value });
|
|
73
|
+
}
|
|
74
|
+
return { ...(decodeOperationReceipt(d) ? { receipt: decodeOperationReceipt(d) } : {}), identifier: d.identifier, value, verifyingShare: vs, epoch: d.epoch, nodeUrl };
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
value.fill(0);
|
|
78
|
+
throw e;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
const valid = new Map();
|
|
82
|
+
const errors = [];
|
|
83
|
+
// Return at the first threshold of verified, distinct replies. A slow or
|
|
84
|
+
// malicious keeper must not delay a successful operation. Wipe late arrivals.
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
let remaining = requests.length;
|
|
87
|
+
let done = false;
|
|
88
|
+
const cancelled = () => { if (done)
|
|
89
|
+
return; done = true; controller.abort(); for (const p of valid.values())
|
|
90
|
+
p.value.fill(0); reject(opts.signal?.reason); };
|
|
91
|
+
opts.signal?.addEventListener('abort', cancelled, { once: true });
|
|
92
|
+
const finish = () => {
|
|
93
|
+
if (valid.size >= opts.threshold) {
|
|
94
|
+
done = true;
|
|
95
|
+
opts.signal?.removeEventListener('abort', cancelled);
|
|
96
|
+
controller.abort();
|
|
97
|
+
resolve([...valid.values()]);
|
|
98
|
+
}
|
|
99
|
+
else if (remaining + valid.size < opts.threshold) {
|
|
100
|
+
done = true;
|
|
101
|
+
opts.signal?.removeEventListener('abort', cancelled);
|
|
102
|
+
controller.abort();
|
|
103
|
+
for (const p of valid.values())
|
|
104
|
+
p.value.fill(0);
|
|
105
|
+
reject(new Error(`sealed partial threshold not met (${valid.size}/${opts.threshold}): ${errors.join('; ')}`));
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
if (opts.signal?.aborted)
|
|
109
|
+
cancelled();
|
|
110
|
+
for (const request of requests)
|
|
111
|
+
void request.then(p => {
|
|
112
|
+
remaining--;
|
|
113
|
+
if (done) {
|
|
114
|
+
p.value.fill(0);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (valid.has(p.identifier))
|
|
118
|
+
p.value.fill(0);
|
|
119
|
+
else
|
|
120
|
+
valid.set(p.identifier, p);
|
|
121
|
+
finish();
|
|
122
|
+
}, error => {
|
|
123
|
+
remaining--;
|
|
124
|
+
if (done)
|
|
125
|
+
return;
|
|
126
|
+
errors.push(String(error));
|
|
127
|
+
finish();
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/** Collect and combine recipient-sealed KEM contributions locally, then clear all partials.
|
|
132
|
+
* Servers receive only the authorized public recipient and never the recovered plaintext.
|
|
133
|
+
* @param opts - Recipient-bound quorum settings, complete ciphertext and original identity bytes.
|
|
134
|
+
*/
|
|
135
|
+
export async function decryptWithCommitteePartials(opts) {
|
|
136
|
+
const partials = await collectSealedPartials(opts, 'decrypt', opts.identity, opts.ciphertext);
|
|
137
|
+
const shares = partials.map(p => ({ id: p.identifier, decryptionShare: p.value, verifyingShare: p.verifyingShare }));
|
|
138
|
+
// collectSealedPartials pairing-verified every share against its authenticated verifying share
|
|
139
|
+
// and this ciphertext's U; the combine's AEAD still authenticates the result.
|
|
140
|
+
try {
|
|
141
|
+
return combineDecryptShares(shares, opts.ciphertext, opts.identity, { verify: false });
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
for (const p of partials)
|
|
145
|
+
p.value.fill(0);
|
|
146
|
+
}
|
|
147
|
+
}
|