35 lines
1.6 KiB
TypeScript
35 lines
1.6 KiB
TypeScript
import { newKey } from './cryptography.js';
|
|
import { attenuate, KEY_LENGTH, mint, Caveat, 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);
|
|
const m2 = await attenuate(m1, Caveat.Rewrite(Rewrite(
|
|
RW.Pattern.PBind(RW.PBind(
|
|
Symbol.for('a'),
|
|
RW.Pattern.PCompound(RW.PCompound(
|
|
RW.ConstructorSpec.CRec(RW.CRec(Symbol.for('says'), 2)),
|
|
new Dictionary<Ref, RW.Pattern>([
|
|
[0, RW.Pattern.Lit(RW.Lit('Tony'))]]))))),
|
|
RW.Template.TRef(RW.TRef(Symbol.for('a'))))));
|
|
console.log(m2);
|
|
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.oid = 'hello world2';
|
|
console.log(m2);
|
|
console.log('should be false:', await validate(m2, new Bytes(KEY_LENGTH)));
|
|
m2.oid = 'hello world';
|
|
console.log(m2);
|
|
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(RW.fromSturdyRef(m2)).asPreservesText());
|
|
}
|
|
|
|
main();
|