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

[Rootstock] Use network as derivation path #2673

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions background/constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const ROOTSTOCK: EVMNetwork = {
name: "Rootstock",
baseAsset: RBTC,
chainID: "30",
derivationPath: "m/44'/137'/0'/0",
family: "EVM",
coingeckoPlatformID: "rootstock",
}
Expand Down
5 changes: 3 additions & 2 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1083,13 +1083,14 @@ export default class Main extends BaseService<never> {
})
})

keyringSliceEmitter.on("generateNewKeyring", async () => {
keyringSliceEmitter.on("generateNewKeyring", async (path) => {
// TODO move unlocking to a reasonable place in the initialization flow
const generated: {
id: string
mnemonic: string[]
} = await this.keyringService.generateNewKeyring(
KeyringTypes.mnemonicBIP39S256
KeyringTypes.mnemonicBIP39S256,
path
)

this.store.dispatch(setKeyringToVerify(generated))
Expand Down
1 change: 1 addition & 0 deletions background/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type Network = {
family: NetworkFamily
chainID?: string
coingeckoPlatformID?: string
derivationPath?: string
}

/**
Expand Down
6 changes: 3 additions & 3 deletions background/redux-slices/keyrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type Events = {
createPassword: string
unlockKeyrings: string
lockKeyrings: never
generateNewKeyring: never
generateNewKeyring: string | undefined
deriveAddress: string
importKeyring: ImportKeyring
}
Expand Down Expand Up @@ -134,8 +134,8 @@ export default keyringsSlice.reducer
// Async thunk to bubble the generateNewKeyring action from store to emitter.
export const generateNewKeyring = createBackgroundAsyncThunk(
"keyrings/generateNewKeyring",
async () => {
await emitter.emit("generateNewKeyring")
async (path?: string) => {
await emitter.emit("generateNewKeyring", path)
}
)

Expand Down
4 changes: 4 additions & 0 deletions background/redux-slices/selectors/accountsSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export type AccountTotal = AddressOnNetwork & {
// FIXME Add `categoryFor(accountSigner): string` utility function to
// FIXME generalize beyond keyrings.
keyringId: string | null
path: string | null
accountSigner: AccountSigner
name?: string
avatarURL?: string
Expand Down Expand Up @@ -373,6 +374,7 @@ function getNetworkAccountTotalsByCategory(

const accountSigner = accountSignersByAddress[address]
const keyringId = keyringsByAddresses[address]?.id
const path = keyringsByAddresses[address]?.path

const accountType = getAccountType(
address,
Expand All @@ -387,6 +389,7 @@ function getNetworkAccountTotalsByCategory(
shortenedAddress,
accountType,
keyringId,
path,
accountSigner,
}
}
Expand All @@ -397,6 +400,7 @@ function getNetworkAccountTotalsByCategory(
shortenedAddress,
accountType,
keyringId,
path,
accountSigner,
name: accountData.ens.name ?? accountData.defaultName,
avatarURL: accountData.ens.avatarURL ?? accountData.defaultAvatar,
Expand Down
13 changes: 11 additions & 2 deletions background/services/keyring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const MAX_OUTSIDE_IDLE_TIME = 60 * MINUTE
export type Keyring = {
type: KeyringTypes
id: string | null
path: string | null
addresses: string[]
}

Expand Down Expand Up @@ -301,7 +302,8 @@ export default class KeyringService extends BaseService<Events> {
* accessed at generation time through this return value.
*/
async generateNewKeyring(
type: KeyringTypes
type: KeyringTypes,
path?: string
): Promise<{ id: string; mnemonic: string[] }> {
this.requireUnlocked()

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

const newKeyring = new HDKeyring({ strength: 256 })
const options: { strength: number; path?: string } = { strength: 256 }

if (path) {
options.path = path
}

const newKeyring = new HDKeyring(options)

const { mnemonic } = newKeyring.serializeSync()

Expand Down Expand Up @@ -382,6 +390,7 @@ export default class KeyringService extends BaseService<Events> {
.filter((address) => this.#hiddenAccounts[address] !== true),
],
id: kr.id,
path: kr.path,
}))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
updateSignerTitle,
} from "@tallyho/tally-background/redux-slices/ui"
import { deriveAddress } from "@tallyho/tally-background/redux-slices/keyrings"
import { ROOTSTOCK } from "@tallyho/tally-background/constants"
import {
AccountTotal,
selectCurrentNetworkAccountTotalsByCategory,
Expand Down Expand Up @@ -77,12 +78,14 @@ function WalletTypeHeader({
accountType,
onClickAddAddress,
walletNumber,
path,
accountSigner,
}: {
accountType: AccountType
onClickAddAddress?: () => void
accountSigner: AccountSigner
walletNumber?: number
path?: string | null
}) {
const { t } = useTranslation()
const { title, icon } = walletTypeDetails[accountType]
Expand All @@ -103,10 +106,13 @@ function WalletTypeHeader({
const sectionTitle = useMemo(() => {
if (accountType === AccountType.ReadOnly) return title

if (sectionCustomName) return sectionCustomName
let networkName = "" // Only for Rootstock
if (path === ROOTSTOCK.derivationPath) networkName = `(${ROOTSTOCK.name})`

return `${title} ${walletNumber}`
}, [accountType, title, sectionCustomName, walletNumber])
if (sectionCustomName) return `${sectionCustomName} ${networkName}`

return `${title} ${walletNumber} ${networkName}`
}, [accountType, title, sectionCustomName, walletNumber, path])

const history = useHistory()
const areKeyringsUnlocked = useAreKeyringsUnlocked(false)
Expand Down Expand Up @@ -339,6 +345,7 @@ export default function AccountsNotificationPanelAccounts({
<WalletTypeHeader
accountType={accountType}
walletNumber={idx + 1}
path={accountTotalsByKeyringId[0].path}
accountSigner={
accountTotalsByKeyringId[0].accountSigner
}
Expand Down
6 changes: 5 additions & 1 deletion ui/pages/Onboarding/OnboardingImportMetamask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isValidMnemonic } from "@ethersproject/hdnode"
import classNames from "classnames"
import { FeatureFlags, isEnabled } from "@tallyho/tally-background/features"
import { useTranslation } from "react-i18next"
import { selectCurrentNetwork } from "@tallyho/tally-background/redux-slices/selectors"
import SharedButton from "../../components/Shared/SharedButton"
import SharedBackButton from "../../components/Shared/SharedBackButton"
import OnboardingDerivationPathSelect from "../../components/Onboarding/OnboardingDerivationPathSelect"
Expand Down Expand Up @@ -105,12 +106,15 @@ export default function OnboardingImportMetamask(props: Props): ReactElement {
keyPrefix: "onboarding.addWallet.importExistingWallet",
})
const { nextPage } = props
const selectedNetwork = useBackgroundSelector(selectCurrentNetwork)

const areKeyringsUnlocked = useAreKeyringsUnlocked(true)

const [recoveryPhrase, setRecoveryPhrase] = useState("")
const [errorMessage, setErrorMessage] = useState("")
const [path, setPath] = useState<string>("m/44'/60'/0'/0")
const [path, setPath] = useState<string>(
selectedNetwork.derivationPath ?? "m/44'/60'/0'/0"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make new variables in background/constants... for these derivation paths (so we are not using plain strings)?

)
const [isImporting, setIsImporting] = useState(false)

const dispatch = useBackgroundDispatch()
Expand Down
12 changes: 9 additions & 3 deletions ui/pages/Onboarding/OnboardingInterstitialCreatePhrase.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import React, { ReactElement, useCallback, useEffect } from "react"
import { generateNewKeyring } from "@tallyho/tally-background/redux-slices/keyrings"
import { selectCurrentNetwork } from "@tallyho/tally-background/redux-slices/selectors"
import { useHistory } from "react-router-dom"
import { useBackgroundDispatch, useAreKeyringsUnlocked } from "../../hooks"
import {
useBackgroundDispatch,
useAreKeyringsUnlocked,
useBackgroundSelector,
} from "../../hooks"

export default function OnboardingInterstitialCreatePhrase(): ReactElement {
const dispatch = useBackgroundDispatch()
const selectedNetwork = useBackgroundSelector(selectCurrentNetwork)
const history = useHistory()

const areKeyringsUnlocked = useAreKeyringsUnlocked(true)

const generateThenContinue = useCallback(
async function generateThenContinue() {
if (areKeyringsUnlocked) {
await dispatch(generateNewKeyring())
await dispatch(generateNewKeyring(selectedNetwork.derivationPath))
history.push("/onboarding/save-seed")
}
},
[areKeyringsUnlocked, dispatch, history]
[areKeyringsUnlocked, dispatch, history, selectedNetwork]
)

useEffect(() => {
Expand Down
7 changes: 5 additions & 2 deletions ui/pages/Onboarding/Tabbed/ImportSeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Redirect, useHistory } from "react-router-dom"
import { isValidMnemonic } from "@ethersproject/hdnode"
import { FeatureFlags, isEnabled } from "@tallyho/tally-background/features"
import { useTranslation } from "react-i18next"
import { selectCurrentNetwork } from "@tallyho/tally-background/redux-slices/selectors"
import SharedButton from "../../../components/Shared/SharedButton"
import OnboardingDerivationPathSelect from "../../../components/Onboarding/OnboardingDerivationPathSelect"
import {
Expand All @@ -19,12 +20,14 @@ type Props = {

export default function ImportSeed(props: Props): ReactElement {
const { nextPage } = props

const selectedNetwork = useBackgroundSelector(selectCurrentNetwork)
const areKeyringsUnlocked = useAreKeyringsUnlocked(false)

const [recoveryPhrase, setRecoveryPhrase] = useState("")
const [errorMessage, setErrorMessage] = useState("")
const [path, setPath] = useState<string>("m/44'/60'/0'/0")
const [path, setPath] = useState<string>(
selectedNetwork.derivationPath ?? "m/44'/60'/0'/0"
)
const [isImporting, setIsImporting] = useState(false)

const { t } = useTranslation("translation", {
Expand Down
8 changes: 5 additions & 3 deletions ui/pages/Onboarding/Tabbed/NewSeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useHistory,
useRouteMatch,
} from "react-router-dom"
import { selectCurrentNetwork } from "@tallyho/tally-background/redux-slices/selectors"
import OnboardingStepsIndicator from "../../../components/Onboarding/OnboardingStepsIndicator"
import {
useAreKeyringsUnlocked,
Expand Down Expand Up @@ -60,16 +61,17 @@ export default function NewSeed(): ReactElement {
const mnemonic = useBackgroundSelector(
(state) => state.keyrings.keyringToVerify?.mnemonic
)
const selectedNetwork = useBackgroundSelector(selectCurrentNetwork)

const areKeyringsUnlocked = useAreKeyringsUnlocked(false)

const history = useHistory()
const { path } = useRouteMatch()

const showNewSeedPhrase = () => {
dispatch(generateNewKeyring()).then(() =>
history.push(NewSeedRoutes.REVIEW_SEED)
)
dispatch(
generateNewKeyring(selectedNetwork.derivationPath ?? "m/44'/60'/0'/0")
).then(() => history.push(NewSeedRoutes.REVIEW_SEED))
}

const showSeedVerification = () => {
Expand Down
6 changes: 5 additions & 1 deletion ui/pages/Onboarding/VerifySeed/VerifySeedSuccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import React, { ReactElement } from "react"
import { useHistory } from "react-router-dom"
import { importKeyring } from "@tallyho/tally-background/redux-slices/keyrings"
import { useTranslation } from "react-i18next"
import { selectCurrentNetwork } from "@tallyho/tally-background/redux-slices/selectors"
import SharedButton from "../../../components/Shared/SharedButton"
import { useBackgroundDispatch } from "../../../hooks"
import { useBackgroundDispatch, useBackgroundSelector } from "../../../hooks"
import { OnboardingBox, OnboardingMessageHeader } from "../styles"

function VerifySeedSuccess({
Expand All @@ -17,6 +18,8 @@ function VerifySeedSuccess({
keyPrefix: "onboarding.seedVerification",
})
const dispatch = useBackgroundDispatch()
const selectedNetwork = useBackgroundSelector(selectCurrentNetwork)

const history = useHistory()

return (
Expand All @@ -40,6 +43,7 @@ function VerifySeedSuccess({
importKeyring({
mnemonic: mnemonic.join(" "),
source: "internal",
path: selectedNetwork.derivationPath ?? "m/44'/60'/0'/0",
})
)
history.push(nextPage)
Expand Down