From 98a091334075bd4365c849de36c9a202eead7789 Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Sun, 18 Jan 2015 23:23:16 +0300 Subject: [PATCH] Update for elliptic 2.0 --- browser.js | 12 +++++------- package.json | 2 +- test.js | 9 +++++++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/browser.js b/browser.js index b84221d..309a80e 100644 --- a/browser.js +++ b/browser.js @@ -67,7 +67,7 @@ var getPublic = exports.getPublic = function(privateKey) { assert(privateKey.length === 32, "Bad private key"); // XXX(Kagami): `elliptic.utils.encode` returns array for every // encoding except `hex`. - return new Buffer(ec.keyPair(privateKey).getPublic("arr")); + return new Buffer(ec.keyFromPrivate(privateKey).getPublic("arr")); }; // NOTE(Kagami): We don't use promise shim in Browser implementation @@ -78,8 +78,7 @@ var getPublic = exports.getPublic = function(privateKey) { exports.sign = function(privateKey, msg) { return new Promise(function(resolve) { assert(privateKey.length === 32, "Bad private key"); - var key = ec.keyPair(privateKey); - resolve(new Buffer(key.sign(msg).toDER())); + resolve(new Buffer(ec.sign(msg, privateKey).toDER())); }); }; @@ -87,8 +86,7 @@ exports.verify = function(publicKey, msg, sig) { return new Promise(function(resolve, reject) { assert(publicKey.length === 65, "Bad public key"); assert(publicKey[0] === 4, "Bad public key"); - var key = ec.keyPair(publicKey); - return key.verify(msg, sig) ? resolve() : reject(); + return ec.verify(msg, sig, publicKey) ? resolve() : reject(); }); }; @@ -97,8 +95,8 @@ var derive = exports.derive = function(privateKeyA, publicKeyB) { assert(privateKeyA.length === 32, "Bad private key"); assert(publicKeyB.length === 65, "Bad public key"); assert(publicKeyB[0] === 4, "Bad public key"); - var keyA = ec.keyPair(privateKeyA); - var keyB = ec.keyPair(publicKeyB); + var keyA = ec.keyFromPrivate(privateKeyA); + var keyB = ec.keyFromPublic(publicKeyB); var Px = keyA.derive(keyB.getPublic()); // BN instance resolve(new Buffer(Px.toString(16, 2), "hex")); }); diff --git a/package.json b/package.json index d639b8f..e45f3a9 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "mocha": "*" }, "dependencies": { - "elliptic": "^1.0.1", + "elliptic": "^2.0.0", "es6-promise": "^2.0.1", "nan": "^1.4.1" }, diff --git a/test.js b/test.js index c9f9153..2324c1a 100644 --- a/test.js +++ b/test.js @@ -64,8 +64,13 @@ describe("ECDSA", function() { it("should reject promise on invalid key when verifying", function(done) { eccrypto.sign(privateKey, msg).then(function(sig) { expect(Buffer.isBuffer(sig)).to.be.true; - eccrypto.verify(Buffer("test"), msg, sig).catch(function(e) { - done(); + eccrypto.verify(Buffer("test"), msg, sig).catch(function() { + var badKey = new Buffer(65); + publicKey.copy(badKey); + badKey[0] ^= 1; + eccrypto.verify(badKey, msg, sig).catch(function() { + done(); + }); }); }); });