import { test } from 'node:test';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { buildAttestation, sha256Hex, schemaUid, isCid, CHARTER_SCHEMA, ZERO_ADDRESS } from '../src/index.ts';
// The Charter attestation verifier's own decoder (charter-verifier/abi.js, MIT, same authors): what we build, it reads.
// @ts-ignore plain JS module
import { decodeCharterData } from './vendor/charter-verifier-abi.js';
const CID0 = 'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG';
const CID1 = 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi';
const CID2 = 'bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku';
const texts = { charter: 'The Founding Charter of Kognai.\n', constitution: 'The Constitution: the Five Laws.\n', soul: 'SOUL: who a citizen is.\n' };
const cids = { charter: CID0, constitution: CID1, soul: CID2 };
const base = { texts, cids, version: '1.0.0', date: '2026-11-14' };
const node = (s: string | Uint8Array) => '0x' + createHash('sha256').update(s).digest('hex');
test('A1 digests match sha256 of the given texts', () => {
const a = buildAttestation(base);
assert.deepEqual(a.digests, { charter: node(texts.charter), constitution: node(texts.constitution), soul: node(texts.soul) });
assert.equal(sha256Hex('abc'), '0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad');
for (const d of Object.values(a.digests)) assert.match(d, /^0x[0-9a-f]{64}$/);
});
test('A1b UTF 8 bytes are hashed exactly (no trimming, no normalisation); bytes input works too', () => {
const s = 'Caf\u00e9 \u0627\u0644\u0645\u064a\u062b\u0627\u0642 ';
const a = buildAttestation({ ...base, texts: { ...texts, soul: s } });
assert.equal((a.digests as Record<string, string>).soul, node(Buffer.from(s, 'utf8')));
const b = buildAttestation({ ...base, texts: { ...texts, soul: new TextEncoder().encode(s) } });
assert.equal(b.data, a.data);
const nfd = buildAttestation({ ...base, texts: { ...texts, soul: s.normalize('NFD') } });
assert.notEqual((nfd.digests as Record<string, string>).soul, (a.digests as Record<string, string>).soul);
});
test('A2 the schema string is stable', () => {
const a = buildAttestation(base), b = buildAttestation({ ...base, version: '2.0.0', date: '2027-01-01', texts: { ...texts, charter: 'other' } });
assert.equal(a.schema, 'string version,string date,string[] names,bytes32[] digests,string[] cids');
assert.equal(a.schema, b.schema);
assert.equal(a.schemaUid, b.schemaUid);
assert.equal(a.schemaUid, schemaUid(CHARTER_SCHEMA, ZERO_ADDRESS, true));
assert.match(a.schemaUid, /^0x[0-9a-f]{64}$/);
assert.notEqual(schemaUid(CHARTER_SCHEMA, ZERO_ADDRESS, false), a.schemaUid);
const nr = buildAttestation({ ...base, revocable: false });
assert.equal(nr.request.data.revocable, false);
assert.equal(nr.schemaUid, schemaUid(CHARTER_SCHEMA, ZERO_ADDRESS, false));
});
test('A3 a changed text changes its digest, and only its digest', () => {
const a = buildAttestation(base) as any;
for (const n of ['charter', 'constitution', 'soul']) {
const b = buildAttestation({ ...base, texts: { ...texts, [n]: (texts as any)[n] + ' ' } }) as any;
assert.notEqual(b.digests[n], a.digests[n], n);
assert.notEqual(b.data, a.data);
for (const m of ['charter', 'constitution', 'soul']) if (m !== n) assert.equal(b.digests[m], a.digests[m]);
}
const flip = buildAttestation({ ...base, texts: { ...texts, charter: 'The Founding Charter of Kognai,\n' } }) as any;
assert.notEqual(flip.digests.charter, a.digests.charter);
});
test('A4 one EAS payload: the data decodes with the Charter verifier to the same fields', () => {
const a = buildAttestation(base);
const d = decodeCharterData(a.data);
assert.deepEqual(d, { version: '1.0.0', date: '2026-11-14', names: ['charter', 'constitution', 'soul'], digests: Object.values(a.digests), cids: [CID0, CID1, CID2] });
assert.equal(a.request.schema, a.schemaUid);
assert.equal(a.request.data.data, a.data);
assert.deepEqual([a.request.data.expirationTime, a.request.data.value, a.request.data.recipient], [0n, 0n, ZERO_ADDRESS]);
});
test('A5 deterministic: same input, same output; key order does not matter', () => {
const a = buildAttestation(base);
const b = buildAttestation({ ...base, texts: { soul: texts.soul, charter: texts.charter, constitution: texts.constitution }, cids: { soul: CID2, constitution: CID1, charter: CID0 } });
assert.deepEqual(b, a);
});
test('A6 array form: [charter, constitution, soul] in, digests array out', () => {
const a = buildAttestation({ ...base, texts: [texts.charter, texts.constitution, texts.soul], cids: [CID0, CID1, CID2] });
assert.deepEqual(a.digests, [node(texts.charter), node(texts.constitution), node(texts.soul)]);
assert.equal(a.data, buildAttestation(base).data);
});
test('A7 extra named texts come after the three, alphabetically', () => {
const a = buildAttestation({ ...base, texts: { ...texts, zeta: 'z', annex: 'a' }, cids: { ...cids, zeta: CID1, annex: CID0 } });
assert.deepEqual(a.names, ['charter', 'constitution', 'soul', 'annex', 'zeta']);
assert.deepEqual(decodeCharterData(a.data).names, a.names);
});
test('A8 invalid input is refused with a plain message', () => {
const bad = (x: unknown, re: RegExp) => assert.throws(() => buildAttestation(x as never), re);
bad(undefined, /both be arrays or both be objects/);
bad({ ...base, cids: [CID0, CID1, CID2] }, /both be arrays or both be objects/);
bad({ ...base, texts: ['a', 'b', 'c'], cids }, /both be arrays or both be objects/);
bad({ ...base, texts: null }, /both be arrays or both be objects/);
bad({ ...base, cids: null }, /both be arrays or both be objects/);
bad({ ...base, texts: ['a', 'b'], cids: [CID0, CID1] }, /exactly the charter, constitution and soul/);
bad({ ...base, texts: ['a', 'b', 'c'], cids: [CID0, CID1] }, /one id per text/);
bad({ ...base, texts: { charter: 'a', constitution: 'b' } }, /missing the soul text/);
bad({ ...base, texts: { ...texts, 'Bad Name': 'x' }, cids: { ...cids, 'Bad Name': CID0 } }, /must be lowercase/);
bad({ ...base, cids: { charter: CID0, constitution: CID1 } }, /same names as texts/);
bad({ ...base, cids: { charter: CID0, constitution: CID1, other: CID2 } }, /same names as texts/);
bad({ ...base, texts: { ...texts, soul: '' } }, /soul text is empty/);
bad({ ...base, texts: { ...texts, soul: 5 } }, /soul text must be a string or bytes/);
bad({ ...base, cids: { ...cids, charter: 'not-a-cid' } }, /charter cid is not an IPFS id/);
bad({ ...base, version: ' ' }, /version must be/);
bad({ ...base, version: 1 }, /version must be/);
bad({ ...base, version: 'v'.repeat(65) }, /version must be/);
bad({ ...base, date: '2026-02-30' }, /calendar date/);
bad({ ...base, date: 20261114 }, /calendar date/);
bad({ ...base, resolver: 'nobody' }, /resolver must be an address/);
bad({ ...base, resolver: 7 }, /resolver must be an address/);
assert.equal(isCid(CID0) && isCid(CID1) && !isCid(5) && !isCid('Qm123'), true);
});
agentgate-source: code mirror on Cloudflare Pages. All repos