-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.coffee
executable file
·250 lines (156 loc) · 5.92 KB
/
app.coffee
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
###
Module dependencies.
###
express = require("express")
http = require("http")
path = require("path")
crypto = require 'crypto'
connect = require('connect')
app = express()
# import configuration options
require('./config')(app)
dogecoin = (require 'node-dogecoin')()
dogecoin.auth(app.get('dogecoin_username'),app.get('dogecoin_password')).set('host', 'localhost').set({port:22555})
app.set 'name', 'dogelet'
app.set 'admin_email', "[email protected]"
app.set 'email_name', 'DogeLet'
require('implied-mail').sendgrid(app)
mailer = app.get 'mailer'
# all environments
app.set "port", process.env.PORT or 3000
app.set "views", __dirname + "/views"
app.set "view engine", "jade"
SALT = app.get('salt')
app.use(express.cookieParser())
app.use(connect.session({ secret: app.get('secret') }))
#RedisStore = require('connect-redis')(connect)
#app.use(connect.session({ store: new RedisStore({port:6379,host:'localhost'}), secret: app.get('secret') }))
app.use express.favicon()
app.use express.logger("dev")
app.use express.bodyParser()
app.use express.methodOverride()
# Middleware to make request available to templates.
app.use (req,res,next)->
res.locals.req = res.locals.request = req
next()
app.use app.router
app.use express.static(path.join(__dirname, "public"))
# development only
app.use express.errorHandler() if "development" is app.get("env")
EMAIL_REGEX = ///
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
///
get_email_hash = (email, callback)->
unless EMAIL_REGEX.test email
throw 'Invalid Email'
hash = crypto.createHmac('sha224', SALT).update(email).digest('hex');
security_error = (res, msg)->
res.render 'message',
message: msg
login_required = (req, res, next)->
if req.session.email
if req.session.address
next()
else
res.send 'Your account seems broken.'
else
res.send 'Login first.'
app.get '/', (req, res)->
email = req.session.email
if email and req.session.address # If we're logged in properly...
dogecoin.getBalance email, (err, result)->
if err
return security_error res, ''+err
# For some reason empty accounts have a composite object as balance.
unless typeof result is 'number'
result = result.result
res.render 'index',
balance: result
address: req.session.address
else
res.render 'index',
email: null
app.get '/signup', (req,res)->
email = req.query.email
hash = get_email_hash email
unless EMAIL_REGEX.test email
return security_error res, 'Invalid email address.'
dogecoin.getAddressesByAccount email, (err, result)->
if err
return security_error res, ''+err
if result.length
mailer.send_mail
to: email
subject: "Your DogeLet (Dogecoin wallet)"
body:"""
Here's your secure DogeLet link:
http://#{app.get('host')}/login?email=#{email}&hash=#{hash}
WARNING: Never share this with anyone, as they will be able to use your wallet!
"""
, (success, message)->
unless success
return security_error res, message
return security_error res, 'You already have a wallet. Check your email (search inbox for dogelet.com)'
else
dogecoin.getNewAddress email, (err, result)->
if err
return security_error res, err
mailer.send_mail
to: email
subject: "Your New DogeLet (Dogecoin wallet)"
body:"""
Here's your secure DogeLet link:
http://#{app.get('host')}/login?email=#{email}&hash=#{hash}
WARNING: Never share this with anyone, as they will be able to use your wallet!
"""
, (success, message)->
unless success
return security_error res, message
return security_error res, 'Your new wallet has been created! Check your email (search inbox for dogelet.com)'
app.get '/login', (req,res)->
unless EMAIL_REGEX.test req.query.email
return security_error res, 'Invalid email address.'
if get_email_hash(req.query.email) is req.query.hash # hash is good.
req.session.email = req.query.email
dogecoin.getAddressesByAccount req.query.email, (err, result)->
if result.length isnt 1
return security_error res, "Duplicate address."
req.session.address = result[0]
res.redirect '/'
else
req.session.email = null
req.session.address = null
security_error res, 'Bad credentials.'
app.get '/send', login_required, (req,res)->
email = req.query.email
if email isnt req.session.email
return security_error res, 'Sender email does not match account email.'
unless /[0-9]*\.?[0-9]/.test req.query.amount
return security_error res, 'Badly formed amount.'
unless /[a-zA-Z\d]+/.test req.query.send_to
return security_error res, 'Badly formed recipient address'
dogecoin.validateAddress req.query.send_to, (err, result)->
if err
return security_error '' + err
if result.isvalid
dogecoin.sendFrom email, req.query.send_to, parseFloat(req.query.amount), 1, ->
res.send arguments
app.get '/history', login_required, (req,res)->
page = parseInt(req.query.page or '0')
dogecoin.listTransactions req.session.email, 11, page * 0, (err, transactions)->
if err
return security_error '' + err
res.render 'history',
transactions: transactions
page: page
app.get '/history/:tid', login_required, (req,res)->
dogecoin.gettransaction req.params.tid, (err, transaction)->
if err
return security_error '' + err
res.send transaction
app.get '/logout', (req,res)->
req.session.email = null
req.session.address = null
res.redirect '/'
http.createServer(app).listen app.get("port"), ->
console.log "Express server listening on port " + app.get("port")