@shroud-fi/self-host-relayer 0.1.1 → 0.1.3

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 (45) hide show
  1. package/package.json +6 -3
  2. package/src/dev-entry.ts +91 -0
  3. package/src/errors.ts +62 -0
  4. package/src/index.ts +31 -0
  5. package/src/prod-entry.ts +112 -0
  6. package/src/relay-eth.ts +122 -0
  7. package/src/relay.ts +110 -0
  8. package/src/server.ts +408 -0
  9. package/src/types.ts +90 -0
  10. package/tsconfig.json +9 -0
  11. package/dist/cjs/dev-entry.d.ts.map +0 -1
  12. package/dist/cjs/dev-entry.js.map +0 -1
  13. package/dist/cjs/errors.d.ts.map +0 -1
  14. package/dist/cjs/errors.js.map +0 -1
  15. package/dist/cjs/index.d.ts.map +0 -1
  16. package/dist/cjs/index.js.map +0 -1
  17. package/dist/cjs/prod-entry.d.ts.map +0 -1
  18. package/dist/cjs/prod-entry.js.map +0 -1
  19. package/dist/cjs/relay-eth.d.ts.map +0 -1
  20. package/dist/cjs/relay-eth.js.map +0 -1
  21. package/dist/cjs/relay.d.ts.map +0 -1
  22. package/dist/cjs/relay.js.map +0 -1
  23. package/dist/cjs/server.d.ts.map +0 -1
  24. package/dist/cjs/server.js.map +0 -1
  25. package/dist/cjs/types.d.ts.map +0 -1
  26. package/dist/cjs/types.js.map +0 -1
  27. package/dist/esm/dev-entry.d.ts.map +0 -1
  28. package/dist/esm/dev-entry.js.map +0 -1
  29. package/dist/esm/errors.d.ts.map +0 -1
  30. package/dist/esm/errors.js.map +0 -1
  31. package/dist/esm/index.d.ts.map +0 -1
  32. package/dist/esm/index.js.map +0 -1
  33. package/dist/esm/prod-entry.d.ts.map +0 -1
  34. package/dist/esm/prod-entry.js.map +0 -1
  35. package/dist/esm/relay-eth.d.ts.map +0 -1
  36. package/dist/esm/relay-eth.js.map +0 -1
  37. package/dist/esm/relay.d.ts.map +0 -1
  38. package/dist/esm/relay.js.map +0 -1
  39. package/dist/esm/server.d.ts.map +0 -1
  40. package/dist/esm/server.js.map +0 -1
  41. package/dist/esm/types.d.ts.map +0 -1
  42. package/dist/esm/types.js.map +0 -1
  43. package/dist/tsconfig.cjs.tsbuildinfo +0 -1
  44. package/dist/tsconfig.esm.tsbuildinfo +0 -1
  45. package/dist/tsconfig.tsbuildinfo +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shroud-fi/self-host-relayer",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Self-hosted gasless sweep relayer service for ShroudFi — Fastify server that broadcasts EIP-7702 / ERC-2771 sweep transactions.",
5
5
  "keywords": [
6
6
  "shroudfi",
@@ -26,13 +26,16 @@
26
26
  }
27
27
  },
28
28
  "files": [
29
- "dist"
29
+ "dist",
30
+ "src",
31
+ "tsconfig.json",
32
+ "README.md"
30
33
  ],
31
34
  "dependencies": {
32
35
  "fastify": "^5.0.0",
33
36
  "@fastify/cors": "^10.0.0",
34
37
  "@fastify/rate-limit": "^10.0.0",
35
- "@shroud-fi/transport": "0.1.2"
38
+ "@shroud-fi/transport": "0.1.4"
36
39
  },
