@provethebuy/verify 0.0.0-stage → 0.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/LICENSE +202 -0
- package/README.md +43 -2
- package/dist/bodyObjects.d.ts +28 -0
- package/dist/bodyObjects.js +47 -0
- package/dist/bodyObjects.js.map +1 -0
- package/dist/cloud.d.ts +42 -0
- package/dist/cloud.js +102 -0
- package/dist/cloud.js.map +1 -0
- package/dist/confirmation.d.ts +17 -0
- package/dist/confirmation.js +31 -0
- package/dist/confirmation.js.map +1 -0
- package/dist/evidence.d.ts +39 -0
- package/dist/evidence.js +96 -0
- package/dist/evidence.js.map +1 -0
- package/dist/express.d.ts +43 -0
- package/dist/express.js +72 -0
- package/dist/express.js.map +1 -0
- package/dist/httpsig.d.ts +58 -0
- package/dist/httpsig.js +203 -0
- package/dist/httpsig.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/jws.d.ts +28 -0
- package/dist/jws.js +93 -0
- package/dist/jws.js.map +1 -0
- package/dist/keys.d.ts +61 -0
- package/dist/keys.js +76 -0
- package/dist/keys.js.map +1 -0
- package/dist/ledger.d.ts +102 -0
- package/dist/ledger.js +131 -0
- package/dist/ledger.js.map +1 -0
- package/dist/mandates/ap2.d.ts +31 -0
- package/dist/mandates/ap2.js +91 -0
- package/dist/mandates/ap2.js.map +1 -0
- package/dist/mandates/index.d.ts +25 -0
- package/dist/mandates/index.js +26 -0
- package/dist/mandates/index.js.map +1 -0
- package/dist/mandates/verifiableIntent.d.ts +20 -0
- package/dist/mandates/verifiableIntent.js +128 -0
- package/dist/mandates/verifiableIntent.js.map +1 -0
- package/dist/order.d.ts +9 -0
- package/dist/order.js +32 -0
- package/dist/order.js.map +1 -0
- package/dist/payment.d.ts +22 -0
- package/dist/payment.js +19 -0
- package/dist/payment.js.map +1 -0
- package/dist/policy.d.ts +39 -0
- package/dist/policy.js +29 -0
- package/dist/policy.js.map +1 -0
- package/dist/replay.d.ts +46 -0
- package/dist/replay.js +61 -0
- package/dist/replay.js.map +1 -0
- package/dist/reputation.d.ts +16 -0
- package/dist/reputation.js +46 -0
- package/dist/reputation.js.map +1 -0
- package/dist/testing.d.ts +53 -0
- package/dist/testing.js +100 -0
- package/dist/testing.js.map +1 -0
- package/dist/types.d.ts +72 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/util.d.ts +15 -0
- package/dist/util.js +59 -0
- package/dist/util.js.map +1 -0
- package/dist/verify.d.ts +50 -0
- package/dist/verify.js +309 -0
- package/dist/verify.js.map +1 -0
- package/package.json +63 -4
package/dist/jws.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/** Compact JWS and SD-JWT helpers (EdDSA, ES256, PS256). */
|
|
2
|
+
import { createPrivateKey, KeyObject } from "node:crypto";
|
|
3
|
+
import { b64url, fromB64url, sha256B64url } from "./util.js";
|
|
4
|
+
import { importJwk } from "./keys.js";
|
|
5
|
+
import { signBytes, verifyBytes } from "./httpsig.js";
|
|
6
|
+
const JOSE_TO_ALG = { EdDSA: "ed25519", ES256: "ecdsa-p256-sha256", PS256: "ps256" };
|
|
7
|
+
export function joseAlgFor(jwk) {
|
|
8
|
+
if (jwk.kty === "OKP" && jwk.crv === "Ed25519")
|
|
9
|
+
return "EdDSA";
|
|
10
|
+
if (jwk.kty === "EC" && jwk.crv === "P-256")
|
|
11
|
+
return "ES256";
|
|
12
|
+
if (jwk.kty === "RSA")
|
|
13
|
+
return "PS256";
|
|
14
|
+
throw new Error(`Unsupported key type ${jwk.kty}`);
|
|
15
|
+
}
|
|
16
|
+
export function signJws(payload, privateJwk, extraHeader = {}) {
|
|
17
|
+
const alg = joseAlgFor(privateJwk);
|
|
18
|
+
const header = { alg, typ: "JWT", ...(privateJwk.kid ? { kid: privateJwk.kid } : {}), ...extraHeader };
|
|
19
|
+
const input = `${b64url(JSON.stringify(header))}.${b64url(JSON.stringify(payload))}`;
|
|
20
|
+
const pk = createPrivateKey({ key: privateJwk, format: "jwk" });
|
|
21
|
+
const sig = signBytes(JOSE_TO_ALG[alg], pk, Buffer.from(input));
|
|
22
|
+
return `${input}.${b64url(sig)}`;
|
|
23
|
+
}
|
|
24
|
+
export function decodeJws(jws) {
|
|
25
|
+
const parts = jws.split(".");
|
|
26
|
+
if (parts.length !== 3)
|
|
27
|
+
throw new Error("JWS must have three parts");
|
|
28
|
+
return { header: JSON.parse(fromB64url(parts[0]).toString()), payload: JSON.parse(fromB64url(parts[1]).toString()) };
|
|
29
|
+
}
|
|
30
|
+
export function verifyJws(jws, key) {
|
|
31
|
+
const parts = jws.split(".");
|
|
32
|
+
if (parts.length !== 3)
|
|
33
|
+
throw new Error("JWS must have three parts");
|
|
34
|
+
const header = JSON.parse(fromB64url(parts[0]).toString());
|
|
35
|
+
const alg = JOSE_TO_ALG[String(header.alg)];
|
|
36
|
+
if (!alg)
|
|
37
|
+
throw new Error(`Unsupported JWS alg ${String(header.alg)}`);
|
|
38
|
+
const keyObj = key instanceof KeyObject ? key : importJwk(key);
|
|
39
|
+
const ok = verifyBytes(alg, keyObj, Buffer.from(`${parts[0]}.${parts[1]}`), fromB64url(parts[2]));
|
|
40
|
+
if (!ok)
|
|
41
|
+
throw new Error("JWS signature does not verify");
|
|
42
|
+
return { header, payload: JSON.parse(fromB64url(parts[1]).toString()) };
|
|
43
|
+
}
|
|
44
|
+
export function splitSdJwt(sdjwt) {
|
|
45
|
+
const parts = sdjwt.split("~");
|
|
46
|
+
return { jwt: parts[0], disclosures: parts.slice(1).filter(Boolean) };
|
|
47
|
+
}
|
|
48
|
+
/** Create a disclosure for one claim: base64url(JSON [salt, name, value]). */
|
|
49
|
+
export function makeDisclosure(name, value, salt) {
|
|
50
|
+
return b64url(JSON.stringify([salt, name, value]));
|
|
51
|
+
}
|
|
52
|
+
export function disclosureDigest(disclosure) {
|
|
53
|
+
return sha256B64url(Buffer.from(disclosure, "ascii"));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Issue an SD-JWT: the listed claims are moved behind disclosures and replaced by their digests in _sd.
|
|
57
|
+
*/
|
|
58
|
+
export function issueSdJwt(claims, selective, privateJwk, saltFn) {
|
|
59
|
+
const payload = { _sd_alg: "sha-256" };
|
|
60
|
+
const disclosures = [];
|
|
61
|
+
const sd = [];
|
|
62
|
+
for (const [k, v] of Object.entries(claims)) {
|
|
63
|
+
if (selective.includes(k)) {
|
|
64
|
+
const d = makeDisclosure(k, v, saltFn(k));
|
|
65
|
+
disclosures.push(d);
|
|
66
|
+
sd.push(disclosureDigest(d));
|
|
67
|
+
}
|
|
68
|
+
else
|
|
69
|
+
payload[k] = v;
|
|
70
|
+
}
|
|
71
|
+
if (sd.length)
|
|
72
|
+
payload._sd = sd.sort();
|
|
73
|
+
return [signJws(payload, privateJwk, { typ: "sd+jwt" }), ...disclosures].join("~") + "~";
|
|
74
|
+
}
|
|
75
|
+
/** Verify issuer signature and reveal disclosed claims. Rejects disclosures not committed in _sd. */
|
|
76
|
+
export function verifySdJwt(sdjwt, key) {
|
|
77
|
+
const { jwt, disclosures } = splitSdJwt(sdjwt);
|
|
78
|
+
const { header, payload } = verifyJws(jwt, key);
|
|
79
|
+
const committed = new Set(Array.isArray(payload._sd) ? payload._sd : []);
|
|
80
|
+
const claims = { ...payload };
|
|
81
|
+
delete claims._sd;
|
|
82
|
+
delete claims._sd_alg;
|
|
83
|
+
for (const d of disclosures) {
|
|
84
|
+
if (!committed.has(disclosureDigest(d)))
|
|
85
|
+
throw new Error("Disclosure is not committed in the signed _sd list");
|
|
86
|
+
const arr = JSON.parse(fromB64url(d).toString());
|
|
87
|
+
if (!Array.isArray(arr) || arr.length !== 3)
|
|
88
|
+
throw new Error("Malformed disclosure");
|
|
89
|
+
claims[String(arr[1])] = arr[2];
|
|
90
|
+
}
|
|
91
|
+
return { header, claims };
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=jws.js.map
|
package/dist/jws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AE,MAAM,EAAE,OAAO,EAAE,GAAG,SAAS,CAA0B,GAAG,EAAE,GAAG,CAAC,CAAC;IACzE,MAAM,SAAS,GAAG,IAAI,GAAG,CAAS,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAE,OAAO,CAAC,GAAgB,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAC/F,MAAM,MAAM,GAA4B,EAAE,GAAG,OAAO,EAAE,CAAC;IACvD,OAAO,MAAM,CAAC,GAAG,CAAC;IAClB,OAAO,MAAM,CAAC,OAAO,CAAC;IACtB,KAAK,MAAM,CAAC,IAAI,WAAW,EAAE,CAAC;QAC5B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;QAC/G,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAc,CAAC;QAC9D,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QACrF,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AAC5B,CAAC"}
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { KeyObject } from "node:crypto";
|
|
2
|
+
export interface Jwk {
|
|
3
|
+
kty: string;
|
|
4
|
+
kid?: string;
|
|
5
|
+
alg?: string;
|
|
6
|
+
use?: string;
|
|
7
|
+
crv?: string;
|
|
8
|
+
x?: string;
|
|
9
|
+
y?: string;
|
|
10
|
+
n?: string;
|
|
11
|
+
e?: string;
|
|
12
|
+
[k: string]: unknown;
|
|
13
|
+
}
|
|
14
|
+
export interface Jwks {
|
|
15
|
+
keys: Jwk[];
|
|
16
|
+
}
|
|
17
|
+
export interface ResolvedKey {
|
|
18
|
+
kid: string;
|
|
19
|
+
jwk: Jwk;
|
|
20
|
+
key: KeyObject;
|
|
21
|
+
/** Directory the key came from, e.g. "visa" or "demo". */
|
|
22
|
+
source: string;
|
|
23
|
+
/** SHA-256 of the canonical public JWK. Stored with evidence so the key can be re-checked after rotation. */
|
|
24
|
+
snapshotSha256: string;
|
|
25
|
+
}
|
|
26
|
+
export interface DirectorySource {
|
|
27
|
+
/** Short name stored with evidence, e.g. "visa", "mastercard", "demo". */
|
|
28
|
+
name: string;
|
|
29
|
+
/** Inline key set (tests, self-hosted directories, pinned keys). */
|
|
30
|
+
jwks?: Jwks;
|
|
31
|
+
/** Remote JWKS URL, fetched and cached. */
|
|
32
|
+
url?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface KeyDirectoryOptions {
|
|
35
|
+
sources: DirectorySource[];
|
|
36
|
+
/** Cache lifetime for remote key sets. Default 10 minutes. */
|
|
37
|
+
ttlMs?: number;
|
|
38
|
+
/** Custom fetch (tests, proxies). Defaults to global fetch. */
|
|
39
|
+
fetch?: typeof fetch;
|
|
40
|
+
}
|
|
41
|
+
/** Public JWK fields only; private members are stripped before hashing or storing. */
|
|
42
|
+
export declare function publicJwk(jwk: Jwk): Jwk;
|
|
43
|
+
export declare function jwkSnapshot(jwk: Jwk): string;
|
|
44
|
+
export declare function importJwk(jwk: Jwk): KeyObject;
|
|
45
|
+
/**
|
|
46
|
+
* Resolves agent public keys by keyid from one or more directories.
|
|
47
|
+
* Remote directories are cached; every resolved key carries a snapshot hash for evidence.
|
|
48
|
+
*/
|
|
49
|
+
export declare class KeyDirectory {
|
|
50
|
+
private readonly sources;
|
|
51
|
+
private readonly ttlMs;
|
|
52
|
+
private readonly fetchImpl;
|
|
53
|
+
private readonly cache;
|
|
54
|
+
constructor(opts: KeyDirectoryOptions);
|
|
55
|
+
get sourceNames(): string[];
|
|
56
|
+
private load;
|
|
57
|
+
/** Find a key by keyid. Optionally restrict to named directories. */
|
|
58
|
+
resolve(kid: string, only?: string[]): Promise<ResolvedKey | null>;
|
|
59
|
+
}
|
|
60
|
+
/** Known public directories. Mastercard's directory URL is not public yet; add it when published. */
|
|
61
|
+
export declare const KNOWN_DIRECTORIES: Record<string, string>;
|
package/dist/keys.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { createPublicKey } from "node:crypto";
|
|
2
|
+
import { canonicalize, sha256Hex } from "./util.js";
|
|
3
|
+
/** Public JWK fields only; private members are stripped before hashing or storing. */
|
|
4
|
+
export function publicJwk(jwk) {
|
|
5
|
+
const allowed = ["kty", "kid", "alg", "use", "crv", "x", "y", "n", "e"];
|
|
6
|
+
const out = { kty: jwk.kty };
|
|
7
|
+
for (const k of allowed)
|
|
8
|
+
if (jwk[k] !== undefined)
|
|
9
|
+
out[k] = jwk[k];
|
|
10
|
+
return out;
|
|
11
|
+
}
|
|
12
|
+
export function jwkSnapshot(jwk) {
|
|
13
|
+
return sha256Hex(canonicalize(publicJwk(jwk)));
|
|
14
|
+
}
|
|
15
|
+
export function importJwk(jwk) {
|
|
16
|
+
return createPublicKey({ key: publicJwk(jwk), format: "jwk" });
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Resolves agent public keys by keyid from one or more directories.
|
|
20
|
+
* Remote directories are cached; every resolved key carries a snapshot hash for evidence.
|
|
21
|
+
*/
|
|
22
|
+
export class KeyDirectory {
|
|
23
|
+
sources;
|
|
24
|
+
ttlMs;
|
|
25
|
+
fetchImpl;
|
|
26
|
+
cache = new Map();
|
|
27
|
+
constructor(opts) {
|
|
28
|
+
this.sources = opts.sources;
|
|
29
|
+
this.ttlMs = opts.ttlMs ?? 10 * 60 * 1000;
|
|
30
|
+
this.fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
31
|
+
}
|
|
32
|
+
get sourceNames() {
|
|
33
|
+
return this.sources.map((s) => s.name);
|
|
34
|
+
}
|
|
35
|
+
async load(src) {
|
|
36
|
+
if (src.jwks)
|
|
37
|
+
return src.jwks;
|
|
38
|
+
if (!src.url)
|
|
39
|
+
return { keys: [] };
|
|
40
|
+
const hit = this.cache.get(src.url);
|
|
41
|
+
if (hit && Date.now() - hit.at < this.ttlMs)
|
|
42
|
+
return hit.jwks;
|
|
43
|
+
const res = await this.fetchImpl(src.url, { headers: { accept: "application/json" } });
|
|
44
|
+
if (!res.ok)
|
|
45
|
+
throw new Error(`Key directory ${src.name} returned HTTP ${res.status}`);
|
|
46
|
+
const jwks = (await res.json());
|
|
47
|
+
if (!jwks || !Array.isArray(jwks.keys))
|
|
48
|
+
throw new Error(`Key directory ${src.name} did not return a JWKS`);
|
|
49
|
+
this.cache.set(src.url, { at: Date.now(), jwks });
|
|
50
|
+
return jwks;
|
|
51
|
+
}
|
|
52
|
+
/** Find a key by keyid. Optionally restrict to named directories. */
|
|
53
|
+
async resolve(kid, only) {
|
|
54
|
+
for (const src of this.sources) {
|
|
55
|
+
if (only && only.length && !only.includes(src.name))
|
|
56
|
+
continue;
|
|
57
|
+
let jwks;
|
|
58
|
+
try {
|
|
59
|
+
jwks = await this.load(src);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
continue; // an unreachable directory must not break the others
|
|
63
|
+
}
|
|
64
|
+
const jwk = jwks.keys.find((k) => k.kid === kid);
|
|
65
|
+
if (jwk) {
|
|
66
|
+
return { kid, jwk: publicJwk(jwk), key: importJwk(jwk), source: src.name, snapshotSha256: jwkSnapshot(jwk) };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Known public directories. Mastercard's directory URL is not public yet; add it when published. */
|
|
73
|
+
export const KNOWN_DIRECTORIES = {
|
|
74
|
+
visa: "https://mcp.visa.com/.well-known/jwks",
|
|
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
|
|
3
|
+
import type { AgentInfo, Check, Decision, MandateInfo, VerificationResult } from "./types.js";
|
|
4
|
+
import type { CheckoutContext } from "./verify.js";
|
|
5
|
+
export interface VerificationRecord {
|
|
6
|
+
type: "verification";
|
|
7
|
+
record_id: string;
|
|
8
|
+
merchant: string;
|
|
9
|
+
received_at: string;
|
|
10
|
+
request: {
|
|
11
|
+
method: string;
|
|
12
|
+
authority: string;
|
|
13
|
+
path: string;
|
|
14
|
+
raw_sha256?: string;
|
|
15
|
+
};
|
|
16
|
+
agent?: AgentInfo;
|
|
17
|
+
checks: Check[];
|
|
18
|
+
mandate?: MandateInfo;
|
|
19
|
+
order?: {
|
|
20
|
+
order_hash?: string;
|
|
21
|
+
order_id?: string;
|
|
22
|
+
amount?: number;
|
|
23
|
+
currency?: string;
|
|
24
|
+
payment_intent?: string;
|
|
25
|
+
};
|
|
26
|
+
policy_version: string;
|
|
27
|
+
decision: Decision;
|
|
28
|
+
enforced: boolean;
|
|
29
|
+
reasons: string[];
|
|
30
|
+
/** Raw material kept so the record can be re-verified months later. */
|
|
31
|
+
artifacts?: {
|
|
32
|
+
request?: HttpRequestLike;
|
|
33
|
+
key_jwk?: Jwk;
|
|
34
|
+
issuer_jwk?: Jwk;
|
|
35
|
+
context?: CheckoutContext;
|
|
36
|
+
};
|
|
37
|
+
prev_hash: string;
|
|
38
|
+
hash: string;
|
|
39
|
+
}
|
|
40
|
+
export interface DisputeRecord {
|
|
41
|
+
type: "dispute";
|
|
42
|
+
record_id: string;
|
|
43
|
+
merchant: string;
|
|
44
|
+
received_at: string;
|
|
45
|
+
dispute_id: string;
|
|
46
|
+
payment_intent?: string;
|
|
47
|
+
verification_record_id?: string;
|
|
48
|
+
agent_keyid?: string;
|
|
49
|
+
status: "opened" | "evidence_submitted" | "won" | "lost";
|
|
50
|
+
reason_code?: string;
|
|
51
|
+
amount?: number;
|
|
52
|
+
currency?: string;
|
|
53
|
+
prev_hash: string;
|
|
54
|
+
hash: string;
|
|
55
|
+
}
|
|
56
|
+
export type LedgerRecord = VerificationRecord | DisputeRecord;
|
|
57
|
+
type Unhashed<T> = Omit<T, "hash" | "prev_hash" | "record_id" | "received_at"> & {
|
|
58
|
+
received_at?: string;
|
|
59
|
+
};
|
|
60
|
+
export declare function recordHash(rec: Omit<LedgerRecord, "hash">): string;
|
|
61
|
+
export declare class EvidenceLedger {
|
|
62
|
+
protected readonly merchant: string;
|
|
63
|
+
protected records: LedgerRecord[];
|
|
64
|
+
constructor(merchant: string);
|
|
65
|
+
get size(): number;
|
|
66
|
+
all(): LedgerRecord[];
|
|
67
|
+
verifications(): VerificationRecord[];
|
|
68
|
+
disputes(): DisputeRecord[];
|
|
69
|
+
get(recordId: string): LedgerRecord | undefined;
|
|
70
|
+
findByPayment(paymentIntent: string): VerificationRecord | undefined;
|
|
71
|
+
protected persist(_rec: LedgerRecord): void;
|
|
72
|
+
private listeners;
|
|
73
|
+
/** Call `fn` after every new record (used by connectCloud). Returns a function that removes the listener. */
|
|
74
|
+
onRecord(fn: (rec: LedgerRecord) => void): () => void;
|
|
75
|
+
protected appendRaw<T extends LedgerRecord>(partial: Unhashed<T>, prefix: string): T;
|
|
76
|
+
/** Append a verification result. Pass the request and context to keep artifacts for re-verification. */
|
|
77
|
+
record(result: VerificationResult, extra: {
|
|
78
|
+
request: HttpRequestLike;
|
|
79
|
+
context?: CheckoutContext;
|
|
80
|
+
keyJwk?: Jwk;
|
|
81
|
+
orderId?: string;
|
|
82
|
+
paymentIntent?: string;
|
|
83
|
+
storeArtifacts?: boolean;
|
|
84
|
+
}): VerificationRecord;
|
|
85
|
+
recordDispute(d: Omit<Unhashed<DisputeRecord>, "type" | "merchant">): DisputeRecord;
|
|
86
|
+
/** Recompute every hash and link. Returns the first broken record, if any. */
|
|
87
|
+
verifyChain(): {
|
|
88
|
+
ok: boolean;
|
|
89
|
+
checked: number;
|
|
90
|
+
brokenAt?: string;
|
|
91
|
+
problem?: string;
|
|
92
|
+
};
|
|
93
|
+
/** Merkle root of the record hashes received on one UTC day (YYYY-MM-DD), for daily anchoring. */
|
|
94
|
+
merkleRoot(day: string): string | null;
|
|
95
|
+
}
|
|
96
|
+
/** Ledger persisted as JSON Lines on disk. Good for local use, self-hosting and shadow mode. */
|
|
97
|
+
export declare class FileLedger extends EvidenceLedger {
|
|
98
|
+
private readonly path;
|
|
99
|
+
constructor(merchant: string, path: string);
|
|
100
|
+
protected persist(rec: LedgerRecord): void;
|
|
101
|
+
}
|
|
102
|
+
export {};
|
package/dist/ledger.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evidence ledger: append-only, hash-chained records. Editing or deleting any record breaks the chain
|
|
3
|
+
* from that point, and verifyChain() says exactly where.
|
|
4
|
+
*/
|
|
5
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
6
|
+
import { dirname } from "node:path";
|
|
7
|
+
import { canonicalize, randomId, sha256Hex } from "./util.js";
|
|
8
|
+
const GENESIS = "0".repeat(64);
|
|
9
|
+
export function recordHash(rec) {
|
|
10
|
+
return sha256Hex(canonicalize(rec));
|
|
11
|
+
}
|
|
12
|
+
export class EvidenceLedger {
|
|
13
|
+
merchant;
|
|
14
|
+
records = [];
|
|
15
|
+
constructor(merchant) {
|
|
16
|
+
this.merchant = merchant;
|
|
17
|
+
}
|
|
18
|
+
get size() {
|
|
19
|
+
return this.records.length;
|
|
20
|
+
}
|
|
21
|
+
all() {
|
|
22
|
+
return [...this.records];
|
|
23
|
+
}
|
|
24
|
+
verifications() {
|
|
25
|
+
return this.records.filter((r) => r.type === "verification");
|
|
26
|
+
}
|
|
27
|
+
disputes() {
|
|
28
|
+
return this.records.filter((r) => r.type === "dispute");
|
|
29
|
+
}
|
|
30
|
+
get(recordId) {
|
|
31
|
+
return this.records.find((r) => r.record_id === recordId);
|
|
32
|
+
}
|
|
33
|
+
findByPayment(paymentIntent) {
|
|
34
|
+
return this.verifications().find((r) => r.order?.payment_intent === paymentIntent);
|
|
35
|
+
}
|
|
36
|
+
persist(_rec) { }
|
|
37
|
+
listeners = [];
|
|
38
|
+
/** Call `fn` after every new record (used by connectCloud). Returns a function that removes the listener. */
|
|
39
|
+
onRecord(fn) {
|
|
40
|
+
this.listeners.push(fn);
|
|
41
|
+
return () => {
|
|
42
|
+
this.listeners = this.listeners.filter((l) => l !== fn);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
appendRaw(partial, prefix) {
|
|
46
|
+
const prev_hash = this.records.length ? this.records[this.records.length - 1].hash : GENESIS;
|
|
47
|
+
const body = { ...partial, record_id: randomId(prefix), received_at: partial.received_at ?? new Date().toISOString(), prev_hash };
|
|
48
|
+
const rec = { ...body, hash: recordHash(body) };
|
|
49
|
+
this.records.push(rec);
|
|
50
|
+
this.persist(rec);
|
|
51
|
+
for (const l of this.listeners) {
|
|
52
|
+
try {
|
|
53
|
+
l(rec);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
/* a listener must never break recording */
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return rec;
|
|
60
|
+
}
|
|
61
|
+
/** Append a verification result. Pass the request and context to keep artifacts for re-verification. */
|
|
62
|
+
record(result, extra) {
|
|
63
|
+
const url = new URL(extra.request.url);
|
|
64
|
+
const order = extra.context?.order;
|
|
65
|
+
return this.appendRaw({
|
|
66
|
+
type: "verification",
|
|
67
|
+
merchant: this.merchant,
|
|
68
|
+
received_at: result.receivedAt,
|
|
69
|
+
request: { method: extra.request.method.toUpperCase(), authority: url.host, path: url.pathname, raw_sha256: result.rawSha256 },
|
|
70
|
+
agent: result.agent,
|
|
71
|
+
checks: result.checks,
|
|
72
|
+
mandate: result.mandate,
|
|
73
|
+
order: order ? { order_hash: result.orderHash, order_id: extra.orderId ?? order.id, amount: order.total, currency: order.currency, payment_intent: extra.paymentIntent } : undefined,
|
|
74
|
+
policy_version: result.policyVersion,
|
|
75
|
+
decision: result.decision,
|
|
76
|
+
enforced: result.enforced,
|
|
77
|
+
reasons: result.reasons,
|
|
78
|
+
artifacts: extra.storeArtifacts === false
|
|
79
|
+
? undefined
|
|
80
|
+
: { request: extra.request, key_jwk: extra.keyJwk ?? result.agentKeyJwk, issuer_jwk: result.issuerKeyJwk, context: extra.context },
|
|
81
|
+
}, "rec");
|
|
82
|
+
}
|
|
83
|
+
recordDispute(d) {
|
|
84
|
+
return this.appendRaw({ ...d, type: "dispute", merchant: this.merchant }, "dsp");
|
|
85
|
+
}
|
|
86
|
+
/** Recompute every hash and link. Returns the first broken record, if any. */
|
|
87
|
+
verifyChain() {
|
|
88
|
+
let prev = GENESIS;
|
|
89
|
+
for (const r of this.records) {
|
|
90
|
+
const { hash, ...body } = r;
|
|
91
|
+
if (r.prev_hash !== prev)
|
|
92
|
+
return { ok: false, checked: this.records.length, brokenAt: r.record_id, problem: "prev_hash does not link to the previous record" };
|
|
93
|
+
if (recordHash(body) !== hash)
|
|
94
|
+
return { ok: false, checked: this.records.length, brokenAt: r.record_id, problem: "record contents changed after it was written" };
|
|
95
|
+
prev = hash;
|
|
96
|
+
}
|
|
97
|
+
return { ok: true, checked: this.records.length };
|
|
98
|
+
}
|
|
99
|
+
/** Merkle root of the record hashes received on one UTC day (YYYY-MM-DD), for daily anchoring. */
|
|
100
|
+
merkleRoot(day) {
|
|
101
|
+
let level = this.records.filter((r) => r.received_at.startsWith(day)).map((r) => r.hash);
|
|
102
|
+
if (!level.length)
|
|
103
|
+
return null;
|
|
104
|
+
while (level.length > 1) {
|
|
105
|
+
const next = [];
|
|
106
|
+
for (let i = 0; i < level.length; i += 2)
|
|
107
|
+
next.push(sha256Hex(level[i] + (level[i + 1] ?? level[i])));
|
|
108
|
+
level = next;
|
|
109
|
+
}
|
|
110
|
+
return level[0];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** Ledger persisted as JSON Lines on disk. Good for local use, self-hosting and shadow mode. */
|
|
114
|
+
export class FileLedger extends EvidenceLedger {
|
|
115
|
+
path;
|
|
116
|
+
constructor(merchant, path) {
|
|
117
|
+
super(merchant);
|
|
118
|
+
this.path = path;
|
|
119
|
+
if (existsSync(path)) {
|
|
120
|
+
const text = readFileSync(path, "utf8");
|
|
121
|
+
this.records = text.split("\n").filter(Boolean).map((l) => JSON.parse(l));
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
persist(rec) {
|
|
128
|
+
appendFileSync(this.path, JSON.stringify(rec) + "\n");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=ledger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ledger.js","sourceRoot":"","sources":["../src/ledger.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC9E,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AA8C9D,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAE/B,MAAM,UAAU,UAAU,CAAC,GAA+B;IACxD,OAAO,SAAS,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC;AACtC,CAAC;AAED,MAAM,OAAO,cAAc;IAGM;IAFrB,OAAO,GAAmB,EAAE,CAAC;IAEvC,YAA+B,QAAgB;QAAhB,aAAQ,GAAR,QAAQ,CAAQ;IAAG,CAAC;IAEnD,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC;IAC7B,CAAC;IAED,GAAG;QACD,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;IAED,aAAa;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAA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AAY;QACzD,KAAK,CAAC,QAAQ,CAAC,CAAC;QAD6B,SAAI,GAAJ,IAAI,CAAQ;QAEzD,IAAI,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;YACrB,MAAM,IAAI,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YACxC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAiB,CAAC,CAAC;QAC5F,CAAC;aAAM,CAAC;YACN,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAChD,CAAC;IACH,CAAC;IAEkB,OAAO,CAAC,GAAiB;QAC1C,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC;IACxD,CAAC;CACF"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Jwk } from "../keys.js";
|
|
2
|
+
import type { MandateContext, MandateOutcome } from "./index.js";
|
|
3
|
+
export interface Ap2Cart {
|
|
4
|
+
cart_id: string;
|
|
5
|
+
merchant_id: string;
|
|
6
|
+
currency: string;
|
|
7
|
+
total: number;
|
|
8
|
+
items: {
|
|
9
|
+
sku: string;
|
|
10
|
+
qty: number;
|
|
11
|
+
unit_amount: number;
|
|
12
|
+
}[];
|
|
13
|
+
expires_at: string;
|
|
14
|
+
}
|
|
15
|
+
export interface Ap2CartMandate {
|
|
16
|
+
type: "ap2-cart";
|
|
17
|
+
/** Compact JWS over the cart, signed first by the merchant. */
|
|
18
|
+
merchant_signature: string;
|
|
19
|
+
/** Compact JWS over the same cart, signed by the user's device key. */
|
|
20
|
+
user_signature: string;
|
|
21
|
+
/** The user's public key as presented. Its trust anchor is undefined in AP2 v0.1. */
|
|
22
|
+
user_jwk: Jwk;
|
|
23
|
+
}
|
|
24
|
+
export interface Ap2IntentMandate {
|
|
25
|
+
type: "ap2-intent";
|
|
26
|
+
/** Compact JWS signed by the user's device key. */
|
|
27
|
+
jws: string;
|
|
28
|
+
user_jwk: Jwk;
|
|
29
|
+
}
|
|
30
|
+
export declare function verifyAp2Cart(m: Ap2CartMandate, ctx: MandateContext): Promise<MandateOutcome>;
|
|
31
|
+
export declare function verifyAp2Intent(m: Ap2IntentMandate, ctx: MandateContext): Promise<MandateOutcome>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Google Agent Payments Protocol (AP2) mandates.
|
|
3
|
+
*
|
|
4
|
+
* AP2 v0.1 leaves the signing format and the issuance of trusted user keys open. This module
|
|
5
|
+
* accepts compact JWS signatures and reports the user-key trust gap as a warning instead of
|
|
6
|
+
* pretending it is solved. STATUS: preview.
|
|
7
|
+
*/
|
|
8
|
+
import { verifyJws } from "../jws.js";
|
|
9
|
+
import { canonicalize } from "../util.js";
|
|
10
|
+
export async function verifyAp2Cart(m, ctx) {
|
|
11
|
+
const checks = [];
|
|
12
|
+
let userCart;
|
|
13
|
+
try {
|
|
14
|
+
userCart = verifyJws(m.user_signature, m.user_jwk).payload;
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
checks.push({ name: "mandate_valid", status: "fail", detail: `User signature on the cart mandate failed: ${e.message}` });
|
|
18
|
+
return { info: { type: m.type, valid: false }, checks };
|
|
19
|
+
}
|
|
20
|
+
if (ctx.merchantJwk) {
|
|
21
|
+
try {
|
|
22
|
+
const merchantCart = verifyJws(m.merchant_signature, ctx.merchantJwk).payload;
|
|
23
|
+
if (canonicalize(merchantCart) !== canonicalize(userCart))
|
|
24
|
+
throw new Error("merchant and user signed different carts");
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
checks.push({ name: "mandate_valid", status: "fail", detail: `Merchant signature on the cart mandate failed: ${e.message}` });
|
|
28
|
+
return { info: { type: m.type, valid: false }, checks };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (Date.parse(userCart.expires_at) / 1000 < ctx.now) {
|
|
32
|
+
checks.push({ name: "mandate_valid", status: "fail", detail: "The cart mandate has expired." });
|
|
33
|
+
return { info: { type: m.type, valid: false }, checks };
|
|
34
|
+
}
|
|
35
|
+
checks.push({
|
|
36
|
+
name: "mandate_valid",
|
|
37
|
+
status: ctx.merchantJwk ? "pass" : "warn",
|
|
38
|
+
detail: ctx.merchantJwk
|
|
39
|
+
? "Cart mandate signed by the merchant and the user."
|
|
40
|
+
: "User signature verified. Merchant signature not checked: configure ap2.merchantJwk.",
|
|
41
|
+
});
|
|
42
|
+
checks.push({
|
|
43
|
+
name: "mandate_user_key_trust",
|
|
44
|
+
status: "warn",
|
|
45
|
+
detail: "AP2 v0.1 does not define how user keys are issued, so the user key is trusted as presented.",
|
|
46
|
+
});
|
|
47
|
+
const problems = [];
|
|
48
|
+
if (userCart.merchant_id !== ctx.merchantId)
|
|
49
|
+
problems.push("cart is for a different merchant");
|
|
50
|
+
if (userCart.currency.toLowerCase() !== ctx.order.currency.toLowerCase())
|
|
51
|
+
problems.push("currency differs");
|
|
52
|
+
if (userCart.total !== ctx.order.total)
|
|
53
|
+
problems.push(`cart total ${userCart.total} differs from order total ${ctx.order.total}`);
|
|
54
|
+
const norm = (items) => canonicalize([...items].map((i) => ({ sku: i.sku, qty: i.qty, unit_amount: i.unit_amount })).sort((a, b) => a.sku.localeCompare(b.sku)));
|
|
55
|
+
if (norm(userCart.items) !== norm(ctx.order.items))
|
|
56
|
+
problems.push("items differ from the signed cart");
|
|
57
|
+
checks.push(problems.length
|
|
58
|
+
? { name: "order_within_mandate", status: "fail", detail: "Order differs from the signed cart: " + problems.join("; ") + "." }
|
|
59
|
+
: { name: "order_within_mandate", status: "pass", detail: "Order is exactly the cart the user signed." });
|
|
60
|
+
return { info: { type: m.type, mode: "human-present", valid: true, constraintsOk: !problems.length, expiresAt: userCart.expires_at, summary: `cart ${userCart.cart_id}` }, checks };
|
|
61
|
+
}
|
|
62
|
+
export async function verifyAp2Intent(m, ctx) {
|
|
63
|
+
const checks = [];
|
|
64
|
+
let intent;
|
|
65
|
+
try {
|
|
66
|
+
intent = verifyJws(m.jws, m.user_jwk).payload;
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
checks.push({ name: "mandate_valid", status: "fail", detail: `Intent mandate signature failed: ${e.message}` });
|
|
70
|
+
return { info: { type: m.type, valid: false }, checks };
|
|
71
|
+
}
|
|
72
|
+
if (Date.parse(intent.expires_at) / 1000 < ctx.now) {
|
|
73
|
+
checks.push({ name: "mandate_valid", status: "fail", detail: "The intent mandate has expired." });
|
|
74
|
+
return { info: { type: m.type, valid: false }, checks };
|
|
75
|
+
}
|
|
76
|
+
checks.push({ name: "mandate_valid", status: "pass", detail: "Intent mandate signature verified." });
|
|
77
|
+
checks.push({ name: "mandate_user_key_trust", status: "warn", detail: "AP2 v0.1 does not define how user keys are issued, so the user key is trusted as presented." });
|
|
78
|
+
const problems = [];
|
|
79
|
+
if (intent.merchants && !intent.merchants.includes(ctx.merchantId))
|
|
80
|
+
problems.push("merchant not in the intent");
|
|
81
|
+
if (intent.max_amount && ctx.order.total > intent.max_amount.amount)
|
|
82
|
+
problems.push(`order total exceeds the intent cap ${intent.max_amount.amount}`);
|
|
83
|
+
checks.push(problems.length
|
|
84
|
+
? { name: "order_within_mandate", status: "fail", detail: "Order is outside the intent: " + problems.join("; ") + "." }
|
|
85
|
+
: { name: "order_within_mandate", status: "pass", detail: "Order is inside the intent mandate." });
|
|
86
|
+
return {
|
|
87
|
+
info: { type: m.type, mode: "human-not-present", valid: true, constraintsOk: !problems.length, expiresAt: intent.expires_at, summary: intent.prompt_playback },
|
|
88
|
+
checks,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=ap2.js.map
|
|
@@ -0,0 +1 @@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,CAAC;IACvK,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,IAAI,MAAM,CAAC,SAAS,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC;QAAE,QAAQ,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAC;IAChH,IAAI,MAAM,CAAC,UAAU,IAAI,GAAG,CAAC,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,UAAU,CAAC,MAAM;QAAE,QAAQ,CAAC,IAAI,CAAC,sCAAsC,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC;IACrJ,MAAM,CAAC,IAAI,CACT,QAAQ,CAAC,MAAM;QACb,CAAC,CAAC,EAAE,IAAI,EAAE,sBAAsB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,+BAA+B,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,EAAE;QACvH,CAAC,CAAC,EAAE,IAAI,EAAE,sBAAsB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,qCAAqC,EAAE,CACpG,CAAC;IACF,OAAO;QACL,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,mBAAmB,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,UAAU,EAAE,OAAO,EAAE,MAAM,CAAC,eAAe,EAAE;QAC9J,MAAM;KACP,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Check, MandateInfo, Order } from "../types.js";
|
|
2
|
+
import type { KeyDirectory, Jwk } from "../keys.js";
|
|
3
|
+
import { type VerifiableIntentMandate } from "./verifiableIntent.js";
|
|
4
|
+
import { type Ap2CartMandate, type Ap2IntentMandate } from "./ap2.js";
|
|
5
|
+
export type Mandate = VerifiableIntentMandate | Ap2CartMandate | Ap2IntentMandate;
|
|
6
|
+
export interface MandateContext {
|
|
7
|
+
order: Order;
|
|
8
|
+
orderHash: string;
|
|
9
|
+
now: number;
|
|
10
|
+
merchantId: string;
|
|
11
|
+
/** Directory holding issuer keys for Verifiable Intent layer 1. */
|
|
12
|
+
issuers?: KeyDirectory;
|
|
13
|
+
/** The merchant's own public key, used to check its signature on AP2 cart mandates. */
|
|
14
|
+
merchantJwk?: Jwk;
|
|
15
|
+
/** Mandate types this merchant accepts (policy). */
|
|
16
|
+
accept: string[];
|
|
17
|
+
}
|
|
18
|
+
export interface MandateOutcome {
|
|
19
|
+
info: MandateInfo;
|
|
20
|
+
checks: Check[];
|
|
21
|
+
/** Issuer key used for layer 1, kept in the evidence record so it can be re-verified later. */
|
|
22
|
+
issuerKeyJwk?: Jwk;
|
|
23
|
+
}
|
|
24
|
+
export declare function verifyMandate(m: Mandate | undefined, ctx: MandateContext): Promise<MandateOutcome | null>;
|
|
25
|
+
export type { VerifiableIntentMandate, Ap2CartMandate, Ap2IntentMandate };
|