Skip to content

Commit 03254f8

Browse files
Merge pull request #85 from Web3Auth/IRT-958-support-switch-chain
feat: support switch chain
2 parents 903bfeb + 8b1735b commit 03254f8

File tree

5 files changed

+177
-66
lines changed

5 files changed

+177
-66
lines changed

demo/redirect-flow-example/package-lock.json

Lines changed: 19 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/redirect-flow-example/src/App.tsx

Lines changed: 100 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Web3 from "web3";
44
import type { provider } from "web3-core";
55

66
import "./App.css";
7-
import { SafeEventEmitterProvider } from "@web3auth/base";
7+
import { CHAIN_NAMESPACES, SafeEventEmitterProvider } from "@web3auth/base";
88
import { BN } from "bn.js";
99

1010
const uiConsole = (...args: any[]): void => {
@@ -45,7 +45,7 @@ function App() {
4545
useEffect(() => {
4646
const init = async () => {
4747
// Example config to handle redirect result manually
48-
await coreKitInstance.init({ handleRedirectResult: false, rehydrate});
48+
await coreKitInstance.init({ handleRedirectResult: false, rehydrate });
4949
if (window.location.hash.includes("#state")) {
5050
await coreKitInstance.handleRedirectResult();
5151
}
@@ -78,7 +78,7 @@ function App() {
7878
}
7979
}, [provider])
8080

81-
81+
8282
const keyDetails = async () => {
8383
if (!coreKitInstance) {
8484
throw new Error('coreKitInstance not found');
@@ -155,7 +155,7 @@ function App() {
155155
if (!answer) {
156156
throw new Error("backupFactorKey not found");
157157
}
158-
158+
159159
let factorKey = await securityQuestion.recoverFactor(coreKitInstance, answer);
160160
setBackupFactorKey(factorKey);
161161
uiConsole("Security Question share: ", factorKey);
@@ -190,7 +190,7 @@ function App() {
190190

191191
uiConsole("Export factor key: ", factorKey);
192192
console.log("menmonic : ", mnemonic);
193-
console.log("key: ", key);
193+
console.log("key: ", key);
194194
}
195195

196196
const deleteFactor = async (): Promise<void> => {
@@ -265,6 +265,69 @@ function App() {
265265
uiConsole(signedMessage);
266266
};
267267

268+
const switchChainSepolia = async () => {
269+
if (!provider) {
270+
uiConsole("provider not initialized yet");
271+
return;
272+
}
273+
const newChainConfig = {
274+
chainId: "0xaa36a7", // for wallet connect make sure to pass in this chain in the loginSettings of the adapter.
275+
displayName: "Ethereum Sepolia",
276+
chainNamespace: CHAIN_NAMESPACES.EIP155,
277+
tickerName: "Ethereum Sepolia",
278+
ticker: "ETH",
279+
decimals: 18,
280+
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
281+
blockExplorer: "https://sepolia.etherscan.io",
282+
logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png",
283+
};
284+
await coreKitInstance.switchChain(newChainConfig);
285+
setProvider(coreKitInstance.provider);
286+
uiConsole("Changed to Sepolia Network");
287+
};
288+
289+
const switchChainPolygon = async () => {
290+
if (!provider) {
291+
uiConsole("provider not initialized yet");
292+
return;
293+
}
294+
const newChainConfig = {
295+
chainNamespace: CHAIN_NAMESPACES.EIP155,
296+
chainId: "0x89", // hex of 137, polygon mainnet
297+
rpcTarget: "https://rpc.ankr.com/polygon",
298+
// Avoid using public rpcTarget in production.
299+
// Use services like Infura, Quicknode etc
300+
displayName: "Polygon Mainnet",
301+
blockExplorer: "https://polygonscan.com",
302+
ticker: "MATIC",
303+
tickerName: "MATIC",
304+
};
305+
await coreKitInstance.switchChain(newChainConfig);
306+
setProvider(coreKitInstance.provider);
307+
uiConsole("Changed to Sepolia Network");
308+
};
309+
310+
const switchChainOPBNB = async () => {
311+
if (!provider) {
312+
uiConsole("provider not initialized yet");
313+
return;
314+
}
315+
const newChainConfig = {
316+
chainNamespace: CHAIN_NAMESPACES.EIP155,
317+
chainId: "0xCC", // hex of 1261120
318+
rpcTarget: "https://opbnb-mainnet-rpc.bnbchain.org",
319+
// Avoid using public rpcTarget in production.
320+
// Use services like Infura, Quicknode etc
321+
displayName: "opBNB Mainnet",
322+
blockExplorer: "https://opbnbscan.com",
323+
ticker: "BNB",
324+
tickerName: "opBNB",
325+
};
326+
await coreKitInstance.switchChain(newChainConfig);
327+
setProvider(coreKitInstance.provider);
328+
uiConsole("Changed to Sepolia Network");
329+
};
330+
268331
const criticalResetAccount = async (): Promise<void> => {
269332
// This is a critical function that should only be used for testing purposes
270333
// Resetting your account means clearing all the metadata associated with it from the metadata server
@@ -304,8 +367,8 @@ function App() {
304367
uiConsole(receipt);
305368
};
306369

307-
const createSecurityQuestion = async ( question: string, answer: string ) => {
308-
if (!coreKitInstance) {
370+
const createSecurityQuestion = async (question: string, answer: string) => {
371+
if (!coreKitInstance) {
309372
throw new Error("coreKitInstance is not set");
310373
}
311374
await securityQuestion.setSecurityQuestion({ mpcCoreKit: coreKitInstance, question, answer, shareType: TssShareType.RECOVERY });
@@ -316,8 +379,8 @@ function App() {
316379
}
317380
}
318381

319-
const changeSecurityQuestion = async ( newQuestion: string, newAnswer: string, answer: string) => {
320-
if (!coreKitInstance) {
382+
const changeSecurityQuestion = async (newQuestion: string, newAnswer: string, answer: string) => {
383+
if (!coreKitInstance) {
321384
throw new Error("coreKitInstance is not set");
322385
}
323386
await securityQuestion.changeSecurityQuestion({ mpcCoreKit: coreKitInstance, newQuestion, newAnswer, answer });
@@ -336,7 +399,7 @@ function App() {
336399

337400
}
338401

339-
const enableMFA = async () => {
402+
const enableMFA = async () => {
340403
if (!coreKitInstance) {
341404
throw new Error("coreKitInstance is not set");
342405
}
@@ -351,7 +414,7 @@ function App() {
351414
throw new Error("coreKitInstance is not set");
352415
}
353416
await coreKitInstance.commitChanges();
354-
}
417+
}
355418

356419
const loggedInView = (
357420
<>
@@ -404,7 +467,7 @@ function App() {
404467
<div className="flex-container">
405468

406469
<label>Share Type:</label>
407-
<select value={exportTssShareType}onChange={(e) => setExportTssShareType(parseInt(e.target.value))}>
470+
<select value={exportTssShareType} onChange={(e) => setExportTssShareType(parseInt(e.target.value))}>
408471
<option value={TssShareType.DEVICE}>Device Share</option>
409472
<option value={TssShareType.RECOVERY}>Recovery Share</option>
410473
</select>
@@ -413,14 +476,14 @@ function App() {
413476
</button>
414477
</div>
415478
<div className="flex-container">
416-
<label>Factor pub:</label>
479+
<label>Factor pub:</label>
417480
<input value={factorPubToDelete} onChange={(e) => setFactorPubToDelete(e.target.value)}></input>
418481
<button onClick={deleteFactor} className="card">
419482
Delete Factor
420483
</button>
421484
</div>
422485
<div className="flex-container">
423-
<input value={backupFactorKey} onChange={(e) => setBackupFactorKey(e.target.value)}></input>
486+
<input value={backupFactorKey} onChange={(e) => setBackupFactorKey(e.target.value)}></input>
424487
<button onClick={() => inputBackupFactorKey()} className="card">
425488
Input Factor Key
426489
</button>
@@ -429,9 +492,9 @@ function App() {
429492

430493
<h4>Security Question</h4>
431494

432-
<div>{ question }</div>
495+
<div>{question}</div>
433496
<div className="flex-container">
434-
<div className={ question ? " disabledDiv": ""}>
497+
<div className={question ? " disabledDiv" : ""}>
435498
<label>Set Security Question:</label>
436499
<input value={question} placeholder="question" onChange={(e) => setNewQuestion(e.target.value)}></input>
437500
<input value={answer} placeholder="answer" onChange={(e) => setAnswer(e.target.value)}></input>
@@ -440,19 +503,19 @@ function App() {
440503
</button>
441504
</div>
442505

443-
<div className={ !question ? " disabledDiv": ""}>
506+
<div className={!question ? " disabledDiv" : ""}>
444507
<label>Change Security Question:</label>
445508
<input value={newQuestion} placeholder="newQuestion" onChange={(e) => setNewQuestion(e.target.value)}></input>
446-
<input value={newAnswer} placeholder="newAnswer" onChange={(e) => setNewAnswer(e.target.value)}></input>
509+
<input value={newAnswer} placeholder="newAnswer" onChange={(e) => setNewAnswer(e.target.value)}></input>
447510
<input value={answer} placeholder="oldAnswer" onChange={(e) => setAnswer(e.target.value)}></input>
448511
<button onClick={() => changeSecurityQuestion(newQuestion!, newAnswer!, answer!)} className="card">
449512
Change Security Question
450513
</button>
451-
514+
452515
</div>
453516
</div>
454517
<div className="flex-container">
455-
<div className={ !question ? "disabledDiv": ""}>
518+
<div className={!question ? "disabledDiv" : ""}>
456519
<button onClick={() => deleteSecurityQuestion()} className="card">
457520
Delete Security Question
458521
</button>
@@ -481,6 +544,18 @@ function App() {
481544
<button onClick={sendTransaction} className="card">
482545
Send Transaction
483546
</button>
547+
548+
<button onClick={switchChainSepolia} className="card">
549+
switchChainSepolia
550+
</button>
551+
552+
<button onClick={switchChainPolygon} className="card">
553+
switchChainPolygon
554+
</button>
555+
556+
<button onClick={switchChainOPBNB} className="card">
557+
switchChainOPBNB
558+
</button>
484559
</div>
485560
</>
486561
);
@@ -490,7 +565,7 @@ function App() {
490565
<button onClick={() => login()} className="card">
491566
Login
492567
</button>
493-
<div className={coreKitStatus=== COREKIT_STATUS.REQUIRED_SHARE ? "" : "disabledDiv" } >
568+
<div className={coreKitStatus === COREKIT_STATUS.REQUIRED_SHARE ? "" : "disabledDiv"} >
494569

495570
<button onClick={() => getDeviceShare()} className="card">
496571
Get Device Share
@@ -505,25 +580,25 @@ function App() {
505580
</button>
506581

507582

508-
<div className={ !question ? "disabledDiv" : ""}>
583+
<div className={!question ? "disabledDiv" : ""}>
509584

510-
<label>Recover Using Security Answer:</label>
585+
<label>Recover Using Security Answer:</label>
511586
<label>{question}</label>
512587
<input value={answer} onChange={(e) => setAnswer(e.target.value)}></input>
513588
<button onClick={() => recoverSecurityQuestionFactor()} className="card">
514589
Recover Using Security Answer
515590
</button>
516591
</div>
517592
</div>
518-
593+
519594
</>
520595
);
521596

522597
return (
523598
<div className="container">
524599
<h1 className="title">
525600
<a target="_blank" href="https://web3auth.io/docs/guides/mpc" rel="noreferrer">
526-
Web3Auth MPC Core Kit
601+
Web3Auth MPC Core Kit
527602
</a> {" "}
528603
Redirect Flow Example
529604
</h1>

0 commit comments

Comments
 (0)