From 8339910481adbc49ca10ebbb0895013c574bd596 Mon Sep 17 00:00:00 2001 From: Joaquim Verges Date: Mon, 12 May 2025 22:13:07 +1200 Subject: [PATCH] Expose inMemoryStorage for inAppWallet backend usage --- .changeset/loud-points-count.md | 5 ++++ packages/thirdweb/src/exports/storage.ts | 1 + .../src/utils/storage/inMemoryStorage.ts | 15 +++++++++++ .../thirdweb/src/wallets/in-app/web/in-app.ts | 6 ++++- .../in-app/web/lib/in-app-backend.test.ts | 27 +++++++++++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 .changeset/loud-points-count.md create mode 100644 packages/thirdweb/src/utils/storage/inMemoryStorage.ts create mode 100644 packages/thirdweb/src/wallets/in-app/web/lib/in-app-backend.test.ts diff --git a/.changeset/loud-points-count.md b/.changeset/loud-points-count.md new file mode 100644 index 00000000000..2bd51f00b00 --- /dev/null +++ b/.changeset/loud-points-count.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Expose inMemoryStorage for inAppWallet backend usage diff --git a/packages/thirdweb/src/exports/storage.ts b/packages/thirdweb/src/exports/storage.ts index d3ec1bb8a64..74284cf45c2 100644 --- a/packages/thirdweb/src/exports/storage.ts +++ b/packages/thirdweb/src/exports/storage.ts @@ -11,3 +11,4 @@ export { type ResolveArweaveSchemeOptions, } from "../utils/arweave.js"; export type { AsyncStorage } from "../utils/storage/AsyncStorage.js"; +export { inMemoryStorage } from "../utils/storage/inMemoryStorage.js"; diff --git a/packages/thirdweb/src/utils/storage/inMemoryStorage.ts b/packages/thirdweb/src/utils/storage/inMemoryStorage.ts new file mode 100644 index 00000000000..7496e07b19b --- /dev/null +++ b/packages/thirdweb/src/utils/storage/inMemoryStorage.ts @@ -0,0 +1,15 @@ +import type { AsyncStorage } from "./AsyncStorage.js"; + +const store = new Map(); + +export const inMemoryStorage: AsyncStorage = { + getItem: async (key: string) => { + return store.get(key) ?? null; + }, + setItem: async (key: string, value: string) => { + store.set(key, value); + }, + removeItem: async (key: string) => { + store.delete(key); + }, +}; diff --git a/packages/thirdweb/src/wallets/in-app/web/in-app.ts b/packages/thirdweb/src/wallets/in-app/web/in-app.ts index 368a8097042..11597f42ff8 100644 --- a/packages/thirdweb/src/wallets/in-app/web/in-app.ts +++ b/packages/thirdweb/src/wallets/in-app/web/in-app.ts @@ -141,10 +141,14 @@ import type { InAppWalletCreationOptions } from "../core/wallet/types.js"; * * ### Connect to a backend account * + * for usage in backends, you might also need to provide a 'storage' to store auth tokens. In-memory usually works for most purposes. + * * ```ts * import { inAppWallet } from "thirdweb/wallets"; * - * const wallet = inAppWallet(); + * const wallet = inAppWallet({ + * storage: inMemoryStorage, // for usage in backends/scripts + * }); * * const account = await wallet.connect({ * client, diff --git a/packages/thirdweb/src/wallets/in-app/web/lib/in-app-backend.test.ts b/packages/thirdweb/src/wallets/in-app/web/lib/in-app-backend.test.ts new file mode 100644 index 00000000000..8da74d7c2eb --- /dev/null +++ b/packages/thirdweb/src/wallets/in-app/web/lib/in-app-backend.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, it } from "vitest"; +import { TEST_CLIENT } from "~test/test-clients.js"; +import { sepolia } from "../../../../chains/chain-definitions/sepolia.js"; +import { inMemoryStorage } from "../../../../utils/storage/inMemoryStorage.js"; +import { inAppWallet } from "../in-app.js"; + +describe("InAppWallet", () => { + it("should sign a message with backend strategy", async () => { + const wallet = inAppWallet({ + smartAccount: { + chain: sepolia, + sponsorGas: true, + }, + storage: inMemoryStorage, + }); + const account = await wallet.connect({ + client: TEST_CLIENT, + strategy: "backend", + walletSecret: "test-secret", + }); + expect(account.address).toBeDefined(); + const message = await account.signMessage({ + message: "Hello, world!", + }); + expect(message).toBeDefined(); + }); +});