|
| 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 | +export function engineAccount(options: EngineAccountOptions): Account { |
| 29 | + const { engineUrl, authToken, walletAddress, chain } = options; |
| 30 | + |
| 31 | + // these are shared across all methods |
| 32 | + const headers: HeadersInit = { |
| 33 | + "x-backend-wallet-address": walletAddress, |
| 34 | + Authorization: `Bearer ${authToken}`, |
| 35 | + "Content-Type": "application/json", |
| 36 | + }; |
| 37 | + |
| 38 | + return { |
| 39 | + address: walletAddress, |
| 40 | + sendTransaction: async (transaction: SendTransactionOption) => { |
| 41 | + // this will be the baseline URL for the requests |
| 42 | + const ENGINE_URL = new URL(engineUrl); |
| 43 | + |
| 44 | + const engineData: Record<string, string | undefined> = { |
| 45 | + // add to address if we have it (is optional to pass to engine) |
| 46 | + toAddress: transaction.to || undefined, |
| 47 | + // engine wants a hex string here so we serialize it |
| 48 | + data: transaction.data || "0x", |
| 49 | + // value is always required |
| 50 | + value: toHex(transaction.value ?? 0n), |
| 51 | + }; |
| 52 | + |
| 53 | + // TODO: gas overrides etc? |
| 54 | + |
| 55 | + ENGINE_URL.pathname = `/backend-wallet/${transaction.chainId}/send-transaction`; |
| 56 | + const engineRes = await fetch(ENGINE_URL, { |
| 57 | + method: "POST", |
| 58 | + headers, |
| 59 | + body: JSON.stringify(engineData), |
| 60 | + }); |
| 61 | + if (!engineRes.ok) { |
| 62 | + const body = await engineRes.text(); |
| 63 | + throw new Error( |
| 64 | + `Engine request failed with status ${engineRes.status} - ${body}`, |
| 65 | + ); |
| 66 | + } |
| 67 | + const engineJson = (await engineRes.json()) as { |
| 68 | + result: { |
| 69 | + queueId: string; |
| 70 | + }; |
| 71 | + }; |
| 72 | + |
| 73 | + // wait for the queueId to be processed |
| 74 | + ENGINE_URL.pathname = `/transaction/status/${engineJson.result.queueId}`; |
| 75 | + const startTime = Date.now(); |
| 76 | + const TIMEOUT_IN_MS = 5 * 60 * 1000; // 5 minutes in milliseconds |
| 77 | + |
| 78 | + while (Date.now() - startTime < TIMEOUT_IN_MS) { |
| 79 | + const queueRes = await fetch(ENGINE_URL, { |
| 80 | + method: "GET", |
| 81 | + headers, |
| 82 | + }); |
| 83 | + if (!queueRes.ok) { |
| 84 | + const body = await queueRes.text(); |
| 85 | + throw new Error( |
| 86 | + `Engine request failed with status ${queueRes.status} - ${body}`, |
| 87 | + ); |
| 88 | + } |
| 89 | + const queueJSON = (await queueRes.json()) as { |
| 90 | + result: { |
| 91 | + status: "queued" | "mined" | "cancelled" | "errored"; |
| 92 | + transactionHash: Hex | null; |
| 93 | + userOpHash: Hex | null; |
| 94 | + errorMessage: string | null; |
| 95 | + }; |
| 96 | + }; |
| 97 | + |
| 98 | + if ( |
| 99 | + queueJSON.result.status === "errored" && |
| 100 | + queueJSON.result.errorMessage |
| 101 | + ) { |
| 102 | + throw new Error(queueJSON.result.errorMessage); |
| 103 | + } |
| 104 | + if (queueJSON.result.transactionHash) { |
| 105 | + return { |
| 106 | + transactionHash: queueJSON.result.transactionHash, |
| 107 | + }; |
| 108 | + } |
| 109 | + // wait 1s before checking again |
| 110 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 111 | + } |
| 112 | + throw new Error("Transaction timed out after 5 minutes"); |
| 113 | + }, |
| 114 | + signMessage: async ({ message }) => { |
| 115 | + let engineMesasage: string | Hex; |
| 116 | + let isBytes = false; |
| 117 | + if (typeof message === "string") { |
| 118 | + engineMesasage = message; |
| 119 | + } else { |
| 120 | + engineMesasage = toHex(message.raw); |
| 121 | + isBytes = true; |
| 122 | + } |
| 123 | + |
| 124 | + // this will be the baseline URL for the requests |
| 125 | + const ENGINE_URL = new URL(engineUrl); |
| 126 | + // set the pathname correctly |
| 127 | + // see: https://redocly.github.io/redoc/?url=https://demo.web3api.thirdweb.com/json#tag/Backend-Wallet/operation/signMessage |
| 128 | + ENGINE_URL.pathname = "/backend-wallet/sign-message"; |
| 129 | + const engineRes = await fetch(ENGINE_URL, { |
| 130 | + method: "POST", |
| 131 | + headers, |
| 132 | + body: JSON.stringify({ |
| 133 | + message: engineMesasage, |
| 134 | + isBytes, |
| 135 | + chainId: chain?.id, |
| 136 | + }), |
| 137 | + }); |
| 138 | + if (!engineRes.ok) { |
| 139 | + const body = await engineRes.text(); |
| 140 | + throw new Error( |
| 141 | + `Engine request failed with status ${engineRes.status} - ${body}`, |
| 142 | + ); |
| 143 | + } |
| 144 | + const engineJson = (await engineRes.json()) as { |
| 145 | + result: Hex; |
| 146 | + }; |
| 147 | + return engineJson.result; |
| 148 | + }, |
| 149 | + signTypedData: async (_typedData) => { |
| 150 | + // this will be the baseline URL for the requests |
| 151 | + const ENGINE_URL = new URL(engineUrl); |
| 152 | + // set the pathname correctly |
| 153 | + // see: https://redocly.github.io/redoc/?url=https://demo.web3api.thirdweb.com/json#tag/Backend-Wallet/operation/signTypedData |
| 154 | + ENGINE_URL.pathname = "/backend-wallet/sign-typed-data"; |
| 155 | + const engineRes = await fetch(ENGINE_URL, { |
| 156 | + method: "POST", |
| 157 | + headers, |
| 158 | + body: JSON.stringify({ |
| 159 | + domain: _typedData.domain, |
| 160 | + types: _typedData.types, |
| 161 | + value: _typedData.message, |
| 162 | + }), |
| 163 | + }); |
| 164 | + if (!engineRes.ok) { |
| 165 | + engineRes.body?.cancel(); |
| 166 | + throw new Error( |
| 167 | + `Engine request failed with status ${engineRes.status}`, |
| 168 | + ); |
| 169 | + } |
| 170 | + const engineJson = (await engineRes.json()) as { |
| 171 | + result: Hex; |
| 172 | + }; |
| 173 | + return engineJson.result; |
| 174 | + }, |
| 175 | + }; |
| 176 | +} |
0 commit comments