def verify_tokens(self): """ GET /tokens https://bitpay.com/api#resource-Tokens """ xidentity = key_utils.get_compressed_public_key_from_pem(self.pem) 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 create_invoice(self, params): """ POST /invoices https://bitpay.com/api#resource-Invoices """ 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 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 verify_tokens(self): xidentity = key_utils.get_compressed_public_key_from_pem(self.pem) 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 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)