@@ -5,9 +5,11 @@ import { Input } from "@/components/ui/input";
5
5
import { Label } from "@/components/ui/label" ;
6
6
import { Button } from "@/components/ui/button" ;
7
7
import { 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" ;
11
13
import { LinkedWallet } from "@/lib/walletLinking/readmeUtils" ;
12
14
import { resolveSolDomain } from "@/lib/walletLinking/sns" ;
13
15
@@ -51,19 +53,26 @@ export function WalletLinkForm({
51
53
setEthAddress ( ethWallet ?. address || "" ) ;
52
54
setSolAddress ( solWallet ?. address || "" ) ;
53
55
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
+ }
60
65
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 ( ) ;
67
76
} , [ wallets ] ) ;
68
77
69
78
useEffect ( ( ) => {
@@ -73,12 +82,16 @@ export function WalletLinkForm({
73
82
return ;
74
83
}
75
84
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 ( ) ;
82
95
} , [ ethAddress ] ) ;
83
96
84
97
useEffect ( ( ) => {
@@ -107,11 +120,13 @@ export function WalletLinkForm({
107
120
108
121
if ( ethAddress ) {
109
122
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
+ }
115
130
116
131
// If the address is not found, set the error and return
117
132
if ( ! address ) {
0 commit comments