|
| 1 | +import { |
| 2 | + Chain, |
| 3 | + chainToPlatform, |
| 4 | + encoding, |
| 5 | + NativeAddress, |
| 6 | + toNative, |
| 7 | +} from '@wormhole-foundation/sdk'; |
| 8 | +import { isValidSuiAddress } from '@mysten/sui.js'; |
| 9 | +import { Connection, PublicKey } from '@solana/web3.js'; |
| 10 | +import { |
| 11 | + getAccount, |
| 12 | + TOKEN_2022_PROGRAM_ID, |
| 13 | + TOKEN_PROGRAM_ID, |
| 14 | +} from '@solana/spl-token'; |
| 15 | +import { getAddress } from 'ethers'; |
| 16 | +import config from 'config'; |
| 17 | + |
| 18 | +function isValidEvmAddress(address: string): boolean { |
| 19 | + if ( |
| 20 | + !address.startsWith('0x') || |
| 21 | + address.length !== 42 || |
| 22 | + !encoding.hex.valid(address) |
| 23 | + ) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + getAddress(address); |
| 29 | + return true; |
| 30 | + } catch { |
| 31 | + return false; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function isValidSolanaAddress(address: string): Promise<boolean> { |
| 36 | + try { |
| 37 | + const key = new PublicKey(address); |
| 38 | + if (config.rpcs.Solana) { |
| 39 | + const connection = new Connection(config.rpcs.Solana); |
| 40 | + const results = await Promise.allSettled([ |
| 41 | + getAccount(connection, key, 'finalized', TOKEN_PROGRAM_ID), |
| 42 | + getAccount(connection, key, 'finalized', TOKEN_2022_PROGRAM_ID), |
| 43 | + ]); |
| 44 | + // A token account is not a valid wallet address |
| 45 | + if (results.some((r) => r.status === 'fulfilled')) return false; |
| 46 | + } |
| 47 | + return true; |
| 48 | + } catch { |
| 49 | + return false; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function isValidAptosAddress(address: string): boolean { |
| 54 | + return ( |
| 55 | + address.startsWith('0x') && |
| 56 | + address.length === 66 && |
| 57 | + encoding.hex.valid(address) |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +export async function validateWalletAddress( |
| 62 | + chain: Chain, |
| 63 | + address: string, |
| 64 | +): Promise<NativeAddress<Chain> | null> { |
| 65 | + const platform = chainToPlatform(chain); |
| 66 | + |
| 67 | + // toNative() is permissive and accepts various address formats, |
| 68 | + // including ICAP Ethereum addresses, hex-encoded Solana addresses, and attempts to parse as a UniversalAddress if parsing fails. |
| 69 | + // We are being more restrictive here to prevent the user from accidentally using an incorrect address. |
| 70 | + switch (platform) { |
| 71 | + case 'Evm': |
| 72 | + if (!isValidEvmAddress(address)) return null; |
| 73 | + break; |
| 74 | + case 'Solana': |
| 75 | + if (!(await isValidSolanaAddress(address))) return null; |
| 76 | + break; |
| 77 | + case 'Sui': |
| 78 | + if (!isValidSuiAddress(address)) return null; |
| 79 | + break; |
| 80 | + case 'Aptos': |
| 81 | + if (!isValidAptosAddress(address)) return null; |
| 82 | + break; |
| 83 | + default: |
| 84 | + console.warn(`Unsupported platform: ${platform}`); |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + try { |
| 89 | + // This will throw an error if the address is invalid |
| 90 | + return toNative(chain, address); |
| 91 | + } catch (e) { |
| 92 | + console.error( |
| 93 | + `Invalid address for chain ${chain}: ${address}, error: ${e}`, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + return null; |
| 98 | +} |
0 commit comments