Skip to content

Commit b332332

Browse files
committed
temp: parallel signing in email auth example
1 parent af29f4f commit b332332

File tree

1 file changed

+66
-17
lines changed

1 file changed

+66
-17
lines changed

examples/email-auth/src/pages/index.tsx

+66-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Image from "next/image";
22
import styles from "./index.module.css";
33
import { useTurnkey } from "@turnkey/sdk-react";
4+
import { Turnkey as TurnkeySDKClient } from "@turnkey/sdk-server";
45
import { useForm } from "react-hook-form";
56
import axios from "axios";
67
import * as React from "react";
78
import { useState } from "react";
8-
99
/**
1010
* Type definition for the server response coming back from `/api/auth`
1111
*/
@@ -54,20 +54,24 @@ export default function AuthPage() {
5454
};
5555

5656
const injectCredentials = async (data: InjectCredentialsFormData) => {
57+
console.log("injecting");
58+
5759
if (authIframeClient === null) {
5860
throw new Error("iframe client is null");
5961
}
6062
if (authResponse === null) {
6163
throw new Error("authResponse is null");
6264
}
6365
try {
66+
console.log("before inject");
6467
await authIframeClient!.injectCredentialBundle(data.authBundle);
6568
} catch (e) {
6669
const msg = `error while injecting bundle: ${e}`;
6770
console.error(msg);
6871
alert(msg);
6972
return;
7073
}
74+
console.log("outside");
7175

7276
// get whoami for suborg
7377
const whoamiResponse = await authIframeClient!.getWhoami({
@@ -76,22 +80,60 @@ export default function AuthPage() {
7680

7781
const orgID = whoamiResponse.organizationId;
7882

79-
const createWalletResponse = await authIframeClient!.createWallet({
80-
organizationId: orgID,
81-
walletName: data.walletName,
82-
accounts: [
83-
{
84-
curve: "CURVE_SECP256K1",
85-
pathFormat: "PATH_FORMAT_BIP32",
86-
path: "m/44'/60'/0'/0/0",
87-
addressFormat: "ADDRESS_FORMAT_ETHEREUM",
88-
},
89-
],
90-
});
83+
console.log("whoami?", whoamiResponse);
9184

92-
const address = refineNonNull(createWalletResponse.addresses[0]);
85+
function createRandomString(length: number): string {
86+
const chars =
87+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
88+
let result = "";
89+
for (let i = 0; i < length; i++) {
90+
result += chars.charAt(Math.floor(Math.random() * chars.length));
91+
}
92+
return result;
93+
}
9394

94-
alert(`SUCCESS! Wallet and new address created: ${address} `);
95+
const start = new Date().getTime();
96+
97+
let signingPromises = [];
98+
for (let i = 0; i < 100; i++) {
99+
signingPromises.push(
100+
authIframeClient!.signRawPayload({
101+
signWith: "0xc95B01326731D17972a4845458fc954f2aD37E8e",
102+
payload: createRandomString(20),
103+
encoding: "PAYLOAD_ENCODING_TEXT_UTF8",
104+
hashFunction: "HASH_FUNCTION_SHA256",
105+
})
106+
);
107+
}
108+
109+
// This is the step which waits on all signing promises to complete
110+
const signatures = await Promise.allSettled(signingPromises);
111+
112+
const end = new Date().getTime();
113+
114+
console.log("signatures", signatures);
115+
console.log({
116+
start,
117+
end,
118+
diff: end - start,
119+
});
120+
121+
// const createWalletResponse = await authIframeClient!.createWallet({
122+
// organizationId: orgID,
123+
// walletName: data.walletName,
124+
// accounts: [
125+
// {
126+
// curve: "CURVE_SECP256K1",
127+
// pathFormat: "PATH_FORMAT_BIP32",
128+
// path: "m/44'/60'/0'/0/0",
129+
// addressFormat: "ADDRESS_FORMAT_ETHEREUM",
130+
// },
131+
// ],
132+
// });
133+
134+
// const address = refineNonNull(createWalletResponse.addresses[0]);
135+
136+
// alert(`SUCCESS! Wallet and new address created: ${address} `);
95137
};
96138

97139
return (
@@ -116,7 +158,10 @@ export default function AuthPage() {
116158
{authIframeClient &&
117159
authIframeClient.iframePublicKey &&
118160
authResponse === null && (
119-
<form className={styles.form} onSubmit={authFormSubmit(auth)}>
161+
<form
162+
className={styles.form}
163+
onSubmit={authFormSubmit(auth)}
164+
>
120165
<label className={styles.label}>
121166
Email
122167
<input
@@ -150,7 +195,11 @@ export default function AuthPage() {
150195
</code>
151196
</label>
152197

153-
<input className={styles.button} type="submit" value="Auth" />
198+
<input
199+
className={styles.button}
200+
type="submit"
201+
value="Auth"
202+
/>
154203
</form>
155204
)}
156205

0 commit comments

Comments
 (0)