Catalog npm
Back to packageRELEASE DETAILS

1.4.0-beta.1

npm

3box

Interact with user data

Published21 Feb 2019
Direct dependencies33
Transitive dependenciesNot calculated

README

Version 1.4.0-beta.1 documentation

CircleCI Discord npm npm Codecov Twitter Follow Greenkeeper badge

Install | Usage | Dapp data | Example | API Docs

3box-js

This is a library which allows you to set, get, and remove private and public data associated with an ethereum account. It can be used to store identity data, user settings, etc. by dapps that use a web3 enabled browser. The data will be retrievable as long as the user has access to the private key for the used ethereum account. The data is encrypted and can not be read by any third party that the user hasn't authorized. There is one shared space for data which all authorized dapps access by default, then there are spaces which dapps have to request explicit consent to access.

Installation

Install 3box in your npm project:

$ npm install 3box

Usage

Import 3Box into your project

Import the 3box module

const Box = require('3box')

Import using the dist build in your html code

<script type="text/javascript" src="../dist/3box.js"></script>

Or optionally by loading remote copy from unpkg CDN.

<!-- The most recent version  -->
<script src="https://unpkg.com/3box/dist/3box.js"></script>
<!-- The most recent minified version  -->
<script src="https://unpkg.com/3box/dist/3box.min.js"></script>
<!-- Load specific versions by specifying the version as follows -->
<script src="https://unpkg.com/3box@<version>/dist/3box.js"></script>

Get the public profile of an address

3Box allows users to create a public profile. In your dapp you might have multiple ethereum addresses that you would like to display a name and picture for. The getProfile method allows you to retrieve the profile of any ethereum address (if it has one). This is a static method so you can call it directly from the Box object.

Using async/await

const profile = await Box.getProfile('0x12345abcde')
console.log(profile)

or using .then

Box.getProfile('0x12345abcde').then(profile => {
  console.log(profile)
})

Get, set, and remove data

To get or modify data in a user's 3Box, first open their 3Box by calling the openBox method. This method prompts the user to authenticate your dapp and returns a promise with a threeBox instance. You can only set, get, and remove data of users that are currently interacting with your dapp. Below ethereumProvider refers to the object that you would get from web3.currentProvider, or window.ethereum.

Open 3Box session

Using async/await

const box = await Box.openBox('0x12345abcde', ethereumProvider)

or using .then

Box.openBox('0x12345abcde', ethereumProvider).then(box => {
  // interact with 3Box data
})

Network sync

When you first open the box in your dapp all data might not be synced from the network yet. You should therefore add a listener using the onSyncDone method. This will allow you to know when all the users data is available to you. We advice against setting any data before this has happened.

box.onSyncDone(yourCallbackFunction)

Interact with 3Box data

You can now use the box instance object to interact with data in the users private store and profile. In both the profile and the private store you use a key to set a value.

Using async/await

// use the public profile
// get
const nickname = await box.public.get('name')
console.log(nickname)
// set
await box.public.set('name', 'oed')
// remove
await box.public.remove('name')

// use the private store
// get
const email = await box.private.get('email')
console.log(email)
// set
await box.private.set('email', 'oed@email.service')
// remove
await box.private.remove('email')

or using .then

// use the public profile
// get
box.public.get('name').then(nickname => {
  console.log(nickname)
  // set
  box.public.set('name', 'oed').then(() => {
    // remove
    box.public.remove('name').then(() => {
    })
  })
})

// use the private store
// get
box.private.get('email').then(email => {
  console.log(email)
  // set
  box.private.set('email', 'oed@email.service').then(() => {
    // remove
    box.private.remove('email').then(() => {
    })
  })
})

IPFS Configs

Two options are available if you want to pass additional IPFS config options to the IPFS object used in the library.

First you can pass your own IPFS object, configured how you decide and then disable the iframe as well. This offers the most optionality but experiences a loss in performace without the iframe.

const IPFS_OPTIONS = {
  EXPERIMENTAL: {
    pubsub: true
  },
  ... // Add your additional options, pubsub is required
}

const ipfs = new IPFS(IPFS_OPTIONS)
const box = await Box.openBox('0x12345abcde', ethereumProvider, { ipfs, iframeStore: false })

