-
Notifications
You must be signed in to change notification settings - Fork 616
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
Add support for sub account and the new addAddress RPC #1490
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -41,3 +41,5 @@ examples/**/yarn.lock | |
|
||
# next.js | ||
.next | ||
|
||
tsconfig.tsbuildinfo |
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
47 changes: 47 additions & 0 deletions
47
examples/testapp/src/pages/sub-accounts/components/AddAddressButton.tsx
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,47 @@ | ||
import { Box, Button } from '@chakra-ui/react'; | ||
import { createCoinbaseWalletSDK } from '@coinbase/wallet-sdk'; | ||
import React, { useCallback, useState } from 'react'; | ||
|
||
export function AddAddressButton({ sdk }: { sdk: ReturnType<typeof createCoinbaseWalletSDK> }) { | ||
const [subAccount, setSubAccount] = useState<string>(); | ||
|
||
const handleAddAddress = useCallback(async () => { | ||
if (!sdk) { | ||
return; | ||
} | ||
const provider = sdk.getProvider(); | ||
const response = (await provider.request({ | ||
method: 'wallet_addAddress', | ||
params: [ | ||
{ | ||
chainId: 84532, | ||
}, | ||
], | ||
})) as { address: string }; | ||
|
||
console.info('customlogs: response', response); | ||
|
||
setSubAccount(response.address); | ||
}, [sdk]); | ||
|
||
return ( | ||
<> | ||
<Button w="full" onClick={handleAddAddress}> | ||
Add Address | ||
</Button> | ||
{subAccount && ( | ||
<Box | ||
as="pre" | ||
w="full" | ||
p={2} | ||
bg="gray.900" | ||
borderRadius="md" | ||
border="1px solid" | ||
borderColor="gray.700" | ||
> | ||
{JSON.stringify(subAccount, null, 2)} | ||
</Box> | ||
)} | ||
</> | ||
); | ||
} |
41 changes: 41 additions & 0 deletions
41
examples/testapp/src/pages/sub-accounts/components/ConnectButton.tsx
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,41 @@ | ||
import { Box, Button } from '@chakra-ui/react'; | ||
import { createCoinbaseWalletSDK } from '@coinbase/wallet-sdk'; | ||
import React, { useCallback, useState } from 'react'; | ||
|
||
export function ConnectButton({ 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('customlogs: response', response); | ||
setState(response 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> | ||
)} | ||
</> | ||
); | ||
} |
48 changes: 48 additions & 0 deletions
48
examples/testapp/src/pages/sub-accounts/components/PersonalSignButton.tsx
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,48 @@ | ||
import { Box, Button } from '@chakra-ui/react'; | ||
import { createCoinbaseWalletSDK } from '@coinbase/wallet-sdk'; | ||
import React, { useCallback, useState } from 'react'; | ||
import { toHex } from 'viem'; | ||
|
||
export function PersonalSignButton({ sdk }: { sdk: ReturnType<typeof createCoinbaseWalletSDK> }) { | ||
const [state, setState] = useState<string>(); | ||
const handlePersonalSign = useCallback(async () => { | ||
if (!sdk) { | ||
return; | ||
} | ||
|
||
const provider = sdk.getProvider(); | ||
try { | ||
const response = await provider.request({ | ||
method: 'personal_sign', | ||
params: [toHex('Hello, world!')], | ||
}); | ||
console.info('customlogs: response', response); | ||
setState(response as string); | ||
} catch (e) { | ||
console.error('customlogs: error', e); | ||
} | ||
}, [sdk]); | ||
|
||
return ( | ||
<> | ||
<Button w="full" onClick={handlePersonalSign}> | ||
Personal Sign | ||
</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> | ||
)} | ||
</> | ||
); | ||
} |
59 changes: 59 additions & 0 deletions
59
examples/testapp/src/pages/sub-accounts/components/SendCallsButton.tsx
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,59 @@ | ||
import { Box, Button } from '@chakra-ui/react'; | ||
import { createCoinbaseWalletSDK } from '@coinbase/wallet-sdk'; | ||
import React, { useCallback, useState } from 'react'; | ||
import { baseSepolia } from 'viem/chains'; | ||
|
||
export function SendCallsButton({ sdk }: { sdk: ReturnType<typeof createCoinbaseWalletSDK> }) { | ||
const [state, setState] = useState<string>(); | ||
const handleSendCalls = useCallback(async () => { | ||
if (!sdk) { | ||
return; | ||
} | ||
|
||
const provider = sdk.getProvider(); | ||
try { | ||
const response = await provider.request({ | ||
method: 'wallet_sendCalls', | ||
params: [ | ||
{ | ||
chainId: baseSepolia.id, | ||
calls: [], | ||
version: '1', | ||
// capabilities: { | ||
// paymasterService: { | ||
// url: '', | ||
// }, | ||
// }, | ||
}, | ||
], | ||
}); | ||
console.info('customlogs: response', response); | ||
setState(response as string); | ||
} catch (e) { | ||
console.error('customlogs: error', e); | ||
} | ||
}, [sdk]); | ||
|
||
return ( | ||
<> | ||
<Button w="full" onClick={handleSendCalls}> | ||
Send Calls | ||
</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> | ||
)} | ||
</> | ||
); | ||
} |
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,44 @@ | ||
import { Container, VStack } from '@chakra-ui/react'; | ||
import { createCoinbaseWalletSDK } from '@coinbase/wallet-sdk'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
import { AddAddressButton } from './components/AddAddressButton'; | ||
import { ConnectButton } from './components/ConnectButton'; | ||
import { PersonalSignButton } from './components/PersonalSignButton'; | ||
import { SendCallsButton } from './components/SendCallsButton'; | ||
|
||
export default function SubAccounts() { | ||
const [sdk, setSDK] = useState<ReturnType<typeof createCoinbaseWalletSDK>>(); | ||
|
||
useEffect(() => { | ||
const sdk = createCoinbaseWalletSDK({ | ||
appName: 'CryptoPlayground', | ||
preference: { | ||
keysUrl: 'http://localhost:3005/connect', | ||
options: 'smartWalletOnly', | ||
}, | ||
}); | ||
|
||
if (!sdk) { | ||
return; | ||
} | ||
|
||
setSDK(sdk); | ||
const provider = sdk.getProvider(); | ||
|
||
provider.on('accountsChanged', (accounts) => { | ||
console.info('customlogs: accountsChanged', accounts); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<Container mb={16}> | ||
<VStack w="full" spacing={4}> | ||
<ConnectButton sdk={sdk} /> | ||
<AddAddressButton sdk={sdk} /> | ||
<PersonalSignButton sdk={sdk} /> | ||
<SendCallsButton sdk={sdk} /> | ||
</VStack> | ||
</Container> | ||
); | ||
} |
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
27 changes: 27 additions & 0 deletions
27
packages/wallet-sdk/src/core/storage/createIdbStorage.test.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,27 @@ | ||
import { createIdbStorage } from './createIdbStorage.js'; | ||
|
||
describe('createIdbStorage', () => { | ||
it('should create an indexedDB storage', () => { | ||
const storage = createIdbStorage('test', 'test'); | ||
expect(storage).toBeDefined(); | ||
}); | ||
|
||
it('getItem', async () => { | ||
const storage = createIdbStorage('test', 'test'); | ||
await storage.setItem('foo', 'bar'); | ||
expect(await storage.getItem('foo')).toEqual('bar'); | ||
}); | ||
|
||
it('setItem', async () => { | ||
const storage = createIdbStorage('test', 'test'); | ||
await storage.setItem('foo', 'bar'); | ||
expect(await storage.getItem('foo')).toEqual('bar'); | ||
}); | ||
|
||
it('removeItem', async () => { | ||
const storage = createIdbStorage('test', 'test'); | ||
await storage.setItem('foo', 'bar'); | ||
await storage.removeItem('foo'); | ||
expect(await storage.getItem('foo')).toEqual(null); | ||
}); | ||
}); |
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,26 @@ | ||
import { createStore, del, get, set } from 'idb-keyval'; | ||
|
||
export type IdbStorage = { | ||
getItem: <T>(key: string) => Promise<T | null>; | ||
removeItem: (key: string) => Promise<void>; | ||
setItem: (key: string, value: unknown) => Promise<void>; | ||
}; | ||
|
||
export function createIdbStorage(scope: string, name: string): IdbStorage { | ||
const store = typeof indexedDB !== 'undefined' ? createStore(scope, name) : undefined; | ||
return { | ||
getItem: async (key: string) => { | ||
const value = await get(key, store); | ||
if (!value) { | ||
return null; | ||
} | ||
return value; | ||
}, | ||
removeItem: async (key: string) => { | ||
return del(key, store); | ||
}, | ||
setItem: async (key: string, value: unknown) => { | ||
return set(key, value, store); | ||
}, | ||
}; | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea being this address is limited to one chain, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check this with partners
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes the idea was each account is per chain and we wont sync across chains