-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(BA-2555): Set primary flow for EOAs in Web #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
Changes from all commits
614d8ea
ee8e736
bf7ae22
3e8c3a6
7d3ebd7
9b1d482
9ddefb3
a1ad726
44138ef
9824676
3a4f68e
b9af725
28099c0
629be42
369ba5e
843af38
0c63511
cebb1c7
771e96d
b49447a
5acb83e
d6b0b0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,20 @@ import { | |
| USERNAME_REVERSE_REGISTRAR_ADDRESSES, | ||
| } from 'apps/web/src/addresses/usernames'; | ||
| import useBasenameChain from 'apps/web/src/hooks/useBasenameChain'; | ||
| import { useCallback, useEffect } from 'react'; | ||
| import { useCallback, useEffect, useState } from 'react'; | ||
| import { Basename } from '@coinbase/onchainkit/identity'; | ||
| import { useAccount } from 'wagmi'; | ||
| import { useAccount, useSignMessage } from 'wagmi'; | ||
| import useBaseEnsName from 'apps/web/src/hooks/useBaseEnsName'; | ||
| import { useErrors } from 'apps/web/contexts/Errors'; | ||
| import useWriteContractWithReceipt from 'apps/web/src/hooks/useWriteContractWithReceipt'; | ||
| import { useUsernameProfile } from 'apps/web/src/components/Basenames/UsernameProfileContext'; | ||
| import useWriteContractsWithLogs from 'apps/web/src/hooks/useWriteContractsWithLogs'; | ||
| import useCapabilitiesSafe from 'apps/web/src/hooks/useCapabilitiesSafe'; | ||
| import L2ReverseRegistrarAbi from 'apps/web/src/abis/L2ReverseRegistrarAbi'; | ||
| import UpgradeableRegistrarControllerAbi from 'apps/web/src/abis/UpgradeableRegistrarControllerAbi'; | ||
| import { UPGRADEABLE_REGISTRAR_CONTROLLER_ADDRESSES } from 'apps/web/src/addresses/usernames'; | ||
| import { type AbiFunction } from 'viem'; | ||
| import { buildReverseRegistrarSignatureDigest } from 'apps/web/src/utils/usernames'; | ||
|
|
||
| /* | ||
| A hook to set a name as primary for resolution. | ||
|
|
@@ -38,6 +42,9 @@ export default function useSetPrimaryBasename({ secondaryUsername }: UseSetPrima | |
| const { paymasterService: paymasterServiceEnabled } = useCapabilitiesSafe({ | ||
| chainId: secondaryUsernameChain.id, | ||
| }); | ||
| const { signMessageAsync } = useSignMessage(); | ||
|
|
||
| const [signatureError, setSignatureError] = useState<Error | null>(null); | ||
|
|
||
| // Get current primary username | ||
| // Note: This is sometimes undefined | ||
|
|
@@ -65,6 +72,28 @@ export default function useSetPrimaryBasename({ secondaryUsername }: UseSetPrima | |
| eventName: 'update_primary_name', | ||
| }); | ||
|
|
||
| const signMessageForReverseRecord = useCallback(async () => { | ||
| if (!address) throw new Error('No address'); | ||
|
|
||
| const reverseRegistrar = USERNAME_L2_REVERSE_REGISTRAR_ADDRESSES[secondaryUsernameChain.id]; | ||
| const functionAbi = L2ReverseRegistrarAbi.find( | ||
| (f) => f.type === 'function' && f.name === 'setNameForAddrWithSignature', | ||
| ) as unknown as AbiFunction; | ||
|
|
||
| const signatureExpiry = BigInt(Math.floor(Date.now() / 1000) + 5 * 60); | ||
| const nameLabel = (secondaryUsername as string).split('.')[0]; | ||
| const { digest, coinTypes } = buildReverseRegistrarSignatureDigest({ | ||
| reverseRegistrar, | ||
| functionAbi, | ||
| address, | ||
| chainId: secondaryUsernameChain.id, | ||
| name: nameLabel, | ||
| signatureExpiry, | ||
| }); | ||
| const signature = await signMessageAsync({ message: { raw: digest } }); | ||
| return { coinTypes, signatureExpiry, signature } as const; | ||
| }, [address, secondaryUsername, secondaryUsernameChain.id, signMessageAsync]); | ||
|
|
||
| useEffect(() => { | ||
| if (transactionIsSuccess) { | ||
| refetchPrimaryUsername() | ||
|
|
@@ -82,16 +111,28 @@ export default function useSetPrimaryBasename({ secondaryUsername }: UseSetPrima | |
|
|
||
| try { | ||
| if (!paymasterServiceEnabled) { | ||
| let payload: { | ||
| coinTypes: readonly bigint[]; | ||
| signatureExpiry: bigint; | ||
| signature: `0x${string}`; | ||
| }; | ||
| try { | ||
| setSignatureError(null); | ||
| payload = await signMessageForReverseRecord(); | ||
| } catch (e) { | ||
| logError(e, 'Reverse record signature step failed'); | ||
| const msg = e instanceof Error && e.message ? e.message : 'Unknown error'; | ||
| setSignatureError(new Error(`Could not prepare reverse record signature: ${msg}`)); | ||
| return undefined; | ||
amiecorso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const nameLabel = (secondaryUsername as string).split('.')[0]; | ||
|
|
||
| await initiateTransaction({ | ||
| abi: ReverseRegistrarAbi, | ||
| address: USERNAME_REVERSE_REGISTRAR_ADDRESSES[secondaryUsernameChain.id], | ||
| args: [ | ||
| address, | ||
| address, | ||
| USERNAME_L2_RESOLVER_ADDRESSES[secondaryUsernameChain.id], | ||
| secondaryUsername, | ||
| ], | ||
| functionName: 'setNameForAddr', | ||
| abi: UpgradeableRegistrarControllerAbi, | ||
| address: UPGRADEABLE_REGISTRAR_CONTROLLER_ADDRESSES[secondaryUsernameChain.id], | ||
| args: [nameLabel, payload.signatureExpiry, payload.coinTypes, payload.signature], | ||
| functionName: 'setReverseRecord', | ||
| }); | ||
| } else { | ||
| await initiateBatchCalls({ | ||
|
|
@@ -130,7 +171,9 @@ export default function useSetPrimaryBasename({ secondaryUsername }: UseSetPrima | |
| address, | ||
| paymasterServiceEnabled, | ||
| initiateTransaction, | ||
| secondaryUsernameChain, | ||
| secondaryUsernameChain.id, | ||
| signMessageForReverseRecord, | ||
| initiateBatchCalls, | ||
| logError, | ||
| ]); | ||
|
|
@@ -146,5 +189,7 @@ export default function useSetPrimaryBasename({ secondaryUsername }: UseSetPrima | |
| canSetUsernameAsPrimary, | ||
| isLoading, | ||
| transactionIsSuccess: transactionIsSuccess || batchCallsIsSuccess, | ||
| transactionPending: transactionIsLoading || batchCallsIsLoading, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this used anywhere?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, it's used to set |
||
| error: signatureError, | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.