Skip to content

Commit f4f81ee

Browse files
demo updates
1 parent ab2fbe6 commit f4f81ee

File tree

5 files changed

+128
-72
lines changed

5 files changed

+128
-72
lines changed

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

Lines changed: 8 additions & 4 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: 93 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ import {
1212
parseToken,
1313
factorKeyCurve,
1414
makeEthereumSigner,
15-
SigType,
15+
SIG_TYPE,
1616
} from "@web3auth/mpc-core-kit";
17-
import Web3 from "web3";
17+
import Web3, { core } from "web3";
1818
import { CHAIN_NAMESPACES, CustomChainConfig, IProvider } from "@web3auth/base";
1919
import { EthereumSigningProvider } from "@web3auth/ethereum-mpc-provider";
2020
import { BN } from "bn.js";
2121
import { KeyType, Point } from "@tkey/common-types";
22-
// import { tssLib } from "@toruslabs/tss-dkls-lib";
23-
// import{ tssLib } from "@toruslabs/tss-frost-lib";
24-
import{ tssLib } from "@toruslabs/tss-frost-lib-bip340";
22+
import { tssLib as tssLibDkls } from "@toruslabs/tss-dkls-lib";
23+
import{ tssLib as tssLibFrost } from "@toruslabs/tss-frost-lib";
24+
import{ tssLib as tssLibFrostBip340 } from "@toruslabs/tss-frost-lib-bip340";
2525

2626
import "./App.css";
2727
import jwt, { Algorithm } from "jsonwebtoken";
2828
import { flow } from "./flow";
2929

