@@ -5,9 +5,11 @@ import { Input } from "@/components/ui/input";
55import { Label } from "@/components/ui/label" ;
66import { Button } from "@/components/ui/button" ;
77import { Loader2 , Info , Shield , ArrowRight } from "lucide-react" ;
8- import { isAddress as isEvmAddress } from "viem" ; // For ETH address validation
9- import { normalize } from "viem/ens" ;
10- import { viemClient } from "@/lib/walletLinking/viem" ;
8+ import {
9+ getViemClient ,
10+ isAddress as isEvmAddressAsync ,
11+ normalizeEns ,
12+ } from "@/lib/walletLinking/viem" ;
1113import { LinkedWallet } from "@/lib/walletLinking/readmeUtils" ;
1214import { resolveSolDomain } from "@/lib/walletLinking/sns" ;
1315
@@ -51,19 +53,26 @@ export function WalletLinkForm({
5153 setEthAddress ( ethWallet ?. address || "" ) ;
5254 setSolAddress ( solWallet ?. address || "" ) ;
5355
54- if ( ! ethWallet ?. address || isEvmAddress ( ethWallet . address ) ) {
55- setIsEthValid ( true ) ;
56- setEthAddressError ( "" ) ;
57- } else {
58- setIsEthValid ( false ) ;
59- }
56+ // Validate initial addresses asynchronously
57+ const validateInitialAddresses = async ( ) => {
58+ if ( ethWallet ?. address ) {
59+ const isValid = await isEvmAddressAsync ( ethWallet . address ) ;
60+ setIsEthValid ( isValid ) ;
61+ if ( ! isValid ) {
62+ setEthAddressError ( "Invalid Ethereum address" ) ;
63+ }
64+ }
6065
61- if ( ! solWallet ?. address || SOL_ADDRESS_REGEX . test ( solWallet . address ) ) {
62- setIsSolValid ( true ) ;
63- setSolAddressError ( "" ) ;
64- } else {
65- setIsSolValid ( false ) ;
66- }
66+ if ( solWallet ?. address ) {
67+ const isValid = SOL_ADDRESS_REGEX . test ( solWallet . address ) ;
68+ setIsSolValid ( isValid ) ;
69+ if ( ! isValid ) {
70+ setSolAddressError ( "Invalid Solana address" ) ;
71+ }
72+ }
73+ } ;
74+
75+ validateInitialAddresses ( ) ;
6776 } , [ wallets ] ) ;
6877
6978 useEffect ( ( ) => {
@@ -73,12 +82,16 @@ export function WalletLinkForm({
7382 return ;
7483 }
7584
76- const isEVMValid = isEvmAddress ( ethAddress ) ;
77- const isENSValid = ENS_NAME_REGEX . test ( ethAddress ) ;
78- setIsEthValid ( isEVMValid || isENSValid ) ;
79- setEthAddressError (
80- isEVMValid || isENSValid ? "" : "Invalid Ethereum address or ENS name." ,
81- ) ;
85+ const validateEthAddress = async ( ) => {
86+ const isEVMValid = await isEvmAddressAsync ( ethAddress ) ;
87+ const isENSValid = ENS_NAME_REGEX . test ( ethAddress ) ;
88+ setIsEthValid ( isEVMValid || isENSValid ) ;
89+ setEthAddressError (
90+ isEVMValid || isENSValid ? "" : "Invalid Ethereum address or ENS name." ,
91+ ) ;
92+ } ;
93+
94+ validateEthAddress ( ) ;
8295 } , [ ethAddress ] ) ;
8396
8497 useEffect ( ( ) => {
@@ -107,11 +120,13 @@ export function WalletLinkForm({
107120
108121 if ( ethAddress ) {
109122 const isENSValid = ENS_NAME_REGEX . test ( ethAddress ) ;
110- const address = isENSValid
111- ? await viemClient . getEnsAddress ( {
112- name : normalize ( ethAddress ) ,
113- } )
114- : ethAddress ;
123+ let address : string | null = ethAddress ;
124+
125+ if ( isENSValid ) {
126+ const viemClient = await getViemClient ( ) ;
127+ const normalizedName = await normalizeEns ( ethAddress ) ;
128+ address = await viemClient . getEnsAddress ( { name : normalizedName } ) ;
129+ }
115130
116131 // If the address is not found, set the error and return
117132 if ( ! address ) {
0 commit comments