Skip to content

Commit

Permalink
update backend
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Nov 4, 2024
1 parent 6b3ea14 commit 07e233a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 112 deletions.
11 changes: 6 additions & 5 deletions examples/wallet-import-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@turnkey/api-key-stamper": "workspace:*",
"@turnkey/http": "workspace:*",
"@turnkey/sdk-server": "workspace:*",
"@turnkey/iframe-stamper": "workspace:*",
"@types/node": "20.3.1",
"@types/react": "18.2.14",
"@types/react-dom": "18.2.6",
"axios": "^1.7.4",
"classnames": "^2.3.2",
"encoding": "^0.1.13",
Expand All @@ -30,5 +26,10 @@
"react-dom": "18.2.0",
"react-hook-form": "^7.45.1",
"typescript": "5.1.3"
},
"devDependencies": {
"@types/node": "20.3.1",
"@types/react": "18.2.14",
"@types/react-dom": "18.2.6"
}
}
34 changes: 10 additions & 24 deletions examples/wallet-import-export/src/pages/api/exportWallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { TurnkeyClient, createActivityPoller } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { Turnkey } from "@turnkey/sdk-server";

type ExportWalletRequest = {
walletId: string;
Expand All @@ -22,36 +21,23 @@ export default async function exportWallet(
) {
try {
const request = req.body as ExportWalletRequest;
const turnkeyClient = new TurnkeyClient(
{ baseUrl: process.env.NEXT_PUBLIC_BASE_URL! },
new ApiKeyStamper({
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
})
);

const activityPoller = createActivityPoller({
client: turnkeyClient,
requestFn: turnkeyClient.exportWallet,
const turnkeyClient = new Turnkey({
apiBaseUrl: "https://api.turnkey.com",
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
defaultOrganizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
});

const completedActivity = await activityPoller({
type: "ACTIVITY_TYPE_EXPORT_WALLET",
timestampMs: String(Date.now()),
organizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
parameters: {
walletId: request.walletId,
targetPublicKey: request.targetPublicKey,
},
const exportResponse = await turnkeyClient.apiClient().exportWallet({
walletId: request.walletId,
targetPublicKey: request.targetPublicKey,
});

const walletId = completedActivity.result.exportWalletResult?.walletId;
const { walletId, exportBundle } = exportResponse;
if (!walletId) {
throw new Error("Expected a non-null wallet ID!");
}

const exportBundle =
completedActivity.result.exportWalletResult?.exportBundle;
if (!exportBundle) {
throw new Error("Expected a non-null export bundle!");
}
Expand Down
19 changes: 8 additions & 11 deletions examples/wallet-import-export/src/pages/api/getWallets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { TurnkeyApiTypes, TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { Turnkey, TurnkeyApiTypes } from "@turnkey/sdk-server";

type TWallet = TurnkeyApiTypes["v1Wallet"];

Expand All @@ -21,19 +20,17 @@ export default async function getWallets(
res: NextApiResponse<GetWalletsResponse | ErrorMessage>
) {
const getWalletsRequest = req.body as GetWalletsRequest;

const turnkeyClient = new TurnkeyClient(
{ baseUrl: process.env.NEXT_PUBLIC_BASE_URL! },
new ApiKeyStamper({
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
})
);
const turnkeyClient = new Turnkey({
apiBaseUrl: "https://api.turnkey.com",
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
defaultOrganizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
});

const organizationId = getWalletsRequest.organizationId;

try {
const walletsResponse = await turnkeyClient.getWallets({
const walletsResponse = await turnkeyClient.apiClient().getWallets({
organizationId,
});

Expand Down
36 changes: 12 additions & 24 deletions examples/wallet-import-export/src/pages/api/importWallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { TurnkeyClient, createActivityPoller } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { Turnkey } from "@turnkey/sdk-server";

type ImportWalletRequest = {
userId: string;
Expand All @@ -22,32 +21,21 @@ export default async function importWallet(
) {
try {
const request = req.body as ImportWalletRequest;
const turnkeyClient = new TurnkeyClient(
{ baseUrl: process.env.NEXT_PUBLIC_BASE_URL! },
new ApiKeyStamper({
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
})
);

const activityPoller = createActivityPoller({
client: turnkeyClient,
requestFn: turnkeyClient.importWallet,
const turnkeyClient = new Turnkey({
apiBaseUrl: "https://api.turnkey.com",
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
defaultOrganizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
});

const completedActivity = await activityPoller({
type: "ACTIVITY_TYPE_IMPORT_WALLET",
timestampMs: String(Date.now()),
organizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
parameters: {
userId: request.userId,
walletName: request.walletName,
encryptedBundle: request.encryptedBundle,
accounts: [],
},
const importWalletResponse = await turnkeyClient.apiClient().importWallet({
userId: request.userId,
walletName: request.walletName,
encryptedBundle: request.encryptedBundle,
accounts: [],
});

const walletId = completedActivity.result.importWalletResult?.walletId;
const { walletId } = importWalletResponse;
if (!walletId) {
throw new Error("Expected a non-null wallet ID!");
}
Expand Down
34 changes: 12 additions & 22 deletions examples/wallet-import-export/src/pages/api/initImportWallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { TurnkeyClient, createActivityPoller } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { Turnkey } from "@turnkey/sdk-server";

type InitImportWalletRequest = {
userId: string;
Expand All @@ -20,30 +19,21 @@ export default async function initImportWallet(
) {
try {
const request = req.body as InitImportWalletRequest;
const turnkeyClient = new TurnkeyClient(
{ baseUrl: process.env.NEXT_PUBLIC_BASE_URL! },
new ApiKeyStamper({
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
})
);

const activityPoller = createActivityPoller({
client: turnkeyClient,
requestFn: turnkeyClient.initImportWallet,
const turnkeyClient = new Turnkey({
apiBaseUrl: "https://api.turnkey.com",
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
defaultOrganizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
});

const completedActivity = await activityPoller({
type: "ACTIVITY_TYPE_INIT_IMPORT_WALLET",
timestampMs: String(Date.now()),
organizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
parameters: {
const initImportWalletResponse = await turnkeyClient
.apiClient()
.initImportWallet({
userId: request.userId,
},
});
});

const { importBundle } = initImportWalletResponse;

const importBundle =
completedActivity.result.initImportWalletResult?.importBundle;
if (!importBundle) {
throw new Error("Expected a non-null import bundle!");
}
Expand Down
19 changes: 8 additions & 11 deletions examples/wallet-import-export/src/pages/api/whoami.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { Turnkey } from "@turnkey/sdk-server";

type GetWhoamiRequest = {
organizationId: string;
Expand All @@ -22,19 +21,17 @@ export default async function getWallets(
res: NextApiResponse<GetWhoamiResponse | ErrorMessage>
) {
const getWhoamiRequest = req.body as GetWhoamiRequest;

const turnkeyClient = new TurnkeyClient(
{ baseUrl: process.env.NEXT_PUBLIC_BASE_URL! },
new ApiKeyStamper({
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
})
);
const turnkeyClient = new Turnkey({
apiBaseUrl: "https://api.turnkey.com",
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
defaultOrganizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
});

const organizationId = getWhoamiRequest.organizationId;

try {
const whoamiResponse = await turnkeyClient.getWhoami({
const whoamiResponse = await turnkeyClient.apiClient().getWhoami({
organizationId,
});

Expand Down
28 changes: 13 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 07e233a

Please sign in to comment.