diff --git a/public/my.js b/public/my.js index d329bbc..6549641 100644 --- a/public/my.js +++ b/public/my.js @@ -319,6 +319,9 @@ const eth=await window.RMCWallet.pick(); if(!eth){err.textContent='No wallet found in this browser. On a phone, open your wallet app (SafePal, Phantom, MetaMask, Trust, Coinbase…), go to its Browser or DApp tab, and type this page\'s address there — your wallet connects automatically.';return;} const accs=await eth.request({method:'eth_requestAccounts'});const account=accs[0]; + // Match the wallet's active chain to the SIWE message's Chain ID (137), + // or the wallet rejects the signature ("chain ID does not match"). + try{await window.RMCWallet.ensureChain(eth);}catch(ce){err.textContent='Please switch your wallet to the Polygon network, then tap Connect again. (Your wallet blocks sign-in when it\'s on a different network.)';return;} const ch=await(await fetch('/api/public/msg-challenge',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({address:account})})).json(); if(!ch.message)throw new Error(ch.error||'Could not start sign-in.'); let hex='0x';for(const b of new TextEncoder().encode(ch.message))hex+=b.toString(16).padStart(2,'0'); diff --git a/public/rmc-wallet.js b/public/rmc-wallet.js index 8d408b9..b1c945c 100644 --- a/public/rmc-wallet.js +++ b/public/rmc-wallet.js @@ -77,6 +77,22 @@ }, 220); }); }, - list:function(){ return probeInjected(found.slice()); } + list:function(){ return probeInjected(found.slice()); }, + // Switch the wallet to Polygon (137) so a SIWE sign-in whose message says + // "Chain ID: 137" matches the wallet's active chain — otherwise wallets + // refuse the signature with a "chain ID does not match" error. Adds Polygon + // if the wallet doesn't have it. Returns a Promise. + ensureChain:function(eth){ + var POLY='0x89'; + return eth.request({method:'eth_chainId'}).then(function(cid){ + if(cid===POLY) return true; + return eth.request({method:'wallet_switchEthereumChain',params:[{chainId:POLY}]}).then(function(){return true;}).catch(function(e){ + if(e&&e.code===4902){ + return eth.request({method:'wallet_addEthereumChain',params:[{chainId:POLY,chainName:'Polygon Mainnet',nativeCurrency:{name:'POL',symbol:'POL',decimals:18},rpcUrls:['https://polygon-rpc.com'],blockExplorerUrls:['https://polygonscan.com']}]}).then(function(){return true;}); + } + throw e; + }); + }); + } }; })(); diff --git a/public/training.js b/public/training.js index e46fa80..03b9aa5 100644 --- a/public/training.js +++ b/public/training.js @@ -63,6 +63,7 @@ if(tid&&/^\d+$/.test(tid))document.querySelectorAll('a[href^="/tools"]').forEach const eth=await window.RMCWallet.pick(); if(!eth){err.textContent='No wallet in this browser. On a phone, open this page inside your wallet app\'s browser — or use the Telegram Mini App.';btn.disabled=false;btn.textContent=UNLOCK_LABEL;return;} const accs=await eth.request({method:'eth_requestAccounts'});const account=accs[0]; + try{await window.RMCWallet.ensureChain(eth);}catch(ce){err.textContent='Switch your wallet to the Polygon network, then tap unlock again.';btn.disabled=false;btn.textContent=UNLOCK_LABEL;return;} const ch=await(await fetch('/api/public/msg-challenge',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({address:account})})).json(); if(!ch.message)throw new Error(ch.error||'Could not start sign-in.'); let hex='0x';for(const b2 of new TextEncoder().encode(ch.message))hex+=b2.toString(16).padStart(2,'0');