Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
leoparis89 committed Apr 12, 2023
1 parent 4114218 commit f4368f1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
31 changes: 6 additions & 25 deletions src/components/sendForm/SendForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = ({
Expand All @@ -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<string>();
Expand All @@ -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 });
Expand Down
44 changes: 33 additions & 11 deletions src/components/sendForm/steps/SubmitStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import { isValid } from "date-fns";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { GoogleAuth } from "../../../GoogleAuth";
import { AccountType } from "../../../types/Account";
import { UmamiEncrypted } from "../../../types/UmamiEncrypted";
import { decrypt } from "../../../utils/aes";
import { useGetOwnedAccount } from "../../../utils/hooks/accountHooks";
import {
mutezToTezNumber,
prettyTezAmount,
Expand Down Expand Up @@ -61,7 +63,8 @@ const makeTransfer = (
);
}

return Promise.reject(`Unrecognized type!`);
const error: never = t;
throw new Error(error);
};

const renderSubTotal = (t: TransactionValues) => {
Expand All @@ -80,32 +83,50 @@ const renderSubTotal = (t: TransactionValues) => {
) : null;
};

type signingType =
| { type: "password"; esk: UmamiEncrypted }
| { type: "GoogleSSO" };

const useGetSigningType = () => {
const getAccount = useGetOwnedAccount();
return (pkh: string): signingType => {
const account = getAccount(pkh);

if (account.type === AccountType.MNEMONIC) {
return { type: "password", esk: account.esk };
} else {
return { type: "GoogleSSO" };
}
};
};

export const RecapDisplay: React.FC<{
network: TezosNetwork;
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 getSigningType = useGetSigningType();

const { register, handleSubmit } = useForm<{ password: string }>();
const toast = useToast();
const [isLoading, setIsLoading] = useState(false);

const isGoogleSSO = !esk;
const signingType = getSigningType(transfer.values.sender);
const isGoogleSSO = signingType.type === "GoogleSSO";

const onSubmitGoogleSSO = async (sk: string) => {
if (signingType.type === "password") {
throw new Error(`Wrong signing method called`);
}

setIsLoading(true);
try {
const result = await makeTransfer(transfer, sk, network);
Expand All @@ -118,14 +139,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 (signingType.type === "GoogleSSO") {
throw new Error(`Wrong signing method called`);
}

setIsLoading(true);
try {
const sk = await decrypt(esk, password);
const sk = await decrypt(signingType.esk, password);
const result = await makeTransfer(transfer, sk, network);

onSucces(result.hash);
Expand Down
11 changes: 11 additions & 0 deletions src/utils/hooks/accountHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
};

0 comments on commit f4368f1

Please sign in to comment.