UNPKG

3.59 kBJavaScriptView Raw
1'use strict';
2
3var _ = require('lodash');
4var $ = require('../util/preconditions');
5var JSUtil = require('../util/js');
6
7var Script = require('../script');
8var Address = require('../address');
9var Unit = require('../unit');
10
11/**
12 * Represents an unspent output information: its script, associated amount and address,
13 * transaction id and output index.
14 *
15 * @constructor
16 * @param {object} data
17 * @param {string} data.txid the previous transaction id
18 * @param {string=} data.txId alias for `txid`
19 * @param {number} data.vout the index in the transaction
20 * @param {number=} data.outputIndex alias for `vout`
21 * @param {string|Script} data.scriptPubKey the script that must be resolved to release the funds
22 * @param {string|Script=} data.script alias for `scriptPubKey`
23 * @param {number} data.amount amount of bitcoins associated
24 * @param {number=} data.satoshis alias for `amount`, but expressed in satoshis (1 BTC = 1e8 satoshis)
25 * @param {string|Address=} data.address the associated address to the script, if provided
26 */
27function UnspentOutput(data) {
28 /* jshint maxcomplexity: 20 */
29 /* jshint maxstatements: 20 */
30 if (!(this instanceof UnspentOutput)) {
31 return new UnspentOutput(data);
32 }
33 $.checkArgument(_.isObject(data), 'Must provide an object from where to extract data');
34 var address = data.address ? new Address(data.address) : undefined;
35 var txId = data.txid ? data.txid : data.txId;
36 if (!txId || !JSUtil.isHexaString(txId) || txId.length > 64) {
37 // TODO: Use the errors library
38 throw new Error('Invalid TXID in object', data);
39 }
40 var outputIndex = _.isUndefined(data.vout) ? data.outputIndex : data.vout;
41 if (!_.isNumber(outputIndex)) {
42 throw new Error('Invalid outputIndex, received ' + outputIndex);
43 }
44 $.checkArgument(!_.isUndefined(data.scriptPubKey) || !_.isUndefined(data.script),
45 'Must provide the scriptPubKey for that output!');
46 var script = new Script(data.scriptPubKey || data.script);
47 $.checkArgument(!_.isUndefined(data.amount) || !_.isUndefined(data.satoshis),
48 'Must provide an amount for the output');
49 var amount = !_.isUndefined(data.amount) ? new Unit.fromBTC(data.amount).toSatoshis() : data.satoshis;
50 $.checkArgument(_.isNumber(amount), 'Amount must be a number');
51 JSUtil.defineImmutable(this, {
52 address: address,
53 txId: txId,
54 outputIndex: outputIndex,
55 script: script,
56 satoshis: amount
57 });
58}
59
60/**
61 * Provide an informative output when displaying this object in the console
62 * @returns string
63 */
64UnspentOutput.prototype.inspect = function() {
65 return '<UnspentOutput: ' + this.txId + ':' + this.outputIndex +
66 ', satoshis: ' + this.satoshis + ', address: ' + this.address + '>';
67};
68
69/**
70 * String representation: just "txid:index"
71 * @returns string
72 */
73UnspentOutput.prototype.toString = function() {
74 return this.txId + ':' + this.outputIndex;
75};
76
77/**
78 * Deserialize an UnspentOutput from an object
79 * @param {object|string} data
80 * @return UnspentOutput
81 */
82UnspentOutput.fromObject = function(data) {
83 return new UnspentOutput(data);
84};
85
86/**
87 * Returns a plain object (no prototype or methods) with the associated info for this output
88 * @return {object}
89 */
90UnspentOutput.prototype.toObject = UnspentOutput.prototype.toJSON = function toObject() {
91 return {
92 address: this.address ? this.address.toString() : undefined,
93 txid: this.txId,
94 vout: this.outputIndex,
95 scriptPubKey: this.script.toBuffer().toString('hex'),
96 amount: Unit.fromSatoshis(this.satoshis).toBTC()
97 };
98};
99
100module.exports = UnspentOutput;