crypto-signer
Version:
crypto-signer is a utility library used to sign transactions in MediSOT v2
49 lines (48 loc) • 2.23 kB
JavaScript
;
/**
* Signing function for Hyperledger Fabric
*
* Example for usage can be found under test directory
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CryptoSigner = void 0;
var elliptic = require('elliptic');
var KEYUTIL = require('jsrsasign').KEYUTIL;
var utils_1 = require("./utils");
var CryptoSigner = /** @class */ (function () {
function CryptoSigner() {
}
CryptoSigner.prototype.signECDSAp256 = function (msgHash, privKeyPEM) {
var prvKeyHex = utils_1.getHexKey(privKeyPEM); // convert the pem encoded key to hex encoded private key
var EC = elliptic.ec;
var ecdsaCurve = elliptic.curves['p256'];
var ecdsa = new EC(ecdsaCurve);
var signKey = ecdsa.keyFromPrivate(prvKeyHex, 'hex');
var sig = ecdsa.sign(Buffer.from(msgHash, 'hex'), signKey);
sig = this._preventECDSAp256Malleability(sig);
// now we have the signature, next we should send the signed transaction proposal to the peer
var signature = Buffer.from(sig.toDER());
return signature;
};
CryptoSigner.prototype.verifyECDSAp256 = function (msgHash, signature, pubKeyPEM) {
var pubKey = KEYUTIL.getKey(utils_1.cleanUpPEM(pubKeyPEM));
var pubPoint = pubKey.getPublicKeyXYHex();
var EC = elliptic.ec;
var ecdsaCurve = elliptic.curves['p256'];
var ecdsa = new EC(ecdsaCurve);
return ecdsa.verify(msgHash, signature, pubPoint);
};
CryptoSigner.prototype._preventECDSAp256Malleability = function (sig) {
var halfOrder = elliptic.curves.p256.n.shrn(1);
// in order to guarantee 's' falls in the lower range of the order, as explained in the above link,
// first see if 's' is larger than half of the order, if so, it needs to be specially treated
if (sig.s.cmp(halfOrder) === 1) { // module 'bn.js', file lib/bn.js, method cmp()
// convert from BigInteger used by jsrsasign Key objects and bn.js used by elliptic Signature objects
var bigNum = elliptic.curves.p256.n;
sig.s = bigNum.sub(sig.s);
}
return sig;
};
return CryptoSigner;
}());
exports.CryptoSigner = CryptoSigner;