Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[do not merge - testing] iframe idempotency testing #468

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions packages/iframe-stamper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ export class IframeStamper {
* Function to sign a payload with the underlying iframe
*/
async stamp(payload: string): Promise<TStamp> {
console.log("calling stamp");
if (this.iframePublicKey === null) {
throw new Error(
"null iframe public key. Have you called/awaited .init()?",
Expand Down
Loading