Skip to content
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

Indicate encrypted transactions in TX lists #505

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/app/components/TransactionEncryptionStatus/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { FC } from 'react'
import { useTranslation } from 'react-i18next'
import LockIcon from '@mui/icons-material/Lock'
import { COLORS } from '../../../styles/theme/colors'
import { RuntimeTransactionEncryptionEnvelope } from '../../../oasis-indexer/api'
import Tooltip from '@mui/material/Tooltip'
import { tooltipDelay } from '../../../styles/theme'

type TransactionEncryptionStatusProps = {
envelope?: RuntimeTransactionEncryptionEnvelope
}

export const TransactionEncrypted = () => {
const { t } = useTranslation()
return (
<Tooltip
arrow
placement="top"
title={t('transaction.tooltips.txEncrypted')}
enterDelay={tooltipDelay}
enterNextDelay={tooltipDelay}
>
<LockIcon htmlColor={COLORS.eucalyptus} />
</Tooltip>
)
}

export const TransactionEncryptionStatus: FC<TransactionEncryptionStatusProps> = ({ envelope }) =>
envelope ? <TransactionEncrypted /> : null
19 changes: 19 additions & 0 deletions src/app/components/Transactions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useTranslation } from 'react-i18next'
import formatDistanceStrict from 'date-fns/formatDistanceStrict'
import Box from '@mui/material/Box'
import ArrowForwardIcon from '@mui/icons-material/ArrowForward'
import LockIcon from '@mui/icons-material/Lock'
import { Table, TableCellAlign } from '../../components/Table'
import { TransactionStatusIcon } from '../../components/TransactionStatusIcon'
import { RuntimeTransactionLabel } from '../../components/RuntimeTransactionLabel'
Expand All @@ -16,6 +17,8 @@ import { AccountLink } from '../Account/AccountLink'
import { TransactionLink } from './TransactionLink'
import { trimLongString } from '../../utils/trimLongString'
import Typography from '@mui/material/Typography'
import { doesAnyOfTheseLayersSupportEncryptedTransactions } from '../../../types/layers'
import { TransactionEncryptionStatus } from '../TransactionEncryptionStatus'

const StyledCircle = styled(Box)(({ theme }) => ({
position: 'absolute',
Expand Down Expand Up @@ -59,8 +62,16 @@ export const Transactions: FC<TransactionsProps> = ({
verbose = true,
}) => {
const { t } = useTranslation()
// We only want to show encryption status of we are listing transactions
// from paratimes that actually support encrypting transactions
const canHaveEncryption = doesAnyOfTheseLayersSupportEncryptedTransactions(
transactions?.map(tx => tx.layer),
)
const tableColumns = [
{ content: t('common.status') },
...(canHaveEncryption
? [{ content: (<LockIcon htmlColor={COLORS.grayMedium} />) as unknown as string }]
: []), // The table does support widgets in the column headers, but the TS definition is unaware of that.
Comment on lines +72 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Table does not support it too well

key={column.content}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that could be improved. (By generating the key using the index, or something similar.)

{ content: t('common.hash') },
...(verbose ? [{ content: t('common.block') }] : []),
{ content: t('common.age'), align: TableCellAlign.Right },
Expand All @@ -77,6 +88,14 @@ export const Transactions: FC<TransactionsProps> = ({
content: <TransactionStatusIcon success={transaction.success} />,
key: 'success',
},
...(canHaveEncryption
? [
{
content: <TransactionEncryptionStatus envelope={transaction.encryption_envelope} />,
key: 'encrypted',
},
]
: []),
{
content: (
<TransactionLink
Expand Down
4 changes: 4 additions & 0 deletions src/app/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ export function getEthAccountAddress(preimage: AddressPreimage | undefined): str
// We need to convert from base64 to hex, add the prefix, and convert to checksum address
return toChecksumAddress(`0x${Buffer.from(preimage.address_data, 'base64').toString('hex')}`)
}

export function uniq<T>(input: T[] | undefined): T[] {
return input === undefined ? [] : [...new Set(input)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we need this function, but Ok. [...new Set(undefined)] will return an empty array anyway

}
1 change: 1 addition & 0 deletions src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
"senderTooltipOasis": "Oasis address for the sender",
"recipientTooltipOasis": "Oasis address for the recipient",
"txTooltipHashUnavailable": "Hash not available in selected address format",
"txEncrypted": "This transaction is encrypted",
"senderTooltipUnavailable": "Sender not available in selected address format",
"recipientTooltipUnavailable": "Recipient not available in selected address format"
}
Expand Down
10 changes: 10 additions & 0 deletions src/types/layers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { uniq } from '../app/utils/helpers'

// Here we need to import from the generated code, in order to break
// a cycle of imports which confuse jest
// eslint-disable-next-line no-restricted-imports
Expand Down Expand Up @@ -26,3 +28,11 @@ const layerOrder: Record<Layer, number> = {

export const orderByLayer = (itemA: HasLayer, itemB: HasLayer): number =>
layerOrder[itemA.layer] - layerOrder[itemB.layer]

const layersWithEncryptedTransactions: Layer[] = [Layer.sapphire]

export const doesLayerSupportEncryptedTransactions = (layer: Layer): boolean =>
layersWithEncryptedTransactions.includes(layer)

export const doesAnyOfTheseLayersSupportEncryptedTransactions = (layers: Layer[] | undefined): boolean =>
uniq(layers).some(doesLayerSupportEncryptedTransactions)