2021-03-04 20:11:58 +00:00
|
|
|
import { newKey } from './cryptography.js';
|
2021-03-23 11:18:57 +00:00
|
|
|
import { attenuate, KEY_LENGTH, mint, Caveat, Rewrite, sturdyEncode, validate } from './sturdy.js';
|
2021-03-04 20:11:58 +00:00
|
|
|
import * as RW from './rewrite.js';
|
2021-03-10 22:49:34 +00:00
|
|
|
import { Bytes, Dictionary } from '@preserves/core';
|
2021-03-11 20:56:35 +00:00
|
|
|
import { Ref } from 'actor.js';
|
2021-03-04 20:11:58 +00:00
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const m1 = await mint('hello world', new Bytes(KEY_LENGTH));
|
2021-03-23 11:18:57 +00:00
|
|
|
console.log(m1);
|
2021-03-23 18:18:26 +00:00
|
|
|
const m2 = await attenuate(m1, Caveat.Rewrite(Rewrite({
|
|
|
|
pattern: RW.Pattern.PBind(RW.PBind({
|
|
|
|
name: Symbol.for('a'),
|
|
|
|
pattern: RW.Pattern.PCompound(RW.PCompound({
|
|
|
|
ctor: RW.ConstructorSpec.CRec(RW.CRec({
|
|
|
|
label: Symbol.for('says'),
|
|
|
|
arity: 2
|
|
|
|
})),
|
|
|
|
members: new Dictionary<Ref, RW.Pattern>([
|
|
|
|
[0, RW.Pattern.Lit(RW.Lit('Tony'))]])
|
|
|
|
}))
|
|
|
|
})),
|
|
|
|
template: RW.Template.TRef(RW.TRef(Symbol.for('a')))
|
|
|
|
})));
|
2021-03-23 11:18:57 +00:00
|
|
|
console.log(m2);
|
2021-03-04 20:11:58 +00:00
|
|
|
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)));
|
2021-03-23 11:18:57 +00:00
|
|
|
m2.oid = 'hello world2';
|
|
|
|
console.log(m2);
|
2021-03-04 20:11:58 +00:00
|
|
|
console.log('should be false:', await validate(m2, new Bytes(KEY_LENGTH)));
|
2021-03-23 11:18:57 +00:00
|
|
|
m2.oid = 'hello world';
|
|
|
|
console.log(m2);
|
2021-03-04 20:11:58 +00:00
|
|
|
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());
|
2021-03-23 11:18:57 +00:00
|
|
|
console.log(sturdyEncode(RW.fromSturdyRef(m2)).asPreservesText());
|
2021-03-04 20:11:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main();
|