def checkd_data_sig(self,sig,pre,pk): sec, compressed = self.keypair.get(pk) pre_hash = Hash(pre) pkey = regenerate_key(sec) secexp = pkey.secret private_key = MySigningKey.from_secret_exponent(secexp, curve=ecdsa.SECP256k1) public_key = private_key.get_verifying_key() print("Data signature ok:") print(public_key.verify_digest(sig[:-1], pre_hash, sigdecode=ecdsa.util.sigdecode_der))
def get_transaction_signature(self, tx, sk, vk): txin = list(filter(lambda x: vk in x['pubkeys'], tx.inputs())) if txin: tx_num = tx.inputs().index(txin[0]) pre_hash = Hash(bfh(tx.serialize_preimage(tx_num))) private_key = MySigningKey.from_secret_exponent(sk.secret, curve = SECP256k1) public_key = private_key.get_verifying_key() sig = private_key.sign_digest_deterministic(pre_hash, hashfunc=hashlib.sha256, sigencode = ecdsa.util.sigencode_der) assert public_key.verify_digest(sig, pre_hash, sigdecode = ecdsa.util.sigdecode_der) result = bh2u(sig) + int_to_hex(tx.nHashType() & 255, 1) return result.encode('utf-8') return b''
def get_transaction_signature(transaction, inputs, secret_keys): "get transaction signature" signatures = {} for txin in transaction.inputs(): pubkey = txin['pubkeys'][0] if pubkey in inputs: tx_num = transaction.inputs().index(txin) pre_hash = Hash(bfh(transaction.serialize_preimage(tx_num))) private_key = MySigningKey.from_secret_exponent(secret_keys[pubkey].secret, curve=SECP256k1) public_key = private_key.get_verifying_key() sig = private_key.sign_digest_deterministic(pre_hash, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_der) assert public_key.verify_digest(sig, pre_hash, sigdecode=ecdsa.util.sigdecode_der) signatures[txin['tx_hash'] + ":" + str(txin['tx_pos'])] = (bh2u(sig) + int_to_hex(transaction.nHashType() & 255, 1)).encode('utf-8') return signatures