Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/loud-points-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Expose inMemoryStorage for inAppWallet backend usage
1 change: 1 addition & 0 deletions packages/thirdweb/src/exports/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
15 changes: 15 additions & 0 deletions packages/thirdweb/src/utils/storage/inMemoryStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { AsyncStorage } from "./AsyncStorage.js";

const store = new Map<string, string>();

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);
},

Check warning on line 14 in packages/thirdweb/src/utils/storage/inMemoryStorage.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/utils/storage/inMemoryStorage.ts#L13-L14

Added lines #L13 - L14 were not covered by tests
};
6 changes: 5 additions & 1 deletion packages/thirdweb/src/wallets/in-app/web/in-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
});
});
Loading