aboutsummaryrefslogtreecommitdiffstats
path: root/public/js/wallet.coffee
blob: 8ced085fc27e4ac8d9ec0a8c2896890add30245b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#= require ../js/jquery-1.8.2.min.js
#= require ../js/jquery-ui.min.js
#= require ../js/jquery.printElement.min.js
#= require ../js/check_password.js
#= require ../js/bootstrap.min.js
#= require ../js/jsbn.js
#= require ../js/jsbn2.js
#= require ../js/crypto-min.js
#= 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/modsqrt.js
#= require ../js/bip32.js
#= require ../js/secure_random.js
#= require ../js/qrcode.js
#= require ../js/aes.js
#= require ../js/key.js

g = exports ? this
g.proceed = false

$(->
  $('#encryption-password').pwstrength(showVerdicts: false)

  user = $('#username').val()
  $.getJSON("/#{user}.json", (data) ->
    $('#pubkey').val(data.pubkey)
    $('#address').val(data.address)
    $('#setup').fadeIn()
  )

  $('#pubkey').blur(->
    if check_address($(this).val())
      $(this).parent().removeClass('has-error')
    else 
      $(this).parent().addClass('has-error')
  )

  $('#generate').click(->
    key = new Key()
    g.pubkey = key.extended_public_key_string()
    g.privkey = key.extended_private_key_string()

    $('#modal').modal()
    $('#privkey_text').html(g.privkey)
    $('#qr').html('')
    new QRCode('qr', text: g.privkey, width: 260, height: 260)
  )

  $('#encryption-password').keyup((e) ->
    return if e.keyCode is 9 or e.keyCode is 16
    g.enc_privkey = CryptoJS.AES.encrypt(g.privkey, $(this).val()).toString()
    g.enc_privkey = g.privkey if $(this).val() == ''
    $('#privkey_text').html(g.enc_privkey)
    $('#qr').html('')
    new QRCode('qr', text: g.enc_privkey, width: 260, height: 260)

    if $(this).val()
      $('#status').html('(Encrypted)')
    else
      $('#status').html('(Unencrypted)')
  )

  $('#confirm').blur(->
    $('#confirm_error').remove() 
    if $('#password').val() != $('#confirm').val()
      $('#password, #confirm').parent().addClass('has-error')
      $('#confirm').parent().after('<div id="confirm_error" class="alert alert-danger">Passwords don\'t match</div>')
    else
      $('#password, #confirm').parent().removeClass('has-error')
  )

  $('#encryption-password, #encryption-confirm').keyup((e) -> check_passwords(e) unless e.keyCode is 9)
  $('#encryption-password, #encryption-confirm').blur((e) -> check_passwords(e))

  check_passwords = (e) ->
    if $('#encryption-password').val() != $('#encryption-confirm').val() or $('#encryption-password').val() is ''
      $('#encryption-password, #encryption-confirm').parent().addClass('has-error')
      $('#modal .alert').removeClass('alert-success').addClass('alert-danger')
    else
      $('#encryption-password, #encryption-confirm').parent().removeClass('has-error')
      $('#modal .alert').removeClass('alert-danger').addClass('alert-success')

    if $('.progress-bar-success').length > 0
      $('#encryption-password').parent().removeClass('has-error')
    else
      $('#encryption-password').parent().addClass('has-error')

  $('#print').click(->
    $('#key-dialog').printElement(printMode: 'popup')
  )

  $('a[data-toggle="tab"]').on('shown.bs.tab', (e) ->
    $('#print').toggle(e.target.attributes.href.value == '#step1')
  )

  $('#close').click(-> 
    $('#modal .form-control').blur()
    if $('#modal .has-error').length > 0
      $('#step1_link').click()
      $('#modal .has-error').effect('shake', 500)
    else
      g.update_key = true
      $('#modal').modal('hide')
  )
  $('#encrypt').submit(-> $('#close').click(); return false)

  $('#modal').on('show.bs.modal', -> 
    $('#encryption-password, #encryption-confirm').val('').keyup().parent().removeClass('has-error')
    $('#modal .alert').removeClass('alert-success').addClass('alert-danger')
  )

  $('#modal').on('shown.bs.modal', -> 
    g.update_key = false
    $('#encryption-password').focus()
  ).on('hidden.bs.modal', ->
    if g.update_key
      $('#pubkey').val(g.pubkey) 
      $('#privkey').val(g.enc_privkey)
  )

  $('#setup').submit(->
    $('#setup .form-control').blur()
    if $('#setup .has-error').length > 0
      $('#setup .has-error').effect('shake', 500)
      return false
  )
)

check_address = (address) ->
  try
    bitcoin.Address.fromBase58Check(address)
    return true
  catch
    return isBip32(address)

isBip32 = (address) ->
  try
    new BIP32(address)
    return true
  catch
    return false