Exemplo n.º 1
0
 def __init__(self, api_uri="https://bitpay.com", insecure=False, pem=key_utils.generate_pem(), tokens={}):
   self.uri = api_uri
   self.verify = not(insecure)
   self.pem = pem
   self.client_id = key_utils.get_sin_from_pem(pem)
   self.tokens = tokens
   self.user_agent = 'bitpay-python'
Exemplo n.º 2
0
def client_from_stored_values():
  for f in ["local.pem", "tokens.json"]:
    try:
      open("temp/" + f)
      exists = True
    except:
      exists = False
      break
  if exists:
    f = open("temp/local.pem", 'r')
    pem = f.read()
    f = open("temp/tokens.json", 'r')
    token = f.read()
    token = json.loads(token)
    client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=pem, tokens=token)
  else:
    claim_code = get_claim_code_from_server()
    pem = key_utils.generate_pem()
    client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=pem)
    token = json.dumps(client.pair_pos_client(claim_code))
    if not os.path.exists("temp"):
      os.makedirs("temp")
    f = open("temp/local.pem", 'w')
    f.write(pem)
    f = open("temp/tokens.json", 'w')
    f.write(token)
  return client
Exemplo n.º 3
0
def client_from_stored_values():
    for f in ["local.pem", "tokens.json"]:
        try:
            open("temp/" + f)
            exists = True
        except:
            exists = False
            break
    if exists:
        f = open("temp/local.pem", 'r')
        pem = f.read()
        f = open("temp/tokens.json", 'r')
        token = f.read()
        token = json.loads(token)
        client = Client(api_uri=ROOT_ADDRESS,
                        insecure=True,
                        pem=pem,
                        tokens=token)
    else:
        claim_code = get_claim_code_from_server()
        pem = key_utils.generate_pem()
        client = Client(api_uri=ROOT_ADDRESS, insecure=True, pem=pem)
        token = json.dumps(client.pair_pos_client(claim_code))
        if not os.path.exists("temp"):
            os.makedirs("temp")
        f = open("temp/local.pem", 'w')
        f.write(pem)
        f = open("temp/tokens.json", 'w')
        f.write(token)
    return client
Exemplo n.º 4
0
 def __init__(self, api_uri="https://bitpay.com", insecure=False, pem=key_utils.generate_pem(), tokens={}):
   self.uri = api_uri
   self.verify = not(insecure)
   self.pem = pem
   self.client_id = key_utils.get_sin_from_pem(pem)
   self.tokens = tokens
   self.user_agent = 'bitpay-python'
Exemplo n.º 5
0
def auth_start(request, **kwargs):
    if request.event.settings.payment_bitpay_token:
        messages.error(request, _('You are already connected to BitPay.'))
        return redirect(
            reverse('control:event.settings.payment.provider',
                    kwargs={
                        'organizer': request.event.organizer.slug,
                        'event': request.event.slug,
                        'provider': 'bitpay'
                    }))
    request.session['payment_bitpay_auth_event'] = request.event.pk
    pem = request.event.settings.payment_bitpay_pem
    if not pem:
        gs = GlobalSettingsObject()
        pem = gs.settings.payment_bitpay_pem = key_utils.generate_pem()

    sin = key_utils.get_sin_from_pem(pem)
    if request.GET.get('url'):
        url = request.GET.get('url')
    else:
        url = 'https://test.bitpay.com' if 'test' in request.GET else 'https://bitpay.com'
    try:
        r = requests.post(url + '/tokens',
                          json={
                              'label': settings.PRETIX_INSTANCE_NAME,
                              'facade': 'merchant',
                              'id': sin
                          })
    except requests.ConnectionError:
        messages.error(request,
                       _('Communication with BitPay was not successful.'))
    else:
        if r.status_code == 200:
            data = r.json()['data'][0]
            request.event.settings.payment_bitpay_token = data['token']
            request.event.settings.payment_bitpay_url = url
            return redirect(url + '/api-access-request?pairingCode=' +
                            data['pairingCode'])
        messages.error(request,
                       _('Communication with BitPay was not successful.'))

    return redirect(
        reverse('control:event.settings.payment.provider',
                kwargs={
                    'organizer': request.event.organizer.slug,
                    'event': request.event.slug,
                    'provider': 'bitpay'
                }))
Exemplo n.º 6
0
import random
import logging

API_HOST = "https://bitpay.com"  #for production, live bitcoin
# API_HOST = "https://test.bitpay.com"  # for testing, testnet bitcoin
KEY_FILE = "realkey.priv"
TOKEN_FILE = "realtoken.priv"

# check if there is a preexisting key file
if os.path.isfile(KEY_FILE):
    f = open(KEY_FILE, 'r')
    key = f.read()
    f.close()
    print("Creating a bitpay client using existing private key from disk.")
else:
    key = bku.generate_pem()
    f = open(KEY_FILE, 'w')
    f.write(key)
    f.close()

client = Client(API_HOST, False, key)


def fetch_token(facade):
    if os.path.isfile(TOKEN_FILE + facade):
        f = open(TOKEN_FILE + facade, 'r')
        token = f.read()
        f.close()
        print("Reading " + facade + " token from disk.")
        # global client
        # client = Client(API_HOST, False, key, {facade: token})
Exemplo n.º 7
0
 def test_generate_pem(self):
   pem = utils.generate_pem()
   match = re.match(r"-----BEGIN EC PRIVATE KEY-----", pem)
   self.assertIsNotNone(match)
Exemplo n.º 8
0
import bitpay.key_utils as key_utils

pem = key_utils.generate_pem()
with open("keys/local.pem", 'w') as f:
    f.write(pem)
sin = key_utils.get_sin_from_pem(pem)
print("Printing SIN")
print(sin)