git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: 24407d385e0f7cc8eed70abe5a5e927a092ebe60

Files: 24407d385e0f7cc8eed70abe5a5e927a092ebe60 / datastore.js

986 bytesRaw
1const Buffer = require('safe-buffer').Buffer
2const crypto = require('node-webcrypto-shim')
3const DAG = require('ipld-graph-builder/datastore.js')
4const treeNode = require('./treeNode.js')
5const HASH_LEN = 20
6
7module.exports = class TreeDAG extends DAG {
8 async put (val) {
9 const encoded = treeNode.encode(val)
10 let key = await crypto.subtle.digest({
11 name: 'SHA-256'
12 }, encoded)
13
14 key = Buffer.from(key.slice(0, HASH_LEN))
15
16 return new Promise((resolve, reject) => {
17 this._dag.put(key, encoded.toString('hex'), () => {
18 resolve(key)
19 })
20 })
21 }
22
23 get (link) {
24 return new Promise((resolve, reject) => {
25 this._dag.get(link, (err, val) => {
26 if (err) {
27 reject(err)
28 } else {
29 val = Buffer.from(val, 'hex')
30 const decoded = treeNode.decode(val)
31 resolve(decoded)
32 }
33 })
34 })
35 }
36
37 isValidLink (link) {
38 return Buffer.isBuffer(link) && link.length === HASH_LEN
39 }
40}
41

Built with git-ssb-web