Second you can access the already initialized default IPFS object and change the IPFS configurations available after initialization. For example you can add a pinning node as follows.

const box = await Box.openBox('0x12345abcde', ethereumProvider)

box._ipfs.swarm.connect(pinningNode, () => {
  ...
})

Reference ipfs-js for additional options.

Open a space

A space is a named section of a users 3Box. Each space has both a public and a private store, and for every space you open the user has to grant explicit consent to view that space. This means that if your dapp uses a space that no other dapp uses, only your dapp is allowed to update the data and read the private store of that particular space. To open a space called narwhal you simply call:

const space = await box.openSpace('narwhal')

Get, set, and remove space data

Interacting with data in a space is done in the same way as interacting with box.public and box.private (see here). For example:

const config = await space.private.get('dapp-config')

Dapp data

Dapps can store data about users that relate to only their dapp. However we encurage dapps to share data between them for a richer web3 experience. Therefore we have created Key Conventions in order to facilitate this. Feel free to make a PR to this file to explain to the community how you use 3Box!

Example

You can quickly run and interact with some code by looking at the files in the /example folder. You run the example with the following command:

$ npm run example:start

This runs a simple server at http://localhost:3000/ that serves the static example/index.html file. This allows it easily interact with metamask. You can edit the example/index.html file to try differnt code.

Optimize build for read only 3Box API

If you only want to fetch profile data from 3Box's profile APIs you can optimize by importing only those functions or the API specific dist file. Since this includes minimal dependencies, file size is ~ 80kb vs 4+mb for the full build.

const { profileGraphQL, getProfile, getProfiles, getVerifiedAccounts } = require('3box/lib/api')
<script src="https://unpkg.com/3box/dist/3box.api.min.js"></script>

API Documentation

Box

Kind: global class

new Box()

Please use the openBox method to instantiate a 3Box

box.public

Kind: instance property of Box
Properties

NameTypeDescription
publicKeyValueStoreaccess the profile store of the users 3Box

box.private

Kind: instance property of Box
Properties

NameTypeDescription
privateKeyValueStoreaccess the private store of the users 3Box

box.verified

Kind: instance property of Box
Properties

NameTypeDescription
verifiedVerifiedcheck and create verifications

box.spaces

Kind: instance property of Box
Properties

NameTypeDescription
spacesObjectan object containing all open spaces indexed by their name.

box.openSpace(name, opts) ⇒ Space

Opens the space with the given name in the users 3Box

Kind: instance method of Box
Returns: Space - the Space instance for the given space name

ParamTypeDescription
nameStringThe name of the space
optsObjectOptional parameters
opts.consentCallbackfunctionA function that will be called when the user has consented to opening the box
opts.onSyncDonefunctionA function that will be called when the space has finished syncing with the pinning node

box.onSyncDone(syncDone)

Sets the callback function that will be called once when the db is fully synced.

Kind: instance method of Box

ParamTypeDescription
syncDonefunctionThe function that will be called

box.logout()

Closes the 3box instance and clears local cache. If you call this, users will need to sign a consent message to log in the next time you call openBox.

Kind: instance method of Box

Box.getProfile(address, opts) ⇒ Object

Get the public profile of a given address

Kind: static method of Box
Returns: Object - a json object with the profile for the given address

ParamTypeDescription
addressStringAn ethereum address
optsObjectOptional parameters
opts.addressServerStringURL of the Address Server
opts.ipfsObjectA js-ipfs ipfs object
opts.useCacheServiceBooleanUse 3Box API and Cache Service to fetch profile instead of OrbitDB. Default true.
opts.profileServerStringURL of Profile API server

Box.getProfiles(address, opts) ⇒ Object

Get a list of public profiles for given addresses. This relies on 3Box profile API.

Kind: static method of Box
Returns: Object - a json object with each key an address and value the profile

ParamTypeDescription
addressArrayAn array of ethereum addresses
optsObjectOptional parameters
opts.profileServerStringURL of Profile API server

Box.profileGraphQL(query, opts) ⇒ Object

GraphQL for 3Box profile API

Kind: static method of Box
Returns: Object - a json object with each key an address and value the profile

