Cleanup elliptic imports (#889)

* Cleanup elliptic imports

* Additional

* Cleanups

* HydraDX

* Flatten
This commit is contained in:
Jaco Greeff
2021-02-25 10:34:37 +01:00
committed by GitHub
parent ca3c932701
commit 82c4b75a12
3 changed files with 49 additions and 56 deletions
+9
View File
@@ -456,6 +456,15 @@ const all: NetworkFromSubstrate[] = [
symbols: ['NEAT'],
website: 'https://neatcoin.org'
},
{
decimals: [12],
displayName: 'HydraDX',
network: 'hydradx',
prefix: 63,
standardAccount: '*25519',
symbols: ['HDX'],
website: 'https://hydradx.io'
},
{
decimals: [18],
displayName: 'AvN Mainnet',
+30 -29
View File
@@ -32,74 +32,75 @@ export class HDKeyEth {
versions: Versions;
public depth: number;
index: number;
private _privateKey: Uint8Array|null;
private _publicKey: Uint8Array|null;
public chainCode: Uint8Array|null;
private _fingerprint: number|null;
parentFingerprint: number|null;
private _identifier: Buffer|null;
public chainCode: Uint8Array | null;
parentFingerprint: number | null;
#fingerprint: number | null;
#identifier: Buffer | null;
#privateKey: Uint8Array | null;
#publicKey: Uint8Array | null;
constructor (versions?: Versions) {
this.versions = versions || BITCOIN_VERSIONS;
this.depth = 0;
this.index = 0;
this._privateKey = null;
this._publicKey = null;
this._identifier = null;
this.#privateKey = null;
this.#publicKey = null;
this.#identifier = null;
this.chainCode = null;
this._fingerprint = null;
this.#fingerprint = null;
this.parentFingerprint = 0;
}
// private key
set privateKey (value: Uint8Array|null) {
set privateKey (value: Uint8Array | null) {
assert(value && value.length === 32, 'Private key must be 32 bytes.');
// TODO: implement privateKeyVerify for local secp256k1
// assert(secp256k1.privateKeyVerify(value) === true, 'Invalid private key');
this._privateKey = value;
this.#privateKey = value;
if (value) {
this._publicKey = secp256k1KeypairFromSeed(value).publicKey;
this._identifier = this._publicKey ? this.hash160(this._publicKey) : null;
this._fingerprint = this._identifier ? this._identifier.slice(0, 4).readUInt32BE(0) : null;
this.#publicKey = secp256k1KeypairFromSeed(value).publicKey;
this.#identifier = this.#publicKey ? this.hash160(this.#publicKey) : null;
this.#fingerprint = this.#identifier ? this.#identifier.slice(0, 4).readUInt32BE(0) : null;
}
}
get privateKey (): Uint8Array|null {
return this._privateKey;
get privateKey (): Uint8Array | null {
return this.#privateKey;
}
// public key
get publicKey (): Uint8Array|null {
return this._publicKey;
get publicKey (): Uint8Array | null {
return this.#publicKey;
}
set publicKey (value: Uint8Array|null) {
set publicKey (value: Uint8Array | null) {
assert(value && (value.length === 33 || value.length === 65), 'Public key must be 33 or 65 bytes.');
// TODO: implement publicKeyVerify for local secp256k1
// assert(secp256k1.publicKeyVerify(value) === true, 'Invalid public key');
// TODO: should I use the compress function here?
this._publicKey = value; // new Uint8Array(Buffer.from(secp256k1.publicKeyConvert(value, true))); // force compressed point
this._identifier = this._publicKey ? this.hash160(this._publicKey) : null;
this._fingerprint = this._identifier ? this._identifier.slice(0, 4).readUInt32BE(0) : null;
this._privateKey = null;
this.#publicKey = value; // new Uint8Array(Buffer.from(secp256k1.publicKeyConvert(value, true))); // force compressed point
this.#identifier = this.#publicKey ? this.hash160(this.#publicKey) : null;
this.#fingerprint = this.#identifier ? this.#identifier.slice(0, 4).readUInt32BE(0) : null;
this.#privateKey = null;
}
// fingerprint
get fingerprint (): number|null {
return this._fingerprint;
get fingerprint (): number | null {
return this.#fingerprint;
}
// identifier
get identifier (): Buffer|null {
return this._identifier;
get identifier (): Buffer | null {
return this.#identifier;
}
// fingerprint
get pubKeyHash (): Buffer|null {
get pubKeyHash (): Buffer | null {
return this.identifier;
}
+10 -27
View File
@@ -2,45 +2,28 @@
// SPDX-License-Identifier: Apache-2.0
import BN from 'bn.js';
import { ec as EC } from 'elliptic';
import { assert, isU8a } from '@polkadot/util';
import { assert, bnToU8a, isU8a } from '@polkadot/util';
const ec = new EC('secp256k1');
const ecparams = ec.curve as {n: BN};
import { secp256k1 } from './secp256k1';
const errors = {
PUBKEY_PARSE: 'Public Key could not be parsed',
TWEAK_ADD: 'The tweak was out of range or the resulted private key is invalid'
};
// Private key
const ecparams = secp256k1.curve as { n: BN };
export function secp256k1PrivateKeyTweakAdd (seckey: Uint8Array, tweak: Uint8Array): Uint8Array {
assert(isU8a(seckey) && seckey.length === 32, 'Expected seckey to be an Uint8Array with length 32');
assert(isU8a(tweak) && tweak.length === 32, 'Expected tweak to be an Uint8Array with length 32');
switch (_secp256k1PrivateKeyTweakAdd(seckey, tweak)) {
case 0:
return seckey;
case 1:
throw new Error(errors.TWEAK_ADD);
}
return seckey;
}
function _secp256k1PrivateKeyTweakAdd (seckey: Uint8Array, tweak: Uint8Array): number {
const bn = new BN(tweak);
if (bn.cmp(ecparams.n) >= 0) return 1;
assert(bn.cmp(ecparams.n) < 0, 'Tweak parameter is out of range');
bn.iadd(new BN(seckey));
if (bn.cmp(ecparams.n) >= 0) bn.isub(ecparams.n);
if (bn.isZero()) return 1;
const tweaked = bn.toArrayLike(Buffer, 'be', 32);
seckey.set(tweaked);
if (bn.cmp(ecparams.n) >= 0) {
bn.isub(ecparams.n);
}
return 0;
assert(!bn.isZero(), 'Invalid resulting private key');
return bnToU8a(bn, { bitLength: 256, isLe: false });
}