Skip to content

Commit 1985de8

Browse files
[SDK] Expose inMemoryStorage for inAppWallet backend usage (#7016)
1 parent 2b87b7b commit 1985de8

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

.changeset/loud-points-count.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Expose inMemoryStorage for inAppWallet backend usage

packages/thirdweb/src/exports/storage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export {
1111
type ResolveArweaveSchemeOptions,
1212
} from "../utils/arweave.js";
1313
export type { AsyncStorage } from "../utils/storage/AsyncStorage.js";
14+
export { inMemoryStorage } from "../utils/storage/inMemoryStorage.js";
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { AsyncStorage } from "./AsyncStorage.js";
2+
3+
const store = new Map<string, string>();
4+
5+
export const inMemoryStorage: AsyncStorage = {
6+
getItem: async (key: string) => {
7+
return store.get(key) ?? null;
8+
},
9+
setItem: async (key: string, value: string) => {
10+
store.set(key, value);
11+
},
12+
removeItem: async (key: string) => {
13+
store.delete(key);
14+
},
15+
};

packages/thirdweb/src/wallets/in-app/web/in-app.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,14 @@ import type { InAppWalletCreationOptions } from "../core/wallet/types.js";
141141
*
142142
* ### Connect to a backend account
143143
*
144+
* for usage in backends, you might also need to provide a 'storage' to store auth tokens. In-memory usually works for most purposes.
145+
*
144146
* ```ts
145147
* import { inAppWallet } from "thirdweb/wallets";
146148
*
147-
* const wallet = inAppWallet();
149+
* const wallet = inAppWallet({
150+
* storage: inMemoryStorage, // for usage in backends/scripts
151+
* });
148152
*
149153
* const account = await wallet.connect({
150154
* client,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from "vitest";
2+
import { TEST_CLIENT } from "~test/test-clients.js";
3+
import { sepolia } from "../../../../chains/chain-definitions/sepolia.js";
4+
import { inMemoryStorage } from "../../../../utils/storage/inMemoryStorage.js";
5+
import { inAppWallet } from "../in-app.js";
6+
7+
describe("InAppWallet", () => {
8+
it("should sign a message with backend strategy", async () => {
9+
const wallet = inAppWallet({
10+
smartAccount: {
11+
chain: sepolia,
12+
sponsorGas: true,
13+
},
14+
storage: inMemoryStorage,
15+
});
16+
const account = await wallet.connect({
17+
client: TEST_CLIENT,
18+
strategy: "backend",
19+
walletSecret: "test-secret",
20+
});
21+
expect(account.address).toBeDefined();
22+
const message = await account.signMessage({
23+
message: "Hello, world!",
24+
});
25+
expect(message).toBeDefined();
26+
});
27+
});

0 commit comments

Comments
 (0)