2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
import { ec as EC } from 'elliptic';
|
2018-06-23 03:30:50 +03:00
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
import { getAddress } from './address';
|
2018-07-13 03:11:32 +03:00
|
|
|
|
2018-07-17 08:44:04 +03:00
|
|
|
import { arrayify, hexlify, hexZeroPad, splitSignature } from './bytes';
|
|
|
|
import { hashMessage } from './hash';
|
2018-06-17 23:32:57 +03:00
|
|
|
import { keccak256 } from './keccak256';
|
2018-06-17 23:47:28 +03:00
|
|
|
import { defineReadOnly } from './properties';
|
2018-06-15 11:18:17 +03:00
|
|
|
|
2018-06-17 23:47:28 +03:00
|
|
|
import * as errors from './errors';
|
2018-06-15 11:18:17 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
///////////////////////////////
|
|
|
|
// Imported Types
|
|
|
|
|
|
|
|
import { Arrayish, Signature } from './bytes';
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
|
2018-07-23 03:05:14 +03:00
|
|
|
let _curve: EC = null
|
|
|
|
function getCurve() {
|
|
|
|
if (!_curve) {
|
|
|
|
_curve = new EC('secp256k1');
|
|
|
|
}
|
|
|
|
return _curve;
|
|
|
|
}
|
|
|
|
|
2018-06-17 23:32:57 +03:00
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
export class KeyPair {
|
|
|
|
|
|
|
|
readonly privateKey: string;
|
|
|
|
|
|
|
|
readonly publicKey: string;
|
|
|
|
readonly compressedPublicKey: string;
|
|
|
|
|
|
|
|
readonly publicKeyBytes: Uint8Array;
|
|
|
|
|
2018-09-06 23:37:44 +03:00
|
|
|
constructor(privateKey: Arrayish | string) {
|
2018-07-23 03:05:14 +03:00
|
|
|
let keyPair = getCurve().keyFromPrivate(arrayify(privateKey));
|
2018-06-15 11:18:17 +03:00
|
|
|
|
|
|
|
defineReadOnly(this, 'privateKey', hexlify(keyPair.priv.toArray('be', 32)));
|
|
|
|
defineReadOnly(this, 'publicKey', '0x' + keyPair.getPublic(false, 'hex'));
|
|
|
|
defineReadOnly(this, 'compressedPublicKey', '0x' + keyPair.getPublic(true, 'hex'));
|
|
|
|
defineReadOnly(this, 'publicKeyBytes', keyPair.getPublic().encode(null, true));
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:37:44 +03:00
|
|
|
sign(digest: Arrayish | string): Signature {
|
2018-07-23 03:05:14 +03:00
|
|
|
let keyPair = getCurve().keyFromPrivate(arrayify(this.privateKey));
|
2018-06-15 11:18:17 +03:00
|
|
|
let signature = keyPair.sign(arrayify(digest), {canonical: true});
|
|
|
|
return {
|
|
|
|
recoveryParam: signature.recoveryParam,
|
2018-06-18 12:42:41 +03:00
|
|
|
r: hexZeroPad('0x' + signature.r.toString(16), 32),
|
|
|
|
s: hexZeroPad('0x' + signature.s.toString(16), 32),
|
2018-06-17 23:32:57 +03:00
|
|
|
v: 27 + signature.recoveryParam
|
2018-06-15 11:18:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:37:44 +03:00
|
|
|
computeSharedSecret(otherKey: Arrayish | string): string {
|
|
|
|
let keyPair = getCurve().keyFromPrivate(arrayify(this.privateKey));
|
|
|
|
let otherKeyPair = getCurve().keyFromPublic(arrayify(computePublicKey(otherKey)));
|
|
|
|
return hexZeroPad('0x' + keyPair.derive(otherKeyPair.getPublic()).toString(16), 32);
|
|
|
|
}
|
2018-06-15 11:18:17 +03:00
|
|
|
}
|
|
|
|
|
2018-09-06 23:37:44 +03:00
|
|
|
export function computePublicKey(key: Arrayish | string, compressed?: boolean): string {
|
2018-06-15 11:18:17 +03:00
|
|
|
|
|
|
|
let bytes = arrayify(key);
|
|
|
|
|
|
|
|
if (bytes.length === 32) {
|
|
|
|
let keyPair: KeyPair = new KeyPair(bytes);
|
|
|
|
if (compressed) {
|
|
|
|
return keyPair.compressedPublicKey;
|
|
|
|
}
|
|
|
|
return keyPair.publicKey;
|
|
|
|
|
|
|
|
} else if (bytes.length === 33) {
|
|
|
|
if (compressed) { return hexlify(bytes); }
|
2018-07-23 03:05:14 +03:00
|
|
|
return '0x' + getCurve().keyFromPublic(bytes).getPublic(false, 'hex');
|
2018-06-15 11:18:17 +03:00
|
|
|
|
|
|
|
} else if (bytes.length === 65) {
|
|
|
|
if (!compressed) { return hexlify(bytes); }
|
2018-07-23 03:05:14 +03:00
|
|
|
return '0x' + getCurve().keyFromPublic(bytes).getPublic(true, 'hex');
|
2018-06-15 11:18:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
errors.throwError('invalid public or private key', errors.INVALID_ARGUMENT, { arg: 'key', value: '[REDACTED]' });
|
|
|
|
return null;
|
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-09-06 23:37:44 +03:00
|
|
|
export function computeAddress(key: Arrayish | string): string {
|
2018-06-17 23:32:57 +03:00
|
|
|
// Strip off the leading "0x04"
|
|
|
|
let publicKey = '0x' + computePublicKey(key).slice(4);
|
|
|
|
return getAddress('0x' + keccak256(publicKey).substring(26));
|
|
|
|
}
|
2018-07-17 08:44:04 +03:00
|
|
|
|
2018-09-06 23:37:44 +03:00
|
|
|
export function recoverPublicKey(digest: Arrayish | string, signature: Signature | string): string {
|
|
|
|
let sig = splitSignature(signature);
|
|
|
|
let rs = { r: arrayify(sig.r), s: arrayify(sig.s) };
|
|
|
|
return '0x' + getCurve().recoverPubKey(arrayify(digest), rs, sig.recoveryParam).encode('hex', false);
|
2018-08-27 14:38:48 +03:00
|
|
|
}
|
2018-07-17 08:44:04 +03:00
|
|
|
|
2018-09-06 23:37:44 +03:00
|
|
|
export function recoverAddress(digest: Arrayish | string, signature: Signature | string): string {
|
|
|
|
return computeAddress(recoverPublicKey(arrayify(digest), signature));
|
2018-07-17 08:44:04 +03:00
|
|
|
}
|
2018-08-27 14:38:48 +03:00
|
|
|
|
|
|
|
export function verifyMessage(message: Arrayish | string, signature: Signature | string): string {
|
2018-09-06 23:37:44 +03:00
|
|
|
return recoverAddress(hashMessage(message), signature);
|
2018-08-27 14:38:48 +03:00
|
|
|
}
|