@forestrie/receipt-verify 3.0.1 → 3.1.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/dist/decode-trust-root-cbor.d.ts +8 -1
- package/dist/decode-trust-root-cbor.d.ts.map +1 -1
- package/dist/decode-trust-root-cbor.js +10 -1
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -0
- package/dist/known-log-key.d.ts +15 -0
- package/dist/known-log-key.d.ts.map +1 -0
- package/dist/known-log-key.js +20 -0
- package/dist/ks256-attestation.d.ts +50 -0
- package/dist/ks256-attestation.d.ts.map +1 -0
- package/dist/ks256-attestation.js +104 -0
- package/dist/resolve-delegated-verify-key.d.ts +18 -9
- package/dist/resolve-delegated-verify-key.d.ts.map +1 -1
- package/dist/resolve-delegated-verify-key.js +26 -9
- package/dist/verify-grant-receipt-offline.d.ts +20 -6
- package/dist/verify-grant-receipt-offline.d.ts.map +1 -1
- package/dist/verify-grant-receipt-offline.js +60 -11
- package/dist/verify-ks256-delegation.d.ts +56 -0
- package/dist/verify-ks256-delegation.d.ts.map +1 -0
- package/dist/verify-ks256-delegation.js +95 -0
- package/package.json +5 -3
- package/src/decode-trust-root-cbor.ts +10 -1
- package/src/index.ts +23 -0
- package/src/known-log-key.ts +28 -0
- package/src/ks256-attestation.ts +144 -0
- package/src/resolve-delegated-verify-key.ts +37 -11
- package/src/verify-grant-receipt-offline.ts +92 -19
- package/src/verify-ks256-delegation.ts +120 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Offline KS256 root → delegation certificate check (plan-2610-03 D4, D5;
|
|
3
|
+
* protocol ADR-0045 amendment 2026-10-18). Receipts are always ES256, signed
|
|
4
|
+
* by a sealer's P-256 delegated key; what a KS256 root changes is how the
|
|
5
|
+
* label-1000 delegation certificate is checked against it.
|
|
6
|
+
*
|
|
7
|
+
* - An EOA root verifies by ecrecover through `@forestrie/delegation-cose`'s
|
|
8
|
+
* `verifyDelegationCertificateKs256` with no hooks: pure, the same guarantee
|
|
9
|
+
* as an ES256 root. delegation-cose also enforces the KS256 alg binding.
|
|
10
|
+
* - Anything ecrecover doesn't match (a contract root such as a Safe, or a
|
|
11
|
+
* wrong signer: offline you can't tell which) fails with
|
|
12
|
+
* `ks256_root_not_recovered`, unless the caller supplies an ERC-1271
|
|
13
|
+
* attestation (ks256-attestation.ts) bound to this root, this digest, this
|
|
14
|
+
* signature and the forest's genesis chain.
|
|
15
|
+
*
|
|
16
|
+
* No network and no injected callback: the chain read is a separate helper's
|
|
17
|
+
* job (forestrie-cli, mcp-resolve), as for the known accumulator.
|
|
18
|
+
*/
|
|
19
|
+
import { type Ks256CertificateDigest } from "@forestrie/delegation-cose";
|
|
20
|
+
import { type Ks256Attestation } from "./ks256-attestation.js";
|
|
21
|
+
/**
|
|
22
|
+
* Chain evidence a caller may supply for a KS256 contract root. `forestChainId`
|
|
23
|
+
* is read from the forest's genesis, never from the attestation or a
|
|
24
|
+
* coordinator; an attestation without it is refused.
|
|
25
|
+
*/
|
|
26
|
+
export type Ks256DelegationEvidence = {
|
|
27
|
+
attestation?: Ks256Attestation | undefined;
|
|
28
|
+
forestChainId?: bigint | undefined;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Check a delegation certificate against a KS256 root address.
|
|
32
|
+
*
|
|
33
|
+
* @param certBytes - The receipt's label-1000 COSE_Sign1 certificate.
|
|
34
|
+
* @param rootAddress - 20-byte root from genesis or a caller-known log key.
|
|
35
|
+
* @param evidence - Optional ERC-1271 attestation and the genesis chain id.
|
|
36
|
+
* @returns `ok`, or a stable reason: `delegation_not_ks256` (wrong alg,
|
|
37
|
+
* WebAuthn envelope or malformed), `ks256_root_not_recovered`, or a
|
|
38
|
+
* `ks256_attestation_*` mismatch.
|
|
39
|
+
*/
|
|
40
|
+
export declare function verifyKs256DelegationCert(certBytes: Uint8Array, rootAddress: Uint8Array, evidence: Ks256DelegationEvidence): Promise<{
|
|
41
|
+
ok: true;
|
|
42
|
+
} | {
|
|
43
|
+
ok: false;
|
|
44
|
+
reason: string;
|
|
45
|
+
}>;
|
|
46
|
+
/**
|
|
47
|
+
* Extract the KS256 delegation certificate's root address, digest and
|
|
48
|
+
* signature from a receipt: what an ERC-1271 attestation helper calls
|
|
49
|
+
* `isValidSignature` with. Does not verify anything.
|
|
50
|
+
*
|
|
51
|
+
* @param receiptCbor - COSE_Sign1 receipt bytes.
|
|
52
|
+
* @returns `null` when the receipt carries no label-1000 certificate.
|
|
53
|
+
* @throws When the certificate is not a KS256 certificate.
|
|
54
|
+
*/
|
|
55
|
+
export declare function extractKs256DelegationDigest(receiptCbor: Uint8Array): Ks256CertificateDigest | null;
|
|
56
|
+
//# sourceMappingURL=verify-ks256-delegation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify-ks256-delegation.d.ts","sourceRoot":"","sources":["../src/verify-ks256-delegation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAGL,KAAK,sBAAsB,EAC5B,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,wBAAwB,CAAC;AAKhC;;;;GAIG;AACH,MAAM,MAAM,uBAAuB,GAAG;IACpC,WAAW,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAC;IAC3C,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,yBAAyB,CAC7C,SAAS,EAAE,UAAU,EACrB,WAAW,EAAE,UAAU,EACvB,QAAQ,EAAE,uBAAuB,GAChC,OAAO,CAAC;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAiCvD;AAED;;;;;;;;GAQG;AACH,wBAAgB,4BAA4B,CAC1C,WAAW,EAAE,UAAU,GACtB,sBAAsB,GAAG,IAAI,CAQ/B"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Offline KS256 root → delegation certificate check (plan-2610-03 D4, D5;
|
|
3
|
+
* protocol ADR-0045 amendment 2026-10-18). Receipts are always ES256, signed
|
|
4
|
+
* by a sealer's P-256 delegated key; what a KS256 root changes is how the
|
|
5
|
+
* label-1000 delegation certificate is checked against it.
|
|
6
|
+
*
|
|
7
|
+
* - An EOA root verifies by ecrecover through `@forestrie/delegation-cose`'s
|
|
8
|
+
* `verifyDelegationCertificateKs256` with no hooks: pure, the same guarantee
|
|
9
|
+
* as an ES256 root. delegation-cose also enforces the KS256 alg binding.
|
|
10
|
+
* - Anything ecrecover doesn't match (a contract root such as a Safe, or a
|
|
11
|
+
* wrong signer: offline you can't tell which) fails with
|
|
12
|
+
* `ks256_root_not_recovered`, unless the caller supplies an ERC-1271
|
|
13
|
+
* attestation (ks256-attestation.ts) bound to this root, this digest, this
|
|
14
|
+
* signature and the forest's genesis chain.
|
|
15
|
+
*
|
|
16
|
+
* No network and no injected callback: the chain read is a separate helper's
|
|
17
|
+
* job (forestrie-cli, mcp-resolve), as for the known accumulator.
|
|
18
|
+
*/
|
|
19
|
+
import { coseUnprotectedToMap, decodeCoseSign1 } from "@forestrie/encoding";
|
|
20
|
+
import { extractKs256CertificateDigest, verifyDelegationCertificateKs256, } from "@forestrie/delegation-cose";
|
|
21
|
+
import { keccak_256 } from "@noble/hashes/sha3";
|
|
22
|
+
import { ERC1271_MAGIC_VALUE, } from "./ks256-attestation.js";
|
|
23
|
+
/** Unprotected header label carrying the delegation certificate. */
|
|
24
|
+
const DELEGATION_CERT_LABEL = 1000;
|
|
25
|
+
/**
|
|
26
|
+
* Check a delegation certificate against a KS256 root address.
|
|
27
|
+
*
|
|
28
|
+
* @param certBytes - The receipt's label-1000 COSE_Sign1 certificate.
|
|
29
|
+
* @param rootAddress - 20-byte root from genesis or a caller-known log key.
|
|
30
|
+
* @param evidence - Optional ERC-1271 attestation and the genesis chain id.
|
|
31
|
+
* @returns `ok`, or a stable reason: `delegation_not_ks256` (wrong alg,
|
|
32
|
+
* WebAuthn envelope or malformed), `ks256_root_not_recovered`, or a
|
|
33
|
+
* `ks256_attestation_*` mismatch.
|
|
34
|
+
*/
|
|
35
|
+
export async function verifyKs256DelegationCert(certBytes, rootAddress, evidence) {
|
|
36
|
+
let parts;
|
|
37
|
+
try {
|
|
38
|
+
parts = extractKs256CertificateDigest(certBytes);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return { ok: false, reason: "delegation_not_ks256" };
|
|
42
|
+
}
|
|
43
|
+
if (await verifyDelegationCertificateKs256(certBytes, rootAddress)) {
|
|
44
|
+
return { ok: true };
|
|
45
|
+
}
|
|
46
|
+
const { attestation, forestChainId } = evidence;
|
|
47
|
+
if (!attestation) {
|
|
48
|
+
return { ok: false, reason: "ks256_root_not_recovered" };
|
|
49
|
+
}
|
|
50
|
+
if (!bytesEqual(attestation.address, rootAddress)) {
|
|
51
|
+
return { ok: false, reason: "ks256_attestation_address_mismatch" };
|
|
52
|
+
}
|
|
53
|
+
if (!bytesEqual(attestation.digest, parts.digest)) {
|
|
54
|
+
return { ok: false, reason: "ks256_attestation_digest_mismatch" };
|
|
55
|
+
}
|
|
56
|
+
if (!bytesEqual(attestation.signatureHash, keccak_256(parts.signature))) {
|
|
57
|
+
return { ok: false, reason: "ks256_attestation_signature_mismatch" };
|
|
58
|
+
}
|
|
59
|
+
if (forestChainId === undefined) {
|
|
60
|
+
return { ok: false, reason: "ks256_attestation_chain_unbound" };
|
|
61
|
+
}
|
|
62
|
+
if (attestation.chainId !== forestChainId) {
|
|
63
|
+
return { ok: false, reason: "ks256_attestation_chain_mismatch" };
|
|
64
|
+
}
|
|
65
|
+
if (!bytesEqual(attestation.magic, ERC1271_MAGIC_VALUE)) {
|
|
66
|
+
return { ok: false, reason: "ks256_attestation_magic_invalid" };
|
|
67
|
+
}
|
|
68
|
+
return { ok: true };
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Extract the KS256 delegation certificate's root address, digest and
|
|
72
|
+
* signature from a receipt: what an ERC-1271 attestation helper calls
|
|
73
|
+
* `isValidSignature` with. Does not verify anything.
|
|
74
|
+
*
|
|
75
|
+
* @param receiptCbor - COSE_Sign1 receipt bytes.
|
|
76
|
+
* @returns `null` when the receipt carries no label-1000 certificate.
|
|
77
|
+
* @throws When the certificate is not a KS256 certificate.
|
|
78
|
+
*/
|
|
79
|
+
export function extractKs256DelegationDigest(receiptCbor) {
|
|
80
|
+
const decoded = decodeCoseSign1(receiptCbor);
|
|
81
|
+
if (!decoded)
|
|
82
|
+
throw new Error("receipt is not a COSE_Sign1");
|
|
83
|
+
const certRaw = coseUnprotectedToMap(decoded.unprotected).get(DELEGATION_CERT_LABEL);
|
|
84
|
+
if (!(certRaw instanceof Uint8Array) || certRaw.length === 0)
|
|
85
|
+
return null;
|
|
86
|
+
return extractKs256CertificateDigest(certRaw);
|
|
87
|
+
}
|
|
88
|
+
function bytesEqual(a, b) {
|
|
89
|
+
if (a.length !== b.length)
|
|
90
|
+
return false;
|
|
91
|
+
let x = 0;
|
|
92
|
+
for (let i = 0; i < a.length; i++)
|
|
93
|
+
x |= a[i] ^ b[i];
|
|
94
|
+
return x === 0;
|
|
95
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forestrie/receipt-verify",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Offline SCITT grant receipt verification (ADR-0045)",
|
|
@@ -28,11 +28,13 @@
|
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@noble/hashes": "^1.7.1",
|
|
31
|
-
"@forestrie/
|
|
31
|
+
"@forestrie/delegation-cose": "0.4.0",
|
|
32
32
|
"@forestrie/encoding": "0.9.1",
|
|
33
|
-
"@forestrie/merklelog": "0.5.0"
|
|
33
|
+
"@forestrie/merklelog": "0.5.0",
|
|
34
|
+
"@forestrie/chain-rpc": "0.3.0"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
37
|
+
"@noble/curves": "^1.9.7",
|
|
36
38
|
"esbuild": "^0.24.0",
|
|
37
39
|
"typescript": "^5.9.2",
|
|
38
40
|
"vitest": "^3.2.4"
|
|
@@ -44,7 +44,16 @@ function parseAlgInt(raw: unknown): number | null {
|
|
|
44
44
|
return null;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Wrap a 20-byte KS256 root address as a {@link ParsedKs256RootKey}.
|
|
49
|
+
*
|
|
50
|
+
* @throws When `address` is not 20 bytes: a key of any other length would
|
|
51
|
+
* fail {@link isParsedKs256RootKey} and be mistaken for an ES256 key.
|
|
52
|
+
*/
|
|
53
|
+
export function ks256RootFromAddress(address: Uint8Array): ParsedKs256RootKey {
|
|
54
|
+
if (address.length !== 20) {
|
|
55
|
+
throw new Error("KS256 root address must be 20 bytes");
|
|
56
|
+
}
|
|
48
57
|
return { kind: "KS256", alg: COSE_ALG_KS256, address };
|
|
49
58
|
}
|
|
50
59
|
|
package/src/index.ts
CHANGED
|
@@ -89,6 +89,29 @@ export type {
|
|
|
89
89
|
} from "./resolve-delegated-verify-key.js";
|
|
90
90
|
/** Import a raw 64-byte x||y P-256 public key as an ES256 verify key. */
|
|
91
91
|
export { importEs256PublicKeyFromGrantDataXy64 } from "./decode-trust-root-cbor.js";
|
|
92
|
+
/**
|
|
93
|
+
* KS256 roots in offline verification (plan-2610-03 D4–D6, protocol ADR-0045
|
|
94
|
+
* amendment 2026-10-18). A KS256 root comes only from genesis, a caller-known
|
|
95
|
+
* log key ({@link knownLogKeyFromBytes}: asserts, does not prove) or, for a
|
|
96
|
+
* contract root, a caller-supplied ERC-1271 attestation; never from the
|
|
97
|
+
* delegation coordinator. EOA roots verify by ecrecover; anything else needs
|
|
98
|
+
* an attestation or fails with `ks256_root_not_recovered`.
|
|
99
|
+
*/
|
|
100
|
+
export { knownLogKeyFromBytes } from "./known-log-key.js";
|
|
101
|
+
export { ks256RootFromAddress } from "./decode-trust-root-cbor.js";
|
|
102
|
+
export type { ParsedKs256RootKey, RootVerifyKey } from "./root-verify-key.js";
|
|
103
|
+
export {
|
|
104
|
+
ERC1271_MAGIC_VALUE,
|
|
105
|
+
decodeKs256Attestation,
|
|
106
|
+
encodeKs256Attestation,
|
|
107
|
+
type Ks256Attestation,
|
|
108
|
+
} from "./ks256-attestation.js";
|
|
109
|
+
export {
|
|
110
|
+
extractKs256DelegationDigest,
|
|
111
|
+
verifyKs256DelegationCert,
|
|
112
|
+
} from "./verify-ks256-delegation.js";
|
|
113
|
+
export type { Ks256DelegationEvidence } from "./verify-ks256-delegation.js";
|
|
114
|
+
export type { Ks256CertificateDigest } from "@forestrie/delegation-cose";
|
|
92
115
|
/**
|
|
93
116
|
* Passkey session-key endorsement v2 (ADR-0064 as amended by ADR-0065):
|
|
94
117
|
* build, assemble, and verify the artifact chaining a passkey log root to the
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Caller-known log key bytes → trust key (FOR-297 "known log key"; plan-2610-03
|
|
3
|
+
* D6). The bytes alone decide the type, as univocity's
|
|
4
|
+
* `GrantDataInvalidKeyLength` and arbor's `grantDataIdentity` do: 64 bytes is
|
|
5
|
+
* an ES256 x||y public key, 20 bytes a KS256 address, anything else is
|
|
6
|
+
* refused. Either way the caller ASSERTS, and does not prove, that the key
|
|
7
|
+
* owns the log — see `verifyReceiptOfflineWithKeys`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
importEs256PublicKeyFromGrantDataXy64,
|
|
12
|
+
ks256RootFromAddress,
|
|
13
|
+
} from "./decode-trust-root-cbor.js";
|
|
14
|
+
import type { RootVerifyKey } from "./root-verify-key.js";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param bytes - 64-byte ES256 x||y or 20-byte KS256 address.
|
|
18
|
+
* @throws When the length is neither.
|
|
19
|
+
*/
|
|
20
|
+
export async function knownLogKeyFromBytes(
|
|
21
|
+
bytes: Uint8Array,
|
|
22
|
+
): Promise<RootVerifyKey> {
|
|
23
|
+
if (bytes.length === 64) return importEs256PublicKeyFromGrantDataXy64(bytes);
|
|
24
|
+
if (bytes.length === 20) return ks256RootFromAddress(bytes);
|
|
25
|
+
throw new Error(
|
|
26
|
+
`known log key must be 64 bytes (ES256 x||y) or 20 bytes (KS256 address), got ${bytes.length}`,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ERC-1271 attestation (plan-2610-03 D4, protocol ADR-0045 amendment
|
|
3
|
+
* 2026-10-18): a caller-supplied record that a contract KS256 root (a Safe)
|
|
4
|
+
* answered `isValidSignature(digest, signature)` with the ERC-1271 magic value
|
|
5
|
+
* at a given block. It lets a contract-rooted delegation certificate verify
|
|
6
|
+
* offline with the narrower guarantee "valid under the contract's signers at
|
|
7
|
+
* the attested block".
|
|
8
|
+
*
|
|
9
|
+
* Same trust model as the known-accumulator snapshot (known-accumulator.ts):
|
|
10
|
+
* the record is the fetching party's chain read, made explicit and auditable.
|
|
11
|
+
* It binds `(chainId, address, digest, signature hash, block)`, so anyone with
|
|
12
|
+
* RPC can re-run exactly that call at that block (the signature itself is in
|
|
13
|
+
* the receipt's certificate) and confirm or disprove it. This package never makes
|
|
14
|
+
* that call; forestrie-cli and mcp-resolve fetch it (slice 04).
|
|
15
|
+
*
|
|
16
|
+
* The verifier accepts an attestation only for the exact root address and
|
|
17
|
+
* certificate digest, and only when `chainId` equals the forest's genesis
|
|
18
|
+
* chain: the same Safe address on another chain can have other owners (D6).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
decodeCborDeterministic,
|
|
23
|
+
encodeCborDeterministic,
|
|
24
|
+
} from "@forestrie/encoding";
|
|
25
|
+
|
|
26
|
+
/** ERC-1271 `isValidSignature` success value, `bytes4(0x1626ba7e)`. */
|
|
27
|
+
export const ERC1271_MAGIC_VALUE = new Uint8Array([0x16, 0x26, 0xba, 0x7e]);
|
|
28
|
+
|
|
29
|
+
/** CBOR map labels for the attestation artifact (strict RFC 8949 §4.2). */
|
|
30
|
+
const LABEL_VERSION = 1;
|
|
31
|
+
const LABEL_CHAIN_ID = 2;
|
|
32
|
+
const LABEL_ADDRESS = 3;
|
|
33
|
+
const LABEL_DIGEST = 4;
|
|
34
|
+
const LABEL_BLOCK_NUMBER = 5;
|
|
35
|
+
const LABEL_BLOCK_HASH = 6;
|
|
36
|
+
const LABEL_MAGIC = 7;
|
|
37
|
+
const LABEL_SIGNATURE_HASH = 8;
|
|
38
|
+
|
|
39
|
+
const KNOWN_LABELS = new Set<number>([
|
|
40
|
+
LABEL_VERSION,
|
|
41
|
+
LABEL_CHAIN_ID,
|
|
42
|
+
LABEL_ADDRESS,
|
|
43
|
+
LABEL_DIGEST,
|
|
44
|
+
LABEL_BLOCK_NUMBER,
|
|
45
|
+
LABEL_BLOCK_HASH,
|
|
46
|
+
LABEL_MAGIC,
|
|
47
|
+
LABEL_SIGNATURE_HASH,
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
const ATTESTATION_VERSION = 1;
|
|
51
|
+
|
|
52
|
+
/** A block-bound ERC-1271 `isValidSignature` result for one digest. */
|
|
53
|
+
export type Ks256Attestation = {
|
|
54
|
+
version: number;
|
|
55
|
+
/** Chain the call was made on; must equal the forest's genesis chain. */
|
|
56
|
+
chainId: bigint;
|
|
57
|
+
/** Contract root address (20 bytes) the call was made to. */
|
|
58
|
+
address: Uint8Array;
|
|
59
|
+
/** keccak256(Sig_structure) of the delegation certificate (32 bytes). */
|
|
60
|
+
digest: Uint8Array;
|
|
61
|
+
/**
|
|
62
|
+
* keccak256 of the signature bytes `isValidSignature` was called with
|
|
63
|
+
* (32 bytes); must match the certificate's signature.
|
|
64
|
+
*/
|
|
65
|
+
signatureHash: Uint8Array;
|
|
66
|
+
/** Block the call was made at. */
|
|
67
|
+
blockNumber: bigint;
|
|
68
|
+
/** Hash of that block (32 bytes): the falsifiability handle. */
|
|
69
|
+
blockHash: Uint8Array;
|
|
70
|
+
/** The call's 4-byte return; only {@link ERC1271_MAGIC_VALUE} attests. */
|
|
71
|
+
magic: Uint8Array;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** Encode an attestation as canonical CBOR (RFC 8949 §4.2, hard policy). */
|
|
75
|
+
export function encodeKs256Attestation(
|
|
76
|
+
attestation: Ks256Attestation,
|
|
77
|
+
): Uint8Array {
|
|
78
|
+
return encodeCborDeterministic(
|
|
79
|
+
new Map<number, unknown>([
|
|
80
|
+
[LABEL_VERSION, attestation.version],
|
|
81
|
+
[LABEL_CHAIN_ID, attestation.chainId],
|
|
82
|
+
[LABEL_ADDRESS, attestation.address],
|
|
83
|
+
[LABEL_DIGEST, attestation.digest],
|
|
84
|
+
[LABEL_BLOCK_NUMBER, attestation.blockNumber],
|
|
85
|
+
[LABEL_BLOCK_HASH, attestation.blockHash],
|
|
86
|
+
[LABEL_MAGIC, attestation.magic],
|
|
87
|
+
[LABEL_SIGNATURE_HASH, attestation.signatureHash],
|
|
88
|
+
]),
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function asBigint(v: unknown, what: string): bigint {
|
|
93
|
+
if (typeof v === "bigint") return v;
|
|
94
|
+
if (typeof v === "number" && Number.isSafeInteger(v) && v >= 0)
|
|
95
|
+
return BigInt(v);
|
|
96
|
+
throw new Error(`ks256 attestation: ${what} must be an unsigned integer`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function asBytes(v: unknown, length: number, what: string): Uint8Array {
|
|
100
|
+
if (!(v instanceof Uint8Array) || v.length !== length) {
|
|
101
|
+
throw new Error(`ks256 attestation: ${what} must be ${length} bytes`);
|
|
102
|
+
}
|
|
103
|
+
return v;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Strict decode and shape validation of an attestation artifact: canonical
|
|
108
|
+
* CBOR, known labels only, version 1, exact types and lengths.
|
|
109
|
+
*/
|
|
110
|
+
export function decodeKs256Attestation(bytes: Uint8Array): Ks256Attestation {
|
|
111
|
+
let decoded: unknown;
|
|
112
|
+
try {
|
|
113
|
+
decoded = decodeCborDeterministic(bytes);
|
|
114
|
+
} catch (err) {
|
|
115
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
116
|
+
throw new Error(`ks256 attestation is not canonical CBOR: ${message}`);
|
|
117
|
+
}
|
|
118
|
+
if (!(decoded instanceof Map)) {
|
|
119
|
+
throw new Error("ks256 attestation must be a CBOR map");
|
|
120
|
+
}
|
|
121
|
+
for (const label of decoded.keys()) {
|
|
122
|
+
if (!KNOWN_LABELS.has(label as number)) {
|
|
123
|
+
throw new Error(`ks256 attestation: unknown label ${String(label)}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const version = Number(asBigint(decoded.get(LABEL_VERSION), "version"));
|
|
127
|
+
if (version !== ATTESTATION_VERSION) {
|
|
128
|
+
throw new Error(`ks256 attestation version ${version} not supported`);
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
version,
|
|
132
|
+
chainId: asBigint(decoded.get(LABEL_CHAIN_ID), "chainId"),
|
|
133
|
+
address: asBytes(decoded.get(LABEL_ADDRESS), 20, "address"),
|
|
134
|
+
digest: asBytes(decoded.get(LABEL_DIGEST), 32, "digest"),
|
|
135
|
+
blockNumber: asBigint(decoded.get(LABEL_BLOCK_NUMBER), "blockNumber"),
|
|
136
|
+
blockHash: asBytes(decoded.get(LABEL_BLOCK_HASH), 32, "blockHash"),
|
|
137
|
+
magic: asBytes(decoded.get(LABEL_MAGIC), 4, "magic"),
|
|
138
|
+
signatureHash: asBytes(
|
|
139
|
+
decoded.get(LABEL_SIGNATURE_HASH),
|
|
140
|
+
32,
|
|
141
|
+
"signatureHash",
|
|
142
|
+
),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
@@ -6,12 +6,13 @@
|
|
|
6
6
|
* trust root and returns the delegated public key as an additional verify
|
|
7
7
|
* candidate — the offline port of canopy-api's `resolveReceiptVerifyKey`.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
9
|
+
* Sealer delegate keys are always secp256r1. A P-256 root's certificate is
|
|
10
|
+
* checked here; a passkey (WebAuthn) root signs the certificate with alg
|
|
11
|
+
* -65800 and the ADR-0063 unprotected-header envelope, which
|
|
12
|
+
* `verifyCoseSign1WithParsedKey` verifies against the same P-256 root keys
|
|
13
|
+
* (plan-2608-13 1.4). A KS256 root's certificate is checked by
|
|
14
|
+
* {@link verifyKs256DelegationCert}: ecrecover for an EOA, or a caller-supplied
|
|
15
|
+
* ERC-1271 attestation for a contract (plan-2610-03 D4).
|
|
15
16
|
*
|
|
16
17
|
* Trust anchors (FOR-297 trust ladder): `rootKeys` need not be genesis-derived
|
|
17
18
|
* — a caller-known log OWNER key works identically (see
|
|
@@ -41,6 +42,11 @@ import {
|
|
|
41
42
|
decodeCoseSign1,
|
|
42
43
|
verifyCoseSign1WithParsedKey,
|
|
43
44
|
} from "@forestrie/encoding";
|
|
45
|
+
import { isParsedKs256RootKey, type RootVerifyKey } from "./root-verify-key.js";
|
|
46
|
+
import {
|
|
47
|
+
verifyKs256DelegationCert,
|
|
48
|
+
type Ks256DelegationEvidence,
|
|
49
|
+
} from "./verify-ks256-delegation.js";
|
|
44
50
|
|
|
45
51
|
/** Unprotected header label carrying the delegation certificate. */
|
|
46
52
|
const DELEGATION_CERT_LABEL = 1000;
|
|
@@ -104,7 +110,8 @@ export type DelegationConstraints = {
|
|
|
104
110
|
* `constraints` carries the cert's coverage/validity window when the payload
|
|
105
111
|
* declares it (null for legacy certs that predate labels 3/4/8/9).
|
|
106
112
|
* broken: a cert is present but did not verify under the root, or its delegated
|
|
107
|
-
* key could not be parsed — the delegation chain is invalid.
|
|
113
|
+
* key could not be parsed — the delegation chain is invalid. `reason` is set
|
|
114
|
+
* when a KS256 root gives a more specific one than `delegation_invalid`.
|
|
108
115
|
*/
|
|
109
116
|
export type DelegatedResolution =
|
|
110
117
|
| { kind: "root-only" }
|
|
@@ -113,7 +120,7 @@ export type DelegatedResolution =
|
|
|
113
120
|
delegatedKey: CryptoKey;
|
|
114
121
|
constraints: DelegationConstraints | null;
|
|
115
122
|
}
|
|
116
|
-
| { kind: "broken" };
|
|
123
|
+
| { kind: "broken"; reason?: string };
|
|
117
124
|
|
|
118
125
|
function extractDelegationCertBytes(unprotected: unknown): Uint8Array | null {
|
|
119
126
|
const umap = coseUnprotectedToMap(unprotected);
|
|
@@ -262,11 +269,16 @@ export function checkDelegationConstraints(
|
|
|
262
269
|
* Resolve the delegated verify key from a receipt's delegation certificate.
|
|
263
270
|
*
|
|
264
271
|
* @param receiptCbor - the COSE_Sign1 receipt bytes.
|
|
265
|
-
* @param rootKeys -
|
|
272
|
+
* @param rootKeys - trust roots: ES256 keys and KS256 addresses, from genesis
|
|
273
|
+
* or caller-known log keys. A KS256 root never comes from the coordinator
|
|
274
|
+
* (its public roots are last-write-wins, FOR-601/FOR-603).
|
|
275
|
+
* @param ks256Evidence - optional ERC-1271 attestation for a KS256 contract
|
|
276
|
+
* root, with the forest's genesis chain id it must match.
|
|
266
277
|
*/
|
|
267
278
|
export async function resolveDelegatedVerifyKey(
|
|
268
279
|
receiptCbor: Uint8Array,
|
|
269
|
-
rootKeys:
|
|
280
|
+
rootKeys: RootVerifyKey[],
|
|
281
|
+
ks256Evidence: Ks256DelegationEvidence = {},
|
|
270
282
|
): Promise<DelegatedResolution> {
|
|
271
283
|
const decoded = decodeCoseSign1(receiptCbor);
|
|
272
284
|
if (!decoded) return { kind: "root-only" };
|
|
@@ -275,7 +287,21 @@ export async function resolveDelegatedVerifyKey(
|
|
|
275
287
|
if (!certBytes) return { kind: "root-only" };
|
|
276
288
|
|
|
277
289
|
let verified = false;
|
|
290
|
+
let ks256Reason: string | undefined;
|
|
278
291
|
for (const rootKey of rootKeys) {
|
|
292
|
+
if (isParsedKs256RootKey(rootKey)) {
|
|
293
|
+
const check = await verifyKs256DelegationCert(
|
|
294
|
+
certBytes,
|
|
295
|
+
rootKey.address,
|
|
296
|
+
ks256Evidence,
|
|
297
|
+
);
|
|
298
|
+
if (check.ok) {
|
|
299
|
+
verified = true;
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
ks256Reason ??= check.reason;
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
279
305
|
if (
|
|
280
306
|
await verifyCoseSign1WithParsedKey(certBytes, rootKey, {
|
|
281
307
|
logPrefix: "delegation-cert-offline",
|
|
@@ -285,7 +311,7 @@ export async function resolveDelegatedVerifyKey(
|
|
|
285
311
|
break;
|
|
286
312
|
}
|
|
287
313
|
}
|
|
288
|
-
if (!verified) return { kind: "broken" };
|
|
314
|
+
if (!verified) return { kind: "broken", reason: ks256Reason };
|
|
289
315
|
|
|
290
316
|
const certDecoded = decodeCoseSign1(certBytes);
|
|
291
317
|
if (!certDecoded) return { kind: "broken" };
|