aboutsummaryrefslogtreecommitdiffstats
path: root/test/integration/bolt12.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'test/integration/bolt12.test.ts')
0 files changed, 0 insertions, 0 deletions
6:49 -0800 committer Adam Soltys <[email protected]> 2012-11-07 17:52:50 -0800 Use coffeescript' href='/coinos-server.git/commit/public/js/calculator.coffee?h=readme-update&id=3e900e0bba211c2bbf2bb10c85a82dd9ce6943f0'>3e900e0b ^
c5d381e2 ^
e37c28ac ^

85740c9e ^
e37c28ac ^
083f1461 ^
c5d381e2 ^
3e900e0b ^
b4037fd1 ^
40083923 ^
b4037fd1 ^

40083923 ^
b4037fd1 ^

3e900e0b ^
85740c9e ^














90da658f ^
85740c9e ^




















898d21a7 ^





85740c9e ^
898d21a7 ^
0fd4b44c ^
74d93fd2 ^

60029a8f ^
120efd8d ^
85740c9e ^



3e900e0b ^

c5d381e2 ^
40083923 ^

0765adfc ^
40083923 ^


cd55bb01 ^


40083923 ^
c1cd5515 ^
41f8c683 ^
c1cd5515 ^
40083923 ^
2e8901c3 ^
40083923 ^
85740c9e ^



3e900e0b ^

c5d381e2 ^
40083923 ^
c5d381e2 ^


40083923 ^
c5d381e2 ^






cd55bb01 ^

c5d381e2 ^
40083923 ^

40083923 ^

c5d381e2 ^



40083923 ^

85740c9e ^


40083923 ^



85740c9e ^












40083923 ^
85740c9e ^



dc762c87 ^


898d21a7 ^
dc762c87 ^









3e900e0b ^
40083923 ^
270993d7 ^




1e24fe8d ^


270993d7 ^




329798f4 ^
1e24fe8d ^

1e24fe8d ^
270993d7 ^


1e24fe8d ^

270993d7 ^
e37c28ac ^
1e24fe8d ^


4e5d09cf ^
dec12454 ^



1e24fe8d ^

40083923 ^
4e5d09cf ^
1e24fe8d ^

4e5d09cf ^
1e24fe8d ^
4e5d09cf ^

4e5d09cf ^

cfed6f91 ^
4e5d09cf ^

083f1461 ^







d270e736 ^
083f1461 ^
4e5d09cf ^



2e8901c3 ^
41f8c683 ^
2e8901c3 ^

cc64f72c ^




2e8901c3 ^
170dee27 ^
4e5d09cf ^
c5d381e2 ^


c5d381e2 ^










c5d381e2 ^



b4037fd1 ^

40083923 ^
b4037fd1 ^


cd55bb01 ^
b4037fd1 ^


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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

                                                                                  
                                
 
                  

                      
                       
         
                   
 
    
         
                                     

                      
                   

             
 














                                             
                          




















                                                                                          





                               
                              
                                            
                                       

                 
 
                                                                         



                                                                           

 
          

                         
                           


                                


                                                                                     
                
                                                    
                                                
                                      
 
              
 



                                      

                       
                     
          


                      
                                                                               






                            

                                                                              
                   

                            

                              



                                       

                                                


                                                                   



                           












                                                             
                                    



                                         


              
             









                                                                               
 
           




                                                  


                                                                




                                                                               
                                                           

                              
                       


                          

                             
                         
              


                                        
                



                              

                                     
                                      
                                       

       
                                  
 

                                                                  

                                 
                           

                  







                                                     
                  
       



                
     
                                                  

                                  




                                                     
 
                                                                                                         
 


                            










                                                



                                                  

               
                    


                             
                            


                                  
EXCHANGE_FAIL = "Problem fetching exchange rate"
SOCKET_FAIL = "Problem connecting to payment server, notifications may not appear"
ADDRESS_FAIL = "Invalid address"

g = exports ? this
g.errors = []
g.amount_requested = 0
g.amount = 0.toFixed(2)
g.tip = 1
g.transactions = []

$(->
  $.ajax(
    url: $('#user').val() + '.json', 
    dataType: 'json',
    success: (data) ->
      g.user = data
      setup()
  )

  $('button').click((e) ->
    e.preventDefault()
  )

  $('.numpad .btn').off('click').click((e) ->
    e.preventDefault()

    m = $(this).html()
    n = parseFloat($('.amount').html())

    if m is 'C'
      g.amount = 0.toFixed(2)
      updateTotal()
      return

    return if n > 10000000
    
    if m is '00'
      n = 100 * n
    else
      n = 10 * n + parseFloat(m) / 100

    g.amount = n.toFixed(2)

    updateTotal()
  )

  $('.numpad .btn:last').off('click').click((e) ->
    e.preventDefault()

    g.amount = (Math.floor(100 * (parseFloat($('.amount').html()) / 10)) / 100).toFixed(2)
    updateTotal()
  )

  $('.tippad .btn').off('click').click((e) ->
    e.preventDefault()

    text = $(this).html()
    if text is 'No Tip'
      value = 0
    else
      value = text.slice(1, -1)

    $(this).addClass('active')
    $(this).siblings().removeClass('active')
    g.tip = 1 + (parseFloat(value)/100)
    updateTotal()
  )

  $('#received').click(-> window.location = "/#{g.user.username}/report")
  $('#slide').click(-> 
    $('#controls').slideToggle() 
    $(this).find('i').toggleClass('fa-sort-up').toggleClass('fa-sort-down')
  )
)

