Skip to content

Commit e98ad5c

Browse files
committed
temp: parallel signing in email auth example
1 parent fa1f11c commit e98ad5c

File tree

1 file changed

+69
-16
lines changed

1 file changed

+69
-16
lines changed

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

+69-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
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";
89

10+
const turnkeyClient = new TurnkeySDKClient({
11+
apiBaseUrl: process.env.NEXT_PUBLIC_BASE_URL!,
12+
apiPublicKey: "03eba1c0dbc9720b3b6fe9bdc49f1eef35c82b3b0d4f3dc44b930756634232c3f7",
13+
apiPrivateKey: "572a8911461934cc7d8d0f78f3c7c7d08d4e46ceda915ee35463edcc6a8c8e65",
14+
defaultOrganizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
15+
});
16+
917
/**
1018
* Type definition for the server response coming back from `/api/auth`
1119
*/
@@ -76,22 +84,60 @@ export default function AuthPage() {
7684

7785
const orgID = whoamiResponse.organizationId;
7886

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-
});
87+
console.log("whoami?", whoamiResponse);
88+
89+
function createRandomString(length: number): string {
90+
const chars =
91+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
92+
let result = "";
93+
for (let i = 0; i < length; i++) {
94+
result += chars.charAt(Math.floor(Math.random() * chars.length));
95+
}
96+
return result;
97+
}
98+
99+
const start = new Date().getTime();
100+
101+
let signingPromises = [];
102+
for (let i = 0; i < 100; i++) {
103+
signingPromises.push(
104+
turnkeyClient.apiClient()!.signRawPayload({
105+
signWith: "0xc95B01326731D17972a4845458fc954f2aD37E8e",
106+
payload: createRandomString(20),
107+
encoding: "PAYLOAD_ENCODING_TEXT_UTF8",
108+
hashFunction: "HASH_FUNCTION_SHA256",
109+
})
110+
);
111+
}
112+
113+
// This is the step which waits on all signing promises to complete
114+
const signatures = await Promise.allSettled(signingPromises);
115+
116+
const end = new Date().getTime();
91117

92-
const address = refineNonNull(createWalletResponse.addresses[0]);
118+
console.log("signatures", signatures);
119+
console.log({
120+
start,
121+
end,
122+
diff: end - start,
123+
});
93124

94-
alert(`SUCCESS! Wallet and new address created: ${address} `);
125+
// const createWalletResponse = await authIframeClient!.createWallet({
126+
// organizationId: orgID,
127+
// walletName: data.walletName,
128+
// accounts: [
129+
// {
130+
// curve: "CURVE_SECP256K1",
131+
// pathFormat: "PATH_FORMAT_BIP32",
132+
// path: "m/44'/60'/0'/0/0",
133+
// addressFormat: "ADDRESS_FORMAT_ETHEREUM",
134+
// },
135+
// ],
136+
// });
137+
138+
// const address = refineNonNull(createWalletResponse.addresses[0]);
139+
140+
// alert(`SUCCESS! Wallet and new address created: ${address} `);
95141
};
96142

97143
return (
@@ -116,7 +162,10 @@ export default function AuthPage() {
116162
{authIframeClient &&
117163
authIframeClient.iframePublicKey &&
118164
authResponse === null && (
119-
<form className={styles.form} onSubmit={authFormSubmit(auth)}>
165+
<form
166+
className={styles.form}
167+
onSubmit={authFormSubmit(auth)}
168+
>
120169
<label className={styles.label}>
121170
Email
122171
<input
@@ -150,7 +199,11 @@ export default function AuthPage() {
150199
</code>
151200
</label>
152201

153-
<input className={styles.button} type="submit" value="Auth" />
202+
<input
203+
className={styles.button}
204+
type="submit"
205+
value="Auth"
206+
/>
154207
</form>
155208
)}
156209

0 commit comments

Comments
 (0)