|
| 1 | +/** |
| 2 | + * Unit tests for core SDK logic (canonicalization, hashing, verification). |
| 3 | + * Uses tweetnacl for Ed25519 key generation — no external test framework needed. |
| 4 | + */ |
| 5 | +import { createRequire } from "node:module"; |
| 6 | +const require = createRequire(import.meta.url); |
| 7 | + |
| 8 | +const { |
| 9 | + canonicalizeStableJsonV1, |
| 10 | + sha256HexUtf8, |
| 11 | + parseEd25519Pubkey, |
| 12 | + verifyEd25519SignatureOverUtf8HashString, |
| 13 | + recomputeReceiptHashSha256, |
| 14 | + verifyReceipt, |
| 15 | + CommandLayerError, |
| 16 | + CommandLayerClient, |
| 17 | +} = require("../dist/index.cjs"); |
| 18 | + |
| 19 | +const nacl = require("tweetnacl"); |
| 20 | + |
| 21 | +let passed = 0; |
| 22 | +let failed = 0; |
| 23 | + |
| 24 | +function assert(condition, name) { |
| 25 | + if (!condition) { |
| 26 | + failed++; |
| 27 | + console.error(`FAIL: ${name}`); |
| 28 | + } else { |
| 29 | + passed++; |
| 30 | + console.log(`PASS: ${name}`); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function assertThrows(fn, name) { |
| 35 | + try { |
| 36 | + fn(); |
| 37 | + failed++; |
| 38 | + console.error(`FAIL: ${name} (did not throw)`); |
| 39 | + } catch { |
| 40 | + passed++; |
| 41 | + console.log(`PASS: ${name}`); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// ---- Canonicalization ---- |
| 46 | + |
| 47 | +assert(canonicalizeStableJsonV1(null) === "null", "canonicalize null"); |
| 48 | +assert(canonicalizeStableJsonV1(true) === "true", "canonicalize true"); |
| 49 | +assert(canonicalizeStableJsonV1(false) === "false", "canonicalize false"); |
| 50 | +assert(canonicalizeStableJsonV1(42) === "42", "canonicalize int"); |
| 51 | +assert(canonicalizeStableJsonV1(3.14) === "3.14", "canonicalize float"); |
| 52 | +assert(canonicalizeStableJsonV1("hello") === '"hello"', "canonicalize string"); |
| 53 | +assert(canonicalizeStableJsonV1([1, 2, 3]) === "[1,2,3]", "canonicalize array"); |
| 54 | +assert( |
| 55 | + canonicalizeStableJsonV1({ b: 2, a: 1 }) === '{"a":1,"b":2}', |
| 56 | + "canonicalize sorts keys" |
| 57 | +); |
| 58 | +assert( |
| 59 | + canonicalizeStableJsonV1({ z: { b: 1, a: 2 } }) === '{"z":{"a":2,"b":1}}', |
| 60 | + "canonicalize nested sorted" |
| 61 | +); |
| 62 | +assertThrows( |
| 63 | + () => canonicalizeStableJsonV1(BigInt(1)), |
| 64 | + "canonicalize rejects bigint" |
| 65 | +); |
| 66 | +assertThrows( |
| 67 | + () => canonicalizeStableJsonV1(Infinity), |
| 68 | + "canonicalize rejects Infinity" |
| 69 | +); |
| 70 | +assertThrows( |
| 71 | + () => canonicalizeStableJsonV1({ a: undefined }), |
| 72 | + "canonicalize rejects undefined value" |
| 73 | +); |
| 74 | + |
| 75 | +// Negative zero |
| 76 | +assert(canonicalizeStableJsonV1(-0) === "0", "canonicalize -0 => 0"); |
| 77 | + |
| 78 | +// ---- SHA-256 ---- |
| 79 | + |
| 80 | +const knownHash = sha256HexUtf8("hello"); |
| 81 | +assert(knownHash.length === 64, "sha256 returns 64 hex chars"); |
| 82 | +assert(sha256HexUtf8("hello") === knownHash, "sha256 deterministic"); |
| 83 | +assert(sha256HexUtf8("hello") !== sha256HexUtf8("world"), "sha256 differs for different inputs"); |
| 84 | + |
| 85 | +// ---- Ed25519 pubkey parsing ---- |
| 86 | + |
| 87 | +const kp = nacl.sign.keyPair(); |
| 88 | +const b64Key = Buffer.from(kp.publicKey).toString("base64"); |
| 89 | +const hexKey = Buffer.from(kp.publicKey).toString("hex"); |
| 90 | + |
| 91 | +const pk1 = parseEd25519Pubkey(b64Key); |
| 92 | +assert(pk1.length === 32, "parse base64 pubkey"); |
| 93 | + |
| 94 | +const pk2 = parseEd25519Pubkey(`ed25519:${b64Key}`); |
| 95 | +assert(pk2.length === 32, "parse ed25519: prefixed pubkey"); |
| 96 | + |
| 97 | +const pk3 = parseEd25519Pubkey(hexKey); |
| 98 | +assert(pk3.length === 32, "parse hex pubkey"); |
| 99 | + |
| 100 | +const pk4 = parseEd25519Pubkey(`0x${hexKey}`); |
| 101 | +assert(pk4.length === 32, "parse 0x-prefixed hex pubkey"); |
| 102 | + |
| 103 | +assertThrows( |
| 104 | + () => parseEd25519Pubkey("not_valid_key!!"), |
| 105 | + "rejects invalid pubkey" |
| 106 | +); |
| 107 | + |
| 108 | +// ---- Signature verification ---- |
| 109 | + |
| 110 | +const hashHex = sha256HexUtf8('{"test":true}'); |
| 111 | +const msg = Buffer.from(hashHex, "utf8"); |
| 112 | +const sig = nacl.sign.detached(new Uint8Array(msg), kp.secretKey); |
| 113 | +const sigB64 = Buffer.from(sig).toString("base64"); |
| 114 | + |
| 115 | +assert( |
| 116 | + verifyEd25519SignatureOverUtf8HashString(hashHex, sigB64, kp.publicKey) === true, |
| 117 | + "valid signature verifies" |
| 118 | +); |
| 119 | + |
| 120 | +const badKp = nacl.sign.keyPair(); |
| 121 | +assert( |
| 122 | + verifyEd25519SignatureOverUtf8HashString(hashHex, sigB64, badKp.publicKey) === false, |
| 123 | + "wrong key rejects" |
| 124 | +); |
| 125 | + |
| 126 | +// ---- Receipt verification (end-to-end) ---- |
| 127 | + |
| 128 | +const receipt = { |
| 129 | + status: "success", |
| 130 | + x402: { verb: "summarize", version: "1.0.0", entry: "x402://summarizeagent.eth/summarize/v1.0.0" }, |
| 131 | + result: { summary: "test" }, |
| 132 | + metadata: { |
| 133 | + proof: { |
| 134 | + alg: "ed25519-sha256", |
| 135 | + canonical: "cl-stable-json-v1", |
| 136 | + signer_id: "runtime.commandlayer.eth", |
| 137 | + }, |
| 138 | + }, |
| 139 | +}; |
| 140 | + |
| 141 | +const { hash_sha256 } = recomputeReceiptHashSha256(receipt); |
| 142 | +const receiptMsg = Buffer.from(hash_sha256, "utf8"); |
| 143 | +const receiptSig = nacl.sign.detached(new Uint8Array(receiptMsg), kp.secretKey); |
| 144 | + |
| 145 | +receipt.metadata.proof.hash_sha256 = hash_sha256; |
| 146 | +receipt.metadata.proof.signature_b64 = Buffer.from(receiptSig).toString("base64"); |
| 147 | +receipt.metadata.receipt_id = hash_sha256; |
| 148 | + |
| 149 | +const vr = await verifyReceipt(receipt, { publicKey: `ed25519:${b64Key}` }); |
| 150 | +assert(vr.ok === true, "verifyReceipt ok for valid receipt"); |
| 151 | +assert(vr.checks.hash_matches === true, "verifyReceipt hash matches"); |
| 152 | +assert(vr.checks.signature_valid === true, "verifyReceipt signature valid"); |
| 153 | +assert(vr.checks.receipt_id_matches === true, "verifyReceipt receipt_id matches"); |
| 154 | + |
| 155 | +// Tampered receipt |
| 156 | +const tamperedReceipt = JSON.parse(JSON.stringify(receipt)); |
| 157 | +tamperedReceipt.result.summary = "tampered"; |
| 158 | +const vr2 = await verifyReceipt(tamperedReceipt, { publicKey: `ed25519:${b64Key}` }); |
| 159 | +assert(vr2.ok === false, "verifyReceipt rejects tampered receipt"); |
| 160 | +assert(vr2.checks.hash_matches === false, "tampered receipt hash mismatch"); |
| 161 | + |
| 162 | +// ---- Client verb validation ---- |
| 163 | + |
| 164 | +const client = new CommandLayerClient(); |
| 165 | +try { |
| 166 | + await client.call("nonexistent", {}); |
| 167 | + failed++; |
| 168 | + console.error("FAIL: client.call accepts unknown verb"); |
| 169 | +} catch (err) { |
| 170 | + assert(err instanceof CommandLayerError, "client.call rejects unknown verb with CommandLayerError"); |
| 171 | +} |
| 172 | + |
| 173 | +// ---- Summary ---- |
| 174 | + |
| 175 | +console.log(`\n${passed} passed, ${failed} failed`); |
| 176 | +if (failed > 0) process.exit(1); |
0 commit comments