@@ -5,32 +5,21 @@ 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 {
9- getViemClient ,
10- isAddress as isEvmAddressAsync ,
11- normalizeEns ,
12- } from "@/lib/walletLinking/viem" ;
138import { LinkedWallet } from "@/lib/walletLinking/readmeUtils" ;
14- import { resolveSolDomain } from "@/lib/walletLinking/sns" ;
9+ import { validateAddress } from "@/lib/walletLinking/chainUtils" ;
10+ import {
11+ resolveSnsDomain ,
12+ resolveEnsDomain ,
13+ validateEnsFormat ,
14+ validateSnsFormat ,
15+ } from "@/lib/walletLinking/domain" ;
1516
1617interface WalletLinkFormProps {
1718 wallets : LinkedWallet [ ] ;
1819 onSubmit : ( wallets : LinkedWallet [ ] ) => Promise < void > ;
1920 isProcessing : boolean ;
2021}
2122
22- // Basic regex for Solana address (Base58, 32-44 chars)
23- // For more robust validation, consider @solana/web3.js PublicKey.isOnCurve or similar
24- const SOL_ADDRESS_REGEX = / ^ [ 1 - 9 A - H J - N P - Z a - k m - z ] { 32 , 44 } $ / ;
25-
26- // ENS name regex (name.eth format)
27- // Matches names that end with .eth and contain valid characters
28- const ENS_NAME_REGEX = / ^ [ a - z A - Z 0 - 9 ] ( [ a - z A - Z 0 - 9 - ] * [ a - z A - Z 0 - 9 ] ) ? \. e t h $ / ;
29-
30- // SNS name regex (name.sol format)
31- // Matches names that end with .sol and contain valid characters
32- const SNS_NAME_REGEX = / ^ [ a - z A - Z 0 - 9 ] ( [ a - z A - Z 0 - 9 - ] * [ a - z A - Z 0 - 9 ] ) ? \. s o l $ / ;
33-
3423export function WalletLinkForm ( {
3524 wallets = [ ] ,
3625 onSubmit,
@@ -56,15 +45,15 @@ export function WalletLinkForm({
5645 // Validate initial addresses asynchronously
5746 const validateInitialAddresses = async ( ) => {
5847 if ( ethWallet ?. address ) {
59- const isValid = await isEvmAddressAsync ( ethWallet . address ) ;
48+ const isValid = await validateAddress ( ethWallet . address , "ethereum" ) ;
6049 setIsEthValid ( isValid ) ;
6150 if ( ! isValid ) {
6251 setEthAddressError ( "Invalid Ethereum address" ) ;
6352 }
6453 }
6554
6655 if ( solWallet ?. address ) {
67- const isValid = SOL_ADDRESS_REGEX . test ( solWallet . address ) ;
56+ const isValid = await validateAddress ( solWallet . address , "solana" ) ;
6857 setIsSolValid ( isValid ) ;
6958 if ( ! isValid ) {
7059 setSolAddressError ( "Invalid Solana address" ) ;
@@ -83,8 +72,8 @@ export function WalletLinkForm({
8372 }
8473
8574 const validateEthAddress = async ( ) => {
86- const isEVMValid = await isEvmAddressAsync ( ethAddress ) ;
87- const isENSValid = ENS_NAME_REGEX . test ( ethAddress ) ;
75+ const isEVMValid = await validateAddress ( ethAddress , "ethereum" ) ;
76+ const isENSValid = validateEnsFormat ( ethAddress ) ;
8877 setIsEthValid ( isEVMValid || isENSValid ) ;
8978 setEthAddressError (
9079 isEVMValid || isENSValid ? "" : "Invalid Ethereum address or ENS name." ,
@@ -101,12 +90,16 @@ export function WalletLinkForm({
10190 return ;
10291 }
10392
104- const isSOLValid = SOL_ADDRESS_REGEX . test ( solAddress ) ;
105- const isSNSValid = SNS_NAME_REGEX . test ( solAddress ) ;
106- setIsSolValid ( isSNSValid || isSOLValid ) ;
107- setSolAddressError (
108- isSNSValid || isSOLValid ? "" : "Invalid Solana address or SNS name." ,
109- ) ;
93+ const validateSolAddress = async ( ) => {
94+ const isSOLValid = await validateAddress ( solAddress , "solana" ) ;
95+ const isSNSValid = validateSnsFormat ( solAddress ) ;
96+ setIsSolValid ( isSNSValid || isSOLValid ) ;
97+ setSolAddressError (
98+ isSNSValid || isSOLValid ? "" : "Invalid Solana address or SNS name." ,
99+ ) ;
100+ } ;
101+
102+ validateSolAddress ( ) ;
110103 } , [ solAddress ] ) ;
111104
112105 const handleSubmit = async ( e : FormEvent < HTMLFormElement > ) => {
@@ -119,14 +112,10 @@ export function WalletLinkForm({
119112 const updatedWallets : LinkedWallet [ ] = [ ] ;
120113
121114 if ( ethAddress ) {
122- const isENSValid = ENS_NAME_REGEX . test ( 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+ const isENSValid = validateEnsFormat ( ethAddress ) ;
116+ const address = isENSValid
117+ ? await resolveEnsDomain ( ethAddress )
118+ : ethAddress ;
130119
131120 // If the address is not found, set the error and return
132121 if ( ! address ) {
@@ -142,9 +131,9 @@ export function WalletLinkForm({
142131 }
143132
144133 if ( solAddress ) {
145- const isSNSValid = SNS_NAME_REGEX . test ( solAddress ) ;
134+ const isSNSValid = validateSnsFormat ( solAddress ) ;
146135 const address = isSNSValid
147- ? await resolveSolDomain ( solAddress )
136+ ? await resolveSnsDomain ( solAddress )
148137 : solAddress ;
149138
150139 // If the address is not found, set the error and return
0 commit comments