index.js
function (dag, node, logLinks, batch, opts, cb) {
  if (opts.hash && node.key !== opts.hash) return cb(CHECKSUM_MISMATCH)
  if (opts.seq && node.seq !== opts.seq) return cb(INVALID_LOG)

  var log = {
    change: node.change,
    node: node.key,
    links: logLinks
  }

  var onclone = function (clone) {
    if (!opts.log) return cb(null, clone, [])
    batch.push({type: 'put', key: dag.logs.key(node.log, node.seq), value: messages.Entry.encode(log)})
    cb(null, clone)
  }

  var done = function () {
    dag.get(node.key, { valueEncoding: 'binary' }, function (_, clone) {
      // This node already exists somewhere in the hyperlog; add it to the
      // log's append-only log, but don't insert it again.
      if (clone) return onclone(clone)

      var links = node.links
      for (var i = 0; i < links.length; i++) batch.push({type: 'del', key: HEADS + links[i]})
      batch.push({type: 'put', key: CHANGES + lexint.pack(node.change, 'hex'), value: node.key})
      batch.push({type: 'put', key: NODES + node.key, value: messages.Node.encode(node)})
      batch.push({type: 'put', key: HEADS + node.key, value: node.key})
      batch.push({type: 'put', key: dag.logs.key(node.log, node.seq), value: messages.Entry.encode(log)})

      cb(null, node)
    })
  }

  // Local node; sign it.
  if (node.log === dag.id) {
    if (!dag.sign || node.signature) return done()
    dag.sign(node, function (err, sig) {
      if (err) return cb(err)
      if (!node.identity) node.identity = dag.identity
      node.signature = sig
      done()
    })
  // Remote node; verify it.
  } else {
    if (!dag.verify) return done()
    dag.verify(node, function (err, valid) {
      if (err) return cb(err)
      if (!valid) return cb(INVALID_SIGNATURE)
      done()
    })
  }
}
function (dag, node, logLinks, batch, opts, cb) {
  if (opts.hash && node.key !== opts.hash) return cb(CHECKSUM_MISMATCH)
  if (opts.seq && node.seq !== opts.seq) return cb(INVALID_LOG)

  var log = {
    change: node.change,
    node: node.key,
    links: logLinks
  }

  var onclone = function (clone) {
    if (!opts.log) return cb(null, clone, [])
    batch.push({type: 'put', key: dag.logs.key(node.log, node.seq), value: messages.Entry.encode(log)})
    cb(null, clone)
  }

  var done = function () {
    dag.get(node.key, { valueEncoding: 'binary' }, function (_, clone) {
      // This node already exists somewhere in the hyperlog; add it to the
      // log's append-only log, but don't insert it again.
      if (clone) return onclone(clone)

      var links = node.links
      for (var i = 0; i < links.length; i++) batch.push({type: 'del', key: HEADS + links[i]})
      batch.push({type: 'put', key: CHANGES + lexint.pack(node.change, 'hex'), value: node.key})
      batch.push({type: 'put', key: NODES + node.key, value: messages.Node.encode(node)})
      batch.push({type: 'put', key: HEADS + node.key, value: node.key})
      batch.push({type: 'put', key: dag.logs.key(node.log, node.seq), value: messages.Entry.encode(log)})

      cb(null, node)
    })
  }

  // Local node; sign it.
  if (node.log === dag.id) {
    if (!dag.sign || node.signature) return done()
    dag.sign(node, function (err, sig) {
      if (err) return cb(err)
      if (!node.identity) node.identity = dag.identity
      node.signature = sig
      done()
    })
  // Remote node; verify it.
  } else {
    if (!dag.verify) return done()
    dag.verify(node, function (err, valid) {
      if (err) return cb(err)
      if (!valid) return cb(INVALID_SIGNATURE)
      done()
    })
  }
}
function ( dag , node , logLinks , batch , opts , cb ) { if ( opts . hash && node . key !== opts . hash ) return cb ( CHECKSUM_MISMATCH ) if ( opts . seq && node . seq !== opts . seq ) return cb ( INVALID_LOG ) var log = { change : node . change , node : node . key , links : logLinks } var onclone = function ( clone ) { if ( ! opts . log ) return cb ( null , clone , [ ] ) batch . push ( { type : 'put' , key : dag . logs . key ( node . log , node . seq ) , value : messages . Entry . encode ( log ) } ) cb ( null , clone ) } var done = function ( ) { dag . get ( node . key , { valueEncoding : 'binary' } , function ( _ , clone ) { // This node already exists somewhere in the hyperlog; add it to the // log's append-only log, but don't insert it again. if ( clone ) return onclone ( clone ) var links = node . links for ( var i = 0 ; i < links . length ; i ++ ) batch . push ( { type : 'del' , key : HEADS + links [ i ] } ) batch . push ( { type : 'put' , key : CHANGES + lexint . pack ( node . change , 'hex' ) , value : node . key } ) batch . push ( { type : 'put' , key : NODES + node . key , value : messages . Node . encode ( node ) } ) batch . push ( { type : 'put' , key : HEADS + node . key , value : node . key } ) batch . push ( { type : 'put' , key : dag . logs . key ( node . log , node . seq ) , value : messages . Entry . encode ( log ) } ) cb ( null , node ) } ) } // Local node; sign it. if ( node . log === dag . id ) { if ( ! dag . sign || node . signature ) return done ( ) dag . sign ( node , function ( err , sig ) { if ( err ) return cb ( err ) if ( ! node . identity ) node . identity = dag . identity node . signature = sig done ( ) } ) // Remote node; verify it. } else { if ( ! dag . verify ) return done ( ) dag . verify ( node , function ( err , valid ) { if ( err ) return cb ( err ) if ( ! valid ) return cb ( INVALID_SIGNATURE ) done ( ) } ) } }
Adds a new hyperlog node to an existing array of leveldb batch insertions. This includes performing crypto signing and verification. Performs deduplication; returns the existing node if alreay present in the hyperlog.
Adds a new hyperlog node to an existing array of leveldb batch insertions . This includes performing crypto signing and verification . Performs deduplication ; returns the existing node if alreay present in the hyperlog .