Skip to content

Commit

Permalink
fix: use network as derivation path
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsan-javaiid committed Nov 24, 2022
1 parent af3ce39 commit df4944c
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 16 deletions.
1 change: 1 addition & 0 deletions background/constants/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 @@ -1038,13 +1038,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 @@ -33,6 +33,7 @@ export type Network = {
baseAsset: NetworkBaseAsset & CoinGeckoAsset
family: NetworkFamily
chainID?: string
derivationPath?: string
coingeckoPlatformID: 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 @@ -30,7 +30,7 @@ export type Events = {
createPassword: string
unlockKeyrings: string
lockKeyrings: never
generateNewKeyring: never
generateNewKeyring: string | undefined
deriveAddress: string
importKeyring: ImportKeyring
}
Expand Down Expand Up @@ -132,8 +132,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
11 changes: 9 additions & 2 deletions background/services/keyring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,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 +312,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
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"
)
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 { useHistory } from "react-router-dom"
import { isValidMnemonic } from "@ethersproject/hdnode"
import classNames from "classnames"
import { FeatureFlags, isEnabled } from "@tallyho/tally-background/features"
import { selectCurrentNetwork } from "@tallyho/tally-background/redux-slices/selectors"
import SharedButton from "../../../components/Shared/SharedButton"
import OnboardingDerivationPathSelect from "../../../components/Onboarding/OnboardingDerivationPathSelect"
import {
Expand Down Expand Up @@ -97,12 +98,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 dispatch = useBackgroundDispatch()
Expand Down
6 changes: 4 additions & 2 deletions ui/pages/Onboarding/Tabbed/SetPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
selectDefaultWallet,
} from "@tallyho/tally-background/redux-slices/ui"
import { useHistory } from "react-router-dom"
import { selectCurrentNetwork } from "@tallyho/tally-background/redux-slices/selectors"
import {
useBackgroundDispatch,
useAreKeyringsUnlocked,
Expand All @@ -32,15 +33,16 @@ export default function SetPassword({

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

const dispatch = useBackgroundDispatch()

useEffect(() => {
if (areKeyringsUnlocked) {
dispatch(generateNewKeyring())
dispatch(generateNewKeyring(selectedNetwork.derivationPath))
history.push(nextPage)
}
}, [areKeyringsUnlocked, dispatch, history, nextPage])
}, [areKeyringsUnlocked, dispatch, history, nextPage, selectedNetwork])

const validatePassword = (): boolean => {
if (password.length < 8) {
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

0 comments on commit df4944c

Please sign in to comment.