diff options
| author | Adam Soltys <[email protected]> | 2015-12-10 12:50:23 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2015-12-10 12:50:23 -0800 |
| commit | bcb889f59b461cf9aef82d6349a46f0941683a87 (patch) | |
| tree | ceba190b50f9cc898b4db241cf1732aa26cdb3d8 /public/js/wallet.coffee | |
| parent | 3de0e94e1343df0ce2cc8f39de53b1eef87b0a84 (diff) | |
| download | coinos-server-bcb889f59b461cf9aef82d6349a46f0941683a87.tar.gz coinos-server-bcb889f59b461cf9aef82d6349a46f0941683a87.zip | |
More work on wallet page
Diffstat (limited to 'public/js/wallet.coffee')
| -rw-r--r-- | public/js/wallet.coffee | 174 |
1 files changed, 109 insertions, 65 deletions
diff --git a/public/js/wallet.coffee b/public/js/wallet.coffee index 4cfdc8b8..ee72acc4 100644 --- a/public/js/wallet.coffee +++ b/public/js/wallet.coffee @@ -1,8 +1,6 @@ -buffer = require('buffer') - g = exports ? this g.proceed = false -g.api = 'https://api.blockcypher.com/v1/btc/main' +g.api = 'https://api.blockcypher.com/v1/bcy/test' $(-> getToken() @@ -11,19 +9,31 @@ $(-> $('#keys form input[type=button]').click(-> $('.form-control').blur() - if $('.has-error').length > 0 - $('.has-error').effect('shake', 500) + if $('#keys .has-error').length > 0 + $('#keys .has-error').effect('shake', 500) + return - $.ajax( - url: "#{g.api}/wallets/hd/#{g.user.username}?token=#{g.token}" - type: 'DELETE' - ) + $('#withdraw').click() + $('#keys_updated').fadeIn() - $.post("/#{g.user.username}", $('#settings form').serializeObject(), -> - $('#keys .alert-success').fadeIn().delay(2000).fadeOut() - setTimeout(-> - $('#withdraw').click() - , 2000) + $.post("/#{g.user.username}", $('#keys form').serializeObject(), -> + $.ajax( + url: "#{g.api}/wallets/hd/#{g.user.username}?token=#{g.token}" + type: 'DELETE' + ).done(-> + setTimeout(-> + data = + name: g.user.username + 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(-> + getBalance() + ) + ) + , 300) + ) ) return false @@ -68,11 +78,32 @@ $(-> ) $('#password').keyup(-> + g.master = null + try + g.master = bitcoin.HDNode.fromBase58(CryptoJS.AES.decrypt($('#privkey').val(), $(this).val()).toString(CryptoJS.enc.Utf8)) + $(this).parent().removeClass('has-error').addClass('has-success') + $('#invalid_keys').fadeOut() + catch + $(this).parent().addClass('has-error').removeClass('has-success') + ) + + $('#new_password').keyup(-> + $('#privkey').val(CryptoJS.AES.encrypt(g.privkey, $(this).val())) + try - g.privkey = CryptoJS.AES.decrypt(g.user.privkey, $(this).val()).toString(CryptoJS.enc.Utf8) + bitcoin.HDNode.fromBase58(CryptoJS.AES.decrypt(g.user.privkey, $(this).val()).toString(CryptoJS.enc.Utf8)) + $(this).parent().removeClass('has-error').addClass('has-success') + catch + $(this).parent().addClass('has-error').removeClass('has-success') + ) + + $('#manage').click(-> + $('#keys, #withdrawal').toggle() + $('#withdraw, #manage').toggle() + $('#privkey').val(g.user.privkey) ) - $('#manage, #withdraw').click(-> + $('#withdraw').click(-> $('#keys, #withdrawal').toggle() $('#withdraw, #manage').toggle() ) @@ -84,6 +115,20 @@ $(-> pom.setAttribute('download', 'coinos-wallet.aes.json') pom.click() ) + + $('#generate').click(-> + mnemonic = bip39.generateMnemonic() + key = bitcoin.HDNode.fromSeedBuffer(bip39.mnemonicToSeed(mnemonic)).deriveHardened(44).deriveHardened(0) + g.privkey = key.toString() + $('#pubkey').val(key.neutered().toString()).effect('highlight', {}, 2000) + $('#privkey').val('') + $('#new_password').parent().show() + $('#new_password').effect('shake', 500).focus() + ) + + $('[data-hide]').on('click', -> + $(this).closest('.alert').fadeOut(); + ) ) @@ -134,50 +179,42 @@ createWallet = -> getBalance = -> $.get("#{g.api}/addrs/#{g.user.username}/balance?token=#{g.token}&omitWalletAddresses=true", (data) -> balance = data.final_balance - g.balance = (balance * multiplier() / 100000000).toFixed(precision()) - fiat = (g.balance * g.exchange / multiplier()).toFixed(2) + g.balance = balance.toBTC() + fiat = balance.toFiat() $('#balance').html(g.balance) - $('.wallet').fadeIn() $('#fiat').html("#{fiat} #{g.user.currency}") $('#amount').attr('max', g.balance) - $('#amount').val(0.00001) - $('#recipient').val('13zP2yHXd6pMM9cATwRtQcTx7CgZsi6saE') + $('#amount').val(g.balance) + $('#recipient').val('CAdJXbDTotrZt4DjC7oj9npQUZgKKMF5e3') $('#amount').focus() + $('.wallet').fadeIn() ) sendTransaction = -> - params = - inputs: [{wallet_name: g.user.username, wallet_token: g.token}] - outputs: [{addresses: [$('#recipient').val()], value: 1}] - preference: $('#priority').val() - - $.post("#{g.api}/txs/new?token=#{g.token}", JSON.stringify(params)).done((data) -> - if data.errors - dialog.getModalBody().html('') - for e in data.errors - dialog.getModalBody().append("<div class='alert alert-danger'>#{e}</div>") - - try - master = bitcoin.HDNode.fromBase58(g.privkey) + if !g.master or typeof g.master.keyPair.d is 'undefined' + $('#invalid_keys').fadeIn() + $('#password').focus() + else + dialog = new BootstrapDialog( + title: '<h3>Confirm Transaction</h3>' + message: '<i class="fa fa-spinner fa-spin"></i> Calculating fee...</i>' + buttons: [ + label: 'Send' + cssClass: 'btn-primary' + , + label: ' Cancel' + cssClass: 'btn-default' + action: (dialogItself) -> dialogItself.close() + icon: 'glyphicon glyphicon-ban-circle' + ] + ).open() - if !master or typeof master.keyPair.d is 'undefined' - $('#withdrawal .alert-danger').fadeIn().delay(3000).fadeOut() - $('#password').focus() - else - dialog = new BootstrapDialog( - title: '<h3>Confirm Transaction</h3>' - message: '<i class="fa fa-spinner fa-spin"></i> Calculating fee...</i>' - buttons: [ - label: 'Send' - cssClass: 'btn-primary' - , - label: ' Cancel' - cssClass: 'btn-default' - action: (dialogItself) -> dialogItself.close() - icon: 'glyphicon glyphicon-ban-circle' - ] - ).open() + params = + inputs: [{wallet_name: g.user.username, wallet_token: g.token}] + outputs: [{addresses: [$('#recipient').val()], value: 1}] + preference: $('#priority').val() + $.post("#{g.api}/txs/new?token=#{g.token}", JSON.stringify(params)).done((data) -> if $('#currency_toggle').html() is g.user.unit amount = $('#amount').val() else @@ -194,39 +231,44 @@ sendTransaction = -> data.pubkeys = [] data.signatures = data.tosign.map((tosign, i) -> path = data.tx.inputs[i].hd_path.split('/') - key = master.derive(path[1]).derive(path[2]) - data.pubkeys.push(key.getPublicKeyBuffer().toString('hex')) - return key.sign(new buffer.Buffer(tosign, "hex")).toDER().toString("hex") + key = g.master.derive(path[1]).derive(path[2]) + data.pubkeys.push(key.keyPair.getPublicKeyBuffer().toString('hex')) + return key.keyPair.sign(new buffer.Buffer(tosign, "hex")).toDER().toString("hex") ) + g.data = data + amount = data.tx.outputs[0].value fee = data.tx.fees total = amount if value > parseFloat(g.balance).toSatoshis() - data.tx.fees total += fee - if data.tx.outputs.length is 2 - $('#change').show() - change = data.tx.outputs[1].value - $('.dialog .change').html("#{(change.toBTC())} #{g.user.unit} (#{change.toFiat()} #{g.user.currency})") - $('.dialog .amount').html("#{(amount.toBTC())} #{g.user.unit} (#{amount.toFiat()} #{g.user.currency})") $('.dialog .fee').html("#{(fee.toBTC())} #{g.user.unit} (#{fee.toFiat()} #{g.user.currency})") $('.dialog .total').html("#{(total.toBTC())} #{g.user.unit} (#{total.toFiat()} #{g.user.currency})") $('.dialog .address').html(data.tx.outputs[0].addresses[0]) dialog.getModalBody().html($('.dialog').html()) + dialog.getModal().find('.btn-primary').click(-> - $.post("#{g.api}/txs/send?token=#{g.token}", JSON.stringify(data)).then((finaltx) -> - $('#withdrawal .alert-success').fadeIn().delay(2000).fadeOut() + $.post("#{g.api}/txs/send?token=#{g.token}", 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/bcy/tx/' + finaltx.tx.hash, '_blank')) + dialog.close() ) ) ).fail((data) -> displayErrors(data.responseJSON, dialog) ) - ).fail((data) -> - displayErrors(data.responseJSON, dialog) - ) + ).fail((data) -> + displayErrors(data.responseJSON, dialog) + ) displayErrors = (data, dialog) -> if data.errors @@ -272,10 +314,12 @@ precision = -> 9 - multiplier().toString().length Number.prototype.toBTC = -> - (this / 100000000 * multiplier()).toFixed(precision()) + parseFloat((this / 100000000 * multiplier()).toFixed(precision())) Number.prototype.toFiat = -> (this * g.exchange / 100000000).toFixed(2) Number.prototype.toSatoshis = -> parseInt(this * 100000000 / multiplier()) + + |
