aboutsummaryrefslogtreecommitdiffstats
path: root/public/js/wallet.coffee
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2015-11-27 22:02:33 -0800
committerAdam Soltys <[email protected]>2015-11-27 22:02:48 -0800
commit747cbd3b8d47cf124a9f121d5e8b7fc94521ecc1 (patch)
tree0d353fe75a41a47f01678e4a0ec4daaeef10d668 /public/js/wallet.coffee
parented284b9ca93f03b9dc8fce3744baab59588d43f1 (diff)
downloadcoinos-server-747cbd3b8d47cf124a9f121d5e8b7fc94521ecc1.tar.gz
coinos-server-747cbd3b8d47cf124a9f121d5e8b7fc94521ecc1.zip
Implement send transaction function
Diffstat (limited to 'public/js/wallet.coffee')
-rw-r--r--public/js/wallet.coffee85
1 files changed, 64 insertions, 21 deletions
diff --git a/public/js/wallet.coffee b/public/js/wallet.coffee
index b7a71d7e..720c6319 100644
--- a/public/js/wallet.coffee
+++ b/public/js/wallet.coffee
@@ -9,14 +9,13 @@
#= 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/bitcoinjs-lib.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
+#= require ../js/buffer.js
g = exports ? this
g.proceed = false
@@ -24,11 +23,26 @@ g.api = 'https://api.blockcypher.com/v1/btc/main'
$(->
getToken()
- $('form').submit(->
+ $('#settings form input[type=button]').click(->
$('.form-control').blur()
if $('.has-error').length > 0
$('.has-error').effect('shake', 500)
- return false
+
+ $.ajax(
+ url: "#{g.api}/wallets/hd/#{g.user.username}?token=#{g.token}"
+ type: 'DELETE'
+ )
+
+ $.post("/#{g.user.username}", $('#settings form').serializeObject(), ->
+ $('#settings .alert-success').fadeIn().delay(500).fadeOut()
+ )
+
+ localStorage.removeItem(g.user.username)
+ return false
+ )
+
+ $('#withdraw form input[type=button]').click(->
+ sendTransaction()
)
$('#currency_toggle').click(->
@@ -80,10 +94,10 @@ getUser = ->
$.getJSON("/#{$('#username').val()}.json", (user) ->
g.user = user
$('#pubkey').val(user.pubkey)
+ $('#privkey').val(user.privkey)
$('#address').val(user.address)
$('#unit').html(user.unit)
$('#currency_toggle').html(user.unit)
- $('form').fadeIn()
$('#amount').attr('step', 0.00000001 * multiplier())
getExchangeRate()
@@ -98,17 +112,16 @@ getExchangeRate = ->
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).fail(getAddresses)
- else
- $('#balance').html(99)
- $('#amount').val(99)
+
+ 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).fail(getAddresses)
else
- getAddresses()
+ $('#balance').html(99)
+ $('#amount').val(99)
getAddresses = ->
addresses = []
@@ -125,7 +138,7 @@ getAddresses = ->
localStorage.setItem(g.user.username, JSON.stringify(g.cache))
chunks = []
- for i in [0..Math.floor(count / 10)]
+ for i in [Math.floor(count / 10)..Math.floor(addresses.length / 10)]
chunks.push(addresses.slice(10 * i + count, 10 * i + count + 10))
getChunk = (chunk, i, last) ->
@@ -141,11 +154,14 @@ getAddresses = ->
)
, i*1500)
+ hit = false
if chunks.length
for chunk, i in chunks
- getChunk(chunk, i, chunks.length - 1) if chunk.length
+ if chunk.length
+ hit = true
+ getChunk(chunk, i, chunks.length - 1)
- calculateBalance()
+ calculateBalance() unless hit
)
calculateBalance = ->
@@ -161,16 +177,43 @@ calculateBalance = ->
$('#fiat').html("#{fiat} #{g.user.currency}")
$('#amount').val(g.balance)
+sendTransaction = ->
+ master = bitcoin.HDNode.fromBase58(g.user.privkey)
+ if typeof master.keyPair.d isnt 'undefined'
+ fee = 12500
+ amount = parseInt($('#amount').val() * 100000000 / multiplier())
+ amount = amount - fee
+ req =
+ inputs: [{wallet_name: g.user.username, wallet_token: g.token}],
+ outputs: [{addresses: [$('#recipient').val()], value: amount}]
+
+ $.post("#{g.api}/txs/new?token=#{g.token}", JSON.stringify(req)).then((tx) ->
+ tx.pubkeys = []
+ tx.signatures = tx.tosign.map((tosign, i) ->
+ path = tx.tx.inputs[i].hd_path.split('/')
+ key = master.derive(path[1]).derive(path[2])
+ tx.pubkeys.push(key.getPublicKeyBuffer().toString('hex'))
+ return key.sign(new buffer.Buffer(tosign, "hex")).toDER().toString("hex")
+ )
+
+ $.post("#{g.api}/txs/send?token=#{g.token}", JSON.stringify(tx)).then((finaltx) ->
+ $('#withdraw .alert-success').fadeIn().delay(500).fadeOut()
+ )
+ )
+ else
+ $('#withdraw .alert-danger').fadeIn().delay(500).fadeOut()
+
+
check_address = (address) ->
try
- bitcoin.Address.fromBase58Check(address)
+ bitcoin.address.fromBase58Check(address)
return true
catch
return isBip32(address)
isBip32 = (address) ->
try
- new BIP32(address)
+ bitcoin.HDNode.fromBase58(address)
return true
catch
return false