Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def ecdsaSECP256k1(self, digest):
# SECP256k1 - Bitcoin elliptic curve
sk = ecdsa.SigningKey.from_string(digest, curve=ecdsa.SECP256k1)
return sk.get_verifying_key()
def sign_ECDSA_msg(private_key):
"""Sign the message to be sent
private_key: must be hex
return
signature: base64 (to make it shorter)
message: str
"""
# Get timestamp, round it, make it into a string and encode it to bytes
message = str(round(time.time()))
bmessage = message.encode()
sk = ecdsa.SigningKey.from_string(bytes.fromhex(private_key), curve=ecdsa.SECP256k1)
signature = base64.b64encode(sk.sign(bmessage))
return signature, message
print verify_message(bitcoinaddress, sig, 'test message')
print verify_message('1GdKjTSg2eMyeVvPV5Nivo6kR8yP2GT7wF',
'GyMn9AdYeZIPWLVCiAblOOG18Qqy4fFaqjg5rjH6QT5tNiUXLS6T2o7iuWkV1gc4DbEWvyi8yJ8FvSkmEs3voWE=',
'freenode:#bitcoin-otc:b42f7e7ea336db4109df6badc05c6b3ea8bfaa13575b51631c5178a7')
print verify_message('1Hpj6xv9AzaaXjPPisQrdAD2tu84cnPv3f',
'INEJxQnSu6mwGnLs0E8eirl5g+0cAC9D5M7hALHD9sK0XQ66CH9mas06gNoIX7K1NKTLaj3MzVe8z3pt6apGJ34=',
'testtest')
print verify_message('18uitB5ARAhyxmkN2Sa9TbEuoGN1he83BX',
'IMAtT1SjRyP6bz6vm5tKDTTTNYS6D8w2RQQyKD3VGPq2i2txGd2ar18L8/nvF1+kAMo5tNc4x0xAOGP0HRjKLjc=',
'testtest')
# sign compressed key
compressed = True
secret = 'dea7715ddcf5aba27530d6a1393813fbdd09af3aeb5f4f1616f563833d07babb'.decode('hex')
private_key = ecdsa.SigningKey.from_string( secret, curve = SECP256k1 )
public_key = private_key.get_verifying_key()
bitcoinaddress = public_key_to_bc_address( encode_point(public_key, compressed) )
print bitcoinaddress
sig = sign_message(private_key, 'test message', compressed)
print sig
print verify_message(bitcoinaddress, sig, 'test message')
print verify_message('1LsPb3D1o1Z7CzEt1kv5QVxErfqzXxaZXv',
'H3I37ur48/fn52ZvWQT+Mj2wXL36gyjfaN5qcgfiVRTJb1eP1li/IacCQspYnUntiRv8r6GDfJYsdiQ5VzlG3As=',
'testtest')
def get_pubkeys_from_secret(secret):
# public key
private_key = ecdsa.SigningKey.from_string( secret, curve = SECP256k1 )
public_key = private_key.get_verifying_key()
K = public_key.to_string()
K_compressed = GetPubKey(public_key.pubkey,True)
return K, K_compressed
def ecdsaSECP256k1(self, digest):
# SECP256k1 - Bitcoin elliptic curve
sk = ecdsa.SigningKey.from_string(digest, curve=ecdsa.SECP256k1)
return sk.get_verifying_key()
def get_public_key(private_key):
# this returns the concatenated x and y coordinates for the supplied private address
# the prepended 04 is used to signify that it's uncompressed
return (bytes.fromhex("04") + SigningKey.from_string(private_key, curve=SECP256k1).verifying_key.to_string())
ndata[0] += 1
privkey = secp256k1.PrivateKey(p, raw=True)
sig = secp256k1.ffi.new(
'secp256k1_ecdsa_recoverable_signature *')
signed = secp256k1.lib.secp256k1_ecdsa_sign_recoverable(
privkey.ctx, sig, self.digest, privkey.private_key,
secp256k1.ffi.NULL, ndata)
assert signed == 1
signature, i = privkey.ecdsa_recoverable_serialize(sig)
if self._is_canonical(signature):
i += 4
i += 27
break
else:
cnt = 0
sk = ecdsa.SigningKey.from_string(p, curve=ecdsa.SECP256k1)
while 1:
cnt += 1
if not cnt % 20:
print("Still searching for a canonical signature. "
"Tried %d times already!" % cnt)
k = ecdsa.rfc6979.generate_k(
sk.curve.generator.order(),
sk.privkey.secret_multiplier,
hashlib.sha256,
hashlib.sha256(
self.digest + struct.pack("d", time.time(
))
).digest())
sigder = sk.sign_digest(
referenced_utxo = find_unspent_tx_out(tx_in.tx_out_id, tx_in.tx_out_index, a_unspent_tx_outs)
if referenced_utxo is None:
print('could not find referenced txOut')
# throw Error()
return False
referenced_address = referenced_utxo.address
if get_public_key(private_key) != referenced_address:
print('trying to sign an input with private' +
' key that does not match the address that is referenced in tx_in')
# throw Error()
return False
# key = ec.keyFromPrivate(private_key, 'hex')
sk = SigningKey.from_string(private_key, curve=SECP256k1)
signature = binascii.b2a_hex(sk.sign(data_to_sign.encode())).decode()
return signature
def sign(self, private_key):
data_to_sign = SHA256.new(str(self.id).encode()).digest()
sk = SigningKey.from_string(bytes.fromhex(private_key), curve=SECP256k1)
self.signature = binascii.b2a_hex(sk.sign(data_to_sign)).decode()
Load the keys from wallet json file.
:param pos_filename:
:param verbose: boolean, print out address and pubkey
:return: True or raise.
"""
global PUB_KEY
global PRIV_KEY
global ADDRESS
if not os.path.exists(pos_filename):
# Create new keys if none there.
gen_keys_file(pos_filename)
with open(pos_filename, 'r') as fp:
wallet = json.load(fp)
# TODO: handle encrypted wallet
PRIV_KEY = SigningKey.from_string(b64decode(wallet['privkey'].encode('ascii')), curve=SECP256k1)
# TODO: We could verify pubkey also, and warn if there is an error
PUB_KEY = VerifyingKey.from_string(b64decode(wallet['pubkey'].encode('ascii')), curve=SECP256k1)
# We recreate address rather than relying on address.txt
ADDRESS = pub_key_to_addr(PUB_KEY.to_string())
assert ADDRESS == wallet['address']
if verbose:
print("Loaded address ", ADDRESS)
print("Loaded pubkey ", raw_to_hex(PUB_KEY.to_string()))
return True