From 0765adfc8993e8641e61a8db40965ef091c0f451 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 15 Sep 2014 10:09:03 -0700 Subject: Mail key --- public/js/calculator.coffee | 1 + public/js/setup.coffee | 42 ++++++++++++++++++----------------- public/js/webqr.js | 2 +- routes/users.js | 38 ++++++++++++++++++++++++++----- src/routes/users.coffee | 30 ++++++++++++++++++++++--- util/migrate_address_to_pubkey.coffee | 13 +++++++++++ views/calculator/sweep.html | 1 - views/users/edit.html | 35 +++++++++++++++++------------ views/users/key.html | 7 ++++++ views/users/welcome.html | 10 +++++++-- 10 files changed, 133 insertions(+), 46 deletions(-) create mode 100644 util/migrate_address_to_pubkey.coffee create mode 100644 views/users/key.html diff --git a/public/js/calculator.coffee b/public/js/calculator.coffee index 14a25e6c..2032b9c3 100644 --- a/public/js/calculator.coffee +++ b/public/js/calculator.coffee @@ -52,6 +52,7 @@ setup = -> g.tip = 1 g.user.address or= '' g.user.commission or= 0 + g.user.currency or= 'CAD' g.user.symbol or= 'quadrigacx' g.user.unit or= 'BTC' diff --git a/public/js/setup.coffee b/public/js/setup.coffee index deffcded..465cfb39 100644 --- a/public/js/setup.coffee +++ b/public/js/setup.coffee @@ -22,6 +22,8 @@ g = exports ? this g.proceed = false $(-> + $('#encryption-password').pwstrength(showVerdicts: false) + $.getJSON("/js/rates.json", (data) -> currencies = Object.keys(data) currencies = currencies.sort() @@ -51,6 +53,7 @@ $(-> user = $('#username').val() $.getJSON("/#{user}.json", (data) -> + $('#email').val(data.email) $('#title').val(data.title) $('#logo').val(data.logo) $('#pubkey').val(data.pubkey) @@ -69,13 +72,6 @@ $(-> $('#unit').append("") ) - $('#title').blur(-> - if $(this).val() == '' and $('#logo').val() == '' - $(this).parent().addClass('has-error') - else - $(this).parent().removeClass('has-error') - ) - $('#pubkey').blur(-> if check_address($(this).val()) $(this).parent().removeClass('has-error') @@ -89,25 +85,23 @@ $(-> g.privkey = key.extended_private_key_string() $('#modal').modal() - $('#privkey').html(g.privkey) + $('#privkey_text').html(g.privkey) $('#qr').html('') new QRCode('qr', text: g.privkey, width: 260, height: 260) ) $('#encryption-password').keyup((e) -> return if e.keyCode is 9 or e.keyCode is 16 - enc_privkey = CryptoJS.AES.encrypt(g.privkey, $(this).val()).toString() - enc_privkey = g.privkey if $(this).val() == '' - $('#privkey').html(enc_privkey) + g.enc_privkey = CryptoJS.AES.encrypt(g.privkey, $(this).val()).toString() + g.enc_privkey = g.privkey if $(this).val() == '' + $('#privkey_text').html(g.enc_privkey) $('#qr').html('') - new QRCode('qr', text: enc_privkey, width: 260, height: 260) + new QRCode('qr', text: g.enc_privkey, width: 260, height: 260) if $(this).val() $('#status').html('(Encrypted)') else - $('#status').html('(Unencrypted Plaintext)') - - + $('#status').html('(Unencrypted)') ) $('#confirm').blur(-> @@ -146,18 +140,26 @@ $(-> $('#close').click(-> $('#modal .form-control').blur() if $('#modal .has-error').length > 0 + $('#step1_link').click() $('#modal .has-error').effect('shake', 500) else - g.update_pub_key = true + g.update_key = true $('#modal').modal('hide') ) + $('#encrypt').submit(-> $('#close').click(); return false) + + $('#modal').on('show.bs.modal', -> + $('#encryption-password, #encryption-confirm').val('').keyup().parent().removeClass('has-error') + $('#modal .alert').removeClass('alert-success').addClass('alert-danger') + ) $('#modal').on('shown.bs.modal', -> - g.update_pub_key = false - $('#encryption-password, #encryption-confirm').val('').blur() - $('#encryption-password').pwstrength(showVerdicts: false) + g.update_key = false + $('#encryption-password').focus() ).on('hidden.bs.modal', -> - $('#pubkey').val(g.pubkey) if g.update_pub_key + if g.update_key + $('#pubkey').val(g.pubkey) + $('#privkey').val(g.enc_privkey) ) $('#setup').submit(-> diff --git a/public/js/webqr.js b/public/js/webqr.js index ad80065d..18d4de4e 100644 --- a/public/js/webqr.js +++ b/public/js/webqr.js @@ -10,7 +10,7 @@ var webkit=false; var moz=false; var v=null; -var vidhtml = ''; +var vidhtml = ''; $(function() { load(); diff --git a/routes/users.js b/routes/users.js index 47562df4..df89eb5b 100644 --- a/routes/users.js +++ b/routes/users.js @@ -114,14 +114,19 @@ }); }); return require('crypto').randomBytes(48, function(ex, buf) { - var token, url; + var host, token, url; token = buf.toString('base64').replace(/\//g, '').replace(/\+/g, ''); - db.set(token, req.body.username); + db.set("token:" + token, req.body.username); + host = req.hostname; + if (host === 'localhost') { + host += ':3000'; + } url = "" + req.protocol + "://" + req.hostname + ":3000/verify/" + token; return res.render('users/welcome', { user: req.params.user, layout: 'mail', url: url, + privkey: req.body.privkey, js: (function() { return global.js; }), @@ -172,7 +177,7 @@ if (req.body.password === '') { delete req.body.password; } - return db.hmset("user:" + req.params.user, req.body, function() { + db.hmset("user:" + req.params.user, req.body, function() { if (req.body.password != null) { return bcrypt.hash(req.body.password, 12, function(err, hash) { return db.hmset("user:" + req.params.user, { @@ -185,11 +190,34 @@ return res.redirect("/" + req.params.user); } }); + if ((req.body.privkey != null) && req.body.privkey !== '' && req.body.email !== '') { + return res.render('users/key', { + user: req.params.user, + layout: 'mail', + key: req.body.privkey, + js: (function() { + return global.js; + }), + css: (function() { + return global.css; + }) + }, function(err, html) { + var email, sendgrid; + sendgrid = require('sendgrid')(config.sendgrid_user, config.sendgrid_password); + email = new sendgrid.Email({ + to: req.body.email, + from: 'adam@coinos.io', + subject: 'CoinOS Wallet Key', + html: html + }); + return sendgrid.send(email); + }); + } }, verify: function(req, res) { - return db.get(req.params.token, function(err, reply) { + return db.get("token:" + req.params.token, function(err, reply) { if (err || !reply) { - res.write('Invalid token'); + res.write("Invalid Verification Token"); return res.end(); } else { return db.hset("user:" + (reply.toString()), "verified", "true", function() { diff --git a/src/routes/users.coffee b/src/routes/users.coffee index dc22303d..e5a26af2 100644 --- a/src/routes/users.coffee +++ b/src/routes/users.coffee @@ -85,13 +85,16 @@ module.exports = (sessions) -> require('crypto').randomBytes(48, (ex, buf) -> token = buf.toString('base64').replace(/\//g,'').replace(/\+/g,'') - db.set(token, req.body.username) + db.set("token:#{token}", req.body.username) + host = req.hostname + host += ':3000' if host is 'localhost' url = "#{req.protocol}://#{req.hostname}:3000/verify/#{token}" res.render('users/welcome', user: req.params.user, layout: 'mail', url: url, + privkey: req.body.privkey, js: (-> global.js), css: (-> global.css), (err, html) -> @@ -130,6 +133,7 @@ module.exports = (sessions) -> update: (req, res) -> if req.body.password is '' delete req.body.password + db.hmset("user:"+req.params.user, req.body, -> if req.body.password? bcrypt.hash(req.body.password, 12, (err, hash) -> @@ -141,10 +145,30 @@ module.exports = (sessions) -> res.redirect("/#{req.params.user}") ) + if req.body.privkey? and req.body.privkey != '' and req.body.email != '' + res.render('users/key', + user: req.params.user, + layout: 'mail', + key: req.body.privkey, + js: (-> global.js), + css: (-> global.css), + (err, html) -> + sendgrid = require('sendgrid')(config.sendgrid_user, config.sendgrid_password) + + email = new sendgrid.Email( + to: req.body.email + from: 'adam@coinos.io' + subject: 'CoinOS Wallet Key' + html: html + ) + + sendgrid.send(email) + ) + verify: (req, res) -> - db.get(req.params.token, (err, reply) -> + db.get("token:#{req.params.token}", (err, reply) -> if err or !reply - res.write('Invalid token') + res.write("Invalid Verification Token") res.end() else db.hset("user:#{reply.toString()}", "verified", "true", -> diff --git a/util/migrate_address_to_pubkey.coffee b/util/migrate_address_to_pubkey.coffee new file mode 100644 index 00000000..8c2c43ac --- /dev/null +++ b/util/migrate_address_to_pubkey.coffee @@ -0,0 +1,13 @@ +db = require('../redis') + +db.keys('user:*', (err, obj) -> + for user, i in obj + do (user) -> + db.hgetall(user, (err, obj) -> + console.log('Updating ' + user.toString()) + if obj.bip32? + db.hset(user, 'pubkey', obj.bip32, ->) + else + db.hset(user, 'pubkey', obj.address, ->) + ) +) diff --git a/views/calculator/sweep.html b/views/calculator/sweep.html index 4a0ee3ee..92be828c 100644 --- a/views/calculator/sweep.html +++ b/views/calculator/sweep.html @@ -2,7 +2,6 @@ - {{/yield-scripts}} diff --git a/views/users/edit.html b/views/users/edit.html index 66251923..0e0af3df 100644 --- a/views/users/edit.html +++ b/views/users/edit.html @@ -6,6 +6,7 @@

Settings

+
@@ -18,9 +19,10 @@
- +
+ @@ -63,32 +65,37 @@