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

Fix Solana address detection #49

Open
wants to merge 1 commit into
base: prod
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions src/pages/content/components/twitter/tweetHandlers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,25 @@ export function handleOpTweet(tweet: HTMLElement) {
tweet.setAttribute("data-dl-tweet-check", "true");
}


function checkForSolAddress(tweetText: string) {
const regexUpper = /[A-Z]/;
const regexLower = /[a-z]/;
const regexNumber = /[0-9]/;

const solanaAddressRegex = /([1-9A-HJ-NP-Za-km-z]{39,44})/g;
const res = tweetText.match(solanaAddressRegex);
if (!res) return false;
return res.some((str) => regexUpper.test(str) && regexLower.test(str) && regexNumber.test(str));
Copy link
Author

Choose a reason for hiding this comment

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

Filtering out address without characters in any of these 3 categories isn't right, because it would result in false negatives - it is possible to have Solana address without numbers or lower/upper cased letter.

}

/**
* Adds a warning message to the tweet if it contains an ethereum/evm or solana address.
*/
export function handleTweetWithAddress(tweet: HTMLElement, tweetText: string, isLinkedTweet: boolean) {
// Regex for EVM and Solana addresses, respectively
const evmAddressRegex = /(0x[a-fA-F0-9]{40})/g;
const solanaAddressRegex = /([1-9A-HJ-NP-Za-km-z]{39,44})/g;

// Check if the tweet text contains an EVM or Solana address
const hasEvmAddress = tweetText.match(evmAddressRegex);
const hasSolAddress = checkForSolAddress(tweetText);
if (!hasEvmAddress && !hasSolAddress) return;
const evmAddressMatch = tweetText.match(evmAddressRegex);
const solAddressMatch = tweetText.match(solanaAddressRegex);
if (!evmAddressMatch && !solAddressMatch) return;

// excption for strings of lowercase letters that are identified as solana addresses
// only applies if there are no evm addresses
if (hasSolAddress && !hasEvmAddress) {
if (solAddressMatch && !evmAddressMatch) {
var numberOfFalsePositiveSolanaAddresses = 0;
// count number of unique lowercase letters in each solana address
for (const solanaAddress of hasSolAddress) {
for (const solanaAddress of solAddressMatch) {
const characters = solanaAddress.match(/[1-9A-HJ-NP-Za-km-z]/g);
// remove duplicates from array
const uniqueCharacters = [...new Set(characters)];
Expand All @@ -123,13 +112,13 @@ export function handleTweetWithAddress(tweet: HTMLElement, tweetText: string, is
}
}
// if all solana addresses were identified as false positives, then don't display warning message
if (hasSolAddress.length == numberOfFalsePositiveSolanaAddresses) {
if (solAddressMatch.length == numberOfFalsePositiveSolanaAddresses) {
return;
}
}

// display warning message on tweet
const dynamicWarningChainString = hasEvmAddress ? "An Ethereum/EVM" : "A Solana";
const dynamicWarningChainString = evmAddressMatch ? "An Ethereum/EVM" : "A Solana";
const warningTextContent = `${dynamicWarningChainString} address was detected in this reply. Proceed with caution.`;
insertTweetWarningMessage(tweet, isLinkedTweet, warningTextContent);
}
Expand Down