Skip to content

Wagmi: Erc7715 permissions with Appkit embedded wallet #2615

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 67 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
614dc78
added custom connector and permissiosn dapp structure
KannuSingh Jul 3, 2024
aea0b7f
added requestPermissions, clear permissions, purchase donut buttons
KannuSingh Jul 3, 2024
494a068
fix and export types
KannuSingh Jul 3, 2024
2a92fbe
Update .env.example
KannuSingh Jul 3, 2024
a6771d0
add reading donut count
KannuSingh Jul 3, 2024
12fbdbe
fix Chain type
KannuSingh Jul 3, 2024
6ab4278
refactor code
KannuSingh Jul 3, 2024
577701c
added GrantedPermissionContext
KannuSingh Jul 8, 2024
3ea0b50
Merge branch 'main' into feat-erc7715-permissions-support
KannuSingh Jul 8, 2024
eeca121
update package deps
KannuSingh Jul 8, 2024
8f651f7
run prettier
KannuSingh Jul 9, 2024
465b6e0
refactor code
KannuSingh Jul 9, 2024
0e859df
fix imports
KannuSingh Jul 9, 2024
33ae7fd
code clean up
KannuSingh Jul 9, 2024
7791e10
add env NEXT_PUBLIC_LOCAL_BUNDLER_URL
KannuSingh Jul 9, 2024
5e3bb48
Update .env.example
KannuSingh Jul 9, 2024
791f3eb
remove foundry chain
KannuSingh Jul 10, 2024
3001933
add create passkey test
KannuSingh Jul 11, 2024
8228825
Merge branch 'main' into erc7715-permissions-with-cosigner
KannuSingh Jul 11, 2024
ff6133b
wallet_grantPermission requests multi-key signer
KannuSingh Jul 11, 2024
649a315
chore: replace deprecated method
KannuSingh Jul 13, 2024
9b8ed76
added buildAndSendTransactionsWithCosignerAndPermissions
KannuSingh Jul 15, 2024
bfa8cf6
added temporary signature verifications
KannuSingh Jul 15, 2024
818b593
fix signatures encoding and set userVerificationRequired to false
KannuSingh Jul 17, 2024
4b68c24
remove solady deps
KannuSingh Jul 17, 2024
41f4ab1
Merge branch 'main' into erc7715-permissions-with-cosigner
KannuSingh Jul 17, 2024
0c9f70d
add getDummySignatureWithContext()
KannuSingh Jul 17, 2024
f62dc41
add useWalletConnectCosigner hook
KannuSingh Jul 26, 2024
2eff343
Merge branch 'main' into erc7715-permissions-with-cosigner
KannuSingh Jul 26, 2024
659a512
update deps
KannuSingh Jul 29, 2024
a8fd199
Merge branch 'main' into erc7715-permissions-with-cosigner
KannuSingh Jul 29, 2024
006698c
Update pnpm-lock.yaml
KannuSingh Jul 29, 2024
e20bfa9
chore: clean up and refactor
KannuSingh Jul 30, 2024
6bc3683
add permissions test for appkit embedded wallet
KannuSingh Jul 30, 2024
dfa957e
temp:disable rpc support check
KannuSingh Jul 30, 2024
376da6f
added handling of W3mFrameProvider
KannuSingh Jul 31, 2024
7087458
change permissions type to donut-purchase
KannuSingh Jul 31, 2024
675243b
fix: functionName on permissions
KannuSingh Aug 2, 2024
5329c87
add handling RPC_RESPONSE_TYPE_OBJECT on wagmi
KannuSingh Aug 2, 2024
f2f7b06
refactor & remove permissionConsumedCount logic
KannuSingh Aug 3, 2024
80f7f18
change getSignature to formatSignature
KannuSingh Aug 5, 2024
29d39a2
chores: code cleanup
KannuSingh Aug 5, 2024
37a3c3c
Merge branch 'main' into erc7715-permissions-with-embedded-wallet
KannuSingh Aug 6, 2024
ca71eeb
fix: merge
KannuSingh Aug 6, 2024
c2f185f
chores:run prettier
KannuSingh Aug 6, 2024
fa9032d
update deps
KannuSingh Aug 6, 2024
d84d271
fix: request serialization
KannuSingh Aug 6, 2024
e403dbc
chores: rename url
KannuSingh Aug 7, 2024
dde8c5b
Update pnpm-lock.yaml
KannuSingh Aug 7, 2024
b5b8bb5
add suggested code refinement
KannuSingh Aug 7, 2024
6b1e885
add walleconnect connector on wagmi config
KannuSingh Aug 7, 2024
8f67030
chores: rename and cleanup
KannuSingh Aug 7, 2024
cfb60ac
add sync and async permissions flow features
KannuSingh Aug 7, 2024
0a2c8a6
Merge branch 'main' into erc7715-permissions-with-embedded-wallet
KannuSingh Aug 7, 2024
29468db
Update pnpm-lock.yaml
KannuSingh Aug 7, 2024
1f390c2
added suggested refactor
KannuSingh Aug 7, 2024
a9d3c8a
moved projectId to context
KannuSingh Aug 7, 2024
7f59a15
Merge branch 'main' into erc7715-permissions-with-embedded-wallet
chris13524 Aug 7, 2024
5b8dc3f
checking: removing handling of RPC_RESPONSE_TYPE_OBJECT
KannuSingh Aug 7, 2024
f3539ca
Merge branch 'erc7715-permissions-with-embedded-wallet' of https://gi…
KannuSingh Aug 7, 2024
d2e1623
adding handling of RPC_RESPONSE_TYPE_OBJECT
KannuSingh Aug 7, 2024
243be28
Merge branch 'main' into erc7715-permissions-with-embedded-wallet
KannuSingh Aug 8, 2024
d5cbf12
change provider.onRpcSuccess handling
KannuSingh Aug 8, 2024
2f88760
more simplified onRpcSuccess
KannuSingh Aug 8, 2024
ce7958f
update onRpcSuccess on ethers client
KannuSingh Aug 8, 2024
bdecc8b
chores: remove comments
KannuSingh Aug 8, 2024
1a913f7
Merge branch 'main' into erc7715-permissions-with-embedded-wallet
KannuSingh Aug 8, 2024
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
4 changes: 3 additions & 1 deletion apps/laboratory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
"rpc-websockets": "7.11.0",
"valtio": "1.11.2",
"viem": "2.17.8",
"wagmi": "2.12.2"
"wagmi": "2.12.2",
"webauthn-p256": "0.0.2",
"axios": "1.7.2"
},
"devDependencies": {
"@aws-sdk/client-cloudwatch": "3.509.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useEffect } from 'react'
import { Button, Stack } from '@chakra-ui/react'
import { privateKeyToAccount } from 'viem/accounts'
import { useChakraToast } from '../Toast'
import { LOCAL_SIGNER_KEY, getItem } from '../../utils/LocalStorage'
import { createCredential } from 'webauthn-p256'
import { useWagmiPermissionsSync } from '../../context/WagmiPermissionsSyncContext'

export function WagmiCreatePasskeySignerTest() {
const { isPasskeyAvailable, setPasskey, passkeyId } = useWagmiPermissionsSync()
const toast = useChakraToast()

async function handleCreatePasskey() {
try {
const credential = await createCredential({
name: 'Session key'
})
setPasskey(credential)
toast({
type: 'success',
title: 'Passkey created succesfully',
description: ''
})
} catch (error) {
toast({
type: 'error',
title: `Unable to create passkey`,
description: `Reason ${error}`
})
}
}

useEffect(() => {
const storedLocalSignerPrivateKey = getItem(LOCAL_SIGNER_KEY)
if (storedLocalSignerPrivateKey) {
privateKeyToAccount(storedLocalSignerPrivateKey as `0x${string}`)
}
}, [])

return (
<Stack direction={['column', 'column', 'row']} align={'center'}>
<Button data-testid="sign-message-button" onClick={handleCreatePasskey}>
Create New Passkey
</Button>
{isPasskeyAvailable && passkeyId && <div>Passkey Id: {passkeyId}</div>}
</Stack>
)
}

This file was deleted.

29 changes: 29 additions & 0 deletions apps/laboratory/src/components/Wagmi/WagmiPermissionsAsyncTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Box, Card, CardBody, CardHeader, Heading, Stack, StackDivider } from '@chakra-ui/react'
import { WagmiRequestPermissionsAsyncTest } from './WagmiRequestPermissionsAsyncTest'
import { WagmiPurchaseDonutAsyncPermissionsTest } from './WagmiPurchaseDonutAsyncPermissionsTest'

