forked from zeropoolnetwork/zeropool-relayer
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
334 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import express from 'express' | ||
import { logger } from '../services/appLogger' | ||
import { createRouter } from './router' | ||
import { init } from './init' | ||
|
||
const app = express() | ||
|
||
init().then(({ pool, signer }) => { | ||
app.use(createRouter({ pool, signer })) | ||
const PORT = 8080 | ||
app.listen(PORT, () => logger.info(`Started guard on port ${PORT}`)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import relayerConfig from '@/configs/relayerConfig' | ||
import config from '@/configs/relayerConfig' | ||
import { Pool } from '@/pool' | ||
import { EvmBackend, Network, NetworkBackend, TronBackend } from '@/services/network' | ||
import { Wallet } from 'ethers' | ||
|
||
export async function init() { | ||
let networkBackend: NetworkBackend<Network> | ||
const baseConfig = { | ||
poolAddress: config.COMMON_POOL_ADDRESS, | ||
tokenAddress: config.RELAYER_TOKEN_ADDRESS, | ||
pk: config.RELAYER_ADDRESS_PRIVATE_KEY, | ||
rpcUrls: config.COMMON_RPC_URL, | ||
requireHTTPS: config.COMMON_REQUIRE_RPC_HTTPS, | ||
} | ||
if (config.RELAYER_NETWORK === Network.Ethereum) { | ||
networkBackend = new EvmBackend({ | ||
...baseConfig, | ||
rpcRequestTimeout: config.COMMON_RPC_REQUEST_TIMEOUT, | ||
rpcSyncCheckInterval: config.COMMON_RPC_SYNC_STATE_CHECK_INTERVAL, | ||
jsonRpcErrorCodes: config.COMMON_JSONRPC_ERROR_CODES, | ||
relayerTxRedundancy: config.RELAYER_TX_REDUNDANCY, | ||
}) | ||
} else if (config.RELAYER_NETWORK === Network.Tron) { | ||
networkBackend = new TronBackend({ | ||
...baseConfig, | ||
}) | ||
} else { | ||
throw new Error('Unsupported network backend') | ||
} | ||
await networkBackend.init() | ||
|
||
const pool = new Pool(networkBackend) | ||
await pool.init(false) | ||
|
||
const signer = new Wallet(relayerConfig.RELAYER_ADDRESS_PRIVATE_KEY) // TODO: config pk | ||
|
||
return { pool, signer } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import cors from 'cors' | ||
import express, { NextFunction, Request, Response } from 'express' | ||
import { checkSignMPCSchema, validateBatch } from '../validation/api/validation' | ||
import { logger } from '../services/appLogger' | ||
import { validateTxMPC } from '../validation/tx/validateTx' | ||
import type { Pool } from '@/pool' | ||
import { TxData, buildTxData } from '@/txProcessor' | ||
import { Signer } from 'ethers' | ||
import { truncateHexPrefix } from '@/utils/helpers' | ||
import { VK } from 'libzkbob-rs-node' | ||
|
||
function wrapErr(f: (_req: Request, _res: Response, _next: NextFunction) => Promise<void> | void) { | ||
return async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
await f(req, res, next) | ||
} catch (e) { | ||
next(e) | ||
} | ||
} | ||
} | ||
|
||
export function createRouter({ pool, signer }: { pool: Pool; signer: Signer }) { | ||
const router = express.Router() | ||
|
||
router.use(cors()) | ||
router.use(express.urlencoded({ extended: true })) | ||
router.use(express.json()) | ||
router.use(express.text()) | ||
|
||
router.use((err: any, _req: Request, res: Response, next: NextFunction) => { | ||
if (err) { | ||
logger.error('Request error:', err) | ||
return res.sendStatus(500) | ||
} | ||
next() | ||
}) | ||
|
||
router.post( | ||
'/sign', | ||
wrapErr(async (req: Request, res: Response) => { | ||
validateBatch([[checkSignMPCSchema, req.body]]) | ||
const message = req.body as TxData | ||
|
||
// Validate | ||
const vk: VK = require('../params/tree_verification_key.json') // TODO: config vk | ||
try { | ||
await validateTxMPC(message, pool, vk) | ||
} catch (e) { | ||
console.log('Validation error', e) | ||
throw new Error('Invalid transaction') | ||
} | ||
|
||
// Sign | ||
const calldata = truncateHexPrefix(buildTxData(message)) | ||
const signature = await signer.signMessage(calldata) | ||
|
||
res.json({ signature }) | ||
}) | ||
) | ||
|
||
return router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.