diff --git a/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts b/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts index 1f63bc4e4..6072a8c79 100644 --- a/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts +++ b/packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts @@ -1,9 +1,13 @@ import { generateArbitrarySignDoc } from '../tx' import { PrivateKey } from './PrivateKey' -const pk = 'f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3' +const pk = process.env.TEST_PRIVATE_KEY as string const seedPhase = process.env.TEST_SEED_PHASE as string +if (!pk || !seedPhase) { + throw new Error('TEST_PRIVATE_KEY or TEST_SEED_PHASE is not set') +} + describe('PrivateKey', () => { it('returns the correct address derived from a mnemonic', () => { const privateKey = PrivateKey.fromMnemonic(seedPhase) diff --git a/packages/sdk-ts/src/core/accounts/PrivateKey.ts b/packages/sdk-ts/src/core/accounts/PrivateKey.ts index dee9906d0..267a28bbf 100644 --- a/packages/sdk-ts/src/core/accounts/PrivateKey.ts +++ b/packages/sdk-ts/src/core/accounts/PrivateKey.ts @@ -1,12 +1,15 @@ import { generateMnemonic } from 'bip39' -import { Wallet } from 'ethers' +import { Wallet, HDNodeWallet } from 'ethers' import secp256k1 from 'secp256k1' import keccak256 from 'keccak256' import { PublicKey } from './PublicKey' import { Address } from './Address' import * as BytesUtils from '@ethersproject/bytes' import { signTypedData, SignTypedDataVersion } from '@metamask/eth-sig-util' -import { recoverTypedSignaturePubKey } from '../../utils' +import { + DEFAULT_DERIVATION_PATH, + recoverTypedSignaturePubKey, +} from '../../utils' import { CosmosTxV1Beta1Tx, InjectiveTypesV1Beta1TxExt, @@ -49,8 +52,13 @@ export class PrivateKey { * @param {string|undefined} path the HD path that follows the BIP32 standard (optional) * @returns {PrivateKey} Initialized PrivateKey object */ - static fromMnemonic(words: string): PrivateKey { - return new PrivateKey(new Wallet(Wallet.fromPhrase(words).signingKey)) + static fromMnemonic( + words: string, + path: string = DEFAULT_DERIVATION_PATH, + ): PrivateKey { + const hdNodeWallet = HDNodeWallet.fromPhrase(words, undefined, path) + + return new PrivateKey(new Wallet(hdNodeWallet.privateKey)) } /**