-
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.
- Loading branch information
1 parent
3a08c4b
commit e0b3caa
Showing
4 changed files
with
223 additions
and
2 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
93 changes: 93 additions & 0 deletions
93
src/components/SendFlow/Beacon/OriginationOperationSignModal.test.tsx
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,93 @@ | ||
import { BeaconMessageType, NetworkType, OperationRequestOutput } from "@airgap/beacon-wallet"; | ||
import type { BatchWalletOperation } from "@taquito/taquito/dist/types/wallet/batch-operation"; | ||
import BigNumber from "bignumber.js"; | ||
|
||
import { OriginationOperationSignModal } from "./OriginationOperationSignModal"; | ||
import { mockContractOrigination, mockImplicitAccount } from "../../../mocks/factories"; | ||
import { | ||
act, | ||
dynamicModalContextMock, | ||
render, | ||
screen, | ||
userEvent, | ||
waitFor, | ||
} from "../../../mocks/testUtils"; | ||
import { GHOSTNET, MAINNET } from "../../../types/Network"; | ||
import { WalletClient } from "../../../utils/beacon/WalletClient"; | ||
import { prettyTezAmount } from "../../../utils/format"; | ||
import { useGetSecretKey } from "../../../utils/hooks/getAccountDataHooks"; | ||
import { networksActions } from "../../../utils/redux/slices/networks"; | ||
import { store } from "../../../utils/redux/store"; | ||
import { executeOperations, makeToolkit } from "../../../utils/tezos"; | ||
import { SuccessStep } from "../SuccessStep"; | ||
|
||
const user = userEvent.setup(); | ||
|
||
const message = { | ||
id: "messageid", | ||
type: BeaconMessageType.OperationRequest, | ||
network: { type: NetworkType.GHOSTNET }, | ||
appMetadata: {}, | ||
} as OperationRequestOutput; | ||
const operation = { | ||
type: "implicit" as const, | ||
sender: mockImplicitAccount(0), | ||
signer: mockImplicitAccount(0), | ||
operations: [mockContractOrigination(0)], | ||
}; | ||
const fee = BigNumber(123); | ||
|
||
jest.mock("../../../utils/tezos", () => ({ | ||
...jest.requireActual("../../../utils/tezos"), | ||
executeOperations: jest.fn(), | ||
makeToolkit: jest.fn(), | ||
})); | ||
|
||
jest.mock("../../../utils/hooks/getAccountDataHooks", () => ({ | ||
...jest.requireActual("../../../utils/hooks/getAccountDataHooks"), | ||
useGetSecretKey: jest.fn(), | ||
})); | ||
|
||
describe("<OriginationOperationSignModal />", () => { | ||
it("renders fee", () => { | ||
render(<OriginationOperationSignModal fee={fee} message={message} operation={operation} />); | ||
|
||
expect(screen.getByText(prettyTezAmount(fee))).toBeVisible(); | ||
}); | ||
|
||
it("uses correct network", async () => { | ||
store.dispatch(networksActions.setCurrent(MAINNET)); | ||
jest.mocked(useGetSecretKey).mockImplementation(() => () => Promise.resolve("secretKey")); | ||
|
||
jest.mocked(executeOperations).mockResolvedValue({ opHash: "ophash" } as BatchWalletOperation); | ||
jest.spyOn(WalletClient, "respond").mockResolvedValue(); | ||
|
||
render(<OriginationOperationSignModal fee={fee} message={message} operation={operation} />); | ||
|
||
expect(screen.getByText("Ghostnet")).toBeVisible(); | ||
expect(screen.queryByText("Mainnet")).not.toBeInTheDocument(); | ||
|
||
await act(() => user.type(screen.getByLabelText("Password"), "Password")); | ||
|
||
const signButton = screen.getByRole("button", { | ||
name: "Confirm Transaction", | ||
}); | ||
await waitFor(() => expect(signButton).toBeEnabled()); | ||
await act(() => user.click(signButton)); | ||
|
||
expect(makeToolkit).toHaveBeenCalledWith({ | ||
type: "mnemonic", | ||
secretKey: "secretKey", | ||
network: GHOSTNET, | ||
}); | ||
|
||
await waitFor(() => | ||
expect(WalletClient.respond).toHaveBeenCalledWith({ | ||
type: BeaconMessageType.OperationResponse, | ||
id: message.id, | ||
transactionHash: "ophash", | ||
}) | ||
); | ||
expect(dynamicModalContextMock.openWith).toHaveBeenCalledWith(<SuccessStep hash="ophash" />); | ||
}); | ||
}); |
112 changes: 112 additions & 0 deletions
112
src/components/SendFlow/Beacon/OriginationOperationSignModal.tsx
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,112 @@ | ||
import { | ||
Accordion, | ||
AccordionButton, | ||
AccordionIcon, | ||
AccordionItem, | ||
AccordionPanel, | ||
AspectRatio, | ||
Flex, | ||
Heading, | ||
Image, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
Text, | ||
} from "@chakra-ui/react"; | ||
import { capitalize } from "lodash"; | ||
|
||
import { BeaconSignPageProps } from "./BeaconSignPageProps"; | ||
import { useSignWithBeacon } from "./useSignWithBeacon"; | ||
import colors from "../../../style/colors"; | ||
import { ContractOrigination } from "../../../types/Operation"; | ||
import { JsValueWrap } from "../../AccountDrawer/JsValueWrap"; | ||
import { SignButton } from "../SignButton"; | ||
import { SignPageFee } from "../SignPageFee"; | ||
import { headerText } from "../SignPageHeader"; | ||
|
||
export const OriginationOperationSignModal: React.FC<BeaconSignPageProps> = ({ | ||
operation, | ||
fee, | ||
message, | ||
}) => { | ||
const { isSigning, onSign, network } = useSignWithBeacon(operation, message); | ||
|
||
return ( | ||
<ModalContent> | ||
<ModalHeader marginBottom="24px"> | ||
<Flex alignItems="center" justifyContent="center"> | ||
Operation Request | ||
</Flex> | ||
<Text marginTop="10px" color={colors.gray[400]} textAlign="center" size="sm"> | ||
{message.appMetadata.name} is requesting permission to sign this operation. | ||
</Text> | ||
|
||
<Flex alignItems="center" justifyContent="center" marginTop="10px"> | ||
<Heading marginRight="4px" color={colors.gray[450]} size="sm"> | ||
Network: | ||
</Heading> | ||
<Text color={colors.gray[400]} size="sm"> | ||
{capitalize(message.network.type)} | ||
</Text> | ||
</Flex> | ||
</ModalHeader> | ||
<ModalCloseButton /> | ||
<ModalBody data-testid="beacon-request-body"> | ||
<Flex | ||
alignItems="center" | ||
marginTop="16px" | ||
padding="15px" | ||
borderRadius="4px" | ||
backgroundColor={colors.gray[800]} | ||
> | ||
<AspectRatio width="60px" marginRight="12px" ratio={1}> | ||
<Image borderRadius="4px" src={message.appMetadata.icon} /> | ||
</AspectRatio> | ||
<Heading size="sm">{message.appMetadata.name}</Heading> | ||
</Flex> | ||
|
||
<Flex alignItems="center" justifyContent="end" marginTop="12px"> | ||
<SignPageFee fee={fee} /> | ||
</Flex> | ||
|
||
<Accordion marginTop="16px" allowToggle={true}> | ||
<AccordionItem background={colors.gray[800]} border="none" borderRadius="8px"> | ||
<AccordionButton> | ||
<Heading flex="1" textAlign="left" marginY="10px" size="md"> | ||
Code | ||
</Heading> | ||
<AccordionIcon /> | ||
</AccordionButton> | ||
<AccordionPanel overflowY="auto" maxHeight="300px"> | ||
<JsValueWrap value={(operation.operations[0] as ContractOrigination).code} /> | ||
</AccordionPanel> | ||
</AccordionItem> | ||
</Accordion> | ||
<Accordion marginTop="16px" allowToggle={true}> | ||
<AccordionItem background={colors.gray[800]} border="none" borderRadius="8px"> | ||
<AccordionButton> | ||
<Heading flex="1" textAlign="left" marginY="10px" size="md"> | ||
Storage | ||
</Heading> | ||
<AccordionIcon /> | ||
</AccordionButton> | ||
<AccordionPanel overflowY="auto" maxHeight="300px"> | ||
<JsValueWrap value={(operation.operations[0] as ContractOrigination).storage} /> | ||
</AccordionPanel> | ||
</AccordionItem> | ||
</Accordion> | ||
</ModalBody> | ||
<ModalFooter padding="16px 0 0 0"> | ||
<SignButton | ||
isLoading={isSigning} | ||
network={network} | ||
onSubmit={onSign} | ||
signer={operation.signer} | ||
text={headerText(operation.type, "single")} | ||
/> | ||
</ModalFooter> | ||
</ModalContent> | ||
); | ||
}; |
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