Skip to content

Commit

Permalink
feat: display warning on flagged nfts (#8025)
Browse files Browse the repository at this point in the history
  • Loading branch information
begonaalvarezd authored Feb 12, 2024
1 parent 041a630 commit 6df8267
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import { localize } from '@core/i18n'
import { ExplorerEndpoint, getOfficialExplorerUrl } from '@core/network'
import {
INft,
NftDownloadMetadata,
allAccountNfts,
convertAndFormatNftMetadata,
getNftByIdFromAllAccountNfts,
INft,
isFlaggedNft,
NftDownloadMetadata,
selectedNftId,
} from '@core/nfts'
import { getBaseToken } from '@core/profile/actions'
Expand Down Expand Up @@ -66,6 +67,7 @@
$: returnIfNftWasSent($allAccountNfts[$selectedAccountIndex], $time)
$: timeDiff = getTimeDifference(new Date(nft.timelockTime), $time)
$: alertText = getAlertText(downloadMetadata)
$: flaggedNftWarning = nft && isFlaggedNft(nft)
$: detailsList = {
...(id && {
nftId: { data: truncateString(id, 20, 20), copyValue: id, isCopyable: true },
Expand Down Expand Up @@ -178,6 +180,9 @@
</Text>
</nft-description>
{/if}
{#if flaggedNftWarning}
<Alert type="warning" message={flaggedNftWarning} />
{/if}
<div class="overflow-y-scroll h-full flex flex-col space-y-4 pr-2 -mr-4">
<nft-details class="flex flex-col space-y-4">
<Text type={TextType.h5} fontWeight={FontWeight.semibold}>
Expand Down
9 changes: 7 additions & 2 deletions packages/shared/components/NftActivityDetails.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import { selectedAccountIndex } from '@core/account/stores'
import { time } from '@core/app'
import { localize } from '@core/i18n'
import { getNftByIdFromAllAccountNfts, ownedNfts, selectedNftId } from '@core/nfts'
import { CollectiblesRoute, collectiblesRouter, DashboardRoute, dashboardRouter } from '@core/router'
import { getNftByIdFromAllAccountNfts, isFlaggedNft, ownedNfts, selectedNftId } from '@core/nfts'
import { CollectiblesRoute, DashboardRoute, collectiblesRouter, dashboardRouter } from '@core/router'
import { ActivityAsyncStatus, NftActivity } from '@core/wallet'
import { getSubjectFromActivity } from '@core/wallet/utils/generateActivity/helper'
import { Alert } from '@ui'
import {
ActivityAsyncStatusPill,
FontWeight,
Expand All @@ -26,6 +27,7 @@
$: nftIsOwned = $ownedNfts.some((nft) => nft.id === activity.nftId)
$: isTimelocked = activity?.asyncData?.timelockDate > $time
$: subject = getSubjectFromActivity(activity)
$: flaggedNftWarning = nft && isFlaggedNft(nft)
async function onClick(): Promise<void> {
closePopup()
Expand Down Expand Up @@ -74,5 +76,8 @@
{#if activity?.subject}
<SubjectBox {subject} />
{/if}
{#if flaggedNftWarning}
<Alert type="warning" message={flaggedNftWarning} />
{/if}
</main-content>
</nft-transaction-details>
1 change: 1 addition & 0 deletions packages/shared/lib/core/nfts/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './composeUrlFromNftUri'
export * from './convertAndFormatNftMetadata'
export * from './getSpendableStatusFromUnspentNftOutput'
export * from './fetchWithTimeout'
export * from './isFlaggedNft'
export * from './isNftOwnedByAnyAccount'
export * from './parseNftMetadata'
export * from './rewriteIpfsUri'
Expand Down
19 changes: 19 additions & 0 deletions packages/shared/lib/core/nfts/utils/isFlaggedNft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { INft } from '../interfaces'

/**
* Check if an NFT is flagged as a potential scam
* @param nft The NFT to check
* @returns True if the NFT is flagged
*/
export function isFlaggedNft(nft: INft): string | undefined {
const name = nft.name ?? ''
const parsedMetadata = nft.parsedMetadata
const parsedName = parsedMetadata?.name ?? ''
const parsedDescription = parsedMetadata?.description ?? ''
const urlRegex = /((https?|ftp|file):\/\/)?([\da-z-]+\.)+([a-z]{2,6})([/\w .-]*)*\/?$/gi
const containsUrl = urlRegex.test(name) || urlRegex.test(parsedName) || urlRegex.test(parsedDescription)
// Note: in order to avoid issues with the translations, we are using a hardcoded string here
const WARNING_MESSAGE =
'Be careful when following unknown links. Never share your private keys, nor enter them into any websites or services. '
return containsUrl ? WARNING_MESSAGE : undefined
}

0 comments on commit 6df8267

Please sign in to comment.