1.0.7-beta-2 • Published 7 years ago

3box v1.0.7-beta-2

Weekly downloads
1,208
License
Apache-2.0
Repository
github
Last release
7 years ago

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. Currently it supports one shared space which all dapps can access. In the future there will be support for more granular access control using namespaces.

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(() => {
    })
  })
})

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.

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
verifiedVerificationscheck and create verifications

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.close()

Closes the 3box instance without clearing the local cache. Should be called after you are done using the 3Box instance, but without logging the user out.

Kind: instance method of Box

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.orbitPathStringA custom path for orbitdb storage
opts.iframeStoreBooleanUse iframe for storage, allows shared store across domains. Default true when run in browser.
opts.useCacheServiceBooleanUse 3Box API and Cache Service to fetch profile instead of OrbitDB. Default true.

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

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

Opens the user space 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.orbitPathStringA custom path for orbitdb storage
opts.addressServerStringURL of the Address Server
opts.iframeStoreBooleanUse iframe for storage, allows shared store across domains. Default true when run in browser.

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.profileStore or box.profileStore 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

Verifications

Kind: global class

new Verifications()

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

verifications.github() ⇒ String

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

Kind: instance method of Verifications
Returns: String - The github handle of the user

verifications.addGithub(gistUrl) ⇒ String

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

Kind: instance method of Verifications
Returns: String - The github handle of the user

ParamTypeDescription
gistUrlObjectURL of the proof

verifications.twitter() ⇒ String

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

Kind: instance method of Verifications
Returns: String - The twitter handle of the user

verifications.addTwitter(tweetUrl) ⇒ String

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

Kind: instance method of Verifications
Returns: String - The twitter handle of the user

ParamTypeDescription
tweetUrlObjectURL of the proof
1.19.1

5 years ago

1.22.2

5 years ago

1.22.2-beta.0

5 years ago

1.22.1

5 years ago

1.22.1-alpha.0

5 years ago

1.22.0

5 years ago

1.21.0

6 years ago

1.20.4-beta.2

6 years ago

1.20.4-beta.1

6 years ago

1.20.3

6 years ago

1.20.2

6 years ago

1.20.2-beta.1

6 years ago

1.20.1

6 years ago

1.20.0

6 years ago

1.20.0-beta.0

6 years ago

1.19.0

6 years ago

1.19.0-rc.1

6 years ago

1.18.1

6 years ago

1.18.0

6 years ago

1.18.0-beta.1

6 years ago

1.17.2-beta.2

6 years ago

1.17.2-beta.1

6 years ago

1.17.1

6 years ago

1.17.0

6 years ago

1.16.3

6 years ago

1.16.3-beta.1

6 years ago

1.16.2

6 years ago

1.16.2-beta.4

6 years ago

1.16.2-beta.3

6 years ago

1.16.2-beta.2

6 years ago

1.16.2-beta.1

6 years ago

1.16.1

6 years ago

1.16.0

6 years ago

1.16.0-beta.3

6 years ago

1.16.0-beta.2

6 years ago

1.16.0-beta.1

6 years ago

1.15.0

6 years ago

1.14.1-beta.1

6 years ago

1.14.0

6 years ago

1.13.3-beta.2

6 years ago

1.13.3-beta.1

6 years ago

1.13.2

6 years ago

1.13.1

6 years ago

1.13.1-beta.1

6 years ago

1.13.0

6 years ago

1.13.0-beta.2

6 years ago

1.13.0-beta.1

6 years ago

1.12.0

6 years ago

1.12.0-beta.2

6 years ago

1.12.0-beta.1

6 years ago

1.11.0

6 years ago

1.10.10

7 years ago

1.10.9

7 years ago

1.10.8

7 years ago

1.10.7

7 years ago

1.11.0-beta.1

7 years ago

1.10.6

7 years ago

1.10.6-beta.1

7 years ago

1.10.5

7 years ago

1.10.5-beta.2

7 years ago

1.10.5-beta.1

7 years ago

1.10.4

7 years ago

1.10.3

7 years ago

1.10.2

7 years ago

1.10.1

7 years ago

1.10.0

7 years ago

1.9.1

7 years ago

1.9.0

7 years ago

1.9.0-rc.4

7 years ago

1.9.0-rc.3

7 years ago

1.9.0-rc.2

7 years ago

1.9.0-rc.1

7 years ago

1.9.0-beta.1

7 years ago

1.8.5

7 years ago

1.8.4

7 years ago

1.8.4-beta.1

7 years ago

1.8.3

7 years ago

1.8.2

7 years ago

1.8.1

7 years ago

1.8.1-beta.1

7 years ago

1.8.0

7 years ago

1.7.2

7 years ago

1.7.2-beta.3

7 years ago

1.7.2-beta.2

7 years ago

1.7.2-beta.1

7 years ago

1.7.1

7 years ago

1.7.0-node-1

7 years ago

1.7.0

7 years ago

1.7.0-beta.2

7 years ago

1.7.0-beta.1

7 years ago

1.6.2

7 years ago

1.6.1-node-1

7 years ago

1.6.1

7 years ago

1.6.1-beta.1

7 years ago

1.6.0

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.4.0-beta.1

7 years ago

1.3.0

7 years ago

1.3.0-beta.2

7 years ago

1.3.0-beta.1

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.2.0-beta.6

7 years ago

1.2.0-beta.5

7 years ago

1.2.0-beta.4

7 years ago

1.2.0-beta.3

7 years ago

1.2.0-beta.2

7 years ago

1.2.0-beta.1

7 years ago

1.1.0

7 years ago

1.1.0-beta-1

7 years ago

1.0.7-beta-2

7 years ago

1.0.7-beta-1

7 years ago

1.0.6-beta-1

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

1.0.0-beta-13

7 years ago

1.0.0-beta-12

7 years ago

1.0.0-beta-11

7 years ago

1.0.0-beta-10

7 years ago

1.0.0-beta-9

7 years ago

1.0.0-beta-8

7 years ago

1.0.0-beta-7

7 years ago

1.0.0-beta-6

7 years ago

1.0.0-beta-5

7 years ago

1.0.0-beta-4

7 years ago

1.0.0-beta-3

8 years ago

1.0.0-beta-2

8 years ago

1.0.0-beta-1

8 years ago

0.0.5-beta-2

8 years ago

0.0.5-beta-1

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago