// TODO: we probably should expose method for replacing the signature, this is too intrusive
s.m=Automerge.change(s.m,'sign',(doc)=>{
doc['signature']=wrappedData.s;
});
s.setSigner(signer);
s.onauthenticate=(v)=>{
console.log('vvv',v);
if(!v){
whohoo(undefined);
return;
}
constopts={
message: pgp.message.fromText(s.toJSON()),
publicKeys: keystore.getEncryptKeys(),
};
pgp.encrypt(opts).then((cipherText)=>{
constsql="INSERT INTO store (owner_fingerprint, hash, content) VALUES ('"+signer.fingerprint()+"', '"+digest+"', '"+cipherText.data+"') ON CONFLICT (hash) DO UPDATE SET content = EXCLUDED.content;";
db.query(sql,(e,rs)=>{
if(e!==null&&e!==undefined){
doh(e);
return;
}
whohoo(true);
});
});
};
s.authenticate(true)
});
}
functionhandleClientMergeGet(db,digest,keystore){
constsql="SELECT content FROM store WHERE hash = '"+digest+"'";
returnnewPromise<string>((whohoo,doh)=>{
db.query(sql,(e,rs)=>{
console.log('rs',e,rs);
if(e!==null&&e!==undefined){
doh(e);
return;
}elseif(rs.rowCount==0){// TODO fix the postgres/sqlite method name issues, this will now break on postgres
whohoo(undefined);
return;
}
constcipherText=rs.rows[0]['content'];
pgp.message.readArmored(cipherText).then((m)=>{
constopts={
message: m,
privateKeys:[keystore.getPrivateKey()],
};
pgp.decrypt(opts).then((plainText)=>{
consto=Syncable.fromJSON(plainText.data);
conste=newEnvelope(o);
whohoo(e.toJSON());
}).catch((e)=>{
console.error('decrypt',e);
doh(e);
});
}).catch((e)=>{
console.error('mesage',e);
doh(e);
});
});
});
}
// TODO: this still needs to merge with the stored version
constsql="INSERT INTO store (owner_fingerprint, hash, content) VALUES ('"+signer.fingerprint()+"', '"+digest+"', '"+cipherText.data+"') ON CONFLICT (hash) DO UPDATE SET content = EXCLUDED.content;";