Skip to content

Commit

Permalink
chore: treeshaking cryptography
Browse files Browse the repository at this point in the history
Signed-off-by: Ivaylo Nikolov <ivaylo.nikolov@limechain.tech>
  • Loading branch information
ivaylonikolov7 committed Feb 20, 2025
1 parent 4352707 commit be665e3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 44 deletions.
14 changes: 7 additions & 7 deletions src/Mnemonic.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* ‍
*/

import * as cryptography from "@hashgraph/cryptography";
import { Mnemonic as MnemonicCryptography } from "@hashgraph/cryptography";
import CACHE from "./Cache.js";

/**
Expand All @@ -35,7 +35,7 @@ const HARDENED_BIT = 0x80000000;
*/
export default class Mnemonic {
/**
* @param {cryptography.Mnemonic} mnemonic
* @param {MnemonicCryptography} mnemonic
* @hideconstructor
* @private
*/
Expand All @@ -50,7 +50,7 @@ export default class Mnemonic {
* @returns {Promise<Mnemonic>}
*/
static async generate() {
return new Mnemonic(await cryptography.Mnemonic._generate(24));
return new Mnemonic(await MnemonicCryptography._generate(24));
}

/**
Expand All @@ -60,7 +60,7 @@ export default class Mnemonic {
* @returns {Promise<Mnemonic>}
*/
static async generate12() {
return new Mnemonic(await cryptography.Mnemonic._generate(12));
return new Mnemonic(await MnemonicCryptography._generate(12));
}

/**
Expand All @@ -77,7 +77,7 @@ export default class Mnemonic {
* @returns {Promise<Mnemonic>}
*/
static async fromWords(words) {
return new Mnemonic(await cryptography.Mnemonic.fromWords(words));
return new Mnemonic(await MnemonicCryptography.fromWords(words));
}

/**
Expand Down Expand Up @@ -236,7 +236,7 @@ export default class Mnemonic {
* @returns {Promise<Mnemonic>}
*/
static async fromString(mnemonic) {
return new Mnemonic(await cryptography.Mnemonic.fromString(mnemonic));
return new Mnemonic(await MnemonicCryptography.fromString(mnemonic));
}

/**
Expand All @@ -253,7 +253,7 @@ export default class Mnemonic {
* @returns {Promise<Uint8Array>}
*/
async toSeed(passphrase) {
return await cryptography.Mnemonic.toSeed(
return await MnemonicCryptography.toSeed(
this._mnemonic.words,
passphrase,
);
Expand Down
50 changes: 21 additions & 29 deletions src/PrivateKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* ‍
*/

import * as cryptography from "@hashgraph/cryptography";
import { PrivateKey as PrivateKeyCrypto } from "@hashgraph/cryptography";
import Mnemonic from "./Mnemonic.js";
import PublicKey from "./PublicKey.js";
import Key from "./Key.js";
Expand Down Expand Up @@ -46,7 +46,7 @@ export default class PrivateKey extends Key {
/**
* @internal
* @hideconstructor
* @param {cryptography.PrivateKey} key
* @param {PrivateKeyCrypto} key
*/
constructor(key) {
super();
Expand All @@ -60,7 +60,7 @@ export default class PrivateKey extends Key {
* @returns {PrivateKey}
*/
static generateED25519() {
return new PrivateKey(cryptography.PrivateKey.generateED25519());
return new PrivateKey(PrivateKeyCrypto.generateED25519());
}

/**
Expand All @@ -69,7 +69,7 @@ export default class PrivateKey extends Key {
* @returns {PrivateKey}
*/
static generateECDSA() {
return new PrivateKey(cryptography.PrivateKey.generateECDSA());
return new PrivateKey(PrivateKeyCrypto.generateECDSA());
}

/**
Expand All @@ -89,7 +89,7 @@ export default class PrivateKey extends Key {
* @returns {Promise<PrivateKey>}
*/
static async generateAsync() {
return new PrivateKey(await cryptography.PrivateKey.generateAsync());
return new PrivateKey(await PrivateKeyCrypto.generateAsync());
}

/**
Expand All @@ -98,9 +98,7 @@ export default class PrivateKey extends Key {
* @returns {Promise<PrivateKey>}
*/
static async generateED25519Async() {
return new PrivateKey(
await cryptography.PrivateKey.generateED25519Async(),
);
return new PrivateKey(await PrivateKeyCrypto.generateED25519Async());
}

/**
Expand All @@ -109,9 +107,7 @@ export default class PrivateKey extends Key {
* @returns {Promise<PrivateKey>}
*/
static async generateECDSAAsync() {
return new PrivateKey(
await cryptography.PrivateKey.generateECDSAAsync(),
);
return new PrivateKey(await PrivateKeyCrypto.generateECDSAAsync());
}

/**
Expand All @@ -121,7 +117,7 @@ export default class PrivateKey extends Key {
* @returns {PrivateKey}
*/
static fromBytes(data) {
return new PrivateKey(cryptography.PrivateKey.fromBytes(data));
return new PrivateKey(PrivateKeyCrypto.fromBytes(data));
}

/**
Expand All @@ -131,7 +127,7 @@ export default class PrivateKey extends Key {
* @returns {PrivateKey}
*/
static fromBytesECDSA(data) {
return new PrivateKey(cryptography.PrivateKey.fromBytesECDSA(data));
return new PrivateKey(PrivateKeyCrypto.fromBytesECDSA(data));
}

/**
Expand All @@ -141,7 +137,7 @@ export default class PrivateKey extends Key {
* @returns {PrivateKey}
*/
static fromBytesED25519(data) {
return new PrivateKey(cryptography.PrivateKey.fromBytesED25519(data));
return new PrivateKey(PrivateKeyCrypto.fromBytesED25519(data));
}

/**
Expand All @@ -152,7 +148,7 @@ export default class PrivateKey extends Key {
* @returns {PrivateKey}
*/
static fromString(text) {
return new PrivateKey(cryptography.PrivateKey.fromString(text));
return new PrivateKey(PrivateKeyCrypto.fromString(text));
}

/**
Expand All @@ -162,7 +158,7 @@ export default class PrivateKey extends Key {
* @returns {PrivateKey}
*/
static fromStringDer(text) {
return new PrivateKey(cryptography.PrivateKey.fromString(text));
return new PrivateKey(PrivateKeyCrypto.fromString(text));
}

/**
Expand All @@ -172,7 +168,7 @@ export default class PrivateKey extends Key {
* @returns {PrivateKey}
*/
static fromStringECDSA(text) {
return new PrivateKey(cryptography.PrivateKey.fromStringECDSA(text));
return new PrivateKey(PrivateKeyCrypto.fromStringECDSA(text));
}

/**
Expand All @@ -182,7 +178,7 @@ export default class PrivateKey extends Key {
* @returns {PrivateKey}
*/
static fromStringED25519(text) {
return new PrivateKey(cryptography.PrivateKey.fromStringED25519(text));
return new PrivateKey(PrivateKeyCrypto.fromStringED25519(text));
}

/**
Expand All @@ -192,9 +188,7 @@ export default class PrivateKey extends Key {
* @returns {Promise<PrivateKey>}
*/
static async fromSeedED25519(seed) {
return new PrivateKey(
await cryptography.PrivateKey.fromSeedED25519(seed),
);
return new PrivateKey(await PrivateKeyCrypto.fromSeedED25519(seed));
}

/**
Expand All @@ -205,23 +199,23 @@ export default class PrivateKey extends Key {
*/
static async fromSeedECDSAsecp256k1(seed) {
return new PrivateKey(
await cryptography.PrivateKey.fromSeedECDSAsecp256k1(seed),
await PrivateKeyCrypto.fromSeedECDSAsecp256k1(seed),
);
}

/**
* @deprecated - Use `Mnemonic.from[Words|String]().to[Ed25519|Ecdsa]PrivateKey()` instead
*
* Recover a private key from a mnemonic phrase (and optionally a password).
* @param {Mnemonic | cryptography.Mnemonic | string} mnemonic
* @param {Mnemonic | string} mnemonic
* @param {string} [passphrase]
* @returns {Promise<PrivateKey>}
*/
static async fromMnemonic(mnemonic, passphrase = "") {
if (mnemonic instanceof Mnemonic) {
return new PrivateKey(
// eslint-disable-next-line deprecation/deprecation
await cryptography.PrivateKey.fromMnemonic(
await PrivateKeyCrypto.fromMnemonic(
mnemonic._mnemonic,
passphrase,
),
Expand All @@ -230,7 +224,7 @@ export default class PrivateKey extends Key {

return new PrivateKey(
// eslint-disable-next-line deprecation/deprecation
await cryptography.PrivateKey.fromMnemonic(mnemonic, passphrase),
await PrivateKeyCrypto.fromMnemonic(mnemonic, passphrase),
);
}

Expand All @@ -246,7 +240,7 @@ export default class PrivateKey extends Key {
*/
static async fromKeystore(data, passphrase = "") {
return new PrivateKey(
await cryptography.PrivateKey.fromKeystore(data, passphrase),
await PrivateKeyCrypto.fromKeystore(data, passphrase),
);
}

Expand All @@ -264,9 +258,7 @@ export default class PrivateKey extends Key {
* @returns {Promise<PrivateKey>}
*/
static async fromPem(data, passphrase = "") {
return new PrivateKey(
await cryptography.PrivateKey.fromPem(data, passphrase),
);
return new PrivateKey(await PrivateKeyCrypto.fromPem(data, passphrase));
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/PublicKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* ‍
*/

import * as cryptography from "@hashgraph/cryptography";
import { PublicKey as PublicKeyCrypto } from "@hashgraph/cryptography";
import { arrayEqual } from "./array.js";
import Key from "./Key.js";
import CACHE from "./Cache.js";
Expand All @@ -40,7 +40,7 @@ export default class PublicKey extends Key {
/**
* @internal
* @hideconstructor
* @param {cryptography.PublicKey} key
* @param {PublicKeyCrypto} key
*/
constructor(key) {
super();
Expand All @@ -53,23 +53,23 @@ export default class PublicKey extends Key {
* @returns {PublicKey}
*/
static fromBytes(data) {
return new PublicKey(cryptography.PublicKey.fromBytes(data));
return new PublicKey(PublicKeyCrypto.fromBytes(data));
}

/**
* @param {Uint8Array} data
* @returns {PublicKey}
*/
static fromBytesED25519(data) {
return new PublicKey(cryptography.PublicKey.fromBytesED25519(data));
return new PublicKey(PublicKeyCrypto.fromBytesED25519(data));
}

/**
* @param {Uint8Array} data
* @returns {PublicKey}
*/
static fromBytesECDSA(data) {
return new PublicKey(cryptography.PublicKey.fromBytesECDSA(data));
return new PublicKey(PublicKeyCrypto.fromBytesECDSA(data));
}

/**
Expand All @@ -82,7 +82,7 @@ export default class PublicKey extends Key {
* @returns {PublicKey}
*/
static fromString(text) {
return new PublicKey(cryptography.PublicKey.fromString(text));
return new PublicKey(PublicKeyCrypto.fromString(text));
}

/**
Expand All @@ -92,7 +92,7 @@ export default class PublicKey extends Key {
* @returns {PublicKey}
*/
static fromStringECDSA(text) {
return new PublicKey(cryptography.PublicKey.fromStringECDSA(text));
return new PublicKey(PublicKeyCrypto.fromStringECDSA(text));
}

/**
Expand All @@ -102,7 +102,7 @@ export default class PublicKey extends Key {
* @returns {PublicKey}
*/
static fromStringED25519(text) {
return new PublicKey(cryptography.PublicKey.fromStringED25519(text));
return new PublicKey(PublicKeyCrypto.fromStringED25519(text));
}

/**
Expand Down

0 comments on commit be665e3

Please sign in to comment.