Skip to content
Merged
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
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface CommandLayerConfig {
}

export const DEFAULT_VERIFIER_URL = "https://runtime.commandlayer.org/verify";
export const DEFAULT_CANONICALIZATION = "json.sorted_keys.v1";

export const TRUST_FAMILY = "trust-verification" as const;
export const TRUST_VERSION = "1.0.0" as const;
Expand Down Expand Up @@ -97,11 +98,18 @@ export class CommandLayer {
throw new Error("Missing privateKeyPem (or deprecated privateKey alias)");
}

const canonicalization = config.canonicalization ?? DEFAULT_CANONICALIZATION;
if (canonicalization !== DEFAULT_CANONICALIZATION) {
throw new Error(
`Unsupported canonicalization \"${canonicalization}\". Only \"${DEFAULT_CANONICALIZATION}\" is supported.`,
);
}

this.config = {
...config,
signer,
privateKeyPem,
canonicalization: config.canonicalization ?? "json.sorted_keys.v1",
canonicalization,
verifierUrl: config.verifierUrl ?? DEFAULT_VERIFIER_URL,
};

Expand Down
12 changes: 4 additions & 8 deletions src/receipt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
signCommandLayerReceipt,
type CommandLayerCanonicalization,
type CommandLayerReceipt,
type SignCommandLayerReceiptParams,
} from "@commandlayer/runtime-core";
import type { JsonValue } from "./canonicalize.js";

Expand All @@ -23,7 +21,7 @@ export interface ReceiptInput {
};
}

export type Receipt = CommandLayerReceipt<ReceiptInput>;
export type Receipt = CommandLayerReceipt;

export function canonicalPayloadFromReceiptInput(receipt: ReceiptInput) {
return {
Expand All @@ -41,13 +39,11 @@ export function canonicalPayloadFromReceiptInput(receipt: ReceiptInput) {
export async function createReceipt(params: {
keyId: string;
privateKeyPem: string;
canonicalization: CommandLayerCanonicalization;
canonicalization: string;
input: ReceiptInput;
}): Promise<Receipt> {
return signCommandLayerReceipt({
receipt: params.input,
return signCommandLayerReceipt(params.input, {
privateKeyPem: params.privateKeyPem,
kid: params.keyId,
canonicalization: params.canonicalization,
} as SignCommandLayerReceiptParams<ReceiptInput>);
});
}
14 changes: 11 additions & 3 deletions test/receipt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ async function generateKeyPair(): Promise<{ pem: string; publicKey: CryptoKey }>
return { pem: toPem(pkcs8), publicKey: keyPair.publicKey };
}

function toSpkiPem(spki: ArrayBuffer): string {
const b64 = Buffer.from(spki).toString("base64");
const lines = b64.match(/.{1,64}/g)?.join("\n") ?? b64;
return `-----BEGIN PUBLIC KEY-----\n${lines}\n-----END PUBLIC KEY-----`;
}

async function generatePrivateKeyPem(): Promise<string> {
return (await generateKeyPair()).pem;
}
Expand Down Expand Up @@ -80,6 +86,8 @@ test("wrap rejects an unrecognized verb before running the wrapped function", as

test("emitted receipt verifies with runtime-core and tampering is invalid", async () => {
const { pem, publicKey } = await generateKeyPair();
const spki = await webcrypto.subtle.exportKey("spki", publicKey);
const publicKeyPem = toSpkiPem(spki);

const cl = new CommandLayer({
signer: "verifyagent.eth",
Expand All @@ -92,11 +100,11 @@ test("emitted receipt verifies with runtime-core and tampering is invalid", asyn
run: async () => ({ y: 2 }),
});

const verification = await verifyCommandLayerReceipt({ receipt });
assert.equal(verification.status, "VALID");
const verification = await verifyCommandLayerReceipt(receipt, { publicKeyPemOrDer: publicKeyPem });
assert.equal(verification.status, "VERIFIED");

const tampered = { ...receipt, output: { y: 99 } };
const tamperedVerification = await verifyCommandLayerReceipt({ receipt: tampered });
const tamperedVerification = await verifyCommandLayerReceipt(tampered, { publicKeyPemOrDer: publicKeyPem });
assert.equal(tamperedVerification.status, "INVALID");
});

Expand Down
Loading