Skip to content

Commit

Permalink
Merge pull request #189 from Web3Auth/feat/separate-precompute
Browse files Browse the repository at this point in the history
make it possible to call precompute seperately
  • Loading branch information
himanshuchawla009 authored Nov 7, 2024
2 parents a9b5248 + ad5fe3e commit 9206669
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 160 deletions.
108 changes: 49 additions & 59 deletions demo/redirect-flow-example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/redirect-flow-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.2",
"@web3auth/base": "^9.0.2",
"@web3auth/ethereum-mpc-provider": "^9.0.2",
"@web3auth/ethereum-mpc-provider": "^9.3.0",
"@web3auth/mpc-core-kit": "file:../..",
"browserify-zlib": "^0.2.0",
"copy-webpack-plugin": "^11.0.0",
Expand Down
60 changes: 40 additions & 20 deletions demo/redirect-flow-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -370,26 +370,10 @@ function App() {
return;
}
const fromAddress = (await web3.eth.getAccounts())[0];
const originalMessage = [
{
type: "string",
name: "fullName",
value: "Satoshi Nakamoto",
},
{
type: "uint32",
name: "userId",
value: "1212",
},
];
const params = [originalMessage, fromAddress];
const method = "eth_signTypedData";
const signedMessage = await (web3.currentProvider as any)?.sendAsync({
id: 1,
method,
params,
fromAddress,
});

const message = "hello";
const signedMessage = await web3.eth.personal.sign(message, fromAddress, "");


uiConsole(signedMessage);
} else if (coreKitInstance.keyType === "ed25519") {
Expand All @@ -398,7 +382,35 @@ function App() {
uiConsole(sig.toString("hex"));
}
};
const signMessageWithPrecomputedTss = async (): Promise<any> => {
if (coreKitInstance.keyType === "secp256k1") {
const precomputedTssClient = await coreKitInstance.precompute_secp256k1();
const msg = Buffer.from("hello signer!");
const sig = await coreKitInstance.sign(msg, false, precomputedTssClient);
uiConsole(sig.toString("hex"));
} else if (coreKitInstance.keyType === "ed25519") {
const msg = Buffer.from("hello signer!");
const sig = await coreKitInstance.sign(msg);
uiConsole(sig.toString("hex"));
}
};

const signMultipleMessagesWithPrecomputedTss = async (): Promise<any> => {
if (coreKitInstance.keyType === "secp256k1") {
const [precomputedTssClient, precomputedTssClient2] = await Promise.all([coreKitInstance.precompute_secp256k1(), coreKitInstance.precompute_secp256k1()]);

const msg = Buffer.from("hello signer!");
const sig = await coreKitInstance.sign(msg, false, precomputedTssClient);
const msg2 = Buffer.from("hello signer2!");

const sig2 = await coreKitInstance.sign(msg2, false, precomputedTssClient2);
uiConsole("Sig1: ", sig.toString("hex"), "Sig2: ", sig2.toString("hex"));
} else if (coreKitInstance.keyType === "ed25519") {
const msg = Buffer.from("hello signer!");
const sig = await coreKitInstance.sign(msg);
uiConsole(sig.toString("hex"));
}
};
const switchChainSepolia = async () => {
if (!provider) {
uiConsole("provider not initialized yet");
Expand Down Expand Up @@ -689,6 +701,14 @@ function App() {
Sign Message
</button>

<button onClick={signMessageWithPrecomputedTss} className="card">
Sign Msgwith precomputed TSS
</button>

<button onClick={signMultipleMessagesWithPrecomputedTss} className="card">
Sign Multiple MSGs with precomputed TSS
</button>

<button onClick={sendTransaction} className="card">
Send Transaction
</button>
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
TorusVerifierResponse,
UX_MODE_TYPE,
} from "@toruslabs/customauth";
import { Client } from "@toruslabs/tss-client";
// TODO: move the types to a base class for both dkls and frost in future
import type { tssLib as TssDklsLib } from "@toruslabs/tss-dkls-lib";
import type { tssLib as TssFrostLib } from "@toruslabs/tss-frost-lib";
Expand Down Expand Up @@ -470,3 +471,8 @@ export interface EthereumSigner {
sign: (msgHash: Buffer) => Promise<EthSig>;
getPublic: () => Promise<Buffer>;
}

export interface Secp256k1PrecomputedClient {
client: Client;
serverCoeffs: Record<string, string>;
}
Loading

0 comments on commit 9206669

Please sign in to comment.