|
| 1 | +import { InvalidAddressError, TssVerifyAddressOptions } from '../../baseCoin/iBaseCoin'; |
| 2 | +import { EDDSAMethods } from '../../tss'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Verifies if an address belongs to a wallet using EdDSA TSS MPC derivation. |
| 6 | + * This is a common implementation for EdDSA-based MPC coins (SOL, DOT, SUI, TON, IOTA, etc.) |
| 7 | + * |
| 8 | + * @param params - Verification options including keychains, address, and derivation index |
| 9 | + * @param isValidAddress - Coin-specific function to validate address format |
| 10 | + * @param getAddressFromPublicKey - Coin-specific function to convert public key to address |
| 11 | + * @returns true if the address matches the derived address, false otherwise |
| 12 | + * @throws {InvalidAddressError} if the address is invalid |
| 13 | + * @throws {Error} if required parameters are missing or invalid |
| 14 | + */ |
| 15 | +export async function verifyEddsaTssWalletAddress( |
| 16 | + params: TssVerifyAddressOptions, |
| 17 | + isValidAddress: (address: string) => boolean, |
| 18 | + getAddressFromPublicKey: (publicKey: string) => string |
| 19 | +): Promise<boolean> { |
| 20 | + const { keychains, address, index } = params; |
| 21 | + |
| 22 | + if (!isValidAddress(address)) { |
| 23 | + throw new InvalidAddressError(`invalid address: ${address}`); |
| 24 | + } |
| 25 | + |
| 26 | + if (!keychains || keychains.length === 0) { |
| 27 | + throw new Error('missing required param keychains'); |
| 28 | + } |
| 29 | + |
| 30 | + // For MPC coins, commonKeychain should be the same for all keychains |
| 31 | + const commonKeychain = keychains[0].commonKeychain as string; |
| 32 | + if (!commonKeychain) { |
| 33 | + throw new Error('missing required param commonKeychain'); |
| 34 | + } |
| 35 | + |
| 36 | + // Verify all keychains have the same commonKeychain |
| 37 | + for (const keychain of keychains) { |
| 38 | + if (keychain.commonKeychain !== commonKeychain) { |
| 39 | + throw new Error('all keychains must have the same commonKeychain for MPC coins'); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Only perform derivation once since commonKeychain is the same |
| 44 | + const MPC = await EDDSAMethods.getInitializedMpcInstance(); |
| 45 | + const derivationPath = 'm/' + index; |
| 46 | + const derivedPublicKey = MPC.deriveUnhardened(commonKeychain, derivationPath).slice(0, 64); |
| 47 | + const expectedAddress = getAddressFromPublicKey(derivedPublicKey); |
| 48 | + |
| 49 | + return address === expectedAddress; |
| 50 | +} |
0 commit comments