2018-06-17 16:32:57 -04:00
|
|
|
'use strict';
|
|
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
|
if (mod && mod.__esModule) return mod;
|
|
|
|
var result = {};
|
|
|
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
|
|
result["default"] = mod;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2018-07-30 18:59:52 -04:00
|
|
|
var elliptic_1 = require("elliptic");
|
2018-06-17 16:32:57 -04:00
|
|
|
var address_1 = require("./address");
|
2018-06-17 16:47:28 -04:00
|
|
|
var bytes_1 = require("./bytes");
|
2018-07-17 01:46:27 -04:00
|
|
|
var hash_1 = require("./hash");
|
2018-06-17 16:32:57 -04:00
|
|
|
var keccak256_1 = require("./keccak256");
|
2018-06-17 16:47:28 -04:00
|
|
|
var properties_1 = require("./properties");
|
2018-09-24 16:07:14 -04:00
|
|
|
var errors = __importStar(require("../errors"));
|
2018-07-30 18:59:52 -04:00
|
|
|
///////////////////////////////
|
2018-07-23 02:59:01 -04:00
|
|
|
var _curve = null;
|
|
|
|
function getCurve() {
|
|
|
|
if (!_curve) {
|
|
|
|
_curve = new elliptic_1.ec('secp256k1');
|
|
|
|
}
|
|
|
|
return _curve;
|
|
|
|
}
|
2018-06-17 16:32:57 -04:00
|
|
|
var KeyPair = /** @class */ (function () {
|
|
|
|
function KeyPair(privateKey) {
|
2018-07-23 02:59:01 -04:00
|
|
|
var keyPair = getCurve().keyFromPrivate(bytes_1.arrayify(privateKey));
|
2018-06-17 16:47:28 -04:00
|
|
|
properties_1.defineReadOnly(this, 'privateKey', bytes_1.hexlify(keyPair.priv.toArray('be', 32)));
|
2018-06-17 16:32:57 -04:00
|
|
|
properties_1.defineReadOnly(this, 'publicKey', '0x' + keyPair.getPublic(false, 'hex'));
|
|
|
|
properties_1.defineReadOnly(this, 'compressedPublicKey', '0x' + keyPair.getPublic(true, 'hex'));
|
|
|
|
properties_1.defineReadOnly(this, 'publicKeyBytes', keyPair.getPublic().encode(null, true));
|
|
|
|
}
|
|
|
|
KeyPair.prototype.sign = function (digest) {
|
2018-07-23 02:59:01 -04:00
|
|
|
var keyPair = getCurve().keyFromPrivate(bytes_1.arrayify(this.privateKey));
|
2018-06-17 16:47:28 -04:00
|
|
|
var signature = keyPair.sign(bytes_1.arrayify(digest), { canonical: true });
|
2018-06-17 16:32:57 -04:00
|
|
|
return {
|
|
|
|
recoveryParam: signature.recoveryParam,
|
2018-06-18 05:42:41 -04:00
|
|
|
r: bytes_1.hexZeroPad('0x' + signature.r.toString(16), 32),
|
|
|
|
s: bytes_1.hexZeroPad('0x' + signature.s.toString(16), 32),
|
2018-06-17 16:32:57 -04:00
|
|
|
v: 27 + signature.recoveryParam
|
|
|
|
};
|
|
|
|
};
|
2018-09-06 16:41:02 -04:00
|
|
|
KeyPair.prototype.computeSharedSecret = function (otherKey) {
|
|
|
|
var keyPair = getCurve().keyFromPrivate(bytes_1.arrayify(this.privateKey));
|
|
|
|
var otherKeyPair = getCurve().keyFromPublic(bytes_1.arrayify(computePublicKey(otherKey)));
|
|
|
|
return bytes_1.hexZeroPad('0x' + keyPair.derive(otherKeyPair.getPublic()).toString(16), 32);
|
|
|
|
};
|
2019-02-01 19:50:10 -05:00
|
|
|
KeyPair.prototype._addPoint = function (other) {
|
|
|
|
var p0 = getCurve().keyFromPublic(bytes_1.arrayify(this.publicKey));
|
|
|
|
var p1 = getCurve().keyFromPublic(bytes_1.arrayify(other));
|
|
|
|
return "0x" + p0.pub.add(p1.pub).encodeCompressed("hex");
|
|
|
|
};
|
2018-06-17 16:32:57 -04:00
|
|
|
return KeyPair;
|
|
|
|
}());
|
|
|
|
exports.KeyPair = KeyPair;
|
|
|
|
function computePublicKey(key, compressed) {
|
2018-06-17 16:47:28 -04:00
|
|
|
var bytes = bytes_1.arrayify(key);
|
2018-06-17 16:32:57 -04:00
|
|
|
if (bytes.length === 32) {
|
|
|
|
var keyPair = new KeyPair(bytes);
|
|
|
|
if (compressed) {
|
|
|
|
return keyPair.compressedPublicKey;
|
|
|
|
}
|
|
|
|
return keyPair.publicKey;
|
|
|
|
}
|
|
|
|
else if (bytes.length === 33) {
|
|
|
|
if (compressed) {
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.hexlify(bytes);
|
2018-06-17 16:32:57 -04:00
|
|
|
}
|
2018-07-23 02:59:01 -04:00
|
|
|
return '0x' + getCurve().keyFromPublic(bytes).getPublic(false, 'hex');
|
2018-06-17 16:32:57 -04:00
|
|
|
}
|
|
|
|
else if (bytes.length === 65) {
|
|
|
|
if (!compressed) {
|
2018-06-17 16:47:28 -04:00
|
|
|
return bytes_1.hexlify(bytes);
|
2018-06-17 16:32:57 -04:00
|
|
|
}
|
2018-07-23 02:59:01 -04:00
|
|
|
return '0x' + getCurve().keyFromPublic(bytes).getPublic(true, 'hex');
|
2018-06-17 16:32:57 -04:00
|
|
|
}
|
|
|
|
errors.throwError('invalid public or private key', errors.INVALID_ARGUMENT, { arg: 'key', value: '[REDACTED]' });
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
exports.computePublicKey = computePublicKey;
|
|
|
|
function computeAddress(key) {
|
|
|
|
// Strip off the leading "0x04"
|
|
|
|
var publicKey = '0x' + computePublicKey(key).slice(4);
|
|
|
|
return address_1.getAddress('0x' + keccak256_1.keccak256(publicKey).substring(26));
|
|
|
|
}
|
|
|
|
exports.computeAddress = computeAddress;
|
2018-09-06 16:41:02 -04:00
|
|
|
function recoverPublicKey(digest, signature) {
|
2018-07-17 01:46:27 -04:00
|
|
|
var sig = bytes_1.splitSignature(signature);
|
2018-09-06 16:41:02 -04:00
|
|
|
var rs = { r: bytes_1.arrayify(sig.r), s: bytes_1.arrayify(sig.s) };
|
|
|
|
return '0x' + getCurve().recoverPubKey(bytes_1.arrayify(digest), rs, sig.recoveryParam).encode('hex', false);
|
2018-07-17 01:46:27 -04:00
|
|
|
}
|
2018-09-06 16:41:02 -04:00
|
|
|
exports.recoverPublicKey = recoverPublicKey;
|
|
|
|
function recoverAddress(digest, signature) {
|
|
|
|
return computeAddress(recoverPublicKey(bytes_1.arrayify(digest), signature));
|
|
|
|
}
|
|
|
|
exports.recoverAddress = recoverAddress;
|
2018-08-27 13:42:26 +02:00
|
|
|
function verifyMessage(message, signature) {
|
2018-09-06 16:41:02 -04:00
|
|
|
return recoverAddress(hash_1.hashMessage(message), signature);
|
2018-08-27 13:42:26 +02:00
|
|
|
}
|
2018-07-17 01:46:27 -04:00
|
|
|
exports.verifyMessage = verifyMessage;
|