|
| 1 | +import { sendTransaction, signMessage } from "@thirdweb-dev/engine"; |
| 2 | +import { beforeAll, describe, expect, it } from "vitest"; |
| 3 | +import { TEST_CLIENT } from "~test/test-clients.js"; |
| 4 | +import { sepolia } from "../../../../chains/chain-definitions/sepolia.js"; |
| 5 | +import { createThirdwebClient } from "../../../../client/client.js"; |
| 6 | +import { waitForTransactionHash } from "../../../../engine/wait-for-tx-hash.js"; |
| 7 | +import { |
| 8 | + getThirdwebBaseUrl, |
| 9 | + setThirdwebDomains, |
| 10 | +} from "../../../../utils/domains.js"; |
| 11 | +import { getClientFetch } from "../../../../utils/fetch.js"; |
| 12 | +import { stringify } from "../../../../utils/json.js"; |
| 13 | +import type { Account } from "../../../interfaces/wallet.js"; |
| 14 | +import { inAppWallet } from "../in-app.js"; |
| 15 | + |
| 16 | +// TODO: productionize this test |
| 17 | +describe |
| 18 | + .runIf(process.env.TW_SECRET_KEY) |
| 19 | + .skip("InAppWallet Gateway Tests", () => { |
| 20 | + let account: Account; |
| 21 | + let authToken: string | null | undefined; |
| 22 | + const clientIdFetch = getClientFetch( |
| 23 | + createThirdwebClient({ |
| 24 | + clientId: TEST_CLIENT.clientId, |
| 25 | + }), |
| 26 | + ); |
| 27 | + |
| 28 | + beforeAll(async () => { |
| 29 | + setThirdwebDomains({ |
| 30 | + bundler: "bundler.thirdweb-dev.com", |
| 31 | + engineCloud: "engine.thirdweb-dev.com", |
| 32 | + inAppWallet: "embedded-wallet.thirdweb-dev.com", |
| 33 | + rpc: "rpc.thirdweb-dev.com", |
| 34 | + }); |
| 35 | + const wallet = inAppWallet(); |
| 36 | + account = await wallet.connect({ |
| 37 | + client: TEST_CLIENT, |
| 38 | + strategy: "backend", |
| 39 | + walletSecret: "test-secret", |
| 40 | + }); |
| 41 | + authToken = wallet.getAuthToken?.(); |
| 42 | + expect(authToken).toBeDefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should sign a message with backend strategy", async () => { |
| 46 | + const rawSignature = await account.signMessage({ |
| 47 | + message: "Hello, world!", |
| 48 | + }); |
| 49 | + |
| 50 | + // sign via api |
| 51 | + const signResult = await signMessage({ |
| 52 | + baseUrl: getThirdwebBaseUrl("engineCloud"), |
| 53 | + body: { |
| 54 | + params: [ |
| 55 | + { |
| 56 | + format: "text", |
| 57 | + message: "Hello, world!", |
| 58 | + }, |
| 59 | + ], |
| 60 | + signingOptions: { |
| 61 | + from: account.address, |
| 62 | + type: "eoa", |
| 63 | + }, |
| 64 | + }, |
| 65 | + bodySerializer: stringify, |
| 66 | + fetch: clientIdFetch, |
| 67 | + headers: { |
| 68 | + "x-wallet-access-token": authToken, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + const signatureResult = signResult.data?.result?.results[0]; |
| 73 | + if (signatureResult && "result" in signatureResult) { |
| 74 | + expect(signatureResult.result.signature).toEqual(rawSignature); |
| 75 | + } else { |
| 76 | + throw new Error( |
| 77 | + `Failed to sign message: ${stringify(signatureResult?.error) || "Unknown error"}`, |
| 78 | + ); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it("should queue a 4337 transaction", async () => { |
| 83 | + const body = { |
| 84 | + executionOptions: { |
| 85 | + chainId: sepolia.id, |
| 86 | + from: account.address, |
| 87 | + type: "auto" as const, |
| 88 | + }, |
| 89 | + params: [ |
| 90 | + { |
| 91 | + data: "0x", |
| 92 | + to: account.address, |
| 93 | + value: "0", |
| 94 | + }, |
| 95 | + ], |
| 96 | + }; |
| 97 | + const result = await sendTransaction({ |
| 98 | + baseUrl: getThirdwebBaseUrl("engineCloud"), |
| 99 | + body, |
| 100 | + bodySerializer: stringify, |
| 101 | + fetch: clientIdFetch, |
| 102 | + headers: { |
| 103 | + "x-wallet-access-token": authToken, |
| 104 | + }, |
| 105 | + }); |
| 106 | + if (result.error) { |
| 107 | + throw new Error( |
| 108 | + `Error sending transaction: ${stringify(result.error)}`, |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + const txId = result.data?.result.transactions[0]?.id; |
| 113 | + console.log(txId); |
| 114 | + if (!txId) { |
| 115 | + throw new Error("No transaction ID found"); |
| 116 | + } |
| 117 | + |
| 118 | + const tx = await waitForTransactionHash({ |
| 119 | + client: TEST_CLIENT, |
| 120 | + transactionId: txId, |
| 121 | + }); |
| 122 | + |
| 123 | + console.log(tx); |
| 124 | + expect(tx.transactionHash).toBeDefined(); |
| 125 | + }); |
| 126 | + }); |
0 commit comments