diff options
| author | Adam Soltys <[email protected]> | 2015-11-18 12:00:48 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2015-11-27 22:02:48 -0800 |
| commit | 1292c860716b353dbb9441422b09a60226370d67 (patch) | |
| tree | 1e3cfbc8e9481fa13f92ff11f17369b7c3db60c7 /public | |
| parent | c53a670b2100f968be10790d58f417702b0dbf09 (diff) | |
| download | coinos-server-1292c860716b353dbb9441422b09a60226370d67.tar.gz coinos-server-1292c860716b353dbb9441422b09a60226370d67.zip | |
Cache wallet addresses
Diffstat (limited to 'public')
| -rw-r--r-- | public/js/wallet.coffee | 110 |
1 files changed, 76 insertions, 34 deletions
diff --git a/public/js/wallet.coffee b/public/js/wallet.coffee index fcbbd062..c2cc5041 100644 --- a/public/js/wallet.coffee +++ b/public/js/wallet.coffee @@ -20,49 +20,91 @@ g = exports ? this g.proceed = false +g.api = 'https://api.blockcypher.com/v1/btc/main' $(-> - api = 'https://api.blockcypher.com/v1/btc/main' + getToken() + $('form').submit(-> + $('.form-control').blur() + if $('.has-error').length > 0 + $('.has-error').effect('shake', 500) + return false + ) +) + +getToken = -> + $.get("/token", (token) -> + g.token = token + getUser() + ) + +getUser = -> + $.getJSON("/#{$('#username').val()}.json", (user) -> + g.user = user + $('#pubkey').val(user.pubkey) + $('#address').val(user.address) + $('form').fadeIn() + + createWallet() + ) + + +createWallet = -> + if localStorage.getItem(g.user.username) is null + localStorage.setItem(g.user.username, JSON.stringify(addresses: [], balances: [])) + if isBip32(g.user.pubkey) + data = + name: g.user.username + extended_public_key: g.user.pubkey + subchain_indexes: [0,1] + $.post("#{g.api}/wallets/hd?token=#{g.token}", JSON.stringify(data), getAddresses) + else + $('#balance').html(99) + $('#amount').val(99) + else + getAddresses() + +getAddresses = -> 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) + $.get("#{g.api}/wallets/hd/#{g.user.username}/addresses?token=#{g.token}", (data) -> + count = JSON.parse(localStorage.getItem(g.user.username)).addresses.length + balances = JSON.parse(localStorage.getItem(g.user.username)).balances - chunks = [] - for i in [0 .. addresses.length] by 10 - chunks.push(addresses.slice(i,i+10)) + for c in data.chains + for a in c.chain_addresses + addresses.push(a.address) - 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) + chunks = [] + for i in [0..Math.floor(count / 10)] + chunks.push(addresses.slice(10 * i + count, 10 * i + count + 10)) - $('#balance').html(val) - $('#amount').val(val) - $('#balance, #amount').fadeIn() - ) - , i*1500) + getChunk = (chunk, i, last) -> + setTimeout(-> + $.get("#{g.api}/addrs/#{chunk.join(';')}/balance?token=#{g.token}", (addresses) -> + for address in addresses + balances.push(parseInt(address.final_balance)) - for c, i in chunks - doSetTimeout(c, i) - ) - ) + if i is last + localStorage.setItem(g.user.username, JSON.stringify(addresses: addresses, balances: balances)) + calculateBalance() + ) + , i*1500) - user = $('#username').val() - $.getJSON("/#{user}.json", (data) -> - $('#pubkey').val(data.pubkey) - $('#address').val(data.address) - $('#setup').fadeIn() + if chunks[0].length + for chunk, i in chunks + getChunk(chunk, i, chunks.length) + else + calculateBalance() + + localStorage.setItem(g.user.username, JSON.stringify(addresses: addresses, balances: balances)) ) -) + +calculateBalance = -> + balances = JSON.parse(localStorage.getItem(g.user.username)).balances + balance = balances.reduce (t,s) -> t + s + val = (balance / 100000000).toFixed(8) + $('#balance').html(val) + $('#amount').val(val) check_address = (address) -> try |
