use libsecp256k1 if available. abstract away ecc stuff. move symmetric crypto and hash functions to crypto.py
This commit is contained in:
@@ -31,6 +31,7 @@ from typing import Sequence, Union
|
||||
|
||||
from .util import print_error, profiler
|
||||
|
||||
from . import ecc
|
||||
from . import bitcoin
|
||||
from .bitcoin import *
|
||||
import struct
|
||||
@@ -653,18 +654,18 @@ class Transaction:
|
||||
if sig in sigs1:
|
||||
continue
|
||||
pre_hash = Hash(bfh(self.serialize_preimage(i)))
|
||||
# der to string
|
||||
order = ecdsa.ecdsa.generator_secp256k1.order()
|
||||
r, s = ecdsa.util.sigdecode_der(bfh(sig[:-2]), order)
|
||||
sig_string = ecdsa.util.sigencode_string(r, s, order)
|
||||
compressed = True
|
||||
sig_string = ecc.sig_string_from_der_sig(bfh(sig[:-2]))
|
||||
for recid in range(4):
|
||||
public_key = MyVerifyingKey.from_signature(sig_string, recid, pre_hash, curve = SECP256k1)
|
||||
pubkey = bh2u(point_to_ser(public_key.pubkey.point, compressed))
|
||||
if pubkey in pubkeys:
|
||||
public_key.verify_digest(sig_string, pre_hash, sigdecode = ecdsa.util.sigdecode_string)
|
||||
j = pubkeys.index(pubkey)
|
||||
print_error("adding sig", i, j, pubkey, sig)
|
||||
try:
|
||||
public_key = ecc.ECPubkey.from_sig_string(sig_string, recid, pre_hash)
|
||||
except ecc.InvalidECPointException:
|
||||
# the point might not be on the curve for some recid values
|
||||
continue
|
||||
pubkey_hex = public_key.get_public_key_hex(compressed=True)
|
||||
if pubkey_hex in pubkeys:
|
||||
public_key.verify_message_hash(sig_string, pre_hash)
|
||||
j = pubkeys.index(pubkey_hex)
|
||||
print_error("adding sig", i, j, pubkey_hex, sig)
|
||||
self.add_signature_to_txin(self._inputs[i], j, sig)
|
||||
#self._inputs[i]['x_pubkeys'][j] = pubkey
|
||||
break
|
||||
@@ -1067,7 +1068,7 @@ class Transaction:
|
||||
if x_pubkey in keypairs.keys():
|
||||
print_error("adding signature for", x_pubkey)
|
||||
sec, compressed = keypairs.get(x_pubkey)
|
||||
pubkey = public_key_from_private_key(sec, compressed)
|
||||
pubkey = ecc.ECPrivkey(sec).get_public_key_hex(compressed=compressed)
|
||||
# add signature
|
||||
sig = self.sign_txin(i, sec)
|
||||
self.add_signature_to_txin(txin, j, sig)
|
||||
@@ -1079,13 +1080,8 @@ class Transaction:
|
||||
|
||||
def sign_txin(self, txin_index, privkey_bytes):
|
||||
pre_hash = Hash(bfh(self.serialize_preimage(txin_index)))
|
||||
pkey = regenerate_key(privkey_bytes)
|
||||
secexp = pkey.secret
|
||||
private_key = bitcoin.MySigningKey.from_secret_exponent(secexp, 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)
|
||||
if not public_key.verify_digest(sig, pre_hash, sigdecode=ecdsa.util.sigdecode_der):
|
||||
raise Exception('Sanity check verifying our own signature failed.')
|
||||
privkey = ecc.ECPrivkey(privkey_bytes)
|
||||
sig = privkey.sign_transaction(pre_hash)
|
||||
sig = bh2u(sig) + '01'
|
||||
return sig
|
||||
|
||||
|
||||
Reference in New Issue
Block a user