How to convert signature to DER encoded format in python

How to convert signature to DER encoded format in python

Manage alerts

Loading saved threads...

aaravm · External communityPost link
External question — Bitcoin Stack Exchange Author: aaravm Original post: https://bitcoin.stackexchange.com/questions/122597 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. vk = ecdsa.VerifyingKey.from_string(bytes.fromhex(pubKey),curve=ecdsa.SECP256k1) if vk.verify(bytes.fromhex(signature), bytes.fromhex(sha256_hash), hashlib.sha256, sigdecode=sigdecode_der) == True: # True print("Signature is Valid") else: print("Signature is not Valid") I am trying to verify a transaction using secp256k1_generator, but I can't seem to get the signature into the correct (r,s) format, and am getting the following output File "/.local/lib/python3.10/site-packages/ecdsa/keys.py", line 741, in verify_digest raise BadSignatureError("Signature verification failed") ecdsa.keys.BadSignatureError: Signature verification failed I have taken help of Signature verification in python using compressed public key and https://stackoverflow.com/questions/73630540/how-to-verify-ecdsa-signatures-with-python But still I am getting this error. How can I resolve it?
Quote
Report
kenadams15 · External communityPost link
External answer — Bitcoin Stack Exchange Author: kenadams15 Original post: https://bitcoin.stackexchange.com/a/122601 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Are you sure that the r and s that you have generated are correct? If yes converting this (r,s) to DER can be done in this way. def der(self): rbin = self.r.to_bytes(32, byteorder='big') # remove all null bytes at the beginning rbin = rbin.lstrip(b'\x00') # if rbin has a high bit, add a \x00 if rbin[0] & 0x80: rbin = b'\x00' + rbin result = bytes([2, len(rbin)]) + rbin # <1> sbin = self.s.to_bytes(32, byteorder='big') # remove all null bytes at the beginning sbin = sbin.lstrip(b'\x00') # if sbin has a high bit, add a \x00 if sbin[0] & 0x80: sbin = b'\x00' + sbin result += bytes([2, len(sbin)]) + sbin return bytes([0x30, len(result)]) + result here self.r and self.s are integers previously generated
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External answer — Bitcoin Stack Exchange Author: kenadams15 Source score (net votes, not local likes): 0 Original post: https://bitcoin.stackexchange.com/a/122601 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Are you sure that the r and s that you have generated are correct? If yes converting this (r,s) to DER can be done in this way. def der(self): rbin = self.r.to_bytes(32, byteorder='big') # remove all null bytes at the beginning rbin = rbin.lstrip(b'\x00') # if rbin has a high bit, add a \x00 if rbin[0] & 0x80: rbin = b'\x00' + rbin result = bytes([2, len(rbin)]) + rbin # <1> sbin = self.s.to_bytes(32, byteorder='big') # remove all null bytes at the beginning sbin = sbin.lstrip(b'\x00') # if sbin has a high bit, add a \x00 if sbin[0] & 0x80: sbin = b'\x00' + sbin result += bytes([2, len(sbin)]) + sbin return bytes([0x30, len(result)]) + result here self.r and self.s are integers previously generated

Cancel quote

Checking account access…