novy-syndicate/src/sturdy-demo.ts

33 lines
1.5 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/core';
import { Ref } from 'actor.js';
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(Symbol.for('a'),
RW.PCompound(RW.CRec(Symbol.for('says'), 2),
new Dictionary<RW.Pattern, Ref>([
[0, RW.Lit('Tony')]]))),
RW.TRef(Symbol.for('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();