Skip to content

Commit

Permalink
temp: parallel signing in email auth example
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Jan 29, 2025
1 parent fb1005f commit 9de2919
Showing 1 changed file with 66 additions and 17 deletions.
83 changes: 66 additions & 17 deletions examples/email-auth/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Image from "next/image";
import styles from "./index.module.css";
import { useTurnkey } from "@turnkey/sdk-react";
import { Turnkey as TurnkeySDKClient } from "@turnkey/sdk-server";
import { useForm } from "react-hook-form";
import axios from "axios";
import * as React from "react";
import { useState } from "react";

/**
* Type definition for the server response coming back from `/api/auth`
*/
Expand Down Expand Up @@ -54,20 +54,24 @@ export default function AuthPage() {
};

const injectCredentials = async (data: InjectCredentialsFormData) => {
console.log("injecting");

if (authIframeClient === null) {
throw new Error("iframe client is null");
}
if (authResponse === null) {
throw new Error("authResponse is null");
}
try {
console.log("before inject");
await authIframeClient!.injectCredentialBundle(data.authBundle);
} catch (e) {
const msg = `error while injecting bundle: ${e}`;
console.error(msg);
alert(msg);
return;
}
console.log("outside");

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

const orgID = whoamiResponse.organizationId;

const createWalletResponse = await authIframeClient!.createWallet({
organizationId: orgID,
walletName: data.walletName,
accounts: [
{
curve: "CURVE_SECP256K1",
pathFormat: "PATH_FORMAT_BIP32",
path: "m/44'/60'/0'/0/0",
addressFormat: "ADDRESS_FORMAT_ETHEREUM",
},
],
});
console.log("whoami?", whoamiResponse);

const address = refineNonNull(createWalletResponse.addresses[0]);
function createRandomString(length: number): string {
const chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}

alert(`SUCCESS! Wallet and new address created: ${address} `);
const start = new Date().getTime();

let signingPromises = [];
for (let i = 0; i < 100; i++) {
signingPromises.push(
authIframeClient!.signRawPayload({
signWith: "0xc95B01326731D17972a4845458fc954f2aD37E8e",
payload: createRandomString(20),
encoding: "PAYLOAD_ENCODING_TEXT_UTF8",
hashFunction: "HASH_FUNCTION_SHA256",
})
);
}

// This is the step which waits on all signing promises to complete
const signatures = await Promise.allSettled(signingPromises);

const end = new Date().getTime();

console.log("signatures", signatures);
console.log({
start,
end,
diff: end - start,
});

// const createWalletResponse = await authIframeClient!.createWallet({
// organizationId: orgID,
// walletName: data.walletName,
// accounts: [
// {
// curve: "CURVE_SECP256K1",
// pathFormat: "PATH_FORMAT_BIP32",
// path: "m/44'/60'/0'/0/0",
// addressFormat: "ADDRESS_FORMAT_ETHEREUM",
// },
// ],
// });

// const address = refineNonNull(createWalletResponse.addresses[0]);

// alert(`SUCCESS! Wallet and new address created: ${address} `);
};

return (
Expand All @@ -116,7 +158,10 @@ export default function AuthPage() {
{authIframeClient &&
authIframeClient.iframePublicKey &&
authResponse === null && (
<form className={styles.form} onSubmit={authFormSubmit(auth)}>
<form
className={styles.form}
onSubmit={authFormSubmit(auth)}
>
<label className={styles.label}>
Email
<input
Expand Down Expand Up @@ -150,7 +195,11 @@ export default function AuthPage() {
</code>
</label>

<input className={styles.button} type="submit" value="Auth" />
<input
className={styles.button}
type="submit"
value="Auth"
/>
</form>
)}

Expand Down

0 comments on commit 9de2919

Please sign in to comment.