export function WagmiPermissionsAsyncTest() {
return (
<Card marginTop={10} marginBottom={10}>
<CardHeader>
<Heading size="md">Test Interactions</Heading>
</CardHeader>
<CardBody>
<Stack divider={<StackDivider />} spacing="4">
<Box>
<Heading size="xs" textTransform="uppercase" pb="2">
Request Permissions
</Heading>
<WagmiRequestPermissionsAsyncTest />
</Box>
<Box>
<Heading size="xs" textTransform="uppercase" pb="2">
Purchase Donut With Permissions
</Heading>
<WagmiPurchaseDonutAsyncPermissionsTest />
</Box>
</Stack>
</CardBody>
</Card>
)
}
36 changes: 36 additions & 0 deletions apps/laboratory/src/components/Wagmi/WagmiPermissionsSyncTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Box, Card, CardBody, CardHeader, Heading, Stack, StackDivider } from '@chakra-ui/react'
import { WagmiRequestPermissionsSyncTest } from './WagmiRequestPermissionsSyncTest'
import { WagmiPurchaseDonutSyncPermissionsTest } from './WagmiPurchaseDonutSyncPermissionsTest'
import { WagmiCreatePasskeySignerTest } from './WagmiCreatePasskeySignerTest'

export function WagmiPermissionsSyncTest() {
return (
<Card marginTop={10} marginBottom={10}>
<CardHeader>
<Heading size="md">Test Interactions</Heading>
</CardHeader>
<CardBody>
<Stack divider={<StackDivider />} spacing="4">
<Box>
<Heading size="xs" textTransform="uppercase" pb="2">
New Passkey
</Heading>
<WagmiCreatePasskeySignerTest />
</Box>
<Box>
<Heading size="xs" textTransform="uppercase" pb="2">
Request Permissions
</Heading>
<WagmiRequestPermissionsSyncTest />
</Box>
<Box>
<Heading size="xs" textTransform="uppercase" pb="2">
Purchase Donut With Permissions
</Heading>
<WagmiPurchaseDonutSyncPermissionsTest />
</Box>
</Stack>
</CardBody>
</Card>
)
}
51 changes: 0 additions & 51 deletions apps/laboratory/src/components/Wagmi/WagmiPermissionsTest.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { Button, Flex, Stack, Text } from '@chakra-ui/react'
import { useReadContract } from 'wagmi'
import { useState } from 'react'
import { useChakraToast } from '../Toast'
import { encodeFunctionData, parseEther } from 'viem'
import { abi as donutContractAbi, address as donutContractaddress } from '../../utils/DonutContract'
import { sepolia } from 'viem/chains'
import { useWagmiPermissionsAsync } from '../../context/WagmiPermissionsAsyncContext'
import { useERC7715PermissionsAsync } from '../../hooks/useERC7715PermissionsAsync'

