-
Notifications
You must be signed in to change notification settings - Fork 698
Update ERC-7895 support for wallet_addSubAccount #1558
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
201fbf1
add import support to sdk
cb-jake 919e1c0
Import workflow
cb-jake bd66ba5
remove logs
cb-jake 69dffea
test locally
cb-jake cf8ff84
test
fan-zhang-sv 03d0133
lint
fan-zhang-sv 2bd74db
test
fan-zhang-sv 8c6f79e
type
fan-zhang-sv fe041da
type
fan-zhang-sv 5ea3b5a
import order
fan-zhang-sv 03147d9
fix types
cb-jake f8d0eb6
typs
cb-jake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or 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
102 changes: 102 additions & 0 deletions
102
examples/testapp/src/pages/import-sub-account/components/AddGlobalOwner.tsx
This file contains hidden or 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,102 @@ | ||
| import { Box, Button } from "@chakra-ui/react"; | ||
| import { createCoinbaseWalletSDK } from "@coinbase/wallet-sdk"; | ||
| import { useCallback, useState } from "react"; | ||
| import { | ||
| Client, | ||
| createPublicClient, | ||
| encodeFunctionData, | ||
| http, | ||
| toHex, | ||
| } from "viem"; | ||
| import { | ||
| createBundlerClient, | ||
| createPaymasterClient, | ||
| SmartAccount, | ||
| } from "viem/account-abstraction"; | ||
| import { baseSepolia } from "viem/chains"; | ||
| import { abi } from "../../../constants"; | ||
|
|
||
| export function AddGlobalOwner({ | ||
| sdk, | ||
| subAccount, | ||
| }: { | ||
| sdk: ReturnType<typeof createCoinbaseWalletSDK>; | ||
| subAccount: SmartAccount; | ||
| }) { | ||
| const [state, setState] = useState<string>(); | ||
|
|
||
| const handleAddGlobalOwner = useCallback(async () => { | ||
| if (!sdk) { | ||
| return; | ||
| } | ||
|
|
||
| const provider = sdk.getProvider(); | ||
| const accounts = await provider.request({ | ||
| method: "eth_accounts", | ||
| }); | ||
|
|
||
| console.log("customlogs: accounts", accounts); | ||
|
|
||
| try { | ||
| const client = createPublicClient({ | ||
| chain: baseSepolia, | ||
| transport: http(), | ||
| }); | ||
| const paymasterClient = createPaymasterClient({ | ||
| transport: http( | ||
| "https://api.developer.coinbase.com/rpc/v1/base-sepolia/S-fOd2n2Oi4fl4e1Crm83XeDXZ7tkg8O" | ||
| ), | ||
| }); | ||
| const bundlerClient = createBundlerClient({ | ||
| account: subAccount, | ||
| client: client as Client, | ||
| transport: http( | ||
| "https://api.developer.coinbase.com/rpc/v1/base-sepolia/S-fOd2n2Oi4fl4e1Crm83XeDXZ7tkg8O" | ||
| ), | ||
| paymaster: paymasterClient, | ||
| }); | ||
| // @ts-expect-error | ||
| const hash = await bundlerClient.sendUserOperation({ | ||
| calls: [ | ||
| { | ||
| to: subAccount.address, | ||
| data: encodeFunctionData({ | ||
| abi, | ||
| functionName: "addOwnerAddress", | ||
| args: [accounts[0]] as const, | ||
| }), | ||
| value: toHex(0), | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| console.info("response", hash); | ||
| setState(hash as string); | ||
| } catch (e) { | ||
| console.error("error", e); | ||
| } | ||
| }, [sdk, subAccount]); | ||
|
|
||
| return ( | ||
| <> | ||
| <Button w="full" onClick={handleAddGlobalOwner}> | ||
| Add Global Owner | ||
| </Button> | ||
| {state && ( | ||
| <Box | ||
| as="pre" | ||
| w="full" | ||
| p={2} | ||
| bg="gray.900" | ||
| borderRadius="md" | ||
| border="1px solid" | ||
| borderColor="gray.700" | ||
| overflow="auto" | ||
| whiteSpace="pre-wrap" | ||
| > | ||
| {JSON.stringify(state, null, 2)} | ||
| </Box> | ||
| )} | ||
| </> | ||
| ); | ||
| } |
64 changes: 64 additions & 0 deletions
64
examples/testapp/src/pages/import-sub-account/components/AddSubAccountDeployed.tsx
This file contains hidden or 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,64 @@ | ||
| import { Box, Button } from '@chakra-ui/react'; | ||
| import { createCoinbaseWalletSDK } from '@coinbase/wallet-sdk'; | ||
| import { useCallback, useState } from 'react'; | ||
| import { numberToHex } from 'viem'; | ||
| import { SmartAccount } from 'viem/account-abstraction'; | ||
| import { baseSepolia } from 'viem/chains'; | ||
|
|
||
| type AddSubAccountProps = { | ||
| sdk: ReturnType<typeof createCoinbaseWalletSDK>; | ||
| subAccount: SmartAccount; | ||
| }; | ||
|
|
||
| export function AddSubAccountDeployed({ sdk, subAccount }: AddSubAccountProps) { | ||
| const [subAccountAddress, setSubAccountAddress] = useState<string | null>(null); | ||
|
|
||
| const handleAddSubAccount = useCallback(async () => { | ||
| if (!sdk) { | ||
| return; | ||
| } | ||
| const provider = sdk.getProvider(); | ||
| await provider.request({ | ||
| method: 'wallet_switchEthereumChain', | ||
| params: [{ chainId: numberToHex(84532) }], | ||
| }); | ||
|
|
||
| const response = (await provider.request({ | ||
| method: 'wallet_addSubAccount', | ||
| params: [ | ||
| { | ||
| version: '1', | ||
| account: { | ||
| type: 'deployed', | ||
| address: subAccount.address, | ||
| chainId: baseSepolia.id, | ||
| }, | ||
| }, | ||
| ], | ||
| })) as { address: string }; | ||
|
|
||
| console.info('response', response); | ||
| setSubAccountAddress(response.address); | ||
| }, [sdk, subAccount]); | ||
|
|
||
| return ( | ||
| <> | ||
| <Button w="full" onClick={handleAddSubAccount}> | ||
| Add Address Deployed | ||
| </Button> | ||
| {subAccountAddress && ( | ||
| <Box | ||
| as="pre" | ||
| w="full" | ||
| p={2} | ||
| bg="gray.900" | ||
| borderRadius="md" | ||
| border="1px solid" | ||
| borderColor="gray.700" | ||
| > | ||
| {subAccountAddress} | ||
| </Box> | ||
| )} | ||
| </> | ||
| ); | ||
| } |
66 changes: 66 additions & 0 deletions
66
examples/testapp/src/pages/import-sub-account/components/AddSubAccountUndeployed.tsx
This file contains hidden or 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,66 @@ | ||
| import { Box, Button } from '@chakra-ui/react'; | ||
| import { createCoinbaseWalletSDK } from '@coinbase/wallet-sdk'; | ||
| import { useCallback, useState } from 'react'; | ||
| import { numberToHex } from 'viem'; | ||
| import { SmartAccount } from 'viem/account-abstraction'; | ||
|
|
||
| type AddSubAccountUndeployedProps = { | ||
| sdk: ReturnType<typeof createCoinbaseWalletSDK>; | ||
| subAccount: SmartAccount; | ||
| }; | ||
|
|
||
| export function AddSubAccountUndeployed({ sdk, subAccount }: AddSubAccountUndeployedProps) { | ||
| const [subAccountAddress, setSubAccountAddress] = useState<string | null>(null); | ||
|
|
||
| const handleAddSubAccount = useCallback(async () => { | ||
| if (!sdk) { | ||
| return; | ||
| } | ||
| const provider = sdk.getProvider(); | ||
| await provider.request({ | ||
| method: 'wallet_switchEthereumChain', | ||
| params: [{ chainId: numberToHex(84532) }], | ||
| }); | ||
|
|
||
| const factoryArgs = await subAccount.getFactoryArgs(); | ||
|
|
||
| const response = (await provider.request({ | ||
| method: 'wallet_addSubAccount', | ||
| params: [ | ||
| { | ||
| version: '1', | ||
| account: { | ||
| type: 'undeployed', | ||
| address: subAccount.address, | ||
| factory: factoryArgs?.factory, | ||
| factoryData: factoryArgs?.factoryData, | ||
| }, | ||
| }, | ||
| ], | ||
| })) as { address: string }; | ||
|
|
||
| console.info('response', response); | ||
| setSubAccountAddress(response.address); | ||
| }, [sdk, subAccount]); | ||
|
|
||
| return ( | ||
| <> | ||
| <Button w="full" onClick={handleAddSubAccount}> | ||
| Add Address Undeployed | ||
| </Button> | ||
| {subAccountAddress && ( | ||
| <Box | ||
| as="pre" | ||
| w="full" | ||
| p={2} | ||
| bg="gray.900" | ||
| borderRadius="md" | ||
| border="1px solid" | ||
| borderColor="gray.700" | ||
| > | ||
| {subAccountAddress} | ||
| </Box> | ||
| )} | ||
| </> | ||
| ); | ||
| } |
56 changes: 56 additions & 0 deletions
56
examples/testapp/src/pages/import-sub-account/components/Connect.tsx
This file contains hidden or 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,56 @@ | ||
| import { Box, Button } from '@chakra-ui/react'; | ||
| import { createCoinbaseWalletSDK } from '@coinbase/wallet-sdk'; | ||
| import React, { useCallback, useEffect, useState } from 'react'; | ||
|
|
||
| export function Connect({ sdk }: { sdk: ReturnType<typeof createCoinbaseWalletSDK> }) { | ||
| const [state, setState] = useState<string[]>(); | ||
| const handleConnect = useCallback(async () => { | ||
| if (!sdk) { | ||
| return; | ||
| } | ||
|
|
||
| const provider = sdk.getProvider(); | ||
| const response = await provider.request({ | ||
| method: 'eth_requestAccounts', | ||
| }); | ||
|
|
||
| console.info('response', response); | ||
| setState(response as string[]); | ||
| }, [sdk]); | ||
|
|
||
| useEffect(() => { | ||
| if (!sdk) { | ||
| return; | ||
| } | ||
|
|
||
| const provider = sdk.getProvider(); | ||
| provider.on('accountsChanged', (accounts) => { | ||
| if (accounts.length === 0) { | ||
| setState(undefined); | ||
| } else { | ||
| setState(accounts as string[]); | ||
| } | ||
| }); | ||
| }, [sdk]); | ||
|
|
||
| return ( | ||
| <> | ||
| <Button w="full" onClick={handleConnect}> | ||
| Connect | ||
| </Button> | ||
| {state && ( | ||
| <Box | ||
| as="pre" | ||
| w="full" | ||
| p={2} | ||
| bg="gray.900" | ||
| borderRadius="md" | ||
| border="1px solid" | ||
| borderColor="gray.700" | ||
| > | ||
| {JSON.stringify(state, null, 2)} | ||
| </Box> | ||
| )} | ||
| </> | ||
| ); | ||
| } |
70 changes: 70 additions & 0 deletions
70
examples/testapp/src/pages/import-sub-account/components/DeploySubAccount.tsx
This file contains hidden or 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,70 @@ | ||
| import { Box, Button } from "@chakra-ui/react"; | ||
| import { createCoinbaseWalletSDK } from "@coinbase/wallet-sdk"; | ||
| import { useCallback, useState } from "react"; | ||
| import { Client, createPublicClient, http } from "viem"; | ||
| import { createBundlerClient, createPaymasterClient, SmartAccount } from "viem/account-abstraction"; | ||
| import { baseSepolia } from "viem/chains"; | ||
|
|
||
| export function DeploySubAccount({ | ||
| sdk, | ||
| subAccount, | ||
| }: { | ||
| sdk: ReturnType<typeof createCoinbaseWalletSDK>; | ||
| subAccount: SmartAccount; | ||
| }) { | ||
| const [state, setState] = useState<string>(); | ||
|
|
||
| const handleDeploySubAccount = useCallback(async () => { | ||
| if (!sdk) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const client = createPublicClient({ | ||
| chain: baseSepolia, | ||
| transport: http(), | ||
| }); | ||
| const paymasterClient = createPaymasterClient({ | ||
| transport: http('https://api.developer.coinbase.com/rpc/v1/base-sepolia/S-fOd2n2Oi4fl4e1Crm83XeDXZ7tkg8O'), | ||
| }) | ||
| const bundlerClient = createBundlerClient({ | ||
| account: subAccount, | ||
| client: client as Client, | ||
| transport: http("https://api.developer.coinbase.com/rpc/v1/base-sepolia/S-fOd2n2Oi4fl4e1Crm83XeDXZ7tkg8O"), | ||
| paymaster: paymasterClient, | ||
| }); | ||
| // @ts-expect-error | ||
| const hash = await bundlerClient.sendUserOperation({ | ||
| calls: [], | ||
| }); | ||
cb-jake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| console.info("response", hash); | ||
| setState(hash as string); | ||
| } catch (e) { | ||
| console.error("error", e); | ||
| } | ||
| }, [sdk, subAccount]); | ||
|
|
||
| return ( | ||
| <> | ||
| <Button w="full" onClick={handleDeploySubAccount}> | ||
| Deploy SubAccount | ||
| </Button> | ||
| {state && ( | ||
| <Box | ||
| as="pre" | ||
| w="full" | ||
| p={2} | ||
| bg="gray.900" | ||
| borderRadius="md" | ||
| border="1px solid" | ||
| borderColor="gray.700" | ||
| overflow="auto" | ||
| whiteSpace="pre-wrap" | ||
| > | ||
| {JSON.stringify(state, null, 2)} | ||
| </Box> | ||
| )} | ||
| </> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.