Merge pull request #89 from Dcousens/eckeyfnc
Adds ECPubKey.verify and tests for ECKey signing
This commit is contained in:
commit
d16ea7f2a6
2 changed files with 46 additions and 15 deletions
31
src/eckey.js
31
src/eckey.js
|
@ -11,8 +11,8 @@ var Network = require('./network')
|
||||||
var ecparams = sec("secp256k1");
|
var ecparams = sec("secp256k1");
|
||||||
|
|
||||||
// input can be nothing, array of bytes, hex string, or base58 string
|
// input can be nothing, array of bytes, hex string, or base58 string
|
||||||
var ECKey = function (input,compressed) {
|
var ECKey = function (input, compressed) {
|
||||||
if (!(this instanceof ECKey)) { return new ECKey(input,compressed); }
|
if (!(this instanceof ECKey)) { return new ECKey(input, compressed); }
|
||||||
if (!input) {
|
if (!input) {
|
||||||
// Generate new key
|
// Generate new key
|
||||||
var n = ecparams.getN();
|
var n = ecparams.getN();
|
||||||
|
@ -110,8 +110,16 @@ ECKey.prototype.multiply = function(key) {
|
||||||
return ECKey(this.priv.multiply(ECKey(key).priv),this.compressed)
|
return ECKey(this.priv.multiply(ECKey(key).priv),this.compressed)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ECPubKey = function(input,compressed) {
|
ECKey.prototype.sign = function(hash) {
|
||||||
if (!(this instanceof ECPubKey)) { return new ECPubKey(input,compressed); }
|
return ecdsa.sign(hash, this.priv);
|
||||||
|
}
|
||||||
|
|
||||||
|
ECKey.prototype.verify = function(hash, sig) {
|
||||||
|
return this.getPub().verify(hash, sig)
|
||||||
|
}
|
||||||
|
|
||||||
|
var ECPubKey = function(input, compressed) {
|
||||||
|
if (!(this instanceof ECPubKey)) { return new ECPubKey(input, compressed); }
|
||||||
if (!input) {
|
if (!input) {
|
||||||
// Generate new key
|
// Generate new key
|
||||||
var n = ecparams.getN();
|
var n = ecparams.getN();
|
||||||
|
@ -178,18 +186,11 @@ ECPubKey.prototype.getAddress = function(version) {
|
||||||
return new Address(util.sha256ripe160(this.toBytes()), version);
|
return new Address(util.sha256ripe160(this.toBytes()), version);
|
||||||
}
|
}
|
||||||
|
|
||||||
ECKey.prototype.sign = function (hash) {
|
ECPubKey.prototype.verify = function(hash, sig) {
|
||||||
return ecdsa.sign(hash, this.priv);
|
return ecdsa.verify(hash, sig, this.toBytes())
|
||||||
};
|
}
|
||||||
|
|
||||||
ECKey.prototype.verify = function (hash, sig) {
|
|
||||||
return ecdsa.verify(hash, sig, this.getPub()['export']('bytes'));
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse an exported private key contained in a string.
|
|
||||||
*/
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
ECKey: ECKey,
|
ECKey: ECKey,
|
||||||
ECPubKey: ECPubKey
|
ECPubKey: ECPubKey
|
||||||
};
|
}
|
||||||
|
|
|
@ -138,4 +138,34 @@ describe('ECKey', function() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('signing', function() {
|
||||||
|
var hpriv = 'ca48ec9783cf3ad0dfeff1fc254395a2e403cbbc666477b61b45e31d3b8ab458'
|
||||||
|
var hcpub = '024b12d9d7c77db68388b6ff7c89046174c871546436806bcd80d07c28ea811992'
|
||||||
|
var message = 'Vires in numeris'
|
||||||
|
|
||||||
|
it('should verify against the private key', function() {
|
||||||
|
var priv = new ECKey(hpriv)
|
||||||
|
var signature = priv.sign(message)
|
||||||
|
|
||||||
|
assert(priv.verify(message, signature))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should verify against the public key', function() {
|
||||||
|
var priv = new ECKey(hpriv)
|
||||||
|
var pub = new ECPubKey(hcpub, true)
|
||||||
|
var signature = priv.sign(message)
|
||||||
|
|
||||||
|
assert(pub.verify(message, signature))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not verify against the wrong private key', function() {
|
||||||
|
var priv1 = new ECKey(hpriv)
|
||||||
|
var priv2 = new ECKey('1111111111111111111111111111111111111111111111111111111111111111')
|
||||||
|
|
||||||
|
var signature = priv1.sign(message)
|
||||||
|
|
||||||
|
assert(!priv2.verify(message, signature))
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue