From 430415f4824d73991d9182366f94e85e8451d552 Mon Sep 17 00:00:00 2001 From: draeder Date: Sat, 30 Jul 2022 17:32:46 -0500 Subject: [PATCH] Added SHA256 hashing to Gunsafe Vault name This is a breaking change. Old vaults prior to this version will no longer work with this version. --- cli/gunsafe.js | 2 +- index.js | 10 ++++------ package.json | 2 +- test/gunsafe_core.js | 24 +++++++++++++++++++++--- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/cli/gunsafe.js b/cli/gunsafe.js index 19fa5c6..50021cd 100755 --- a/cli/gunsafe.js +++ b/cli/gunsafe.js @@ -49,7 +49,7 @@ async function auth(line){ function keypair(){ let key = gun.gunsafe.key() - console.log(key) + console.log(JSON.stringify(key)) } function peers(line){ diff --git a/index.js b/index.js index 3f62ffe..bef5600 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -import EventEmitter from 'events' +import crypto from 'crypto' import Gun from 'gun' import SEA from 'gun/sea.js' import Pair from './pair.js' @@ -6,12 +6,11 @@ import Pair from './pair.js' Gun.chain.gunsafe = function(opts) { const gun = this - const events = new EventEmitter() - let pair gun.gunsafe = { name: async (key, name) => { - pair = await Pair(key, name) + let hash = crypto.createHash('SHA256').update(name).digest('hex') + pair = await Pair(key, hash) gun.user().auth(pair) }, put: async (name, data) => { @@ -24,7 +23,7 @@ Gun.chain.gunsafe = function(opts) { gun.user().get('gunsafe').get('items').get(name).once(async data => { if(!data) return cb('Record not found') data = await SEA.decrypt(data, pair) - try { data = data.join(' '); if(!run) cb(data) } + try { data = data.join(' '); if(!run && !global) cb(data) } catch (err){if(err){}} try { data = JSON.parse(data) } catch (err){if(err){}} @@ -44,7 +43,6 @@ Gun.chain.gunsafe = function(opts) { if(run){ try{ if(global === false) { - console.log('Running Function') let fn = new Function(data); fn() } diff --git a/package.json b/package.json index 4a0aa79..a540787 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gunsafe", - "version": "2.5.0", + "version": "2.6.0", "description": "A decentralized secure vault API built on Gun chain", "main": "index.js", "type": "module", diff --git a/test/gunsafe_core.js b/test/gunsafe_core.js index 2be947b..aeea099 100644 --- a/test/gunsafe_core.js +++ b/test/gunsafe_core.js @@ -18,15 +18,21 @@ describe('Test all gunsafe methods', async () => { let authenticated = gun.user().is expect(Object.keys(authenticated).length === 3).to.equal(true) }) - it('A single line record has been put to gunsafe', () => { + it('A single line record has been put to gunsafe', (done) => { gun.gunsafe.put('hi', 'hi?') + done() }) - it('A multiline record has been put to gunsafe', () => { + it('A multiline record has been put to gunsafe', (done) => { let doc = { [+new Date()]: 'hi?', [+new Date()+1]: 'hello?', } gun.gunsafe.put('hi', doc) + done() + }) + it('Adds a javascript function to gunsafe', (done) => { + gun.gunsafe.put('fn', 'let a = 1; let b = 2; global.value = a+b;') + done() }) it('Gunsafe can get the single line record', () => { gun.gunsafe.get('hi', false, false, data => { @@ -35,7 +41,19 @@ describe('Test all gunsafe methods', async () => { }) it('Gunsafe can get the multiline record', () => { gun.gunsafe.get('hi', false, false, data => { - expect(data === 'hi?').to.equal(true) + expect(global.value).to.equal(3) + }) + }) + it('Gunsafe can get and execute the function in global scope', (done) => { + gun.gunsafe.get('fn', true, false, data => { + expect(data === 'hi').to.equal(true) + done() + }) + }) + it('Gunsafe can get and execute the function in global scope', (done) => { + gun.gunsafe.get('fn', true, false, data => { + expect(data === 'hi').to.equal(true) + done() }) }) it('Gunsafe can list the available record names', () => {