Skip to content

Commit

Permalink
adds failed simulation state to tx scan label, updates tx scan respon…
Browse files Browse the repository at this point in the history
…se types, updates non json response error
  • Loading branch information
aristidesstaffieri committed Sep 5, 2024
1 parent 9fd5a83 commit f207efb
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 37 deletions.
70 changes: 51 additions & 19 deletions extension/src/popup/components/WarningMessages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1147,33 +1147,65 @@ export const BlockAidSiteScanLabel = ({
return <BlockAidBenignLabel />;
};

export const BlockaidMaliciousTxSignWarning = ({
type,
export const BlockaidTxScanLabel = ({
scanResult,
}: {
type: BlockAidScanTxResult["validation"]["result_type"];
scanResult: BlockAidScanTxResult;
}) => {
const { t } = useTranslation();
const details =
type === "Malicious"
? {
const { simulation, validation } = scanResult;

if ("error" in simulation) {
return (
<WarningMessage
header="This transaction is expected to fail"
variant={WarningMessageVariant.warning}
>
<div>
<p>{t(simulation.error)}</p>
</div>
</WarningMessage>
);
}

let message = null;
if ("result_type" in validation) {
switch (validation.result_type) {
case "Malicious": {
message = {
header: "This contract was flagged as malicious",
variant: WarningMessageVariant.highAlert,
message:
"Proceed with caution. Blockaid has flagged this transaction as malicious.",
}
: {
message: validation.description,
};
return (
<WarningMessage header={message.header} variant={message.variant}>
<div>
<p>{t(message.message)}</p>
</div>
</WarningMessage>
);
}

case "Warning": {
message = {
header: "This contract was flagged as suspicious",
variant: WarningMessageVariant.warning,
message:
"Proceed with caution. Blockaid has flagged this transaction as suspicious.",
message: validation.description,
};
return (
<WarningMessage header={details.header} variant={details.variant}>
<div>
<p>{t(details.message)}</p>
</div>
</WarningMessage>
);
return (
<WarningMessage header={message.header} variant={message.variant}>
<div>
<p>{t(message.message)}</p>
</div>
</WarningMessage>
);
}

case "Benign":
default:
}
}
return <></>;
};

export const BlockaidMaliciousTxInternalWarning = ({
Expand Down
1 change: 1 addition & 0 deletions extension/src/popup/components/WarningMessages/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@

&__children-wrapper {
flex-grow: 1;
word-break: break-word;

a {
color: var(--color-white);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import {
import { HardwareSign } from "popup/components/hardwareConnect/HardwareSign";
import { useScanAsset, useScanTx } from "popup/helpers/blockaid";
import {
BlockaidMaliciousTxInternalWarning,
BlockaidTxScanLabel,
FlaggedWarningMessage,
} from "popup/components/WarningMessages";
import { View } from "popup/basics/layout/View";
Expand Down Expand Up @@ -620,11 +620,7 @@ export const TransactionDetails = ({ goBack }: { goBack: () => void }) => {
</div>
</div>
)}
{scanResult && scanResult.validation.result_type !== "Benign" && (
<BlockaidMaliciousTxInternalWarning
description={scanResult.validation.description}
/>
)}
{scanResult && <BlockaidTxScanLabel scanResult={scanResult} />}
{submission.submitStatus === ActionStatus.IDLE && (
<FlaggedWarningMessage
isMemoRequired={isMemoRequired}
Expand Down
41 changes: 36 additions & 5 deletions extension/src/popup/helpers/blockaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,43 @@ export interface BlockAidScanSiteResult {
// ...
}

interface ValidationResult {
status: "Success" | "Error";
result_type: "Benign" | "Warning" | "Malicious";
description: string;
}
interface ValidationError {
status: "Success" | "Error";
error: string;
}
type Validation = ValidationResult | ValidationError;

interface SimulationResult {
status: "Success" | "Error";
}
interface SimulationError {
status: "Success" | "Error";
error: string;
}
type Simulation = SimulationResult | SimulationError;

export const castValidation = (validation: Validation) => {
if (validation.status === "Error") {
return validation as ValidationResult;
}
return validation as ValidationError;
};

export const castSimulation = (simulation: Simulation) => {
if (simulation.status === "Error") {
return simulation as SimulationResult;
}
return simulation as SimulationError;
};

export interface BlockAidScanTxResult {
simulation: object;
validation: {
result_type: "Benign" | "Warning" | "Malicious";
description: string;
};
simulation: Simulation;
validation: Validation;
}

export const useScanSite = () => {
Expand Down
2 changes: 1 addition & 1 deletion extension/src/popup/helpers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const fetchJson = async <T>(url: string, options?: RequestInit) => {

if (!res.headers.get("content-type")?.includes("application/json")) {
const content = await res.text();
throw new Error(content);
throw new Error(`Did not receive json error:${content}`);
}

const data = (await res.json()) as T;
Expand Down
8 changes: 2 additions & 6 deletions extension/src/popup/views/SignTransaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
FirstTimeWarningMessage,
MemoWarningMessage,
SSLWarningMessage,
BlockaidMaliciousTxSignWarning,
BlockaidTxScanLabel,
} from "popup/components/WarningMessages";
import { HardwareSign } from "popup/components/hardwareConnect/HardwareSign";
import { KeyIdenticon } from "popup/components/identicons/KeyIdenticon";
Expand Down Expand Up @@ -328,11 +328,7 @@ export const SignTransaction = () => {
{!isDomainListedAllowed && !isSubmitDisabled ? (
<FirstTimeWarningMessage />
) : null}
{scanResult && scanResult.validation.result_type !== "Benign" && (
<BlockaidMaliciousTxSignWarning
type={scanResult.validation.result_type}
/>
)}
{scanResult && <BlockaidTxScanLabel scanResult={scanResult} />}
{renderTabBody()}
</div>
);
Expand Down

0 comments on commit f207efb

Please sign in to comment.