Skip to content

Commit 0f8c009

Browse files
committed
fix: use network as derivation path
1 parent 1c427db commit 0f8c009

File tree

12 files changed

+62
-20
lines changed

12 files changed

+62
-20
lines changed

background/constants/networks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const ROOTSTOCK: EVMNetwork = {
2424
name: "Rootstock",
2525
baseAsset: RBTC,
2626
chainID: "30",
27+
derivationPath: "m/44'/137'/0'/0",
2728
family: "EVM",
2829
coingeckoPlatformID: "rootstock",
2930
}

background/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,13 +1083,14 @@ export default class Main extends BaseService<never> {
10831083
})
10841084
})
10851085

1086-
keyringSliceEmitter.on("generateNewKeyring", async () => {
1086+
keyringSliceEmitter.on("generateNewKeyring", async (path) => {
10871087
// TODO move unlocking to a reasonable place in the initialization flow
10881088
const generated: {
10891089
id: string
10901090
mnemonic: string[]
10911091
} = await this.keyringService.generateNewKeyring(
1092-
KeyringTypes.mnemonicBIP39S256
1092+
KeyringTypes.mnemonicBIP39S256,
1093+
path
10931094
)
10941095

10951096
this.store.dispatch(setKeyringToVerify(generated))

background/networks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type Network = {
3535
family: NetworkFamily
3636
chainID?: string
3737
coingeckoPlatformID?: string
38+
derivationPath?: string
3839
}
3940

4041
/**

background/redux-slices/keyrings.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type Events = {
3232
createPassword: string
3333
unlockKeyrings: string
3434
lockKeyrings: never
35-
generateNewKeyring: never
35+
generateNewKeyring: string | undefined
3636
deriveAddress: string
3737
importKeyring: ImportKeyring
3838
}
@@ -134,8 +134,8 @@ export default keyringsSlice.reducer
134134
// Async thunk to bubble the generateNewKeyring action from store to emitter.
135135
export const generateNewKeyring = createBackgroundAsyncThunk(
136136
"keyrings/generateNewKeyring",
137-
async () => {
138-
await emitter.emit("generateNewKeyring")
137+
async (path?: string) => {
138+
await emitter.emit("generateNewKeyring", path)
139139
}
140140
)
141141

background/redux-slices/selectors/accountsSelectors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ export type AccountTotal = AddressOnNetwork & {
288288
// FIXME Add `categoryFor(accountSigner): string` utility function to
289289
// FIXME generalize beyond keyrings.
290290
keyringId: string | null
291+
path: string | null
291292
accountSigner: AccountSigner
292293
name?: string
293294
avatarURL?: string
@@ -373,6 +374,7 @@ function getNetworkAccountTotalsByCategory(
373374

374375
const accountSigner = accountSignersByAddress[address]
375376
const keyringId = keyringsByAddresses[address]?.id
377+
const path = keyringsByAddresses[address]?.path
376378

377379
const accountType = getAccountType(
378380
address,
@@ -387,6 +389,7 @@ function getNetworkAccountTotalsByCategory(
387389
shortenedAddress,
388390
accountType,
389391
keyringId,
392+
path,
390393
accountSigner,
391394
}
392395
}
@@ -397,6 +400,7 @@ function getNetworkAccountTotalsByCategory(
397400
shortenedAddress,
398401
accountType,
399402
keyringId,
403+
path,
400404
accountSigner,
401405
name: accountData.ens.name ?? accountData.defaultName,
402406
avatarURL: accountData.ens.avatarURL ?? accountData.defaultAvatar,

background/services/keyring/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const MAX_OUTSIDE_IDLE_TIME = 60 * MINUTE
2828
export type Keyring = {
2929
type: KeyringTypes
3030
id: string | null
31+
path: string | null
3132
addresses: string[]
3233
}
3334

@@ -301,7 +302,8 @@ export default class KeyringService extends BaseService<Events> {
301302
* accessed at generation time through this return value.
302303
*/
303304
async generateNewKeyring(
304-
type: KeyringTypes
305+
type: KeyringTypes,
306+
path?: string
305307
): Promise<{ id: string; mnemonic: string[] }> {
306308
this.requireUnlocked()
307309

@@ -311,7 +313,13 @@ export default class KeyringService extends BaseService<Events> {
311313
)
312314
}
313315

314-
const newKeyring = new HDKeyring({ strength: 256 })
316+
const options: { strength: number; path?: string } = { strength: 256 }
317+
318+
if (path) {
319+
options.path = path
320+
}
321+
322+
const newKeyring = new HDKeyring(options)
315323

316324
const { mnemonic } = newKeyring.serializeSync()
317325

@@ -382,6 +390,7 @@ export default class KeyringService extends BaseService<Events> {
382390
.filter((address) => this.#hiddenAccounts[address] !== true),
383391
],
384392
id: kr.id,
393+
path: kr.path,
385394
}))
386395
}
387396

ui/components/AccountsNotificationPanel/AccountsNotificationPanelAccounts.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
updateSignerTitle,
1212
} from "@tallyho/tally-background/redux-slices/ui"
1313
import { deriveAddress } from "@tallyho/tally-background/redux-slices/keyrings"
14+
import { ROOTSTOCK } from "@tallyho/tally-background/constants"
1415
import {
1516
AccountTotal,
1617
selectCurrentNetworkAccountTotalsByCategory,
@@ -77,12 +78,14 @@ function WalletTypeHeader({
7778
accountType,
7879
onClickAddAddress,
7980
walletNumber,
81+
path,
8082
accountSigner,
8183
}: {
8284
accountType: AccountType
8385
onClickAddAddress?: () => void
8486
accountSigner: AccountSigner
8587
walletNumber?: number
88+
path?: string | null
8689
}) {
8790
const { t } = useTranslation()
8891
const { title, icon } = walletTypeDetails[accountType]
@@ -103,10 +106,13 @@ function WalletTypeHeader({
103106
const sectionTitle = useMemo(() => {
104107
if (accountType === AccountType.ReadOnly) return title
105108

106-
if (sectionCustomName) return sectionCustomName
109+
let networkName = "" // Only for Rootstock
110+
if (path === ROOTSTOCK.derivationPath) networkName = `(${ROOTSTOCK.name})`
107111

108-
return `${title} ${walletNumber}`
109-
}, [accountType, title, sectionCustomName, walletNumber])
112+
if (sectionCustomName) return `${sectionCustomName} ${networkName}`
113+
114+
return `${title} ${walletNumber} ${networkName}`
115+
}, [accountType, title, sectionCustomName, walletNumber, path])
110116

111117
const history = useHistory()
112118
const areKeyringsUnlocked = useAreKeyringsUnlocked(false)
@@ -339,6 +345,7 @@ export default function AccountsNotificationPanelAccounts({
339345
<WalletTypeHeader
340346
accountType={accountType}
341347
walletNumber={idx + 1}
348+
path={accountTotalsByKeyringId[0].path}
342349
accountSigner={
343350
accountTotalsByKeyringId[0].accountSigner
344351
}

ui/pages/Onboarding/OnboardingImportMetamask.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isValidMnemonic } from "@ethersproject/hdnode"
55
import classNames from "classnames"
66
import { FeatureFlags, isEnabled } from "@tallyho/tally-background/features"
77
import { useTranslation } from "react-i18next"
8+
import { selectCurrentNetwork } from "@tallyho/tally-background/redux-slices/selectors"
89
import SharedButton from "../../components/Shared/SharedButton"
910
import SharedBackButton from "../../components/Shared/SharedBackButton"
1011
import OnboardingDerivationPathSelect from "../../components/Onboarding/OnboardingDerivationPathSelect"
@@ -105,12 +106,15 @@ export default function OnboardingImportMetamask(props: Props): ReactElement {
105106
keyPrefix: "onboarding.addWallet.importExistingWallet",
106107
})
107108
const { nextPage } = props
109+
const selectedNetwork = useBackgroundSelector(selectCurrentNetwork)
108110

109111
const areKeyringsUnlocked = useAreKeyringsUnlocked(true)
110112

111113
const [recoveryPhrase, setRecoveryPhrase] = useState("")
112114
const [errorMessage, setErrorMessage] = useState("")
113-
const [path, setPath] = useState<string>("m/44'/60'/0'/0")
115+
const [path, setPath] = useState<string>(
116+
selectedNetwork.derivationPath ?? "m/44'/60'/0'/0"
117+
)
114118
const [isImporting, setIsImporting] = useState(false)
115119

116120
const dispatch = useBackgroundDispatch()

ui/pages/Onboarding/OnboardingInterstitialCreatePhrase.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
import React, { ReactElement, useCallback, useEffect } from "react"
22
import { generateNewKeyring } from "@tallyho/tally-background/redux-slices/keyrings"
3+
import { selectCurrentNetwork } from "@tallyho/tally-background/redux-slices/selectors"
34
import { useHistory } from "react-router-dom"
4-
import { useBackgroundDispatch, useAreKeyringsUnlocked } from "../../hooks"
5+
import {
6+
useBackgroundDispatch,
7+
useAreKeyringsUnlocked,
8+
useBackgroundSelector,
9+
} from "../../hooks"
510

611
export default function OnboardingInterstitialCreatePhrase(): ReactElement {
712
const dispatch = useBackgroundDispatch()
13+
const selectedNetwork = useBackgroundSelector(selectCurrentNetwork)
814
const history = useHistory()
915

1016
const areKeyringsUnlocked = useAreKeyringsUnlocked(true)
1117

1218
const generateThenContinue = useCallback(
1319
async function generateThenContinue() {
1420
if (areKeyringsUnlocked) {
15-
await dispatch(generateNewKeyring())
21+
await dispatch(generateNewKeyring(selectedNetwork.derivationPath))
1622
history.push("/onboarding/save-seed")
1723
}
1824
},
19-
[areKeyringsUnlocked, dispatch, history]
25+
[areKeyringsUnlocked, dispatch, history, selectedNetwork]
2026
)
2127

2228
useEffect(() => {

ui/pages/Onboarding/Tabbed/ImportSeed.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Redirect, useHistory } from "react-router-dom"
44
import { isValidMnemonic } from "@ethersproject/hdnode"
55
import { FeatureFlags, isEnabled } from "@tallyho/tally-background/features"
66
import { useTranslation } from "react-i18next"
7+
import { selectCurrentNetwork } from "@tallyho/tally-background/redux-slices/selectors"
78
import SharedButton from "../../../components/Shared/SharedButton"
89
import OnboardingDerivationPathSelect from "../../../components/Onboarding/OnboardingDerivationPathSelect"
910
import {
@@ -19,12 +20,14 @@ type Props = {
1920

2021
export default function ImportSeed(props: Props): ReactElement {
2122
const { nextPage } = props
22-
23+
const selectedNetwork = useBackgroundSelector(selectCurrentNetwork)
2324
const areKeyringsUnlocked = useAreKeyringsUnlocked(false)
2425

2526
const [recoveryPhrase, setRecoveryPhrase] = useState("")
2627
const [errorMessage, setErrorMessage] = useState("")
27-
const [path, setPath] = useState<string>("m/44'/60'/0'/0")
28+
const [path, setPath] = useState<string>(
29+
selectedNetwork.derivationPath ?? "m/44'/60'/0'/0"
30+
)
2831
const [isImporting, setIsImporting] = useState(false)
2932

3033
const { t } = useTranslation("translation", {

0 commit comments

Comments
 (0)