thirdweb 5.84.0-nightly-0fdfb8aa778a344aa847503f668e48feb5f72548-20250115000312 → 5.84.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/cjs/ai/chat.js +29 -0
  2. package/dist/cjs/ai/chat.js.map +1 -0
  3. package/dist/cjs/ai/common.js +69 -0
  4. package/dist/cjs/ai/common.js.map +1 -0
  5. package/dist/cjs/ai/execute.js +43 -0
  6. package/dist/cjs/ai/execute.js.map +1 -0
  7. package/dist/cjs/ai/index.js +8 -0
  8. package/dist/cjs/ai/index.js.map +1 -0
  9. package/dist/cjs/exports/ai.js +5 -0
  10. package/dist/cjs/exports/ai.js.map +1 -0
  11. package/dist/cjs/exports/wallets/engine.js +6 -0
  12. package/dist/cjs/exports/wallets/engine.js.map +1 -0
  13. package/dist/cjs/version.js +1 -1
  14. package/dist/cjs/version.js.map +1 -1
  15. package/dist/cjs/wallets/engine/index.js +143 -0
  16. package/dist/cjs/wallets/engine/index.js.map +1 -0
  17. package/dist/cjs/wallets/smart/lib/signing.js +1 -1
  18. package/dist/esm/ai/chat.js +26 -0
  19. package/dist/esm/ai/chat.js.map +1 -0
  20. package/dist/esm/ai/common.js +66 -0
  21. package/dist/esm/ai/common.js.map +1 -0
  22. package/dist/esm/ai/execute.js +40 -0
  23. package/dist/esm/ai/execute.js.map +1 -0
  24. package/dist/esm/ai/index.js +3 -0
  25. package/dist/esm/ai/index.js.map +1 -0
  26. package/dist/esm/exports/ai.js +2 -0
  27. package/dist/esm/exports/ai.js.map +1 -0
  28. package/dist/esm/exports/wallets/engine.js +2 -0
  29. package/dist/esm/exports/wallets/engine.js.map +1 -0
  30. package/dist/esm/version.js +1 -1
  31. package/dist/esm/version.js.map +1 -1
  32. package/dist/esm/wallets/engine/index.js +140 -0
  33. package/dist/esm/wallets/engine/index.js.map +1 -0
  34. package/dist/esm/wallets/smart/lib/signing.js +1 -1
  35. package/dist/types/ai/chat.d.ts +24 -0
  36. package/dist/types/ai/chat.d.ts.map +1 -0
  37. package/dist/types/ai/common.d.ts +22 -0
  38. package/dist/types/ai/common.d.ts.map +1 -0
  39. package/dist/types/ai/execute.d.ts +29 -0
  40. package/dist/types/ai/execute.d.ts.map +1 -0
  41. package/dist/types/ai/index.d.ts +4 -0
  42. package/dist/types/ai/index.d.ts.map +1 -0
  43. package/dist/types/exports/ai.d.ts +2 -0
  44. package/dist/types/exports/ai.d.ts.map +1 -0
  45. package/dist/types/exports/wallets/engine.d.ts +2 -0
  46. package/dist/types/exports/wallets/engine.d.ts.map +1 -0
  47. package/dist/types/version.d.ts +1 -1
  48. package/dist/types/version.d.ts.map +1 -1
  49. package/dist/types/wallets/engine/index.d.ts +54 -0
  50. package/dist/types/wallets/engine/index.d.ts.map +1 -0
  51. package/dist/types/wallets/smart/lib/signing.d.ts +1 -1
  52. package/package.json +9 -1
  53. package/src/ai/chat.test.ts +31 -0
  54. package/src/ai/chat.ts +26 -0
  55. package/src/ai/common.ts +114 -0
  56. package/src/ai/execute.test.ts +43 -0
  57. package/src/ai/execute.ts +44 -0
  58. package/src/ai/index.ts +3 -0
  59. package/src/exports/ai.ts +1 -0
  60. package/src/exports/wallets/engine.ts +4 -0
  61. package/src/version.ts +1 -1
  62. package/src/wallets/engine/engine-account.test.ts +69 -0
  63. package/src/wallets/engine/index.ts +198 -0
  64. package/src/wallets/smart/lib/signing.ts +1 -1
