247 lines
7.8 KiB
JavaScript
247 lines
7.8 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Envelope = exports.ArgPair = exports.Syncable = void 0;
|
|
var Automerge = __importStar(require("automerge"));
|
|
var constants_1 = require("./constants");
|
|
var fullSpec = {
|
|
name: 'cic',
|
|
version: '1',
|
|
ext: {
|
|
network: constants_1.cryptoSpec,
|
|
engine: constants_1.engineSpec,
|
|
},
|
|
};
|
|
var Envelope = /** @class */ (function () {
|
|
function Envelope(payload) {
|
|
this.o = fullSpec;
|
|
this.set(payload);
|
|
}
|
|
Envelope.prototype.set = function (payload) {
|
|
this.o['payload'] = payload;
|
|
};
|
|
Envelope.prototype.get = function () {
|
|
return this.o['payload'];
|
|
};
|
|
Envelope.prototype.toJSON = function () {
|
|
return JSON.stringify(this.o);
|
|
};
|
|
Envelope.fromJSON = function (s) {
|
|
var e = new Envelope(undefined);
|
|
e.o = JSON.parse(s);
|
|
return e;
|
|
};
|
|
Envelope.prototype.unwrap = function () {
|
|
return Syncable.fromJSON(this.o['payload']);
|
|
};
|
|
return Envelope;
|
|
}());
|
|
exports.Envelope = Envelope;
|
|
var ArgPair = /** @class */ (function () {
|
|
function ArgPair(k, v) {
|
|
this.k = k;
|
|
this.v = v;
|
|
}
|
|
return ArgPair;
|
|
}());
|
|
exports.ArgPair = ArgPair;
|
|
var SignablePart = /** @class */ (function () {
|
|
function SignablePart(s) {
|
|
this.s = s;
|
|
}
|
|
SignablePart.prototype.digest = function () {
|
|
return this.s;
|
|
};
|
|
return SignablePart;
|
|
}());
|
|
function orderDict(src) {
|
|
var dst;
|
|
if (Array.isArray(src)) {
|
|
dst = [];
|
|
src.forEach(function (v) {
|
|
if (typeof (v) == 'object') {
|
|
v = orderDict(v);
|
|
}
|
|
dst.push(v);
|
|
});
|
|
}
|
|
else {
|
|
dst = {};
|
|
Object.keys(src).sort().forEach(function (k) {
|
|
var v = src[k];
|
|
if (typeof (v) == 'object') {
|
|
v = orderDict(v);
|
|
}
|
|
dst[k] = v;
|
|
});
|
|
}
|
|
return dst;
|
|
}
|
|
var Syncable = /** @class */ (function () {
|
|
// TODO: Move data to sub-object so timestamp, id, signature don't collide
|
|
function Syncable(id, v) {
|
|
this.id = id;
|
|
var o = {
|
|
'id': id,
|
|
'timestamp': Math.floor(Date.now() / 1000),
|
|
'data': v,
|
|
};
|
|
//this.m = Automerge.from(v)
|
|
this.m = Automerge.from(o);
|
|
}
|
|
Syncable.prototype.setSigner = function (signer) {
|
|
var _this = this;
|
|
this.signer = signer;
|
|
this.signer.onsign = function (s) {
|
|
_this.wrap(s);
|
|
};
|
|
};
|
|
// TODO: To keep integrity, the non-link key/value pairs for each step also need to be hashed
|
|
Syncable.prototype.digest = function () {
|
|
var links = [];
|
|
Automerge.getAllChanges(this.m).forEach(function (ch) {
|
|
var op = ch['ops'];
|
|
ch['ops'].forEach(function (op) {
|
|
if (op['action'] == 'link') {
|
|
//console.log('op link', op);
|
|
links.push([op['obj'], op['value']]);
|
|
}
|
|
});
|
|
});
|
|
//return JSON.stringify(links);
|
|
var j = JSON.stringify(links);
|
|
return Buffer.from(j).toString('base64');
|
|
};
|
|
Syncable.prototype.wrap = function (s) {
|
|
this.m = Automerge.change(this.m, 'sign', function (doc) {
|
|
doc['signature'] = s;
|
|
});
|
|
this.e = new Envelope(this.toJSON());
|
|
if (this.onwrap !== undefined) {
|
|
this.onwrap(this.e);
|
|
}
|
|
};
|
|
// private _verifyLoop(i:number, history:Array<any>, signable:Signable, result:boolean) {
|
|
// if (!result) {
|
|
// this.onauthenticate(false);
|
|
// return;
|
|
// } else if (history.length == 0) {
|
|
// this.onauthenticate(true);
|
|
// return;
|
|
// }
|
|
// const h = history.shift()
|
|
// if (i % 2 == 0) {
|
|
// i++;
|
|
// signable = {
|
|
// digest: () => {
|
|
// return Automerge.save(h.snapshot)
|
|
// },
|
|
// };
|
|
// this._verifyLoop(i, history, signable, true);
|
|
// } else {
|
|
// i++;
|
|
// const signature = h.snapshot['signature'];
|
|
// console.debug('signature', signature, signable.digest());
|
|
// this.signer.onverify = (v) => {
|
|
// this._verifyLoop(i, history, signable, v)
|
|
// }
|
|
// this.signer.verify(signable, signature);
|
|
// }
|
|
// }
|
|
//
|
|
// // TODO: This should replay the graph and check signatures on each step
|
|
// public _authenticate(full:boolean=false) {
|
|
// let h = Automerge.getHistory(this.m);
|
|
// h.forEach((m) => {
|
|
// //console.debug(m.snapshot);
|
|
// });
|
|
// const signable = {
|
|
// digest: () => { return '' },
|
|
// }
|
|
// if (!full) {
|
|
// h = h.slice(h.length-2);
|
|
// }
|
|
// this._verifyLoop(0, h, signable, true);
|
|
// }
|
|
Syncable.prototype.authenticate = function (full) {
|
|
var _this = this;
|
|
if (full === void 0) { full = false; }
|
|
if (full) {
|
|
console.warn('only doing shallow authentication for now, sorry');
|
|
}
|
|
//console.log('authenticating', signable.digest());
|
|
//console.log('signature', this.m.signature);
|
|
this.signer.onverify = function (v) {
|
|
//this._verifyLoop(i, history, signable, v)
|
|
_this.onauthenticate(v);
|
|
};
|
|
this.signer.verify(this.m.signature.digest, this.m.signature);
|
|
};
|
|
Syncable.prototype.sign = function () {
|
|
//this.signer.prepare(this);
|
|
this.signer.sign(this.digest());
|
|
};
|
|
Syncable.prototype.update = function (changes, changesDescription) {
|
|
this.m = Automerge.change(this.m, changesDescription, function (m) {
|
|
changes.forEach(function (c) {
|
|
var path = c.k.split('.');
|
|
var target = m['data'];
|
|
while (path.length > 1) {
|
|
var part = path.shift();
|
|
target = target[part];
|
|
}
|
|
target[path[0]] = c.v;
|
|
});
|
|
m['timestamp'] = Math.floor(Date.now() / 1000);
|
|
});
|
|
};
|
|
Syncable.prototype.replace = function (o, changesDescription) {
|
|
this.m = Automerge.change(this.m, changesDescription, function (m) {
|
|
Object.keys(o).forEach(function (k) {
|
|
m['data'][k] = o[k];
|
|
});
|
|
Object.keys(m).forEach(function (k) {
|
|
if (o[k] == undefined) {
|
|
delete m['data'][k];
|
|
}
|
|
});
|
|
m['timestamp'] = Math.floor(Date.now() / 1000);
|
|
});
|
|
};
|
|
Syncable.prototype.merge = function (s) {
|
|
this.m = Automerge.merge(s.m, this.m);
|
|
};
|
|
Syncable.prototype.toJSON = function () {
|
|
var s = Automerge.save(this.m);
|
|
var o = JSON.parse(s);
|
|
var oo = orderDict(o);
|
|
return JSON.stringify(oo);
|
|
};
|
|
Syncable.fromJSON = function (s) {
|
|
var doc = Automerge.load(s);
|
|
var y = new Syncable(doc['id'], {});
|
|
y.m = doc;
|
|
return y;
|
|
};
|
|
return Syncable;
|
|
}());
|
|
exports.Syncable = Syncable;
|