2021-03-04 20:11:58 +00:00
|
|
|
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());
|
2021-03-05 20:37:58 +00:00
|
|
|
const m2 = await attenuate(m1, Rewrite(RW.PBind(Symbol.for('a'),
|
2021-03-04 20:11:58 +00:00
|
|
|
RW.PCompound(RW.CRec(Symbol.for('says'), 2),
|
|
|
|
new Dictionary<RW.Pattern, never>([
|
|
|
|
[0, RW.Lit('Tony')]]))),
|
2021-03-05 20:37:58 +00:00
|
|
|
RW.TRef(Symbol.for('a'))));
|
2021-03-04 20:11:58 +00:00
|
|
|
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();
|