Skip to content

Commit

Permalink
Update XDR validation + show error message on View XDR page (#877)
Browse files Browse the repository at this point in the history
* Update XDR validation + show error message on View XDR page

* Update XDR validation + show error message on View XDR page
  • Loading branch information
quietbits committed Jun 14, 2024
1 parent 80691ee commit ae1c715
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/app/(sidebar)/transaction/sign/components/Import.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ export const Import = () => {
if (value.length > 0) {
const validatedXDR = validate.xdr(value);

if (validatedXDR.result === "success") {
setTxSuccessMsg(validatedXDR.message);
} else {
setTxErrMsg(validatedXDR.message);
if (validatedXDR?.result && validatedXDR.message) {
if (validatedXDR.result === "success") {
setTxSuccessMsg(validatedXDR.message);
} else {
setTxErrMsg(validatedXDR.message);
}
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/app/(sidebar)/xdr/view/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default function ViewXdr() {
} catch (e) {
return {
jsonString: "",
error: `Unable to decode input as ${xdr.type}`,
error: `Unable to decode input as ${xdr.type}: ${e}`,
};
}
};
Expand Down
13 changes: 11 additions & 2 deletions src/components/formComponentTemplateEndpoints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ type TemplateRenderIncludeFailedProps = {
isRequired?: boolean;
};

type TemplateRenderTxProps = {
value: string | undefined;
error:
| { result: string; message: "string"; originalError?: string }
| undefined;
onChange: (val: any) => void;
isRequired?: boolean;
};

type FormComponentTemplateEndpointsProps = {
render: (...args: any[]) => JSX.Element;
validate: ((...args: any[]) => any) | null;
Expand Down Expand Up @@ -656,14 +665,14 @@ export const formComponentTemplateEndpoints = (
};
case "tx":
return {
render: (templ: TemplateRenderProps) => (
render: (templ: TemplateRenderTxProps) => (
<XdrPicker
key={id}
id={id}
label="Transaction Envelope XDR"
labelSuffix={!templ.isRequired ? "optional" : undefined}
value={templ.value || ""}
error={templ.error}
error={templ.error?.result === "error" ? templ.error?.message : ""}
onChange={templ.onChange}
/>
),
Expand Down
4 changes: 4 additions & 0 deletions src/validate/methods/xdr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const validateBase64 = (value: string) => {
};

export const xdr = (value: string) => {
if (!value) {
return undefined;
}

const sanitizedXdr = trim(value);
const base64Validation = validateBase64(sanitizedXdr);

Expand Down

0 comments on commit ae1c715

Please sign in to comment.