Esteban Ordano
10 years ago
7 changed files with 185 additions and 69 deletions
@ -0,0 +1,80 @@ |
|||
'use strict'; |
|||
|
|||
var Networks = require('../networks'); |
|||
var JSUtil = require('../util/js'); |
|||
var $ = require('../util/preconditions'); |
|||
var _ = require('lodash'); |
|||
var Address = require('../address'); |
|||
var Transaction = require('../transaction'); |
|||
var UTXO = require('../utxo'); |
|||
|
|||
var request = require('request'); |
|||
|
|||
// var insight = new Insight(Networks.livenet);
|
|||
|
|||
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; |
|||
} |
|||
|
|||
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, UTXO); |
|||
|
|||
return callback(null, unspent); |
|||
}); |
|||
}; |
|||
|
|||
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); |
|||
}); |
|||
}; |
|||
|
|||
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; |
@ -0,0 +1,72 @@ |
|||
'use strict'; |
|||
|
|||
var _ = require('lodash'); |
|||
var $ = require('./util/preconditions'); |
|||
var JSUtil = require('./util/js'); |
|||
|
|||
var Script = require('./script'); |
|||
var Address = require('./address'); |
|||
var Unit = require('./unit'); |
|||
|
|||
function UTXO(data) { |
|||
/* jshint maxcomplexity: 20 */ |
|||
/* jshint maxstatements: 20 */ |
|||
if (!(this instanceof UTXO)) { |
|||
return new UTXO(data); |
|||
} |
|||
$.checkArgument(_.isObject(data), 'Must provide an object from where to extract data'); |
|||
var address = data.address ? new Address(data.address) : undefined; |
|||
var txId = data.txid ? data.txid : data.txId; |
|||
if (!txId || !JSUtil.isHexaString(txId) || txId.length > 64) { |
|||
// TODO: Use the errors library
|
|||
throw new Error('Invalid TXID in object', data); |
|||
} |
|||
var outputIndex = _.isUndefined(data.vout) ? data.outputIndex : data.vout; |
|||
if (!_.isNumber(outputIndex)) { |
|||
throw new Error('Invalid outputIndex, received ' + outputIndex); |
|||
} |
|||
$.checkArgument(data.scriptPubKey || data.script, 'Must provide the scriptPubKey for that output!'); |
|||
var script = new Script(data.scriptPubKey || data.script); |
|||
$.checkArgument(data.amount || data.satoshis, 'Must provide the scriptPubKey for that output!'); |
|||
var amount = data.amount ? new Unit.fromBTC(data.amount).toSatoshis() : data.satoshis; |
|||
$.checkArgument(_.isNumber(amount), 'Amount must be a number'); |
|||
JSUtil.defineImmutable(this, { |
|||
address: address, |
|||
txId: txId, |
|||
outputIndex: outputIndex, |
|||
script: script, |
|||
satoshis: amount |
|||
}); |
|||
} |
|||
|
|||
UTXO.prototype.inspect = function() { |
|||
return '<UTXO: ' + this.txId + ':' + this.outputIndex + |
|||
', satoshis: ' + this.satoshis + ', address: ' + this.address + '>'; |
|||
}; |
|||
|
|||
UTXO.prototype.toString = function() { |
|||
return this.txId + ':' + this.outputIndex; |
|||
}; |
|||
|
|||
UTXO.fromJSON = UTXO.fromObject = function(data) { |
|||
if (_.isString(data)) { |
|||
data = JSON.parse(data); |
|||
} |
|||
return new UTXO(data); |
|||
}; |
|||
|
|||
UTXO.prototype.toJSON = function() { |
|||
return JSON.stringify(this.toObject()); |
|||
}; |
|||
|
|||
UTXO.prototype.toObject = function() { |
|||
return { |
|||
address: this.address.toObject(), |
|||
txid: this.txId, |
|||
vout: this.outputIndex, |
|||
scriptPubKey: this.script.toObject(), |
|||
amount: Unit.fromSatoshis(this.satoshis).toBTC() |
|||
}; |
|||
}; |
|||
|
|||
module.exports = UTXO; |
Loading…
Reference in new issue