Merge pull request #1284 from mruddy/fix/signature_has_defined_hashtype
fix signature hasDefinedHashtype
This commit is contained in:
commit
e99ccb8223
|
@ -4,6 +4,7 @@ var BN = require('./bn');
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var $ = require('../util/preconditions');
|
var $ = require('../util/preconditions');
|
||||||
var BufferUtil = require('../util/buffer');
|
var BufferUtil = require('../util/buffer');
|
||||||
|
var JSUtil = require('../util/js');
|
||||||
|
|
||||||
var Signature = function Signature(r, s) {
|
var Signature = function Signature(r, s) {
|
||||||
if (!(this instanceof Signature)) {
|
if (!(this instanceof Signature)) {
|
||||||
|
@ -285,7 +286,12 @@ Signature.prototype.hasLowS = function() {
|
||||||
* Translated from bitcoind's IsDefinedHashtypeSignature
|
* Translated from bitcoind's IsDefinedHashtypeSignature
|
||||||
*/
|
*/
|
||||||
Signature.prototype.hasDefinedHashtype = function() {
|
Signature.prototype.hasDefinedHashtype = function() {
|
||||||
if (this.nhashtype < Signature.SIGHASH_ALL || this.nhashtype > Signature.SIGHASH_SINGLE) {
|
if (!JSUtil.isNaturalNumber(this.nhashtype)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// accept with or without Signature.SIGHASH_ANYONECANPAY by ignoring the bit
|
||||||
|
var temp = this.nhashtype & ~Signature.SIGHASH_ANYONECANPAY;
|
||||||
|
if (temp < Signature.SIGHASH_ALL || temp > Signature.SIGHASH_SINGLE) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var _ = require('lodash');
|
||||||
var should = require('chai').should();
|
var should = require('chai').should();
|
||||||
var bitcore = require('../..');
|
var bitcore = require('../..');
|
||||||
var BN = bitcore.crypto.BN;
|
var BN = bitcore.crypto.BN;
|
||||||
|
@ -293,4 +294,35 @@ describe('Signature', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#hasDefinedHashtype', function() {
|
||||||
|
it('should reject invalid sighash types and accept valid ones', function() {
|
||||||
|
var sig = new Signature();
|
||||||
|
sig.hasDefinedHashtype().should.equal(false);
|
||||||
|
var testCases = [
|
||||||
|
[undefined, false],
|
||||||
|
[null, false],
|
||||||
|
[0, false],
|
||||||
|
[1.1, false],
|
||||||
|
[-1, false],
|
||||||
|
[-1.1, false],
|
||||||
|
['', false],
|
||||||
|
['1', false],
|
||||||
|
[Signature.SIGHASH_ANYONECANPAY, false],
|
||||||
|
[Signature.SIGHASH_ANYONECANPAY | Signature.SIGHASH_ALL, true],
|
||||||
|
[Signature.SIGHASH_ANYONECANPAY | Signature.SIGHASH_NONE, true],
|
||||||
|
[Signature.SIGHASH_ANYONECANPAY | Signature.SIGHASH_SINGLE, true],
|
||||||
|
[Signature.SIGHASH_ALL, true],
|
||||||
|
[Signature.SIGHASH_NONE, true],
|
||||||
|
[Signature.SIGHASH_SINGLE, true],
|
||||||
|
[Signature.SIGHASH_SINGLE + 1, false],
|
||||||
|
[(Signature.SIGHASH_ANYONECANPAY | Signature.SIGHASH_SINGLE) + 1, false],
|
||||||
|
[(Signature.SIGHASH_ANYONECANPAY | Signature.SIGHASH_ALL) - 1, false],
|
||||||
|
];
|
||||||
|
_.each(testCases, function(testCase) {
|
||||||
|
sig.nhashtype = testCase[0];
|
||||||
|
sig.hasDefinedHashtype().should.equal(testCase[1]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue