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

Detect invalid brc20 tokens #455

Merged
merged 9 commits into from
Jun 20, 2023
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

232 changes: 118 additions & 114 deletions src/app/screens/ordinalDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ const RowContainer = styled.div((props) => ({
flexDirection: 'row',
}));

const ColumnContainer = styled.div((props) => ({
const ColumnContainer = styled.div({
display: 'flex',
alignItems: 'flex-start',
flexDirection: 'column',
width: '100%',
}));
});

const Row = styled.div((props) => ({
const Row = styled.div({
display: 'flex',
justifyContent: 'space-between',
flexDirection: 'row',
}));
});

const MintLimitContainer = styled.div((props) => ({
marginLeft: props.theme.spacing(30),
Expand Down Expand Up @@ -299,10 +299,8 @@ function OrdinalDetailScreen() {
}, [selectedOrdinal?.content_type]);

useEffect(() => {
if (textContent) {
if (textContent.includes('brc-20')) {
setIsBrc20Ordinal(true);
}
if (textContent?.includes('brc-20')) {
setIsBrc20Ordinal(true);
}
}, [textContent]);

Expand All @@ -317,6 +315,7 @@ function OrdinalDetailScreen() {
url: chrome.runtime.getURL('options.html#/nft-dashboard/ordinal-detail'),
});
};

const showAlert = () => {
setshowSendOridnalsAlert(true);
};
Expand All @@ -343,79 +342,6 @@ function OrdinalDetailScreen() {
window.open(`https://www.ord.io/${selectedOrdinal?.number}`);
};

const showBrc20OrdinalDetail = (isGallery: boolean) => {
const regex = /”/g;
const validBrcContentValue = textContent.replace(regex, '"');
const content = JSON.parse(validBrcContentValue);
if (content.op === 'mint') {
return (
<ColumnContainer>
<OrdinalAttributeComponent title={t('AMOUNT_TO_MINT')} value={content.amt} />
{!isGallery && (
<OrdinalAttributeComponent
title={t('OWNED_BY')}
value={`${ordinalsAddress.substring(0, 4)}...${ordinalsAddress.substring(
ordinalsAddress.length - 4,
ordinalsAddress.length,
)}`}
showOridnalTag
isAddress
/>
)}
</ColumnContainer>
);
}
if (content.op === 'transfer') {
return (
<ColumnContainer>
<DetailSection isGallery={isGalleryOpen}>
<OrdinalAttributeComponent title={t('AMOUNT_TO_TRANSFER')} value={content.amt} />
<OrdinalAttributeComponent
title={t('BRC20_TRANSFER_STATUS')}
value={isTransferValid}
valueColor={isBrcTransferValid(selectedOrdinal!) ? theme.colors.feedback.success : theme.colors.feedback.error}
isAddress
/>
</DetailSection>
{!isGallery && (
<OrdinalAttributeComponent
title={t('OWNED_BY')}
value={`${ordinalsAddress.substring(0, 4)}...${ordinalsAddress.substring(
ordinalsAddress.length - 4,
ordinalsAddress.length,
)}`}
showOridnalTag
isAddress
/>
)}
</ColumnContainer>
);
}
if (content.op === 'deploy') {
return (
<ColumnContainer>
<Row>
<OrdinalAttributeComponent title={t('TOTAL_SUPPLY')} value={content.max} />
<MintLimitContainer>
<OrdinalAttributeComponent title={t('MINT_LIMIT')} value={content.lim} />
</MintLimitContainer>
</Row>
{!isGallery && (
<OrdinalAttributeComponent
title={t('OWNED_BY')}
value={`${ordinalsAddress.substring(0, 4)}...${ordinalsAddress.substring(
ordinalsAddress.length - 4,
ordinalsAddress.length,
)}`}
showOridnalTag
isAddress
/>
)}
</ColumnContainer>
);
}
};

const ownedByView = (
<RowContainer>
<NftOwnedByText>{t('OWNED_BY')}</NftOwnedByText>
Expand All @@ -431,11 +357,119 @@ function OrdinalDetailScreen() {
</OrdinalsTag>
</RowContainer>
);

const ordinalDescriptionData = (
<>
<DescriptionTile title={t('ID')} value={selectedOrdinal?.id!} />
{selectedOrdinal?.content_length && (
<DescriptionTile
title={t('CONTENT_LENGTH')}
value={selectedOrdinal?.content_length.toString()!}
/>
)}
{selectedOrdinal?.content_type && (
<DescriptionTile title={t('CONTENT_TYPE')} value={selectedOrdinal?.content_type!} />
)}
{selectedOrdinal?.value && (
<DescriptionTile title={t('OUTPUT_VALUE')} value={selectedOrdinal?.value!} />
)}
{selectedOrdinal?.timestamp && (
<DescriptionTile title={t('TIMESTAMP')} value={selectedOrdinal?.timestamp.toString()!} />
)}
{selectedOrdinal?.genesis_block_height && (
<DescriptionTile
title={t('GENESIS_HEIGHT')}
value={selectedOrdinal?.genesis_block_height.toString()!}
/>
)}
{selectedOrdinal?.location && (
<DescriptionTile title={t('LOCATION')} value={selectedOrdinal?.location!} />
)}
</>
);

const showBrc20OrdinalDetail = (isGallery: boolean) => {
try {
const regex = /”/g;
const validBrcContentValue = textContent.replace(regex, '"');
const content = JSON.parse(validBrcContentValue);

switch (content.op) {
case 'mint':
return (
<ColumnContainer>
<OrdinalAttributeComponent title={t('AMOUNT_TO_MINT')} value={content.amt} />
{!isGallery && (
<OrdinalAttributeComponent
title={t('OWNED_BY')}
value={`${ordinalsAddress.substring(0, 4)}...${ordinalsAddress.substring(
ordinalsAddress.length - 4,
ordinalsAddress.length,
)}`}
showOridnalTag
isAddress
/>
)}
</ColumnContainer>
);
case 'transfer':
return (
<ColumnContainer>
<DetailSection isGallery={isGalleryOpen}>
<OrdinalAttributeComponent title={t('AMOUNT_TO_TRANSFER')} value={content.amt} />
<OrdinalAttributeComponent
title={t('BRC20_TRANSFER_STATUS')}
value={isTransferValid}
valueColor={isBrcTransferValid(selectedOrdinal!) ? theme.colors.feedback.success : theme.colors.feedback.error}
isAddress
/>
</DetailSection>
{!isGallery && (
<OrdinalAttributeComponent
title={t('OWNED_BY')}
value={`${ordinalsAddress.substring(0, 4)}...${ordinalsAddress.substring(
ordinalsAddress.length - 4,
ordinalsAddress.length,
)}`}
showOridnalTag
isAddress
/>
)}
</ColumnContainer>
);
case 'deploy':
return (
<ColumnContainer>
<Row>
<OrdinalAttributeComponent title={t('TOTAL_SUPPLY')} value={content.max} />
<MintLimitContainer>
<OrdinalAttributeComponent title={t('MINT_LIMIT')} value={content.lim} />
</MintLimitContainer>
</Row>
{!isGallery && (
<OrdinalAttributeComponent
title={t('OWNED_BY')}
value={`${ordinalsAddress.substring(0, 4)}...${ordinalsAddress.substring(
ordinalsAddress.length - 4,
ordinalsAddress.length,
)}`}
showOridnalTag
isAddress
/>
)}
</ColumnContainer>
);
default:
return null;
}
} catch (error) {
return isGallery ? ordinalDescriptionData : ownedByView;
}
};

const extensionView = (
<ExtensionContainer>
<CollectibleText>
{isBrc20Ordinal ? t('BRC20_INSCRIPTION') : t('COLLECTIBLE')}
</CollectibleText>
<CollectibleText>{isBrc20Ordinal ? t('BRC20_INSCRIPTION') : t('COLLECTIBLE')}</CollectibleText>
<OrdinalTitleText>{`${t('INSCRIPTION')} ${selectedOrdinal?.number}`}</OrdinalTitleText>
<WebGalleryButton onClick={openInGalleryView}>
<>
Expand Down Expand Up @@ -467,36 +501,6 @@ function OrdinalDetailScreen() {
</ExtensionContainer>
);

const ordinalDescriptionData = (
<>
<DescriptionTile title={t('ID')} value={selectedOrdinal?.id!} />
{selectedOrdinal?.content_length && (
<DescriptionTile
title={t('CONTENT_LENGTH')}
value={selectedOrdinal?.content_length.toString()!}
/>
)}
{selectedOrdinal?.content_type && (
<DescriptionTile title={t('CONTENT_TYPE')} value={selectedOrdinal?.content_type!} />
)}
{selectedOrdinal?.value && (
<DescriptionTile title={t('OUTPUT_VALUE')} value={selectedOrdinal?.value!} />
)}
{selectedOrdinal?.timestamp && (
<DescriptionTile title={t('TIMESTAMP')} value={selectedOrdinal?.timestamp.toString()!} />
)}
{selectedOrdinal?.genesis_block_height && (
<DescriptionTile
title={t('GENESIS_HEIGHT')}
value={selectedOrdinal?.genesis_block_height.toString()!}
/>
)}
{selectedOrdinal?.location && (
<DescriptionTile title={t('LOCATION')} value={selectedOrdinal?.location!} />
)}
</>
);

const galleryView = (
<Container>
<BackButtonContainer>
Expand Down
Loading