From 3cfed2d3b999c670f0401534870e98ef5268977f Mon Sep 17 00:00:00 2001 From: "Hong Jing (Jingles)" Date: Mon, 10 Jun 2024 15:43:45 +0800 Subject: [PATCH 1/4] feat: remove async from fromBip39Entropy --- packages/crypto/src/Bip32/Bip32PrivateKey.ts | 22 +++++++++----------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/crypto/src/Bip32/Bip32PrivateKey.ts b/packages/crypto/src/Bip32/Bip32PrivateKey.ts index 764b14f17f4..46422e7daab 100644 --- a/packages/crypto/src/Bip32/Bip32PrivateKey.ts +++ b/packages/crypto/src/Bip32/Bip32PrivateKey.ts @@ -5,7 +5,7 @@ import { Bip32PublicKey } from './Bip32PublicKey'; import { EXTENDED_ED25519_PRIVATE_KEY_LENGTH, Ed25519PrivateKey } from '../Ed25519e'; import { InvalidArgumentError } from '@cardano-sdk/util'; import { crypto_scalarmult_ed25519_base_noclamp, ready } from 'libsodium-wrappers-sumo'; -import { pbkdf2 } from 'pbkdf2'; +import { pbkdf2Sync } from 'pbkdf2'; const SCALAR_INDEX = 0; const SCALAR_SIZE = 32; @@ -75,17 +75,15 @@ export class Bip32PrivateKey { * @param password The second factor authentication password for the mnemonic phrase. * @returns The secret extended key. */ - static fromBip39Entropy(entropy: Buffer, password: string): Promise { - return new Promise((resolve, reject) => { - pbkdf2(password, entropy, PBKDF2_ITERATIONS, PBKDF2_KEY_SIZE, PBKDF2_DIGEST_ALGORITHM, (err, xprv) => { - if (err) { - reject(err); - } - - xprv = clampScalar(xprv); - resolve(Bip32PrivateKey.fromBytes(xprv)); - }); - }); + static fromBip39Entropy(entropy: Buffer, password: string): Bip32PrivateKey { + const xprv = pbkdf2Sync( + password, + entropy, + PBKDF2_ITERATIONS, + PBKDF2_KEY_SIZE, + PBKDF2_DIGEST_ALGORITHM + ); + return Bip32PrivateKey.fromBytes(clampScalar(xprv)); } /** From f720c74fca79972779fd3a34dd53e338de5678d2 Mon Sep 17 00:00:00 2001 From: "Hong Jing (Jingles)" Date: Mon, 10 Jun 2024 21:05:43 +0800 Subject: [PATCH 2/4] fix: remove async from fromBip39Entropy func call --- packages/crypto/src/Bip32Ed25519.ts | 2 +- packages/crypto/src/strategies/SodiumBip32Ed25519.ts | 6 +++--- packages/crypto/test/bip32/Bip32PrivateKey.test.ts | 2 +- packages/crypto/test/strategies/Bip32Ed25519.test.ts | 2 +- packages/key-management/src/InMemoryKeyAgent.ts | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/crypto/src/Bip32Ed25519.ts b/packages/crypto/src/Bip32Ed25519.ts index ca5da2349b6..29d6ecbdbd0 100644 --- a/packages/crypto/src/Bip32Ed25519.ts +++ b/packages/crypto/src/Bip32Ed25519.ts @@ -29,7 +29,7 @@ export interface Bip32Ed25519 { * @param passphrase The second factor authentication passphrase for the mnemonic phrase. * @returns The secret extended key. */ - fromBip39Entropy(entropy: Buffer, passphrase: string): Promise; + fromBip39Entropy(entropy: Buffer, passphrase: string): Bip32PrivateKeyHex; /** * The function computes a public key from the provided private key. diff --git a/packages/crypto/src/strategies/SodiumBip32Ed25519.ts b/packages/crypto/src/strategies/SodiumBip32Ed25519.ts index 87221148fea..9949b5798ad 100644 --- a/packages/crypto/src/strategies/SodiumBip32Ed25519.ts +++ b/packages/crypto/src/strategies/SodiumBip32Ed25519.ts @@ -16,9 +16,9 @@ import { HexBlob } from '@cardano-sdk/util'; const EXTENDED_KEY_HEX_LENGTH = 128; export class SodiumBip32Ed25519 implements Bip32Ed25519 { - public async fromBip39Entropy(entropy: Buffer, passphrase: string): Promise { - return (await Bip32PrivateKey.fromBip39Entropy(entropy, passphrase)).hex(); - } + public fromBip39Entropy(entropy: Buffer, passphrase: string): Bip32PrivateKeyHex { + return Bip32PrivateKey.fromBip39Entropy(entropy, passphrase).hex(); + } public async getPublicKey( privateKey: Ed25519PrivateExtendedKeyHex | Ed25519PrivateNormalKeyHex diff --git a/packages/crypto/test/bip32/Bip32PrivateKey.test.ts b/packages/crypto/test/bip32/Bip32PrivateKey.test.ts index 3bc362dfbfa..f5b2297b1e0 100644 --- a/packages/crypto/test/bip32/Bip32PrivateKey.test.ts +++ b/packages/crypto/test/bip32/Bip32PrivateKey.test.ts @@ -28,7 +28,7 @@ describe('Bip32PrivateKey', () => { expect.assertions(extendedVectors.length); for (const vector of extendedVectors) { - const bip32Key = await Crypto.Bip32PrivateKey.fromBip39Entropy( + const bip32Key = Crypto.Bip32PrivateKey.fromBip39Entropy( Buffer.from(vector.bip39Entropy, 'hex'), vector.password ); diff --git a/packages/crypto/test/strategies/Bip32Ed25519.test.ts b/packages/crypto/test/strategies/Bip32Ed25519.test.ts index e3e503b79ae..07a1b7dcaff 100644 --- a/packages/crypto/test/strategies/Bip32Ed25519.test.ts +++ b/packages/crypto/test/strategies/Bip32Ed25519.test.ts @@ -23,7 +23,7 @@ const testBip32Ed25519 = (name: string, bip32Ed25519: Crypto.Bip32Ed25519) => { expect.assertions(extendedVectors.length); for (const vector of extendedVectors) { - const bip32Key = await bip32Ed25519.fromBip39Entropy(Buffer.from(vector.bip39Entropy, 'hex'), vector.password); + const bip32Key = bip32Ed25519.fromBip39Entropy(Buffer.from(vector.bip39Entropy, 'hex'), vector.password); expect(bip32Key).toBe(vector.rootKey); } }); diff --git a/packages/key-management/src/InMemoryKeyAgent.ts b/packages/key-management/src/InMemoryKeyAgent.ts index 8903ba4fc63..25304203342 100644 --- a/packages/key-management/src/InMemoryKeyAgent.ts +++ b/packages/key-management/src/InMemoryKeyAgent.ts @@ -101,7 +101,7 @@ export class InMemoryKeyAgent extends KeyAgentBase implements KeyAgent { const validMnemonic = validateMnemonic(mnemonic); if (!validMnemonic) throw new errors.InvalidMnemonicError(); const entropy = Buffer.from(mnemonicWordsToEntropy(mnemonicWords), 'hex'); - const rootPrivateKey = await dependencies.bip32Ed25519.fromBip39Entropy(entropy, mnemonic2ndFactorPassphrase); + const rootPrivateKey = dependencies.bip32Ed25519.fromBip39Entropy(entropy, mnemonic2ndFactorPassphrase); const passphrase = await getPassphraseRethrowTypedError(getPassphrase); const encryptedRootPrivateKey = await emip3encrypt(Buffer.from(rootPrivateKey, 'hex'), passphrase); const accountPrivateKey = await deriveAccountPrivateKey({ From 5a76602a33666b86699c34663d1853396bc38cc8 Mon Sep 17 00:00:00 2001 From: "Hong Jing (Jingles)" Date: Tue, 11 Jun 2024 14:20:35 +0800 Subject: [PATCH 3/4] fix: CML endpoint remove async --- packages/crypto/src/strategies/CmlBip32Ed25519.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/crypto/src/strategies/CmlBip32Ed25519.ts b/packages/crypto/src/strategies/CmlBip32Ed25519.ts index f41870b9cb3..2aa359ad38b 100644 --- a/packages/crypto/src/strategies/CmlBip32Ed25519.ts +++ b/packages/crypto/src/strategies/CmlBip32Ed25519.ts @@ -20,14 +20,14 @@ export class CmlBip32Ed25519 implements Bip32Ed25519 { constructor(CML: CardanoMultiplatformLib) { this.#CML = CML; } - - public fromBip39Entropy(entropy: Buffer, passphrase: string): Promise { + + public fromBip39Entropy(entropy: Buffer, passphrase: string): Bip32PrivateKeyHex { const hexKey = usingAutoFree((scope) => { const cmlKey = scope.manage(this.#CML.Bip32PrivateKey.from_bip39_entropy(entropy, Buffer.from(passphrase))); return cmlKey.as_bytes(); }); - return Promise.resolve(Bip32PrivateKeyHex(Buffer.from(hexKey).toString('hex'))); + return Bip32PrivateKeyHex(Buffer.from(hexKey).toString('hex')); } public getPublicKey( From f0766d85c7e3bfca1be1086601f7baf7a55d79b9 Mon Sep 17 00:00:00 2001 From: "Hong Jing (Jingles)" Date: Wed, 19 Jun 2024 21:32:13 +0800 Subject: [PATCH 4/4] fix: lint errors --- packages/crypto/src/Bip32/Bip32PrivateKey.ts | 8 +------- packages/crypto/src/strategies/CmlBip32Ed25519.ts | 2 +- packages/crypto/src/strategies/SodiumBip32Ed25519.ts | 2 +- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/crypto/src/Bip32/Bip32PrivateKey.ts b/packages/crypto/src/Bip32/Bip32PrivateKey.ts index 46422e7daab..41803919b59 100644 --- a/packages/crypto/src/Bip32/Bip32PrivateKey.ts +++ b/packages/crypto/src/Bip32/Bip32PrivateKey.ts @@ -76,13 +76,7 @@ export class Bip32PrivateKey { * @returns The secret extended key. */ static fromBip39Entropy(entropy: Buffer, password: string): Bip32PrivateKey { - const xprv = pbkdf2Sync( - password, - entropy, - PBKDF2_ITERATIONS, - PBKDF2_KEY_SIZE, - PBKDF2_DIGEST_ALGORITHM - ); + const xprv = pbkdf2Sync(password, entropy, PBKDF2_ITERATIONS, PBKDF2_KEY_SIZE, PBKDF2_DIGEST_ALGORITHM); return Bip32PrivateKey.fromBytes(clampScalar(xprv)); } diff --git a/packages/crypto/src/strategies/CmlBip32Ed25519.ts b/packages/crypto/src/strategies/CmlBip32Ed25519.ts index 2aa359ad38b..e4049cff5c0 100644 --- a/packages/crypto/src/strategies/CmlBip32Ed25519.ts +++ b/packages/crypto/src/strategies/CmlBip32Ed25519.ts @@ -20,7 +20,7 @@ export class CmlBip32Ed25519 implements Bip32Ed25519 { constructor(CML: CardanoMultiplatformLib) { this.#CML = CML; } - + public fromBip39Entropy(entropy: Buffer, passphrase: string): Bip32PrivateKeyHex { const hexKey = usingAutoFree((scope) => { const cmlKey = scope.manage(this.#CML.Bip32PrivateKey.from_bip39_entropy(entropy, Buffer.from(passphrase))); diff --git a/packages/crypto/src/strategies/SodiumBip32Ed25519.ts b/packages/crypto/src/strategies/SodiumBip32Ed25519.ts index 9949b5798ad..d719b075265 100644 --- a/packages/crypto/src/strategies/SodiumBip32Ed25519.ts +++ b/packages/crypto/src/strategies/SodiumBip32Ed25519.ts @@ -18,7 +18,7 @@ const EXTENDED_KEY_HEX_LENGTH = 128; export class SodiumBip32Ed25519 implements Bip32Ed25519 { public fromBip39Entropy(entropy: Buffer, passphrase: string): Bip32PrivateKeyHex { return Bip32PrivateKey.fromBip39Entropy(entropy, passphrase).hex(); - } + } public async getPublicKey( privateKey: Ed25519PrivateExtendedKeyHex | Ed25519PrivateNormalKeyHex