Skip to content
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

Waas connector typed data sig support #223

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 2 deletions examples/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"@0xsequence/kit-wallet": "workspace:*",
"@0xsequence/kit-example-shared-components": "workspace:*",
"@tanstack/react-query": "^5.37.1",
"@0xsequence/network": "2.1.4",
"@0xsequence/waas": "2.1.4",
"@0xsequence/network": ">= 2.1.4",
"@0xsequence/waas": "0.0.0-20241218203257",
"framer-motion": "^8.5.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
97 changes: 97 additions & 0 deletions examples/react/src/components/Connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export const Connected = () => {
const [isSigningMessage, setIsSigningMessage] = React.useState(false)
const [isMessageValid, setIsMessageValid] = React.useState<boolean | undefined>()
const [messageSig, setMessageSig] = React.useState<string | undefined>()
const [isSigningTypedData, setIsSigningTypedData] = React.useState(false)
const [typedDataSig, setTypedDataSig] = React.useState<string | undefined>()
const [isTypedDataValid, setIsTypedDataValid] = React.useState<boolean | undefined>()

const [lastTxnDataHash, setLastTxnDataHash] = React.useState<string | undefined>()
const [lastTxnDataHash2, setLastTxnDataHash2] = React.useState<string | undefined>()
Expand Down Expand Up @@ -192,6 +195,69 @@ export const Connected = () => {
}
}, [txnData, txnData2, txnData3])

const domain = {
name: 'Sequence Example',
version: '1',
chainId: chainId,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC'
} as const

const types = {
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' }
]
} as const

const value = {
name: 'John Doe',
wallet: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC'
} as const

const signTypedData = async () => {
if (!walletClient || !address || !publicClient) {
return
}

setIsSigningTypedData(true)

try {
const sig = await walletClient.signTypedData({
account: address,
domain,
types,
primaryType: 'Person',
message: value
})

console.log('signature:', sig)

const [account] = await walletClient.getAddresses()

const isValid = await publicClient.verifyTypedData({
address: account,
domain,
types,
primaryType: 'Person',
message: value,
signature: sig
})

console.log('isValid?', isValid)

setTypedDataSig(sig)
setIsTypedDataValid(isValid)
setIsSigningTypedData(false)
} catch (e) {
setIsSigningTypedData(false)
if (e instanceof Error) {
console.error(e.cause)
} else {
console.error(e)
}
}
}

