all files / merkle-trie/ transcoders.js

57.14% Statements 8/14
0% Branches 0/2
75% Functions 3/4
57.14% Lines 8/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                                                  
const ipld = require('ipld')
 
module.exports = class Transcoder {
  static get prefix () {
    return
  }
 
  static toBuffer (vertex) {
    const edges = [...vertex.edges].map(item => {
      item[1] = item[1].toBuffer()
      return item
    })
    return ipld.marshal({value: vertex.value, edges: edges})
  }
 
  static fromBuffer (data) {
    let {value, edges} = ipld.unmarshal(data)
    edges = edges.map((edge) => {
      if (Buffer.isBuffer(edge[1])) {
        return edge
      } else {
        return Transcoder.fromBuffer(edge)
      }
    })
    // to do handle externtions
    return [value, new Map(edges)]
  }
 
  static hash (data) {
    return ipld.multihash(data)
  }
}