Skip to content

Commit

Permalink
feat: connect siwe related packages
Browse files Browse the repository at this point in the history
  • Loading branch information
pociej committed May 6, 2024
1 parent a070ef7 commit a500a18
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 262 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"cSpell.words": [
"Fastify",
"Siwe",
"viem"
]
}
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"fastify": "^4.26.1",
"fastify-plugin": "^4.5.1",
"fastify-sse-v2": "^4.0.0",
"ftp-srv": "^4.6.3",
"js-big-decimal": "^2.0.7",
"jsonwebtoken": "^9.0.2",
"loginWithCrypto": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { startupFastifyServer } from "./fastify.js";
import "./di.js";

startupFastifyServer();
2 changes: 1 addition & 1 deletion backend/src/services/file/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const fileService = (
//this is task executor abstraction so it should handle it for me
const results = await worker.context
?.beginBatch()
.uploadFile(`${DIR_NAME}${fileName}`, `/golem/workdir/${fileName}`)
.uploadFile(`ftp://127.0.0.1/${fileName}`, `/golem/workdir/${fileName}`)
.run(`/golem/scripts/clamscan-json.sh /golem/workdir/${fileName}`)
.run("ls /golem/output/")
.run(`cat /golem/output/temp/metadata.json`)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/services/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { v4 as uuidv4 } from "uuid";
import mongoose from "mongoose";
import { container } from "../../di.js";
const randomNonce = () => {
return Math.floor(Math.random() * 10000000);
return Math.floor(Math.random() * 10000000000);
};

export const userService: IUserService = {
Expand Down
3 changes: 3 additions & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

</head>


<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>



</html>
5 changes: 4 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
"preview": "vite preview"
},
"dependencies": {
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@heroicons/react": "^2.1.3",
"@tanstack/react-query": "^5.25.0",
"@types/ramda": "^0.29.11",
"@uidotdev/usehooks": "^2.4.1",
"@web3modal/wagmi": "4.0.13",
"@web3modal/siwe": "^4.1.11",
"@web3modal/wagmi": "^4.1.11",
"axios": "^1.6.7",
"dayjs": "^1.11.10",
"framer-motion": "^11.0.23",
Expand All @@ -24,6 +26,7 @@
"react": "^18.2.0",
"react-daisyui": "^5.0.0",
"react-dom": "^18.2.0",
"siwe": "^2.3.2",
"swr": "^2.2.5",
"ts-pattern": "^5.1.1",
"usehooks-ts": "^3.1.0",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { UserProvider } from "components/providers/userProvider";
import { FileUploaderProvider } from "components/providers/fileUploader";
import { formatEther } from "viem";

console.log("DUPA");
function App() {
return (
//@ts-ignore
Expand Down
62 changes: 62 additions & 0 deletions frontend/src/components/providers/blockchainProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { createWeb3Modal } from "@web3modal/wagmi/react";
import { defaultWagmiConfig } from "@web3modal/wagmi/react/config";
import { createSIWEConfig } from "@web3modal/siwe";
import { SiweMessage } from "siwe";

import { WagmiProvider } from "wagmi";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { PropsWithChildren } from "react";
import { config } from "config";
const queryClient = new QueryClient();

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const metadata = {
name: "Web3Modal",
description: "Web3Modal Example",
Expand All @@ -20,9 +25,66 @@ const wagmiConfig = defaultWagmiConfig({
metadata,
});

function createMessage({ nonce, address, chainId }: any) {
console.log("createMessage", nonce, address, chainId);
console.log("goubg to call constructor");
const message = new SiweMessage({
version: "1",
domain: window.location.host,
uri: window.location.origin,
address,
chainId,
nonce: nonce.toString(),
statement: "Sign in with ethereum",
});

console.log("after constructor");

console.log("message", message);
return message.prepareMessage();
}

const siweConfig = createSIWEConfig({
createMessage,
getNonce: async (address) => {
const response = await fetch(
`${import.meta.env.VITE_BACKEND_URL}/register`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ walletAddress: address }),
}
);

if (!response.ok) {
throw new Error(`Error registering user: ${response.statusText}`);
}
const responseData = await response.json();

console.log("nonce response", responseData.nonce.toString());
return responseData.nonce.toString();
},
getSession: async () => {
return {
address: "0x8e2A131F99b4Dce031D3BceEb67b632c4d6C12fF",
chainId: 1700,
};
},
verifyMessage: async ({ message, signature }) => {
return true;
},
signOut: async () => {
return true;
},
signOutOnNetworkChange: true,
});

createWeb3Modal({
wagmiConfig,
projectId: config.projectId,
siweConfig,
themeVariables: {
"--w3m-font-family": "Kanit-Light",
"--w3m-accent": "#181ea9a6",
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseEther } from "viem";
import { holesky } from "viem/chains";
import { holesky, polygon } from "viem/chains";

type Chain = { id: number };

Expand All @@ -19,7 +19,7 @@ type Config<Chains extends Chain[]> = {
};

export const config: Config<[typeof holesky]> = {
supportedChains: [holesky] as const,
supportedChains: [holesky, polygon] as const,
projectId: "20bd2ed396d80502980b6d2a3fb425f4",
depositContractAddress: {
[holesky.id]: "0xA3D86ebF4FAC94114526f4D09C3fA093898347a6",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/hooks/userUserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useUserData = (): {
const { data, error, isLoading } = useSWR<UserData>(
`${import.meta.env.VITE_BACKEND_URL}/me`,
fetcher,
{ refreshInterval: 1000 }
{ refreshInterval: 10000 }
);
return {
userData: data,
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
window.process = {
env: {
NODE_ENV: "dessvelopment",
},
};

console.log("process.env.NODE_ENV", process);

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./index.css";
import "./utils/axios";

// // @ts-ignore

console.log("VITE_BACKEND_URL", import.meta.env.VITE_BACKEND_URL);

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<App />
Expand Down
16 changes: 16 additions & 0 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineConfig } from "vite";
import { resolve } from "path";
import react from "@vitejs/plugin-react";
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";

export default defineConfig((configEnv) => {
const isDevelopment = configEnv.mode === "development";
Expand All @@ -21,6 +22,21 @@ export default defineConfig((configEnv) => {
utils: resolve(__dirname, "src", "utils"),
},
},
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: "globalThis",
},
// Enable esbuild polyfill plugins
plugins: [
NodeGlobalsPolyfillPlugin({
process: true,
buffer: true,
}),
],
},
},
css: {
modules: {
generateScopedName: isDevelopment
Expand Down
1 change: 0 additions & 1 deletion loginWithCrypto/src/fastify/routes/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const register: RouteOptions = {
const { walletAddress } = req.body;
// @ts-ignore temporary
const { userService } = req.routeOptions.config;
console.log("registering user", walletAddress);
const user = await userService.registerUser(walletAddress);
rep.send(user);
},
Expand Down
Loading

0 comments on commit a500a18

Please sign in to comment.