Skip to content

Commit

Permalink
Merge pull request #70 from maraoz/readme
Browse files Browse the repository at this point in the history
Readme updated
  • Loading branch information
maraoz committed Apr 30, 2015
2 parents 145de82 + fe820c8 commit 80141ab
Show file tree
Hide file tree
Showing 28 changed files with 2,067 additions and 1,658 deletions.
2,047 changes: 1,771 additions & 276 deletions README.md

Large diffs are not rendered by default.

18 changes: 0 additions & 18 deletions api/config/default.yml

This file was deleted.

16 changes: 0 additions & 16 deletions api/config/livenet.yml

This file was deleted.

16 changes: 0 additions & 16 deletions api/config/testnet.yml

This file was deleted.

6 changes: 2 additions & 4 deletions api/controllers/addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ var _ = bitcore.deps._;
var $ = bitcore.util.preconditions;
var Address = bitcore.Address;

var BitcoreNode = require('../../');

var Addresses = {};

var node;
Expand All @@ -23,7 +21,7 @@ Addresses.setNode = function(aNode) {
* Finds an address' info by it's string representation
*/
Addresses.addressParam = function(req, res, next, address) {
if (!Address.isValid(address)) {
if (!Address.isValid(address, bitcore.Networks.defaultNetwork)) {
res.status(422);
res.send('/v1/addresses/ parameter must be a valid bitcoin address');
return;
Expand Down Expand Up @@ -76,7 +74,7 @@ Addresses.utxos = function(req, res) {
$.checkState(_.all(req.addresses, function(addr) {
return addr instanceof Address;
}));
node.getUTXOs(req.addresses)
node.addressService.getUnspent(req.addresses)
.then(function(utxos) {
res.send(utxos);
});
Expand Down
7 changes: 3 additions & 4 deletions api/controllers/blocks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

var bitcore = require('bitcore');
var _ = bitcore.deps._;
var $ = bitcore.util.preconditions;
var Block = bitcore.Block;

var BitcoreNode = require('../../');
var errors = require('../../lib/errors');

var Blocks = {};

Expand All @@ -28,7 +27,7 @@ Blocks.blockHashParam = function(req, res, next, blockHash) {
req.block = block;
})
.then(next)
.catch(BitcoreNode.errors.Blocks.NotFound, function() {
.catch(errors.Blocks.NotFound, function() {
res.status(404).send('Block with id ' + blockHash + ' not found');
})
.catch(function() {
Expand All @@ -46,7 +45,7 @@ Blocks.heightParam = function(req, res, next, height) {
req.block = block;
})
.then(next)
.catch(BitcoreNode.errors.Blocks.NotFound, function() {
.catch(errors.Blocks.NotFound, function() {
res.status(404).send('Block with height ' + height + ' not found');
})
.catch(function() {
Expand Down
22 changes: 5 additions & 17 deletions api/controllers/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var _ = bitcore.deps._;
var $ = bitcore.util.preconditions;
var Transaction = bitcore.Transaction;

var BitcoreNode = require('../../');
var errors = require('../../lib/errors');

var Transactions = {};

Expand All @@ -30,7 +30,7 @@ Transactions.txHashParam = function(req, res, next, txHash) {
req.tx = tx;
})
.then(next)
.catch(BitcoreNode.errors.Transactions.NotFound, function() {
.catch(errors.Transactions.NotFound, function() {
res.status(404).send('Transaction with id ' + txHash + ' not found');
})
.catch(function() {
Expand Down Expand Up @@ -81,24 +81,12 @@ Transactions.send = function(req, res) {
.then(function() {
res.send('Transaction broadcasted successfully');
})
.catch(BitcoreNode.errors.Transactions.CantBroadcast, function(err) {
.catch(errors.Transactions.CantBroadcast, function(err) {
res.status(422).send(err.message);
});
};


/*
* Returns a list of transactions given certain request options
*/
Transactions.list = function(req, res) {
var opts = {};
opts.address = req.address;
node.listTransactions(opts)
.then(function(transactions) {
res.send(transactions);
});
};


var buildIOHelper = function(name) {
$.checkArgument(name === 'inputs' || name === 'outputs');
Expand All @@ -110,11 +98,11 @@ var buildIOHelper = function(name) {
' for ' + req.tx.id + ' not found, it only has ' + req.tx[name].length + ' ' + name + '.');
return;
}
res.send(req.tx[name][req.index].toJSON());
res.send(req.tx[name][req.index].toObject());
return;
}
res.send(req.tx[name].map(function(x) {
return x.toJSON();
return x.toObject();
}));
};

Expand Down
15 changes: 0 additions & 15 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
'use strict';

var BitcoreHTTP = require('./lib/http');
var bitcore = require('bitcore');

if (require.main === module) {
var config = require('config');
var network = config.get('BitcoreHTTP.BitcoreNode').network;
console.log('Starting bitcore-node-http', network, 'network');
bitcore.Networks.defaultNetwork = bitcore.Networks.get(network);
var http = BitcoreHTTP.create(config.get('BitcoreHTTP'));
http.start()
.catch(function(err) {
http.stop();
throw err;
});
}

module.exports = BitcoreHTTP;
2 changes: 2 additions & 0 deletions api/lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ BitcoreHTTP.prototype.setupExpress = function() {
app.use(morgan('dev'));
}

app.set('json spaces', 2);

// install routes
app.use('/', routes(this.node));

Expand Down
1 change: 0 additions & 1 deletion api/routes/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function initRouter(node) {

// Address routes
router.get('/addresses/:address', Addresses.get);
router.get('/addresses/:address/transactions', Transactions.list);
router.get('/addresses/:addresses/utxos', Addresses.utxos);

// error routes
Expand Down
29 changes: 4 additions & 25 deletions api/test/v1/addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@ var _ = bitcore.deps._;


var mockAddresses = require('../data/addresses');
var mockTransactions = require('../data/transactions');

describe('BitcoreHTTP v1 addresses routes', function() {

// mocks
var transactionList = _.values(mockTransactions);
var nodeMock, agent;
var txs_for_addr = function(addr) {
var amount = mockAddresses[addr].summary.transactions.length;
return transactionList.slice(0, amount);
};
var utxos_for_addrs = function(addrs) {
return _.reduce(addrs, function(utxos, addr) {
return utxos.concat(mockAddresses[addr].utxos);
Expand Down Expand Up @@ -53,10 +47,7 @@ describe('BitcoreHTTP v1 addresses routes', function() {
nodeMock.addressService.getSummary = function(address) {
return Promise.resolve(mockAddresses[address.toString()].summary);
};
nodeMock.listTransactions = function(opts) {
return Promise.resolve(txs_for_addr(opts.address));
};
nodeMock.getUTXOs = function(addresses) {
nodeMock.addressService.getUnspent = function(addresses) {
return Promise.resolve(utxos_for_addrs(addresses));
};
agent = require('../app')(nodeMock);
Expand All @@ -77,19 +68,7 @@ describe('BitcoreHTTP v1 addresses routes', function() {
it('works with valid address ' + addr, function(cb) {
agent.get('/v1/addresses/' + addr)
.expect(200)
.expect(JSON.stringify(info.summary), cb);
});
});
});
describe('/addresses/:address/transactions', function() {
it('fails with invalid address', function(cb) {
failsWithInvalidAddress(agent, '/v1/addresses/1BpbpfLdY7oBS9gK7aDXgvMgr1DpvNH3B2/transactions', cb);
});
_.keys(mockAddresses).forEach(function(addr) {
it('works with valid address ' + addr, function(cb) {
agent.get('/v1/addresses/' + addr + '/transactions')
.expect(200)
.expect(JSON.stringify(txs_for_addr(addr)), cb);
.expect(info.summary, cb);
});
});
});
Expand All @@ -104,7 +83,7 @@ describe('BitcoreHTTP v1 addresses routes', function() {
it('works with valid address ' + addr, function(cb) {
agent.get('/v1/addresses/' + addr + '/utxos')
.expect(200)
.expect(JSON.stringify(utxos_for_addrs([addr])), cb);
.expect(utxos_for_addrs([addr]), cb);
});
});
});
Expand All @@ -118,7 +97,7 @@ describe('BitcoreHTTP v1 addresses routes', function() {
var path = '/v1/addresses/' + list + '/utxos';
agent.get(path)
.expect(200)
.expect(JSON.stringify(utxos_for_addrs(addresses)), cb);
.expect(utxos_for_addrs(addresses), cb);
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions api/test/v1/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('BitcoreHTTP v1 blocks routes', function() {
it('works with valid blockHash ...' + hash.substring(hash.length - 8), function(cb) {
agent.get('/v1/blocks/' + hash)
.expect(200)
.expect(block.toJSON(), cb);
.expect(block.toObject(), cb);
});
});
});
Expand All @@ -138,7 +138,7 @@ describe('BitcoreHTTP v1 blocks routes', function() {
it('works with valid height', function(cb) {
agent.get('/v1/blocks/100000')
.expect(200)
.expect(b1.toJSON(), cb);
.expect(b1.toObject(), cb);
});
});

Expand Down
6 changes: 3 additions & 3 deletions api/test/v1/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('BitcoreHTTP v1 transactions routes', function() {
it('works with valid txHash ...' + hash.substring(hash.length - 8), function(cb) {
agent.get('/v1/transactions/' + hash)
.expect(200)
.expect(mockTransactions[hash].toJSON(), cb);
.expect(mockTransactions[hash].toObject(), cb);
});
});
});
Expand Down Expand Up @@ -112,15 +112,15 @@ describe('BitcoreHTTP v1 transactions routes', function() {
agent.get('/v1/transactions/' + hash + '/' + name + '/')
.expect(200)
.expect(tx[name].map(function(x) {
return x.toJSON();
return x.toObject();
}), cb);
});
var canGetSpecificInput = function(i) {
var x = tx[name][i];
return function(cb) {
agent.get('/v1/transactions/' + hash + '/' + name + '/' + i)
.expect(200)
.expect(x.toJSON(), cb);
.expect(x.toObject(), cb);
};
};
for (var i = 0; i < tx[name].length; i++) {
Expand Down
32 changes: 17 additions & 15 deletions config/default.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
BitcoreNode:
LevelUp: ./db
network: livenet
NetworkMonitor:
host: localhost
port: 8333
Reporter: none # none, simple, matrix
RPC:
user: user
pass: password
protocol: http # http, https
#rejectUnauthorized: false
#disableAgent: true
host: 127.0.0.1
port: 8332
BitcoreHTTP:
port: 8080
logging: true
BitcoreNode:
LevelUp: ./db
network: livenet
NetworkMonitor:
host: localhost
port: 8333
RPC:
user: user
pass: password
protocol: http #http, https
#rejectUnauthorized: false
#disableAgent: true
host: 127.0.0.1
port: 8332
28 changes: 15 additions & 13 deletions config/livenet.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
BitcoreNode:
LevelUp: ./db
network: livenet
NetworkMonitor:
host: localhost
port: 8333
Reporter: none # none, simple, matrix
RPC:
user: user
pass: password
protocol: http
host: 127.0.0.1
port: 8332
BitcoreHTTP:
port: 8080
logging: true
BitcoreNode:
LevelUp: ./db
network: livenet
NetworkMonitor:
host: localhost
port: 8333
RPC:
user: user
pass: password
protocol: http
host: 127.0.0.1
port: 8332
Loading

0 comments on commit 80141ab

Please sign in to comment.