From 3aa3ee135f1e214b9468d3420835c1df5092ade0 Mon Sep 17 00:00:00 2001 From: Andrew Min Date: Mon, 11 Sep 2023 14:04:10 -0400 Subject: [PATCH] minor refactor: rename routes, use poller --- .../src/pages/api/createKey.ts | 77 +++++++++---------- .../pages/api/{subOrg.ts => createSubOrg.ts} | 12 +-- .../src/pages/api/utils.ts | 10 +++ .../src/pages/index.tsx | 2 +- .../src/pages/api/createKey.ts | 77 +++++++++---------- .../pages/api/{subOrg.ts => createSubOrg.ts} | 12 +-- .../src/pages/api/utils.ts | 10 +++ .../src/pages/index.tsx | 2 +- 8 files changed, 98 insertions(+), 104 deletions(-) rename examples/with-ethers-and-passkeys/src/pages/api/{subOrg.ts => createSubOrg.ts} (93%) create mode 100644 examples/with-ethers-and-passkeys/src/pages/api/utils.ts rename examples/with-viem-and-passkeys/src/pages/api/{subOrg.ts => createSubOrg.ts} (93%) create mode 100644 examples/with-viem-and-passkeys/src/pages/api/utils.ts diff --git a/examples/with-ethers-and-passkeys/src/pages/api/createKey.ts b/examples/with-ethers-and-passkeys/src/pages/api/createKey.ts index ad1562063..ccd7d766e 100644 --- a/examples/with-ethers-and-passkeys/src/pages/api/createKey.ts +++ b/examples/with-ethers-and-passkeys/src/pages/api/createKey.ts @@ -1,8 +1,12 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import { TSignedRequest, TurnkeyClient } from "@turnkey/http"; import axios from "axios"; -import { TActivityResponse } from "@turnkey/http/dist/shared"; +import type { NextApiRequest, NextApiResponse } from "next"; +import { + TSignedRequest, + TurnkeyClient, + createActivityPoller, +} from "@turnkey/http"; import { ApiKeyStamper } from "@turnkey/api-key-stamper"; +import { refineNonNull } from "./utils"; type TResponse = { message: string; @@ -42,49 +46,42 @@ export default async function createKey( }); } - let response = activityResponse.data as TActivityResponse; - let attempts = 0; + const stamper = new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }); + const client = new TurnkeyClient( + { baseUrl: process.env.NEXT_PUBLIC_TURNKEY_API_BASE_URL! }, + stamper + ); - while (attempts < 3) { - if (response.activity.status != "ACTIVITY_STATUS_COMPLETED") { - const stamper = new ApiKeyStamper({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - }); - const client = new TurnkeyClient( - { baseUrl: process.env.NEXT_PUBLIC_TURNKEY_API_BASE_URL! }, - stamper - ); - response = await client.getActivity({ - organizationId: response.activity.organizationId, - activityId: response.activity.id, - }); + const activityPoller = createActivityPoller({ + client: client, + requestFn: client.getActivity, + }); - await sleep(500); + const activityId = refineNonNull(activityResponse.data.activity?.id); + const subOrgId = refineNonNull(activityResponse.data.activity?.organizationId); - attempts++; - } else { - const privateKeys = - response.activity.result.createPrivateKeysResultV2?.privateKeys; + const completedActivity = await activityPoller({ + activityId, + organizationId: subOrgId, + }); - // XXX: sorry for the ugly code! We expect a single key / address returned. - // If we have more than one key / address returned, or none, this would break. - const address = privateKeys - ?.map((pk) => pk.addresses?.map((addr) => addr.address).join("")) - .join(""); - const privateKeyId = privateKeys?.map((pk) => pk.privateKeyId).join(""); + const privateKeys = + completedActivity.result.createPrivateKeysResultV2?.privateKeys; - res.status(200).json({ - message: "successfully created key", - address: address, - privateKeyId: privateKeyId, - }); - return; - } - } + // XXX: sorry for the ugly code! We expect a single key / address returned. + // If we have more than one key / address returned, or none, this would break. + const address = privateKeys + ?.map((pk) => pk.addresses?.map((addr) => addr.address).join("")) + .join(""); + const privateKeyId = privateKeys?.map((pk) => pk.privateKeyId).join(""); - res.status(500).json({ - message: "failed to create key", + res.status(200).json({ + message: "successfully created key", + address: address, + privateKeyId: privateKeyId, }); } catch (e) { console.error(e); diff --git a/examples/with-ethers-and-passkeys/src/pages/api/subOrg.ts b/examples/with-ethers-and-passkeys/src/pages/api/createSubOrg.ts similarity index 93% rename from examples/with-ethers-and-passkeys/src/pages/api/subOrg.ts rename to examples/with-ethers-and-passkeys/src/pages/api/createSubOrg.ts index 6cdd81ae2..87e8207ea 100644 --- a/examples/with-ethers-and-passkeys/src/pages/api/subOrg.ts +++ b/examples/with-ethers-and-passkeys/src/pages/api/createSubOrg.ts @@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { TurnkeyApiTypes, TurnkeyClient } from "@turnkey/http"; import { createActivityPoller } from "@turnkey/http"; import { ApiKeyStamper } from "@turnkey/api-key-stamper"; +import { refineNonNull } from "./utils"; type TAttestation = TurnkeyApiTypes["v1Attestation"]; @@ -99,14 +100,3 @@ export default async function createUser( }); } } - -function refineNonNull( - input: T | null | undefined, - errorMessage?: string -): T { - if (input == null) { - throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`); - } - - return input; -} diff --git a/examples/with-ethers-and-passkeys/src/pages/api/utils.ts b/examples/with-ethers-and-passkeys/src/pages/api/utils.ts new file mode 100644 index 000000000..d26b4fd42 --- /dev/null +++ b/examples/with-ethers-and-passkeys/src/pages/api/utils.ts @@ -0,0 +1,10 @@ +export function refineNonNull( + input: T | null | undefined, + errorMessage?: string +): T { + if (input == null) { + throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`); + } + + return input; +} diff --git a/examples/with-ethers-and-passkeys/src/pages/index.tsx b/examples/with-ethers-and-passkeys/src/pages/index.tsx index 30fbc54d7..1720048a0 100644 --- a/examples/with-ethers-and-passkeys/src/pages/index.tsx +++ b/examples/with-ethers-and-passkeys/src/pages/index.tsx @@ -147,7 +147,7 @@ export default function Home() { }, }); - const res = await axios.post("/api/subOrg", { + const res = await axios.post("/api/createSubOrg", { subOrgName: subOrgName, attestation, challenge: base64UrlEncode(challenge), diff --git a/examples/with-viem-and-passkeys/src/pages/api/createKey.ts b/examples/with-viem-and-passkeys/src/pages/api/createKey.ts index ad1562063..ccd7d766e 100644 --- a/examples/with-viem-and-passkeys/src/pages/api/createKey.ts +++ b/examples/with-viem-and-passkeys/src/pages/api/createKey.ts @@ -1,8 +1,12 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import { TSignedRequest, TurnkeyClient } from "@turnkey/http"; import axios from "axios"; -import { TActivityResponse } from "@turnkey/http/dist/shared"; +import type { NextApiRequest, NextApiResponse } from "next"; +import { + TSignedRequest, + TurnkeyClient, + createActivityPoller, +} from "@turnkey/http"; import { ApiKeyStamper } from "@turnkey/api-key-stamper"; +import { refineNonNull } from "./utils"; type TResponse = { message: string; @@ -42,49 +46,42 @@ export default async function createKey( }); } - let response = activityResponse.data as TActivityResponse; - let attempts = 0; + const stamper = new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }); + const client = new TurnkeyClient( + { baseUrl: process.env.NEXT_PUBLIC_TURNKEY_API_BASE_URL! }, + stamper + ); - while (attempts < 3) { - if (response.activity.status != "ACTIVITY_STATUS_COMPLETED") { - const stamper = new ApiKeyStamper({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - }); - const client = new TurnkeyClient( - { baseUrl: process.env.NEXT_PUBLIC_TURNKEY_API_BASE_URL! }, - stamper - ); - response = await client.getActivity({ - organizationId: response.activity.organizationId, - activityId: response.activity.id, - }); + const activityPoller = createActivityPoller({ + client: client, + requestFn: client.getActivity, + }); - await sleep(500); + const activityId = refineNonNull(activityResponse.data.activity?.id); + const subOrgId = refineNonNull(activityResponse.data.activity?.organizationId); - attempts++; - } else { - const privateKeys = - response.activity.result.createPrivateKeysResultV2?.privateKeys; + const completedActivity = await activityPoller({ + activityId, + organizationId: subOrgId, + }); - // XXX: sorry for the ugly code! We expect a single key / address returned. - // If we have more than one key / address returned, or none, this would break. - const address = privateKeys - ?.map((pk) => pk.addresses?.map((addr) => addr.address).join("")) - .join(""); - const privateKeyId = privateKeys?.map((pk) => pk.privateKeyId).join(""); + const privateKeys = + completedActivity.result.createPrivateKeysResultV2?.privateKeys; - res.status(200).json({ - message: "successfully created key", - address: address, - privateKeyId: privateKeyId, - }); - return; - } - } + // XXX: sorry for the ugly code! We expect a single key / address returned. + // If we have more than one key / address returned, or none, this would break. + const address = privateKeys + ?.map((pk) => pk.addresses?.map((addr) => addr.address).join("")) + .join(""); + const privateKeyId = privateKeys?.map((pk) => pk.privateKeyId).join(""); - res.status(500).json({ - message: "failed to create key", + res.status(200).json({ + message: "successfully created key", + address: address, + privateKeyId: privateKeyId, }); } catch (e) { console.error(e); diff --git a/examples/with-viem-and-passkeys/src/pages/api/subOrg.ts b/examples/with-viem-and-passkeys/src/pages/api/createSubOrg.ts similarity index 93% rename from examples/with-viem-and-passkeys/src/pages/api/subOrg.ts rename to examples/with-viem-and-passkeys/src/pages/api/createSubOrg.ts index 6cdd81ae2..87e8207ea 100644 --- a/examples/with-viem-and-passkeys/src/pages/api/subOrg.ts +++ b/examples/with-viem-and-passkeys/src/pages/api/createSubOrg.ts @@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next"; import { TurnkeyApiTypes, TurnkeyClient } from "@turnkey/http"; import { createActivityPoller } from "@turnkey/http"; import { ApiKeyStamper } from "@turnkey/api-key-stamper"; +import { refineNonNull } from "./utils"; type TAttestation = TurnkeyApiTypes["v1Attestation"]; @@ -99,14 +100,3 @@ export default async function createUser( }); } } - -function refineNonNull( - input: T | null | undefined, - errorMessage?: string -): T { - if (input == null) { - throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`); - } - - return input; -} diff --git a/examples/with-viem-and-passkeys/src/pages/api/utils.ts b/examples/with-viem-and-passkeys/src/pages/api/utils.ts new file mode 100644 index 000000000..d26b4fd42 --- /dev/null +++ b/examples/with-viem-and-passkeys/src/pages/api/utils.ts @@ -0,0 +1,10 @@ +export function refineNonNull( + input: T | null | undefined, + errorMessage?: string +): T { + if (input == null) { + throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`); + } + + return input; +} diff --git a/examples/with-viem-and-passkeys/src/pages/index.tsx b/examples/with-viem-and-passkeys/src/pages/index.tsx index dd5c6748c..6f306b1c8 100644 --- a/examples/with-viem-and-passkeys/src/pages/index.tsx +++ b/examples/with-viem-and-passkeys/src/pages/index.tsx @@ -157,7 +157,7 @@ export default function Home() { }, }); - const res = await axios.post("/api/subOrg", { + const res = await axios.post("/api/createSubOrg", { subOrgName: subOrgName, attestation, challenge: base64UrlEncode(challenge),