Drop explorers
This commit is contained in:
parent
7d4cda171a
commit
6923b9bed9
|
@ -1,36 +0,0 @@
|
|||
title: Insight Explorer
|
||||
description: Provides an interface to fetch information about the state of the blockchain from a trusted Insight server.
|
||||
---
|
||||
# Insight
|
||||
|
||||
## Description
|
||||
|
||||
`bitcore.transport.explorers.Insight` is a simple agent to perform queries to an Insight blockchain explorer. The default servers are `https://insight.bitpay.com` and `https://test-insight.bitpay.com`, hosted by BitPay Inc. You can (and we strongly suggest you do) run your own insight server. For more information, head to https://github.com/bitpay/insight-api
|
||||
|
||||
There are currently two methods implemented (the API will grow as features are requested): `getUnspentUtxos` and `broadcast`.
|
||||
|
||||
### Retrieving Unspent UTXOs for an Address (or set of)
|
||||
|
||||
```javascript
|
||||
var insight = new Insight();
|
||||
insight.getUnspentUtxos('1Bitcoin...', function(err, utxos) {
|
||||
if (err) {
|
||||
// Handle errors...
|
||||
} else {
|
||||
// Maybe use the UTXOs to create a transaction
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Broadcasting a Transaction
|
||||
|
||||
```javascript
|
||||
var insight = new Insight();
|
||||
insight.broadcast(tx, function(err, returnedTxId) {
|
||||
if (err) {
|
||||
// Handle errors...
|
||||
} else {
|
||||
// Mark the transaction as broadcasted
|
||||
}
|
||||
});
|
||||
```
|
|
@ -1,3 +0,0 @@
|
|||
module.exports = {
|
||||
Insight: require('./insight')
|
||||
};
|
|
@ -1,115 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
var $ = require('../../util/preconditions');
|
||||
var _ = require('lodash');
|
||||
|
||||
var Address = require('../../address');
|
||||
var JSUtil = require('../../util/js');
|
||||
var Networks = require('../../networks');
|
||||
var Transaction = require('../../transaction');
|
||||
var UnspentOutput = Transaction.UnspentOutput;
|
||||
|
||||
var request = require('request');
|
||||
|
||||
/**
|
||||
* Allows the retrieval of information regarding the state of the blockchain
|
||||
* (and broadcasting of transactions) from/to a trusted Insight server.
|
||||
* @param {string=} url the url of the Insight server
|
||||
* @param {Network=} network whether to use livenet or testnet
|
||||
* @constructor
|
||||
*/
|
||||
function Insight(url, network) {
|
||||
if (!url && !network) {
|
||||
return new Insight(Networks.defaultNetwork);
|
||||
}
|
||||
if (Networks.get(url)) {
|
||||
network = Networks.get(url);
|
||||
if (network === Networks.livenet) {
|
||||
url = 'https://insight.bitpay.com';
|
||||
} else {
|
||||
url = 'https://test-insight.bitpay.com';
|
||||
}
|
||||
}
|
||||
JSUtil.defineImmutable(this, {
|
||||
url: url,
|
||||
network: Networks.get(network) || Networks.defaultNetwork
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @callback Insight.GetUnspentUtxosCallback
|
||||
* @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
|
||||
*/
|
||||
Insight.prototype.getUnspentUtxos = function(addresses, callback) {
|
||||
$.checkArgument(_.isFunction(callback));
|
||||
if (!_.isArray(addresses)) {
|
||||
addresses = [addresses];
|
||||
}
|
||||
addresses = _.map(addresses, function(address) { return new Address(address); });
|
||||
|
||||
this.requestPost('/api/addrs/utxo', {
|
||||
addrs: _.map(addresses, function(address) { return address.toString(); }).join(',')
|
||||
}, function(err, res, unspent) {
|
||||
if (err || res.statusCode !== 200) {
|
||||
return callback(err || res);
|
||||
}
|
||||
unspent = _.map(unspent, UnspentOutput);
|
||||
|
||||
return callback(null, unspent);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @callback Insight.BroadcastCallback
|
||||
* @param {Error} err
|
||||
* @param {string} txid
|
||||
*/
|
||||
|
||||
/**
|
||||
* Broadcast a transaction to the bitcoin network
|
||||
* @param {transaction|string} transaction
|
||||
* @param {BroadcastCallback} callback
|
||||
*/
|
||||
Insight.prototype.broadcast = function(transaction, callback) {
|
||||
$.checkArgument(JSUtil.isHexa(transaction) || transaction instanceof Transaction);
|
||||
$.checkArgument(_.isFunction(callback));
|
||||
if (transaction instanceof Transaction) {
|
||||
transaction = transaction.serialize();
|
||||
}
|
||||
|
||||
this.requestPost('/api/tx/send', {
|
||||
rawtx: transaction
|
||||
}, function(err, res, body) {
|
||||
if (err || res.statusCode !== 200) {
|
||||
return callback(err || body);
|
||||
}
|
||||
return callback(null, body ? body.txid : null);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Internal function to make a post request to the server
|
||||
* @param {string} path
|
||||
* @param {?} data
|
||||
* @param {function} callback
|
||||
* @private
|
||||
*/
|
||||
Insight.prototype.requestPost = function(path, data, callback) {
|
||||
$.checkArgument(_.isString(path));
|
||||
$.checkArgument(_.isFunction(callback));
|
||||
request({
|
||||
method: 'POST',
|
||||
url: this.url + path,
|
||||
json: data
|
||||
}, callback);
|
||||
};
|
||||
|
||||
module.exports = Insight;
|
|
@ -1,6 +0,0 @@
|
|||
/**
|
||||
* @namespace Transport
|
||||
*/
|
||||
module.exports = {
|
||||
explorers: require('./explorers')
|
||||
};
|
Loading…
Reference in New Issue