fcoin/lib/bcoin/input.js
2015-12-22 05:00:55 -08:00

92 lines
1.9 KiB
JavaScript

/**
* input.js - input object for bcoin
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* https://github.com/indutny/bcoin
*/
var bn = require('bn.js');
var bcoin = require('../bcoin');
var utils = bcoin.utils;
/**
* Input
*/
function Input(options) {
var tx = options.tx;
var lock;
if (!(this instanceof Input))
return new Input(options);
if (!tx)
throw new Error('No TX passed into Input.');
this.out = {
tx: options.out.tx || null,
hash: options.out.hash || null,
index: options.out.index
};
this.script = options.script ? options.script.slice() : [];
this.seq = options.seq === undefined ? 0xffffffff : options.seq;
if (options.script && options.script._raw)
utils.hidden(this.script, '_raw', options.script._raw);
if (this.output) {
lock = this.lock;
if (lock >= 0) {
if (tx._lock === 0)
tx.lock = Math.max(lock, tx.lock);
if (!bcoin.script.spendable(this.output.script, tx.lock))
throw new Error('Cannot spend ' + utils.revHex(this.out.hash));
}
}
if (tx.lock !== 0) {
if (options.seq === undefined)
this.seq = 0;
}
}
Input.prototype.__defineGetter__('data', function() {
return bcoin.tx.getInputData(this);
});
Input.prototype.__defineGetter__('addr', function() {
return this.data.addr;
});
Input.prototype.__defineGetter__('type', function() {
return this.data.type;
});
Input.prototype.__defineGetter__('lock', function() {
if (!this.output)
return;
return this.output.lock;
});
Input.prototype.__defineGetter__('output', function() {
if (!this.out.tx)
return;
return this.out.tx.outputs[this.out.index];
});
Input.prototype.__defineGetter__('value', function() {
if (!this.output)
return;
return this.output.value;
});
Input.prototype.__defineGetter__('tx', function() {
return this.out.tx;
});
/**
* Expose
*/
module.exports = Input;