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.md | changed |
index.js | changed |
package.json | changed |
treeNode.js | changed |
dag.js | deleted |
README.md | |||
---|---|---|---|
@@ -4,58 +4,53 @@ | |||
4 | 4 … | ||
5 | 5 … | [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | |
6 | 6 … | ||
7 | 7 … | # install | |
8 | -`npm install merkle-radix-tree` | ||
8 … | +`npm install js-dfinity-radix-tree` | ||
9 | 9 … | ||
10 | 10 … | # SYNOPSIS | |
11 | 11 … | This implements a binary merkle radix tree. The point of using a binary radix | |
12 | 12 … | tree is that it generates smaller proof size then trees with larger radixes. | |
13 | 13 … | This tree is well suited for storing large dictonaries of fairly random keys. | |
14 | 14 … | 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 | ||
16 | 16 … | top of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder) | |
17 | 17 … | and the resulting state and proofs are generated using it. | |
18 | 18 … | ||
19 | 19 … | # INSTALL | |
20 | -`npm install merkle-radix-tree` | ||
20 … | +`npm install js-dfinity-radix-tree` | ||
21 | 21 … | ||
22 | 22 … | # USAGE | |
23 | 23 … | ||
24 | 24 … | ```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') | ||
27 | 28 … | ||
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 () { | ||
35 | 30 … | const prover = new RadixTree({ | |
36 | - dag: node.dag | ||
31 … | + db: db | ||
37 | 32 … | }) | |
38 | 33 … | ||
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')) | ||
44 | 38 … | ||
45 | 39 … | // create a merkle root and save the tree | |
46 | - const merkleRoot = await prover.flush() | ||
40 … | + const merkleroot = await prover.flush() | ||
47 | 41 … | ||
48 | 42 … | // start a new Instance with the root | |
49 | 43 … | const verifier = new RadixTree({ | |
50 | - dag: node.dag, | ||
51 | - root: merkleRoot | ||
44 … | + db: db, | ||
45 … | + root: merkleroot | ||
52 | 46 … | }) | |
53 | 47 … | ||
54 | - // gets the merkle proof from ipfs-js and returns the result | ||
55 | 48 … | const val = await verifier.get('monkey') | |
56 | - console.log(val) | ||
57 | -}) | ||
49 … | + console.log(val.toString()) | ||
50 … | +} | ||
51 … | + | ||
52 … | +main() | ||
58 | 53 … | ``` | |
59 | 54 … | # API | |
60 | 55 … | ['./docs/'](./docs/index.md) | |
61 | 56 … |
index.js | ||
---|---|---|
@@ -1,25 +1,26 @@ | ||
1 | 1 … | const Graph = require('ipld-graph-builder') |
2 | 2 … | const Uint1Array = require('uint1array') |
3 | 3 … | const TextEncoder = require('text-encoding').TextEncoder |
4 | -const DAG = require('./dag.js') | |
4 … | +const DataStore = require('./datastore.js') | |
5 | 5 … | const treeNode = require('./treeNode.js') |
6 | 6 … | |
7 | 7 … | const encoder = new TextEncoder('utf-8') |
8 | 8 … | |
9 | 9 … | module.exports = class RadixTree { |
10 | 10 … | /** |
11 | 11 … | * @param opts |
12 | 12 … | * @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 | |
13 | 14 … | * @param opts.graph {object} an instance of [ipld-graph-builder](https://github.com/ipld/js-ipld-graph-builder) alternitvly `opts.dag` can be used |
14 | 15 … | * @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. |
15 | 16 … | */ |
16 | 17 … | constructor (opts) { |
17 | 18 … | this.root = opts.root || { |
18 | 19 … | '/': RadixTree.emptyTreeState |
19 | 20 … | } |
20 | 21 … | |
21 | - this.dag = opts.dag || new DAG(opts.db) | |
22 … | + this.dag = opts.dag || new DataStore(opts.db) | |
22 | 23 … | this.graph = opts.graph || new Graph(this.dag) |
23 | 24 … | } |
24 | 25 … | |
25 | 26 … | /** |
package.json | ||
---|---|---|
@@ -19,18 +19,19 @@ | ||
19 | 19 … | ], |
20 | 20 … | "devDependencies": { |
21 | 21 … | "coveralls": "^2.13.1", |
22 | 22 … | "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", | |
25 | 25 … | "standard": "^10.0.0", |
26 | 26 … | "tape": "^4.6.3" |
27 | 27 … | }, |
28 | 28 … | "dependencies": { |
29 | - "borc": "^2.0.2", | |
29 … | + "blakejs": "^1.1.0", | |
30 | 30 … | "ipld-graph-builder": "^1.2.4", |
31 | 31 … | "leb128": "0.0.2", |
32 | - "level": "^1.7.0", | |
32 … | + "node-webcrypto-ossl": "^1.0.31", | |
33 … | + "node-webcrypto-shim": "0.0.0", | |
33 | 34 … | "safe-buffer": "^5.1.1", |
34 | 35 … | "text-encoding": "^0.6.4", |
35 | 36 … | "uint1array": "^1.0.5" |
36 | 37 … | }, |
treeNode.js | ||
---|---|---|
@@ -99,9 +99,8 @@ | ||
99 | 99 … | if (encodeLen) { |
100 | 100 … | const len = leb128.encode(encoded.length) |
101 | 101 … | encoded = Buffer.concat([len, encoded]) |
102 | 102 … | } |
103 | - console.log(encoded.toString('hex')) | |
104 | 103 … | return encoded |
105 | 104 … | } |
106 | 105 … | |
107 | 106 … | exports.decode = function (val) { |
dag.js | ||
---|---|---|
@@ -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