@@ -0,0 +1,69 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { TEST_CLIENT } from "../../../test/src/test-clients.js";
3
+ import { TEST_ACCOUNT_B } from "../../../test/src/test-wallets.js";
4
+ import { typedData } from "../../../test/src/typed-data.js";
5
+ import { sepolia } from "../../chains/chain-definitions/sepolia.js";
6
+ import { getContract } from "../../contract/contract.js";
7
+ import { claimTo } from "../../extensions/erc1155/drops/write/claimTo.js";
8
+ import { sendTransaction } from "../../transaction/actions/send-transaction.js";
9
+ import { engineAccount } from "./index.js";
10
+
11
+ describe.runIf(
12
+ process.env.TW_SECRET_KEY &&
13
+ process.env.ENGINE_URL &&
14
+ process.env.ENGINE_AUTH_TOKEN &&
15
+ process.env.ENGINE_WALLET_ADDRESS,
16
+ )("Engine", () => {
17
+ const engineAcc = engineAccount({
18
+ engineUrl: process.env.ENGINE_URL as string,
19
+ authToken: process.env.ENGINE_AUTH_TOKEN as string,
20
+ walletAddress: process.env.ENGINE_WALLET_ADDRESS as string,
21
+ chain: sepolia,
22
+ });
23
+
24
+ it("should sign a message", async () => {
25
+ const signature = await engineAcc.signMessage({
26
+ message: "hello",
27
+ });
28
+ expect(signature).toBeDefined();
29
+ });
30
+
31
+ it("should sign typed data", async () => {
32
+ const signature = await engineAcc.signTypedData({
33
+ ...typedData.basic,
34
+ });
35
+ expect(signature).toBeDefined();
36
+ });
37
+
38
+ it("should send a tx", async () => {
39
+ const tx = await sendTransaction({
40
+ account: engineAcc,
41
+ transaction: {
42
+ client: TEST_CLIENT,
43
+ chain: sepolia,
44
+ to: TEST_ACCOUNT_B.address,
45
+ value: 0n,
46
+ },
47
+ });
48
+ expect(tx).toBeDefined();
49
+ });
50
+
51
+ it("should send a extension tx", async () => {
52
+ const nftContract = getContract({
53
+ client: TEST_CLIENT,
54
+ chain: sepolia,
55
+ address: "0xe2cb0eb5147b42095c2FfA6F7ec953bb0bE347D8",
56
+ });
57
+ const claimTx = claimTo({
58
+ contract: nftContract,
59
+ to: TEST_ACCOUNT_B.address,
60
+ tokenId: 0n,
61
+ quantity: 1n,
62
+ });
63
+ const tx = await sendTransaction({
64
+ account: engineAcc,
65
+ transaction: claimTx,
66
+ });
67
+ expect(tx).toBeDefined();
68
+ });
69
+ });
@@ -0,0 +1,198 @@
1
+ import type { Chain } from "../../chains/types.js";
2
+ import type { Hex } from "../../utils/encoding/hex.js";
3
+ import { toHex } from "../../utils/encoding/hex.js";
4
+ import type { Account, SendTransactionOption } from "../interfaces/wallet.js";
5
+
6
+ /**
7
+ * Options for creating an engine account.
8
+ */
9
+ export type EngineAccountOptions = {
10
+ /**
11
+ * The URL of your engine instance.
12
+ */
13
+ engineUrl: string;
14
+ /**
15
+ * The auth token to use with the engine instance.
16
+ */
17
+ authToken: string;
18
+ /**
19
+ * The backend wallet to use for sending transactions inside engine.
20
+ */
21
+ walletAddress: string;
22
+ /**
23
+ * The chain to use for signing messages and typed data (smart backend wallet only).
24
+ */
25
+ chain?: Chain;
26
+ };
27
+
28
+ /**
29
+ * Creates an account that uses your engine backend wallet for sending transactions and signing messages.
30
+ *
31
+ * @param options - The options for the engine account.
32
+ * @returns An account that uses your engine backend wallet.
33
+ *
34
+ * @beta
35
+ * @wallet
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import { engineAccount } from "thirdweb/wallets/engine";
40
+ *
41
+ * const engineAcc = engineAccount({
42
+ * engineUrl: "https://engine.thirdweb.com",
43
+ * authToken: "your-auth-token",
44
+ * walletAddress: "0x...",
45
+ * });
46
+ *
47
+ * // then use the account as you would any other account
48
+ * const transaction = claimTo({
49
+ * contract,
50
+ * to: "0x...",
51
+ * quantity: 1n,
52
+ * });
53
+ * const result = await sendTransaction({ transaction, account: engineAcc });
54
+ * console.log("Transaction sent:", result.transactionHash);
55
+ * ```
56
+ */
57
+ export function engineAccount(options: EngineAccountOptions): Account {
58
+ const { engineUrl, authToken, walletAddress, chain } = options;
59
+
60
+ // these are shared across all methods
61
+ const headers: HeadersInit = {
62
+ "x-backend-wallet-address": walletAddress,
63
+ Authorization: `Bearer ${authToken}`,
64
+ "Content-Type": "application/json",
65
+ };
66
+
67
+ return {
68
+ address: walletAddress,
69
+ sendTransaction: async (transaction: SendTransactionOption) => {
70
+ const ENGINE_URL = new URL(engineUrl);
71
+ ENGINE_URL.pathname = `/backend-wallet/${transaction.chainId}/send-transaction`;
72
+
73
+ const engineData: Record<string, string | undefined> = {
74
+ // add to address if we have it (is optional to pass to engine)
75
+ toAddress: transaction.to || undefined,
76
+ // engine wants a hex string here so we serialize it
77
+ data: transaction.data || "0x",
78
+ // value is always required
79
+ value: toHex(transaction.value ?? 0n),
80
+ };
81
+
82
+ // TODO: gas overrides etc?
83
+
84
+ const engineRes = await fetch(ENGINE_URL, {
85
+ method: "POST",
86
+ headers,
87
+ body: JSON.stringify(engineData),
88
+ });
89
+ if (!engineRes.ok) {
90
+ const body = await engineRes.text();
91
+ throw new Error(
92
+ `Engine request failed with status ${engineRes.status} - ${body}`,
93
+ );
94
+ }
95
+ const engineJson = (await engineRes.json()) as {
96
+ result: {
97
+ queueId: string;
98
+ };
99
+ };
100
+
101
+ // wait for the queueId to be processed
102
+ ENGINE_URL.pathname = `/transaction/status/${engineJson.result.queueId}`;
103
+ const startTime = Date.now();
104
+ const TIMEOUT_IN_MS = 5 * 60 * 1000; // 5 minutes in milliseconds
105
+
106
+ while (Date.now() - startTime < TIMEOUT_IN_MS) {
107
+ const queueRes = await fetch(ENGINE_URL, {
108
+ method: "GET",
109
+ headers,
110
+ });
111
+ if (!queueRes.ok) {
112
+ const body = await queueRes.text();
113
+ throw new Error(
114
+ `Engine request failed with status ${queueRes.status} - ${body}`,
115
+ );
116
+ }
117
+ const queueJSON = (await queueRes.json()) as {
118
+ result: {
119
+ status: "queued" | "mined" | "cancelled" | "errored";
120
+ transactionHash: Hex | null;
121
+ userOpHash: Hex | null;
122
+ errorMessage: string | null;
123
+ };
124
+ };
125
+
126
+ if (
127
+ queueJSON.result.status === "errored" &&
128
+ queueJSON.result.errorMessage
129
+ ) {
130
+ throw new Error(queueJSON.result.errorMessage);
131
+ }
132
+ if (queueJSON.result.transactionHash) {
133
+ return {
134
+ transactionHash: queueJSON.result.transactionHash,
135
+ };
136
+ }
137
+ // wait 1s before checking again
138
+ await new Promise((resolve) => setTimeout(resolve, 1000));
139
+ }
140
+ throw new Error("Transaction timed out after 5 minutes");
141
+ },
142
+ signMessage: async ({ message }) => {
143
+ let engineMessage: string | Hex;
144
+ let isBytes = false;
145
+ if (typeof message === "string") {
146
+ engineMessage = message;
147
+ } else {
148
+ engineMessage = toHex(message.raw);
149
+ isBytes = true;
150
+ }
151
+
152
+ const ENGINE_URL = new URL(engineUrl);
153
+ ENGINE_URL.pathname = "/backend-wallet/sign-message";
154
+ const engineRes = await fetch(ENGINE_URL, {
155
+ method: "POST",
156
+ headers,
157
+ body: JSON.stringify({
158
+ message: engineMessage,
159
+ isBytes,
160
+ chainId: chain?.id,
161
+ }),
162
+ });
163
+ if (!engineRes.ok) {
164
+ const body = await engineRes.text();
165
+ throw new Error(
166
+ `Engine request failed with status ${engineRes.status} - ${body}`,
167
+ );
168
+ }
169
+ const engineJson = (await engineRes.json()) as {
170
+ result: Hex;
171
+ };
172
+ return engineJson.result;
173
+ },
174
+ signTypedData: async (_typedData) => {
175
+ const ENGINE_URL = new URL(engineUrl);
176
+ ENGINE_URL.pathname = "/backend-wallet/sign-typed-data";
177
+ const engineRes = await fetch(ENGINE_URL, {
178
+ method: "POST",
179
+ headers,
180
+ body: JSON.stringify({
181
+ domain: _typedData.domain,
182
+ types: _typedData.types,
183
+ value: _typedData.message,
184
+ }),
185
+ });
186
+ if (!engineRes.ok) {
187
+ engineRes.body?.cancel();
188
+ throw new Error(
189
+ `Engine request failed with status ${engineRes.status}`,
190
+ );
191
+ }
192
+ const engineJson = (await engineRes.json()) as {
193
+ result: Hex;
194
+ };
195
+ return engineJson.result;
196
+ },
197
+ };
198
+ }
@@ -288,7 +288,7 @@ async function checkFor712Factory({
288
288
  * });
289
289
  * ```
290
290
  *
291
- * @wallets
291
+ * @wallet
292
292
  */
293
293
  export async function deploySmartAccount(args: {
294
294
  smartAccount: Account;