Skip to content

Commit

Permalink
minor refactor: rename routes, use poller
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Sep 11, 2023
1 parent 228f173 commit cde1a5f
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 104 deletions.
79 changes: 39 additions & 40 deletions examples/with-ethers-and-passkeys/src/pages/api/createKey.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,49 +46,44 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down Expand Up @@ -99,14 +100,3 @@ export default async function createUser(
});
}
}

function refineNonNull<T>(
input: T | null | undefined,
errorMessage?: string
): T {
if (input == null) {
throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`);
}

return input;
}
10 changes: 10 additions & 0 deletions examples/with-ethers-and-passkeys/src/pages/api/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function refineNonNull<T>(
input: T | null | undefined,
errorMessage?: string
): T {
if (input == null) {
throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`);
}

return input;
}
2 changes: 1 addition & 1 deletion examples/with-ethers-and-passkeys/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
79 changes: 39 additions & 40 deletions examples/with-viem-and-passkeys/src/pages/api/createKey.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,49 +46,44 @@ 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down Expand Up @@ -99,14 +100,3 @@ export default async function createUser(
});
}
}

function refineNonNull<T>(
input: T | null | undefined,
errorMessage?: string
): T {
if (input == null) {
throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`);
}

return input;
}
10 changes: 10 additions & 0 deletions examples/with-viem-and-passkeys/src/pages/api/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function refineNonNull<T>(
input: T | null | undefined,
errorMessage?: string
): T {
if (input == null) {
throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`);
}

return input;
}
2 changes: 1 addition & 1 deletion examples/with-viem-and-passkeys/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit cde1a5f

Please sign in to comment.