@orangecheck/agent-core 1.0.1 → 1.2.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/README.md +4 -0
- package/dist/index.d.mts +88 -3
- package/dist/index.d.ts +88 -3
- package/dist/index.js +193 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +188 -1
- package/dist/index.mjs.map +1 -1
- package/dist/types.d.mts +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.js.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +1 -1
- package/src/assert-scope.test.ts +72 -0
- package/src/assert-scope.ts +80 -0
- package/src/federation.test.ts +139 -0
- package/src/federation.ts +329 -0
- package/src/index.ts +21 -0
- package/src/test-vectors.test.ts +17 -0
- package/src/types.ts +7 -1
package/dist/index.mjs
CHANGED
|
@@ -944,7 +944,194 @@ function isHex64(s) {
|
|
|
944
944
|
function isIsoUtc(s) {
|
|
945
945
|
return typeof s === "string" && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/.test(s);
|
|
946
946
|
}
|
|
947
|
+
function federationDescriptorCanonicalMessage(descriptor) {
|
|
948
|
+
const addresses = descriptor.guardians.map((g) => g.address).sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
949
|
+
return [
|
|
950
|
+
"oc-agent:federation:v1",
|
|
951
|
+
`threshold: ${descriptor.threshold}`,
|
|
952
|
+
...addresses.map((a) => `guardian: ${a}`)
|
|
953
|
+
].join("\n");
|
|
954
|
+
}
|
|
955
|
+
function computeFederationDescriptorId(descriptor) {
|
|
956
|
+
return hexEncode(sha256(new TextEncoder().encode(federationDescriptorCanonicalMessage(descriptor))));
|
|
957
|
+
}
|
|
958
|
+
function parseThreshold(t) {
|
|
959
|
+
if (typeof t !== "string") return null;
|
|
960
|
+
const m = /^(\d+)-of-(\d+)$/.exec(t);
|
|
961
|
+
if (!m) return null;
|
|
962
|
+
const mm = Number(m[1]);
|
|
963
|
+
const nn = Number(m[2]);
|
|
964
|
+
if (!Number.isInteger(mm) || !Number.isInteger(nn) || mm < 1 || mm > nn) return null;
|
|
965
|
+
return { m: mm, n: nn };
|
|
966
|
+
}
|
|
967
|
+
var HEX64 = /^[0-9a-f]{64}$/;
|
|
968
|
+
var ISO_UTC = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/;
|
|
969
|
+
async function checkFederationQuorum(principal, sig, reconstructedId, input) {
|
|
970
|
+
if (principal?.alg !== "federation") {
|
|
971
|
+
return fail("E_MALFORMED", 'principal.alg must be "federation"');
|
|
972
|
+
}
|
|
973
|
+
const descriptor = principal.descriptor;
|
|
974
|
+
if (!descriptor || descriptor.kind !== "agent-federation") {
|
|
975
|
+
return fail("E_MALFORMED", "principal.descriptor missing or wrong kind");
|
|
976
|
+
}
|
|
977
|
+
const parsed = parseThreshold(descriptor.threshold);
|
|
978
|
+
if (!parsed) return fail("E_MALFORMED", `malformed threshold "${descriptor.threshold}"`);
|
|
979
|
+
if (!Array.isArray(descriptor.guardians) || descriptor.guardians.length !== parsed.n) {
|
|
980
|
+
return fail("E_MALFORMED", "guardians length must equal N in M-of-N");
|
|
981
|
+
}
|
|
982
|
+
const computedDescId = computeFederationDescriptorId(descriptor);
|
|
983
|
+
if (principal.descriptor_id !== computedDescId) {
|
|
984
|
+
return fail(
|
|
985
|
+
"E_BAD_FEDERATION_DESCRIPTOR",
|
|
986
|
+
`declared descriptor_id (${principal.descriptor_id}) != canonical hash (${computedDescId})`
|
|
987
|
+
);
|
|
988
|
+
}
|
|
989
|
+
if (sig?.alg !== "federation-bip322") {
|
|
990
|
+
return fail("E_MALFORMED", 'sig.alg must be "federation-bip322"');
|
|
991
|
+
}
|
|
992
|
+
if (sig.threshold !== descriptor.threshold) {
|
|
993
|
+
return fail(
|
|
994
|
+
"E_THRESHOLD_MISMATCH",
|
|
995
|
+
`sig.threshold (${sig.threshold}) != descriptor.threshold (${descriptor.threshold})`
|
|
996
|
+
);
|
|
997
|
+
}
|
|
998
|
+
const sigs = sig.signatures;
|
|
999
|
+
if (!Array.isArray(sigs)) return fail("E_MALFORMED", "sig.signatures must be an array");
|
|
1000
|
+
if (sigs.length < parsed.m) {
|
|
1001
|
+
return fail(
|
|
1002
|
+
"E_THRESHOLD_NOT_MET",
|
|
1003
|
+
`${sigs.length} signature(s) below threshold M=${parsed.m}`
|
|
1004
|
+
);
|
|
1005
|
+
}
|
|
1006
|
+
const guardianSet = new Set(descriptor.guardians.map((g) => g.address));
|
|
1007
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1008
|
+
for (const s of sigs) {
|
|
1009
|
+
if (!guardianSet.has(s.guardian_address)) {
|
|
1010
|
+
return fail("E_UNKNOWN_GUARDIAN", `${s.guardian_address} is not a declared guardian`);
|
|
1011
|
+
}
|
|
1012
|
+
if (seen.has(s.guardian_address)) {
|
|
1013
|
+
return fail("E_DUPLICATE_GUARDIAN", `${s.guardian_address} signed more than once`);
|
|
1014
|
+
}
|
|
1015
|
+
seen.add(s.guardian_address);
|
|
1016
|
+
}
|
|
1017
|
+
if (!input.skipSignatureVerification) {
|
|
1018
|
+
if (!input.verifyBip322) return fail("E_BAD_SIG", "no BIP-322 verifier supplied");
|
|
1019
|
+
for (const s of sigs) {
|
|
1020
|
+
const ok = await input.verifyBip322(reconstructedId, s.value, s.guardian_address);
|
|
1021
|
+
if (!ok) {
|
|
1022
|
+
return fail("E_BAD_SIG", `guardian ${s.guardian_address} signature did not verify`);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
return { ok: true };
|
|
1027
|
+
}
|
|
1028
|
+
async function verifyFederationDelegation(input) {
|
|
1029
|
+
const env = input.envelope;
|
|
1030
|
+
if (env?.kind !== "agent-delegation") return fail("E_MALFORMED", 'kind must be "agent-delegation"');
|
|
1031
|
+
if (!HEX64.test(env.id ?? "")) return fail("E_MALFORMED", "id must be 64 lowercase hex chars");
|
|
1032
|
+
if (!env.agent?.address || env.agent.alg !== "bip322") return fail("E_MALFORMED", "agent invalid");
|
|
1033
|
+
if (!Array.isArray(env.scopes) || env.scopes.length === 0) {
|
|
1034
|
+
return fail("E_MALFORMED", "scopes must be a non-empty array");
|
|
1035
|
+
}
|
|
1036
|
+
if (!ISO_UTC.test(env.issued_at) || !ISO_UTC.test(env.expires_at)) {
|
|
1037
|
+
return fail("E_MALFORMED", "issued_at / expires_at must be ISO 8601 UTC");
|
|
1038
|
+
}
|
|
1039
|
+
if (!/^[0-9a-f]{32}$/.test(env.nonce)) return fail("E_MALFORMED", "nonce must be 32 hex chars");
|
|
1040
|
+
let canonicalScopes;
|
|
1041
|
+
try {
|
|
1042
|
+
canonicalScopes = canonicalizeScopes(env.scopes);
|
|
1043
|
+
} catch (e) {
|
|
1044
|
+
return fail("E_BAD_SCOPE_GRAMMAR", e.message);
|
|
1045
|
+
}
|
|
1046
|
+
for (let i = 0; i < canonicalScopes.length; i++) {
|
|
1047
|
+
if (env.scopes[i] !== canonicalScopes[i]) {
|
|
1048
|
+
return fail("E_BAD_SCOPE_GRAMMAR", `scope index ${i} not in canonical order`);
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
const canonInput = {
|
|
1052
|
+
principal: `federation:${env.principal?.descriptor_id ?? ""}`,
|
|
1053
|
+
agent: env.agent.address,
|
|
1054
|
+
scopes: canonicalScopes,
|
|
1055
|
+
bond_sats: env.bond?.sats ?? 0,
|
|
1056
|
+
bond_attestation: env.bond?.attestation_id ?? "none",
|
|
1057
|
+
issued_at: env.issued_at,
|
|
1058
|
+
expires_at: env.expires_at,
|
|
1059
|
+
nonce: env.nonce
|
|
1060
|
+
};
|
|
1061
|
+
const reconstructedId = computeDelegationId(canonInput);
|
|
1062
|
+
if (reconstructedId !== env.id) {
|
|
1063
|
+
return fail("E_BAD_ID", `reconstructed id (${reconstructedId}) != envelope.id (${env.id})`);
|
|
1064
|
+
}
|
|
1065
|
+
const quorum = await checkFederationQuorum(env.principal, env.sig, env.id, input);
|
|
1066
|
+
if (!quorum.ok) return quorum;
|
|
1067
|
+
if (!input.skipTemporalCheck) {
|
|
1068
|
+
const now = input.now ?? /* @__PURE__ */ new Date();
|
|
1069
|
+
const issued = new Date(env.issued_at);
|
|
1070
|
+
const expires = new Date(env.expires_at);
|
|
1071
|
+
if (expires <= issued) return fail("E_MALFORMED", "expires_at <= issued_at");
|
|
1072
|
+
if (now < issued) return fail("E_NOT_YET_VALID", `delegation not valid until ${env.issued_at}`);
|
|
1073
|
+
if (now >= expires) return fail("E_EXPIRED", `delegation expired at ${env.expires_at}`);
|
|
1074
|
+
}
|
|
1075
|
+
return { ok: true, id: env.id, canonicalMessage: delegationCanonicalMessage(canonInput) };
|
|
1076
|
+
}
|
|
1077
|
+
async function verifyFederationRevocation(input) {
|
|
1078
|
+
const env = input.envelope;
|
|
1079
|
+
if (env?.kind !== "agent-revocation") return fail("E_MALFORMED", 'kind must be "agent-revocation"');
|
|
1080
|
+
if (!HEX64.test(env.id ?? "")) return fail("E_MALFORMED", "id must be 64 lowercase hex chars");
|
|
1081
|
+
if (!HEX64.test(env.delegation_id ?? "")) return fail("E_MALFORMED", "delegation_id must be 64-hex");
|
|
1082
|
+
if (typeof env.reason !== "string" || env.reason.length > 128) {
|
|
1083
|
+
return fail("E_MALFORMED", "reason must be a string \u2264128 bytes");
|
|
1084
|
+
}
|
|
1085
|
+
if (!ISO_UTC.test(env.signed_at)) return fail("E_MALFORMED", "signed_at must be ISO 8601 UTC");
|
|
1086
|
+
const canonInput = {
|
|
1087
|
+
address: `federation:${env.signer?.descriptor_id ?? ""}`,
|
|
1088
|
+
delegation_id: env.delegation_id,
|
|
1089
|
+
reason: env.reason,
|
|
1090
|
+
signed_at: env.signed_at
|
|
1091
|
+
};
|
|
1092
|
+
const reconstructedId = computeRevocationId(canonInput);
|
|
1093
|
+
if (reconstructedId !== env.id) {
|
|
1094
|
+
return fail("E_BAD_ID", `reconstructed id (${reconstructedId}) != envelope.id (${env.id})`);
|
|
1095
|
+
}
|
|
1096
|
+
const quorum = await checkFederationQuorum(env.signer, env.sig, env.id, input);
|
|
1097
|
+
if (!quorum.ok) return quorum;
|
|
1098
|
+
return { ok: true, id: env.id, canonicalMessage: revocationCanonicalMessage(canonInput) };
|
|
1099
|
+
}
|
|
1100
|
+
function fail(code, message) {
|
|
1101
|
+
return { ok: false, code, message };
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
// src/assert-scope.ts
|
|
1105
|
+
var ScopeNotGrantedError = class extends Error {
|
|
1106
|
+
constructor(reason, message) {
|
|
1107
|
+
super(message);
|
|
1108
|
+
this.name = "ScopeNotGrantedError";
|
|
1109
|
+
this.reason = reason;
|
|
1110
|
+
}
|
|
1111
|
+
};
|
|
1112
|
+
function assertScopeGranted(delegation, scopeExercised, fnName) {
|
|
1113
|
+
if (hasPrivateScopes(delegation)) {
|
|
1114
|
+
throw new ScopeNotGrantedError(
|
|
1115
|
+
"scopes_encrypted",
|
|
1116
|
+
`${fnName}: this delegation uses v1.2 private scopes \u2014 \`scopes_encrypted\` is set and \`scopes\` is absent, so the granted set cannot be read here. Decrypt it with decryptPrivateScopes() using a matching device key and pass the recovered scopes as \`delegation.scopes\`. Refusing to stamp \`${scopeExercised}\` against an unreadable grant.`
|
|
1117
|
+
);
|
|
1118
|
+
}
|
|
1119
|
+
const scopes = delegation.scopes;
|
|
1120
|
+
if (scopes === void 0 || scopes.length === 0) {
|
|
1121
|
+
throw new ScopeNotGrantedError(
|
|
1122
|
+
"no_scopes",
|
|
1123
|
+
`${fnName}: delegation grants no scopes, so \`${scopeExercised}\` cannot be exercised. An absent or empty \`scopes\` means nothing is granted, never everything.`
|
|
1124
|
+
);
|
|
1125
|
+
}
|
|
1126
|
+
const exercised = parseScope(scopeExercised);
|
|
1127
|
+
if (!scopes.map(parseScope).some((g) => isSubScope(exercised, g))) {
|
|
1128
|
+
throw new ScopeNotGrantedError(
|
|
1129
|
+
"not_subscope",
|
|
1130
|
+
`${fnName}: scope_exercised (${scopeExercised}) is not a sub-scope of any granted scope (${scopes.join(", ")})`
|
|
1131
|
+
);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
947
1134
|
|
|
948
|
-
export { AgentError, DEFAULT_MAX_CHAIN_DEPTH, ENVELOPE_VERSION, REGISTERED_SCOPES, ScopeParseError, actionCanonicalBytes, actionCanonicalMessage, canonicalActionBytes, canonicalDelegationBytes, canonicalRevocationBytes, canonicalizeAction, canonicalizeDelegation, canonicalizeRevocation, canonicalizeScope, canonicalizeScopeString, canonicalizeScopes, computeActionId, computeDelegationId, computeRevocationId, computeSubdelegationId, decodeScopesPayload, delegationCanonicalBytes, delegationCanonicalMessage, encodeScopesPayload, hasPrivateScopes, isSubScope, parseAndCanonicalizeScopes, parseScope, revocationCanonicalBytes, revocationCanonicalMessage, sealScopes, sha256Hex, subdelegationCanonicalBytes, subdelegationCanonicalMessage, unsealScopes, validateScope, verifyAction, verifyDelegation, verifyRevocation, verifySubdelegation };
|
|
1135
|
+
export { AgentError, DEFAULT_MAX_CHAIN_DEPTH, ENVELOPE_VERSION, REGISTERED_SCOPES, ScopeNotGrantedError, ScopeParseError, actionCanonicalBytes, actionCanonicalMessage, assertScopeGranted, canonicalActionBytes, canonicalDelegationBytes, canonicalRevocationBytes, canonicalizeAction, canonicalizeDelegation, canonicalizeRevocation, canonicalizeScope, canonicalizeScopeString, canonicalizeScopes, computeActionId, computeDelegationId, computeFederationDescriptorId, computeRevocationId, computeSubdelegationId, decodeScopesPayload, delegationCanonicalBytes, delegationCanonicalMessage, encodeScopesPayload, federationDescriptorCanonicalMessage, hasPrivateScopes, isSubScope, parseAndCanonicalizeScopes, parseScope, revocationCanonicalBytes, revocationCanonicalMessage, sealScopes, sha256Hex, subdelegationCanonicalBytes, subdelegationCanonicalMessage, unsealScopes, validateScope, verifyAction, verifyDelegation, verifyFederationDelegation, verifyFederationRevocation, verifyRevocation, verifySubdelegation };
|
|
949
1136
|
//# sourceMappingURL=index.mjs.map
|
|
950
1137
|
//# sourceMappingURL=index.mjs.map
|