-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
126 lines (102 loc) · 3.11 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const request = require('request')
const express = require('express')
const Browserify = require('browserify')
const bodyParser = require('body-parser')
const cors = require('cors')
const ethUtil = require('ethereumjs-util')
const Web3 = require('web3')
const rpcWrapperEngine = require('./index.js')
const pageCode = require('fs').readFileSync('./index.html', 'utf-8')
const PORT = process.env.PORT || 9000
const PRIVATE_KEY = process.env.PRIVATE_KEY || '0x693148ab1226b1c6536bcf240079bcb36a12cd1c8e4f42468903c734d22718be'
if (!PRIVATE_KEY) throw new Error('Env var PRIVATE_KEY not specified.')
const RPC_NODE = process.env.RPC_NODE || 'https://rawtestrpc.metamask.io/'
if (!RPC_NODE) throw new Error('Env var RPC_NODE not specified.')
// calculate faucet address
var faucetKey = ethUtil.toBuffer(PRIVATE_KEY)
var faucetAddress = ethUtil.privateToAddress(faucetKey)
var faucetAddressHex = '0x'+faucetAddress.toString('hex')
var ether = 1e18
var weiValue = 1.337*ether
console.log('Acting as faucet for address:', faucetAddressHex)
//
// create engine
//
// ProviderEngine based caching layer, with fallback to geth
var engine = rpcWrapperEngine({
rpcUrl: RPC_NODE,
addressHex: faucetAddressHex,
privateKey: faucetKey,
})
var web3 = new Web3(engine)
var browserify = Browserify()
browserify.add('./app.js')
browserify.bundle(function(err, bundle){
if (err) throw err
var appCode = bundle.toString()
startServer(appCode)
})
//
// create webserver
//
function startServer(appCode) {
const app = express()
app.use(cors())
app.use(bodyParser.text({ type: '*/*' }))
// serve app
app.get('/', deliverPage)
app.get('/index.html', deliverPage)
app.get('/app.js', deliverApp)
// send ether
app.post('/', function(req, res){
// address: 18a3462427bcc9133bb46e88bcbe39cd7ef0e761
// priv: 693148ab1226b1c6536bcf240079bcb36a12cd1c8e4f42468903c734d22718be
// parse request
var targetAddress = req.body
if (targetAddress.slice(0,2) !== '0x') {
targetAddress = '0x'+targetAddress
}
if (targetAddress.length != 42) {
return didError(new Error('Address parse failure - '+targetAddress))
}
web3.eth.sendTransaction({
to: targetAddress,
from: faucetAddressHex,
value: weiValue,
}, function(err, result){
if (err) {
didError(err)
} else {
res.send(result)
}
})
function didError(err){
console.error(err.stack)
res.status(500).json({ error: err.message })
}
function invalidRequest(){
res.status(400).json({ error: 'Not a valid request.' })
}
})
app.listen(PORT, function(){
console.log('ethereum rpc listening on', PORT)
console.log('and proxying to', RPC_NODE)
})
function deliverPage(req, res){
res.status(200).send(pageCode)
}
function deliverApp(req, res){
res.status(200).send(appCode)
}
}
// example request format
//
// {
// "jsonrpc": "2.0",
// "method": "eth_getBalance",
// "params": ["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"],
// "id":1
// }
function validateRequest( requestObject ){
return typeof requestObject === 'object' && !!requestObject.method
}