diff options
Diffstat (limited to 'public/js')
| -rw-r--r-- | public/js/calculator.coffee | 38 | ||||
| -rw-r--r-- | public/js/setup.coffee | 104 | ||||
| -rw-r--r-- | public/js/wallet.coffee | 79 |
3 files changed, 102 insertions, 119 deletions
diff --git a/public/js/calculator.coffee b/public/js/calculator.coffee index cc2dcece..f709465f 100644 --- a/public/js/calculator.coffee +++ b/public/js/calculator.coffee @@ -11,8 +11,8 @@ #= require js/modsqrt.js #= require js/rfc1751.js -EXCHANGE_FAIL = "Error fetching exchange rate" -SOCKET_FAIL = "Error connecting to payment server" +EXCHANGE_FAIL = "Problem fetching exchange rate" +SOCKET_FAIL = "Problem connecting to payment server, notifications may not appear" ADDRESS_FAIL = "Invalid address" g = exports ? this @@ -59,7 +59,9 @@ setup = -> $('#title').html("<a href='/#{g.user.username}/edit'>#{g.user.title}</a>").show() if g.user.logo - $('#logo').attr('src', g.user.logo).show() + ext = g.user.logo.substr(g.user.logo.length - 3) + src = "/assets/img/logos/#{g.user.username}.#{ext}" + $('#logo').attr('src', src).show() getAddress() @@ -108,25 +110,29 @@ updateTotal = -> new QRCode('qr', text: "bitcoin:#{g.user.address}?amount=#{g.amount_requested.toString()}", width: 280, height: 280) listen = -> - setTimeout(listen, 10000) - - unless g.blockchain and g.blockchain.readyState is 1 + g.attempts++ + if g.blockchain and g.blockchain.readyState is 1 + setTimeout(listen, 12000) + else + fail(SOCKET_FAIL) if g.attempts > 3 g.blockchain = new WebSocket("wss://ws.blockchain.info/inv") g.blockchain.onopen = -> - $('#connection').fadeIn().removeClass('glyphicon-exclamation-sign').addClass('glyphicon-signal') - clear(SOCKET_FAIL) - g.blockchain.send('{"op":"addr_sub", "addr":"' + g.user.address + '"}') + if g.blockchain.readyState is 1 + g.attempts = 0 + clear(SOCKET_FAIL) + g.blockchain.send('{"op":"addr_sub", "addr":"' + g.user.address + '"}') + else + setTimeout(g.blockchain.onopen, 12000) g.blockchain.onerror = -> - $('#connection').addClass('glyphicon-exclamation-sign').removeClass('glyphicon-signal') - g.blockchain = null fail(SOCKET_FAIL) + g.blockchain.close() + delete g.blockchain + listen() g.blockchain.onclose = -> - $('#connection').addClass('glyphicon-exclamation-sign').removeClass('glyphicon-signal') - g.blockchain = null - fail(SOCKET_FAIL) + delete g.blockchain listen() g.blockchain.onmessage = (e) -> @@ -149,7 +155,7 @@ logTransaction = (txid, amount) -> $('#amount').blur() $('#payment').hide() $('#received').fadeIn('slow') - $('#chaching')[0].play() + $('#success')[0].play() g.user.index++ $.post("/#{g.user.username}/transactions", @@ -171,6 +177,8 @@ getAddress = -> master = bitcoin.HDNode.fromBase58(g.user.pubkey) child = master.derive(0).derive(g.user.index) g.user.address = child.pubKey.getAddress().toString() + g.blockchain.close() if g.blockchain + listen() catch fail(ADDRESS_FAIL) diff --git a/public/js/setup.coffee b/public/js/setup.coffee index 465cfb39..01705cdd 100644 --- a/public/js/setup.coffee +++ b/public/js/setup.coffee @@ -56,7 +56,6 @@ $(-> $('#email').val(data.email) $('#title').val(data.title) $('#logo').val(data.logo) - $('#pubkey').val(data.pubkey) $('#address').val(data.address) $("#symbol option[value='#{data.symbol}']").attr('selected', 'selected') $('#commission').val(data.commission) @@ -72,96 +71,6 @@ $(-> $('#unit').append("<option value='#{v}'>#{v}</option>") ) - $('#pubkey').blur(-> - if check_address($(this).val()) - $(this).parent().removeClass('has-error') - else - $(this).parent().addClass('has-error') - ) - - $('#generate').click(-> - key = new Key() - g.pubkey = key.extended_public_key_string() - g.privkey = key.extended_private_key_string() - - $('#modal').modal() - $('#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 - 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: g.enc_privkey, width: 260, height: 260) - - if $(this).val() - $('#status').html('(Encrypted)') - else - $('#status').html('(Unencrypted)') - ) - - $('#confirm').blur(-> - $('#confirm_error').remove() - if $('#password').val() != $('#confirm').val() - $('#password, #confirm').parent().addClass('has-error') - $('#confirm').parent().after('<div id="confirm_error" class="alert alert-danger">Passwords don\'t match</div>') - else - $('#password, #confirm').parent().removeClass('has-error') - ) - - $('#encryption-password, #encryption-confirm').keyup((e) -> check_passwords(e) unless e.keyCode is 9) - $('#encryption-password, #encryption-confirm').blur((e) -> check_passwords(e)) - - check_passwords = (e) -> - if $('#encryption-password').val() != $('#encryption-confirm').val() or $('#encryption-password').val() is '' - $('#encryption-password, #encryption-confirm').parent().addClass('has-error') - $('#modal .alert').removeClass('alert-success').addClass('alert-danger') - else - $('#encryption-password, #encryption-confirm').parent().removeClass('has-error') - $('#modal .alert').removeClass('alert-danger').addClass('alert-success') - - if $('.progress-bar-success').length > 0 - $('#encryption-password').parent().removeClass('has-error') - else - $('#encryption-password').parent().addClass('has-error') - - $('#print').click(-> - $('#key-dialog').printElement(printMode: 'popup') - ) - - $('a[data-toggle="tab"]').on('shown.bs.tab', (e) -> - $('#print').toggle(e.target.attributes.href.value == '#step1') - ) - - $('#close').click(-> - $('#modal .form-control').blur() - if $('#modal .has-error').length > 0 - $('#step1_link').click() - $('#modal .has-error').effect('shake', 500) - else - 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_key = false - $('#encryption-password').focus() - ).on('hidden.bs.modal', -> - if g.update_key - $('#pubkey').val(g.pubkey) - $('#privkey').val(g.enc_privkey) - ) - $('#setup').submit(-> $('#setup .form-control').blur() if $('#setup .has-error').length > 0 @@ -170,16 +79,3 @@ $(-> ) ) -check_address = (address) -> - try - bitcoin.Address.fromBase58Check(address) - return true - catch - return isBip32(address) - -isBip32 = (address) -> - try - new BIP32(address) - return true - catch - return false diff --git a/public/js/wallet.coffee b/public/js/wallet.coffee new file mode 100644 index 00000000..fcbbd062 --- /dev/null +++ b/public/js/wallet.coffee @@ -0,0 +1,79 @@ +#= require ../js/jquery-1.8.2.min.js +#= require ../js/jquery-ui.min.js +#= require ../js/jquery.printElement.min.js +#= require ../js/check_password.js +#= require ../js/bootstrap.min.js +#= require ../js/jsbn.js +#= require ../js/jsbn2.js +#= require ../js/crypto-min.js +#= require ../js/2.5.3-crypto-sha256.js +#= require ../js/sha512.js +#= require ../js/rfc1751.js +#= require ../js/bitcoinjs-min-1.0.2.js +#= require ../js/bitcoinjs-min.js +#= require ../js/modsqrt.js +#= require ../js/bip32.js +#= require ../js/secure_random.js +#= require ../js/qrcode.js +#= require ../js/aes.js +#= require ../js/key.js + +g = exports ? this +g.proceed = false + +$(-> + api = 'https://api.blockcypher.com/v1/btc/main' + addresses = [] + balance = 0 + token = '' + $('#balance, #amount').hide() + $.get("/token", (data) -> + token = data + $.get("#{api}/wallets/hd/yummy/addresses?token=#{token}", (data) -> + for c in data.chains + for a in c.chain_addresses + addresses.push(a.address) + + chunks = [] + for i in [0 .. addresses.length] by 10 + chunks.push(addresses.slice(i,i+10)) + + doSetTimeout = (c,i) -> + setTimeout(-> + $.get("#{api}/addrs/#{c.join(';')}/balance?token=#{token}", (addresses) -> + for address in addresses + balance = parseInt(balance) + parseInt(address.final_balance) + val = (balance / 100000000).toFixed(8) + + $('#balance').html(val) + $('#amount').val(val) + $('#balance, #amount').fadeIn() + ) + , i*1500) + + for c, i in chunks + doSetTimeout(c, i) + ) + ) + + user = $('#username').val() + $.getJSON("/#{user}.json", (data) -> + $('#pubkey').val(data.pubkey) + $('#address').val(data.address) + $('#setup').fadeIn() + ) +) + +check_address = (address) -> + try + bitcoin.Address.fromBase58Check(address) + return true + catch + return isBip32(address) + +isBip32 = (address) -> + try + new BIP32(address) + return true + catch + return false |
