import { test } from 'node:test';
import assert from 'node:assert/strict';
import { privateKeyToAccount } from 'viem/accounts';
import {
createIdentityChamber, createChallenge, verifyChallenge, challengeMessage, DEFAULT_TTL_MS, MIN_TTL_MS, MAX_TTL_MS,
} from '../src/index.ts';
import type { Challenge } from '../src/index.ts';
// Throwaway test keys (public test vectors, hold nothing).
const owner = privateKeyToAccount('0x1111111111111111111111111111111111111111111111111111111111111111');
const other = privateKeyToAccount('0x2222222222222222222222222222222222222222222222222222222222222222');
const DID_ = 'did:pkh:eip155:8453:' + owner.address;
const LISTING = 'lax-ext-0123456789ab';
const T0 = Date.parse('2026-10-24T12:00:00.000Z');
let n = 0;
const fixedNonce = () => (++n).toString(16).padStart(32, '0');
const fresh = (opts = {}) => createIdentityChamber({ randomNonce: fixedNonce, ...opts });
const sign = (c: Challenge, who = owner) => who.signMessage({ message: c.message });
test('A1 valid: the listing owner signature over a fresh challenge verifies', async () => {
const ch = fresh();
const c = ch.createChallenge({ did: DID_, listingId: LISTING, now: T0 });
assert.equal(c.issuedAt, '2026-10-24T12:00:00.000Z');
assert.equal(Date.parse(c.expiresAt) - T0, DEFAULT_TTL_MS);
assert.equal(c.message, ['LAX identity challenge', `did: ${DID_}`, `listing: ${LISTING}`, `nonce: ${c.nonce}`, `issued: ${c.issuedAt}`, `expires: ${c.expiresAt}`].join('\n'));
assert.deepEqual(await ch.verifyChallenge({ challenge: c, signature: await sign(c), ownerAddress: owner.address, now: T0 + 1000 }), { ok: true, reason: 'signed by the listing owner' });
});
test('A1b valid with a lowercase owner address and the module level chamber', async () => {
const c = createChallenge({ did: DID_, listingId: LISTING, now: T0 });
assert.match(c.nonce, /^[0-9a-f]{32}$/);
const v = await verifyChallenge({ challenge: c, signature: await sign(c), ownerAddress: owner.address.toLowerCase(), now: T0 });
assert.equal(v.ok, true);
});
test('A2 wrong signer: a signature by another key is refused', async () => {
const ch = fresh();
const c = ch.createChallenge({ did: DID_, listingId: LISTING, now: T0 });
const v = await ch.verifyChallenge({ challenge: c, signature: await sign(c, other), ownerAddress: owner.address, now: T0 });
assert.deepEqual(v, { ok: false, reason: 'the challenge is not signed by the listing owner' });
// the refused attempt does not burn the nonce: the real owner can still answer
assert.equal((await ch.verifyChallenge({ challenge: c, signature: await sign(c), ownerAddress: owner.address, now: T0 })).ok, true);
});
test('A3 expired: at or after expiresAt is refused; before issuedAt is refused', async () => {
const ch = fresh();
const c = ch.createChallenge({ did: DID_, listingId: LISTING, now: T0 });
const signature = await sign(c);
assert.deepEqual(await ch.verifyChallenge({ challenge: c, signature, ownerAddress: owner.address, now: Date.parse(c.expiresAt) }), { ok: false, reason: 'the challenge expired' });
assert.deepEqual(await ch.verifyChallenge({ challenge: c, signature, ownerAddress: owner.address, now: T0 - 1 }), { ok: false, reason: 'the challenge is not valid yet' });
assert.equal((await ch.verifyChallenge({ challenge: c, signature, ownerAddress: owner.address, now: Date.parse(c.expiresAt) - 1 })).ok, true);
});
test('A4 replayed nonce: a challenge verifies once', async () => {
const ch = fresh();
const c = ch.createChallenge({ did: DID_, listingId: LISTING, now: T0 });
const signature = await sign(c);
assert.equal((await ch.verifyChallenge({ challenge: c, signature, ownerAddress: owner.address, now: T0 })).ok, true);
assert.deepEqual(await ch.verifyChallenge({ challenge: c, signature, ownerAddress: owner.address, now: T0 + 5 }), { ok: false, reason: 'the challenge was already used (replay)' });
});
test('A4b replay guard is shared through an injected store', async () => {
const seen = new Set<string>();
const a = fresh({ seen }), b = fresh({ seen });
const c = a.createChallenge({ did: DID_, listingId: LISTING, now: T0 });
const signature = await sign(c);
assert.equal((await a.verifyChallenge({ challenge: c, signature, ownerAddress: owner.address, now: T0 })).ok, true);
assert.equal((await b.verifyChallenge({ challenge: c, signature, ownerAddress: owner.address, now: T0 })).reason, 'the challenge was already used (replay)');
});
test('A5 tampered DID: changing the DID (with or without the message) is refused', async () => {
const ch = fresh();
const c = ch.createChallenge({ did: DID_, listingId: LISTING, now: T0 });
const signature = await sign(c);
const forged = { ...c, did: 'did:pkh:eip155:8453:' + other.address };
assert.deepEqual(await ch.verifyChallenge({ challenge: forged, signature, ownerAddress: owner.address, now: T0 }), { ok: false, reason: 'the challenge text does not match its fields (tampered)' });
// rewritten consistently: the message changes, so the old signature no longer matches
const rewritten = { ...forged, message: challengeMessage(forged) };
assert.deepEqual(await ch.verifyChallenge({ challenge: rewritten, signature, ownerAddress: owner.address, now: T0 }), { ok: false, reason: 'the challenge is not signed by the listing owner' });
});
test('A6 other tampering: listing, expiry stretched past 15 minutes, message text', async () => {
const ch = fresh();
const c = ch.createChallenge({ did: DID_, listingId: LISTING, now: T0 });
const signature = await sign(c);
const v = (challenge: Challenge) => ch.verifyChallenge({ challenge, signature, ownerAddress: owner.address, now: T0 });
assert.equal((await v({ ...c, listingId: 'lax-ext-other' })).reason, 'the challenge text does not match its fields (tampered)');
assert.equal((await v({ ...c, message: c.message + '\n' })).reason, 'the challenge text does not match its fields (tampered)');
const long = { ...c, expiresAt: new Date(T0 + MAX_TTL_MS + 1).toISOString() };
assert.equal((await v({ ...long, message: challengeMessage(long) })).reason, 'the challenge lifetime is not between 1 and 15 minutes');
const short = { ...c, expiresAt: new Date(T0 + MIN_TTL_MS - 1).toISOString() };
assert.equal((await v({ ...short, message: challengeMessage(short) })).reason, 'the challenge lifetime is not between 1 and 15 minutes');
});
test('A7 malformed inputs are refused with a reason, never thrown', async () => {
const ch = fresh();
const c = ch.createChallenge({ did: DID_, listingId: LISTING, now: T0 });
const signature = await sign(c);
const ok = { challenge: c, signature, ownerAddress: owner.address, now: T0 };
const r = async (x: unknown) => (await ch.verifyChallenge(x as never)).reason;
assert.equal(await r(undefined), 'no challenge');
assert.equal(await r({ ...ok, challenge: 'x' }), 'no challenge');
for (const bad of [{ did: 5 }, { did: 'not a did' }, { listingId: '' }, { listingId: 'a b' }, { listingId: 7 }, { nonce: 'xyz' }, { issuedAt: '2026-10-24' }, { expiresAt: 5 }]) {
assert.equal(await r({ ...ok, challenge: { ...c, ...bad } }), 'the challenge is malformed', JSON.stringify(bad));
}
assert.equal(await r({ ...ok, now: 'soon' }), 'now must be milliseconds since the epoch');
assert.equal(await r({ ...ok, signature: '0x1234' }), 'the signature is malformed');
assert.equal(await r({ ...ok, signature: 12 }), 'the signature is malformed');
assert.equal(await r({ ...ok, ownerAddress: 'alice' }), 'the owner address is malformed');
assert.equal(await r({ ...ok, ownerAddress: null }), 'the owner address is malformed');
// well formed length but not a valid signature (bad recovery byte): refused, not thrown
assert.equal(await r({ ...ok, signature: '0x' + 'ff'.repeat(65) }), 'the challenge is not signed by the listing owner');
});
test('A8 createChallenge validates its input and options', () => {
const ch = fresh();
assert.throws(() => ch.createChallenge(undefined as never), /did must be a DID/);
assert.throws(() => ch.createChallenge({ did: 'x', listingId: LISTING, now: T0 }), /did must be a DID/);
assert.throws(() => ch.createChallenge({ did: DID_, listingId: ' ', now: T0 }), /listingId/);
assert.throws(() => ch.createChallenge({ did: DID_, listingId: LISTING, now: NaN }), /now must be/);
assert.throws(() => createIdentityChamber({ randomNonce: () => 'short' }).createChallenge({ did: DID_, listingId: LISTING, now: T0 }), /randomNonce/);
assert.throws(() => createIdentityChamber({ ttlMs: 1000 }), /ttlMs/);
assert.throws(() => createIdentityChamber({ ttlMs: MAX_TTL_MS + 1 }), /ttlMs/);
assert.throws(() => createIdentityChamber({ ttlMs: Infinity }), /ttlMs/);
const c = createIdentityChamber({ ttlMs: MIN_TTL_MS, randomNonce: fixedNonce }).createChallenge({ did: DID_, listingId: LISTING, now: T0 });
assert.equal(Date.parse(c.expiresAt) - Date.parse(c.issuedAt), MIN_TTL_MS);
});
test('A9 two challenges never share a nonce (default source)', () => {
const ch = createIdentityChamber();
const s = new Set(Array.from({ length: 50 }, () => ch.createChallenge({ did: DID_, listingId: LISTING, now: T0 }).nonce));
assert.equal(s.size, 50);
});
agentgate-source: code mirror on Cloudflare Pages. All repos