Skip to content

Commit

Permalink
Allow switching chain
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Jun 13, 2024
1 parent 05b1d4c commit d1924f9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/hooks/wallets/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const getRpcServiceUrl = (rpcUri: RpcUri): string => {
export const createWeb3ReadOnly = (chain: ChainInfo, customRpc?: string): JsonRpcProvider | undefined => {
const url = customRpc || getRpcServiceUrl(chain.rpcUri)
if (!url) return
return new JsonRpcProvider(url, undefined, {
return new JsonRpcProvider(url, Number(chain.chainId), {
staticNetwork: true,
batchMaxCount: BATCH_MAX_COUNT,
})
Expand Down
4 changes: 2 additions & 2 deletions src/services/private-key-module/icon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const icon = `<svg width="65" height="64" viewBox="0 0 65 64" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M19.3337 7C18.4255 7 17.6893 7.73621 17.6893 8.64436V11.9999H14.334C13.4258 11.9999 12.6896 12.7361 12.6896 13.6443V16.9999H16.0452C16.9534 16.9999 17.6896 16.2637 17.6896 15.3556V12H47.6893V15.3556C47.6893 16.2637 48.4255 16.9999 49.3337 16.9999H52.689V46.9999H56.0447C56.9528 46.9999 57.689 46.2637 57.689 45.3555V18.6442C57.689 17.7361 56.9528 16.9999 56.0447 16.9999H52.6893V13.6443C52.6893 12.7361 51.9531 11.9999 51.0449 11.9999H47.6894V8.64436C47.6894 7.73621 46.9532 7 46.045 7H19.3337ZM47.6893 48.6444C47.6893 47.7363 48.4255 47.0001 49.3337 47.0001H52.6893V50.3557C52.6893 51.2639 51.9531 52.0001 51.0449 52.0001H47.6894V55.3556C47.6894 56.2638 46.9532 57 46.045 57H19.3337C18.4255 57 17.6893 56.2638 17.6893 55.3556V52.0001H14.334C13.4258 52.0001 12.6896 51.2639 12.6896 50.3557V47.0001H16.0452C16.9534 47.0001 17.6896 47.7363 17.6896 48.6444V52H47.6893V48.6444ZM9.33382 16.9999C8.42566 16.9999 7.68945 17.7361 7.68945 18.6442V45.3555C7.68945 46.2637 8.42566 46.9999 9.33382 46.9999H12.6895V16.9999H9.33382ZM36.8004 27.248C36.8004 28.9337 35.7858 30.3824 34.3339 31.0168V40.403C34.3339 40.857 33.9658 41.2252 33.5117 41.2252H31.8673C31.4133 41.2252 31.0452 40.857 31.0452 40.403V31.0168C29.5932 30.3825 28.5786 28.9337 28.5786 27.248C28.5786 24.9776 30.4191 23.1371 32.6895 23.1371C34.9599 23.1371 36.8004 24.9776 36.8004 27.248Z" fill="#000" style="fill: var(--color-text-primary)"/>
const icon = `<svg width="100%" height="100%" viewBox="0 0 65 64" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M19.3337 7C18.4255 7 17.6893 7.73621 17.6893 8.64436V11.9999H14.334C13.4258 11.9999 12.6896 12.7361 12.6896 13.6443V16.9999H16.0452C16.9534 16.9999 17.6896 16.2637 17.6896 15.3556V12H47.6893V15.3556C47.6893 16.2637 48.4255 16.9999 49.3337 16.9999H52.689V46.9999H56.0447C56.9528 46.9999 57.689 46.2637 57.689 45.3555V18.6442C57.689 17.7361 56.9528 16.9999 56.0447 16.9999H52.6893V13.6443C52.6893 12.7361 51.9531 11.9999 51.0449 11.9999H47.6894V8.64436C47.6894 7.73621 46.9532 7 46.045 7H19.3337ZM47.6893 48.6444C47.6893 47.7363 48.4255 47.0001 49.3337 47.0001H52.6893V50.3557C52.6893 51.2639 51.9531 52.0001 51.0449 52.0001H47.6894V55.3556C47.6894 56.2638 46.9532 57 46.045 57H19.3337C18.4255 57 17.6893 56.2638 17.6893 55.3556V52.0001H14.334C13.4258 52.0001 12.6896 51.2639 12.6896 50.3557V47.0001H16.0452C16.9534 47.0001 17.6896 47.7363 17.6896 48.6444V52H47.6893V48.6444ZM9.33382 16.9999C8.42566 16.9999 7.68945 17.7361 7.68945 18.6442V45.3555C7.68945 46.2637 8.42566 46.9999 9.33382 46.9999H12.6895V16.9999H9.33382ZM36.8004 27.248C36.8004 28.9337 35.7858 30.3824 34.3339 31.0168V40.403C34.3339 40.857 33.9658 41.2252 33.5117 41.2252H31.8673C31.4133 41.2252 31.0452 40.857 31.0452 40.403V31.0168C29.5932 30.3825 28.5786 28.9337 28.5786 27.248C28.5786 24.9776 30.4191 23.1371 32.6895 23.1371C34.9599 23.1371 36.8004 24.9776 36.8004 27.248Z" style="fill: var(--color-text-primary, #000)"/>
</svg>`

export default icon
44 changes: 39 additions & 5 deletions src/services/private-key-module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import type { ChainInfo } from '@safe-global/safe-gateway-typescript-sdk'
import { type WalletInit, createEIP1193Provider } from '@web3-onboard/common'
import { getRpcServiceUrl } from '@/hooks/wallets/web3'
import pkPopupStore from './pk-popup-store'
import { numberToHex } from '@/utils/hex'

export const PRIVATE_KEY_MODULE_LABEL = 'Private Key'
export const PRIVATE_KEY_MODULE_LABEL = 'Private key'

async function getPrivateKey() {
const savedKey = pkPopupStore.getStore()?.privateKey
Expand All @@ -23,7 +24,13 @@ async function getPrivateKey() {
})
}

let currentChainId = ''
let currentRpcUri = ''

const PrivateKeyModule = (chainId: ChainInfo['chainId'], rpcUri: ChainInfo['rpcUri']): WalletInit => {
currentChainId = chainId
currentRpcUri = getRpcServiceUrl(rpcUri)

return () => {
return {
label: PRIVATE_KEY_MODULE_LABEL,
Expand All @@ -34,20 +41,41 @@ const PrivateKeyModule = (chainId: ChainInfo['chainId'], rpcUri: ChainInfo['rpcU
throw new Error('You rejected the connection')
}

const provider = new JsonRpcProvider(getRpcServiceUrl(rpcUri))
const wallet = new Wallet(privateKey, provider)
let provider: JsonRpcProvider
let wallet: Wallet
let lastChainId = ''
const chainChangedListeners = new Set<(chainId: string) => void>()

const updateProvider = () => {
console.log('[Private key signer] Updating provider to chainId', currentChainId, currentRpcUri)
provider?.destroy()
provider = new JsonRpcProvider(currentRpcUri, Number(currentChainId), { staticNetwork: true })
wallet = new Wallet(privateKey, provider)
lastChainId = currentChainId
chainChangedListeners.forEach((listener) => listener(numberToHex(Number(currentChainId))))
}

updateProvider()

return {
provider: createEIP1193Provider(
{
...wallet.provider,
on: (event: string, listener: (...args: any[]) => void) => {
if (event === 'accountsChanged') {
} else if (event === 'chainChanged') {
chainChangedListeners.add(listener)
} else {
provider.on(event, listener)
}
},

request: async (request: { method: string; params: any[] }) => {
if (currentChainId !== lastChainId) {
updateProvider()
}
return provider.send(request.method, request.params)
},

disconnect: () => {
pkPopupStore.setStore({
isOpen: false,
Expand All @@ -56,7 +84,7 @@ const PrivateKeyModule = (chainId: ChainInfo['chainId'], rpcUri: ChainInfo['rpcU
},
},
{
eth_chainId: async () => chainId,
eth_chainId: async () => currentChainId,

// @ts-ignore
eth_getCode: async ({ params }) => provider.getCode(params[0], params[1]),
Expand All @@ -80,6 +108,12 @@ const PrivateKeyModule = (chainId: ChainInfo['chainId'], rpcUri: ChainInfo['rpcU
const signedMessage = await wallet.signTypedData(params[1].domain, params[1].data, params[1].value)
return signedMessage
},

// @ts-ignore
wallet_switchEthereumChain: async ({ params }) => {
console.log('[Private key signer] Switching chain', params)
updateProvider()
},
},
),
}
Expand Down

0 comments on commit d1924f9

Please sign in to comment.