This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
input.js
219 lines (194 loc) · 6.49 KB
/
input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
var _ = require('lodash');
var $ = require('../../util/preconditions');
var errors = require('../../errors');
var BufferWriter = require('../../encoding/bufferwriter');
var buffer = require('buffer');
var BufferUtil = require('../../util/buffer');
var JSUtil = require('../../util/js');
var Script = require('../../script');
var Sighash = require('../sighash');
var Output = require('../output');
var MAXINT = 0xffffffff; // Math.pow(2, 32) - 1;
var DEFAULT_RBF_SEQNUMBER = MAXINT - 2;
var DEFAULT_SEQNUMBER = MAXINT;
var DEFAULT_LOCKTIME_SEQNUMBER = MAXINT - 1;
function Input(params) {
if (!(this instanceof Input)) {
return new Input(params);
}
if (params) {
return this._fromObject(params);
}
}
Input.MAXINT = MAXINT;
Input.DEFAULT_SEQNUMBER = DEFAULT_SEQNUMBER;
Input.DEFAULT_LOCKTIME_SEQNUMBER = DEFAULT_LOCKTIME_SEQNUMBER;
Input.DEFAULT_RBF_SEQNUMBER = DEFAULT_RBF_SEQNUMBER;
Object.defineProperty(Input.prototype, 'script', {
configurable: false,
enumerable: true,
get: function() {
if (this.isNull()) {
return null;
}
if (!this._script) {
this._script = new Script(this._scriptBuffer);
this._script._isInput = true;
}
return this._script;
}
});
Input.fromObject = function(obj) {
$.checkArgument(_.isObject(obj));
var input = new Input();
return input._fromObject(obj);
};
Input.prototype._fromObject = function(params) {
var prevTxId;
if (_.isString(params.prevTxId) && JSUtil.isHexa(params.prevTxId)) {
prevTxId = new buffer.Buffer(params.prevTxId, 'hex');
} else {
prevTxId = params.prevTxId;
}
this.witnesses = [];
this.output = params.output ?
(params.output instanceof Output ? params.output : new Output(params.output)) : undefined;
this.prevTxId = prevTxId || params.txidbuf;
this.outputIndex = _.isUndefined(params.outputIndex) ? params.txoutnum : params.outputIndex;
this.sequenceNumber = _.isUndefined(params.sequenceNumber) ?
(_.isUndefined(params.seqnum) ? DEFAULT_SEQNUMBER : params.seqnum) : params.sequenceNumber;
if (_.isUndefined(params.script) && _.isUndefined(params.scriptBuffer)) {
throw new errors.Transaction.Input.MissingScript();
}
this.setScript(params.scriptBuffer || params.script);
return this;
};
Input.prototype.toObject = Input.prototype.toJSON = function toObject() {
var obj = {
prevTxId: this.prevTxId.toString('hex'),
outputIndex: this.outputIndex,
sequenceNumber: this.sequenceNumber,
script: this._scriptBuffer.toString('hex'),
};
// add human readable form if input contains valid script
if (this.script) {
obj.scriptString = this.script.toString();
}
if (this.output) {
obj.output = this.output.toObject();
}
return obj;
};
Input.fromBufferReader = function(br) {
var input = new Input();
input.prevTxId = br.readReverse(32);
input.outputIndex = br.readUInt32LE();
input._scriptBuffer = br.readVarLengthBuffer();
input.sequenceNumber = br.readUInt32LE();
// TODO: return different classes according to which input it is
// e.g: CoinbaseInput, PublicKeyHashInput, MultiSigScriptHashInput, etc.
return input;
};
Input.prototype.toBufferWriter = function(writer) {
if (!writer) {
writer = new BufferWriter();
}
writer.writeReverse(this.prevTxId);
writer.writeUInt32LE(this.outputIndex);
var script = this._scriptBuffer;
writer.writeVarintNum(script.length);
writer.write(script);
writer.writeUInt32LE(this.sequenceNumber);
return writer;
};
Input.prototype.setScript = function(script) {
this._script = null;
if (script instanceof Script) {
this._script = script;
this._script._isInput = true;
this._scriptBuffer = script.toBuffer();
} else if (JSUtil.isHexa(script)) {
// hex string script
this._scriptBuffer = new buffer.Buffer(script, 'hex');
} else if (_.isString(script)) {
// human readable string script
this._script = new Script(script);
this._script._isInput = true;
this._scriptBuffer = this._script.toBuffer();
} else if (BufferUtil.isBuffer(script)) {
// buffer script
this._scriptBuffer = new buffer.Buffer(script);
} else {
throw new TypeError('Invalid argument type: script');
}
return this;
};
/**
* Retrieve signatures for the provided PrivateKey.
*
* @param {Transaction} transaction - the transaction to be signed
* @param {PrivateKey} privateKey - the private key to use when signing
* @param {number} inputIndex - the index of this input in the provided transaction
* @param {number} sigType - defaults to Signature.SIGHASH_ALL
* @param {Buffer} addressHash - if provided, don't calculate the hash of the
* public key associated with the private key provided
* @abstract
*/
Input.prototype.getSignatures = function() {
throw new errors.AbstractMethodInvoked(
'Trying to sign unsupported output type (only P2PKH and P2SH multisig inputs are supported)' +
' for input: ' + JSON.stringify(this)
);
};
Input.prototype.getSatoshisBuffer = function() {
$.checkState(this.output instanceof Output);
$.checkState(this.output._satoshisBN);
return new BufferWriter().writeUInt64LEBN(this.output._satoshisBN).toBuffer();
};
Input.prototype.isFullySigned = function() {
throw new errors.AbstractMethodInvoked('Input#isFullySigned');
};
Input.prototype.isFinal = function() {
return this.sequenceNumber !== 4294967295;
};
Input.prototype.addSignature = function() {
throw new errors.AbstractMethodInvoked('Input#addSignature');
};
Input.prototype.clearSignatures = function() {
throw new errors.AbstractMethodInvoked('Input#clearSignatures');
};
Input.prototype.hasWitnesses = function() {
if (this.witnesses && this.witnesses.length > 0) {
return true;
}
return false;
};
Input.prototype.getWitnesses = function() {
return this.witnesses;
};
Input.prototype.setWitnesses = function(witnesses) {
this.witnesses = witnesses;
};
Input.prototype.isValidSignature = function(transaction, signature) {
// FIXME: Refactor signature so this is not necessary
signature.signature.nhashtype = signature.sigtype;
return Sighash.verify(
transaction,
signature.signature,
signature.publicKey,
signature.inputIndex,
this.output.script
);
};
/**
* @returns true if this is a coinbase input (represents no input)
*/
Input.prototype.isNull = function() {
return this.prevTxId.toString('hex') === '0000000000000000000000000000000000000000000000000000000000000000' &&
this.outputIndex === 0xffffffff;
};
Input.prototype._estimateSize = function() {
return this.toBufferWriter().toBuffer().length;
};
module.exports = Input;