git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Commit c3ceee3d4a1abc0272ae876b02e06c9fb67ee82a

updated docs

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 10/3/2017, 6:19:08 AM
Parent: 8b3b6ad9bccab11efb8cdb0f5a71e03b011e8ee0

Files changed

README.mdchanged
index.jschanged
package.jsonchanged
treeNode.jschanged
dag.jsdeleted
README.mdView
@@ -4,58 +4,53 @@
44
55 [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
66
77 # install
8-`npm install merkle-radix-tree`
8 +`npm install js-dfinity-radix-tree`
99
1010 # SYNOPSIS
1111 This implements a binary merkle radix tree. The point of using a binary radix
1212 tree is that it generates smaller proof size then trees with larger radixes.
1313 This tree is well suited for storing large dictonaries of fairly random keys.
1414 And is optimized for storing keys of the same length. If the keys are not
15-random better performance can be achived by hashing them first.It builds on
15 +random better performance can be achived by hashing them first. It builds on
1616 top of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder)
1717 and the resulting state and proofs are generated using it.
1818
1919 # INSTALL
20-`npm install merkle-radix-tree`
20 +`npm install js-dfinity-radix-tree`
2121
2222 # USAGE
2323
2424 ```javascript
25-const IPFS = require('ipfs')
26-const RadixTree = require('merkle-radix-tree')
25 +const RadixTree = require('js-dfinity-radix-tree')
26 +const level = require('level')
27 +const db = level('./tempdb')
2728
28-// start ipfs
29-const node = new IPFS({
30- start: false,
31- repo: './ipfs-repo'
32-})
33-
34-node.on('ready', async () => {
29 +async function main () {
3530 const prover = new RadixTree({
36- dag: node.dag
31 + db: db
3732 })
3833
39- // set some values
40- await prover.set('test', 'value')
41- await prover.set('doge', 'coin')
42- await prover.set('cat', 'dog')
43- await prover.set('monkey', 'wrench')
34 + await prover.set('test', Buffer.from('value'))
35 + await prover.set('doge', Buffer.from('coin'))
36 + await prover.set('cat', Buffer.from('dog'))
37 + await prover.set('monkey', Buffer.from('wrench'))
4438
4539 // create a merkle root and save the tree
46- const merkleRoot = await prover.flush()
40 + const merkleroot = await prover.flush()
4741
4842 // start a new Instance with the root
4943 const verifier = new RadixTree({
50- dag: node.dag,
51- root: merkleRoot
44 + db: db,
45 + root: merkleroot
5246 })
5347
54- // gets the merkle proof from ipfs-js and returns the result
5548 const val = await verifier.get('monkey')
56- console.log(val)
57-})
49 + console.log(val.toString())
50 +}
51 +
52 +main()
5853 ```
5954 # API
6055 ['./docs/'](./docs/index.md)
6156
index.jsView
@@ -1,25 +1,26 @@
11 const Graph = require('ipld-graph-builder')
22 const Uint1Array = require('uint1array')
33 const TextEncoder = require('text-encoding').TextEncoder
4-const DAG = require('./dag.js')
4 +const DataStore = require('./datastore.js')
55 const treeNode = require('./treeNode.js')
66
77 const encoder = new TextEncoder('utf-8')
88
99 module.exports = class RadixTree {
1010 /**
1111 * @param opts
1212 * @param opts.root {object} a merkle root to a radix tree. If none, RadixTree will create an new root.
13 + * @param opts.db {object} a level db instance alternitly `opts.graph` can be used
1314 * @param opts.graph {object} an instance of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder) alternitvly `opts.dag` can be used
1415 * @param opts.dag {object} an instance if [ipfs.dag](https://github.com/ipfs/js-ipfs#dag). If there is no `opts.graph` this will be used to create a new graph instance.
1516 */
1617 constructor (opts) {
1718 this.root = opts.root || {
1819 '/': RadixTree.emptyTreeState
1920 }
2021
21- this.dag = opts.dag || new DAG(opts.db)
22 + this.dag = opts.dag || new DataStore(opts.db)
2223 this.graph = opts.graph || new Graph(this.dag)
2324 }
2425
2526 /**
package.jsonView
@@ -19,18 +19,19 @@
1919 ],
2020 "devDependencies": {
2121 "coveralls": "^2.13.1",
2222 "documentation": "^5.1.1",
23- "ipfs": "^0.25.0",
24- "istanbul": "^1.1.0-alpha.1",
23 + "level": "^1.7.0",
24 + "nyc": "^11.2.1",
2525 "standard": "^10.0.0",
2626 "tape": "^4.6.3"
2727 },
2828 "dependencies": {
29- "borc": "^2.0.2",
29 + "blakejs": "^1.1.0",
3030 "ipld-graph-builder": "^1.2.4",
3131 "leb128": "0.0.2",
32- "level": "^1.7.0",
32 + "node-webcrypto-ossl": "^1.0.31",
33 + "node-webcrypto-shim": "0.0.0",
3334 "safe-buffer": "^5.1.1",
3435 "text-encoding": "^0.6.4",
3536 "uint1array": "^1.0.5"
3637 },
treeNode.jsView
@@ -99,9 +99,8 @@
9999 if (encodeLen) {
100100 const len = leb128.encode(encoded.length)
101101 encoded = Buffer.concat([len, encoded])
102102 }
103- console.log(encoded.toString('hex'))
104103 return encoded
105104 }
106105
107106 exports.decode = function (val) {
dag.jsView
@@ -1,34 +1,0 @@
1-const crypto = require('crypto')
2-const DAG = require('ipld-graph-builder/dag')
3-const treeNode = require('./treeNode.js')
4-const HASH_LEN = 20
5-
6-module.exports = class TreeDAG extends DAG {
7- put (val, options) {
8- const encoded = treeNode.encode(val)
9- const key = crypto.createHash('sha256').update(encoded).digest().slice(0, HASH_LEN)
10- return new Promise((resolve, reject) => {
11- this._dag.put(key, encoded.toString('hex'), () => {
12- resolve(key)
13- })
14- })
15- }
16-
17- get (link) {
18- return new Promise((resolve, reject) => {
19- this._dag.get(link, (err, val) => {
20- if (err) {
21- reject(err)
22- } else {
23- val = Buffer.from(val, 'hex')
24- const decoded = treeNode.decode(val)
25- resolve(decoded)
26- }
27- })
28- })
29- }
30-
31- isValidLink (link) {
32- return Buffer.isBuffer(link) && link.length === HASH_LEN
33- }
34-}

Built with git-ssb-web