37
40
  "peerDependencies": {
38
41
  "viem": "^2.21.0"
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Dev entry: reads config from env vars and starts the server.
3
+ *
4
+ * Required env:
5
+ * SHROUDFI_RELAYER_CONTRACT - deployed ShroudFiRelayer address
6
+ * SHROUDFI_RELAYER_FORWARDER_KEY - 0x-prefixed 32-byte forwarder EOA key
7
+ * SHROUDFI_RELAYER_RPC_URL - chain RPC URL
8
+ *
9
+ * Optional env:
10
+ * SHROUDFI_RELAYER_CHAIN_ID - default 84532
11
+ * SHROUDFI_RELAYER_PORT - default 8787
12
+ * SHROUDFI_RELAYER_ORIGINS - comma-sep allowed origins (default localhost demo)
13
+ * SHROUDFI_RELAYER_RATE_LIMIT - per-IP requests per minute (default 30)
14
+ * SHROUDFI_ETH_RELAYER_CONTRACT - deployed ShroudFiEthRelayer address.
15
+ * When set, /relay-eth becomes active for
16
+ * gasless ETH sweeps via EIP-7702. When
17
+ * unset, /relay-eth returns 501.
18
+ *
19
+ * Privacy: this file is the ONLY place that touches process.env for the
20
+ * forwarder key. Once read, the key flows into a viem account closure inside
21
+ * the server and never resurfaces.
22
+ */
23
+
24
+ import { startServer } from './server.js';
25
+ import { RelayConfigError } from './errors.js';
26
+ import type { Address, Hex } from 'viem';
27
+ import type { ServiceConfig } from './types.js';
28
+
29
+ function envOrThrow(name: string): string {
30
+ const v = process.env[name];
31
+ if (v === undefined || v.trim().length === 0) {
32
+ throw new RelayConfigError(name);
33
+ }
34
+ return v.trim();
35
+ }
36
+
37
+ function loadConfig(): ServiceConfig {
38
+ const chainId = Number(
39
+ process.env.SHROUDFI_RELAYER_CHAIN_ID?.trim() ?? '84532',
40
+ );
41
+ const port = Number(process.env.SHROUDFI_RELAYER_PORT?.trim() ?? '8787');
42
+ const rateLimitPerMinute = Number(
43
+ process.env.SHROUDFI_RELAYER_RATE_LIMIT?.trim() ?? '30',
44
+ );
45
+ const originsCsv =
46
+ process.env.SHROUDFI_RELAYER_ORIGINS?.trim() ??
47
+ 'http://localhost:3000,http://localhost:3001,http://localhost:3002,http://localhost:3003,http://localhost:3005,http://localhost:3006,http://localhost:3008,http://localhost:3009,http://127.0.0.1:3005,http://127.0.0.1:3009';
48
+ const allowedOrigins = originsCsv
49
+ .split(',')
50
+ .map((s) => s.trim())
51
+ .filter((s) => s.length > 0);
52
+
53
+ const ethRelayerRaw = process.env.SHROUDFI_ETH_RELAYER_CONTRACT?.trim();
54
+ const ethRelayerContract: Address | undefined =
55
+ ethRelayerRaw !== undefined && ethRelayerRaw.length > 0
56
+ ? (ethRelayerRaw as Address)
57
+ : undefined;
58
+
59
+ const base = {
60
+ chainId,
61
+ port,
62
+ rateLimitPerMinute,
63
+ allowedOrigins,
64
+ relayerContract: envOrThrow('SHROUDFI_RELAYER_CONTRACT') as Address,
65
+ rpcUrl: envOrThrow('SHROUDFI_RELAYER_RPC_URL'),
66
+ forwarderPrivateKey: envOrThrow(
67
+ 'SHROUDFI_RELAYER_FORWARDER_KEY',
68
+ ) as Hex,
69
+ };
70
+ return ethRelayerContract === undefined
71
+ ? base
72
+ : { ...base, ethRelayerContract };
73
+ }
74
+
75
+ async function main(): Promise<void> {
76
+ const cfg = loadConfig();
77
+ await startServer(cfg);
78
+ // Print a single line so the operator knows the service is up. NO key,
79
+ // NO RPC URL, NO contract address (those are public but we keep the
80
+ // surface minimal — they can be queried via /health).
81
+ // eslint-disable-next-line no-console
82
+ console.log(
83
+ `self-host-relayer listening on http://127.0.0.1:${cfg.port} (chain ${cfg.chainId})`,
84
+ );
85
+ }
86
+
87
+ void main().catch((err: unknown) => {
88
+ // eslint-disable-next-line no-console
89
+ console.error(err instanceof Error ? err.name : 'StartupError');
90
+ process.exit(1);
91
+ });
package/src/errors.ts ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Privacy-safe errors for @shroud-fi/self-host-relayer.
3
+ *
4
+ * Invariants mirrored from other packages:
5
+ * - No key bytes in messages
6
+ * - No amount values in messages
7
+ * - No private signature bytes in messages
8
+ */
9
+
10
+ export class SelfHostRelayerError extends Error {
11
+ override readonly name: string = 'SelfHostRelayerError';
12
+ constructor(message: string, options?: { cause?: unknown }) {
13
+ super(message, options);
14
+ }
15
+ }
16
+
17
+ export class InvalidRelayRequestError extends SelfHostRelayerError {
18
+ override readonly name: string = 'InvalidRelayRequestError';
19
+ constructor() {
20
+ super('Relay request body failed validation');
21
+ }
22
+ }
23
+
24
+ export class RelayBroadcastError extends SelfHostRelayerError {
25
+ override readonly name: string = 'RelayBroadcastError';
26
+ constructor(underlyingName?: string, options?: { cause?: unknown }) {
27
+ super(
28
+ underlyingName !== undefined
29
+ ? `Broadcast failed (${underlyingName})`
30
+ : 'Broadcast failed',
31
+ options,
32
+ );
33
+ }
34
+ }
35
+
36
+ export class RelayConfigError extends SelfHostRelayerError {
37
+ override readonly name: string = 'RelayConfigError';
38
+ constructor(field: string) {
39
+ super(`Relayer config missing required field: ${field}`);
40
+ }
41
+ }
42
+
43
+ export class InvalidRelayEthRequestError extends SelfHostRelayerError {
44
+ override readonly name: string = 'InvalidRelayEthRequestError';
45
+ constructor() {
46
+ super('Relay ETH request body failed validation');
47
+ }
48
+ }
49
+
50
+ export class EthRelayerNotConfiguredError extends SelfHostRelayerError {
51
+ override readonly name: string = 'EthRelayerNotConfiguredError';
52
+ constructor() {
53
+ super('ETH relayer contract address not configured');
54
+ }
55
+ }
56
+
57
+ export class AuthorizationContractMismatchError extends SelfHostRelayerError {
58
+ override readonly name: string = 'AuthorizationContractMismatchError';
59
+ constructor() {
60
+ super('EIP-7702 authorization address does not match configured ETH relayer');
61
+ }
62
+ }
package/src/index.ts ADDED
@@ -0,0 +1,31 @@
1
+ // Public surface — @shroud-fi/self-host-relayer
2
+ //
3
+ // Exports the server builder + the pure relay-logic helpers so the same
4
+ // package can be embedded in another Node process or run standalone via
5
+ // `pnpm dev`.
6
+
7
+ export { buildServer, startServer } from './server.js';
8
+ export { buildRelayCalldata, relayRequest } from './relay.js';
9
+ export type { RelayCtx, RelayResult } from './relay.js';
10
+ export {
11
+ buildEthSweepCalldata,
12
+ relayEthRequest,
13
+ } from './relay-eth.js';
14
+ export type { RelayEthCtx, RelayEthResult } from './relay-eth.js';
15
+ export type {
16
+ RelayRequest,
17
+ RelayResponse,
18
+ RelaySuccess,
19
+ RelayFailure,
20
+ ServiceConfig,
21
+ RelayEthRequest,
22
+ } from './types.js';
23
+ export {
24
+ SelfHostRelayerError,
25
+ InvalidRelayRequestError,
26
+ RelayBroadcastError,
27
+ RelayConfigError,
28
+ InvalidRelayEthRequestError,
29
+ EthRelayerNotConfiguredError,
30
+ AuthorizationContractMismatchError,
31
+ } from './errors.js';
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Production entry: strict env, fail-fast on missing required values, no
3
+ * permissive defaults that could mask misconfiguration in deployment.
4
+ *
5
+ * Required env (no defaults — startup fails if any are missing):
6
+ * SHROUDFI_RELAYER_CONTRACT - deployed ShroudFiRelayer address
7
+ * SHROUDFI_RELAYER_FORWARDER_KEY - 0x-prefixed 32-byte forwarder EOA key
8
+ * SHROUDFI_RELAYER_RPC_URL - chain RPC URL
9
+ * SHROUDFI_RELAYER_ORIGINS - comma-sep allowed origins (e.g. https://app.shroudfi.live)
10
+ * SHROUDFI_RELAYER_CHAIN_ID - chain id (84532 = Base Sepolia, 8453 = Base)
11
+ * SHROUDFI_RELAYER_PORT - bind port (typically 8789 on droplet)
12
+ *
13
+ * Optional env:
14
+ * SHROUDFI_RELAYER_RATE_LIMIT - per-IP requests per minute (default 30)
15
+ * SHROUDFI_ETH_RELAYER_CONTRACT - deployed ShroudFiEthRelayer address.
16
+ * When set, /relay-eth becomes active for
17
+ * gasless ETH sweeps via EIP-7702. When
18
+ * unset, /relay-eth returns 501.
19
+ * DEBUG_DIAGNOSTICS - "1" to enable per-request preflight + sig recovery logs
20
+ *
21
+ * Differences from dev-entry.ts:
22
+ * - Every required field uses envOrThrow; no fallbacks
23
+ * - SHROUDFI_RELAYER_ORIGINS is REQUIRED (dev defaults to localhost; prod must be explicit)
24
+ * - No startup log line printed to stdout — only journald via PM2 captures fastify's
25
+ * own "Server listening at ..." line
26
+ * - DEBUG_DIAGNOSTICS env opts into the per-request debug logs (off by default)
27
+ *
28
+ * Privacy: this file is the ONLY place that touches process.env for the
29
+ * forwarder key. Once read, the key flows into a viem account closure inside
30
+ * the server and never resurfaces.
31
+ */
32
+
33
+ import { startServer } from './server.js';
34
+ import { RelayConfigError } from './errors.js';
35
+ import type { Address, Hex } from 'viem';
36
+ import type { ServiceConfig } from './types.js';
37
+
38
+ function envOrThrow(name: string): string {
39
+ const v = process.env[name];
40
+ if (v === undefined || v.trim().length === 0) {
41
+ throw new RelayConfigError(name);
42
+ }
43
+ return v.trim();
44
+ }
45
+
46
+ function loadConfig(): ServiceConfig {
47
+ const chainId = Number(envOrThrow('SHROUDFI_RELAYER_CHAIN_ID'));
48
+ if (!Number.isFinite(chainId) || chainId <= 0) {
49
+ throw new RelayConfigError('SHROUDFI_RELAYER_CHAIN_ID');
50
+ }
51
+
52
+ const port = Number(envOrThrow('SHROUDFI_RELAYER_PORT'));
53
+ if (!Number.isInteger(port) || port < 1 || port > 65535) {
54
+ throw new RelayConfigError('SHROUDFI_RELAYER_PORT');
55
+ }
56
+
57
+ const rateLimitRaw = process.env.SHROUDFI_RELAYER_RATE_LIMIT?.trim();
58
+ const rateLimitPerMinute =
59
+ rateLimitRaw && rateLimitRaw.length > 0 ? Number(rateLimitRaw) : 30;
60
+ if (!Number.isFinite(rateLimitPerMinute) || rateLimitPerMinute <= 0) {
61
+ throw new RelayConfigError('SHROUDFI_RELAYER_RATE_LIMIT');
62
+ }
63
+
64
+ const originsCsv = envOrThrow('SHROUDFI_RELAYER_ORIGINS');
65
+ const allowedOrigins = originsCsv
66
+ .split(',')
67
+ .map((s) => s.trim())
68
+ .filter((s) => s.length > 0);
69
+ if (allowedOrigins.length === 0) {
70
+ throw new RelayConfigError('SHROUDFI_RELAYER_ORIGINS');
71
+ }
72
+
73
+ const debugDiagnostics = process.env.DEBUG_DIAGNOSTICS === '1';
74
+
75
+ const ethRelayerRaw = process.env.SHROUDFI_ETH_RELAYER_CONTRACT?.trim();
76
+ const ethRelayerContract: Address | undefined =
77
+ ethRelayerRaw !== undefined && ethRelayerRaw.length > 0
78
+ ? (ethRelayerRaw as Address)
79
+ : undefined;
80
+
81
+ const base = {
82
+ chainId,
83
+ port,
84
+ rateLimitPerMinute,
85
+ allowedOrigins,
86
+ relayerContract: envOrThrow('SHROUDFI_RELAYER_CONTRACT') as Address,
87
+ rpcUrl: envOrThrow('SHROUDFI_RELAYER_RPC_URL'),
88
+ forwarderPrivateKey: envOrThrow(
89
+ 'SHROUDFI_RELAYER_FORWARDER_KEY',
90
+ ) as Hex,
91
+ debugDiagnostics,
92
+ };
93
+ return ethRelayerContract === undefined
94
+ ? base
95
+ : { ...base, ethRelayerContract };
96
+ }
97
+
98
+ async function main(): Promise<void> {
99
+ const cfg = loadConfig();
100
+ await startServer(cfg);
101
+ // Intentionally no stdout — Fastify's logger already emits a "Server
102
+ // listening at ..." line that PM2/journald captures. Avoid extra surface.
103
+ }
104
+
105
+ void main().catch((err: unknown) => {
106
+ // Startup failure → emit only the error class name to stderr, then exit 1.
107
+ // PM2 will record this and (per ecosystem config) attempt restart up to
108
+ // max_restarts before giving up.
109
+ // eslint-disable-next-line no-console
110
+ console.error(err instanceof Error ? err.name : 'StartupError');
111
+ process.exit(1);
112
+ });
@@ -0,0 +1,122 @@
1
+ /**
2
+ * ETH gasless sweep relay — broadcasts an EIP-7702 type-0x04 tx that delegates
3
+ * the stealth EOA to ShroudFiEthRelayer and calls sweepETH(destination,
4
+ * deadline, signature).
5
+ *
6
+ * Privacy invariants:
7
+ * - Relayer EOA's private key held in the viem account closure. Never
8
+ * logged or attached to thrown errors.
9
+ * - The EIP-712 signature in the request is a one-time spending
10
+ * authorization bound to (destination, deadline, chainId, stealthEOA).
11
+ * We pass it through verbatim — never store or re-sign.
12
+ * - No amount fields anywhere in the request or response.
13
+ */
14
+
15
+ import {
16
+ encodeFunctionData,
17
+ type Account,
18
+ type Chain,
19
+ type Hex,
20
+ type PublicClient,
21
+ type SignedAuthorization,
22
+ type Transport,
23
+ type WalletClient,
24
+ } from 'viem';
25
+ import { RelayBroadcastError } from './errors.js';
26
+ import type { RelayEthRequest } from './types.js';
27
+
28
+ const SWEEP_ETH_ABI = [
29
+ {
30
+ type: 'function',
31
+ name: 'sweepETH',
32
+ stateMutability: 'nonpayable',
33
+ inputs: [
34
+ { name: 'destination', type: 'address' },
35
+ { name: 'deadline', type: 'uint256' },
36
+ { name: 'signature', type: 'bytes' },
37
+ ],
38
+ outputs: [],
39
+ },
40
+ ] as const;
41
+
42
+ /**
43
+ * Same generic-loose shape as RelayCtx. Holds the broadcast-side viem clients
44
+ * plus the relayer EOA. The relayer EOA pays gas for the outer tx.
45
+ */
46
+ export interface RelayEthCtx {
47
+ readonly publicClient: PublicClient<Transport, Chain | undefined>;
48
+ readonly walletClient: WalletClient<Transport, Chain | undefined, Account>;
49
+ readonly account: Account;
50
+ }
51
+
52
+ export interface RelayEthResult {
53
+ readonly txHash: Hex;
54
+ readonly blockNumber: bigint;
55
+ }
56
+
57
+ /**
58
+ * Reconstruct a viem `SignedAuthorization` from the wire-encoded fields. The
59
+ * fields are exactly what the SDK serialises in @shroud-fi/relayer's
60
+ * eth-sweep.ts — `nonce` arrives as a number, `chainId` as a number, `r`/`s`
61
+ * as 0x-prefixed hex, `yParity` as 0 or 1.
62
+ */
63
+ function reconstructAuthorization(req: RelayEthRequest): SignedAuthorization {
64
+ return {
65
+ address: req.authorization.address,
66
+ chainId: req.authorization.chainId,
67
+ nonce: req.authorization.nonce,
68
+ r: req.authorization.r,
69
+ s: req.authorization.s,
70
+ yParity: req.authorization.yParity,
71
+ };
72
+ }
73
+
74
+ /**
75
+ * Build the inner sweepETH calldata. The OUTER tx is the type-0x04 transaction
76
+ * itself which carries the authorization list and the `to: stealthAddress`;
77
+ * the inner data is what executes once the EOA's code is set to the relayer.
78
+ */
79
+ export function buildEthSweepCalldata(req: RelayEthRequest): Hex {
80
+ return encodeFunctionData({
81
+ abi: SWEEP_ETH_ABI,
82
+ functionName: 'sweepETH',
83
+ args: [req.destination, BigInt(req.deadline), req.signature],
84
+ });
85
+ }
86
+
87
+ /**
88
+ * Submit the type-0x04 tx and wait for receipt.
89
+ *
90
+ * On any viem error we wrap with RelayBroadcastError(name, { cause: err }) so
91
+ * the server can walk the cause chain and log the actual revert reason
92
+ * (public on-chain data) without surfacing it to clients.
93
+ */
94
+ export async function relayEthRequest(
95
+ ctx: RelayEthCtx,
96
+ req: RelayEthRequest,
97
+ ): Promise<RelayEthResult> {
98
+ const data = buildEthSweepCalldata(req);
99
+ const authorization = reconstructAuthorization(req);
100
+
101
+ try {
102
+ const txHash = await ctx.walletClient.sendTransaction({
103
+ account: ctx.account,
104
+ chain: ctx.walletClient.chain ?? null,
105
+ to: req.stealthAddress,
106
+ data,
107
+ value: 0n,
108
+ authorizationList: [authorization],
109
+ });
110
+ const receipt = await ctx.publicClient.waitForTransactionReceipt({
111
+ hash: txHash,
112
+ });
113
+ if (receipt.status !== 'success') {
114
+ throw new RelayBroadcastError('TransactionReverted');
115
+ }
116
+ return { txHash, blockNumber: receipt.blockNumber };
117
+ } catch (err) {
118
+ if (err instanceof RelayBroadcastError) throw err;
119
+ const name = err instanceof Error ? err.name : 'UnknownError';
120
+ throw new RelayBroadcastError(name, { cause: err });
121
+ }
122
+ }
package/src/relay.ts ADDED
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Core relay logic. Pure function — given a validated request + viem clients,
3
+ * encodes the ERC-2771 wrapped calldata and broadcasts via the relayer EOA.
4
+ *
5
+ * Privacy invariants:
6
+ * - The relayer EOA's private key is held in a viem account closure. It is
7
+ * never logged, serialized, or attached to thrown errors.
8
+ * - The stealth address is data, not key material.
9
+ * - The permit (v, r, s, deadline) is a one-time authorization for the
10
+ * specific (owner, spender, value, deadline) bound at signing time.
11
+ */
12
+
13
+ import {
14
+ encodeFunctionData,
15
+ type Account,
16
+ type Address,
17
+ type Chain,
18
+ type Hex,
19
+ type PublicClient,
20
+ type Transport,
21
+ type WalletClient,
22
+ } from 'viem';
23
+ import { RelayBroadcastError } from './errors.js';
24
+ import type { RelayRequest } from './types.js';
25
+
26
+ const SWEEP_ABI = [
27
+ {
28
+ type: 'function',
29
+ name: 'sweepERC20WithPermit',
30
+ stateMutability: 'nonpayable',
31
+ inputs: [
32
+ { name: 'token', type: 'address' },
33
+ { name: 'destination', type: 'address' },
34
+ { name: 'deadline', type: 'uint256' },
35
+ { name: 'v', type: 'uint8' },
36
+ { name: 'r', type: 'bytes32' },
37
+ { name: 's', type: 'bytes32' },
38
+ ],
39
+ outputs: [],
40
+ },
41
+ ] as const;
42
+
43
+ // Generic-loose client types: the relay function only calls a small slice of
44
+ // the viem surface (sendTransaction + waitForTransactionReceipt), so we let
45
+ // callers pass clients keyed to any specific chain (Base, Base Sepolia, etc.)
46
+ // without forcing their chain shape onto our type signature.
47
+ export interface RelayCtx {
48
+ readonly publicClient: PublicClient<Transport, Chain | undefined>;
49
+ readonly walletClient: WalletClient<Transport, Chain | undefined, Account>;
50
+ readonly account: Account;
51
+ readonly relayerContract: Address;
52
+ }
53
+
54
+ export interface RelayResult {
55
+ readonly txHash: Hex;
56
+ readonly blockNumber: bigint;
57
+ }
58
+
59
+ /**
60
+ * Build the ERC-2771-wrapped calldata: encoded sweepERC20WithPermit(...) with
61
+ * the 20-byte stealth address appended. ShroudFiRelayer's ERC2771Context
62
+ * reads _msgSender() as the appended address when msg.sender is the trusted
63
+ * forwarder (= the relayer EOA = `account` here).
64
+ */
65
+ export function buildRelayCalldata(req: RelayRequest): Hex {
66
+ const inner = encodeFunctionData({
67
+ abi: SWEEP_ABI,
68
+ functionName: 'sweepERC20WithPermit',
69
+ args: [
70
+ req.token,
71
+ req.destination,
72
+ BigInt(req.deadline),
73
+ req.v,
74
+ req.r,
75
+ req.s,
76
+ ],
77
+ });
78
+ const suffix = req.stealthAddress.slice(2).toLowerCase();
79
+ // Inner is `0x...`; concatenate stealth bytes (20 = 40 hex chars).
80
+ return (inner + suffix) as Hex;
81
+ }
82
+
83
+ /**
84
+ * Broadcast the wrapped tx. Waits for the receipt before returning.
85
+ *
86
+ * Throws RelayBroadcastError on any underlying viem error. Errors are wrapped
87
+ * to strip out potentially key-bearing payloads from the visible message.
88
+ */
89
+ export async function relayRequest(
90
+ ctx: RelayCtx,
91
+ req: RelayRequest,
92
+ ): Promise<RelayResult> {
93
+ const data = buildRelayCalldata(req);
94
+ try {
95
+ const txHash = await ctx.walletClient.sendTransaction({
96
+ account: ctx.account,
97
+ chain: ctx.walletClient.chain ?? null,
98
+ to: ctx.relayerContract,
99
+ data,
100
+ value: 0n,
101
+ });
102
+ const receipt = await ctx.publicClient.waitForTransactionReceipt({
103
+ hash: txHash,
104
+ });
105
+ return { txHash, blockNumber: receipt.blockNumber };
106
+ } catch (err) {
107
+ const name = err instanceof Error ? err.name : 'UnknownError';
108
+ throw new RelayBroadcastError(name, { cause: err });
109
+ }
110
+ }