ParamTypeDescription
queryObjectA graphQL query object.
optsObjectOptional parameters
opts.graphqlServerStringURL of graphQL 3Box profile service

Box.getVerifiedAccounts(profile) ⇒ Object

Verifies the proofs of social accounts that is present in the profile.

Kind: static method of Box
Returns: Object - An object containing the accounts that have been verified

ParamTypeDescription
profileObjectA user profile object, received from the getProfile function

Box.openBox(address, ethereumProvider, opts) ⇒ Box

Opens the 3Box associated with the given address

Kind: static method of Box
Returns: Box - the 3Box instance for the given address

ParamTypeDescription
addressStringAn ethereum address
ethereumProviderethereumProviderAn ethereum provider
optsObjectOptional parameters
opts.consentCallbackfunctionA function that will be called when the user has consented to opening the box
opts.pinningNodeStringA string with an ipfs multi-address to a 3box pinning node
opts.ipfsObjectA js-ipfs ipfs object
opts.addressServerStringURL of the Address Server

Box.isLoggedIn(address) ⇒ Boolean

Check if the given address is logged in

Kind: static method of Box
Returns: Boolean - true if the user is logged in

ParamTypeDescription
addressStringAn ethereum address

KeyValueStore

Kind: global class

new KeyValueStore()

Please use box.public or box.private to get the instance of this class

keyValueStore.log ⇒ Array.<Object>

Returns array of underlying log entries. In linearized order according to their Lamport clocks. Useful for generating a complete history of all operations on store.

Kind: instance property of KeyValueStore
Returns: Array.<Object> - Array of ordered log entry objects
Example

const log = store.log
 const entry = log[0]
 console.log(entry)
 // { op: 'PUT', key: 'Name', value: 'Botbot', timeStamp: '1538575416068' }

keyValueStore.get(key) ⇒ String

Get the value of the given key

Kind: instance method of KeyValueStore
Returns: String - the value associated with the key

ParamTypeDescription
keyStringthe key

keyValueStore.set(key, value) ⇒ Boolean

Set a value for the given key

Kind: instance method of KeyValueStore
Returns: Boolean - true if successful

ParamTypeDescription
keyStringthe key
valueStringthe value

keyValueStore.remove(key) ⇒ Boolean

Remove the value for the given key

Kind: instance method of KeyValueStore
Returns: Boolean - true if successful

ParamTypeDescription
keyStringthe key

Space

Kind: global class

new Space()

Please use box.openSpace to get the instance of this class

space.public

Kind: instance property of Space
Properties

NameTypeDescription
publicKeyValueStoreaccess the profile store of the space

space.private

Kind: instance property of Space
Properties

NameTypeDescription
privateKeyValueStoreaccess the private store of the space

Verified

Kind: global class

new Verified()

Please use box.verified to get the instance of this class

verified.DID() ⇒ String

Returns the verified DID of the user

Kind: instance method of Verified
Returns: String - The DID of the user

verified.github() ⇒ Object

Verifies that the user has a valid github account Throws an error otherwise.

Kind: instance method of Verified
Returns: Object - Object containing username, and proof

verified.addGithub(gistUrl) ⇒ Object

Adds a github verification to the users profile Throws an error if the verification fails.

Kind: instance method of Verified
Returns: Object - Object containing username, and proof

ParamTypeDescription
gistUrlObjectURL of the proof

verified.twitter() ⇒ Object

Verifies that the user has a valid twitter account Throws an error otherwise.

Kind: instance method of Verified
Returns: Object - Object containing username, proof, and the verifier

verified.addTwitter(claim) ⇒ Object

Adds a twitter verification to the users profile Throws an error if the verification fails.

Kind: instance method of Verified
Returns: Object - Object containing username, proof, and the verifier

ParamTypeDescription
claimStringA did-JWT claim ownership of a twitter username

verified.email() ⇒ Object

Verifies that the user has a verified email account Throws an error otherwise.

Kind: instance method of Verified
Returns: Object - Object containing username, proof, and the verifier

verified.addEmail(claim) ⇒ Object

Adds an email verification to the users profile Throws an error if the verification fails.

Kind: instance method of Verified
Returns: Object - Object containing username, proof, and the verifier

ParamTypeDescription
claimStringA did-JWT claim ownership of an email username

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 33

