Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 1 addition & 13 deletions packages/auth-server/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,7 @@
</template>

<script lang="ts" setup>
import { createAppKit } from "@reown/appkit/vue";

const { defaultChain } = useClientStore();
const { metadata, projectId, wagmiAdapter } = useAppKit();

createAppKit({
adapters: [wagmiAdapter],
networks: [defaultChain],
projectId,
metadata,
});

// BigInt polyfill
// BigInt polyfill for JSON serialization
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(BigInt.prototype as any).toJSON = function () {
return this.toString();
Expand Down
63 changes: 55 additions & 8 deletions packages/auth-server/composables/useAppKit.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,75 @@
import { createAppKit } from "@reown/appkit/vue";
import { WagmiAdapter } from "@reown/appkit-adapter-wagmi";

// Track if AppKit has been initialized
let appKitInitialized = false;
let wagmiAdapterInstance: WagmiAdapter | null = null;

export const useAppKit = () => {
const runtimeConfig = useRuntimeConfig();
const { defaultChain } = useClientStore();

const projectId = runtimeConfig.public.appKitProjectId;
const origin = typeof window !== "undefined" ? window.location.origin : "https://auth.zksync.dev";

const metadata = {
name: "ZKsync SSO Auth Server",
description: "ZKsync SSO Auth Server",
url: window.location.origin,
icons: [new URL("/icon-512.png", window.location.origin).toString()],
url: origin,
icons: [`${origin}/icon-512.png`],
};

const wagmiAdapter = new WagmiAdapter({
networks: [defaultChain],
projectId,
});
// Create plain chain object to avoid Viem Proxy issues
const plainChain = {
id: defaultChain.id,
name: defaultChain.name,
nativeCurrency: {
name: defaultChain.nativeCurrency.name,
symbol: defaultChain.nativeCurrency.symbol,
decimals: defaultChain.nativeCurrency.decimals,
},
rpcUrls: {
default: {
http: [...defaultChain.rpcUrls.default.http],
},
},
blockExplorers: defaultChain.blockExplorers
? {
default: {
name: defaultChain.blockExplorers.default.name,
url: defaultChain.blockExplorers.default.url,
},
}
: undefined,
};

// Lazy initialization - only create AppKit when first used
if (!appKitInitialized && typeof window !== "undefined") {
try {
wagmiAdapterInstance = new WagmiAdapter({
networks: [plainChain],
projectId,
});

createAppKit({
adapters: [wagmiAdapterInstance],
networks: [plainChain],
projectId,
metadata,
});

appKitInitialized = true;
} catch (error) {
console.warn("Failed to initialize AppKit:", error);
}
}

const wagmiConfig = wagmiAdapter.wagmiConfig;
const wagmiConfig = wagmiAdapterInstance?.wagmiConfig;

return {
metadata,
projectId,
wagmiAdapter,
wagmiAdapter: wagmiAdapterInstance,
wagmiConfig,
};
};
Loading