diff --git a/sea.js b/sea.js index f6a4b45a..02f4ef8e 100644 --- a/sea.js +++ b/sea.js @@ -1087,6 +1087,43 @@ }()); return gun; } + + /** + * returns the decrypted value, encrypted by secret + * @returns {Promise} + */ + User.prototype.decrypt = function(cb) { + let gun = this, + path = '' + gun.back(function(at) { + if (at.is) { + return + } + path += at.get || '' + }) + return gun + .then(async data => { + if (data == null) { + return + } + const user = gun.back(-1).user() + const pair = user.pair() + let sec = await user + .get('trust') + .get(pair.pub) + .get(path) + sec = await SEA.decrypt(sec, pair) + if (!sec) { + return data + } + let decrypted = await SEA.decrypt(data, sec) + return decrypted + }) + .then(res => { + cb && cb(res) + return res + }) + } module.exports = User })(USE, './create'); diff --git a/sea/create.js b/sea/create.js index e0946b40..0b77e5a4 100644 --- a/sea/create.js +++ b/sea/create.js @@ -226,6 +226,7 @@ } // If authenticated user wants to delete his/her account, let's support it! User.prototype.delete = async function(alias, pass, cb){ + console.log("user.delete() IS DEPRECATED AND WILL BE MOVED TO A MODULE!!!"); var gun = this, root = gun.back(-1), user = gun.back('user'); try { user.auth(alias, pass, function(ack){ @@ -267,6 +268,7 @@ return gun; } User.prototype.alive = async function(){ + console.log("user.alive() IS DEPRECATED!!!"); const gunRoot = this.back(-1) try { // All is good. Should we do something more with actual recalled data? @@ -286,25 +288,32 @@ console.log(ctx, ev) }) } + user.get('trust').get(path).put(theirPubkey); + + // do a lookup on this gun chain directly (that gets bob's copy of the data) + // do a lookup on the metadata trust table for this path (that gets all the pubkeys allowed to write on this path) + // do a lookup on each of those pubKeys ON the path (to get the collab data "layers") + // THEN you perform Jachen's mix operation + // and return the result of that to... } User.prototype.grant = function(to, cb){ console.log("`.grant` API MAY BE DELETED OR CHANGED OR RENAMED, DO NOT USE!"); - var gun = this, user = gun.back(-1).user(), pair = user.pair(), path = ''; + var gun = this, user = gun.back(-1).user(), pair = user._.sea, path = ''; gun.back(function(at){ if(at.is){ return } path += (at.get||'') }); (async function(){ - var enc, sec = await user.get('trust').get(pair.pub).get(path).then(); + var enc, sec = await user.get('grant').get(pair.pub).get(path).then(); sec = await SEA.decrypt(sec, pair); if(!sec){ sec = SEA.random(16).toString(); enc = await SEA.encrypt(sec, pair); - user.get('trust').get(pair.pub).get(path).put(enc); + user.get('grant').get(pair.pub).get(path).put(enc); } var pub = to.get('pub').then(); var epub = to.get('epub').then(); pub = await pub; epub = await epub; var dh = await SEA.secret(epub, pair); enc = await SEA.encrypt(sec, dh); - user.get('trust').get(pub).get(path).put(enc, cb); + user.get('grant').get(pub).get(path).put(enc, cb); }()); return gun; } @@ -325,5 +334,42 @@ }()); return gun; } + + /** + * returns the decrypted value, encrypted by secret + * @returns {Promise} + */ + User.prototype.decrypt = function(cb) { + let gun = this, + path = '' + gun.back(function(at) { + if (at.is) { + return + } + path += at.get || '' + }) + return gun + .then(async data => { + if (data == null) { + return + } + const user = gun.back(-1).user() + const pair = user.pair() + let sec = await user + .get('trust') + .get(pair.pub) + .get(path) + sec = await SEA.decrypt(sec, pair) + if (!sec) { + return data + } + let decrypted = await SEA.decrypt(data, sec) + return decrypted + }) + .then(res => { + cb && cb(res) + return res + }) + } module.exports = User \ No newline at end of file diff --git a/sea/shim.js b/sea/shim.js index 42716477..83c4c539 100644 --- a/sea/shim.js +++ b/sea/shim.js @@ -26,8 +26,8 @@ const isocrypto = require('isomorphic-webcrypto'); api.ossl = api.subtle = isocrypto.subtle; }catch(e){ - console.log("node-webcrypto-ossl and text-encoding may not be included by default, please add it to your package.json!"); - OSSL_WEBCRYPTO_OR_TEXT_ENCODING_NOT_INSTALLED; + console.log("text-encoding and @peculiar/webcrypto may not be included by default, please add it to your package.json!"); + TEXT_ENCODING_OR_PECULIAR_WEBCRYPTO_NOT_INSTALLED; }} module.exports = api diff --git a/sea/then.js b/sea/then.js index 31facfd1..6e6ded1e 100644 --- a/sea/then.js +++ b/sea/then.js @@ -7,4 +7,4 @@ })); return cb? p.then(cb) : p; } - + \ No newline at end of file