Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeesun Kim authored and Jeesun Kim committed Apr 10, 2024
1 parent bf28182 commit b6d1993
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/app/(sidebar)/explore-endpoints/[[...pages]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export default function ExploreEndpoints() {
.find((page) => pathname.includes(page.route))
?.nestedItems?.find((i) => i.route === pathname);

console.log("page: ", page);

const pageData = page?.form;
const requiredFields = sanitizeArray(
pageData?.requiredParams?.split(",") || [],
Expand Down
37 changes: 13 additions & 24 deletions src/app/(sidebar)/transaction/sign/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,30 @@
import { useState } from "react";
import { Alert, Card, Icon, Text, Button } from "@stellar/design-system";

import { useStore } from "@/store/useStore";

import { ExpandBox } from "@/components/ExpandBox";
import { PubKeyPicker } from "@/components/FormElements/PubKeyPicker";
import { MuxedAccountResult } from "@/components/MuxedAccountResult";
import { SdsLink } from "@/components/SdsLink";

import { muxedAccount } from "@/helpers/muxedAccount";

import { XdrPicker } from "@/components/FormElements/XdrPicker";

import { validate } from "@/validate";
import { collapseTextChangeRangesAcrossMultipleVersions } from "typescript";
import { unescape } from "querystring";

export default function SignTransaction() {
const [txInput, setTxInput] = useState<string>("");
const [isTxValid, setIsTxValid] = useState<boolean | undefined>(undefined);

const [muxedFieldError, setMuxedFieldError] = useState<string>("");
const [sdkError, setSdkError] = useState<string>("");
const [txError, setTxError] = useState<string>("");

const onChange = (value: string) => {
setTxError("");
setTxInput(value);

const isValid = Boolean(validate.xdr(txInput));

console.log("**LOG** validate.xdr(txInput): ", validate.xdr(txInput));
console.log("**LOG** isValid: ", isValid);
if (value.length > 0) {
const validatedXDR = validate.xdr(value);

setIsTxValid(isValid);
if (validatedXDR.result === "success") {
setIsTxValid(true);
} else if (validatedXDR.result === "error") {
setIsTxValid(false);
setTxError(validatedXDR.message);
}
}
};

console.log("**LOG** txInput: ", txInput);

return (
<div className="SignTx">
<Card>
Expand All @@ -53,13 +42,13 @@ export default function SignTransaction() {
</Text>
}
value={txInput || ""}
error={sdkError}
error={txError}
onChange={(e) => onChange(e.target.value)}
/>

<div className="SignTx__CTA">
<Button
// disabled={!muxedAddress || Boolean(muxedFieldError)}
disabled={!txInput || !isTxValid}
size="md"
variant={"secondary"}
// onClick={parseMuxedAccount}
Expand Down
94 changes: 94 additions & 0 deletions src/constants/signTransactionPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { FeeBumpTransaction } from "@stellar/stellar-sdk";

import { Routes } from "@/constants/routes";
import { AnyObject } from "@/types/types";

type SignTransactionPagesProps = {
overview: [
{
label: "Signing for";
type: "all";
// component type
},
{
label: "Transaction Envelope XDR";
type: "all";
// component type
},
{
label: "Transaction Hash";
type: "all";
// component type
},
{
label: "Fee source account";
type: FeeBumpTransaction;
// component type
},
{
label: "Transaction Fee (stroops)";
type: FeeBumpTransaction;
// component type
},
{
label: "Number of existing signatures";
type: FeeBumpTransaction;
// component type
},
{
label: "Inner transaction hash";
type: FeeBumpTransaction;
// component type
},
{
label: "Inner transaction source account";
type: FeeBumpTransaction;
// component type
},
{
label: "Inner transaction sequence number";
type: FeeBumpTransaction;
// component type
},
{
label: "Inner transaction fee (stroops)";
type: FeeBumpTransaction;
// component type
},
{
label: "Inner transaction number of operations";
type: FeeBumpTransaction;
// component type
},
{
label: "Inner transaction number of existing signatures";
type: FeeBumpTransaction;
// component type
},
{
label: "Source account";
type: FeeBumpTransaction;
// component type
},
{
label: "Sequence number";
type: FeeBumpTransaction;
// component type
},
{
label: "Transaction Fee (stroops)";
type: FeeBumpTransaction;
// component type
},
{
label: "Number of operations";
type: FeeBumpTransaction;
// component type
},
{
label: "Number of existing signatures";
type: FeeBumpTransaction;
// component type
},
];
};
3 changes: 3 additions & 0 deletions src/helpers/trim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Regex explained: https://regexr.com/4v6jg
export const trim = (str: string, c = "\\s") =>
str.replace(new RegExp(`^([${c}]*)(.*?)([${c}]*)$`), "$2");
10 changes: 10 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ export type AssetObject = {
value: AssetObjectValue;
};

// =============================================================================
// Sign Transaction
// =============================================================================

export type SignTxPageComponent = {
[key: string]: any;
id: string;
name: string;
};

// =============================================================================
// Component
// =============================================================================
Expand Down
17 changes: 12 additions & 5 deletions src/validate/methods/xdr.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { trim } from "@/helpers/trim";
import { xdr as stellarXDR } from "@stellar/stellar-sdk";

const validateBase64 = (value: string) => {
if (value.match(/^[-A-Za-z0-9+/=]*$/) === null) {
return "Input is not valid base64";
return {
result: "error",
message: "The input is not valid base64 (a-zA-Z0-9+/=).",
};
}

return true;
return { result: "success", message: "Valid Base64" };
};

export const xdr = (value: string) => {
let base64Validation = validateBase64(value);
if (!base64Validation) {
let sanitizedXdr = trim(value);
let base64Validation = validateBase64(sanitizedXdr);

if (base64Validation.result !== "success") {
return base64Validation;
}

try {
stellarXDR.TransactionEnvelope.fromXDR(value, "base64");
stellarXDR.TransactionEnvelope.fromXDR(sanitizedXdr, "base64");

return {
result: "success",
message: "Valid Transaction Envelope XDR",
Expand Down

0 comments on commit b6d1993

Please sign in to comment.