@@ -104,8 +104,8 @@ export default function LinkExternalWallet() {
104104 // Generate a nonce for the signature
105105 const nonce = generateNonce ( ) ;
106106 // Ensure addresses are in the correct format - lowercase 0x-prefixed
107- const formattedExternalWalletAddress = externalWalletAddress . toLowerCase ( ) as `0x${string } `;
108- const formattedPassportAddress = accountAddress . toLowerCase ( ) as `0x${string } `;
107+ const metamaskAddress = externalWalletAddress . toLowerCase ( ) as `0x${string } `;
108+ const passportAddress = accountAddress . toLowerCase ( ) as `0x${string } `;
109109
110110 const dataToSign = {
111111 types : {
@@ -136,62 +136,52 @@ export default function LinkExternalWallet() {
136136 } ,
137137 primaryType : "LinkWallet" ,
138138 domain : {
139- chainId : 1 ,
139+ chainId : 1 , // Must be set to 1 for Ethereum Mainnet
140140 } ,
141141 message : {
142- walletAddress : formattedExternalWalletAddress ,
143- immutablePassportAddress : formattedPassportAddress ,
142+ walletAddress : metamaskAddress ,
143+ immutablePassportAddress : passportAddress ,
144144 condition : "I agree to link this wallet to my Immutable Passport account." ,
145145 nonce
146146 }
147147 }
148148
149- console . log ( 'dataToSign' , dataToSign ) ;
150-
151149 if ( typeof window === 'undefined' || ! window . ethereum ) {
152150 throw new Error ( 'MetaMask not installed' ) ;
153151 }
154152
155- // Sign the message using window.ethereum directly
156- const signature = await window . ethereum . request ( {
157- method : 'eth_signTypedData_v4' ,
158- params : [ formattedExternalWalletAddress , JSON . stringify ( dataToSign ) ]
159- } ) ;
160-
161- console . log ( 'signature' , signature ) ;
153+ let signature : string ;
162154
163- // Import and use our signature validation function
164- const { validateSignatureComprehensive } = await import ( '../utils/validateEIP712Signature' ) ;
165-
166- const isValid = await validateSignatureComprehensive (
167- formattedExternalWalletAddress , // the signer address
168- dataToSign , // the payload
169- signature ,
170- window . ethereum // pass the provider for contract wallet validation if needed
171- ) ;
155+ try {
156+ // Metamask must be connected to Ethereum Mainnet
157+ await window . ethereum . request ( {
158+ method : 'wallet_switchEthereumChain' ,
159+ params : [ { chainId : '0x1' } ] , // chainId must be in hexadecimal format
160+ } ) ;
172161
173- console . log ( 'isValid' , isValid ) ;
162+ // Now request the signature
163+ signature = await window . ethereum . request ( {
164+ method : 'eth_signTypedData_v4' ,
165+ params : [ metamaskAddress , JSON . stringify ( dataToSign ) ]
166+ } ) ;
174167
175- if ( ! isValid ) {
176- throw new Error ( 'Invalid signature' ) ;
168+ } catch ( error ) {
169+ console . error ( 'Error signing message:' , error ) ;
170+ setLinkingError ( 'Failed to sign message' ) ;
171+ setLinkingStatus ( 'Make sure you have MetaMask installed and connected to Ethereum Mainnet' ) ;
172+ setIsLinking ( false ) ;
173+ return
177174 }
178-
175+
179176 setLinkingStatus ( 'Linking wallet...' ) ;
180177
181- console . log ( 'linkExternalWallet type:' , "External" ) ;
182- console . log ( 'linkExternalWallet walletAddress:' , formattedExternalWalletAddress ) ;
183- console . log ( 'linkExternalWallet signature:' , signature ) ;
184- console . log ( 'linkExternalWallet nonce:' , nonce ) ;
185-
186178 // Call the linkExternalWallet method to link the wallet
187179 const result = await passportInstance . linkExternalWallet ( {
188180 type : "External" ,
189- walletAddress : formattedExternalWalletAddress ,
181+ walletAddress : metamaskAddress ,
190182 signature,
191183 nonce
192184 } ) ;
193-
194- console . log ( 'result' , result ) ;
195185
196186 const linkedAddresses = await passportInstance . getLinkedAddresses ( ) ;
197187 setLinkedAddresses ( linkedAddresses ) ;
@@ -266,12 +256,7 @@ export default function LinkExternalWallet() {
266256 < Table . Cell > < b > External Wallet</ b > </ Table . Cell >
267257 < Table . Cell > { externalWalletAddress || 'Not connected' } </ Table . Cell >
268258 </ Table . Row >
269- { linkingStatus && (
270- < Table . Row >
271- < Table . Cell > < b > Status</ b > </ Table . Cell >
272- < Table . Cell > { linkingStatus } </ Table . Cell >
273- </ Table . Row >
274- ) }
259+
275260 { linkedAddresses . length > 0 && (
276261 < Table . Row >
277262 < Table . Cell > < b > Linked Addresses</ b > </ Table . Cell >
@@ -284,6 +269,12 @@ export default function LinkExternalWallet() {
284269 < Table . Cell > { linkingError } </ Table . Cell >
285270 </ Table . Row >
286271 ) }
272+ { linkingStatus && (
273+ < Table . Row >
274+ < Table . Cell > < b > Message</ b > </ Table . Cell >
275+ < Table . Cell > { linkingStatus } </ Table . Cell >
276+ </ Table . Row >
277+ ) }
287278 </ Table . Body >
288279 </ Table >
289280
0 commit comments