Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def sign_raw_data(raw_data, privatekey_hex):
"""
Sign a string of data.
Returns signature as a base64 string
"""
# force uncompressed
priv = str(privatekey_hex)
if len(priv) > 64:
assert priv[-2:] == '01'
priv = priv[:64]
pk_i = int(priv, 16)
sig_r, sig_s = fastecdsa.ecdsa.sign(raw_data, pk_i, curve=fastecdsa.curve.secp256k1)
# enforce low-s
if sig_s * 2 >= fastecdsa.curve.secp256k1.q:
log.debug("High-S to low-S")
sig_s = fastecdsa.curve.secp256k1.q - sig_s
sig_bin = '{:064x}{:064x}'.format(sig_r, sig_s).decode('hex')
assert len(sig_bin) == 64
sig_b64 = base64.b64encode(sig_bin)
return sig_b64
"""
if self.is_coinbase():
return
for vin in self.Vin:
if not prev_txs[vin.txid].ID:
raise ValueError(
"Error: previous transaction is not correct."
)
tx_copy = self.trimmed_copy()
for inId, vin in enumerate(tx_copy.Vin):
prev_tx = prev_txs[vin.txid]
tx_copy.Vin[inId].signature = ""
tx_copy.Vin[inId].pubkey = prev_tx.Vout[vin.vout].pub_key_hash
r, s = ecdsa.sign(tx_copy, priv_key)
signature = "".join([str(r), str(s)])
self.Vin[inId].signature = signature
tx_copy.Vin[inId].pubkey = ""
def sign_msg(msg: str, priv_key: str) -> str:
'''
Given a message, and the private key,
return the signature (in hex). x, and y are joined
with 'x'
'''
r, s = ecdsa.sign(msg, int(priv_key, 16))
return '{:x}x{:x}'.format(r, s)