refactor sighash.

This commit is contained in:
Christopher Jeffrey 2016-06-30 08:17:04 -07:00
parent c7b9091da0
commit e364e2b619
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 15 additions and 24 deletions

View File

@ -61,8 +61,8 @@ Coin.prototype.fromOptions = function fromOptions(options) {
assert(utils.isNumber(options.height));
assert(utils.isNumber(options.value));
assert(typeof options.coinbase === 'boolean');
assert(!options.hash || typeof options.hash === 'string');
assert(!options.index || utils.isNumber(options.index));
assert(options.hash == null || typeof options.hash === 'string');
assert(options.index == null || utils.isNumber(options.index));
this.version = options.version;
this.height = options.height;

View File

@ -460,6 +460,15 @@ TX.prototype.hasWitness = function hasWitness() {
*/
TX.prototype.signatureHash = function signatureHash(index, prev, type, version) {
if (typeof index !== 'number')
index = this.inputs.indexOf(index);
if (typeof type === 'string')
type = constants.hashType[type.toUpperCase()];
assert(index >= 0 && index < this.inputs.length);
assert(prev instanceof Script);
// Traditional sighashing
if (version === 0)
return this.signatureHashV0(index, prev, type);
@ -483,19 +492,7 @@ TX.prototype.signatureHash = function signatureHash(index, prev, type, version)
TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
var i, input, output;
var p = new BufferWriter();
var hashType;
if (typeof index !== 'number')
index = this.inputs.indexOf(index);
if (typeof type === 'string')
type = constants.hashType[type.toUpperCase()];
assert(index >= 0 && index < this.inputs.length);
assert(prev instanceof Script);
// Get the unmasked hash type.
hashType = type & 0x1f;
var hashType = type & 0x1f;
if (hashType === constants.hashType.SINGLE) {
// Bitcoind used to return 1 as an error code:
@ -610,15 +607,6 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) {
var p = new BufferWriter();
var i, hashPrevouts, hashSequence, hashOutputs;
if (typeof index !== 'number')
index = this.inputs.indexOf(index);
if (typeof type === 'string')
type = constants.hashType[type.toUpperCase()];
assert(index >= 0 && index < this.inputs.length);
assert(prev instanceof Script);
if (!(type & constants.hashType.ANYONECANPAY)) {
if (this._hashPrevouts) {
hashPrevouts = this._hashPrevouts;
@ -2206,6 +2194,9 @@ TX.prototype.frameWitness = function frameWitness(writer) {
p.writeU32(this.locktime);
if (witnessSize === this.inputs.length)
throw new Error('Cannot serialize empty-witness tx.');
this._lastWitnessSize = witnessSize + 2;
if (!writer)