30+
type TssLib = typeof tssLibDkls | typeof tssLibFrost | typeof tssLibFrostBip340;
31+
3032
const uiConsole = (...args: any[]): void => {
3133
const el = document.querySelector("#console>p");
3234
if (el) {
@@ -48,16 +50,6 @@ const DEFAULT_CHAIN_CONFIG: CustomChainConfig = {
4850
decimals: 18,
4951
};
5052

51-
const coreKitInstance = new Web3AuthMPCCoreKit({
52-
web3AuthClientId: "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ",
53-
web3AuthNetwork: selectedNetwork,
54-
uxMode: "redirect",
55-
manualSync: true,
56-
storage: window.localStorage,
57-
// sessionTime: 3600, // <== can provide variable session time based on user subscribed plan
58-
tssLib,
59-
useDKG: false,
60-
});
6153

6254
const privateKey = "MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCCD7oLrcKae+jVZPGx52Cb/lKhdKxpXjl9eGNa1MlY57A==";
6355
const jwtPrivateKey = `-----BEGIN PRIVATE KEY-----\n${privateKey}\n-----END PRIVATE KEY-----`;
@@ -99,7 +91,18 @@ function App() {
9991
const [question, setQuestion] = useState<string | undefined>(undefined);
10092
const [newQuestion, setNewQuestion] = useState<string | undefined>(undefined);
10193
const securityQuestion = useMemo(() => new TssSecurityQuestion(), []);
102-
94+
const [selectedTssLib, setSelectedTssLib] = useState<TssLib>(tssLibDkls);
95+
const [coreKitInstance, setCoreKitInstance] = useState<Web3AuthMPCCoreKit>(
96+
new Web3AuthMPCCoreKit({
97+
web3AuthClientId: "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ",
98+
web3AuthNetwork: selectedNetwork,
99+
uxMode: "redirect",
100+
manualSync: true,
101+
storage: window.localStorage,
102+
tssLib: selectedTssLib,
103+
useDKG: false,
104+
})
105+
);
103106
async function setupProvider(chainConfig?: CustomChainConfig) {
104107
if (coreKitInstance.keyType !== KeyType.secp256k1) {
105108
console.warn(`Ethereum requires keytype ${KeyType.secp256k1}, skipping provider setup`);
@@ -112,44 +115,58 @@ function App() {
112115

113116
// decide whether to rehydrate session
114117
const rehydrate = true;
115-
const initialized = useRef(false);
116-
useEffect(() => {
117-
const init = async () => {
118-
// Example config to handle redirect result manually
119-
if (coreKitInstance.status === COREKIT_STATUS.NOT_INITIALIZED) {
120-
await coreKitInstance.init({ handleRedirectResult: false, rehydrate });
121-
if (window.location.hash.includes("#state")) {
122-
await coreKitInstance.handleRedirectResult();
123-
}
124-
}
125-
if (coreKitInstance.status === COREKIT_STATUS.LOGGED_IN) {
126-
await setupProvider();
127-
}
128118

129-
if (coreKitInstance.status === COREKIT_STATUS.REQUIRED_SHARE) {
130-
uiConsole(
131-
"required more shares, please enter your backup/ device factor key, or reset account unrecoverable once reset, please use it with caution]"
132-
);
119+
120+
const init = async (newCoreKitInstance: Web3AuthMPCCoreKit) => {
121+
// Example config to handle redirect result manually
122+
if (newCoreKitInstance.status === COREKIT_STATUS.NOT_INITIALIZED) {
123+
await newCoreKitInstance.init({ handleRedirectResult: false, rehydrate });
124+
if (window.location.hash.includes("#state")) {
125+
await newCoreKitInstance.handleRedirectResult();
133126
}
127+
}
128+
if (newCoreKitInstance.status === COREKIT_STATUS.LOGGED_IN) {
129+
await setupProvider();
130+
}
134131

135-
console.log("coreKitInstance.status", coreKitInstance.status);
136-
setCoreKitStatus(coreKitInstance.status);
132+
if (newCoreKitInstance.status === COREKIT_STATUS.REQUIRED_SHARE) {
133+
uiConsole(
134+
"required more shares, please enter your backup/ device factor key, or reset account unrecoverable once reset, please use it with caution]"
135+
);
136+
}
137137

138-
try {
139-
let result = securityQuestion.getQuestion(coreKitInstance!);
140-
setQuestion(result);
141-
uiConsole("security question set");
142-
} catch (e) {
143-
uiConsole("security question not set");
144-
}
145-
};
146-
if (!initialized.current)
138+
console.log("newCoreKitInstance.status", newCoreKitInstance.status);
139+
setCoreKitStatus(newCoreKitInstance.status);
140+
141+
try {
142+
let result = securityQuestion.getQuestion(newCoreKitInstance!);
143+
setQuestion(result);
144+
uiConsole("security question set");
145+
} catch (e) {
146+
uiConsole("security question not set");
147+
}
148+
};
149+
150+
151+
useEffect(() => {
152+
const instance = new Web3AuthMPCCoreKit({
153+
web3AuthClientId: "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ",
154+
web3AuthNetwork: selectedNetwork,
155+
uxMode: "redirect",
156+
manualSync: true,
157+
storage: window.localStorage,
158+
tssLib: selectedTssLib,
159+
useDKG: false,
160+
})
161+
setCoreKitInstance(
162+
instance
163+
)
164+
if (instance.status === COREKIT_STATUS.NOT_INITIALIZED)
147165
{
148-
init();
149-
initialized.current = true;
166+
init(instance);
150167
}
151168

152-
}, []);
169+
}, [selectedTssLib]);
153170

154171
useEffect(() => {
155172
if (provider) {
@@ -367,19 +384,18 @@ function App() {
367384
};
368385

369386
const signMessage = async (): Promise<any> => {
370-
if (coreKitInstance.sigType === SigType.ecdsa_secp256k1) {
387+
if (coreKitInstance.sigType === SIG_TYPE.ECDSA_SECP256K1) {
371388
if (!web3) {
372389
uiConsole("web3 not initialized yet");
373390
return;
374391
}
375392
const fromAddress = (await web3.eth.getAccounts())[0];
376-
377393
const message = "hello";
378394
const signedMessage = await web3.eth.personal.sign(message, fromAddress, "");
379395

380396

381397
uiConsole(signedMessage);
382-
} else if (coreKitInstance.sigType === SigType.ed25519 || coreKitInstance.sigType === SigType.bip340) {
398+
} else if (coreKitInstance.sigType === SIG_TYPE.ED25519 || coreKitInstance.sigType === SIG_TYPE.BIP340) {
383399
const msg = Buffer.from("hello signer!");
384400
const sig = await coreKitInstance.sign(msg);
385401
uiConsole(sig.toString("hex"));
@@ -575,6 +591,32 @@ function App() {
575591
await coreKitInstance.commitChanges();
576592
};
577593

594+
const tssLibSelector = (
595+
<div className="flex-container">
596+
<label>TSS Library:</label>
597+
<select
598+
value={selectedTssLib === tssLibDkls ? "dkls" : selectedTssLib === tssLibFrost ? "frost" : "frostBip340"}
599+
onChange={(e) => {
600+
switch(e.target.value) {
601+
case "dkls":
602+
setSelectedTssLib(tssLibDkls);
603+
break;
604+
case "frost":
605+
setSelectedTssLib(tssLibFrost);
606+
break;
607+
case "frostBip340":
608+
setSelectedTssLib(tssLibFrostBip340);
609+
break;
610+
}
611+
}}
612+
>
613+
<option value="dkls">DKLS</option>
614+
<option value="frost">FROST</option>
615+
<option value="frostBip340">FROST BIP340</option>
616+
</select>
617+
</div>
618+
);
619+
578620
const loggedInView = (
579621
<>
580622
<h2 className="subtitle">Account Details</h2>
@@ -733,6 +775,7 @@ function App() {
733775

734776
const unloggedInView = (
735777
<>
778+
{tssLibSelector}
736779
<input value={mockEmail} onChange={(e) => setMockEmail(e.target.value)}></input>
737780
<button onClick={() => loginWithMock()} className="card">
738781
MockLogin

package-lock.json

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

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
},
4242
"@toruslabs/tss-frost-lib": {
4343
"optional": true
44+
},
45+
"@toruslabs/tss-frost-lib-bip340": {
46+
"optional": true
4447
}
4548
},
4649
"dependencies": {
@@ -49,15 +52,15 @@
4952
"@tkey/share-serialization": "^15.1.0",
5053
"@tkey/storage-layer-torus": "^15.1.0",
5154
"@tkey/tss": "^15.1.0",
52-
"@toruslabs/constants": "^14.0.0",
55+
"@toruslabs/constants": "^14.2.0",
5356
"@toruslabs/customauth": "^20.3.0",
5457
"@toruslabs/elliptic-wrapper": "^0.1.1",
55-
"@toruslabs/fetch-node-details": "^14.0.1",
58+
"@toruslabs/fetch-node-details": "^14.2.0",
5659
"@toruslabs/fnd-base": "^14.2.0",
5760
"@toruslabs/metadata-helpers": "^6.0.0",
5861
"@toruslabs/openlogin-utils": "^8.2.1",
5962
"@toruslabs/session-manager": "^3.1.0",
60-
"@toruslabs/torus.js": "^15.1.0",
63+
"@toruslabs/torus.js": "^15.1.1",
6164
"@toruslabs/tss-client": "^3.3.0-alpha.0",
6265
"@toruslabs/tss-frost-client": "^1.0.0-alpha.0",
6366
"@toruslabs/tss-frost-common": "^1.0.1",

src/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TORUS_SAPPHIRE_NETWORK } from "@toruslabs/constants";
1+
import { SIG_TYPE, TORUS_SAPPHIRE_NETWORK } from "@toruslabs/constants";
22

33
export const WEB3AUTH_NETWORK = {
44
MAINNET: TORUS_SAPPHIRE_NETWORK.SAPPHIRE_MAINNET,
@@ -51,3 +51,5 @@ export const FIELD_ELEMENT_HEX_LEN = 32 * 2; // Length of secp256k1 field elemen
5151

5252
export const MAX_FACTORS = 10; // Maximum number of factors that can be added to an account.
5353
export const SOCIAL_TKEY_INDEX = 1;
54+
55+
export { SIG_TYPE };

0 commit comments

Comments
 (0)