export function WagmiPurchaseDonutAsyncPermissionsTest() {
const { grantedPermissions, wcCosignerData, privateKey, projectId } = useWagmiPermissionsAsync()

const { executeActionsWithECDSAAndCosignerPermissions } = useERC7715PermissionsAsync({
chain: sepolia,
permissions: grantedPermissions,
projectId
})

const {
data: donutsOwned,
refetch: fetchDonutsOwned,
isLoading: donutsQueryLoading,
isRefetching: donutsQueryRefetching
} = useReadContract({
abi: donutContractAbi,
address: donutContractaddress,
functionName: 'getBalance',
args: [grantedPermissions?.signerData?.submitToAddress || '0x']
})

const [isTransactionPending, setTransactionPending] = useState<boolean>(false)
const toast = useChakraToast()

async function onPurchaseDonutWithPermissions() {
setTransactionPending(true)
try {
if (!wcCosignerData) {
throw Error('No wc-cosigner data available')
}

if (!privateKey) {
throw new Error(`Unable to get dApp private key`)
}
const purchaseDonutCallData = encodeFunctionData({
abi: donutContractAbi,
functionName: 'purchase',
args: [1]
})
const purchaseDonutCallDataExecution = [
{
target: donutContractaddress as `0x${string}`,
value: parseEther('0.0001'),
callData: purchaseDonutCallData
}
]
const txHash = await executeActionsWithECDSAAndCosignerPermissions({
actions: purchaseDonutCallDataExecution,
chain: sepolia,
ecdsaPrivateKey: privateKey as `0x${string}`
})
if (txHash) {
toast({
title: 'Transaction success',
description: txHash,
type: 'success'
})
await fetchDonutsOwned()
}
} catch (error) {
// Console.log(error)
toast({
title: 'Transaction Failed',
description: `${error}`,
type: 'error'
})
}
setTransactionPending(false)
}
if (!grantedPermissions) {
return (
<Text fontSize="md" color="yellow">
Dapp does not have the permissions
</Text>
)
}

return (
<Stack direction={['column', 'column', 'row']}>
<Button
isDisabled={!grantedPermissions}
isLoading={isTransactionPending}
onClick={onPurchaseDonutWithPermissions}
>
Purchase Donut
</Button>
<Flex alignItems="center">
{donutsQueryLoading || donutsQueryRefetching ? (
<Text>Fetching donuts...</Text>
) : (
<>
<Text marginRight="5px">Crypto donuts left:</Text>
<Text>{donutsOwned?.toString()}</Text>
</>
)}
</Flex>
</Stack>
)
}
Loading
Loading