pass some more tests

This commit is contained in:
Manuel Araoz 2014-12-15 11:40:49 -03:00
parent 401b5b1b4a
commit 6a0512e1bf
2 changed files with 35 additions and 18 deletions

View File

@ -52,8 +52,6 @@ var PublicKey = function PublicKey(data, extra) {
info.point = data; info.point = data;
} else if (PublicKey._isJSON(data)) { } else if (PublicKey._isJSON(data)) {
info = PublicKey._transformJSON(data); info = PublicKey._transformJSON(data);
} else if (typeof(data) === 'string') {
info = PublicKey._transformDER(new Buffer(data, 'hex'));
} else if (PublicKey._isBuffer(data)) { } else if (PublicKey._isBuffer(data)) {
info = PublicKey._transformDER(data); info = PublicKey._transformDER(data);
} else if (PublicKey._isPrivateKey(data)) { } else if (PublicKey._isPrivateKey(data)) {
@ -92,8 +90,7 @@ var PublicKey = function PublicKey(data, extra) {
* @private * @private
*/ */
PublicKey._isPrivateKey = function(param) { PublicKey._isPrivateKey = function(param) {
return param && param.constructor && param.constructor.name return param && param.constructor && param.constructor.name && param.constructor.name === 'PrivateKey';
&& param.constructor.name === 'PrivateKey';
}; };
/** /**
@ -143,7 +140,7 @@ PublicKey._transformPrivateKey = function(privkey) {
* @returns {Object} An object with keys: point and compressed * @returns {Object} An object with keys: point and compressed
* @private * @private
*/ */
PublicKey._transformDER = function(buf){ PublicKey._transformDER = function(buf) {
var info = {}; var info = {};
if (!PublicKey._isBuffer(buf)) { if (!PublicKey._isBuffer(buf)) {
throw new TypeError('Must be a hex buffer of DER encoded public key'); throw new TypeError('Must be a hex buffer of DER encoded public key');
@ -188,7 +185,7 @@ PublicKey._transformDER = function(buf){
* @returns {Object} An object with keys: point and compressed * @returns {Object} An object with keys: point and compressed
* @private * @private
*/ */
PublicKey._transformX = function(odd, x){ PublicKey._transformX = function(odd, x) {
var info = {}; var info = {};
if (typeof odd !== 'boolean') { if (typeof odd !== 'boolean') {
throw new TypeError('Must specify whether y is odd or not (true or false)'); throw new TypeError('Must specify whether y is odd or not (true or false)');
@ -225,7 +222,9 @@ PublicKey._transformJSON = function(json) {
var x = BN(json.x, 'hex'); var x = BN(json.x, 'hex');
var y = BN(json.y, 'hex'); var y = BN(json.y, 'hex');
var point = new Point(x, y); var point = new Point(x, y);
return new PublicKey(point, {compressed: json.compressed}); return new PublicKey(point, {
compressed: json.compressed
});
}; };
/** /**
@ -239,7 +238,10 @@ PublicKey.fromPrivateKey = function(privkey) {
throw new TypeError('Must be an instance of PrivateKey'); throw new TypeError('Must be an instance of PrivateKey');
} }
var info = PublicKey._transformPrivateKey(privkey); var info = PublicKey._transformPrivateKey(privkey);
return new PublicKey(info.point, {compressed: info.compressed, network: info.network}); return new PublicKey(info.point, {
compressed: info.compressed,
network: info.network
});
}; };
/** /**
@ -253,7 +255,9 @@ PublicKey.fromDER = PublicKey.fromBuffer = function(buf) {
throw new TypeError('Must be a hex buffer of DER encoded public key'); throw new TypeError('Must be a hex buffer of DER encoded public key');
} }
var info = PublicKey._transformDER(buf); var info = PublicKey._transformDER(buf);
return new PublicKey(info.point, {compressed: info.compressed}); return new PublicKey(info.point, {
compressed: info.compressed
});
}; };
/** /**
@ -263,11 +267,13 @@ PublicKey.fromDER = PublicKey.fromBuffer = function(buf) {
* @param {boolean=true} compressed - whether to store this public key as compressed format * @param {boolean=true} compressed - whether to store this public key as compressed format
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromPoint = function(point, compressed){ PublicKey.fromPoint = function(point, compressed) {
if (!(point instanceof Point)) { if (!(point instanceof Point)) {
throw new TypeError('First argument must be an instance of Point.'); throw new TypeError('First argument must be an instance of Point.');
} }
return new PublicKey(point, {compressed: compressed}); return new PublicKey(point, {
compressed: compressed
});
}; };
/** /**
@ -280,7 +286,9 @@ PublicKey.fromPoint = function(point, compressed){
PublicKey.fromString = function(str, encoding) { PublicKey.fromString = function(str, encoding) {
var buf = new Buffer(str, encoding || 'hex'); var buf = new Buffer(str, encoding || 'hex');
var info = PublicKey._transformDER(buf); var info = PublicKey._transformDER(buf);
return new PublicKey(info.point, {compressed: info.compressed}); return new PublicKey(info.point, {
compressed: info.compressed
});
}; };
/** /**
@ -292,7 +300,9 @@ PublicKey.fromString = function(str, encoding) {
*/ */
PublicKey.fromX = function(odd, x) { PublicKey.fromX = function(odd, x) {
var info = PublicKey._transformX(odd, x); var info = PublicKey._transformX(odd, x);
return new PublicKey(info.point, {compressed: info.compressed}); return new PublicKey(info.point, {
compressed: info.compressed
});
}; };
/** /**
@ -334,7 +344,7 @@ PublicKey.prototype.toObject = function toObject() {
}; };
}; };
PublicKey.prototype.toJSON = function toJSON(){ PublicKey.prototype.toJSON = function toJSON() {
return JSON.stringify(this.toObject()); return JSON.stringify(this.toObject());
}; };
@ -347,8 +357,12 @@ PublicKey.prototype.toBuffer = PublicKey.prototype.toDER = function() {
var x = this.point.getX(); var x = this.point.getX();
var y = this.point.getY(); var y = this.point.getY();
var xbuf = x.toBuffer({size: 32}); var xbuf = x.toBuffer({
var ybuf = y.toBuffer({size: 32}); size: 32
});
var ybuf = y.toBuffer({
size: 32
});
var prefix; var prefix;
if (!this.compressed) { if (!this.compressed) {
@ -395,4 +409,5 @@ PublicKey.prototype.inspect = function() {
(this.network ? this.network.name : '') + '>'; (this.network ? this.network.name : '') + '>';
}; };
module.exports = PublicKey; module.exports = PublicKey;

View File

@ -149,7 +149,7 @@ ScriptInterpreter.prototype.checkSignatureEncoding = function(buf) {
* Translated from bitcoind's CheckPubKeyEncoding * Translated from bitcoind's CheckPubKeyEncoding
*/ */
ScriptInterpreter.prototype.checkPubkeyEncoding = function(buf) { ScriptInterpreter.prototype.checkPubkeyEncoding = function(buf) {
if ((this.flags & ScriptInterpreter.SCRIPT_VERIFY_STRICTENC) !== 0 && !PublicKey.isCompressedOrUncompressed(buf)) { if ((this.flags & ScriptInterpreter.SCRIPT_VERIFY_STRICTENC) !== 0 && !PublicKey.isValid(buf)) {
this.errstr = 'SCRIPT_ERR_PUBKEYTYPE'; this.errstr = 'SCRIPT_ERR_PUBKEYTYPE';
return false; return false;
} }
@ -879,7 +879,9 @@ ScriptInterpreter.prototype.step = function() {
}); });
// Drop the signature, since there's no way for a signature to sign itself // Drop the signature, since there's no way for a signature to sign itself
subscript.findAndDelete(Script().writeBuffer(bufSig)); console.log(subscript.toString());
subscript.findAndDelete(Script().add(bufSig));
console.log(subscript.toString());
if (!this.checkSignatureEncoding(bufSig) || !this.checkPubkeyEncoding(bufPubkey)) { if (!this.checkSignatureEncoding(bufSig) || !this.checkPubkeyEncoding(bufPubkey)) {
// serror is set // serror is set