From b332332e67902376d36f5b554ca05b9b830a3871 Mon Sep 17 00:00:00 2001 From: Andrew Min Date: Tue, 7 Jan 2025 10:01:01 -0800 Subject: [PATCH] temp: parallel signing in email auth example --- examples/email-auth/src/pages/index.tsx | 83 ++++++++++++++++++++----- 1 file changed, 66 insertions(+), 17 deletions(-) diff --git a/examples/email-auth/src/pages/index.tsx b/examples/email-auth/src/pages/index.tsx index 05df8aa99..aa75f35e1 100644 --- a/examples/email-auth/src/pages/index.tsx +++ b/examples/email-auth/src/pages/index.tsx @@ -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` */ @@ -54,6 +54,8 @@ export default function AuthPage() { }; const injectCredentials = async (data: InjectCredentialsFormData) => { + console.log("injecting"); + if (authIframeClient === null) { throw new Error("iframe client is null"); } @@ -61,6 +63,7 @@ export default function AuthPage() { 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}`; @@ -68,6 +71,7 @@ export default function AuthPage() { alert(msg); return; } + console.log("outside"); // get whoami for suborg const whoamiResponse = await authIframeClient!.getWhoami({ @@ -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 ( @@ -116,7 +158,10 @@ export default function AuthPage() { {authIframeClient && authIframeClient.iframePublicKey && authResponse === null && ( -
+ - +
)}