Skip to content

Commit

Permalink
chore: allow hd path for the privateKey class
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Nov 7, 2024
1 parent c55f0f6 commit b5d7e58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 5 additions & 1 deletion packages/sdk-ts/src/core/accounts/PrivateKey.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 12 additions & 4 deletions packages/sdk-ts/src/core/accounts/PrivateKey.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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))
}

/**
Expand Down

0 comments on commit b5d7e58

Please sign in to comment.