2016-04-15 05:28:23 -07:00
|
|
|
/*!
|
2015-12-22 04:57:35 -08:00
|
|
|
* input.js - input object for bcoin
|
|
|
|
|
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
|
2017-02-03 22:47:26 -08:00
|
|
|
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
|
2016-06-09 16:18:50 -07:00
|
|
|
* https://github.com/bcoin-org/bcoin
|
2015-12-22 04:57:35 -08:00
|
|
|
*/
|
|
|
|
|
|
2016-06-13 01:06:01 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-06-29 20:54:07 -07:00
|
|
|
const assert = require('assert');
|
2017-11-16 11:43:24 -08:00
|
|
|
const bio = require('bufio');
|
2017-06-29 20:54:07 -07:00
|
|
|
const Network = require('../protocol/network');
|
|
|
|
|
const Witness = require('../script/witness');
|
|
|
|
|
const Outpoint = require('./outpoint');
|
2016-06-17 02:26:48 -07:00
|
|
|
|
2015-12-22 04:57:35 -08:00
|
|
|
/**
|
2017-11-15 18:54:47 -08:00
|
|
|
* Input
|
2016-04-15 05:28:23 -07:00
|
|
|
* Represents a transaction input.
|
2017-02-03 22:47:26 -08:00
|
|
|
* @alias module:primitives.Input
|
2016-06-18 22:32:14 -07:00
|
|
|
* @property {Outpoint} prevout - Outpoint.
|
2016-04-15 05:28:23 -07:00
|
|
|
* @property {Script} script - Input script / scriptSig.
|
|
|
|
|
* @property {Number} sequence - nSequence.
|
|
|
|
|
* @property {Witness} witness - Witness (empty if not present).
|
2015-12-22 04:57:35 -08:00
|
|
|
*/
|
|
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
class Input {
|
|
|
|
|
/**
|
|
|
|
|
* Create transaction input.
|
|
|
|
|
* @constructor
|
|
|
|
|
* @param {Object} options
|
|
|
|
|
*/
|
2015-12-22 04:57:35 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
constructor(options) {
|
|
|
|
|
this.prevout = new Outpoint();
|
|
|
|
|
this.witness = new Witness();
|
2018-01-04 20:43:30 -08:00
|
|
|
this.sequence = 0xffffffff;
|
2016-07-01 02:15:25 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
if (options)
|
|
|
|
|
this.fromOptions(options);
|
2016-06-28 18:11:04 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Inject properties from options object.
|
|
|
|
|
* @private
|
|
|
|
|
* @param {Object} options
|
|
|
|
|
*/
|
2015-12-22 14:50:07 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
fromOptions(options) {
|
|
|
|
|
assert(options, 'Input data is required.');
|
2016-06-17 01:34:59 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
this.prevout.fromOptions(options.prevout);
|
2016-06-20 01:09:27 -07:00
|
|
|
|
2018-01-04 20:43:30 -08:00
|
|
|
if (options.witness)
|
|
|
|
|
this.witness.fromOptions(options.witness);
|
|
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
if (options.sequence != null) {
|
|
|
|
|
assert((options.sequence >>> 0) === options.sequence,
|
|
|
|
|
'Sequence must be a uint32.');
|
|
|
|
|
this.sequence = options.sequence;
|
|
|
|
|
}
|
2017-01-15 15:14:48 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
return this;
|
|
|
|
|
}
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Instantiate an Input from options object.
|
2017-12-30 03:49:28 -08:00
|
|
|
* @param {Object} options
|
2017-11-15 18:54:47 -08:00
|
|
|
* @returns {Input}
|
|
|
|
|
*/
|
2016-02-25 13:23:02 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
static fromOptions(options) {
|
|
|
|
|
return new this().fromOptions(options);
|
|
|
|
|
}
|
2016-02-25 13:23:02 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Clone the input.
|
|
|
|
|
* @returns {Input}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
clone() {
|
|
|
|
|
const input = new this.constructor();
|
|
|
|
|
input.prevout = this.prevout;
|
|
|
|
|
input.witness.inject(this.witness);
|
2018-01-04 20:43:30 -08:00
|
|
|
input.sequence = this.sequence;
|
2017-11-15 18:54:47 -08:00
|
|
|
return input;
|
|
|
|
|
}
|
2017-07-27 14:51:55 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Test equality against another input.
|
|
|
|
|
* @param {Input} input
|
|
|
|
|
* @returns {Boolean}
|
|
|
|
|
*/
|
2016-02-25 13:23:02 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
equals(input) {
|
|
|
|
|
assert(Input.isInput(input));
|
|
|
|
|
return this.prevout.equals(input.prevout);
|
|
|
|
|
}
|
2016-02-05 22:46:04 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Compare against another input (BIP69).
|
|
|
|
|
* @param {Input} input
|
|
|
|
|
* @returns {Number}
|
|
|
|
|
*/
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
compare(input) {
|
|
|
|
|
assert(Input.isInput(input));
|
|
|
|
|
return this.prevout.compare(input.prevout);
|
|
|
|
|
}
|
2016-02-26 17:08:06 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Get the previous output script's address. Will "guess"
|
|
|
|
|
* based on the input script and/or witness if coin
|
|
|
|
|
* is not available.
|
|
|
|
|
* @param {Coin?} coin
|
|
|
|
|
* @returns {Address?} addr
|
|
|
|
|
*/
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
getAddress(coin) {
|
|
|
|
|
if (this.isCoinbase())
|
|
|
|
|
return null;
|
2016-02-01 11:39:38 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
if (coin)
|
|
|
|
|
return coin.getAddress();
|
2016-10-18 06:39:00 -07:00
|
|
|
|
2018-01-02 20:24:56 -08:00
|
|
|
return this.witness.getInputAddress();
|
2017-11-15 18:54:47 -08:00
|
|
|
}
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Get the address hash.
|
2017-12-20 09:50:17 -08:00
|
|
|
* @param {Coin?} coin
|
2017-11-15 18:54:47 -08:00
|
|
|
* @param {String?} enc
|
|
|
|
|
* @returns {Hash} hash
|
|
|
|
|
*/
|
2016-02-02 16:09:48 -08:00
|
|
|
|
2017-12-20 09:50:17 -08:00
|
|
|
getHash(coin, enc) {
|
|
|
|
|
const addr = this.getAddress(coin);
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
if (!addr)
|
|
|
|
|
return null;
|
2016-12-09 16:31:52 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
return addr.getHash(enc);
|
|
|
|
|
}
|
2016-12-09 16:31:52 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Test to see if nSequence is equal to uint32max.
|
|
|
|
|
* @returns {Boolean}
|
|
|
|
|
*/
|
2015-12-22 19:27:15 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
isFinal() {
|
|
|
|
|
return this.sequence === 0xffffffff;
|
|
|
|
|
}
|
2016-12-10 19:42:46 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Test to see if outpoint is null.
|
|
|
|
|
* @returns {Boolean}
|
|
|
|
|
*/
|
2016-11-17 04:05:32 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
isCoinbase() {
|
|
|
|
|
return this.prevout.isNull();
|
2016-12-09 03:52:36 -08:00
|
|
|
}
|
2016-06-22 11:44:43 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Convert the input to a more user-friendly object.
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
*/
|
2016-02-22 01:54:42 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
inspect() {
|
|
|
|
|
return this.format();
|
|
|
|
|
}
|
2016-06-20 01:09:27 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Convert the input to a more user-friendly object.
|
|
|
|
|
* @param {Coin?} coin
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
format(coin) {
|
|
|
|
|
return {
|
|
|
|
|
address: this.getAddress(coin),
|
|
|
|
|
prevout: this.prevout,
|
2018-01-04 20:43:30 -08:00
|
|
|
witness: this.witness,
|
|
|
|
|
sequence: this.sequence,
|
2017-11-15 18:54:47 -08:00
|
|
|
coin: coin || null
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-02-23 02:27:10 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Convert the input to an object suitable
|
|
|
|
|
* for JSON serialization.
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
*/
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
toJSON(network, coin) {
|
|
|
|
|
return this.getJSON();
|
|
|
|
|
}
|
2016-02-23 02:27:10 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Convert the input to an object suitable
|
|
|
|
|
* for JSON serialization. Note that the hashes
|
|
|
|
|
* will be reversed to abide by bitcoind's legacy
|
|
|
|
|
* of little-endian uint256s.
|
|
|
|
|
* @param {Network} network
|
|
|
|
|
* @param {Coin} coin
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
getJSON(network, coin) {
|
|
|
|
|
network = Network.get(network);
|
|
|
|
|
|
|
|
|
|
let addr;
|
|
|
|
|
if (!coin) {
|
|
|
|
|
addr = this.getAddress();
|
|
|
|
|
if (addr)
|
|
|
|
|
addr = addr.toString(network);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
prevout: this.prevout.toJSON(),
|
|
|
|
|
witness: this.witness.toJSON(),
|
|
|
|
|
sequence: this.sequence,
|
|
|
|
|
address: addr,
|
|
|
|
|
coin: coin ? coin.getJSON(network, true) : undefined
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-12-11 12:19:18 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Inject properties from a JSON object.
|
|
|
|
|
* @private
|
|
|
|
|
* @param {Object} json
|
|
|
|
|
*/
|
2016-12-11 12:19:18 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
fromJSON(json) {
|
|
|
|
|
assert(json, 'Input data is required.');
|
|
|
|
|
assert((json.sequence >>> 0) === json.sequence,
|
|
|
|
|
'Sequence must be a uint32.');
|
|
|
|
|
this.prevout.fromJSON(json.prevout);
|
|
|
|
|
this.witness.fromJSON(json.witness);
|
|
|
|
|
this.sequence = json.sequence;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Instantiate an Input from a jsonified input object.
|
|
|
|
|
* @param {Object} json - The jsonified input object.
|
|
|
|
|
* @returns {Input}
|
|
|
|
|
*/
|
2016-06-17 01:34:59 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
static fromJSON(json) {
|
|
|
|
|
return new this().fromJSON(json);
|
|
|
|
|
}
|
2016-12-03 18:02:10 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Calculate size of serialized input.
|
|
|
|
|
* @returns {Number}
|
|
|
|
|
*/
|
2016-02-23 02:27:10 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
getSize() {
|
2018-01-06 10:29:29 -08:00
|
|
|
return 40 + this.witness.getVarSize();
|
2017-11-15 18:54:47 -08:00
|
|
|
}
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Serialize the input.
|
|
|
|
|
* @param {String?} enc - Encoding, can be `'hex'` or null.
|
|
|
|
|
* @returns {Buffer|String}
|
|
|
|
|
*/
|
2016-02-23 02:27:10 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
toRaw() {
|
|
|
|
|
const size = this.getSize();
|
2017-11-16 11:43:24 -08:00
|
|
|
return this.toWriter(bio.write(size)).render();
|
2017-11-15 18:54:47 -08:00
|
|
|
}
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Write the input to a buffer writer.
|
|
|
|
|
* @param {BufferWriter} bw
|
|
|
|
|
*/
|
2016-03-06 12:08:33 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
toWriter(bw) {
|
|
|
|
|
this.prevout.toWriter(bw);
|
2018-01-02 20:24:56 -08:00
|
|
|
this.witness.toWriter(bw);
|
2017-11-15 18:54:47 -08:00
|
|
|
bw.writeU32(this.sequence);
|
|
|
|
|
return bw;
|
|
|
|
|
}
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Inject properties from buffer reader.
|
|
|
|
|
* @private
|
|
|
|
|
* @param {BufferReader} br
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
fromReader(br) {
|
|
|
|
|
this.prevout.fromReader(br);
|
2018-01-02 20:24:56 -08:00
|
|
|
this.witness.fromReader(br);
|
2017-11-15 18:54:47 -08:00
|
|
|
this.sequence = br.readU32();
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2016-03-06 12:08:33 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Inject properties from serialized data.
|
|
|
|
|
* @param {Buffer} data
|
|
|
|
|
*/
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
fromRaw(data) {
|
2017-11-16 11:43:24 -08:00
|
|
|
return this.fromReader(bio.read(data));
|
2017-11-15 18:54:47 -08:00
|
|
|
}
|
2016-03-06 12:08:33 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Instantiate an input from a buffer reader.
|
|
|
|
|
* @param {BufferReader} br
|
|
|
|
|
* @returns {Input}
|
|
|
|
|
*/
|
2017-01-08 01:35:48 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
static fromReader(br) {
|
|
|
|
|
return new this().fromReader(br);
|
|
|
|
|
}
|
2017-01-08 01:35:48 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Instantiate an input from a serialized Buffer.
|
|
|
|
|
* @param {Buffer} data
|
|
|
|
|
* @param {String?} enc - Encoding, can be `'hex'` or null.
|
|
|
|
|
* @returns {Input}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static fromRaw(data, enc) {
|
|
|
|
|
if (typeof data === 'string')
|
|
|
|
|
data = Buffer.from(data, enc);
|
|
|
|
|
return new this().fromRaw(data);
|
|
|
|
|
}
|
2017-01-08 01:35:48 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Inject properties from outpoint.
|
|
|
|
|
* @private
|
|
|
|
|
* @param {Outpoint} outpoint
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
fromOutpoint(outpoint) {
|
|
|
|
|
assert(typeof outpoint.hash === 'string');
|
|
|
|
|
assert(typeof outpoint.index === 'number');
|
|
|
|
|
this.prevout.hash = outpoint.hash;
|
|
|
|
|
this.prevout.index = outpoint.index;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2017-01-08 01:35:48 -08:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Instantiate input from outpoint.
|
|
|
|
|
* @param {Outpoint}
|
|
|
|
|
* @returns {Input}
|
|
|
|
|
*/
|
2016-06-27 16:51:42 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
static fromOutpoint(outpoint) {
|
|
|
|
|
return new this().fromOutpoint(outpoint);
|
|
|
|
|
}
|
2016-06-27 16:51:42 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Inject properties from coin.
|
|
|
|
|
* @private
|
|
|
|
|
* @param {Coin} coin
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
fromCoin(coin) {
|
|
|
|
|
assert(typeof coin.hash === 'string');
|
|
|
|
|
assert(typeof coin.index === 'number');
|
|
|
|
|
this.prevout.hash = coin.hash;
|
|
|
|
|
this.prevout.index = coin.index;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2016-06-27 16:51:42 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Instantiate input from coin.
|
|
|
|
|
* @param {Coin}
|
|
|
|
|
* @returns {Input}
|
|
|
|
|
*/
|
2016-06-27 16:51:42 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
static fromCoin(coin) {
|
|
|
|
|
return new this().fromCoin(coin);
|
|
|
|
|
}
|
2016-06-27 16:51:42 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Inject properties from transaction.
|
|
|
|
|
* @private
|
|
|
|
|
* @param {TX} tx
|
|
|
|
|
* @param {Number} index
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
fromTX(tx, index) {
|
|
|
|
|
assert(tx);
|
|
|
|
|
assert(typeof index === 'number');
|
|
|
|
|
assert(index >= 0 && index < tx.outputs.length);
|
|
|
|
|
this.prevout.hash = tx.hash('hex');
|
|
|
|
|
this.prevout.index = index;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2016-06-27 16:51:42 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Instantiate input from tx.
|
|
|
|
|
* @param {TX} tx
|
|
|
|
|
* @param {Number} index
|
|
|
|
|
* @returns {Input}
|
|
|
|
|
*/
|
2016-06-27 16:51:42 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
static fromTX(tx, index) {
|
|
|
|
|
return new this().fromTX(tx, index);
|
|
|
|
|
}
|
2016-06-27 16:51:42 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
/**
|
|
|
|
|
* Test an object to see if it is an Input.
|
|
|
|
|
* @param {Object} obj
|
|
|
|
|
* @returns {Boolean}
|
|
|
|
|
*/
|
2016-04-15 05:28:23 -07:00
|
|
|
|
2017-11-15 18:54:47 -08:00
|
|
|
static isInput(obj) {
|
|
|
|
|
return obj instanceof Input;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-24 13:28:24 -07:00
|
|
|
|
2016-05-15 18:07:06 -07:00
|
|
|
/*
|
|
|
|
|
* Expose
|
|
|
|
|
*/
|
|
|
|
|
|
2016-08-24 04:08:40 -07:00
|
|
|
module.exports = Input;
|