Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
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
def secp256k1_secret_key_from(seed):
"""
Calculate a valid secp256k1 secret key by hashing a seed value;
if the result isn't a valid key, increment a seq value and try
again.
Returns a secret key as a 32-byte integer.
"""
seq = 0
while True:
buf = seed + seq.to_bytes(4, byteorder="big", signed=False)
h = sha512half(buf)
h_i = int.from_bytes(h, byteorder="big", signed=False)
if h_i < curve.secp256k1.q and h_i != 0:
return h_i
# Else, not a valid secp256k1 key; try again with a new sequence value.
seq += 1