2014-12-23 23:14:28 +00:00
|
|
|
/**
|
|
|
|
* Node.js eccrypto implementation.
|
|
|
|
* @module eccrypto
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2015-01-06 11:21:46 +00:00
|
|
|
var promise = typeof Promise === "undefined" ?
|
|
|
|
require("es6-promise").Promise :
|
|
|
|
Promise;
|
2014-12-23 20:28:40 +00:00
|
|
|
var secp256k1 = require("secp256k1");
|
|
|
|
|
2014-12-23 23:14:28 +00:00
|
|
|
/**
|
|
|
|
* Compute the public key for a given private key.
|
2015-01-02 15:21:24 +00:00
|
|
|
* @param {Buffer} privateKey - A 32-byte private key
|
|
|
|
* @return {Buffer} A 65-byte public key.
|
2015-01-02 13:47:09 +00:00
|
|
|
* @function
|
2014-12-23 23:14:28 +00:00
|
|
|
*/
|
2015-01-13 13:21:11 +00:00
|
|
|
exports.getPublic = secp256k1.createPublicKey;
|
2014-12-23 23:14:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an ECDSA signature.
|
2015-01-02 15:21:24 +00:00
|
|
|
* @param {Buffer} privateKey - A 32-byte private key
|
|
|
|
* @param {Buffer} msg - The message being signed
|
|
|
|
* @return {Promise.<Buffer>} A promise that resolves with the
|
|
|
|
* signature and rejects on bad key or message.
|
2014-12-23 23:14:28 +00:00
|
|
|
*/
|
|
|
|
exports.sign = function(privateKey, msg) {
|
2015-01-12 17:50:21 +00:00
|
|
|
return new promise(function(resolve) {
|
|
|
|
resolve(secp256k1.sign(privateKey, msg));
|
2014-12-23 23:14:28 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify an ECDSA signature.
|
2015-01-13 12:11:53 +00:00
|
|
|
* @param {Buffer} publicKey - A 65-byte public key
|
2015-01-02 15:21:24 +00:00
|
|
|
* @param {Buffer} msg - The message being verified
|
|
|
|
* @param {Buffer} sig - The signature
|
|
|
|
* @return {Promise.<undefined>} A promise that resolves on correct
|
|
|
|
* signature and rejects on bad key or signature.
|
2014-12-23 23:14:28 +00:00
|
|
|
*/
|
2015-01-13 12:11:53 +00:00
|
|
|
exports.verify = function(publicKey, msg, sig) {
|
2015-01-06 11:21:46 +00:00
|
|
|
return new promise(function(resolve, reject) {
|
2015-01-12 17:50:21 +00:00
|
|
|
return secp256k1.verify(publicKey, msg, sig) === 1 ? resolve() : reject();
|
2014-12-23 23:14:28 +00:00
|
|
|
});
|
|
|
|
};
|