Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Added support for a "balance" endpoint #8

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ service((err, adapter) => {

// start the API server
debug('starting API server')
app.use(api(adapter, {testnet: TESTNET}))
app.use(api(adapter, {testnet: TESTNET || REGTEST}))
app.listen(process.env.SERVER_PORT);
debug("App listening on port "+process.env.SERVER_PORT);
})
38 changes: 38 additions & 0 deletions lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let express = require('express')
let parallel = require('run-parallel')
let rpc = require('./rpc')
let types = require('indexd/types')
let BN = require('bn.js')

function Hex256bit (value) {
return typeof value === 'string' &&
Expand Down Expand Up @@ -107,6 +108,43 @@ module.exports = function initialize (adapter, opts) {
})
})

router.get('/a/:address/balance', (req, res) => {
let scId
try {
let script = bitcoin.address.toOutputScript(req.params.address, network)
scId = bitcoin.crypto.sha256(script).toString('hex')
} catch (e) { return respond(req, res, e) }

let height = resolveHeight(req.query.height)

parallel({
tip: (cb) => adapter.blockchain.db.get(types.tip, {}, cb),
utxos: (cb) => adapter.utxosByScriptId(scId, height, cb)
}, (err, results) => {
if (err) return respond(req, res, err)

let tipHeight = results.tip.height
let balance = {
confirmed: new BN('0', 16),
unconfirmed: new BN('0', 16)
}

Object.keys(results.utxos).forEach(function (key) {
let utxo = results.utxos[key]
let height = utxo.height
if (height && height <= tipHeight) {
balance.confirmed.iadd(new BN(utxo.value))
} else {
balance.unconfirmed.iadd(new BN(utxo.value))
}
})
respond(req, res, null, {
confirmed: balance.confirmed.toString(10),
unconfirmed: balance.unconfirmed.toString(10)
})
})
})

router.get('/a/:address/txs', (req, res) => {
let scId
try {
Expand Down
1 change: 0 additions & 1 deletion lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,3 @@ module.exports = function initialize (callback) {
callback(null, adapter)
})
}

5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "MIT",
"dependencies": {
"bitcoinjs-lib": "^3.3.1",
"bn.js": "^4.11.8",
"dotenv": "^4.0.0",
"express": "^4.16.2",
"indexd": "=0.8.13",
Expand Down