Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
add getTransaction endpoint, upgrade to bitcore-lib 0.13.14
Browse files Browse the repository at this point in the history
  • Loading branch information
maraoz committed Dec 16, 2016
1 parent 98f2238 commit 0c388ca
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ At the moment, only Insight is supported, and only getting the UTXOs for an addr
var explorers = require('bitcore-explorers');
var insight = new explorers.Insight();

insight.getUnspentUtxos('1Bitcoin...', function(err, utxos) {
insight.getUtxos('1Bitcoin...', function(err, utxos) {
if (err) {
// Handle errors...
} else {
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
models: require('./models'),
Insight: require('./insight')
Insight: require('./insight'),
bitcore: require('bitcore-lib'),
};
32 changes: 29 additions & 3 deletions lib/insight.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,43 @@ function Insight(url, network) {
}

/**
* @callback Insight.GetUnspentUtxosCallback
* @callback Insight.GetTransactionCallback
* @param {Error} err
* @param {Object} transaction
*/

/**
* Get transaction by txid
* @param {string} txid
* @param {GetTransactionCallback} callback
*/
Insight.prototype.getTransaction = function(txid, callback) {
$.checkArgument(_.isFunction(callback));
$.checkArgument(_.isString(txid));
$.checkArgument(txid.length === 64);

this.requestGet('/api/tx/' + txid, function(err, res, body) {
if (err || res.statusCode !== 200) {
return callback(err || res);
}
var tx = JSON.parse(body);

return callback(null, tx);
});
};

/**
* @callback Insight.GetUtxosCallback
* @param {Error} err
* @param {Array.UnspentOutput} utxos
*/

/**
* Retrieve a list of unspent outputs associated with an address or set of addresses
* @param {Address|string|Array.Address|Array.string} addresses
* @param {GetUnspentUtxosCallback} callback
* @param {GetUtxosCallback} callback
*/
Insight.prototype.getUnspentUtxos = function(addresses, callback) {
Insight.prototype.getUtxos = function(addresses, callback) {
$.checkArgument(_.isFunction(callback));
if (!_.isArray(addresses)) {
addresses = [addresses];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"sinon": "^1.12.2"
},
"dependencies": {
"bitcore-lib": "^0.13.7",
"bitcore-lib": "^0.13.14",
"browser-request": "^0.3.3",
"request": "^2.51.0"
}
Expand Down
12 changes: 6 additions & 6 deletions test/insight.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ describe('Insight', function() {
});
});
it('can receive an address', function(callback) {
insight.getUnspentUtxos(new Address(address), callback);
insight.getUtxos(new Address(address), callback);
});
it('can receive a address as a string', function(callback) {
insight.getUnspentUtxos(address, callback);
insight.getUtxos(address, callback);
});
it('can receive an array of addresses', function(callback) {
insight.getUnspentUtxos([address, new Address(address)], callback);
insight.getUtxos([address, new Address(address)], callback);
});
it('errors if server is not available', function(callback) {
insight.requestPost.onFirstCall().callsArgWith(2, 'Unable to connect');
insight.getUnspentUtxos(address, function(error) {
insight.getUtxos(address, function(error) {
expect(error).to.equal('Unable to connect');
callback();
});
Expand All @@ -76,7 +76,7 @@ describe('Insight', function() {
insight.requestPost.onFirstCall().callsArgWith(2, null, {
statusCode: 400
});
insight.getUnspentUtxos(address, function(error) {
insight.getUtxos(address, function(error) {
expect(error).to.deep.equal({
statusCode: 400
});
Expand All @@ -96,7 +96,7 @@ describe('Insight', function() {
insight.requestPost.onFirstCall().callsArgWith(2, null, {
statusCode: 200
}, [invalidUtxo]);
insight.getUnspentUtxos(address, function(error, unspent) {
insight.getUtxos(address, function(error, unspent) {
expect(error).to.exist;
expect(error.name).to.equal('bitcore.ErrorInvalidArgument');
expect(error.toString()).to.contain('scriptPubKey');
Expand Down

0 comments on commit 0c388ca

Please sign in to comment.