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

Sign a tx from link opened via CLI #987

Merged
merged 13 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
18 changes: 18 additions & 0 deletions src/app/(sidebar)/transaction/cli-sign/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use client";

import { Routes } from "@/constants/routes";
import { useStore } from "@/store/useStore";
import { Loader } from "@stellar/design-system";
import { useRouter } from "next/navigation";
import { useEffect } from "react";

export default function CliSignTransaction() {
const { network, transaction } = useStore();
const { sign } = transaction;
const router = useRouter();
useEffect(() => {
router.push(Routes.SIGN_TRANSACTION);
}, [sign.importXdr, network, router]);

return <Loader />;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this idea of using the route to do the redirect. 🙌 Do you think we could move NetworkByPasswordProvider logic here and remove it completely from src/app/layout.tsx. That way, everything related to CLI sign transaction would be handled in one place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, great idea! I'll get an update pushed shortly

9 changes: 6 additions & 3 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from "react";
import type { Metadata } from "next";
import React from "react";

import { LayoutMain } from "@/components/layout/LayoutMain";
import { QueryProvider } from "@/query/QueryProvider";
import { StoreProvider } from "@/store/StoreProvider";

import "@stellar/design-system/build/styles.min.css";
import { NetworkByPasswordProvider } from "@/components/NetworkByPasswordProvider";
import "@/styles/globals.scss";
import "@stellar/design-system/build/styles.min.css";
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's keep the Design System import before the global.css to make sure the global overrides work.


// Needed for CSP
export const dynamic = "force-dynamic";
Expand All @@ -29,7 +30,9 @@ export default function RootLayout({
<div id="root">
<StoreProvider>
<QueryProvider>
<LayoutMain>{children}</LayoutMain>
<NetworkByPasswordProvider>
<LayoutMain>{children}</LayoutMain>
</NetworkByPasswordProvider>
</QueryProvider>
</StoreProvider>
</div>
Expand Down
42 changes: 42 additions & 0 deletions src/components/NetworkByPasswordProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use client";

import { NetworkOptions } from "@/constants/settings";
import { useStore } from "@/store/useStore";
import { useSearchParams } from "next/navigation";
import { useEffect } from "react";

// Component to set Network and Xdr if only password is given in query string
export const NetworkByPasswordProvider = ({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd love some feedback/other ideas on the name of this component.

children,
}: {
children: React.ReactNode;
}) => {
const { updateIsDynamicNetworkSelect, transaction, selectNetwork } =
useStore();

const searchParams = useSearchParams();

const getNetworkByPassphrase = (passphrase: string) => {
return NetworkOptions.find((network) => network.passphrase === passphrase);
};

useEffect(() => {
const networkPassphrase = searchParams.get("networkPassphrase");
const xdr = searchParams.get("xdr");

if (networkPassphrase) {
const network = getNetworkByPassphrase(networkPassphrase);
if (network) {
updateIsDynamicNetworkSelect(true);
selectNetwork(network);
}
}

if (xdr) {
transaction.updateSignActiveView("overview");
transaction.updateSignImportXdr(xdr);
}
}, [searchParams, selectNetwork, updateIsDynamicNetworkSelect, transaction]);

return children;
};
1 change: 1 addition & 0 deletions src/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export enum Routes {
// Transactions
BUILD_TRANSACTION = "/transaction/build",
SIGN_TRANSACTION = "/transaction/sign",
CLI_SIGN_TRANSACTION = "/transaction/cli-sign",
SIMULATE_TRANSACTION = "/transaction/simulate",
SUBMIT_TRANSACTION = "/transaction/submit",
FEE_BUMP_TRANSACTION = "/transaction/fee-bump",
Expand Down