ec: recover.
This commit is contained in:
parent
bf63691fd2
commit
666ff94ddf
@ -48,7 +48,7 @@ config.parse = function parse(options) {
|
||||
var arg = {};
|
||||
var text = {};
|
||||
var data = {};
|
||||
var prefix, cfg;
|
||||
var prefix;
|
||||
|
||||
if (!options)
|
||||
options = {};
|
||||
@ -190,7 +190,7 @@ config.parseFile = function parseFile(file) {
|
||||
|
||||
config.parseText = function parseText(text) {
|
||||
var data = {};
|
||||
var i, parts, line, pair, key, value, eq, col, alias;
|
||||
var i, parts, line, key, value, eq, col, alias;
|
||||
|
||||
assert(typeof text === 'string', 'Config must be text.');
|
||||
|
||||
@ -243,7 +243,7 @@ config.parseText = function parseText(text) {
|
||||
|
||||
config.parseArg = function parseArg(argv) {
|
||||
var data = {};
|
||||
var arg, key, value, alias;
|
||||
var i, arg, key, value, alias;
|
||||
|
||||
if (!argv)
|
||||
argv = process.argv;
|
||||
|
||||
@ -92,7 +92,9 @@ ec.publicKeyCreate = function publicKeyCreate(priv, compressed) {
|
||||
if (secp256k1)
|
||||
return secp256k1.publicKeyCreate(priv, compressed);
|
||||
|
||||
priv = ec.elliptic.keyPair({ priv: priv }).getPublic(compressed, 'array');
|
||||
priv = ec.elliptic.keyPair({ priv: priv });
|
||||
priv = priv.getPublic(compressed !== false, 'array');
|
||||
|
||||
return new Buffer(priv);
|
||||
};
|
||||
|
||||
@ -110,7 +112,7 @@ ec.publicKeyConvert = function publicKeyConvert(key, compressed) {
|
||||
|
||||
point = ec.elliptic.curve.decodePoint(key);
|
||||
|
||||
return new Buffer(point.encode('array', compressed));
|
||||
return new Buffer(point.encode('array', compressed !== false));
|
||||
};
|
||||
|
||||
/**
|
||||
@ -153,7 +155,7 @@ ec.publicKeyTweakAdd = function publicKeyTweakAdd(publicKey, tweak, compressed)
|
||||
|
||||
point = ec.curve.decodePoint(publicKey);
|
||||
point = ec.curve.g.mul(new bn(tweak)).add(point);
|
||||
key = new Buffer(point.encode('array', compressed));
|
||||
key = new Buffer(point.encode('array', compressed !== false));
|
||||
|
||||
if (!ec.publicKeyVerify(key))
|
||||
throw new Error('Public key is invalid.');
|
||||
@ -178,6 +180,63 @@ ec.ecdh = function ecdh(pub, priv) {
|
||||
return priv.derive(pub.getPublic()).toArrayLike(Buffer, 'be', 32);
|
||||
};
|
||||
|
||||
/**
|
||||
* Recover a public key.
|
||||
* @param {Buffer} msg
|
||||
* @param {Buffer} sig
|
||||
* @param {Number?} j
|
||||
* @param {Boolean?} compressed
|
||||
* @returns {Buffer[]|Buffer|null}
|
||||
*/
|
||||
|
||||
ec.recover = function recover(msg, sig, j, compressed) {
|
||||
var keys = [];
|
||||
var i, point, key;
|
||||
|
||||
if (typeof j === 'boolean') {
|
||||
compressed = j;
|
||||
j = null;
|
||||
}
|
||||
|
||||
if (secp256k1) {
|
||||
try {
|
||||
sig = secp256k1.signatureImport(sig);
|
||||
} catch (e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i <= 3; i++) {
|
||||
if (j != null && j !== i)
|
||||
continue;
|
||||
|
||||
if (secp256k1) {
|
||||
try {
|
||||
key = secp256k1.recover(msg, sig, i, compressed);
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
keys.push(key);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
point = ec.elliptic.recoverPubKey(msg, sig, i);
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
key = ec.elliptic.keyPair({ pub: point });
|
||||
key = key.getPublic(compressed !== false, 'array');
|
||||
keys.push(new Buffer(key));
|
||||
}
|
||||
|
||||
if (j != null)
|
||||
return keys[0];
|
||||
|
||||
return keys;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generate some random bytes.
|
||||
* @param {Number} size
|
||||
|
||||
@ -323,6 +323,7 @@ HTTPServer.prototype._init = function _init() {
|
||||
|
||||
self.rpc.execute(req.body, function(err, json) {
|
||||
if (err) {
|
||||
self.logger.error(err);
|
||||
return send(400, {
|
||||
result: null,
|
||||
error: err.message,
|
||||
@ -1134,23 +1135,6 @@ ClientSocket.prototype.destroy = function() {
|
||||
this.socket.disconnect();
|
||||
};
|
||||
|
||||
/*
|
||||
* Helpers
|
||||
*/
|
||||
|
||||
function textCmp(a, b) {
|
||||
if (!a)
|
||||
return false;
|
||||
|
||||
if (!b)
|
||||
return false;
|
||||
|
||||
a = new Buffer(a, 'utf8');
|
||||
b = new Buffer(b, 'utf8');
|
||||
|
||||
return utils.ccmp(a, b);
|
||||
}
|
||||
|
||||
/*
|
||||
* Expose
|
||||
*/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user