From aadc5cd3d65421e13ebd4e4d7c293ac3ece5e178 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Thu, 13 May 2021 23:02:00 -0400 Subject: [PATCH] Added convenience method for HD path derivation. --- packages/ethers/src.ts/utils.ts | 3 ++- packages/hdnode/src.ts/index.ts | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/ethers/src.ts/utils.ts b/packages/ethers/src.ts/utils.ts index 6b217b14c..00d40836e 100644 --- a/packages/ethers/src.ts/utils.ts +++ b/packages/ethers/src.ts/utils.ts @@ -6,7 +6,7 @@ import * as base64 from "@ethersproject/base64"; import { Base58 as base58 } from "@ethersproject/basex"; import { arrayify, concat, hexConcat, hexDataSlice, hexDataLength, hexlify, hexStripZeros, hexValue, hexZeroPad, isBytes, isBytesLike, isHexString, joinSignature, zeroPad, splitSignature, stripZeros } from "@ethersproject/bytes"; import { _TypedDataEncoder, hashMessage, id, isValidName, namehash } from "@ethersproject/hash"; -import { defaultPath, entropyToMnemonic, HDNode, isValidMnemonic, mnemonicToEntropy, mnemonicToSeed } from "@ethersproject/hdnode"; +import { defaultPath, entropyToMnemonic, getAccountPath, HDNode, isValidMnemonic, mnemonicToEntropy, mnemonicToSeed } from "@ethersproject/hdnode"; import { getJsonWalletAddress } from "@ethersproject/json-wallets"; import { keccak256 } from "@ethersproject/keccak256"; import { Logger } from "@ethersproject/logger"; @@ -165,6 +165,7 @@ export { verifyMessage, verifyTypedData, + getAccountPath, mnemonicToEntropy, entropyToMnemonic, isValidMnemonic, diff --git a/packages/hdnode/src.ts/index.ts b/packages/hdnode/src.ts/index.ts index 8c94416a9..f3dba2551 100644 --- a/packages/hdnode/src.ts/index.ts +++ b/packages/hdnode/src.ts/index.ts @@ -403,3 +403,10 @@ export function isValidMnemonic(mnemonic: string, wordlist?: Wordlist): boolean } catch (error) { } return false; } + +export function getAccountPath(index: number): string { + if (typeof(index) !== "number" || index < 0 || index >= HardenedBit || index % 1) { + logger.throwArgumentError("invalid account index", "index", index); + } + return `m/44'/60'/${ index }'/0/0`; +}