Commit f47aea5be452991650859d2af99234ea38e6efd4
expost merkleLink function
Signed-off-by: wanderer <mjbecze@gmail.com>wanderer committed on 10/9/2017, 7:02:55 PM
Parent: 41d856e209658485fcf9b1adaa49cada3e75c9de
Files changed
datastore.js | changed |
index.js | changed |
package.json | changed |
tests/index.js | changed |
datastore.js | |||
---|---|---|---|
@@ -6,14 +6,10 @@ | |||
6 | 6 … | ||
7 | 7 … | module.exports = class TreeDAG extends DAG { | |
8 | 8 … | async put (val) { | |
9 | 9 … | const encoded = treeNode.encode(val) | |
10 | - let key = await crypto.subtle.digest({ | ||
11 | - name: 'SHA-256' | ||
12 | - }, encoded) | ||
10 … | + let key = await TreeDAG.getMerkleLink(encoded) | ||
13 | 11 … | ||
14 | - key = Buffer.from(key.slice(0, HASH_LEN)) | ||
15 | - | ||
16 | 12 … | return new Promise((resolve, reject) => { | |
17 | 13 … | this._dag.put(key, encoded.toString('hex'), () => { | |
18 | 14 … | resolve(key) | |
19 | 15 … | }) | |
@@ -33,8 +29,14 @@ | |||
33 | 29 … | }) | |
34 | 30 … | }) | |
35 | 31 … | } | |
36 | 32 … | ||
37 | - isValidLink (link) { | ||
33 … | + static isValidLink (link) { | ||
38 | 34 … | return Buffer.isBuffer(link) && link.length === HASH_LEN | |
39 | 35 … | } | |
36 … | + | ||
37 … | + static getMerkleLink (buf) { | ||
38 … | + return crypto.subtle.digest({ | ||
39 … | + name: 'SHA-256' | ||
40 … | + }, buf).then(link => Buffer.from(link.slice(0, HASH_LEN))) | ||
41 … | + } | ||
40 | 42 … | } |
index.js | ||
---|---|---|
@@ -37,8 +37,17 @@ | ||
37 | 37 … | static get ArrayConstructor () { |
38 | 38 … | return Uint1Array |
39 | 39 … | } |
40 | 40 … | |
41 … | + /** | |
42 … | + * returns a merkle link for some given data | |
43 … | + * @param {Buffer} data - the data which you would like to hash | |
44 … | + * @returns {Buffer} | |
45 … | + */ | |
46 … | + static getMerkleLink (data) { | |
47 … | + return DataStore.getMerkleLink(data) | |
48 … | + } | |
49 … | + | |
41 | 50 … | async _get (key) { |
42 | 51 … | let index = 0 |
43 | 52 … | let root = this.root |
44 | 53 … | let parent |
package.json | ||
---|---|---|
@@ -26,9 +26,9 @@ | ||
26 | 26 … | "standard": "^10.0.0", |
27 | 27 … | "tape": "^4.6.3" |
28 | 28 … | }, |
29 | 29 … | "dependencies": { |
30 | - "ipld-graph-builder": "^1.3.3", | |
30 … | + "ipld-graph-builder": "^1.3.5", | |
31 | 31 … | "leb128": "0.0.3", |
32 | 32 … | "node-webcrypto-shim": "0.0.0", |
33 | 33 … | "safe-buffer": "^5.1.1", |
34 | 34 … | "text-encoding": "^0.6.4", |
tests/index.js | ||
---|---|---|
@@ -4,8 +4,12 @@ | ||
4 | 4 … | const RadixTree = require('../') |
5 | 5 … | const db = level('./testdb') |
6 | 6 … | |
7 | 7 … | tape('set and get', async t => { |
8 … | + const r = await RadixTree.getMerkleLink(Buffer.from([0])) | |
9 … | + | |
10 … | + t.equal(r.toString('hex'), '6e340b9cffb37a989ca544e6bb780a2c78901d3f', 'should hash') | |
11 … | + | |
8 | 12 … | let tree = new RadixTree({ |
9 | 13 … | db: db |
10 | 14 … | }) |
11 | 15 … | |
@@ -78,33 +82,29 @@ | ||
78 | 82 … | tape('delete', async t => { |
79 | 83 … | const tree = new RadixTree({ |
80 | 84 … | db: db |
81 | 85 … | }) |
82 | - try { | |
83 | - await tree.set('test', Buffer.from('cat')) | |
84 | - await tree.set('ter', Buffer.from('cat3')) | |
85 | - await tree.delete('te') | |
86 | - await tree.delete('test') | |
87 | - await tree.delete('ter') | |
88 | - t.deepEquals(tree.root['/'], RadixTree.emptyTreeState) | |
86 … | + await tree.set('test', Buffer.from('cat')) | |
87 … | + await tree.set('ter', Buffer.from('cat3')) | |
88 … | + await tree.delete('te') | |
89 … | + await tree.delete('test') | |
90 … | + await tree.delete('ter') | |
91 … | + t.deepEquals(tree.root['/'], RadixTree.emptyTreeState) | |
89 | 92 … | |
90 | - // tests delete midle branchs | |
91 | - await tree.set('test', Buffer.from('cat')) | |
92 | - await tree.set('te', Buffer.from('cat2')) | |
93 | - await tree.set('ter', Buffer.from('cat3')) | |
94 | - await tree.delete('te') | |
95 | - let val = await tree.get('test') | |
96 | - t.equals(val.toString(), 'cat') | |
93 … | + // tests delete midle branchs | |
94 … | + await tree.set('test', Buffer.from('cat')) | |
95 … | + await tree.set('te', Buffer.from('cat2')) | |
96 … | + await tree.set('ter', Buffer.from('cat3')) | |
97 … | + await tree.delete('te') | |
98 … | + let val = await tree.get('test') | |
99 … | + t.equals(val.toString(), 'cat') | |
97 | 100 … | |
98 | - // tests delete end branchs | |
99 | - await tree.set('te', 'cat2') | |
100 | - await tree.delete('ter') | |
101 | - await tree.delete('te') | |
102 | - await tree.delete('test') | |
103 | - t.deepEquals(tree.root['/'], RadixTree.emptyTreeState) | |
104 | - } catch (e) { | |
105 | - console.log(e) | |
106 | - } | |
101 … | + // tests delete end branchs | |
102 … | + await tree.set('te', 'cat2') | |
103 … | + await tree.delete('ter') | |
104 … | + await tree.delete('te') | |
105 … | + await tree.delete('test') | |
106 … | + t.deepEquals(tree.root['/'], RadixTree.emptyTreeState) | |
107 | 107 … | t.end() |
108 | 108 … | }) |
109 | 109 … | |
110 | 110 … | tape('large values', async t => { |
Built with git-ssb-web