let crypto = require('crypto')

function sha256 (hex) {
  return crypto.createHash('sha256')
    .update(Buffer.from(hex, 'hex'))
    .digest('hex')
}

function augment (tx) {
  tx.txBuffer = Buffer.from(tx.hex, 'hex')
  delete tx.hex
  tx.txId = tx.txid
  delete tx.txid
  tx.vin.forEach((input) => {
    input.prevTxId = input.txid
    delete input.txid
  })
  tx.vout.forEach((output) => {
    output.scId = sha256(output.scriptPubKey.hex)
    output.value = Math.round(output.value * 1e8)
    output.vout = output.n
    delete output.n
  })
  tx.ins = tx.vin
  tx.outs = tx.vout
  delete tx.vin
  delete tx.vout
  return tx
}

function transaction (rpc, txId, next, forgiving) {
  rpc('getrawtransaction', [txId, true], (err, tx) => {
    if (err) {
      if (forgiving && /No such mempool or blockchain transaction/.test(err)) return next()
      return next(err)
    }

    next(null, augment(tx))
  })
}

function block (rpc, blockId, done) {
  rpc('getblock', [blockId, 2], (err, block) => {
    if (err) return done(err)

    block.transactions = block.tx.map(t => augment(t))
    delete block.tx
    done(null, block)
  })
}

function header (rpc, blockId, done) {
  rpc('getblockheader', [blockId, false], (err, hex) => {
    if (err) return done(err)

    done(null, Buffer.from(hex, 'hex'))
  })
}

function headerJSON (rpc, blockId, done) {
  rpc('getblockheader', [blockId, true], (err, hex) => {
    if (err) return done(err)

    done(null, Buffer.from(hex, 'hex'))
  })
}

function mempool (rpc, done) {
  rpc('getrawmempool', [false], done)
}

function blockIdAtHeight (rpc, height, done) {
  rpc('getblockhash', [height], done)
}

function tip (rpc, done) {
  rpc('getbestblockhash', [], done)
}

module.exports = {
  block,
  blockIdAtHeight,
  header,
  headerJSON,
  mempool,
  tip,
  transaction
}

@@ -23,16 +23,245 @@
 crypto')
+%0Alet debug = require('./debug')('indexd:rpc')%0A%0Afunction rpcd (rpc, method, params, done) %7B%0A  rpc(method, params, (err, result) =%3E %7B%0A    if (err) debug(method, params, err)%0A    if (err) return done(err)%0A%0A    done(null, err)%0A  %7D)%0A%7D
 %0A%0Afuncti
@@ -857,289 +857,8 @@
 %0A%7D%0A%0A
-function transaction (rpc, txId, next, forgiving) %7B%0A  rpc('getrawtransaction', %5BtxId, true%5D, (err, tx) =%3E %7B%0A    if (err) %7B%0A      if (forgiving && /No such mempool or blockchain transaction/.test(err)) return next()%0A      return next(err)%0A    %7D%0A%0A    next(null, augment(tx))%0A  %7D)%0A%7D%0A%0A
 func
@@ -888,33 +888,39 @@
 d, done) %7B%0A  rpc
-(
+d(rpc, 
 'getblock', %5Bblo
@@ -1075,32 +1075,126 @@
  block)%0A  %7D)%0A%7D%0A%0A
+function blockIdAtHeight (rpc, height, done) %7B%0A  rpcd(rpc, 'getblockhash', %5Bheight%5D, done)%0A%7D%0A%0A
 function header 
@@ -1213,33 +1213,39 @@
 d, done) %7B%0A  rpc
-(
+d(rpc, 
 'getblockheader'
@@ -1399,33 +1399,39 @@
 d, done) %7B%0A  rpc
-(
+d(rpc, 
 'getblockheader'
@@ -1576,25 +1576,31 @@
 one) %7B%0A  rpc
-(
+d(rpc, 
 'getrawmempo
@@ -1635,36 +1635,16 @@
 ion 
-blockIdAtHeight (rpc, height
+tip (rpc
 , do
@@ -1654,22 +1654,32 @@
  %7B%0A  rpc
-(
+d(rpc, 
 'getb
+estb
 lockhash
@@ -1674,38 +1674,32 @@
 estblockhash', %5B
-height
 %5D, done)%0A%7D%0A%0Afunc
@@ -1708,59 +1708,280 @@
 on t
-ip (rpc, done) %7B%0A  rpc('getbest
+ransaction (rpc, txId, next, forgiving) %7B%0A  rpcd(rpc, 'getrawtransaction', %5BtxId, true%5D, (err, tx) =%3E %7B%0A    if (err) %7B%0A      if (forgiving && /No such mempool or 
 block
+c
 ha
-sh', %5B%5D, done
+in transaction/.test(err)) return next()%0A      return next(err)%0A    %7D%0A%0A    next(null, augment(tx))%0A  %7D
 )%0A%7D%0A

add debug for rpc