Lazy load the elliptic curve; also fixed circular dependencies internal to the library that cause issues in browserify.
This commit is contained in:
parent
0bc72c5ed4
commit
e3da92d1ee
@ -8,11 +8,20 @@ import { arrayify, hexlify, hexZeroPad, splitSignature } from './bytes';
|
|||||||
import { hashMessage } from './hash';
|
import { hashMessage } from './hash';
|
||||||
import { keccak256 } from './keccak256';
|
import { keccak256 } from './keccak256';
|
||||||
import { defineReadOnly } from './properties';
|
import { defineReadOnly } from './properties';
|
||||||
|
import { ec as EC } from 'elliptic';
|
||||||
|
|
||||||
import { Arrayish, Signature } from './types';
|
import { Arrayish, Signature } from './types';
|
||||||
|
|
||||||
import * as errors from './errors';
|
import * as errors from './errors';
|
||||||
|
|
||||||
|
let _curve: EC = null
|
||||||
|
function getCurve() {
|
||||||
|
if (!_curve) {
|
||||||
|
_curve = new EC('secp256k1');
|
||||||
|
}
|
||||||
|
return _curve;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export class KeyPair {
|
export class KeyPair {
|
||||||
|
|
||||||
@ -24,7 +33,7 @@ export class KeyPair {
|
|||||||
readonly publicKeyBytes: Uint8Array;
|
readonly publicKeyBytes: Uint8Array;
|
||||||
|
|
||||||
constructor(privateKey: Arrayish) {
|
constructor(privateKey: Arrayish) {
|
||||||
let keyPair = curve.keyFromPrivate(arrayify(privateKey));
|
let keyPair = getCurve().keyFromPrivate(arrayify(privateKey));
|
||||||
|
|
||||||
defineReadOnly(this, 'privateKey', hexlify(keyPair.priv.toArray('be', 32)));
|
defineReadOnly(this, 'privateKey', hexlify(keyPair.priv.toArray('be', 32)));
|
||||||
defineReadOnly(this, 'publicKey', '0x' + keyPair.getPublic(false, 'hex'));
|
defineReadOnly(this, 'publicKey', '0x' + keyPair.getPublic(false, 'hex'));
|
||||||
@ -33,7 +42,7 @@ export class KeyPair {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sign(digest: Arrayish): Signature {
|
sign(digest: Arrayish): Signature {
|
||||||
let keyPair = curve.keyFromPrivate(arrayify(this.privateKey));
|
let keyPair = getCurve().keyFromPrivate(arrayify(this.privateKey));
|
||||||
let signature = keyPair.sign(arrayify(digest), {canonical: true});
|
let signature = keyPair.sign(arrayify(digest), {canonical: true});
|
||||||
return {
|
return {
|
||||||
recoveryParam: signature.recoveryParam,
|
recoveryParam: signature.recoveryParam,
|
||||||
@ -50,7 +59,7 @@ export function recoverPublicKey(digest: Arrayish, signature: Signature): string
|
|||||||
r: arrayify(signature.r),
|
r: arrayify(signature.r),
|
||||||
s: arrayify(signature.s)
|
s: arrayify(signature.s)
|
||||||
};
|
};
|
||||||
return '0x' + curve.recoverPubKey(arrayify(digest), sig, signature.recoveryParam).encode('hex', false);
|
return '0x' + getCurve().recoverPubKey(arrayify(digest), sig, signature.recoveryParam).encode('hex', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function computePublicKey(key: Arrayish, compressed?: boolean): string {
|
export function computePublicKey(key: Arrayish, compressed?: boolean): string {
|
||||||
@ -66,11 +75,11 @@ export function computePublicKey(key: Arrayish, compressed?: boolean): string {
|
|||||||
|
|
||||||
} else if (bytes.length === 33) {
|
} else if (bytes.length === 33) {
|
||||||
if (compressed) { return hexlify(bytes); }
|
if (compressed) { return hexlify(bytes); }
|
||||||
return '0x' + curve.keyFromPublic(bytes).getPublic(false, 'hex');
|
return '0x' + getCurve().keyFromPublic(bytes).getPublic(false, 'hex');
|
||||||
|
|
||||||
} else if (bytes.length === 65) {
|
} else if (bytes.length === 65) {
|
||||||
if (!compressed) { return hexlify(bytes); }
|
if (!compressed) { return hexlify(bytes); }
|
||||||
return '0x' + curve.keyFromPublic(bytes).getPublic(true, 'hex');
|
return '0x' + getCurve().keyFromPublic(bytes).getPublic(true, 'hex');
|
||||||
}
|
}
|
||||||
|
|
||||||
errors.throwError('invalid public or private key', errors.INVALID_ARGUMENT, { arg: 'key', value: '[REDACTED]' });
|
errors.throwError('invalid public or private key', errors.INVALID_ARGUMENT, { arg: 'key', value: '[REDACTED]' });
|
||||||
@ -102,12 +111,3 @@ export function verifyMessage(message: Arrayish | string, signature: Signature |
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// !!!!!! IMPORTANT !!!!!!!!
|
|
||||||
//
|
|
||||||
// This import MUST be at the bottom, otehrwise browserify executes several imports
|
|
||||||
// BEFORE they are exported, resulting in undefined
|
|
||||||
|
|
||||||
import { ec as EC } from 'elliptic';
|
|
||||||
const curve = new EC('secp256k1');
|
|
||||||
|
|
||||||
export const N = '0x' + curve.n.toString(16);
|
|
||||||
|
@ -3,6 +3,7 @@ import { getAddress } from './address';
|
|||||||
import { bigNumberify, ConstantZero } from './bignumber';
|
import { bigNumberify, ConstantZero } from './bignumber';
|
||||||
import { arrayify, hexlify, hexZeroPad, splitSignature, stripZeros, } from './bytes';
|
import { arrayify, hexlify, hexZeroPad, splitSignature, stripZeros, } from './bytes';
|
||||||
import { keccak256 } from './keccak256';
|
import { keccak256 } from './keccak256';
|
||||||
|
import { recoverAddress } from './secp256k1';
|
||||||
|
|
||||||
import * as RLP from './rlp';
|
import * as RLP from './rlp';
|
||||||
|
|
||||||
@ -159,4 +160,3 @@ export function parse(rawTransaction: Arrayish): Transaction {
|
|||||||
return tx;
|
return tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
import { recoverAddress } from './secp256k1';
|
|
||||||
|
@ -17,9 +17,11 @@ import { toUtf8Bytes, UnicodeNormalizationForm } from '../utils/utf8';
|
|||||||
import { pbkdf2 } from '../utils/pbkdf2';
|
import { pbkdf2 } from '../utils/pbkdf2';
|
||||||
import { computeHmac } from '../utils/hmac';
|
import { computeHmac } from '../utils/hmac';
|
||||||
import { defineReadOnly } from '../utils/properties';
|
import { defineReadOnly } from '../utils/properties';
|
||||||
import { KeyPair, N } from '../utils/secp256k1';
|
import { KeyPair } from '../utils/secp256k1';
|
||||||
import { sha256 } from '../utils/sha2';
|
import { sha256 } from '../utils/sha2';
|
||||||
|
|
||||||
|
const N = bigNumberify("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
|
||||||
|
|
||||||
import { Arrayish, HDNode as _HDNode ,Wordlist } from '../utils/types';
|
import { Arrayish, HDNode as _HDNode ,Wordlist } from '../utils/types';
|
||||||
|
|
||||||
import * as errors from '../utils/errors';
|
import * as errors from '../utils/errors';
|
||||||
|
@ -67,14 +67,3 @@ export class SigningKey {
|
|||||||
return this.keyPair.sign(digest);
|
return this.keyPair.sign(digest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
export function recoverAddress(digest: Arrayish, signature: Signature): string {
|
|
||||||
return computeAddress(recoverPublicKey(digest, signature));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function computeAddress(key: string): string {
|
|
||||||
// Strip off the leading "0x04"
|
|
||||||
let publicKey = '0x' + computePublicKey(key).slice(4);
|
|
||||||
return getAddress('0x' + keccak256(publicKey).substring(26));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user