def verify_tokens(self): """ Deprecated, will be made private in 2.4 """ xidentity = key_utils.get_compressed_public_key_from_pem(self.pem) url = self.uri + "/tokens" xsignature = key_utils.sign(self.uri + "/tokens", self.pem) headers = { "content-type": "application/json", 'X-Identity': xidentity, 'X-Signature': xsignature, 'X-accept-version': '2.0.0' } response = requests.get(self.uri + "/tokens", headers=headers, verify=self.verify) if response.ok: allTokens = response.json()['data'] selfKeys = self.tokens.keys() matchedTokens = [ token for token in allTokens for key in selfKeys if token.get(key) == self.tokens.get(key) ] if not matchedTokens: return False return True
def test_sign(self): pem = '-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEICg7E4NN53YkaWuAwpoqjfAofjzKI7Jq1f532dX+0O6QoAcGBSuBBAAK\noUQDQgAEjZcNa6Kdz6GQwXcUD9iJ+t1tJZCx7hpqBuJV2/IrQBfue8jh8H7Q/4vX\nfAArmNMaGotTpjdnymWlMfszzXJhlw==\n-----END EC PRIVATE KEY-----\n' signed = utils.sign("message", pem) sk = SigningKey.from_pem(pem) vk = sk.get_verifying_key() print(signed) signed = binascii.unhexlify(signed) vk.verify(signed, "message".encode(), hashfunc=hashlib.sha256, sigdecode=ecdsaUtil.sigdecode_der)
def create_invoice(self, params): self.verify_invoice_params(params['price'], params['currency']) payload = json.dumps(params) uri = self.uri + "/invoices" xidentity = key_utils.get_compressed_public_key_from_pem(self.pem) xsignature = key_utils.sign(uri + payload, self.pem) headers = {"content-type": "application/json", 'accept': 'application/json', 'X-Identity': xidentity, 'X-Signature': xsignature, 'X-accept-version': '2.0.0'} try: response = requests.post(uri, data=payload, headers=headers, verify=self.verify) except Exception as pro: raise BitPayConnectionError(pro.args) if response.ok: return response.json()['data'] self.response_error(response)
def get_from_bitpay_api(client, uri, token): payload = "?token=%s" % token xidentity = bku.get_compressed_public_key_from_pem(client.pem) xsignature = bku.sign(uri + payload, client.pem) headers = {"content-type": "application/json", "X-Identity": xidentity, "X-Signature": xsignature, "X-accept-version": "2.0.0"} try: pp.pprint(headers) print(uri + payload) response = requests.get(uri + payload, headers=headers, verify=client.verify) except Exception as pro: raise BitPayConnectionError(pro.args) if response.ok: return response.json()['data'] client.response_error(response)
def verify_tokens(self): """ Deprecated, will be made private in 2.4 """ xidentity = key_utils.get_compressed_public_key_from_pem(self.pem) url = self.uri + "/tokens" xsignature = key_utils.sign(self.uri + "/tokens", self.pem) headers = {"content-type": "application/json", 'accept': 'application/json', 'X-Identity': xidentity, 'X-Signature': xsignature, 'X-accept-version': '2.0.0'} response = requests.get(self.uri + "/tokens", headers=headers, verify=self.verify) if response.ok: allTokens = response.json()['data'] selfKeys = self.tokens.keys() matchedTokens = [token for token in allTokens for key in selfKeys if token.get(key) == self.tokens.get(key)] if not matchedTokens: return False return True
def get_invoice(self, invoice_id, token=None): if token: uri = self.uri + "/invoices/" + invoice_id + "?token=" + token xidentity = key_utils.get_compressed_public_key_from_pem(self.pem) xsignature = key_utils.sign(uri, self.pem) headers = { "content-type": "application/json", 'X-Identity': xidentity, 'X-Signature': xsignature, 'X-accept-version': '2.0.0' } else: uri = self.uri + "/invoices/" + invoice_id headers = {"content-type": "application/json"} try: response = requests.get(uri, headers=headers, verify=self.verify) except Exception as pro: raise BitPayConnectionError(pro.args) if response.ok: return response.json()['data'] self.response_error(response)
def _refund(self, refund): payload = json.dumps({ 'token': refund.payment.info_data.get('token'), 'amount': refund.payment.info_data.get('price'), 'currency': refund.payment.info_data.get('currency'), 'refundEmail': refund.order.email }) uri = self.client.uri + "/invoices/" + refund.payment.info_data.get('id') + "/refunds" xidentity = key_utils.get_compressed_public_key_from_pem(self.client.pem) xsignature = key_utils.sign(uri + payload, self.client.pem) headers = {"content-type": "application/json", 'accept': 'application/json', 'X-Identity': xidentity, 'X-Signature': xsignature, 'X-accept-version': '2.0.0'} try: response = requests.post(uri, data=payload, headers=headers, verify=self.client.verify) except Exception: logger.exception('Failure during bitpay refund.') raise PaymentException(_('We had trouble communicating with BitPay. Please try again and get in touch ' 'with us if this problem persists.')) if not response.ok: e = response.json()['error'] logger.exception('Failure during bitpay refund: {}'.format(e)) raise PaymentException(_('BitPay reported an error: {}').format(e))