Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove async from fromBip39Entropy #1326

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions packages/crypto/src/Bip32/Bip32PrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Bip32PrivateKey> {
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint error here

entropy,
PBKDF2_ITERATIONS,
PBKDF2_KEY_SIZE,
PBKDF2_DIGEST_ALGORITHM
);
return Bip32PrivateKey.fromBytes(clampScalar(xprv));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/src/Bip32Ed25519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bip32PrivateKeyHex>;
fromBip39Entropy(entropy: Buffer, passphrase: string): Bip32PrivateKeyHex;

/**
* The function computes a public key from the provided private key.
Expand Down
6 changes: 3 additions & 3 deletions packages/crypto/src/strategies/SodiumBip32Ed25519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bip32PrivateKeyHex> {
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
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/test/bip32/Bip32PrivateKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/test/strategies/Bip32Ed25519.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/key-management/src/InMemoryKeyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading