You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.3 KiB
73 lines
2.3 KiB
10 years ago
|
'use strict';
|
||
|
|
||
|
var _ = require('lodash');
|
||
10 years ago
|
var $ = require('../util/preconditions');
|
||
|
var JSUtil = require('../util/js');
|
||
10 years ago
|
|
||
10 years ago
|
var Script = require('../script');
|
||
|
var Address = require('../address');
|
||
|
var Unit = require('../unit');
|
||
10 years ago
|
|
||
10 years ago
|
function UnspentOutput(data) {
|
||
10 years ago
|
/* jshint maxcomplexity: 20 */
|
||
|
/* jshint maxstatements: 20 */
|
||
10 years ago
|
if (!(this instanceof UnspentOutput)) {
|
||
|
return new UnspentOutput(data);
|
||
10 years ago
|
}
|
||
|
$.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
|
||
|
});
|
||
|
}
|
||
|
|
||
10 years ago
|
UnspentOutput.prototype.inspect = function() {
|
||
|
return '<UnspentOutput: ' + this.txId + ':' + this.outputIndex +
|
||
10 years ago
|
', satoshis: ' + this.satoshis + ', address: ' + this.address + '>';
|
||
|
};
|
||
|
|
||
10 years ago
|
UnspentOutput.prototype.toString = function() {
|
||
10 years ago
|
return this.txId + ':' + this.outputIndex;
|
||
|
};
|
||
|
|
||
10 years ago
|
UnspentOutput.fromJSON = UnspentOutput.fromObject = function(data) {
|
||
10 years ago
|
if (JSUtil.isValidJSON(data)) {
|
||
10 years ago
|
data = JSON.parse(data);
|
||
|
}
|
||
10 years ago
|
return new UnspentOutput(data);
|
||
10 years ago
|
};
|
||
|
|
||
10 years ago
|
UnspentOutput.prototype.toJSON = function() {
|
||
10 years ago
|
return JSON.stringify(this.toObject());
|
||
|
};
|
||
|
|
||
10 years ago
|
UnspentOutput.prototype.toObject = function() {
|
||
10 years ago
|
return {
|
||
10 years ago
|
address: this.address.toString(),
|
||
10 years ago
|
txid: this.txId,
|
||
|
vout: this.outputIndex,
|
||
10 years ago
|
scriptPubKey: this.script.toBuffer().toString('hex'),
|
||
10 years ago
|
amount: Unit.fromSatoshis(this.satoshis).toBTC()
|
||
|
};
|
||
|
};
|
||
|
|
||
10 years ago
|
module.exports = UnspentOutput;
|