CCrypto VPN public website https://vpn.ccrypto.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

107 lines
3.2 KiB

  1. from decimal import Decimal
  2. from django.shortcuts import redirect
  3. from django.utils.translation import ugettext_lazy as _
  4. from django.core.urlresolvers import reverse
  5. from .base import BackendBase
  6. class BitcoinBackend(BackendBase):
  7. """ Bitcoin backend.
  8. Connects to a bitcoind.
  9. """
  10. backend_id = 'bitcoin'
  11. backend_verbose_name = _("Bitcoin")
  12. backend_display_name = _("Bitcoin")
  13. COIN = 100000000
  14. def __init__(self, settings):
  15. from bitcoin import SelectParams
  16. from bitcoin.rpc import Proxy
  17. self.btc_value = settings.get('BITCOIN_VALUE')
  18. self.account = settings.get('ACCOUNT', 'ccvpn3')
  19. chain = settings.get('CHAIN')
  20. if chain:
  21. SelectParams(chain)
  22. self.url = settings.get('URL')
  23. if not self.url:
  24. return
  25. assert isinstance(self.btc_value, int)
  26. self.make_rpc = lambda: Proxy(self.url)
  27. self.rpc = self.make_rpc()
  28. self.backend_enabled = True
  29. def new_payment(self, payment):
  30. rpc = self.make_rpc()
  31. # bitcoins amount = (amount in cents) / (cents per bitcoin)
  32. btc_price = round(Decimal(payment.amount) / self.btc_value, 5)
  33. address = str(rpc.getnewaddress(self.account))
  34. msg = _("Please send %(amount)s BTC to %(address)s")
  35. payment.status_message = msg % dict(amount=str(btc_price), address=address)
  36. payment.backend_extid = address
  37. payment.backend_data = dict(btc_price=str(btc_price), btc_address=address)
  38. payment.save()
  39. return redirect(reverse('payments:view', args=(payment.id,)))
  40. def check(self, payment):
  41. rpc = self.make_rpc()
  42. if payment.status != 'new':
  43. return
  44. btc_price = payment.backend_data.get('btc_price')
  45. address = payment.backend_data.get('btc_address')
  46. if not btc_price or not address:
  47. return
  48. btc_price = Decimal(btc_price)
  49. received = Decimal(rpc.getreceivedbyaddress(address)) / self.COIN
  50. payment.paid_amount = int(received * self.btc_value)
  51. payment.backend_data['btc_paid_price'] = str(received)
  52. if received >= btc_price:
  53. payment.user.vpnuser.add_paid_time(payment.time)
  54. payment.user.vpnuser.on_payment_confirmed(payment)
  55. payment.user.vpnuser.save()
  56. payment.status = 'confirmed'
  57. payment.save()
  58. def get_info(self):
  59. rpc = self.make_rpc()
  60. try:
  61. info = rpc.getinfo()
  62. if not info:
  63. return [(_("Status"), "Error: got None")]
  64. except Exception as e:
  65. return [(_("Status"), "Error: " + repr(e))]
  66. v = info.get('version', 0)
  67. return (
  68. (_("Bitcoin value"), "%.2f €" % (self.btc_value / 100)),
  69. (_("Testnet"), info['testnet']),
  70. (_("Balance"), '{:f}'.format(info['balance'] / self.COIN)),
  71. (_("Blocks"), info['blocks']),
  72. (_("Bitcoind version"), '.'.join(str(v // 10 ** (2 * i) % 10 ** (2 * i))
  73. for i in range(3, -1, -1))),
  74. )
  75. def get_ext_url(self, payment):
  76. if not payment.backend_extid:
  77. return None
  78. return 'http://blockr.io/address/info/%s' % payment.backend_extid