Skip to content

Commit 31fd129

Browse files
Merge pull request #193 from Web3Auth/feat/bip340
v4: Add bip340
2 parents 78457af + f4f81ee commit 31fd129

File tree

12 files changed

+570
-239
lines changed

12 files changed

+570
-239
lines changed

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

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

demo/redirect-flow-example/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
"private": true,
55
"dependencies": {
66
"@tkey/common-types": "^15.1.0",
7-
"@toruslabs/tss-dkls-lib": "^4.0.0",
8-
"@toruslabs/tss-frost-lib": "^1.0.0",
7+
"@toruslabs/tss-dkls-lib": "^5.0.0-alpha.0",
8+
"@toruslabs/tss-frost-lib": "^2.0.0-alpha.0",
9+
"@toruslabs/tss-frost-lib-bip340": "^0.1.0-alpha.0",
910
"@types/jest": "^27.5.2",
1011
"@types/node": "^16.18.48",
1112
"@types/react": "^18.3.11",

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

Lines changed: 97 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@ import {
1212
parseToken,
1313
factorKeyCurve,
1414
makeEthereumSigner,
15+
SIG_TYPE,
1516
} from "@web3auth/mpc-core-kit";
16-
import Web3 from "web3";
17+
import Web3, { core } from "web3";
1718
import { CHAIN_NAMESPACES, CustomChainConfig, IProvider } from "@web3auth/base";
1819
import { EthereumSigningProvider } from "@web3auth/ethereum-mpc-provider";
1920
import { BN } from "bn.js";
2021
import { KeyType, Point } from "@tkey/common-types";
21-
import { tssLib } from "@toruslabs/tss-dkls-lib";
22-
// import{ tssLib } from "@toruslabs/tss-frost-lib";
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";
2325

2426
import "./App.css";
2527
import jwt, { Algorithm } from "jsonwebtoken";
2628
import { flow } from "./flow";
2729

30+
type TssLib = typeof tssLibDkls | typeof tssLibFrost | typeof tssLibFrostBip340;
31+
2832
const uiConsole = (...args: any[]): void => {
2933
const el = document.querySelector("#console>p");
3034
if (el) {
@@ -46,16 +50,6 @@ const DEFAULT_CHAIN_CONFIG: CustomChainConfig = {
4650
decimals: 18,
4751
};
4852

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

6054
const privateKey = "MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCCD7oLrcKae+jVZPGx52Cb/lKhdKxpXjl9eGNa1MlY57A==";
6155
const jwtPrivateKey = `-----BEGIN PRIVATE KEY-----\n${privateKey}\n-----END PRIVATE KEY-----`;
@@ -97,7 +91,18 @@ function App() {
9791
const [question, setQuestion] = useState<string | undefined>(undefined);
9892
const [newQuestion, setNewQuestion] = useState<string | undefined>(undefined);
9993
const securityQuestion = useMemo(() => new TssSecurityQuestion(), []);
100-
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+
);
101106
async function setupProvider(chainConfig?: CustomChainConfig) {
102107
if (coreKitInstance.keyType !== KeyType.secp256k1) {
103108
console.warn(`Ethereum requires keytype ${KeyType.secp256k1}, skipping provider setup`);
@@ -110,44 +115,58 @@ function App() {
110115

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

127-
if (coreKitInstance.status === COREKIT_STATUS.REQUIRED_SHARE) {
128-
uiConsole(
129-
"required more shares, please enter your backup/ device factor key, or reset account unrecoverable once reset, please use it with caution]"
130-
);
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();
131126
}
127+
}
128+
if (newCoreKitInstance.status === COREKIT_STATUS.LOGGED_IN) {
129+
await setupProvider();
130+
}
132131

133-
console.log("coreKitInstance.status", coreKitInstance.status);
134-
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+
}
135137

136-
try {
137-
let result = securityQuestion.getQuestion(coreKitInstance!);
138-
setQuestion(result);
139-
uiConsole("security question set");
140-
} catch (e) {
141-
uiConsole("security question not set");
142-
}
143-
};
144-
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)
145165
{
146-
init();
147-
initialized.current = true;
166+
init(instance);
148167
}
149168

150-
}, []);
169+
}, [selectedTssLib]);
151170

152171
useEffect(() => {
153172
if (provider) {
@@ -171,7 +190,7 @@ function App() {
171190
if (!factorPubs) {
172191
throw new Error("factorPubs not found");
173192
}
174-
const pubsHex = factorPubs[coreKitInstance.tKey.tssTag].map((pub: Point) => {
193+
const pubsHex = factorPubs[coreKitInstance.tKey.tssTag].map(pub => {
175194
return pub.toSEC1(factorKeyCurve, true).toString("hex");
176195
});
177196
uiConsole(pubsHex);
@@ -357,26 +376,26 @@ function App() {
357376
}
358377
const address = (await web3.eth.getAccounts())[0];
359378
const balance = web3.utils.fromWei(
360-
await web3.eth.getBalance(address) // Balance is in wei
379+
await web3.eth.getBalance(address), // Balance is in wei
380+
"ether"
361381
);
362382
uiConsole(balance);
363383
return balance;
364384
};
365385

366386
const signMessage = async (): Promise<any> => {
367-
if (coreKitInstance.keyType === "secp256k1") {
387+
if (coreKitInstance.sigType === SIG_TYPE.ECDSA_SECP256K1) {
368388
if (!web3) {
369389
uiConsole("web3 not initialized yet");
370390
return;
371391
}
372392
const fromAddress = (await web3.eth.getAccounts())[0];
373-
374393
const message = "hello";
375394
const signedMessage = await web3.eth.personal.sign(message, fromAddress, "");
376395

377396

378397
uiConsole(signedMessage);
379-
} else if (coreKitInstance.keyType === "ed25519") {
398+
} else if (coreKitInstance.sigType === SIG_TYPE.ED25519 || coreKitInstance.sigType === SIG_TYPE.BIP340) {
380399
const msg = Buffer.from("hello signer!");
381400
const sig = await coreKitInstance.sign(msg);
382401
uiConsole(sig.toString("hex"));
@@ -512,7 +531,7 @@ function App() {
512531
const fromAddress = (await web3.eth.getAccounts())[0];
513532

514533
const destination = "0x2E464670992574A613f10F7682D5057fB507Cc21";
515-
const amount = web3.utils.toWei("0.0001"); // Convert 1 ether to wei
534+
const amount = web3.utils.toWei("0.0001", "ether"); // Convert 1 ether to wei
516535

517536
// Submit transaction to the blockchain and wait for it to be mined
518537
uiConsole("Sending transaction...");
@@ -572,6 +591,32 @@ function App() {
572591
await coreKitInstance.commitChanges();
573592
};
574593

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+
575620
const loggedInView = (
576621
<>
577622
<h2 className="subtitle">Account Details</h2>
@@ -730,6 +775,7 @@ function App() {
730775

731776
const unloggedInView = (
732777
<>
778+
{tssLibSelector}
733779
<input value={mockEmail} onChange={(e) => setMockEmail(e.target.value)}></input>
734780
<button onClick={() => loginWithMock()} className="card">
735781
MockLogin

0 commit comments

Comments
 (0)