Skip to content

Commit

Permalink
Added SHA256 hashing to Gunsafe Vault name
Browse files Browse the repository at this point in the history
This is a breaking change. Old vaults prior to this version will no longer work with this version.
  • Loading branch information
draeder committed Jul 30, 2022
1 parent 8bd1bf3 commit 430415f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cli/gunsafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
10 changes: 4 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import EventEmitter from 'events'
import crypto from 'crypto'
import Gun from 'gun'
import SEA from 'gun/sea.js'
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) => {
Expand All @@ -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){}}
Expand All @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 21 additions & 3 deletions test/gunsafe_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit 430415f

Please sign in to comment.