refactor sighash.
This commit is contained in:
parent
c7b9091da0
commit
e364e2b619
@ -61,8 +61,8 @@ Coin.prototype.fromOptions = function fromOptions(options) {
|
|||||||
assert(utils.isNumber(options.height));
|
assert(utils.isNumber(options.height));
|
||||||
assert(utils.isNumber(options.value));
|
assert(utils.isNumber(options.value));
|
||||||
assert(typeof options.coinbase === 'boolean');
|
assert(typeof options.coinbase === 'boolean');
|
||||||
assert(!options.hash || typeof options.hash === 'string');
|
assert(options.hash == null || typeof options.hash === 'string');
|
||||||
assert(!options.index || utils.isNumber(options.index));
|
assert(options.index == null || utils.isNumber(options.index));
|
||||||
|
|
||||||
this.version = options.version;
|
this.version = options.version;
|
||||||
this.height = options.height;
|
this.height = options.height;
|
||||||
|
|||||||
@ -460,6 +460,15 @@ TX.prototype.hasWitness = function hasWitness() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
TX.prototype.signatureHash = function signatureHash(index, prev, type, version) {
|
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
|
// Traditional sighashing
|
||||||
if (version === 0)
|
if (version === 0)
|
||||||
return this.signatureHashV0(index, prev, type);
|
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) {
|
TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
|
||||||
var i, input, output;
|
var i, input, output;
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
var hashType;
|
var hashType = type & 0x1f;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (hashType === constants.hashType.SINGLE) {
|
if (hashType === constants.hashType.SINGLE) {
|
||||||
// Bitcoind used to return 1 as an error code:
|
// 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 p = new BufferWriter();
|
||||||
var i, hashPrevouts, hashSequence, hashOutputs;
|
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 (!(type & constants.hashType.ANYONECANPAY)) {
|
||||||
if (this._hashPrevouts) {
|
if (this._hashPrevouts) {
|
||||||
hashPrevouts = this._hashPrevouts;
|
hashPrevouts = this._hashPrevouts;
|
||||||
@ -2206,6 +2194,9 @@ TX.prototype.frameWitness = function frameWitness(writer) {
|
|||||||
|
|
||||||
p.writeU32(this.locktime);
|
p.writeU32(this.locktime);
|
||||||
|
|
||||||
|
if (witnessSize === this.inputs.length)
|
||||||
|
throw new Error('Cannot serialize empty-witness tx.');
|
||||||
|
|
||||||
this._lastWitnessSize = witnessSize + 2;
|
this._lastWitnessSize = witnessSize + 2;
|
||||||
|
|
||||||
if (!writer)
|
if (!writer)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user