aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2015-11-18 12:00:48 -0800
committerAdam Soltys <[email protected]>2015-11-27 22:02:48 -0800
commit1292c860716b353dbb9441422b09a60226370d67 (patch)
tree1e3cfbc8e9481fa13f92ff11f17369b7c3db60c7
parentc53a670b2100f968be10790d58f417702b0dbf09 (diff)
downloadcoinos-server-1292c860716b353dbb9441422b09a60226370d67.tar.gz
coinos-server-1292c860716b353dbb9441422b09a60226370d67.zip
Cache wallet addresses
-rw-r--r--public/js/wallet.coffee110
-rw-r--r--src/install.coffee2
-rw-r--r--views/users/wallet.html12
3 files changed, 84 insertions, 40 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
diff --git a/src/install.coffee b/src/install.coffee
index 38e32b6f..7ba2449d 100644
--- a/src/install.coffee
+++ b/src/install.coffee
@@ -1,5 +1,5 @@
db = require("./redis")
-bcrypt = require('bcrypt')
+bcrypt = require('bcryptjs')
db.get("user:admin", (err, res) ->
if !res
diff --git a/views/users/wallet.html b/views/users/wallet.html
index 873a12d1..592aec3b 100644
--- a/views/users/wallet.html
+++ b/views/users/wallet.html
@@ -2,7 +2,7 @@
{{#js}}js/wallet{{/js}}
{{/yield-scripts}}
-<form id="setup" action="/{{user}}" method="post" style="display: none">
+<form action="/{{user}}" method="post" style="display: none">
<h1>Wallet</h1>
<input type="hidden" id="username" name="username" value="{{user}}" />
@@ -10,7 +10,7 @@
<div class="form-group">
<label>Available Balance:</label>
- <b id="balance" class="lead">0.05154234</b> BTC
+ <b id="balance" class="lead">0</b> BTC
</div>
<div>
@@ -24,7 +24,7 @@
<div class="form-group">
<label for="amount">Amount:</label>
<div class="input-group">
- <input type="number" id="amount" name="amount" class="form-control" value="0.05154232" />
+ <input type="number" id="amount" name="amount" class="form-control" />
<span class="input-group-btn">
<button type="button" class="btn">BTC</button>
</span>
@@ -35,7 +35,7 @@
</div>
<div class="form-group">
- <label for="amount">Recipient:</label>
+ <label for="amount">Receiving Address:</label>
<div class="input-group">
<input type="text" id="recipient" name="recipient" class="form-control" />
<span class="input-group-btn">
@@ -47,10 +47,12 @@
<div role="tabpanel" class="tab-pane" id="settings">
<div class="form-group">
<label for="pubkey">Bitcoin Receiving Address or HD Wallet Pubkey:</label>
- <input type="text" id="pubkey" name="pubkey" class="form-control" placeholder="xpub..." />
+ <textarea id="pubkey" name="pubkey" class="form-control" placeholder="xpub..." rows="3"></textarea>
<input type="hidden" id="privkey" name="privkey" />
</div>
</div>
</div>
</div>
+
+ <input class="btn btn-primary" type="submit" value="Done" />
</form>