logicsrc/packages/opencreds/src/signing-keys.test.ts
Anthony Ettinger 3e62d500ce fix: bound the wallet-name regex, build PEM fixtures at runtime
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 19:28:20 +00:00

39 lines
2.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { isSigningKey, isSigningKeyName, looksLikeSigningKeyValue } from "./signing-keys.js";
const fx = JSON.parse(readFileSync(new URL("./signing-keys.fixtures.json", import.meta.url), "utf8")) as { signing: string[]; not_signing: string[] };
describe("isSigningKeyName", () => {
it.each(fx.signing)("%s is a signing key", (n) => expect(isSigningKeyName(n)).toBe(true));
it.each(fx.not_signing)("%s is not", (n) => expect(isSigningKeyName(n)).toBe(false));
});
describe("looksLikeSigningKeyValue", () => {
const hex = "ab".repeat(32);
// Built at runtime so secret scanners do not read the fixtures as real keys.
const pem = (kind: string) => ["-----BEGIN", `${kind}-----`].join(" ");
it("catches PEM, 0x-hex, Solana base58 and byte arrays, and mnemonic shapes", () => {
expect(looksLikeSigningKeyValue(`${pem("OPENSSH PRIVATE KEY")}\nb3Bl`)).toBe(true);
expect(looksLikeSigningKeyValue(pem("EC PRIVATE KEY"))).toBe(true);
expect(looksLikeSigningKeyValue(`0x${hex}`)).toBe(true);
expect(looksLikeSigningKeyValue(`"0x${hex}"`)).toBe(true);
expect(looksLikeSigningKeyValue("4".repeat(44) + "5".repeat(44))).toBe(true);
expect(looksLikeSigningKeyValue(`[${Array.from({ length: 64 }, (_, i) => i).join(",")}]`)).toBe(true);
expect(looksLikeSigningKeyValue(Array(12).fill("abandon").join(" "))).toBe(true);
expect(looksLikeSigningKeyValue(Array(24).fill("zoo").join(" "))).toBe(true);
});
it("leaves ordinary secrets alone", () => {
expect(looksLikeSigningKeyValue(hex)).toBe(false); // bare hex: HMAC/session secrets look like this
expect(looksLikeSigningKeyValue("sk_live_51Habc")).toBe(false);
expect(looksLikeSigningKeyValue("https://example.com/path")).toBe(false);
expect(looksLikeSigningKeyValue(pem("PUBLIC KEY"))).toBe(false);
expect(looksLikeSigningKeyValue("hello world this is a normal sentence of eleven words ok")).toBe(false);
expect(looksLikeSigningKeyValue(Array(12).fill("Abandon").join(" "))).toBe(false);
});
it("isSigningKey checks the name, then the value", () => {
expect(isSigningKey("NOTES", `0x${hex}`)).toBe(true);
expect(isSigningKey("SOLANA_PRIVATE_KEY")).toBe(true);
expect(isSigningKey("NOTES", "plain")).toBe(false);
});
});