-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[embed] Add SignPayloadModal to embed iframe
- Loading branch information
1 parent
9ba5aad
commit f3532d7
Showing
9 changed files
with
219 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Box, Flex, Heading, Text, VStack } from "@chakra-ui/react"; | ||
import * as Auth from "@umami/social-auth"; | ||
|
||
import { UmamiLogoIcon } from "./assets/icons/UmamiLogo"; | ||
|
||
import { getErrorContext } from "./imported/utils/getErrorContext"; | ||
import { withTimeout } from "./imported/utils/withTimeout"; | ||
import { sendOperationErrorResponse, sendResponse, toTezosNetwork } from "./utils"; | ||
import { makeToolkit } from "@umami/tezos"; | ||
import { useEmbedApp } from "./EmbedAppContext"; | ||
import { useColor } from "./imported/style/useColor"; | ||
import { LoginButtonComponent } from "./LoginButtonComponent"; | ||
import { getDAppByOrigin } from "./ClientsPermissions"; | ||
import { useSignPayloadModalContext } from "./SignPayloadModalContext"; | ||
import { decodePayload } from "@umami/core"; | ||
|
||
const SIGN_TIMEOUT = 5 * 60 * 1000; // 5 minutes | ||
|
||
export const SignPayloadModalContent = () => { | ||
const { onClose, setIsLoading, signingType, payload } = useSignPayloadModalContext(); | ||
const { getNetwork, getUserData, getDAppOrigin } = useEmbedApp(); | ||
|
||
const color = useColor(); | ||
const dAppName = getDAppByOrigin(getDAppOrigin()); | ||
|
||
const onClick = async () => { | ||
setIsLoading(true); | ||
try { | ||
const { secretKey } = await withTimeout( | ||
async () => Auth.forIDP(getUserData()!.typeOfLogin).getCredentials(), | ||
SIGN_TIMEOUT | ||
); | ||
const toolkit = await makeToolkit({ | ||
type: "social", | ||
secretKey, | ||
network: toTezosNetwork(getNetwork()!), | ||
}); | ||
|
||
const result = await toolkit.signer.sign(payload!); | ||
|
||
sendResponse({ | ||
type: "sign_response", | ||
signingType: signingType!, | ||
signature: result.prefixSig, | ||
}); | ||
} catch (error) { | ||
sendOperationErrorResponse(getErrorContext(error).description); | ||
} finally { | ||
setIsLoading(false); | ||
onClose(); | ||
} | ||
}; | ||
|
||
return ( | ||
<VStack spacing="0"> | ||
<Box marginBottom="10px"> | ||
<UmamiLogoIcon /> | ||
</Box> | ||
|
||
<Heading marginBottom="10px" fontSize="16px" lineHeight="22px"> | ||
Sign Payload | ||
</Heading> | ||
|
||
<Flex justifyContent="center" marginBottom="16px"> | ||
<Text color={color("900")} size="sm" lineHeight="14px" textAlign="center"> | ||
<Text as="span">{dAppName ? dAppName : getDAppOrigin()}</Text> | ||
<Text as="span" color={color("500")} marginLeft="5px"> | ||
is requesting permission to sign this payload | ||
</Text> | ||
</Text> | ||
</Flex> | ||
|
||
<Box | ||
width="100%" | ||
marginBottom="20px" | ||
overflowY="auto" | ||
maxHeight="200px" | ||
padding="16px" | ||
borderRadius="5px" | ||
backgroundColor={color("100")} | ||
> | ||
<Text size="sm">{decodePayload(payload!)}</Text> | ||
</Box> | ||
|
||
<LoginButtonComponent | ||
loginType={getUserData()!.typeOfLogin} | ||
prefix="Sign with" | ||
onClick={onClick} | ||
/> | ||
</VStack> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useDisclosure } from "@chakra-ui/react"; | ||
import { createContext, PropsWithChildren, useContext, useState } from "react"; | ||
import { SigningType } from "@airgap/beacon-types"; | ||
|
||
interface SignPayloadModalContextState { | ||
isOpen: boolean; | ||
onOpen: () => void; | ||
onClose: () => void; | ||
isLoading: boolean; | ||
setIsLoading: (isLoading: boolean) => void; | ||
signingType: SigningType | null; | ||
setSigningType: (signingType: SigningType) => void; | ||
payload: string | null; | ||
setPayload: (payload: string) => void; | ||
} | ||
|
||
const SignPayloadModalContext = createContext<SignPayloadModalContextState | undefined>(undefined); | ||
|
||
export const SignPayloadModalProvider = ({ children }: PropsWithChildren) => { | ||
const { isOpen, onOpen, onClose } = useDisclosure(); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [signingType, setSigningType] = useState<SigningType | null>(null); | ||
const [payload, setPayload] = useState<string | null>(null); | ||
|
||
return ( | ||
<SignPayloadModalContext.Provider | ||
value={{ | ||
isOpen, | ||
onOpen, | ||
onClose, | ||
isLoading, | ||
setIsLoading, | ||
signingType, | ||
setSigningType, | ||
payload, | ||
setPayload, | ||
}} | ||
> | ||
{children} | ||
</SignPayloadModalContext.Provider> | ||
); | ||
}; | ||
|
||
export const useSignPayloadModalContext = (): SignPayloadModalContextState => { | ||
const context = useContext(SignPayloadModalContext); | ||
if (context === undefined) { | ||
throw new Error("useSignPayloadModalContext must be used within a SignPayloadModalProvider"); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Center, Modal, ModalCloseButton, ModalContent } from "@chakra-ui/react"; | ||
|
||
import { sendSignPayloadErrorResponse } from "./utils"; | ||
import { useSignPayloadModalContext } from "./SignPayloadModalContext"; | ||
import { ModalLoadingOverlay } from "./ModalLoadingOverlay"; | ||
import { SignPayloadModalContent } from "./SignPayloadModalContent"; | ||
import { SigningType } from "@airgap/beacon-types"; | ||
|
||
export const useSignPayloadModal = () => { | ||
const { isOpen, onOpen, onClose, isLoading, setSigningType, setPayload } = | ||
useSignPayloadModalContext(); | ||
|
||
const onModalCLose = () => { | ||
sendSignPayloadErrorResponse("User closed the modal"); | ||
onClose(); | ||
}; | ||
|
||
return { | ||
modalElement: ( | ||
<Center> | ||
<Modal | ||
autoFocus={false} | ||
closeOnOverlayClick={false} | ||
isCentered | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
> | ||
<ModalContent> | ||
<ModalCloseButton onClick={onModalCLose} /> | ||
<SignPayloadModalContent /> | ||
{isLoading && <ModalLoadingOverlay />} | ||
</ModalContent> | ||
</Modal> | ||
</Center> | ||
), | ||
onOpen: (signingType: SigningType, payload: string) => { | ||
setSigningType(signingType); | ||
setPayload(payload); | ||
onOpen(); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f3532d7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.