|
|
@ -1,10 +1,7 @@ |
|
|
|
import express from 'express' |
|
|
|
|
|
|
|
import cors from 'cors' |
|
|
|
import cors from 'cors' |
|
|
|
import bodyParser from 'body-parser' |
|
|
|
import bodyParser from 'body-parser' |
|
|
|
import expressWs from 'express-ws' |
|
|
|
|
|
|
|
import { Provider, ProviderOptions } from './provider' |
|
|
|
import { Provider, ProviderOptions } from './provider' |
|
|
|
import { log } from './utils/logs' |
|
|
|
import { log, error } from './utils/logs' |
|
|
|
const app = express() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type CliOptions = { |
|
|
|
export type CliOptions = { |
|
|
|
rpc?: boolean, |
|
|
|
rpc?: boolean, |
|
|
@ -26,7 +23,10 @@ export class Server { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
start (cliOptions: CliOptions) { |
|
|
|
async start (cliOptions: CliOptions) { |
|
|
|
|
|
|
|
const expressWs = (await import('express-ws')).default |
|
|
|
|
|
|
|
const express = (await import('express')).default |
|
|
|
|
|
|
|
const app = express() |
|
|
|
const wsApp = expressWs(app) |
|
|
|
const wsApp = expressWs(app) |
|
|
|
|
|
|
|
|
|
|
|
app.use(cors()) |
|
|
|
app.use(cors()) |
|
|
@ -40,16 +40,16 @@ export class Server { |
|
|
|
if (cliOptions.rpc) { |
|
|
|
if (cliOptions.rpc) { |
|
|
|
app.use((req, res) => { |
|
|
|
app.use((req, res) => { |
|
|
|
if (req && req.body && (req.body.method === 'eth_sendTransaction' || req.body.method === 'eth_call')) { |
|
|
|
if (req && req.body && (req.body.method === 'eth_sendTransaction' || req.body.method === 'eth_call')) { |
|
|
|
console.log('Receiving call/transaction:') |
|
|
|
log('Receiving call/transaction:') |
|
|
|
console.log(req.body.params) |
|
|
|
log(req.body.params) |
|
|
|
} |
|
|
|
} |
|
|
|
this.provider.sendAsync(req.body, (err, jsonResponse) => { |
|
|
|
this.provider.sendAsync(req.body, (err, jsonResponse) => { |
|
|
|
if (err) { |
|
|
|
if (err) { |
|
|
|
console.error(err) |
|
|
|
error(err) |
|
|
|
return res.send(JSON.stringify({ error: err })) |
|
|
|
return res.send(JSON.stringify({ error: err })) |
|
|
|
} |
|
|
|
} |
|
|
|
if (req && req.body && (req.body.method === 'eth_sendTransaction' || req.body.method === 'eth_call')) { |
|
|
|
if (req && req.body && (req.body.method === 'eth_sendTransaction' || req.body.method === 'eth_call')) { |
|
|
|
console.log(jsonResponse) |
|
|
|
log(jsonResponse) |
|
|
|
} |
|
|
|
} |
|
|
|
res.send(jsonResponse) |
|
|
|
res.send(jsonResponse) |
|
|
|
}) |
|
|
|
}) |
|
|
@ -59,16 +59,16 @@ export class Server { |
|
|
|
ws.on('message', (msg) => { |
|
|
|
ws.on('message', (msg) => { |
|
|
|
const body = JSON.parse(msg.toString()) |
|
|
|
const body = JSON.parse(msg.toString()) |
|
|
|
if (body && (body.method === 'eth_sendTransaction' || body.method === 'eth_call')) { |
|
|
|
if (body && (body.method === 'eth_sendTransaction' || body.method === 'eth_call')) { |
|
|
|
console.log('Receiving call/transaction:') |
|
|
|
log('Receiving call/transaction:') |
|
|
|
console.log(body.params) |
|
|
|
log(body.params) |
|
|
|
} |
|
|
|
} |
|
|
|
this.provider.sendAsync(body, (err, jsonResponse) => { |
|
|
|
this.provider.sendAsync(body, (err, jsonResponse) => { |
|
|
|
if (err) { |
|
|
|
if (err) { |
|
|
|
console.error(err) |
|
|
|
error(err) |
|
|
|
return ws.send(JSON.stringify({ error: err })) |
|
|
|
return ws.send(JSON.stringify({ error: err })) |
|
|
|
} |
|
|
|
} |
|
|
|
if (body && (body.method === 'eth_sendTransaction' || body.method === 'eth_call')) { |
|
|
|
if (body && (body.method === 'eth_sendTransaction' || body.method === 'eth_call')) { |
|
|
|
console.log(jsonResponse) |
|
|
|
log(jsonResponse) |
|
|
|
} |
|
|
|
} |
|
|
|
ws.send(JSON.stringify(jsonResponse)) |
|
|
|
ws.send(JSON.stringify(jsonResponse)) |
|
|
|
}) |
|
|
|
}) |
|
|
|