Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sign_hash(hash,privkey):
- try:
- import ecdsa
- k_value = int(ecdsa.rfc6979.generate_k(ecdsa.ecdsa.ellipticcurve.Point(ecdsa.ecdsa.curve_secp256k1,ecdsa.ecdsa._Gx,ecdsa.ecdsa._Gy,ecdsa.ecdsa._r),int(privkey,16),hashlib.sha256,hash))
- except:
- k_value = int(double_sha256(os.urandom(32)),16)
- r = int(str(privkey_to_pubkey(k_value,output_compressed=True))[2:],16) % N_ORDER
- r = str(hex(r)).replace("0x","").replace("L","").zfill(64)
- assert len(r) == 64
- s = ((int(hash,16) + (int(r,16) * int(privkey,16))) * (ec_modular_inverse(k_value,N_ORDER))) % N_ORDER
- s = str(hex(s)).replace("0x","").replace("L","").zfill(64)
- assert len(s) == 64
- if int(r[:2],16) > 127:
- r = str(str("00") + str(r))
- assert len(r) == 66
- if int(s[:2],16) > 127:
- s = str(str("00") + str(s))
- assert len(s) == 66
- if len(r) == 66:
- r_prefix = str("0221")
- else:
- r_prefix = str("0220")
- if len(s) == 66:
- s_prefix = str("0221")
- else:
- s_prefix = str("0220")
- finalsig = r_prefix + r + s_prefix + s
- len_byte = str(hex(int(len(finalsig) // 2))).replace("0x","").replace("L","").zfill(2)
- assert len(len_byte) == 2
- finalsig = str("30") + len_byte + finalsig # Does NOT include 0x01 SIGHASH_ALL postfix
- return str(finalsig)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement