verify pubkey.

This commit is contained in:
Christopher Jeffrey 2016-03-31 03:51:53 -07:00
parent fb370eda1c
commit fec9f2e308
2 changed files with 14 additions and 20 deletions

View File

@ -56,7 +56,7 @@ ec.publicKeyCreate = function publicKeyCreate(priv, compressed) {
ec.random = function random(size) { ec.random = function random(size) {
if (crypto) if (crypto)
return crypto.randomBytes(size); return crypto.randomBytes(size);
return new Buffer(elliptic.rand(size)); return new Buffer(ec.elliptic.rand(size));
}; };
ec.verify = function verify(msg, sig, key, historical) { ec.verify = function verify(msg, sig, key, historical) {
@ -86,26 +86,22 @@ ec.verify = function verify(msg, sig, key, historical) {
// Import from DER. // Import from DER.
sig = secp256k1.signatureImport(sig); sig = secp256k1.signatureImport(sig);
// This is supposed to lower the S value
// but it doesn't seem to work.
// if (historical)
// sig = bcoin.secp256k1.signatureNormalize(sig);
return secp256k1.verify(msg, sig, key); return secp256k1.verify(msg, sig, key);
} }
return ec.elliptic.verify(msg, sig, key); return ec.elliptic.verify(msg, sig, key);
} catch (e) { } catch (e) {
utils.debug('Elliptic threw during verification:'); // if (!ec.publicKeyVerify(key))
utils.debug(e.stack + ''); // utils.debug('Public key is invalid.');
utils.debug({
msg: utils.toHex(msg),
sig: utils.toHex(sig),
key: utils.toHex(key)
});
return false; return false;
} }
}; };
ec.publicKeyVerify = function publicKeyVerify(key) {
if (secp256k1)
return secp256k1.publicKeyVerify(key);
return ec.elliptic.keyPair({ pub: key }).validate();
};
ec.sign = function sign(msg, key) { ec.sign = function sign(msg, key) {
var sig; var sig;

View File

@ -135,7 +135,7 @@ KeyPair.prototype.toJSON = function toJSON(passphrase) {
encrypted: passphrase ? true : false encrypted: passphrase ? true : false
}; };
if (this.key.priv) { if (this.key.privateKey) {
json.privateKey = passphrase json.privateKey = passphrase
? utils.encrypt(this.toSecret(), passphrase) ? utils.encrypt(this.toSecret(), passphrase)
: this.toSecret(); : this.toSecret();
@ -148,13 +148,13 @@ KeyPair.prototype.toJSON = function toJSON(passphrase) {
}; };
KeyPair._fromJSON = function _fromJSON(json, passphrase) { KeyPair._fromJSON = function _fromJSON(json, passphrase) {
var privateKey, publicKey, compressed; var privateKey;
assert.equal(json.v, 1); assert.equal(json.v, 1);
assert.equal(json.name, 'keypair'); assert.equal(json.name, 'keypair');
if (json.encrypted && !passphrase) if (json.encrypted && !passphrase)
throw new Error('Cannot decrypt address'); throw new Error('Cannot decrypt key.');
if (json.privateKey) { if (json.privateKey) {
privateKey = json.privateKey; privateKey = json.privateKey;
@ -164,11 +164,9 @@ KeyPair._fromJSON = function _fromJSON(json, passphrase) {
} }
if (json.publicKey) { if (json.publicKey) {
publicKey = utils.fromBase58(json.publicKey);
compressed = publicKey[0] !== 0x04;
return { return {
publicKey: publicKey, publicKey: utils.fromBase58(json.publicKey),
compressed: compressed compressed: publicKey[0] !== 0x04
}; };
} }