Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #406 from 3box/release/1.7.2
Browse files Browse the repository at this point in the history
Release/1.7.2
  • Loading branch information
oed authored Apr 30, 2019
2 parents 1105624 + 092044b commit 6acc347
Show file tree
Hide file tree
Showing 21 changed files with 180 additions and 120 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Discord](https://img.shields.io/discord/484729862368526356.svg?style=for-the-badge)](https://discordapp.com/invite/Z3f3Cxy)
[![npm](https://img.shields.io/npm/dt/3box.svg?style=for-the-badge)](https://www.npmjs.com/package/3box)
[![npm](https://img.shields.io/npm/v/3box.svg?style=for-the-badge)](https://www.npmjs.com/package/3box)
[![Codecov](https://img.shields.io/codecov/c/github/uport-project/3box-js.svg?style=for-the-badge)](https://codecov.io/gh/uport-project/3box-js)
[![Codecov](https://img.shields.io/codecov/c/github/3box/3box-js.svg?style=for-the-badge)](https://codecov.io/gh/3box/3box-js)
[![Twitter Follow](https://img.shields.io/twitter/follow/3boxdb.svg?style=for-the-badge&label=Twitter)](https://twitter.com/3boxdb)
[![Greenkeeper badge](https://badges.greenkeeper.io/3box/3box-js.svg)](https://greenkeeper.io/)

Expand Down
5 changes: 5 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes

## v1.7.2 - 2019-04-30
* Fix: Don't allow setting values wihtout a 'key'
* Fix: Ensure that linkProfile only happens once
* Fix: Import keys in correct format

## v1.7.1 - 2019-04-25
* Fix: Throw error on openSpace if user denies consent
* Fix: Return correct timestamp format in metadata
Expand Down
132 changes: 55 additions & 77 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "3box",
"version": "1.7.1",
"version": "1.7.2",
"description": "Interact with user data",
"main": "lib/3box.js",
"directories": {
"lib": "lib"
},
"scripts": {
"lint": "./node_modules/.bin/standard --verbose src/**",
"test": "rm -rf ./tmp ; jest --forceExit --coverage --runInBand --testURL=\"http://localhost\"",
"test": "rm -rf ./tmp ; jest --detectOpenHandles --coverage --runInBand --testURL=\"http://localhost\"",
"build:es5": "rm -rf ./lib; ./node_modules/.bin/babel src --out-dir lib --ignore=src/__tests__/,src/__mocks__/",
"build:dist": "./node_modules/.bin/webpack --config webpack.config.js --mode=development",
"build:dist:dev": "./node_modules/.bin/webpack --config webpack.dev.config.js --mode=development",
Expand Down Expand Up @@ -45,7 +45,6 @@
"dependencies": {
"@babel/runtime": "^7.1.2",
"did-jwt": "^0.1.1",
"elliptic": "^6.4.1",
"ethers": "^4.0.20",
"graphql-request": "^1.8.2",
"https-did-resolver": "^0.1.0",
Expand Down
2 changes: 1 addition & 1 deletion readme-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Discord](https://img.shields.io/discord/484729862368526356.svg?style=for-the-badge)](https://discordapp.com/invite/Z3f3Cxy)
[![npm](https://img.shields.io/npm/dt/3box.svg?style=for-the-badge)](https://www.npmjs.com/package/3box)
[![npm](https://img.shields.io/npm/v/3box.svg?style=for-the-badge)](https://www.npmjs.com/package/3box)
[![Codecov](https://img.shields.io/codecov/c/github/uport-project/3box-js.svg?style=for-the-badge)](https://codecov.io/gh/uport-project/3box-js)
[![Codecov](https://img.shields.io/codecov/c/github/3box/3box-js.svg?style=for-the-badge)](https://codecov.io/gh/3box/3box-js)
[![Twitter Follow](https://img.shields.io/twitter/follow/3boxdb.svg?style=for-the-badge&label=Twitter)](https://twitter.com/3boxdb)
[![Greenkeeper badge](https://badges.greenkeeper.io/3box/3box-js.svg)](https://greenkeeper.io/)

Expand Down
17 changes: 9 additions & 8 deletions src/3box.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class Box {
this._orbitdb = new OrbitDB(this._ipfs, opts.orbitPath) // , { cache })
globalOrbitDB = this._orbitdb

const key = this._3id.getKeyringBySpaceName(rootStoreName).getDBKey()
const dbKey = this._3id.getKeyringBySpaceName(rootStoreName).getDBKey()
const key = await this._orbitdb.keystore.importPrivateKey(dbKey)
this._rootStore = await this._orbitdb.feed(rootStoreName, {
key,
write: [key.getPublic('hex')]
Expand Down Expand Up @@ -423,7 +424,13 @@ class Box {
consent_signature: consent.sig,
linked_did: did
}
await this.public.set('ethereum_proof', linkData)
await this.public.set('ethereum_proof', linkData, { noLink: true })
}

// Ensure we self-published our did
if (!(await this.public.get('proof_did'))) {
// we can just sign an empty JWT as a proof that we own this DID
await this.public.set('proof_did', await this._3id.signJWT(), { noLink: true })
}

// Send consentSignature to 3box-address-server to link profile with ethereum address
Expand All @@ -432,12 +439,6 @@ class Box {
} catch (err) {
console.error(err)
}

// Ensure we self-published our did
if (!(await this.public.get('proof_did'))) {
// we can just sign an empty JWT as a proof that we own this DID
await this.public.set('proof_did', await this._3id.signJWT())
}
}

async _ensurePinningNodeConnected (odbAddress, isThread) {
Expand Down
2 changes: 1 addition & 1 deletion src/3id/__tests__/keyring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Keyring', () => {

it('getDBKey works correctly', async () => {
const key = keyring1.getDBKey()
expect(key.getPublic('hex')).toEqual('048aaa695fa16f2a2279e1de718d80e00f4f4ddf30fe8674bbdb9e1f11778c2f77f8ffb5ad2bd3f1f9840b3462c26f756ec0f47626894c20ed247145f5c0e26fe8')
expect(key).toEqual('68957df606e586990a6286239987aaf4c902971ae642d50609d35ff36fb0728a')
})

it('signs data correctly', async () => {
Expand Down
4 changes: 1 addition & 3 deletions src/3id/keyring.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { HDNode } = require('ethers').utils
const nacl = require('tweetnacl')
nacl.util = require('tweetnacl-util')
const EC = require('elliptic').ec
const ec = new EC('secp256k1')
const SimpleSigner = require('did-jwt').SimpleSigner
const { sha256 } = require('../utils/index')

Expand Down Expand Up @@ -60,7 +58,7 @@ class Keyring {
}

getDBKey () {
return ec.keyFromPrivate(this.signingKey.privateKey.slice(2))
return this.signingKey.privateKey.slice(2)
}

getDBSalt () {
Expand Down
22 changes: 12 additions & 10 deletions src/__tests__/3box.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const testUtils = require('./testUtils')
const OrbitDB = require('orbit-db')
const Pubsub = require('orbit-db-pubsub')
const jsdom = require('jsdom')
const IPFS = require('ipfs')
const Box = require('../3box')
global.window = new jsdom.JSDOM().window

Expand Down Expand Up @@ -31,7 +30,7 @@ jest.mock('../3id', () => {
muportFingerprint: managementKey === '0x12345' ? 'b932fe7ab' : 'ab8c73d8f',
getDidDocument: () => { return { managementKey } },
getKeyringBySpaceName: () => {
return { getDBKey: () => ec.keyFromPrivate('f917ac6883f88798a8ce39821fa523f2acd17c0ba80c724f219367e76d8f2c46') }
return { getDBKey: () => 'f917ac6883f88798a8ce39821fa523f2acd17c0ba80c724f219367e76d8f2c46' }
}
}
}
Expand Down Expand Up @@ -146,7 +145,7 @@ describe('3Box', () => {
let ipfs, pubsub, boxOpts, ipfsBox, box
jest.setTimeout(30000)

beforeEach(async () => {
beforeAll(async () => {
if (!ipfs) ipfs = await testUtils.initIPFS(0)
const ipfsMultiAddr = (await ipfs.id()).addresses[0]
if (!pubsub) pubsub = new Pubsub(ipfs, (await ipfs.id()).id)
Expand All @@ -159,9 +158,7 @@ describe('3Box', () => {
}

if (!ipfsBox) {
//ipfsBox = new IPFS(IPFS_OPTIONS)
ipfsBox = await testUtils.initIPFS(1)
//await new Promise((resolve, reject) => { ipfsBox.on('ready', () => resolve() )})
}

boxOpts = {
Expand All @@ -173,7 +170,9 @@ describe('3Box', () => {
iframeStore: false,
pinningNode: ipfsMultiAddr
}
})

beforeEach(async () => {
mockedUtils.openBoxConsent.mockClear()
mockedUtils.fetchJson.mockClear()
mockedUtils.getLinkConsent.mockClear()
Expand All @@ -182,10 +181,11 @@ describe('3Box', () => {
})

afterAll(async () => {
await pubsub.disconnect()
//await ipfs.stop()
})

await pubsub.disconnect()
await box.close()
await testUtils.stopIPFS(ipfs, 0)
await testUtils.stopIPFS(ipfsBox, 1)
})

it('should openBox correctly', async () => {
const publishPromise = new Promise((resolve, reject) => {
Expand Down Expand Up @@ -255,6 +255,7 @@ describe('3Box', () => {
expect(mockedUtils.fetchJson).toHaveBeenCalledTimes(1)
await publishPromise
pubsub.unsubscribe('3box-pinning')
await orbitdb.stop()
})

it('should open spaces correctly', async () => {
Expand Down Expand Up @@ -304,7 +305,7 @@ describe('3Box', () => {

// It will check the self-signed did
expect(box.public.get).toHaveBeenNthCalledWith(2, 'proof_did')
expect(box.public.set).toHaveBeenNthCalledWith(2, 'proof_did', 'veryJWT,did:muport:Qmsdfp98yw4t7')
expect(box.public.set).toHaveBeenNthCalledWith(2, 'proof_did', 'veryJWT,did:muport:Qmsdfp98yw4t7', { noLink: true })

expect(global.console.error).toHaveBeenCalledTimes(1)
expect(mockedUtils.fetchJson).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -425,6 +426,7 @@ describe('3Box', () => {
})

it('should get profile (when API is used)', async () => {
delete boxOpts.useCacheService
const profile = await Box.getProfile('0x12345', boxOpts)
expect(profile).toEqual({
name: 'zfer',
Expand Down
20 changes: 14 additions & 6 deletions src/__tests__/keyValueStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const STORE_NAME = '09ab7cd93f9e.public'

const THREEID_MOCK = {
getKeyringBySpaceName: () => {
return { getDBKey: () => ec.keyFromPrivate('f917ac6883f88798a8ce39821fa523f2acd17c0ba80c724f219367e76d8f2c46') }
return { getDBKey: () => 'f917ac6883f88798a8ce39821fa523f2acd17c0ba80c724f219367e76d8f2c46' }
}
}

Expand Down Expand Up @@ -67,6 +67,11 @@ describe('KeyValueStore', () => {
expect(ensureConnected).toHaveBeenCalledTimes(2)
})

it('should throw if key not given', async () => {
expect(keyValueStore.set()).rejects.toEqual(new Error('key is a required argument'))
expect(keyValueStore.remove()).rejects.toEqual(new Error('key is a required argument'))
})

it('should sync an old profile correctly', async () => {
let ipfs2 = await utils.initIPFS(3)
let orbitdb2 = new OrbitDB(ipfs2, './tmp/orbitdb2')
Expand All @@ -81,7 +86,7 @@ describe('KeyValueStore', () => {
expect(await keyValueStore2.get('key2')).toBeUndefined()
expect(await keyValueStore2.get('key3')).toBeUndefined()
await orbitdb2.stop()
// await ipfs2.stop()
await utils.stopIPFS(ipfs2, 3)
})

describe('metdata', () => {
Expand Down Expand Up @@ -150,8 +155,11 @@ describe('KeyValueStore', () => {
})
})

// afterAll(async () => {
// await orbitdb.stop()
// await ipfs.stop()
// })
it('should including ALL entries, including DEL ops', async () => {
})

afterAll(async () => {
await orbitdb.stop()
await utils.stopIPFS(ipfs, 2)
})
})
5 changes: 5 additions & 0 deletions src/__tests__/privateStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ describe('PrivateStore', () => {
expect(await privateStore.get('key2')).toBeNull()
})

it('should throw if key not given', async () => {
expect(privateStore.set()).rejects.toEqual(new Error('Entry to encrypt cannot be undefined'))
expect(privateStore.remove()).rejects.toEqual(new Error('key is a required argument'))
})

describe('log', () => {

beforeEach(async () => {
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/publicStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ describe('PublicStore', () => {
expect(linkProfile).toHaveBeenCalledWith()
})

it('should not call linkProfile when noLink is true', async () => {
linkProfile.mockClear()
await publicStore._load()
let ret = await publicStore.set('key1', 'value1', { noLink: true })
expect(ret).toEqual(true)
expect(linkProfile).toHaveBeenCalledTimes(0)
})

it('should throw if key not given', async () => {
expect(publicStore.set()).rejects.toEqual(new Error('key is a required argument'))
})

it('should return profile correctly', async () => {
expect(await publicStore.all()).toEqual({ key1: 'value1' })
})
Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/space.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ describe('Space', () => {
k3: 'v3'
})
})

it('should throw if key not given', async () => {
expect(space.public.set()).rejects.toEqual(new Error('key is a required argument'))
expect(space.public.remove()).rejects.toEqual(new Error('key is a required argument'))
})
})

describe('private store reducer', () => {
Expand Down Expand Up @@ -142,6 +147,11 @@ describe('Space', () => {
k3: 'sv3'
})
})

it('should throw if key not given', async () => {
expect(space.private.set()).rejects.toEqual(new Error('key is a required argument'))
expect(space.private.remove()).rejects.toEqual(new Error('key is a required argument'))
})
})

describe('Threads', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/testUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const IPFS = require('ipfs')
const fs = require('fs')

const CONF_1 = {
EXPERIMENTAL: {
Expand Down Expand Up @@ -116,5 +117,11 @@ module.exports = {
ipfs.on('error', reject)
ipfs.on('ready', () => resolve(ipfs))
})
},
stopIPFS: async (ipfs, useAltConf) => {
// seems to be an issue with the api file not being present when trying to close ipfs
const apiFilePath = CONFS[useAltConf].repo + 'api'
fs.closeSync(fs.openSync(apiFilePath, 'w'))
await ipfs.stop()
}
}
14 changes: 12 additions & 2 deletions src/__tests__/thread.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const MSG4 = 'message4'

const THREEID1_MOCK = {
_mainKeyring: {
getDBKey: () => ec.keyFromPrivate('f917ac6883f88798a8ce39821fa523f2acd17c0ba80c724f219367e76d8f2c46')
getDBKey: () => 'f917ac6883f88798a8ce39821fa523f2acd17c0ba80c724f219367e76d8f2c46'
},
getDid: () => 'did:3:mydid1'
}
const THREEID2_MOCK = {
_mainKeyring: {
getDBKey: () => ec.keyFromPrivate('f977777aaaaaaabbbbbbb9821fa523f2acd17c0ba80c724f219367e76d8f2c46')
getDBKey: () => 'f977777aaaaaaabbbbbbb9821fa523f2acd17c0ba80c724f219367e76d8f2c46'
},
getDid: () => 'did:3:mydid2'
}
Expand Down Expand Up @@ -147,5 +147,15 @@ describe('Thread', () => {
expect(posts1[1].message).toEqual(MSG2)
expect(posts1[1].postId).toEqual(posts2[1].postId)
})

afterAll(async () => {
await orbitdb2.stop()
await utils.stopIPFS(ipfs2, 5)
})
})

afterAll(async () => {
await orbitdb.stop()
await utils.stopIPFS(ipfs, 4)
})
})
7 changes: 6 additions & 1 deletion src/keyValueStore.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { throwIfUndefined } = require('./utils/index')

class KeyValueStore {
/**
* Please use **box.public** or **box.private** to get the instance of this class
Expand Down Expand Up @@ -47,6 +49,7 @@ class KeyValueStore {
* @return {Boolean} true if successful
*/
async set (key, value) {
throwIfUndefined(key, 'key')
this._requireLoad()
this._ensureConnected()
const timeStamp = new Date().getTime()
Expand All @@ -61,6 +64,7 @@ class KeyValueStore {
* @return {Boolean} true if successful
*/
async remove (key) {
throwIfUndefined(key, 'key')
this._requireLoad()
this._ensureConnected()
await this._db.del(key)
Expand Down Expand Up @@ -113,7 +117,8 @@ class KeyValueStore {
}

async _load (odbAddress) {
const key = this._3id.getKeyringBySpaceName(this._name).getDBKey()
const dbKey = this._3id.getKeyringBySpaceName(this._name).getDBKey()
const key = await this._orbitdb.keystore.importPrivateKey(dbKey)
this._db = await this._orbitdb.keyvalue(odbAddress || this._name, {
key,
write: [key.getPublic('hex')]
Expand Down
1 change: 1 addition & 0 deletions src/privateStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class PrivateStore extends KeyValueStore {
}

_genDbKey (key) {
utils.throwIfUndefined(key, 'key')
return utils.sha256Multihash(this._salt + key)
}

Expand Down
Loading

0 comments on commit 6acc347

Please sign in to comment.