Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions scripts/parity-check.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ function normalize(value) {
}

function comparableVector(vector) {
const checks = vector.checks ?? {};
return normalize({
name: vector.name,
expected_ok: vector.expected_ok,
ok: vector.ok,
checks: vector.checks,
semantic_ok: checks.alg_matches && checks.canonical_matches && checks.hash_matches && checks.signature_valid,
checks: {
alg_matches: checks.alg_matches,
canonical_matches: checks.canonical_matches,
hash_matches: checks.hash_matches,
receipt_id_matches: checks.receipt_id_matches,
signature_valid: checks.signature_valid
},
errors: vector.errors,
values: vector.values,
recomputed_hash: vector.recomputed_hash
Expand Down Expand Up @@ -61,7 +68,7 @@ for (const vector of manifest.verification_vectors) {
const pyVector = pyReport.vector_results.find((entry) => entry.name === vector.name);
const tsComparable = comparableVector(tsVector);
const pyComparable = comparableVector(pyVector);
const matchesExpectation = tsVector.ok === vector.expected_ok && pyVector.ok === vector.expected_ok;
const matchesExpectation = tsComparable.semantic_ok === vector.expected_ok && pyComparable.semantic_ok === vector.expected_ok;
const matchesEachOther = JSON.stringify(tsComparable) === JSON.stringify(pyComparable);
const status = matchesExpectation && matchesEachOther ? "PASS" : "FAIL";
console.log(`- ${status} ${vector.name}`);
Expand Down
32 changes: 27 additions & 5 deletions typescript-sdk/scripts/template-tests.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import { spawnSync } from "node:child_process";
import { globSync } from "node:fs";

const suites = [
"runtime/tests/*.test.mjs",
"typescript-sdk/tests/*.test.mjs"
{
pattern: "runtime/tests/*.test.mjs",
optional: true
},
{
pattern: "typescript-sdk/tests/*.test.mjs",
optional: false
}
];

for (const pattern of suites) {
const run = spawnSync("node", ["--test", pattern], {
const cwd = new URL("../..", import.meta.url);

for (const suite of suites) {
const matches = globSync(suite.pattern, { cwd });

if (matches.length === 0) {
if (suite.optional) {
console.log(`Skipping optional template test suite: ${suite.pattern}`);
continue;
}

console.error(`No template tests found for required suite: ${suite.pattern}`);
process.exit(1);
}

const run = spawnSync("node", ["--test", ...matches], {
stdio: "inherit",
cwd: new URL("../..", import.meta.url)
cwd
});

if (run.status !== 0) {
process.exit(run.status ?? 1);
}
Expand Down
1 change: 1 addition & 0 deletions typescript-sdk/scripts/unit-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ await assertRejects(

const receipt = {
status: "success",
verb: "summarize",
result: { summary: "test" },
metadata: {
proof: {
Expand Down
15 changes: 11 additions & 4 deletions typescript-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ export type RuntimeMetadata = {
[k: string]: unknown;
};

export type ReceiptProtocolMetadata = {
verb: string;
version?: string;
[k: string]: unknown;
};

export type CommandResponse<TResult = unknown, TError = unknown> = {
receipt: CanonicalReceipt<TResult, TError>;
receipt: CanonicalReceipt<TResult>;
runtime_metadata?: RuntimeMetadata;
};

export type LegacyBlendedReceipt<TResult = unknown, TError = unknown> = CanonicalReceipt<TResult, TError> & {
export type LegacyBlendedReceipt<TResult = unknown, TError = unknown> = CanonicalReceipt<TResult> & {
trace?: RuntimeMetadata;
};

Expand Down Expand Up @@ -295,7 +301,7 @@ function extractReceipt(subject: CanonicalReceipt | CommandResponse | LegacyBlen

export function extractReceiptVerb(subject: CanonicalReceipt | CommandResponse | LegacyBlendedReceipt): string | null {
const receipt = extractReceipt(subject);
return isRecord(receipt.x402) && typeof receipt.x402.verb === "string" ? receipt.x402.verb : null;
return getReceiptVerb(receipt);
}

export function normalizeCommandResponse<T = unknown>(payload: unknown): CommandResponse<T> {
Expand Down Expand Up @@ -349,6 +355,7 @@ export async function verifyReceipt(receiptLike: CanonicalReceipt | CommandRespo
const { hash_sha256: recomputedHash } = recomputeReceiptHashSha256(receipt);
const hashMatches = claimedHash === recomputedHash;
const receiptId = typeof receipt.metadata?.receipt_id === "string" ? receipt.metadata.receipt_id : null;
const receiptIdPresent = typeof receipt.metadata?.receipt_id === "string";
const receiptIdMatches = !receiptId || !claimedHash ? true : receiptId === claimedHash;

let pubkey: Uint8Array | null = null;
Expand Down Expand Up @@ -396,7 +403,7 @@ export async function verifyReceipt(receiptLike: CanonicalReceipt | CommandRespo
},
values: {
verb: getReceiptVerb(receipt),
signer_id,
signer_id: signerId,
alg,
canonical,
claimed_hash: claimedHash,
Expand Down
Loading