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
26 changes: 19 additions & 7 deletions typescript-sdk/scripts/template-tests.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { readdirSync } from "node:fs";
import path from "node:path";
import { spawnSync } from "node:child_process";

const suites = [
"runtime/tests/*.test.mjs",
"typescript-sdk/tests/*.test.mjs"
];
const rootDir = new URL("../..", import.meta.url);

for (const pattern of suites) {
const run = spawnSync("node", ["--test", pattern], {
const suites = ["runtime/tests", "typescript-sdk/tests"];

function getTestFiles(relativeDir) {
const absoluteDir = new URL(relativeDir, rootDir);
return readdirSync(absoluteDir)
.filter((file) => file.endsWith(".test.mjs"))
.map((file) => path.posix.join(relativeDir, file));
}

for (const suite of suites) {
const files = getTestFiles(suite);
if (files.length === 0) {
continue;
}
const run = spawnSync("node", ["--test", ...files], {
stdio: "inherit",
cwd: new URL("../..", import.meta.url)
cwd: rootDir
});
if (run.status !== 0) {
process.exit(run.status ?? 1);
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 @@ -45,6 +45,7 @@ export type ReceiptMetadata = {

export type CanonicalReceipt<T = unknown> = {
status: "success" | "error" | string;
verb?: string;
/**
* Legacy / commercial-only metadata.
* Commons v1.1.0 receipts should not rely on or emit this block.
Expand Down Expand Up @@ -76,12 +77,17 @@ export type RuntimeMetadata = {
[k: string]: unknown;
};

export type ReceiptProtocolMetadata = {
verb: string;
version: string;
};

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) ?? "summarize";
}

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 = !!receiptId;
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