fix some more tests

This commit is contained in:
Manuel Araoz 2014-12-15 19:50:34 -03:00
parent a761ac91c2
commit 0ddec23c02
5 changed files with 48 additions and 8 deletions

View File

@ -51,8 +51,8 @@ Signature.fromCompact = function(buf) {
return sig;
};
Signature.fromDER = function(buf) {
var obj = Signature.parseDER(buf);
Signature.fromDER = function(buf, strict) {
var obj = Signature.parseDER(buf, strict);
var sig = new Signature();
sig.r = obj.r;
@ -75,7 +75,16 @@ Signature.fromString = function(str) {
return Signature.fromDER(buf);
};
Signature.parseDER = function(buf) {
/**
* In order to mimic the non-strict DER encoding of OpenSSL, set strict = false.
*/
Signature.parseDER = function(buf, strict) {
if (typeof strict === 'undefined') {
strict = true;
}
console.log('strict: '+strict);
if (!Buffer.isBuffer(buf))
throw new Error('DER formatted signature should be a buffer');
@ -85,8 +94,12 @@ Signature.parseDER = function(buf) {
throw new Error('Header byte should be 0x30');
var length = buf[1];
if (length !== buf.slice(2).length)
var buflength = buf.slice(2).length;
if (strict && length !== buflength) {
throw new Error('Length byte should length of what follows');
} else {
length = length < buflength ? length : buflength;
}
var rheader = buf[2 + 0];
if (rheader !== 0x02)
@ -132,6 +145,7 @@ Signature.parseDER = function(buf) {
return obj;
};
Signature.prototype.toCompact = function(i, compressed) {
i = typeof i === 'number' ? i : this.i;
compressed = typeof compressed === 'boolean' ? compressed : this.compressed;

View File

@ -6,6 +6,7 @@ var BN = require('./crypto/bn');
var Point = require('./crypto/point');
var JSUtil = require('./util/js');
var Network = require('./networks');
var _ = require('lodash');
/**
* Instantiate a PublicKey from a 'PrivateKey', 'Point', 'string', 'Buffer'.
@ -137,21 +138,24 @@ PublicKey._transformPrivateKey = function(privkey) {
* Internal function to transform DER into a public key point
*
* @param {Buffer} buf - An hex encoded buffer
* @param {bool} [strict] - if set to false, will loosen some conditions
* @returns {Object} An object with keys: point and compressed
* @private
*/
PublicKey._transformDER = function(buf) {
PublicKey._transformDER = function(buf, strict) {
var info = {};
if (!PublicKey._isBuffer(buf)) {
throw new TypeError('Must be a hex buffer of DER encoded public key');
}
strict = _.isUndefined(strict) ? true : strict;
var x;
var y;
var xbuf;
var ybuf;
if (buf[0] === 0x04) {
if (buf[0] === 0x04 || (!strict && (buf[0] === 0x06 || buf[0] === 0x07))) {
xbuf = buf.slice(1, 33);
ybuf = buf.slice(33, 65);
if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65) {
@ -166,7 +170,7 @@ PublicKey._transformDER = function(buf) {
x = BN(xbuf);
info = PublicKey._transformX(true, x);
info.compressed = true;
} else if (buf[0] == 0x02) {
} else if (buf[0] === 0x02) {
xbuf = buf.slice(1);
x = BN(xbuf);
info = PublicKey._transformX(false, x);
@ -276,6 +280,18 @@ PublicKey.fromPoint = function(point, compressed) {
});
};
/**
* Instantiate a PublicKey from a DER Buffer
*
* @param {Buffer} buf - A DER Buffer
* @param {bool} [strict] - if set to false, will loosen some conditions
* @returns {PublicKey} A new valid instance of PublicKey
*/
PublicKey.fromDER = function(buf, strict) {
var info = PublicKey._transformDER(buf, strict);
return new PublicKey(info.point, info.compressed);
};
/**
* Instantiate a PublicKey from a DER hex encoded string
*

View File

@ -888,6 +888,7 @@ ScriptInterpreter.prototype.step = function() {
}
var fSuccess;
var sig = Signature.fromTxFormat(bufSig);
try {
var sig = Signature.fromTxFormat(bufSig);
var pubkey = PublicKey.fromBuffer(bufPubkey, false);
@ -897,7 +898,6 @@ ScriptInterpreter.prototype.step = function() {
console.log('FALSEEEEEEEEEEEEEEEEee ' + e);
fSuccess = false;
}
this.tx.verify(sig, pubkey, this.nin, subscript);
this.stack.pop();
this.stack.pop();

View File

@ -10,6 +10,7 @@ var BufferWriter = require('../encoding/bufferwriter');
var BN = require('../crypto/bn');
var Hash = require('../crypto/hash');
var ECDSA = require('../crypto/ecdsa');
var $ = require('../util/preconditions');
var SIGHASH_SINGLE_BUG = '0000000000000000000000000000000000000000000000000000000000000001';
var BITS_64_ON = 'ffffffffffffffff';
@ -99,6 +100,8 @@ function sign(transaction, keypair, nhashtype, nin, subscript) {
}
function verify(transaction, sig, pubkey, nin, subscript) {
$.checkArgument(transaction);
$.checkArgument(sig && sig.nhashtype);
var hashbuf = sighash(transaction, sig.nhashtype, nin, subscript);
console.log('actual:');
console.log(hashbuf.toString('hex'));

View File

@ -149,6 +149,13 @@ describe('Signature', function() {
parsed.s.toString().should.equal('44212963026209759051804639008236126356702363229859210154760104982946304432721');
});
it('should parse this signature from script_valid.json', function() {
var sighex = '304502203e4516da7253cf068effec6b95c41221c0cf3a8e6ccb8cbf1725b562e9afde2c022100ab1e3da73d67e32045a20e0b999e049978ea8d6ee5480d485fcf2ce0d03b2ef051';
var sig = Buffer(sighex, 'hex');
var parsed = Signature.parseDER(sig, false);
should.exist(parsed);
});
});
describe('#toDER', function() {