-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
206 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const APP_CHAIN_ID = 80001 | ||
|
||
export { APP_CHAIN_ID } |
37 changes: 37 additions & 0 deletions
37
examples/safe-4337-passkeys/src/hooks/useLocalStorageState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useEffect, useState } from "react" | ||
import { setItem, getItem } from "../logic/storage.ts" | ||
|
||
/** | ||
* Custom hook that manages state in local storage. | ||
* | ||
* @template T - The type of the state value. | ||
* @param {string} key - The key used to store the state value in local storage. | ||
* @param {T} initialValue - The initial value of the state. | ||
* @returns {[T, React.Dispatch<React.SetStateAction<T>>]} - An array containing the current state value and a function to update the state. | ||
*/ | ||
function useLocalStorageState<T>( | ||
key: string, | ||
initialValue: T | ||
): [T, React.Dispatch<React.SetStateAction<T>>] { | ||
const [state, setState] = useState<T>(() => { | ||
const storedValue = getItem(key) | ||
|
||
if (storedValue) { | ||
try { | ||
return JSON.parse(storedValue) as T | ||
} catch { | ||
// trick eslint with a no-op | ||
} | ||
} | ||
|
||
return initialValue | ||
}) | ||
|
||
useEffect(() => { | ||
setItem(key, JSON.stringify(state)) | ||
}, [key, state]) | ||
|
||
return [state, setState] | ||
} | ||
|
||
export { useLocalStorageState } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { createWeb3Modal, defaultConfig } from "@web3modal/ethers/react" | ||
import { ethers } from "ethers" | ||
import { numberToUnpaddedHex } from "../utils" | ||
|
||
// 1. Get projectId at https://cloud.walletconnect.com | ||
const projectId = import.meta.env.VITE_WC_CLOUD_PROJECT_ID | ||
|
||
if (!projectId) { | ||
throw new Error("Walletconnect Project ID is missing") | ||
} | ||
|
||
// 2. Set chains | ||
const mumbai = { | ||
chainId: 80001, | ||
name: "Polygon Mumbai", | ||
currency: "MATIC", | ||
explorerUrl: "https://mumbai.polygonscan.com", | ||
rpcUrl: "https://rpc-mumbai.maticvigil.com", | ||
} | ||
|
||
// 3. Create modal | ||
const metadata = { | ||
name: "Safe 4337 Passkeys Example", | ||
description: | ||
"An example application to deploy a 4337-compatible Safe Account with Passkeys signer", | ||
url: "https://safe.global", | ||
icons: ["https://app.safe.global/favicons/favicon.ico"], | ||
} | ||
|
||
createWeb3Modal({ | ||
ethersConfig: defaultConfig({ metadata }), | ||
// defaultChain: mumbai, | ||
chains: [mumbai], | ||
projectId, | ||
}) | ||
|
||
async function switchToMumbai(provider: ethers.Eip1193Provider) { | ||
return provider | ||
.request({ | ||
method: "wallet_addEthereumChain", | ||
params: [ | ||
{ | ||
chainId: numberToUnpaddedHex(mumbai.chainId), | ||
blockExplorerUrls: [mumbai.explorerUrl], | ||
chainName: mumbai.name, | ||
nativeCurrency: { | ||
name: mumbai.currency, | ||
symbol: mumbai.currency, | ||
decimals: 18, | ||
}, | ||
rpcUrls: [mumbai.rpcUrl], | ||
}, | ||
], | ||
}) | ||
.catch(() => { | ||
provider.request({ | ||
method: "wallet_switchEthereumChain", | ||
params: [{ chainId: numberToUnpaddedHex(mumbai.chainId) }], | ||
}) | ||
}) | ||
} | ||
|
||
export { switchToMumbai } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Converts a number to an unpadded hexadecimal string. | ||
* Metamask requires unpadded chain id for wallet_switchEthereumChain and wallet_addEthereumChain. | ||
* | ||
* @param n - The number to convert. | ||
* @param withPrefix - Whether to include the "0x" prefix in the result. Default is true. | ||
* @returns The unpadded hexadecimal string representation of the number. | ||
*/ | ||
function numberToUnpaddedHex(n: number, withPrefix = true): string { | ||
const hex = n.toString(16) | ||
return `${withPrefix ? "0x" : ""}${hex}` | ||
} | ||
|
||
export { numberToUnpaddedHex } |
This file was deleted.
Oops, something went wrong.