@t3foundry/tasra-sdk 0.0.0-stage → 0.3.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +148 -0
- package/LICENSE +202 -0
- package/NOTICE +94 -0
- package/README.md +137 -2
- package/SECURITY.md +115 -0
- package/dist/app/approvals.d.ts +50 -0
- package/dist/app/approvals.js +90 -0
- package/dist/app/authorization.d.ts +19 -0
- package/dist/app/authorization.js +20 -0
- package/dist/app/client.d.ts +231 -0
- package/dist/app/client.js +272 -0
- package/dist/app/creation.d.ts +73 -0
- package/dist/app/creation.js +77 -0
- package/dist/app/deployment.d.ts +36 -0
- package/dist/app/deployment.js +18 -0
- package/dist/app/identity.d.ts +211 -0
- package/dist/app/identity.js +287 -0
- package/dist/app/index.d.ts +22 -0
- package/dist/app/index.js +12 -0
- package/dist/app/manifest.d.ts +74 -0
- package/dist/app/manifest.js +120 -0
- package/dist/app/node.d.ts +30 -0
- package/dist/app/node.js +159 -0
- package/dist/app/ready-slot.d.ts +157 -0
- package/dist/app/ready-slot.js +199 -0
- package/dist/app/tasra-client.d.ts +125 -0
- package/dist/app/tasra-client.js +79 -0
- package/dist/app/viem.d.ts +10 -0
- package/dist/app/viem.js +32 -0
- package/dist/app/wallet.d.ts +126 -0
- package/dist/app/wallet.js +137 -0
- package/dist/auth/dpop.d.ts +86 -0
- package/dist/auth/dpop.js +142 -0
- package/dist/auth/holderProof.d.ts +102 -0
- package/dist/auth/holderProof.js +161 -0
- package/dist/auth/identityScope.d.ts +19 -0
- package/dist/auth/identityScope.js +82 -0
- package/dist/auth/index.d.ts +11 -0
- package/dist/auth/index.js +13 -0
- package/dist/auth/oid4vp.d.ts +230 -0
- package/dist/auth/oid4vp.js +780 -0
- package/dist/auth/rego/wasm/PINS.json +11 -0
- package/dist/auth/rego/wasm/keykeeper_rego_wasm.d.ts +76 -0
- package/dist/auth/rego/wasm/keykeeper_rego_wasm.js +379 -0
- package/dist/auth/rego/wasm/keykeeper_rego_wasm_bg.wasm +0 -0
- package/dist/auth/rego.d.ts +192 -0
- package/dist/auth/rego.js +492 -0
- package/dist/auth/verifier.d.ts +166 -0
- package/dist/auth/verifier.js +211 -0
- package/dist/chain/abis/accountantSetRegistry.d.ts +441 -0
- package/dist/chain/abis/accountantSetRegistry.js +574 -0
- package/dist/chain/abis/accountantSlashing.d.ts +2071 -0
- package/dist/chain/abis/accountantSlashing.js +2670 -0
- package/dist/chain/abis/bondingCurve.d.ts +398 -0
- package/dist/chain/abis/bondingCurve.js +521 -0
- package/dist/chain/abis/equivocationSlasher.d.ts +292 -0
- package/dist/chain/abis/equivocationSlasher.js +379 -0
- package/dist/chain/abis/fixedTasraPriceOracle.d.ts +52 -0
- package/dist/chain/abis/fixedTasraPriceOracle.js +72 -0
- package/dist/chain/abis/index.d.ts +12801 -0
- package/dist/chain/abis/index.js +64 -0
- package/dist/chain/abis/keeperShareRegistry.d.ts +519 -0
- package/dist/chain/abis/keeperShareRegistry.js +675 -0
- package/dist/chain/abis/keyRegistry.d.ts +2421 -0
- package/dist/chain/abis/keyRegistry.js +3114 -0
- package/dist/chain/abis/livenessRegistry.d.ts +402 -0
- package/dist/chain/abis/livenessRegistry.js +524 -0
- package/dist/chain/abis/mockEurc.d.ts +210 -0
- package/dist/chain/abis/mockEurc.js +276 -0
- package/dist/chain/abis/nodeRegistry.d.ts +1630 -0
- package/dist/chain/abis/nodeRegistry.js +2113 -0
- package/dist/chain/abis/platformExecutor.d.ts +328 -0
- package/dist/chain/abis/platformExecutor.js +429 -0
- package/dist/chain/abis/prevrandaoSaltBeacon.d.ts +16 -0
- package/dist/chain/abis/prevrandaoSaltBeacon.js +24 -0
- package/dist/chain/abis/serviceRegistry.d.ts +556 -0
- package/dist/chain/abis/serviceRegistry.js +719 -0
- package/dist/chain/abis/settlement.d.ts +1624 -0
- package/dist/chain/abis/settlement.js +2096 -0
- package/dist/chain/abis/tasraSwapRouter.d.ts +92 -0
- package/dist/chain/abis/tasraSwapRouter.js +122 -0
- package/dist/chain/abis/tasraToken.d.ts +455 -0
- package/dist/chain/abis/tasraToken.js +599 -0
- package/dist/chain/abis/tasraVestingVault.d.ts +165 -0
- package/dist/chain/abis/tasraVestingVault.js +215 -0
- package/dist/chain/abis/thresholdRandomBeacon.d.ts +378 -0
- package/dist/chain/abis/thresholdRandomBeacon.js +494 -0
- package/dist/chain/abis/treasury.d.ts +301 -0
- package/dist/chain/abis/treasury.js +390 -0
- package/dist/chain/abis/verifierSetRegistry.d.ts +445 -0
- package/dist/chain/abis/verifierSetRegistry.js +579 -0
- package/dist/chain/client.d.ts +212 -0
- package/dist/chain/client.js +576 -0
- package/dist/chain/commitmentRecovery.d.ts +21 -0
- package/dist/chain/commitmentRecovery.js +44 -0
- package/dist/chain/committeeClient.d.ts +102 -0
- package/dist/chain/committeeClient.js +137 -0
- package/dist/chain/deployments.d.ts +56 -0
- package/dist/chain/deployments.js +269 -0
- package/dist/chain/discovery.d.ts +49 -0
- package/dist/chain/discovery.js +107 -0
- package/dist/chain/events.d.ts +58 -0
- package/dist/chain/events.js +125 -0
- package/dist/chain/format.d.ts +25 -0
- package/dist/chain/format.js +51 -0
- package/dist/chain/index.d.ts +41 -0
- package/dist/chain/index.js +56 -0
- package/dist/chain/manifest.d.ts +165 -0
- package/dist/chain/manifest.js +132 -0
- package/dist/chain/network-profiles.json +75 -0
- package/dist/chain/networks.d.ts +73 -0
- package/dist/chain/networks.js +62 -0
- package/dist/chain/node.d.ts +44 -0
- package/dist/chain/node.js +176 -0
- package/dist/chain/offchain.d.ts +149 -0
- package/dist/chain/offchain.js +73 -0
- package/dist/chain/provisionRule.d.ts +164 -0
- package/dist/chain/provisionRule.js +212 -0
- package/dist/chain/registeredAgent.d.ts +64 -0
- package/dist/chain/registeredAgent.js +140 -0
- package/dist/chain/registeredOperation.d.ts +49 -0
- package/dist/chain/registeredOperation.js +136 -0
- package/dist/chain/registeredRelay.d.ts +206 -0
- package/dist/chain/registeredRelay.js +312 -0
- package/dist/chain/revertReason.d.ts +14 -0
- package/dist/chain/revertReason.js +62 -0
- package/dist/chain/ruleAmendment.d.ts +80 -0
- package/dist/chain/ruleAmendment.js +195 -0
- package/dist/chain/ruleAmendmentCore.d.ts +129 -0
- package/dist/chain/ruleAmendmentCore.js +152 -0
- package/dist/chain/ruleInput.d.ts +10 -0
- package/dist/chain/ruleInput.js +13 -0
- package/dist/chain/serviceIdentity.d.ts +176 -0
- package/dist/chain/serviceIdentity.js +237 -0
- package/dist/chain/serviceProfiles.d.ts +39 -0
- package/dist/chain/serviceProfiles.js +74 -0
- package/dist/chain/services.d.ts +141 -0
- package/dist/chain/services.js +79 -0
- package/dist/chain/slotClient.d.ts +55 -0
- package/dist/chain/slotClient.js +62 -0
- package/dist/chain/slotSeed.d.ts +31 -0
- package/dist/chain/slotSeed.js +98 -0
- package/dist/chain/write.d.ts +431 -0
- package/dist/chain/write.js +958 -0
- package/dist/client/client.d.ts +112 -0
- package/dist/client/client.js +111 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +2 -0
- package/dist/client/session.d.ts +67 -0
- package/dist/client/session.js +238 -0
- package/dist/committee/client.d.ts +284 -0
- package/dist/committee/client.js +351 -0
- package/dist/committee/dual-sign.d.ts +110 -0
- package/dist/committee/dual-sign.js +202 -0
- package/dist/committee/ecdsa.d.ts +36 -0
- package/dist/committee/ecdsa.js +45 -0
- package/dist/committee/extraction.d.ts +82 -0
- package/dist/committee/extraction.js +96 -0
- package/dist/committee/index.d.ts +19 -0
- package/dist/committee/index.js +13 -0
- package/dist/committee/oauth.d.ts +51 -0
- package/dist/committee/oauth.js +90 -0
- package/dist/committee/receipts.d.ts +51 -0
- package/dist/committee/receipts.js +59 -0
- package/dist/committee/request.d.ts +264 -0
- package/dist/committee/request.js +305 -0
- package/dist/committee/token.d.ts +274 -0
- package/dist/committee/token.js +418 -0
- package/dist/crypto/constants.d.ts +10 -0
- package/dist/crypto/constants.js +32 -0
- package/dist/crypto/detect.d.ts +20 -0
- package/dist/crypto/detect.js +44 -0
- package/dist/crypto/envelope.d.ts +60 -0
- package/dist/crypto/envelope.js +243 -0
- package/dist/crypto/frost.d.ts +49 -0
- package/dist/crypto/frost.js +152 -0
- package/dist/crypto/hex.d.ts +7 -0
- package/dist/crypto/hex.js +24 -0
- package/dist/crypto/ibe-blob.d.ts +114 -0
- package/dist/crypto/ibe-blob.js +208 -0
- package/dist/crypto/ibe.d.ts +88 -0
- package/dist/crypto/ibe.js +262 -0
- package/dist/crypto/index.d.ts +12 -0
- package/dist/crypto/index.js +12 -0
- package/dist/crypto/kem.d.ts +55 -0
- package/dist/crypto/kem.js +241 -0
- package/dist/decryption/client.d.ts +64 -0
- package/dist/decryption/client.js +82 -0
- package/dist/decryption/index.d.ts +2 -0
- package/dist/decryption/index.js +2 -0
- package/dist/errors.d.ts +124 -0
- package/dist/errors.js +167 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +98 -0
- package/dist/keys/index.d.ts +1 -0
- package/dist/keys/index.js +4 -0
- package/dist/keys/node-client.d.ts +27 -0
- package/dist/keys/node-client.js +97 -0
- package/dist/oid4vp/binding.d.ts +76 -0
- package/dist/oid4vp/binding.js +118 -0
- package/dist/oid4vp/did-web.d.ts +49 -0
- package/dist/oid4vp/did-web.js +57 -0
- package/dist/oid4vp/index.d.ts +9 -0
- package/dist/oid4vp/index.js +10 -0
- package/dist/oid4vp/jose.d.ts +166 -0
- package/dist/oid4vp/jose.js +242 -0
- package/dist/oid4vp/jwe.d.ts +30 -0
- package/dist/oid4vp/jwe.js +93 -0
- package/dist/oid4vp/oid4vci.d.ts +84 -0
- package/dist/oid4vp/oid4vci.js +85 -0
- package/dist/oid4vp/request-object.d.ts +109 -0
- package/dist/oid4vp/request-object.js +96 -0
- package/dist/oid4vp/sd-jwt.d.ts +177 -0
- package/dist/oid4vp/sd-jwt.js +208 -0
- package/dist/oid4vp/verifier-agent.d.ts +161 -0
- package/dist/oid4vp/verifier-agent.js +132 -0
- package/dist/oid4vp/wallet.d.ts +116 -0
- package/dist/oid4vp/wallet.js +108 -0
- package/dist/recipient/store.d.ts +67 -0
- package/dist/recipient/store.js +98 -0
- package/dist/signing/ecdsa.d.ts +49 -0
- package/dist/signing/ecdsa.js +79 -0
- package/dist/signing/frost.d.ts +88 -0
- package/dist/signing/frost.js +145 -0
- package/dist/signing/index.d.ts +4 -0
- package/dist/signing/index.js +3 -0
- package/dist/slots/faucet.d.ts +23 -0
- package/dist/slots/faucet.js +35 -0
- package/dist/slots/index.d.ts +2 -0
- package/dist/slots/index.js +4 -0
- package/dist/verifier-agent/index.d.ts +241 -0
- package/dist/verifier-agent/index.js +411 -0
- package/docs/DEVELOPER-EXPERIENCE.md +62 -0
- package/docs/README.md +33 -0
- package/docs/access-control.md +76 -0
- package/docs/ai-development.md +292 -0
- package/docs/api.md +338 -0
- package/docs/application-api.md +335 -0
- package/docs/architecture.md +65 -0
- package/docs/basics.md +48 -0
- package/docs/capabilities.md +86 -0
- package/docs/chain.md +153 -0
- package/docs/compatibility.md +36 -0
- package/docs/consumer-migrations.md +80 -0
- package/docs/create-slot.md +104 -0
- package/docs/document-signing.md +243 -0
- package/docs/encrypted-notes.md +129 -0
- package/docs/encryption.md +100 -0
- package/docs/errors.md +76 -0
- package/docs/flows.md +289 -0
- package/docs/fuji.md +127 -0
- package/docs/funding.md +126 -0
- package/docs/getting-started.md +94 -0
- package/docs/glossary.md +44 -0
- package/docs/installation.md +132 -0
- package/docs/manage-slot.md +95 -0
- package/docs/native-approvals.md +124 -0
- package/docs/prerequisites.md +137 -0
- package/docs/read-slot.md +41 -0
- package/docs/reference/README.md +30 -0
- package/docs/reference/app-node.md +102 -0
- package/docs/reference/app.md +1737 -0
- package/docs/reference/chain-node.md +159 -0
- package/docs/reference/chain.md +3609 -0
- package/docs/reference/committee.md +2176 -0
- package/docs/reference/main.md +4068 -0
- package/docs/reference/oid4vp.md +2547 -0
- package/docs/reference/verifier-agent.md +498 -0
- package/docs/shared-account.md +168 -0
- package/docs/signing-and-encryption.md +52 -0
- package/docs/signing.md +172 -0
- package/examples/committee-slot.ts +16 -0
- package/examples/connect-fuji.ts +41 -0
- package/examples/connect-network.ts +14 -0
- package/examples/credential-approvals.ts +46 -0
- package/examples/document-signing/README.md +59 -0
- package/examples/document-signing/backend.ts +132 -0
- package/examples/document-signing/build.mjs +19 -0
- package/examples/document-signing/config.ts +34 -0
- package/examples/document-signing/gitignore.template +9 -0
- package/examples/document-signing/model.ts +140 -0
- package/examples/document-signing/network.ts +58 -0
- package/examples/document-signing/package.json +25 -0
- package/examples/document-signing/public/index.html +27 -0
- package/examples/document-signing/public/sample.pdf +31 -0
- package/examples/document-signing/public/styles.css +531 -0
- package/examples/document-signing/server.ts +358 -0
- package/examples/document-signing/setup.ts +111 -0
- package/examples/document-signing/store.ts +105 -0
- package/examples/document-signing/tsconfig.json +24 -0
- package/examples/document-signing/verify.ts +24 -0
- package/examples/document-signing/web/app.ts +312 -0
- package/examples/document-signing/web/preview.ts +67 -0
- package/examples/document-signing/web/wallet.ts +146 -0
- package/examples/document-signing/workflow.test.ts +228 -0
- package/examples/document-signing/workflow.ts +241 -0
- package/examples/document-signing.ts +96 -0
- package/examples/encrypted-notes.ts +86 -0
- package/examples/ethers-signer.ts +204 -0
- package/examples/fund-slot.ts +73 -0
- package/examples/getting-started.ts +25 -0
- package/examples/learning-authorize.ts +23 -0
- package/examples/learning-cancel-slot.ts +11 -0
- package/examples/learning-context.ts +28 -0
- package/examples/learning-create-slot.ts +25 -0
- package/examples/learning-read-slot.ts +8 -0
- package/examples/learning-renew-slot.ts +12 -0
- package/examples/learning-sign.ts +8 -0
- package/examples/live-config.ts +71 -0
- package/examples/minimal.ts +6 -0
- package/examples/native-approvals.ts +57 -0
- package/examples/network.ts +58 -0
- package/examples/offline.ts +16 -0
- package/examples/personal-vault.ts +20 -0
- package/examples/provision-slot.ts +47 -0
- package/examples/shared-account.ts +46 -0
- package/examples/slot-address.ts +13 -0
- package/examples/slot-creation.ts +9 -0
- package/examples/tutorial-negative.ts +26 -0
- package/examples/tutorial-support.ts +76 -0
- package/examples/viem-account.ts +203 -0
- package/package.json +303 -4
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// GroupEnvelope - self-describing wire format for threshold-encrypted chat messages.
|
|
2
|
+
// Wire-compatible with the reference envelope implementation.
|
|
3
|
+
//
|
|
4
|
+
// v0x01 layout (no epoch):
|
|
5
|
+
// [version:1][slot_id:32][identity_len:2 LE][identity:L]
|
|
6
|
+
// [u:96][nonce:12][aead_ct_len:4 LE][aead_ct:M]
|
|
7
|
+
//
|
|
8
|
+
// v0x02 layout (embedded epoch):
|
|
9
|
+
// [version:1][slot_id:32][epoch:8 LE][identity_len:2 LE][identity:L]
|
|
10
|
+
// [u:96][nonce:12][aead_ct_len:4 LE][aead_ct:M]
|
|
11
|
+
import { bls12_381 } from '@noble/curves/bls12-381';
|
|
12
|
+
import { COMPRESSED_G2_LEN, ENVELOPE_VERSION_V1, ENVELOPE_VERSION_V2, MAX_AEAD_CT_LEN, MAX_IDENTITY_LEN, NONCE_LEN, SLOT_ID_LEN, } from './constants.js';
|
|
13
|
+
import { assembleKey, decryptWithMasterKey, encrypt as kemEncrypt, } from './kem.js';
|
|
14
|
+
// Serialize
|
|
15
|
+
/** Largest epoch a v0x02 envelope can carry: the field is a signed 64-bit integer. */
|
|
16
|
+
const MAX_EPOCH = (1n << 63n) - 1n;
|
|
17
|
+
/**
|
|
18
|
+
* Serialize an envelope using the version selected by its epoch. Reject invalid epoch and oversized fields.
|
|
19
|
+
*
|
|
20
|
+
* @param env - Envelope to serialize, including its slot, ciphertext and optional epoch.
|
|
21
|
+
*/
|
|
22
|
+
export function toBytes(env) {
|
|
23
|
+
const { slotId, identity, ciphertext, epoch } = env;
|
|
24
|
+
const idLen = identity.length;
|
|
25
|
+
const ctLen = ciphertext.aeadCt.length;
|
|
26
|
+
// Past 2^63 - 1 the epoch would be written negative, an envelope both parsers refuse.
|
|
27
|
+
if (epoch !== null && (epoch < 0n || epoch > MAX_EPOCH)) {
|
|
28
|
+
throw new Error(`toBytes: epoch ${epoch} does not fit a signed 64-bit integer`);
|
|
29
|
+
}
|
|
30
|
+
// A clamped length prefix in front of the full bytes is an envelope nobody can parse. Refuse
|
|
31
|
+
// instead; `encryptEnvelope` never produces one this large.
|
|
32
|
+
if (idLen > MAX_IDENTITY_LEN) {
|
|
33
|
+
throw new Error(`toBytes: identity length ${idLen} exceeds cap ${MAX_IDENTITY_LEN}`);
|
|
34
|
+
}
|
|
35
|
+
if (ctLen > MAX_AEAD_CT_LEN) {
|
|
36
|
+
throw new Error(`toBytes: aead_ct length ${ctLen} exceeds cap ${MAX_AEAD_CT_LEN}`);
|
|
37
|
+
}
|
|
38
|
+
const headerLen = epoch !== null
|
|
39
|
+
? 1 + SLOT_ID_LEN + 8 + 2 // v2: version + slot_id + epoch + id_len
|
|
40
|
+
: 1 + SLOT_ID_LEN + 2; // v1: version + slot_id + id_len
|
|
41
|
+
const total = headerLen + idLen + COMPRESSED_G2_LEN + NONCE_LEN + 4 + ctLen;
|
|
42
|
+
const out = new Uint8Array(total);
|
|
43
|
+
const view = new DataView(out.buffer);
|
|
44
|
+
let cur = 0;
|
|
45
|
+
if (epoch !== null) {
|
|
46
|
+
out[cur++] = ENVELOPE_VERSION_V2;
|
|
47
|
+
out.set(slotId, cur);
|
|
48
|
+
cur += SLOT_ID_LEN;
|
|
49
|
+
view.setBigInt64(cur, epoch, true);
|
|
50
|
+
cur += 8;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
out[cur++] = ENVELOPE_VERSION_V1;
|
|
54
|
+
out.set(slotId, cur);
|
|
55
|
+
cur += SLOT_ID_LEN;
|
|
56
|
+
}
|
|
57
|
+
view.setUint16(cur, idLen, true);
|
|
58
|
+
cur += 2;
|
|
59
|
+
out.set(identity, cur);
|
|
60
|
+
cur += idLen;
|
|
61
|
+
out.set(ciphertext.u, cur);
|
|
62
|
+
cur += COMPRESSED_G2_LEN;
|
|
63
|
+
out.set(ciphertext.nonce, cur);
|
|
64
|
+
cur += NONCE_LEN;
|
|
65
|
+
view.setUint32(cur, ctLen, true);
|
|
66
|
+
cur += 4;
|
|
67
|
+
out.set(ciphertext.aeadCt, cur);
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
// Parse
|
|
71
|
+
/**
|
|
72
|
+
* Parse a supported binary envelope and validate its version, field lengths and boundaries.
|
|
73
|
+
*
|
|
74
|
+
* @param bytes - Complete binary envelope to parse.
|
|
75
|
+
*/
|
|
76
|
+
export function fromBytes(bytes) {
|
|
77
|
+
if (bytes.length === 0) {
|
|
78
|
+
throw new Error('fromBytes: empty input');
|
|
79
|
+
}
|
|
80
|
+
switch (bytes[0]) {
|
|
81
|
+
case ENVELOPE_VERSION_V1:
|
|
82
|
+
return fromBytesV1(bytes);
|
|
83
|
+
case ENVELOPE_VERSION_V2:
|
|
84
|
+
return fromBytesV2(bytes);
|
|
85
|
+
default:
|
|
86
|
+
throw new Error(`fromBytes: unknown version byte 0x${bytes[0].toString(16)}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function fromBytesV1(bytes) {
|
|
90
|
+
const minLen = 1 + SLOT_ID_LEN + 2 + COMPRESSED_G2_LEN + NONCE_LEN + 4;
|
|
91
|
+
if (bytes.length < minLen) {
|
|
92
|
+
throw new Error('fromBytes(v1): truncated header');
|
|
93
|
+
}
|
|
94
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
95
|
+
let cur = 1; // skip version byte
|
|
96
|
+
const slotId = bytes.slice(cur, cur + SLOT_ID_LEN);
|
|
97
|
+
cur += SLOT_ID_LEN;
|
|
98
|
+
const idLen = view.getUint16(cur, true);
|
|
99
|
+
cur += 2;
|
|
100
|
+
if (idLen > MAX_IDENTITY_LEN) {
|
|
101
|
+
throw new Error(`fromBytes(v1): identity_len ${idLen} exceeds cap ${MAX_IDENTITY_LEN}`);
|
|
102
|
+
}
|
|
103
|
+
if (bytes.length < cur + idLen + COMPRESSED_G2_LEN + NONCE_LEN + 4) {
|
|
104
|
+
throw new Error('fromBytes(v1): truncated identity/ciphertext');
|
|
105
|
+
}
|
|
106
|
+
const identity = bytes.slice(cur, cur + idLen);
|
|
107
|
+
cur += idLen;
|
|
108
|
+
const u = bytes.slice(cur, cur + COMPRESSED_G2_LEN);
|
|
109
|
+
cur += COMPRESSED_G2_LEN;
|
|
110
|
+
const nonce = bytes.slice(cur, cur + NONCE_LEN);
|
|
111
|
+
cur += NONCE_LEN;
|
|
112
|
+
const ctLen = view.getUint32(cur, true);
|
|
113
|
+
cur += 4;
|
|
114
|
+
if (ctLen > MAX_AEAD_CT_LEN) {
|
|
115
|
+
throw new Error(`fromBytes(v1): aead_ct_len ${ctLen} exceeds cap ${MAX_AEAD_CT_LEN}`);
|
|
116
|
+
}
|
|
117
|
+
if (bytes.length !== cur + ctLen) {
|
|
118
|
+
throw new Error('fromBytes(v1): trailing bytes or truncated aead_ct');
|
|
119
|
+
}
|
|
120
|
+
const aeadCt = bytes.slice(cur, cur + ctLen);
|
|
121
|
+
return { slotId, identity, ciphertext: { u, nonce, aeadCt }, epoch: null };
|
|
122
|
+
}
|
|
123
|
+
function fromBytesV2(bytes) {
|
|
124
|
+
const minLen = 1 + SLOT_ID_LEN + 8 + 2 + COMPRESSED_G2_LEN + NONCE_LEN + 4;
|
|
125
|
+
if (bytes.length < minLen) {
|
|
126
|
+
throw new Error('fromBytes(v2): truncated header');
|
|
127
|
+
}
|
|
128
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
129
|
+
let cur = 1;
|
|
130
|
+
const slotId = bytes.slice(cur, cur + SLOT_ID_LEN);
|
|
131
|
+
cur += SLOT_ID_LEN;
|
|
132
|
+
const epoch = view.getBigInt64(cur, true);
|
|
133
|
+
cur += 8;
|
|
134
|
+
if (epoch < 0n) {
|
|
135
|
+
throw new Error(`fromBytes(v2): epoch ${epoch} is negative`);
|
|
136
|
+
}
|
|
137
|
+
const idLen = view.getUint16(cur, true);
|
|
138
|
+
cur += 2;
|
|
139
|
+
if (idLen > MAX_IDENTITY_LEN) {
|
|
140
|
+
throw new Error(`fromBytes(v2): identity_len ${idLen} exceeds cap ${MAX_IDENTITY_LEN}`);
|
|
141
|
+
}
|
|
142
|
+
if (bytes.length < cur + idLen + COMPRESSED_G2_LEN + NONCE_LEN + 4) {
|
|
143
|
+
throw new Error('fromBytes(v2): truncated identity/ciphertext');
|
|
144
|
+
}
|
|
145
|
+
const identity = bytes.slice(cur, cur + idLen);
|
|
146
|
+
cur += idLen;
|
|
147
|
+
const u = bytes.slice(cur, cur + COMPRESSED_G2_LEN);
|
|
148
|
+
cur += COMPRESSED_G2_LEN;
|
|
149
|
+
const nonce = bytes.slice(cur, cur + NONCE_LEN);
|
|
150
|
+
cur += NONCE_LEN;
|
|
151
|
+
const ctLen = view.getUint32(cur, true);
|
|
152
|
+
cur += 4;
|
|
153
|
+
if (ctLen > MAX_AEAD_CT_LEN) {
|
|
154
|
+
throw new Error(`fromBytes(v2): aead_ct_len ${ctLen} exceeds cap ${MAX_AEAD_CT_LEN}`);
|
|
155
|
+
}
|
|
156
|
+
if (bytes.length !== cur + ctLen) {
|
|
157
|
+
throw new Error('fromBytes(v2): trailing bytes or truncated aead_ct');
|
|
158
|
+
}
|
|
159
|
+
const aeadCt = bytes.slice(cur, cur + ctLen);
|
|
160
|
+
return { slotId, identity, ciphertext: { u, nonce, aeadCt }, epoch };
|
|
161
|
+
}
|
|
162
|
+
// High-level helpers
|
|
163
|
+
/**
|
|
164
|
+
* Encrypt `plaintext` to a slot's group key - ChaCha20-Poly1305 under a BLS12-381
|
|
165
|
+
* G2 ElGamal KEM. Local and synchronous: it needs only the slot's **public** key,
|
|
166
|
+
* so no JWT, no node round-trip, and no assembled secret.
|
|
167
|
+
*
|
|
168
|
+
* @param slotId the 32-byte slot id (raw bytes, not hex)
|
|
169
|
+
* @param mpkBytes the slot's 96-byte compressed G2 group public key, as served by
|
|
170
|
+
* `GET /v1/keys/{slot}/public` (see `fetchMpk`)
|
|
171
|
+
* @param identity additional authenticated data bound into the AEAD. Conventionally
|
|
172
|
+
* the slot id itself; the managed session defaults to exactly that.
|
|
173
|
+
* @param plaintext the bytes to encrypt
|
|
174
|
+
* @param epoch the current slot epoch, producing a v0x02 envelope; `null` produces
|
|
175
|
+
* a legacy v0x01 envelope with no epoch binding
|
|
176
|
+
* @returns the envelope - pass through `toBytes()` then `buildTasraText()` for
|
|
177
|
+
* the opaque `[KK]<base64>` wire form
|
|
178
|
+
* @throws {Error} if `slotId` is not 32 bytes, `identity` exceeds its cap, `plaintext`
|
|
179
|
+
* exceeds {@link MAX_PLAINTEXT_LEN}, or `epoch` is negative or above 2^63 - 1
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* const {mpkBytes, epoch} = await fetchMpk(nodeUrl, slotHex)
|
|
184
|
+
* const env = encryptEnvelope(hexToBytes(slotHex), mpkBytes, hexToBytes(slotHex), bytes, BigInt(epoch))
|
|
185
|
+
* const wire = buildTasraText(toBytes(env)) // hand to ANY transport
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
export function encryptEnvelope(slotId, mpkBytes, identity, plaintext, epoch = null) {
|
|
189
|
+
if (slotId.length !== SLOT_ID_LEN) {
|
|
190
|
+
throw new Error(`encryptEnvelope: slot_id must be ${SLOT_ID_LEN} bytes`);
|
|
191
|
+
}
|
|
192
|
+
if (identity.length > MAX_IDENTITY_LEN) {
|
|
193
|
+
throw new Error(`encryptEnvelope: identity exceeds cap ${MAX_IDENTITY_LEN}`);
|
|
194
|
+
}
|
|
195
|
+
if (epoch !== null && (epoch < 0n || epoch > MAX_EPOCH)) {
|
|
196
|
+
throw new Error('encryptEnvelope: epoch must be non-negative and fit a signed 64-bit integer');
|
|
197
|
+
}
|
|
198
|
+
// As in the reference envelope implementation: the AEAD adds a 16-byte tag, and a ciphertext past the cap would never
|
|
199
|
+
// serialise. Fail before encrypting; chunk larger payloads above this layer.
|
|
200
|
+
if (plaintext.length > MAX_PLAINTEXT_LEN) {
|
|
201
|
+
throw new Error(`encryptEnvelope: plaintext exceeds cap ${MAX_PLAINTEXT_LEN} bytes`);
|
|
202
|
+
}
|
|
203
|
+
const ciphertext = kemEncrypt(mpkBytes, identity, plaintext);
|
|
204
|
+
return { slotId, identity, ciphertext, epoch };
|
|
205
|
+
}
|
|
206
|
+
// Decrypt a GroupEnvelope using an assembled master secret key.
|
|
207
|
+
// mskBytes: 32-byte LE scalar (output of assembleKey).
|
|
208
|
+
export function decryptEnvelope(env, mskBytes) {
|
|
209
|
+
return decryptWithMasterKey(mskBytes, env.ciphertext, env.identity);
|
|
210
|
+
}
|
|
211
|
+
// Convenience: parse base64-encoded envelope bytes then decrypt.
|
|
212
|
+
export function decryptEnvelopeBase64(envelopeBase64, mskBytes) {
|
|
213
|
+
const bytes = base64Decode(envelopeBase64);
|
|
214
|
+
const env = fromBytes(bytes);
|
|
215
|
+
return decryptEnvelope(env, mskBytes);
|
|
216
|
+
}
|
|
217
|
+
// Base64 helpers
|
|
218
|
+
// Bytes per `String.fromCharCode` call. Passing every byte as a separate argument
|
|
219
|
+
// (`fromCharCode(...bytes)`) overflows the call stack once the input passes ~124 KiB on
|
|
220
|
+
// Node 24 (engines differ), so large envelopes and ciphertexts failed to encode.
|
|
221
|
+
const BASE64_CHUNK = 0x8000;
|
|
222
|
+
/** Largest plaintext one envelope carries: the AEAD ciphertext cap minus the 16-byte tag. */
|
|
223
|
+
export const MAX_PLAINTEXT_LEN = MAX_AEAD_CT_LEN - 16;
|
|
224
|
+
export function base64Encode(bytes) {
|
|
225
|
+
let binary = '';
|
|
226
|
+
for (let i = 0; i < bytes.length; i += BASE64_CHUNK) {
|
|
227
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + BASE64_CHUNK));
|
|
228
|
+
}
|
|
229
|
+
return btoa(binary);
|
|
230
|
+
}
|
|
231
|
+
export function base64Decode(b64) {
|
|
232
|
+
return Uint8Array.from(atob(b64), c => c.charCodeAt(0));
|
|
233
|
+
}
|
|
234
|
+
// Re-export low-level ops for callers that need them
|
|
235
|
+
export { assembleKey, decryptWithMasterKey, kemEncrypt };
|
|
236
|
+
// Re-export G2 public key parsing helper.
|
|
237
|
+
export function mpkFromBase64(base64) {
|
|
238
|
+
return base64Decode(base64);
|
|
239
|
+
}
|
|
240
|
+
// Parse a G2 compressed point and return it as a noble G2 point (for DKG-level work).
|
|
241
|
+
export function parseMpk(bytes) {
|
|
242
|
+
return bls12_381.G2.ProjectivePoint.fromHex(bytes);
|
|
243
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** bigint to 32-byte little-endian, reduced canonical mod L. */
|
|
2
|
+
export declare function scalarToLe(n: bigint): Uint8Array;
|
|
3
|
+
/** One node's Round-1 commitment (from POST /v1/shards/sign/commit). */
|
|
4
|
+
export interface FrostCommitment {
|
|
5
|
+
/** 1-indexed FROST participant id (u16). */
|
|
6
|
+
identifier: number;
|
|
7
|
+
/** 32-byte compressed Edwards hiding nonce commitment D_i. */
|
|
8
|
+
hiding: Uint8Array;
|
|
9
|
+
/** 32-byte compressed Edwards binding nonce commitment E_i. */
|
|
10
|
+
binding: Uint8Array;
|
|
11
|
+
}
|
|
12
|
+
/** One node's Round-2 signature share (from POST /v1/shards/sign/partial). */
|
|
13
|
+
export interface FrostShare {
|
|
14
|
+
/** Nonzero threshold participant identifier. */
|
|
15
|
+
identifier: number;
|
|
16
|
+
/** 32-byte little-endian scalar z_i. */
|
|
17
|
+
z: Uint8Array;
|
|
18
|
+
/** 32-byte compressed Edwards verifying share Y_i = g^{s_i}. */
|
|
19
|
+
verifyingShare: Uint8Array;
|
|
20
|
+
}
|
|
21
|
+
/** A FROST-Ed25519 group signature: R (32B compressed) + z (32B LE scalar). */
|
|
22
|
+
export interface FrostSignature {
|
|
23
|
+
/** 32-byte compressed Edwards group commitment. */
|
|
24
|
+
r: Uint8Array;
|
|
25
|
+
/** 32-byte little-endian aggregate signature scalar. */
|
|
26
|
+
z: Uint8Array;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Combine k Round-1 commitments + k Round-2 shares into the group signature.
|
|
30
|
+
* `commitments` MUST be in the same order that was sent to every node (the
|
|
31
|
+
* binding factors depend on the serialized list order). Each share is
|
|
32
|
+
* identifiable-abort verified; an invalid share throws naming its identifier.
|
|
33
|
+
*
|
|
34
|
+
* @param message - Message signed by every participant.
|
|
35
|
+
* @param groupPublicKey - 32-byte FROST group public key.
|
|
36
|
+
* @param commitments - Commitments from the selected signing participants.
|
|
37
|
+
* @param shares - Signature shares for those commitments.
|
|
38
|
+
*/
|
|
39
|
+
export declare function aggregate(message: Uint8Array, groupPublicKey: Uint8Array, commitments: FrostCommitment[], shares: FrostShare[]): FrostSignature;
|
|
40
|
+
/**
|
|
41
|
+
* Verify the FROST-Ed25519 group signature using the challenge SHA-512 over
|
|
42
|
+
* the concatenated commitment, group public key and message, reduced modulo
|
|
43
|
+
* the scalar order. Return false if point decoding or signature verification fails.
|
|
44
|
+
*
|
|
45
|
+
* @param groupPublicKey - 32-byte FROST group public key.
|
|
46
|
+
* @param message - Original signed message bytes.
|
|
47
|
+
* @param sig - Aggregated FROST signature to verify.
|
|
48
|
+
*/
|
|
49
|
+
export declare function verify(groupPublicKey: Uint8Array, message: Uint8Array, sig: FrostSignature): boolean;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// FROST-Ed25519 client-side aggregation + verification.
|
|
2
|
+
//
|
|
3
|
+
// This is the CLIENT half of the shard-delivery signing flow: the
|
|
4
|
+
// nodes each return a Round-1 commitment (hiding D_i, binding E_i) and a Round-2
|
|
5
|
+
// signature share z_i; the client (this code) combines them into a single group
|
|
6
|
+
// Schnorr signature, byte-for-byte compatible with the keepers' implementation.
|
|
7
|
+
//
|
|
8
|
+
// The CHALLENGE is RFC 9591 H2, which IS the RFC 8032 PureEdDSA challenge:
|
|
9
|
+
// a bare SHA-512(R || Y || M) mod L, with NO domain tag and NO length prefix.
|
|
10
|
+
// So an aggregate here is also a valid stock Ed25519 (and `ssh-ed25519`)
|
|
11
|
+
// signature. Adding a domain tag here rejects every signature the network
|
|
12
|
+
// produces. The BINDING FACTOR (ρ) does carry one - do not "simplify" that one
|
|
13
|
+
// to match. test/frost.crypto.ts pins both against known-answer vectors.
|
|
14
|
+
//
|
|
15
|
+
// All scalars are little-endian mod L (the ed25519 group order); all points are
|
|
16
|
+
// 32-byte compressed Edwards. Hash-to-scalar wide-reduces SHA-512 little-endian.
|
|
17
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
18
|
+
import { mod, invert } from '@noble/curves/abstract/modular';
|
|
19
|
+
import { sha512 } from '@noble/hashes/sha512';
|
|
20
|
+
import { concatBytes, utf8ToBytes } from '@noble/hashes/utils';
|
|
21
|
+
const Point = ed25519.Point;
|
|
22
|
+
/** ed25519 group order L = 2^252 + 27742317777372353535851937790883648493. */
|
|
23
|
+
const L = ed25519.CURVE.n;
|
|
24
|
+
// Domain separator for binding factors; the signature challenge has no domain tag.
|
|
25
|
+
const DST_RHO = utf8ToBytes('FROST-Ed25519-SHA512-v1/rho');
|
|
26
|
+
// encoding helpers
|
|
27
|
+
function u16le(n) {
|
|
28
|
+
const b = new Uint8Array(2);
|
|
29
|
+
new DataView(b.buffer).setUint16(0, n, true);
|
|
30
|
+
return b;
|
|
31
|
+
}
|
|
32
|
+
function u64le(n) {
|
|
33
|
+
const b = new Uint8Array(8);
|
|
34
|
+
new DataView(b.buffer).setBigUint64(0, BigInt(n), true);
|
|
35
|
+
return b;
|
|
36
|
+
}
|
|
37
|
+
/** 32-byte little-endian to bigint (also used wide, for the 64-byte SHA-512). */
|
|
38
|
+
function leToBig(b) {
|
|
39
|
+
let n = 0n;
|
|
40
|
+
for (let i = b.length - 1; i >= 0; i--)
|
|
41
|
+
n = (n << 8n) | BigInt(b[i]);
|
|
42
|
+
return n;
|
|
43
|
+
}
|
|
44
|
+
/** bigint to 32-byte little-endian, reduced canonical mod L. */
|
|
45
|
+
export function scalarToLe(n) {
|
|
46
|
+
let v = mod(n, L);
|
|
47
|
+
const out = new Uint8Array(32);
|
|
48
|
+
for (let i = 0; i < 32; i++) {
|
|
49
|
+
out[i] = Number(v & 0xffn);
|
|
50
|
+
v >>= 8n;
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
/** SHA-512 over the parts, interpreted little-endian, reduced mod L. */
|
|
55
|
+
function hashToScalar(...parts) {
|
|
56
|
+
return mod(leToBig(sha512(concatBytes(...parts))), L);
|
|
57
|
+
}
|
|
58
|
+
function decompress(b) {
|
|
59
|
+
return Point.fromHex(b);
|
|
60
|
+
}
|
|
61
|
+
/** Scalar-multiply, tolerating k ≡ 0 (noble's multiply throws on 0). */
|
|
62
|
+
function mul(p, k) {
|
|
63
|
+
const kk = mod(k, L);
|
|
64
|
+
return kk === 0n ? Point.ZERO : p.multiply(kk);
|
|
65
|
+
}
|
|
66
|
+
// Lagrange basis polynomial for `xi` evaluated at 0 over the index set `xs`,
|
|
67
|
+
// in the scalar field mod L. Mirrors the reference lagrange-at-0.
|
|
68
|
+
function lagrangeAtZero(xi, xs) {
|
|
69
|
+
let num = 1n;
|
|
70
|
+
let den = 1n;
|
|
71
|
+
for (const xj of xs) {
|
|
72
|
+
if (xj === xi)
|
|
73
|
+
continue;
|
|
74
|
+
num = mod(num * mod(-xj, L), L);
|
|
75
|
+
den = mod(den * mod(xi - xj, L), L);
|
|
76
|
+
}
|
|
77
|
+
return mod(num * invert(den, L), L);
|
|
78
|
+
}
|
|
79
|
+
// aggregation + verification
|
|
80
|
+
/**
|
|
81
|
+
* Combine k Round-1 commitments + k Round-2 shares into the group signature.
|
|
82
|
+
* `commitments` MUST be in the same order that was sent to every node (the
|
|
83
|
+
* binding factors depend on the serialized list order). Each share is
|
|
84
|
+
* identifiable-abort verified; an invalid share throws naming its identifier.
|
|
85
|
+
*
|
|
86
|
+
* @param message - Message signed by every participant.
|
|
87
|
+
* @param groupPublicKey - 32-byte FROST group public key.
|
|
88
|
+
* @param commitments - Commitments from the selected signing participants.
|
|
89
|
+
* @param shares - Signature shares for those commitments.
|
|
90
|
+
*/
|
|
91
|
+
export function aggregate(message, groupPublicKey, commitments, shares) {
|
|
92
|
+
if (commitments.length === 0)
|
|
93
|
+
throw new Error('aggregate: no commitments');
|
|
94
|
+
// serialized = || over commitments of (u16_LE(id) || hiding32 || binding32)
|
|
95
|
+
const serialized = concatBytes(...commitments.flatMap(c => [u16le(c.identifier), c.hiding, c.binding]));
|
|
96
|
+
const lenMsg = u64le(message.length);
|
|
97
|
+
// ρ_i = H_rho(id_i, len(msg), msg, serialized) - per signer
|
|
98
|
+
const rho = new Map();
|
|
99
|
+
for (const c of commitments) {
|
|
100
|
+
rho.set(c.identifier, hashToScalar(DST_RHO, u16le(c.identifier), lenMsg, message, serialized));
|
|
101
|
+
}
|
|
102
|
+
// R = Σ_i (D_i + ρ_i * E_i)
|
|
103
|
+
let R = Point.ZERO;
|
|
104
|
+
for (const c of commitments) {
|
|
105
|
+
const Ri = decompress(c.hiding).add(mul(decompress(c.binding), rho.get(c.identifier)));
|
|
106
|
+
R = R.add(Ri);
|
|
107
|
+
}
|
|
108
|
+
const Rc = R.toRawBytes();
|
|
109
|
+
// c = SHA-512(R || Y || M) mod L - RFC 8032, no domain tag, no length prefix
|
|
110
|
+
const chal = hashToScalar(Rc, groupPublicKey, message);
|
|
111
|
+
const ids = commitments.map(c => BigInt(c.identifier));
|
|
112
|
+
const shareById = new Map(shares.map(s => [s.identifier, s]));
|
|
113
|
+
let z = 0n;
|
|
114
|
+
for (const c of commitments) {
|
|
115
|
+
const share = shareById.get(c.identifier);
|
|
116
|
+
if (!share)
|
|
117
|
+
throw new Error(`aggregate: missing share for identifier ${c.identifier}`);
|
|
118
|
+
const lambda = lagrangeAtZero(BigInt(c.identifier), ids);
|
|
119
|
+
const zi = leToBig(share.z);
|
|
120
|
+
// identifiable abort: g^{z_i} == D_i + ρ_i*E_i + (λ_i*c)*Y_i
|
|
121
|
+
const lhs = mul(Point.BASE, zi);
|
|
122
|
+
const rhs = decompress(c.hiding)
|
|
123
|
+
.add(mul(decompress(c.binding), rho.get(c.identifier)))
|
|
124
|
+
.add(mul(decompress(share.verifyingShare), mod(lambda * chal, L)));
|
|
125
|
+
if (!lhs.equals(rhs)) {
|
|
126
|
+
throw new Error(`aggregate: invalid signature share from identifier ${c.identifier}`);
|
|
127
|
+
}
|
|
128
|
+
z = mod(z + zi, L);
|
|
129
|
+
}
|
|
130
|
+
return { r: Rc, z: scalarToLe(z) };
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Verify the FROST-Ed25519 group signature using the challenge SHA-512 over
|
|
134
|
+
* the concatenated commitment, group public key and message, reduced modulo
|
|
135
|
+
* the scalar order. Return false if point decoding or signature verification fails.
|
|
136
|
+
*
|
|
137
|
+
* @param groupPublicKey - 32-byte FROST group public key.
|
|
138
|
+
* @param message - Original signed message bytes.
|
|
139
|
+
* @param sig - Aggregated FROST signature to verify.
|
|
140
|
+
*/
|
|
141
|
+
export function verify(groupPublicKey, message, sig) {
|
|
142
|
+
try {
|
|
143
|
+
const R = decompress(sig.r);
|
|
144
|
+
const Y = decompress(groupPublicKey);
|
|
145
|
+
const z = leToBig(sig.z);
|
|
146
|
+
const chal = hashToScalar(sig.r, groupPublicKey, message);
|
|
147
|
+
return mul(Point.BASE, z).equals(R.add(mul(Y, chal)));
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decode hexadecimal text, accepting an optional 0x prefix. Reject odd-length input; callers must validate hexadecimal characters before decoding.
|
|
3
|
+
*
|
|
4
|
+
* @param hex - Hexadecimal bytes, with an optional 0x prefix.
|
|
5
|
+
*/
|
|
6
|
+
export declare function hexToBytes(hex: string): Uint8Array;
|
|
7
|
+
export declare function bytesToHex(bytes: Uint8Array): string;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Utility: 0x-prefixed hex string to Uint8Array (32 bytes for slot IDs).
|
|
2
|
+
/**
|
|
3
|
+
* Decode hexadecimal text, accepting an optional 0x prefix. Reject odd-length input; callers must validate hexadecimal characters before decoding.
|
|
4
|
+
*
|
|
5
|
+
* @param hex - Hexadecimal bytes, with an optional 0x prefix.
|
|
6
|
+
*/
|
|
7
|
+
export function hexToBytes(hex) {
|
|
8
|
+
const clean = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
9
|
+
if (clean.length % 2 !== 0) {
|
|
10
|
+
throw new Error(`hexToBytes: odd-length hex string: ${clean}`);
|
|
11
|
+
}
|
|
12
|
+
const out = new Uint8Array(clean.length / 2);
|
|
13
|
+
for (let i = 0; i < out.length; i++) {
|
|
14
|
+
out[i] = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
// Utility: Uint8Array to 0x-prefixed hex string.
|
|
19
|
+
export function bytesToHex(bytes) {
|
|
20
|
+
return ('0x' +
|
|
21
|
+
Array.from(bytes)
|
|
22
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
23
|
+
.join(''));
|
|
24
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { type IbeCiphertext } from './ibe.js';
|
|
2
|
+
export declare const IBE_BLOB_VERSION = 1;
|
|
3
|
+
export declare const IBE_BLOB_AAD_DOMAIN = "keykeeper/ibe-blob/v1";
|
|
4
|
+
/** Default chunk: 1 MiB of plaintext (+16-byte tag on the wire). */
|
|
5
|
+
export declare const IBE_BLOB_DEFAULT_CHUNK: number;
|
|
6
|
+
/** The clear header stored beside the ciphertext body. Nothing in it is secret. */
|
|
7
|
+
export interface IbeBlobHeader {
|
|
8
|
+
/** Encrypted blob format version. */
|
|
9
|
+
v: 1;
|
|
10
|
+
/** 16 random bytes, base64 - the object's identity for the chunk binding. */
|
|
11
|
+
blobId: string;
|
|
12
|
+
/** The IBE identity the data key is wrapped to. */
|
|
13
|
+
identity: string;
|
|
14
|
+
/** Media type of the original plaintext object. */
|
|
15
|
+
contentType: string;
|
|
16
|
+
/** Plaintext size in bytes. */
|
|
17
|
+
size: number;
|
|
18
|
+
/** Maximum plaintext bytes in each chunk. */
|
|
19
|
+
chunkSize: number;
|
|
20
|
+
/** Number of encrypted chunks in the body. */
|
|
21
|
+
chunkCount: number;
|
|
22
|
+
/** `ibeEncrypt(mpk, identity, dek)` - the wire shape of `bls::ibe::Ciphertext`. */
|
|
23
|
+
wrappedKey: {
|
|
24
|
+
u: string;
|
|
25
|
+
nonce: string;
|
|
26
|
+
aead_ct: string;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** Encrypted blob header and ordered authenticated ciphertext chunks. */
|
|
30
|
+
export interface SealedBlob {
|
|
31
|
+
/** Metadata and wrapped data key required to open the blob. */
|
|
32
|
+
header: IbeBlobHeader;
|
|
33
|
+
/** The concatenated encrypted chunks. */
|
|
34
|
+
body: Uint8Array;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Return a chunk's encrypted byte range as [start, end), including its 16-byte tag.
|
|
38
|
+
* The final chunk has header.size - index * header.chunkSize plaintext bytes.
|
|
39
|
+
* An empty object has one chunk: start 0, end 16, plainLength 0.
|
|
40
|
+
* Use a trusted header and validate that index is a safe integer before calling.
|
|
41
|
+
* Negative indices and indices at or beyond header.chunkCount throw RangeError.
|
|
42
|
+
* For an HTTP Range header, use end - 1 as the inclusive last byte.
|
|
43
|
+
*
|
|
44
|
+
* @param header - Blob header describing plaintext length and chunk size.
|
|
45
|
+
* @param index - Zero-based integer chunk index below header.chunkCount.
|
|
46
|
+
*/
|
|
47
|
+
export declare function ibeBlobChunkRange(header: IbeBlobHeader, index: number): {
|
|
48
|
+
start: number;
|
|
49
|
+
end: number;
|
|
50
|
+
plainLength: number;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Seal `plaintext` to `identity` under the slot's master public key: a fresh data key,
|
|
54
|
+
* IBE-wrapped, and the body as independently-decryptable AES-256-GCM chunks. Offline and
|
|
55
|
+
* permissionless, like `ibeEncrypt`.
|
|
56
|
+
* Empty plaintext produces one authenticated chunk with no plaintext and a 16-byte tag.
|
|
57
|
+
* An exact multiple of chunkSize has no extra chunk; otherwise the last chunk is shorter.
|
|
58
|
+
* Retain a trusted header, or authenticate a manifest containing both the header
|
|
59
|
+
* and body digest. A signed body digest alone does not authenticate the media type.
|
|
60
|
+
*
|
|
61
|
+
* @param mpkBytes - 96-byte compressed BLS master public key.
|
|
62
|
+
* @param identity - Identity string used to wrap the object data key.
|
|
63
|
+
* @param plaintext - Complete plaintext object bytes.
|
|
64
|
+
* @param opts - Optional media type and integer plaintext chunk size of at least 1024 bytes; default chunk size is 1 MiB.
|
|
65
|
+
*/
|
|
66
|
+
export declare function ibeSealBlob(mpkBytes: Uint8Array, identity: string, plaintext: Uint8Array, opts?: {
|
|
67
|
+
contentType?: string;
|
|
68
|
+
chunkSize?: number;
|
|
69
|
+
}): Promise<SealedBlob>;
|
|
70
|
+
/**
|
|
71
|
+
* The blob's IBE-wrapped data key as an `IbeCiphertext` (what `ibeDecryptWithKey` takes).
|
|
72
|
+
*
|
|
73
|
+
* @param header - Blob header containing the wrapped data key.
|
|
74
|
+
*/
|
|
75
|
+
export declare function ibeBlobWrappedKey(header: IbeBlobHeader): IbeCiphertext;
|
|
76
|
+
/**
|
|
77
|
+
* Unwrap the data key with the identity's extracted key `sk_ID` (48-byte compressed G1 -
|
|
78
|
+
* `ibeCombineExtract`'s output). One pairing; the caller keeps the returned key in memory only
|
|
79
|
+
* as long as it decrypts, then zeroizes it.
|
|
80
|
+
*
|
|
81
|
+
* @param skIdBytes - 48-byte compressed extracted identity key.
|
|
82
|
+
* @param header - Header containing the wrapped data key and identity.
|
|
83
|
+
*/
|
|
84
|
+
export declare function ibeUnwrapBlobKey(skIdBytes: Uint8Array, header: IbeBlobHeader): Uint8Array;
|
|
85
|
+
/**
|
|
86
|
+
* A WebCrypto key for `dek`, importable once per blob and reused across chunks.
|
|
87
|
+
*
|
|
88
|
+
* @param dek - 32-byte unwrapped AES data key to import for decryption.
|
|
89
|
+
*/
|
|
90
|
+
export declare function ibeBlobDecryptKey(dek: Uint8Array): Promise<CryptoKey>;
|
|
91
|
+
/**
|
|
92
|
+
* Decrypt one chunk (its exact body slice, see {@link ibeBlobChunkRange}).
|
|
93
|
+
*
|
|
94
|
+
* @param key - AES-GCM decryption key imported from the unwrapped data key.
|
|
95
|
+
* @param header - Header providing chunk identity and authentication context.
|
|
96
|
+
* @param index - Zero-based integer chunk index below header.chunkCount.
|
|
97
|
+
* @param chunk - Complete encrypted chunk including its authentication tag.
|
|
98
|
+
*/
|
|
99
|
+
export declare function ibeDecryptBlobChunk(key: CryptoKey, header: IbeBlobHeader, index: number, chunk: Uint8Array): Promise<Uint8Array>;
|
|
100
|
+
/**
|
|
101
|
+
* Open a whole sealed blob with `sk_ID`: unwrap the key, decrypt every chunk, return the
|
|
102
|
+
* plaintext. Streaming consumers use `ibeUnwrapBlobKey` + `ibeDecryptBlobChunk` per range.
|
|
103
|
+
*
|
|
104
|
+
* @param skIdBytes - 48-byte compressed extracted identity key.
|
|
105
|
+
* @param header - Header returned when the blob was sealed.
|
|
106
|
+
* @param body - Concatenated encrypted chunks in their original order.
|
|
107
|
+
*/
|
|
108
|
+
export declare function ibeOpenBlob(skIdBytes: Uint8Array, header: IbeBlobHeader, body: Uint8Array): Promise<Uint8Array>;
|
|
109
|
+
/**
|
|
110
|
+
* `sha256(body)` - what a producer signs in its manifest so a reader can check provenance.
|
|
111
|
+
*
|
|
112
|
+
* @param body - Complete encrypted blob body to hash.
|
|
113
|
+
*/
|
|
114
|
+
export declare function ibeBlobDigest(body: Uint8Array): Uint8Array;
|