add: decrypt opposite of secret

This commit is contained in:
Hadar Rottenberg 2020-01-13 13:00:51 +02:00
parent 761030d721
commit dab91974c1
4 changed files with 90 additions and 7 deletions

37
sea.js
View File

@ -1087,6 +1087,43 @@
}());
return gun;
}
/**
* returns the decrypted value, encrypted by secret
* @returns {Promise<any>}
*/
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');

View File

@ -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<any>}
*/
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

View File

@ -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

View File

@ -7,4 +7,4 @@
}));
return cb? p.then(cb) : p;
}