-
Notifications
You must be signed in to change notification settings - Fork 1
Switch to JSX #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Switch to JSX #45
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,56 @@ | ||
| import { installSnap } from '@metamask/snaps-jest'; | ||
| import { Box, Text, Heading } from '@metamask/snaps-sdk/jsx'; | ||
|
|
||
| describe('onTransaction handler tests (JSX UI, live server)', () => { | ||
| describe('when address is verified', () => { | ||
| it('should display UI components correctly', async () => { | ||
| const snap: any = await installSnap(); | ||
| const recipientAddress = '0x00000000000000000000000000000000f0cacc1a'; | ||
|
|
||
| const response = await snap.onTransaction({ | ||
| to: recipientAddress, | ||
| origin: 'https://example.com', | ||
| }); | ||
|
|
||
| const screen = response.getInterface(); | ||
|
|
||
| expect(screen).toRender( | ||
| <Box> | ||
| <Text key="el-0">✅ Verified address</Text> | ||
| <Heading key="el-1">Example Corp LTD</Heading> | ||
| <Text key="el-2">LEI: 1234567890</Text> | ||
| <Text key="el-3">Email: example@example.com</Text> | ||
| <Text key="el-4">Message: Hello there</Text> | ||
| </Box>, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when address is malicious', () => { | ||
| it('should display UI components correctly', async () => { | ||
| const snap: any = await installSnap(); | ||
| const recipientAddress = '0x00000000000000000000000000000000f00dbabe'; | ||
|
|
||
| const response = await snap.onTransaction({ | ||
| to: recipientAddress, | ||
| origin: 'https://example.com', | ||
| }); | ||
|
|
||
| const screen = response.getInterface(); | ||
|
|
||
| expect(screen).toRender( | ||
| <Box> | ||
| <Heading key="el-0"> | ||
| Security Alert: Potentially Unsafe Action Detected! | ||
| </Heading> | ||
| <Text key="el-1"> | ||
| 🚫 STOP: Your transaction is directed towards an address that has | ||
| been flagged for suspicious activity. Engaging with this address may | ||
| result in the loss of your digital assets or compromise your | ||
| personal security. | ||
| </Text> | ||
| </Box>, | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,82 @@ | ||
| import type { OnTransactionHandler } from '@metamask/snaps-sdk'; | ||
| import { SeverityLevel } from '@metamask/snaps-sdk'; | ||
| import { | ||
| Box, | ||
| Heading, | ||
| Text, | ||
| Divider, | ||
| Copyable, | ||
| Image, | ||
| } from '@metamask/snaps-sdk/jsx'; | ||
|
|
||
| type ElementDefinition = { type: string; value?: string }; | ||
|
|
||
| const requestUiDefinition = async ( | ||
| address: string, | ||
| transactionOrigin: string, | ||
| chainId: string, | ||
| ): Promise<{ ui: ElementDefinition[]; severity?: 'critical' | string }> => { | ||
| const baseUrl = 'https://app.onchaintrust.org/api/getAddressInfo'; | ||
| const uri = new URL(baseUrl); | ||
| uri.searchParams.append('address', address); | ||
| uri.searchParams.append('origin', transactionOrigin); | ||
| uri.searchParams.append('chain_id', chainId); | ||
| uri.searchParams.append('client', 'metamask'); | ||
|
|
||
| try { | ||
| const res = await fetch(uri); | ||
| if (!res.ok) { | ||
| throw new Error('Bad response from server'); | ||
| } | ||
| return await res.json(); | ||
| } catch (error) { | ||
| console.error('UI fetch failed:', error); | ||
| return { | ||
| ui: [ | ||
| { type: 'heading', value: 'Error' }, | ||
| { type: 'text', value: 'An error occurred, please try again later' }, | ||
| ], | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| const renderElement = (el: ElementDefinition, idx: number) => { | ||
| const k = `el-${idx}`; | ||
| switch (el.type) { | ||
| case 'heading': | ||
| return el.value ? <Heading key={k}>{el.value}</Heading> : null; | ||
| case 'text': | ||
| return el.value ? <Text key={k}>{el.value}</Text> : null; | ||
| case 'divider': | ||
| return <Divider />; | ||
| case 'copyable': | ||
| return el.value ? <Copyable key={k} value={el.value} /> : null; | ||
| case 'image': | ||
| return el.value ? <Image key={k} src={el.value} alt="" /> : null; | ||
| default: | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| // Handle outgoing transactions (Transaction Insights) | ||
| export const onTransaction: OnTransactionHandler = async ({ | ||
| transactionOrigin, | ||
| chainId, | ||
| transaction, | ||
| }) => { | ||
| const apiResponse = await requestUiDefinition( | ||
| transaction.to ?? '', | ||
| transactionOrigin ?? '', | ||
| chainId, | ||
| ); | ||
|
|
||
| const content = ( | ||
| <Box>{apiResponse.ui?.map((el, i) => renderElement(el, i))}</Box> | ||
| ); | ||
| const result = | ||
| apiResponse.severity === 'critical' | ||
| ? { content, severity: SeverityLevel.Critical } | ||
| : { content }; | ||
|
|
||
| return result; | ||
| }; | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The Divider component is missing a key prop, which will cause React warnings when rendered in a list. Add
key={k}to maintain consistency with other elements.