|
| 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 | +} |
0 commit comments