Native requirements
33 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
ipfs
Environment
{
  "local_name": "ipfs",
  "alias": false,
  "peer_metadata": null
}
^0.33.1runtime
store
Environment
{
  "local_name": "store",
  "alias": false,
  "peer_metadata": null
}
^2.0.12runtime
ethers
Environment
{
  "local_name": "ethers",
  "alias": false,
  "peer_metadata": null
}
^4.0.20runtime
did-jwt
Environment
{
  "local_name": "did-jwt",
  "alias": false,
  "peer_metadata": null
}
^0.1.1runtime
orbit-db
Environment
{
  "local_name": "orbit-db",
  "alias": false,
  "peer_metadata": null
}
git://github.com/orbitdb/orbit-db.git#dddb271runtime
ipfs-mini
Environment
{
  "local_name": "ipfs-mini",
  "alias": false,
  "peer_metadata": null
}
^1.1.5runtime
js-sha256
Environment
{
  "local_name": "js-sha256",
  "alias": false,
  "peer_metadata": null
}
^0.9.0runtime
tweetnacl
Environment
{
  "local_name": "tweetnacl",
  "alias": false,
  "peer_metadata": null
}
^1.0.1runtime
node-fetch
Environment
{
  "local_name": "node-fetch",
  "alias": false,
  "peer_metadata": null
}
^2.3.0runtime
@babel/runtime
Environment
{
  "local_name": "@babel/runtime",
  "alias": false,
  "peer_metadata": null
}
^7.1.2runtime
tweetnacl-util
Environment
{
  "local_name": "tweetnacl-util",
  "alias": false,
  "peer_metadata": null
}
^0.15.0runtime
graphql-request
Environment
{
  "local_name": "graphql-request",
  "alias": false,
  "peer_metadata": null
}
^1.8.2runtime
https-did-resolver
Environment
{
  "local_name": "https-did-resolver",
  "alias": false,
  "peer_metadata": null
}
^0.1.0runtime
ipfs-postmsg-proxy
Environment
{
  "local_name": "ipfs-postmsg-proxy",
  "alias": false,
  "peer_metadata": null
}
^3.1.1runtime
muport-did-resolver
Environment
{
  "local_name": "muport-did-resolver",
  "alias": false,
  "peer_metadata": null
}
^0.3.0-alpha.2runtime
orbit-db-cache-postmsg-proxy
Environment
{
  "local_name": "orbit-db-cache-postmsg-proxy",
  "alias": false,
  "peer_metadata": null
}
^0.1.1runtime
jest
Environment
{
  "local_name": "jest",
  "alias": false,
  "peer_metadata": null
}
^23.6.0development
express
Environment
{
  "local_name": "express",
  "alias": false,
  "peer_metadata": null
}
^4.16.4development
webpack
Environment
{
  "local_name": "webpack",
  "alias": false,
  "peer_metadata": null
}
^4.20.2development
standard
Environment
{
  "local_name": "standard",
  "alias": false,
  "peer_metadata": null
}
^12.0.1development
ipfsd-ctl
Environment
{
  "local_name": "ipfsd-ctl",
  "alias": false,
  "peer_metadata": null
}
^0.40.1development
@babel/cli
Environment
{
  "local_name": "@babel/cli",
  "alias": false,
  "peer_metadata": null
}
^7.1.2development
babel-core
Environment
{
  "local_name": "babel-core",
  "alias": false,
  "peer_metadata": null
}
7.0.0-bridge.0development
@babel/core
Environment
{
  "local_name": "@babel/core",
  "alias": false,
  "peer_metadata": null
}
^7.1.2development
ganache-cli
Environment
{
  "local_name": "ganache-cli",
  "alias": false,
  "peer_metadata": null
}
^6.1.0development
webpack-cli
Environment
{
  "local_name": "webpack-cli",
  "alias": false,
  "peer_metadata": null
}
^3.1.2development
babel-loader
Environment
{
  "local_name": "babel-loader",
  "alias": false,
  "peer_metadata": null
}
^8.0.5development
@babel/preset-env
Environment
{
  "local_name": "@babel/preset-env",
  "alias": false,
  "peer_metadata": null
}
^7.1.0development
jsdoc-to-markdown
Environment
{
  "local_name": "jsdoc-to-markdown",
  "alias": false,
  "peer_metadata": null
}
^4.0.1development
@babel/preset-react
Environment
{
  "local_name": "@babel/preset-react",
  "alias": false,
  "peer_metadata": null
}
^7.0.0development
@babel/plugin-transform-runtime
Environment
{
  "local_name": "@babel/plugin-transform-runtime",
  "alias": false,
  "peer_metadata": null
}
^7.1.0development
@babel/plugin-transform-modules-commonjs
Environment
{
  "local_name": "@babel/plugin-transform-modules-commonjs",
  "alias": false,
  "peer_metadata": null
}
^7.2.0development
@babel/plugin-proposal-object-rest-spread
Environment
{
  "local_name": "@babel/plugin-proposal-object-rest-spread",
  "alias": false,
  "peer_metadata": null
}
^7.0.0development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "shasum": "5578e684d60130fb8d41931ab224d3ab1f72db06",
  "tarball": "https://registry.npmjs.org/3box/-/3box-1.4.0-beta.1.tgz",
  "fileCount": 54,
  "integrity": "sha512-UyjT4IuoRT1ezkLf4d5QHJ73tNwS66N4J1+OfQtMvcwAeiMwRAuEe7mqUPW4uT3N52LG4sdGE49RG4kw0XEjdQ==",
  "signatures": [
    {
      "sig": "MEUCIQDq1xUjyrY3ORow5M833hVDYndKlekIfruH5xHBUUFnLQIgVd8mkaWPcjP6QUw7xA4L2ZDfrpW6qhGs1bulKUqLcZs=",
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"
    }
  ],
  "unpackedSize": 16693957,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcbwvvCRA9TVsSAnZWagAANVgP/2o6LH67Qi+IO8N3k0Xo\nkezIuqPG4z6FvrFHjoZgglHiF1OYWjFcaD6M4K7EX5JDNzo4KzpLvD8CpRf5\nQQ5wR2QP+EcFX6025Od8KQG/r3jAb75GNLpLbRso81FyNT5KkrdS/mB6Q344\nIM8UEe6k+9iCRe31XTt9eA0M3F3M2d+jxgDvoZYGlM1ArbJr67wBizlLpeKm\nmy9BdZx8vedq0zGLYelIEZMf5o9dCWAS9+4pHAbM1JcQjV7Ap8INlKV1s6TP\n6Ynbnzm7YHnMsH774BdYnL4NXAmq6Nml0bAG4sdqsMHnZ82hCIYibtLhkSTR\nfEbQCeGI6vc7yOeMbgYMly/aXOh51QQ9nBq3Xkuy5RmRU/3IWhiuH4qQ0fL0\n03DeUFwb2i7KuvxFj4o9UHiKpAN9zZB/oty7DdVTjdjlt97eYPVVNXplDqEg\npUPwFdb8bYV4Kc9lMPpFCvHmlLVCu3ssR3scv9ihCWcW1rwHWy80vxNimJGX\nn9c0SmpEm+uAEFncqYiqNN2ue+9drE8NdpNi+LKIbpVp08TFm8QplJfuUSDH\n8JeTNMy2t9ALXq5WHIMAGv2Wm2tJ8V/Njds6/CWn2yURzB+LuunclfDWi6JS\niN8CohPPawXHzlauk0ZbchjMVTFSajyzpubzA4PJ7QLskRQyP4sUtaHi/m+p\npJQU\r\n=pKDW\r\n-----END PGP SIGNATURE-----\r\n"
}

Repository

PENDING
3box/3box-js

Shared repository data · GitHub

License detected in repository
Not collected

Repository

MOVED
uport-project/3box-js

Shared repository data · GitHub

Repository moved · manual review pending

HTTP 301 · detected 21 Oct 2026

Reported destination
License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:31 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:8f65d08ef0b176c0aecd18239b5f2d5bddac3a667c0de61f88bf0476c91ba8be

Other collected versions

1.19.123 Feb1.22.228 Sept1.22.2-beta.028 Sept1.22.121 Sept1.22.1-alpha.021 Sept1.22.015 SeptView all collected versions →