aboutsummaryrefslogtreecommitdiffstats
path: root/public/js/generate_address.coffee
blob: 5b816ecf29a4a9836f68fee4decdc5cc9c0c164d (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
#= require js/jquery-1.8.2.min.js
#= require js/jquery-ui.min.js
#= require js/jquery.printElement.min.js
#= require js/bootstrap.min.js
#= require js/bitcoinjs-min-1.0.2.js
#= require js/qrcode.js
#= require js/bip38.js
#= require js/check_password.js

g = this

$(->
  genKey()
  $('.regenerate').click(genKey)

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

    $('#modal').modal()
    return false
  )

  $('#modal').on('shown.bs.modal', -> 
    bip38 = new Bip38
    g.enc_privkey = bip38.encrypt(g.privkey, $('#password').val(), g.pubkey)
    $('#modal-body').html('Done!')
    setTimeout(
      -> $('#modal').modal('hide')
      1000
    )
  )

  $('#modal').on('hidden.bs.modal', -> 
    if $('#password').val() == ''
      g.enc_privkey = g.privkey 
      $('#status').html('')
    else
      $('#status').html('Encrypted')

    $('#privkey').html(g.enc_privkey)
    $('#privqr').html('')
    new QRCode('privqr', text: $('#privkey').html(), width: 260, height: 260)
  )

  $('.print').click(-> window.print())
  $('#password').pwstrength(showVerdicts: false)
  $('#password').blur(->
    $('#confirm').blur()
    $(this).parent().next('.alert').remove()

    if $('.progress-bar-success').length > 0
      $(this).parent().removeClass('has-error')
    else
      $(this).parent().addClass('has-error')
  )
  $('#confirm').blur(->
    if $('#password').val() == $('#confirm').val()
      $('#confirm').parent().removeClass('has-error')
    else
      $('#confirm').parent().addClass('has-error')
  )
)

genKey = ->
  $('#password').focus()
  key = bitcoin.ECKey.makeRandom(false)
  g.privkey = key.toWIF()
  g.pubkey = key.pub.getAddress().toString()

  $('#pubkey').html(g.pubkey)
  $('#privkey').html(g.privkey)

  $('#pubqr, #privqr, #status').html('')
  $('#password, #confirm').val('').keyup().parent().removeClass('has-error')

  new QRCode('pubqr', text: $('#pubkey').html(), width: 260, height: 260)
  new QRCode('privqr', text: $('#privkey').html(), width: 260, height: 260)