Skip to content

Commit

Permalink
Indicate encrypted transactions in TX lists
Browse files Browse the repository at this point in the history
  • Loading branch information
csillag committed Jun 13, 2023
1 parent 642990b commit b032346
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
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.
{ 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)]
}
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)

0 comments on commit b032346

Please sign in to comment.