novy-syndicate/src/sturdy-demo.ts

31 lines
1.6 KiB
TypeScript

import { newKey } from './cryptography.js';
import { attenuate, KEY_LENGTH, mint, Rewrite, sturdyEncode, validate } from './sturdy.js';
import * as RW from './rewrite.js';
import { Bytes, Dictionary } from 'preserves';
async function main() {
const m1 = await mint('hello world', new Bytes(KEY_LENGTH));
console.log(m1.asPreservesText());
const m2 = await attenuate(m1, Rewrite(RW.PBind('a',
RW.PCompound(RW.CRec(Symbol.for('says'), 2),
new Dictionary<RW.Pattern, never>([
[0, RW.Lit('Tony')]]))),
RW.TRef('a')));
console.log(m2.asPreservesText());
console.log('should be true:', await validate(m1, new Bytes(KEY_LENGTH)));
console.log('should be true:', await validate(m2, new Bytes(KEY_LENGTH)));
console.log('should be false:', await validate(m2, Bytes.of(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)));
m2[0] = 'hello world2';
console.log(m2.asPreservesText());
console.log('should be false:', await validate(m2, new Bytes(KEY_LENGTH)));
m2[0] = 'hello world';
console.log(m2.asPreservesText());
console.log('should be true:', await validate(m2, new Bytes(KEY_LENGTH)));
console.log('should be false:', await validate(m2, await newKey()));
console.log((await newKey()).asPreservesText());
console.log((await newKey()).asPreservesText());
console.log(sturdyEncode(m2).asPreservesText());
}
main();