diff options
| author | Adam Soltys <[email protected]> | 2015-12-11 11:28:18 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2015-12-11 11:29:30 -0800 |
| commit | f466c6aa4db08d953df9916fad8ba5d03020e0a0 (patch) | |
| tree | 14c139e53929cae7c7e39f5ad298780716be5c9a /public | |
| parent | 208311023aa6eb433d5e1201eafcd53fab0e6dbd (diff) | |
| download | coinos-server-f466c6aa4db08d953df9916fad8ba5d03020e0a0.tar.gz coinos-server-f466c6aa4db08d953df9916fad8ba5d03020e0a0.zip | |
Proxy blockcypher API requests
Diffstat (limited to 'public')
| -rw-r--r-- | public/js/wallet.coffee | 43 | ||||
| -rw-r--r-- | public/js/wallet.js | 47 |
2 files changed, 42 insertions, 48 deletions
diff --git a/public/js/wallet.coffee b/public/js/wallet.coffee index ebe6826a..cfaac22b 100644 --- a/public/js/wallet.coffee +++ b/public/js/wallet.coffee @@ -1,15 +1,17 @@ g = exports ? this g.proceed = false -g.api = 'https://api.blockcypher.com/v1/btc/main' +g.api = '/blockcypher/v1/btc/main' validators = address: (e) -> + return true if e.val() is '' try bitcoin.address.fromBase58Check(e.val()) catch return false password: (e) -> + return true if e.val() is '' g.master = null try g.master = bitcoin.HDNode.fromBase58(CryptoJS.AES.decrypt($('#privkey').val(), e.val()).toString(CryptoJS.enc.Utf8)) @@ -17,13 +19,13 @@ validators = catch return false - errors = - address: 'Invalid address' - password: 'Wrong password' + address: 'Invalid address.' + password: 'Wrong password.' $(-> - getToken() + getUser() + $('form').validator(custom: validators, errors: errors) $('button[data-toggle=tooltip]').tooltip(trigger: 'hover') @@ -39,7 +41,7 @@ $(-> $.post("/#{g.user.username}", $('#keys form').serializeObject(), -> $.ajax( - url: "#{g.api}/wallets/hd/#{g.user.username}?token=#{g.token}" + url: "#{g.api}/wallets/hd/#{g.user.username}" type: 'DELETE' ).done(-> setTimeout(-> @@ -48,8 +50,8 @@ $(-> extended_public_key: $('#pubkey').val() subchain_indexes: [0,1] - $.post("#{g.api}/wallets/hd?token=#{g.token}", JSON.stringify(data)).always(-> - $.post("#{g.api}/wallets/hd/#{g.user.username}/addresses/derive?token=#{g.token}").always(-> + $.post("#{g.api}/wallets/hd", JSON.stringify(data)).always(-> + $.post("#{g.api}/wallets/hd/#{g.user.username}/addresses/derive").always(-> getBalance() ) ) @@ -79,11 +81,11 @@ $(-> $('#max').click(-> if $('#currency_toggle').html() is g.user.unit - $('#amount').val(g.balance) + $('#amount').val(g.balance).blur() else g.amount = parseFloat(g.balance).toFixed(precision()) amount = (g.balance / multiplier() * g.exchange).toFixed(2) - $('#amount').val(amount) + $('#amount').val(amount).blur() ) $('#amount').change(-> @@ -141,13 +143,6 @@ $(-> $('.close').on('click', -> $(this).closest('.alert').fadeOut()) ) - -getToken = -> - $.get("/token", (token) -> - g.token = token - getUser() - ) - getUser = -> $.getJSON("/#{$('#username').val()}.json", (user) -> g.user = user @@ -168,7 +163,7 @@ getExchangeRate = -> ) createWallet = -> - $.get("#{g.api}/wallets?token=#{g.token}", (data) -> + $.get("#{g.api}/wallets", (data) -> if g.user.username in data.wallet_names getBalance() else @@ -178,7 +173,7 @@ createWallet = -> extended_public_key: g.user.pubkey subchain_indexes: [0,1] - $.post("#{g.api}/wallets/hd?token=#{g.token}", JSON.stringify(data)).always(getBalance) + $.post("#{g.api}/wallets/hd", JSON.stringify(data)).always(getBalance) else # TODO implement this properly $('#balance').html(99) @@ -187,7 +182,7 @@ createWallet = -> getBalance = -> - $.get("#{g.api}/addrs/#{g.user.username}/balance?token=#{g.token}&omitWalletAddresses=true", (data) -> + $.get("#{g.api}/addrs/#{g.user.username}/balance?omitWalletAddresses=true", (data) -> balance = data.final_balance g.balance = balance.toBTC() fiat = balance.toFiat() @@ -227,7 +222,7 @@ sendTransaction = -> outputs: [{addresses: [$('#recipient').val()], value: 1}] preference: $('#priority').val() - $.post("#{g.api}/txs/new?token=#{g.token}", JSON.stringify(params)).done((data) -> + $.post("#{g.api}/txs/new", JSON.stringify(params)).done((data) -> if $('#currency_toggle').html() is g.user.unit amount = $('#amount').val() else @@ -240,7 +235,7 @@ sendTransaction = -> if value > parseFloat(g.balance).toSatoshis() - params.fees params.outputs[0].value -= params.fees - $.post("#{g.api}/txs/new?token=#{g.token}", JSON.stringify(params)).done((data) -> + $.post("#{g.api}/txs/new", JSON.stringify(params)).done((data) -> data.pubkeys = [] data.signatures = data.tosign.map((tosign, i) -> path = data.tx.inputs[i].hd_path.split('/') @@ -265,14 +260,14 @@ sendTransaction = -> dialog.getModalBody().html($('.dialog').html()) dialog.getModal().find('.btn-primary').click(-> - $.post("#{g.api}/txs/send?token=#{g.token}", JSON.stringify(g.data)).then((finaltx) -> + $.post("#{g.api}/txs/send", JSON.stringify(g.data)).then((finaltx) -> $('#transaction_sent').fadeIn() balance = g.balance.toSatoshis() - finaltx.tx.outputs[0].value - finaltx.tx.fees g.balance = balance.toBTC() fiat = balance.toFiat() $('#balance').html(g.balance) $('#fiat').html("#{fiat} #{g.user.currency}") - $('#blockchain').off('click').on('click', -> window.open('https://live.blockcypher.com/btc/main/' + finaltx.tx.hash, '_blank')) + $('#blockchain').off('click').on('click', -> window.open('https://live.blockcypher.com/btc/main/tx/' + finaltx.tx.hash, '_blank')) dialog.close() ) ) diff --git a/public/js/wallet.js b/public/js/wallet.js index d83b764d..cadd6db3 100644 --- a/public/js/wallet.js +++ b/public/js/wallet.js @@ -1,16 +1,19 @@ ;(function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0](function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({1:[function(require,module,exports){ (function(){(function() { - var check_address, convertedAmount, createWallet, displayErrors, errors, g, getBalance, getExchangeRate, getToken, getUser, isBip32, multiplier, precision, sendTransaction, validators, + var check_address, convertedAmount, createWallet, displayErrors, errors, g, getBalance, getExchangeRate, getUser, isBip32, multiplier, precision, sendTransaction, validators, __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; g = typeof exports !== "undefined" && exports !== null ? exports : this; g.proceed = false; - g.api = 'https://api.blockcypher.com/v1/btc/main'; + g.api = '/blockcypher/v1/btc/main'; validators = { address: function(e) { + if (e.val() === '') { + return true; + } try { return bitcoin.address.fromBase58Check(e.val()); } catch (_error) { @@ -18,6 +21,9 @@ } }, password: function(e) { + if (e.val() === '') { + return true; + } g.master = null; try { g.master = bitcoin.HDNode.fromBase58(CryptoJS.AES.decrypt($('#privkey').val(), e.val()).toString(CryptoJS.enc.Utf8)); @@ -29,12 +35,12 @@ }; errors = { - address: 'Invalid address', - password: 'Wrong password' + address: 'Invalid address.', + password: 'Wrong password.' }; $(function() { - getToken(); + getUser(); $('form').validator({ custom: validators, errors: errors @@ -52,7 +58,7 @@ $('#keys_updated').fadeIn(); $.post("/" + g.user.username, $('#keys form').serializeObject(), function() { return $.ajax({ - url: "" + g.api + "/wallets/hd/" + g.user.username + "?token=" + g.token, + url: "" + g.api + "/wallets/hd/" + g.user.username, type: 'DELETE' }).done(function() { return setTimeout(function() { @@ -62,8 +68,8 @@ extended_public_key: $('#pubkey').val(), subchain_indexes: [0, 1] }; - return $.post("" + g.api + "/wallets/hd?token=" + g.token, JSON.stringify(data)).always(function() { - return $.post("" + g.api + "/wallets/hd/" + g.user.username + "/addresses/derive?token=" + g.token).always(function() { + return $.post("" + g.api + "/wallets/hd", JSON.stringify(data)).always(function() { + return $.post("" + g.api + "/wallets/hd/" + g.user.username + "/addresses/derive").always(function() { return getBalance(); }); }); @@ -92,11 +98,11 @@ $('#max').click(function() { var amount; if ($('#currency_toggle').html() === g.user.unit) { - return $('#amount').val(g.balance); + return $('#amount').val(g.balance).blur(); } else { g.amount = parseFloat(g.balance).toFixed(precision()); amount = (g.balance / multiplier() * g.exchange).toFixed(2); - return $('#amount').val(amount); + return $('#amount').val(amount).blur(); } }); $('#amount').change(function() { @@ -152,13 +158,6 @@ }); }); - getToken = function() { - return $.get("/token", function(token) { - g.token = token; - return getUser(); - }); - }; - getUser = function() { return $.getJSON("/" + ($('#username').val()) + ".json", function(user) { g.user = user; @@ -180,7 +179,7 @@ }; createWallet = function() { - return $.get("" + g.api + "/wallets?token=" + g.token, function(data) { + return $.get("" + g.api + "/wallets", function(data) { var _ref; if (_ref = g.user.username, __indexOf.call(data.wallet_names, _ref) >= 0) { return getBalance(); @@ -191,7 +190,7 @@ extended_public_key: g.user.pubkey, subchain_indexes: [0, 1] }; - return $.post("" + g.api + "/wallets/hd?token=" + g.token, JSON.stringify(data)).always(getBalance); + return $.post("" + g.api + "/wallets/hd", JSON.stringify(data)).always(getBalance); } else { $('#balance').html(99); return $('#amount').val(99); @@ -201,7 +200,7 @@ }; getBalance = function() { - return $.get("" + g.api + "/addrs/" + g.user.username + "/balance?token=" + g.token + "&omitWalletAddresses=true", function(data) { + return $.get("" + g.api + "/addrs/" + g.user.username + "/balance?omitWalletAddresses=true", function(data) { var balance, fiat; balance = data.final_balance; g.balance = balance.toBTC(); @@ -257,7 +256,7 @@ ], preference: $('#priority').val() }; - return $.post("" + g.api + "/txs/new?token=" + g.token, JSON.stringify(params)).done(function(data) { + return $.post("" + g.api + "/txs/new", JSON.stringify(params)).done(function(data) { var amount, value; if ($('#currency_toggle').html() === g.user.unit) { amount = $('#amount').val(); @@ -270,7 +269,7 @@ if (value > parseFloat(g.balance).toSatoshis() - params.fees) { params.outputs[0].value -= params.fees; } - return $.post("" + g.api + "/txs/new?token=" + g.token, JSON.stringify(params)).done(function(data) { + return $.post("" + g.api + "/txs/new", JSON.stringify(params)).done(function(data) { var fee, total; data.pubkeys = []; data.signatures = data.tosign.map(function(tosign, i) { @@ -293,7 +292,7 @@ $('.dialog .address').html(data.tx.outputs[0].addresses[0]); dialog.getModalBody().html($('.dialog').html()); return dialog.getModal().find('.btn-primary').click(function() { - return $.post("" + g.api + "/txs/send?token=" + g.token, JSON.stringify(g.data)).then(function(finaltx) { + return $.post("" + g.api + "/txs/send", JSON.stringify(g.data)).then(function(finaltx) { var balance, fiat; $('#transaction_sent').fadeIn(); balance = g.balance.toSatoshis() - finaltx.tx.outputs[0].value - finaltx.tx.fees; @@ -302,7 +301,7 @@ $('#balance').html(g.balance); $('#fiat').html("" + fiat + " " + g.user.currency); $('#blockchain').off('click').on('click', function() { - return window.open('https://live.blockcypher.com/btc/main/' + finaltx.tx.hash, '_blank'); + return window.open('https://live.blockcypher.com/btc/main/tx/' + finaltx.tx.hash, '_blank'); }); return dialog.close(); }); |