const signMessage = async () => {
if (!walletClient || !publicClient) {
return
Expand Down Expand Up @@ -411,6 +477,7 @@ export const Connected = () => {
setLastTxnDataHash2(undefined)
setLastTxnDataHash3(undefined)
setIsMessageValid(undefined)
setTypedDataSig(undefined)
resetWriteContract()
resetSendUnsponsoredTransaction()
resetSendTransaction()
Expand Down Expand Up @@ -503,6 +570,36 @@ export const Connected = () => {
</Text>
</Card>
)}
<CardButton
title="Sign typed data"
description="Sign typed data with your wallet"
onClick={signTypedData}
isPending={isSigningTypedData}
/>
{typedDataSig && (
<Card style={{ width: '332px' }} color={'text100'} flexDirection={'column'} gap={'2'}>
<Text variant="medium">Signed typed data:</Text>
<Text variant="code" as="p">
{JSON.stringify(
{
domain,
types,
primaryType: 'Person',
message: value
},
null,
2
)}
</Text>
<Text variant="medium">Signature:</Text>
<Text variant="code" as="p" ellipsis>
{typedDataSig}
</Text>
<Text variant="medium">
isValid: <Text variant="code">{isTypedDataValid?.toString()}</Text>
</Text>
</Card>
)}
<CardButton title="Add Funds" description="Buy Cryptocurrency with a Credit Card" onClick={() => onClickAddFunds()} />
{(chainId === ChainId.ARBITRUM_NOVA || chainId === ChainId.ARBITRUM_SEPOLIA) && (
<CardButton
Expand Down
18 changes: 12 additions & 6 deletions examples/react/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ export const config =
},
walletConnect: {
projectId: walletConnectProjectId
}
},
isDev: isDebugMode
})
: createConfig('universal', {
...kitConfig,
appName: 'Kit Demo',
chainIds: [
ChainId.ARBITRUM_NOVA,
ChainId.ARBITRUM,
ChainId.ARBITRUM_SEPOLIA,
ChainId.POLYGON,
ChainId.IMMUTABLE_ZKEVM,
Expand All @@ -110,10 +112,14 @@ export const getErc1155SaleContractConfig = (walletAddress: string) => ({
// contractAddress: '0xf0056139095224f4eec53c578ab4de1e227b9597',
// collectionAddress: '0x92473261f2c26f2264429c451f70b0192f858795',
wallet: walletAddress,
items: [{
tokenId: '1',
quantity: '1'
}],
onSuccess: () => { console.log('success') },
items: [
{
tokenId: '1',
quantity: '1'
}
],
onSuccess: () => {
console.log('success')
},
isDev: isDebugMode
})
18 changes: 9 additions & 9 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@0xsequence/api": "^2.1.4",
"@0xsequence/auth": "^2.1.4",
"@0xsequence/core": "^2.1.4",
"@0xsequence/api": ">= 2.1.4",
"@0xsequence/auth": ">= 2.1.4",
"@0xsequence/core": ">= 2.1.4",
"@0xsequence/design-system": "^1.7.8",
"@0xsequence/ethauth": "^1.0.0",
"@0xsequence/indexer": "^2.1.4",
"@0xsequence/metadata": "^2.1.4",
"@0xsequence/network": "^2.1.4",
"@0xsequence/provider": "^2.1.4",
"@0xsequence/utils": "^2.1.4",
"@0xsequence/waas": "^2.1.4",
"@0xsequence/indexer": ">= 2.1.4",
"@0xsequence/metadata": ">= 2.1.4",
"@0xsequence/network": ">= 2.1.4",
"@0xsequence/provider": ">= 2.1.4",
"@0xsequence/utils": ">= 2.1.4",
"@0xsequence/waas": "0.0.0-20241218203257",
"framer-motion": "^8.5.2",
"uuid": "^10.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,7 @@ export class SequenceWaasProvider extends ethers.AbstractProvider implements EIP
}
}

if (
method === 'eth_sign' ||
method === 'eth_signTypedData' ||
method === 'eth_signTypedData_v4' ||
method === 'personal_sign'
) {
if (method === 'eth_sign' || method === 'personal_sign') {
if (this.requestConfirmationHandler && this.showConfirmation) {
const id = uuidv4()
const confirmation = await this.requestConfirmationHandler.confirmSignMessageRequest(
Expand Down Expand Up @@ -422,6 +417,47 @@ export class SequenceWaasProvider extends ethers.AbstractProvider implements EIP
return sig.data.signature
}

if (method === 'eth_signTypedData' || method === 'eth_signTypedData_v4') {
if (this.requestConfirmationHandler && this.showConfirmation) {
const id = uuidv4()
const confirmation = await this.requestConfirmationHandler.confirmSignMessageRequest(
id,
JSON.stringify(JSON.parse(params?.[1]), null, 2), // Pretty print the typed data for confirmation
Number(this.currentNetwork.chainId)
)

if (!confirmation.confirmed) {
throw new UserRejectedRequestError(new Error('User rejected sign typed data request'))
}

if (id !== confirmation.id) {
throw new UserRejectedRequestError(new Error('User confirmation ids do not match'))
}
}

let sig

try {
sig = await this.sequenceWaas.signTypedData({
typedData: JSON.parse(params?.[1]),
network: Number(this.currentNetwork.chainId)
})
} catch (error) {
if (isSessionInvalidOrNotFoundError(error)) {
await this.emit('error', error)
throw new ProviderDisconnectedError(new Error('Provider is not connected'))
} else {
const message =
typeof error === 'object' && error !== null && 'cause' in error
? (String(error.cause) ?? 'Failed to sign typed data')
: 'Failed to sign typed data'
throw new InternalRpcError(new Error(message))
}
}

return sig.data.signature
}

return await this.jsonRpcProvider.send(method, params ?? [])
}

Expand Down
Loading
Loading