def get_signatures_from_script(script): """Returns a list of signatures retrieved from the provided (partially) signed multisig scriptSig. :param script: The partially-signed multisig scriptSig. :type script: ``bytes`` :returns: A list of retrieved signature from the provided scriptSig. :rtype: A ``list`` of ``bytes`` signatures """ script = script[1:] # remove the first OP_0 sigs = [] while len(script) > 0: # Consume script while extracting possible signatures: val, script = read_var_int(script) potential_sig, script = read_bytes(script, val) try: # Raises ValueError if argument to `der_to_cdata` is not a # DER-encoding of a signature (without the appended SIGHASH). der_to_cdata(potential_sig[:-1]) # We only add if DER-encoded signature: sigs.append(potential_sig) except ValueError: pass return sigs
def verify(self, signature, message, hasher=sha256): msg_hash = hasher(message) if hasher is not None else message if len(msg_hash) != 32: raise ValueError('Message hash must be 32 bytes long.') verified = lib.secp256k1_ecdsa_verify(self.context.ctx, der_to_cdata(signature), msg_hash, self.public_key) # A performance hack to avoid global bool() lookup. return not not verified
def verify(self, signature: bytes, message: bytes, hasher: Hasher = sha256) -> bool: """ :param signature: The ECDSA signature. :param message: The message that was supposedly signed. :param hasher: The hash function to use, which must return 32 bytes. By default, the `sha256` algorithm is used. If `None`, no hashing occurs. :return: A boolean indicating whether or not the signature is correct. :raises ValueError: If the message hash was not 32 bytes long or the DER-encoded signature could not be parsed. """ msg_hash = hasher(message) if hasher is not None else message if len(msg_hash) != 32: raise ValueError('Message hash must be 32 bytes long.') verified = lib.secp256k1_ecdsa_verify(self.context.ctx, der_to_cdata(signature), msg_hash, self.public_key) # A performance hack to avoid global bool() lookup. return not not verified
def test_der(): assert cdata_to_der(der_to_cdata(SIGNATURE)) == SIGNATURE
def sign_message(self, msg_bytes): # check if ledger wallet sig = self._pk.sign(msg_bytes) return serialize_compact(raw_sig=der_to_cdata(der=sig))