diff --git a/src/components/sendForm/SendForm.tsx b/src/components/sendForm/SendForm.tsx index 91a344a84..f1318583f 100644 --- a/src/components/sendForm/SendForm.tsx +++ b/src/components/sendForm/SendForm.tsx @@ -2,9 +2,7 @@ import { TezosNetwork } from "@airgap/tezos"; import { useToast } from "@chakra-ui/react"; import { Estimate } from "@taquito/taquito"; import { useState } from "react"; -import { AccountType } from "../../types/Account"; -import { UmamiEncrypted } from "../../types/UmamiEncrypted"; -import { useAccounts } from "../../utils/hooks/accountHooks"; +import { useGetOwnedAccount } from "../../utils/hooks/accountHooks"; import { useSelectedNetwork } from "../../utils/hooks/assetsHooks"; import { estimateDelegation, @@ -54,21 +52,9 @@ const makeSimulation = ( return Promise.reject(`Unrecognized type!`); }; -export const useGetPkAndEsk = () => { - const accounts = useAccounts(); - - return (pkh: string) => { - const account = accounts.find((a) => a.pkh === pkh); - if (!account) { - throw new Error("No account found"); - } - - if (account.type === AccountType.MNEMONIC) { - return { esk: account.esk, pk: account.pk }; - } else { - return { esk: undefined, pk: account.pk }; - } - }; +export const useGetPk = () => { + const getAccount = useGetOwnedAccount(); + return (pkh: string) => getAccount(pkh).pk; }; export const SendForm = ({ @@ -82,14 +68,13 @@ export const SendForm = ({ }) => { const network = useSelectedNetwork(); const toast = useToast(); - const getPkAndEsk = useGetPkAndEsk(); + const getPk = useGetPk(); const [isLoading, setIsLoading] = useState(false); const [transferValues, setTransferValues] = useState<{ transaction: TransactionValues; estimate: Estimate; - esk: UmamiEncrypted | undefined; }>(); const [hash, setHash] = useState(); @@ -99,18 +84,14 @@ export const SendForm = ({ try { const sender = transaction.values.sender; - const { pk, esk } = getPkAndEsk(sender); + const pk = getPk(sender); // pk needed for simulation const estimate = await makeSimulation(transaction, pk, network); - // esk needed for real transfer - // If esk is undefined we are using SSO. Hacky. - // TOOD implement better solution setTransferValues({ transaction, estimate, - esk, }); } catch (error: any) { toast({ title: "Invalid transaction", description: error.message }); diff --git a/src/components/sendForm/steps/SubmitStep.tsx b/src/components/sendForm/steps/SubmitStep.tsx index 44f012fad..ad72fe83a 100644 --- a/src/components/sendForm/steps/SubmitStep.tsx +++ b/src/components/sendForm/steps/SubmitStep.tsx @@ -21,8 +21,9 @@ import { isValid } from "date-fns"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { GoogleAuth } from "../../../GoogleAuth"; -import { UmamiEncrypted } from "../../../types/UmamiEncrypted"; +import { AccountType } from "../../../types/Account"; import { decrypt } from "../../../utils/aes"; +import { useGetOwnedAccount } from "../../../utils/hooks/accountHooks"; import { mutezToTezNumber, prettyTezAmount, @@ -61,7 +62,8 @@ const makeTransfer = ( ); } - return Promise.reject(`Unrecognized type!`); + const error: never = t; + throw new Error(error); }; const renderSubTotal = (t: TransactionValues) => { @@ -85,27 +87,29 @@ export const RecapDisplay: React.FC<{ recap: { transaction: TransactionValues; estimate: Estimate; - esk: UmamiEncrypted | undefined; }; onSucces: (hash: string) => void; -}> = ({ - recap: { estimate, transaction: transfer, esk }, - network, - onSucces, -}) => { +}> = ({ recap: { estimate, transaction: transfer }, network, onSucces }) => { const isTez = transfer.type === "tez"; const isDelegation = transfer.type === "delegation"; const nft = transfer.type === "nft" ? transfer.data : undefined; const renderAccountTile = useRenderAccountSmallTile(); const renderBakerTile = useRenderBakerSmallTile(); + const getAccount = useGetOwnedAccount(); const { register, handleSubmit } = useForm<{ password: string }>(); const toast = useToast(); const [isLoading, setIsLoading] = useState(false); - const isGoogleSSO = !esk; + const signerAccount = getAccount(transfer.values.sender); + + const isGoogleSSO = signerAccount.type === AccountType.SOCIAL; const onSubmitGoogleSSO = async (sk: string) => { + if (signerAccount.type === AccountType.MNEMONIC) { + throw new Error(`Wrong signing method called`); + } + setIsLoading(true); try { const result = await makeTransfer(transfer, sk, network); @@ -118,14 +122,15 @@ export const RecapDisplay: React.FC<{ setIsLoading(false); }; + // TODO remove duplication const onSubmitNominal = async ({ password }: { password: string }) => { - if (!esk) { - throw new Error("esk required"); + if (signerAccount.type === AccountType.SOCIAL) { + throw new Error(`Wrong signing method called`); } setIsLoading(true); try { - const sk = await decrypt(esk, password); + const sk = await decrypt(signerAccount.esk, password); const result = await makeTransfer(transfer, sk, network); onSucces(result.hash); diff --git a/src/utils/hooks/accountHooks.ts b/src/utils/hooks/accountHooks.ts index c33920d33..ba7a1d60c 100644 --- a/src/utils/hooks/accountHooks.ts +++ b/src/utils/hooks/accountHooks.ts @@ -27,3 +27,14 @@ export const useReset = () => { dispatch(accountsSlice.actions.reset()); }; }; + +export const useGetOwnedAccount = () => { + const accounts = useAccounts(); + return (pkh: string) => { + const account = accounts.find((a) => a.pkh === pkh); + if (!account) { + throw new Error(`You do not ownn account:${pkh}`); + } + return account; + }; +};