setup = ->
  g.user.address or= ''
  g.user.commission or= 0
  g.user.currency or= 'CAD'
  g.user.symbol or= 'quadrigacx'
  g.user.unit or= 'BTC'

  if g.user.title 
    $('#title').html("<a href='/#{g.user.username}/edit'>#{g.user.title}</a>").show()

  if g.user.logo
    ext = g.user.logo.substr(g.user.logo.length - 3)
    src = "/img/logos/#{g.user.username}.#{ext}"
    $('#logo').attr('src', src).show()

  getAddress()

  $('.symbol').html(g.user.currency)
  $('.currency').html(g.user.currency)
  $('.unit').html(g.user.unit)

  $('#received').hide()

  fetchExchangeRate()
  listen()

fetchExchangeRate = ->
  $.ajax(
    url: "ticker?currency=#{g.user.currency}&symbol=#{g.user.symbol}&type=bid",
    success: (exchange) -> 
      if exchange?
        clear(EXCHANGE_FAIL)
      else
        fail(EXCHANGE_FAIL)
        return

      g.exchange = (exchange - exchange * g.user.commission * 0.01).toFixed(2)
      $('#exchange').html(g.exchange)
      updateTotal()

      unless g.setupComplete
        g.setupComplete = true

    error: -> fail(EXCHANGE_FAIL)
  )
  setTimeout(fetchExchangeRate, 900000)

updateTotal = ->
  precision = 9 - multiplier().toString().length
  subtotal = parseFloat(g.amount * g.tip).toFixed(2)
  total = (subtotal * multiplier() / g.exchange).toFixed(precision)
  g.amount_requested = (subtotal / g.exchange).toFixed(8)

  unless $.isNumeric(total)
    total = ''

  $('#received').hide()
  $('#payment').fadeIn('slow')

  $('.tip').html((subtotal - g.amount).toFixed(2))
  $('.subtotal').html(subtotal.toString())
  $('.amount').html(g.amount)
  $('.exchange').html(g.exchange)
  $('.symbol').html(g.user.symbol)

  if g.user.commission < 0
    $('.commission').html("+#{Math.abs(g.user.commission)}%")


  $('#total').html(total.toString())
 
  # size = $('#calculator').width() * 0.7
  size = 300
  $('#qr').css('height', size)

  time = 0
  if g.timeout
    time = 10

  clearTimeout(g.timeout)
  g.timeout = setTimeout(->
    $('#qr').html('')
    new QRCode('qr', 
      text: "bitcoin:#{g.user.address}?amount=#{g.amount_requested.toString()}"
      width: size
      height: size
    )
  , time)

listen = ->
  g.attempts++
  if g.blockchain and g.blockchain.readyState is 1
    setTimeout(listen, 12000)
  else
    fail(SOCKET_FAIL) if g.attempts > 3
    g.blockchain = new WebSocket("wss://ws.blockchain.info/inv")

    g.blockchain.onopen = -> 
      if g.blockchain.readyState is 1
        g.attempts = 0
        clear(SOCKET_FAIL)
        g.blockchain.send('{"op":"addr_sub", "addr":"' + g.user.address + '"}')
      else
        setTimeout(g.blockchain.onopen, 12000 * g.attempts)
    
    g.blockchain.onerror =  ->
      fail(SOCKET_FAIL)
      g.blockchain.close()
      delete g.blockchain
      listen()

    g.blockchain.onclose = ->
      delete g.blockchain
      listen()

    g.blockchain.onmessage = (e) ->
      results = eval('(' + e.data + ')')
      amount = 0
      txid = results.x.hash

      return if txid == g.last
      g.last = txid
      
      $.each(results.x.out, (i, v) ->
        if (v.addr == g.user.address) 
          amount += v.value / 100000000
      )

      logTransaction(txid, amount)

logTransaction = (txid, amount) ->
  if $('#received').is(":hidden") and amount >= g.amount_requested
    $('#payment').hide()
    $('#received').fadeIn('slow')
    $('#success')[0].play()
    g.user.index++

    if g.transactions.indexOf(txid) is -1
      g.transactions.push(txid)
      $.post("/#{g.user.username}/transactions",
        txid: txid,
        address: g.user.address,
        date: moment().format("YYYY-MM-DD HH:mm:ss"),
        received: amount,
        exchange: g.exchange
        tip: g.tip
      )

    getAddress()

getAddress = ->
  try
    bitcoin.address.fromBase58Check(g.user.pubkey)
    g.user.address = g.user.pubkey
  catch
    master = bitcoin.HDNode.fromBase58(g.user.pubkey)
    child = master.derive(0).derive(g.user.index)
    g.user.address = child.getAddress().toString()
    g.blockchain.close() if g.blockchain
    listen()

  $('#address').html("<a href='https://blockchain.info/address/#{g.user.address}'>#{g.user.address}</a>")

fail = (msg) ->
  g.errors.push(msg)
  g.errors = g.errors.uniq()
  $('#error').show().html(g.errors.toString())
  
clear = (msg) ->
  i = g.errors.indexOf(msg)
  g.errors.splice(i, 1) if i >= 0
  if (g.errors.length > 0)
    $('#error').show().html(g.errors.toString())
  else
    $('#error').hide()
    $('#calculator').fadeIn('slow')

Array::uniq = ->
  output = {}
  output[@[key]] = @[key] for key in [0...@length]
  value for key, value of output

multiplier = ->
  switch g.user.unit
    when 'BTC' then 1
    when 'mBTC' then 1000
    when 'µBTC' then 1000000
    when 'bits' then 1000000
    when 'satoshis' then 100000000