Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from copy import deepcopy
from ecdsa import SigningKey, SECP256k1
from mybankcoin import Transfer, Bank
from utils import serialize
# The usual suspects
bob_private_key = SigningKey.generate(curve=SECP256k1)
alice_private_key = SigningKey.generate(curve=SECP256k1)
bob_public_key = bob_private_key.get_verifying_key()
alice_public_key = alice_private_key.get_verifying_key()
def test_valid_transfers():
# Issue a coin and transfer it twice
# Bank issues coins to Alice
bank = Bank()
coin = bank.issue(alice_public_key)
initial_coin_copy = deepcopy(coin)
assert bank.fetch_coins(alice_public_key) == [initial_coin_copy]
assert bank.fetch_coins(bob_public_key) == []
# Alice constructs transfer to Bob, but doesn't tell the bank
def lookup_private_key(name):
exponent = {
"alice": 1, "bob": 2, "node0": 3, "node1": 4, "node2": 5
}[name]
return SigningKey.from_secret_exponent(exponent, curve=SECP256k1)
def validate_signature(public_key, signature, message):
"""Verifies if the signature is correct. This is used to prove
it's you (and not someone else) trying to do a transaction with your
address. Called when a user tries to submit a new transaction.
"""
public_key = (base64.b64decode(public_key)).hex()
signature = base64.b64decode(signature)
vk = ecdsa.VerifyingKey.from_string(bytes.fromhex(public_key), curve=ecdsa.SECP256k1)
# Try changing into an if/else statement as except is too broad.
try:
return vk.verify(signature, message.encode())
except:
return False
def sign_vote(votestr, mnprivkey):
privatekey = utils.wifToPrivateKey(mnprivkey)
signingkey = Bip62SigningKey.from_string(
privatekey.decode('hex'),
curve=ecdsa.SECP256k1)
public_key = signingkey.get_verifying_key()
key = Key.from_text(mnprivkey)
address = key.address(use_uncompressed=True)
msghash = utils.double_sha256(utils.msg_magic(votestr))
signature = signingkey.sign_digest_deterministic(
msghash,
hashfunc=hashlib.sha256,
sigencode=ecdsa.util.sigencode_string)
assert public_key.verify_digest(
signature,
msghash,
sigdecode=ecdsa.util.sigdecode_string)
for i in range(4):
sig = base64.b64encode(chr(27+i) + signature)
if verify_dash_signature(generator_secp256k1, address, msghash, sig):
return sig
data_hash = hashlib.sha256(hashlib.sha256(key_bytes).digest()).digest()
if not checksum == data_hash[0:4]:
raise ValueError('Checksum is wrong. Possible mistype?')
# get network prefix and check with current setup
network_prefix = key_bytes[:1]
if NETWORK_WIF_PREFIXES[get_network()] != network_prefix:
raise ValueError('Using the wrong network!')
# remove network prefix
key_bytes = key_bytes[1:]
# check length of bytes and if > 32 then compressed
# use this to instantite an ecdsa key
if len(key_bytes) > 32:
self.key = SigningKey.from_string(key_bytes[:-1], curve=SECP256k1)
else:
self.key = SigningKey.from_string(key_bytes, curve=SECP256k1)
ecdh = int(header[0x108:0x188].rstrip('\0'), 16)
cofactor = ecdh/prod
if prod > ecdh:
return "Superfluous factors or incorrect factorization detected!"
if cofactor*prod != ecdh:
return "Error: factors don't divide either pubkey"
i = 1
while i < 1<
def get_signature(seq, validation_pk_human, validation_sk_human, master_sk_human):
v, validation_pk = Base58.decode_version(validation_pk_human)
check_validation_public_key(v, validation_pk)
v, validation_sk_str = Base58.decode_version(validation_sk_human)
check_secret_key(v, validation_sk_str)
validation_sk = ecdsa.SigningKey.from_string(validation_sk_str, curve=ecdsa.SECP256k1)
v, master_sk = Base58.decode_version(master_sk_human)
check_secret_key(v, master_sk)
pk = ed25519.publickey(master_sk)
apk = ED25519_BYTE + pk
m = make_manifest(apk, validation_pk, seq)
m1 = sign_manifest(m, validation_sk, master_sk, pk)
return base64.b64encode(m1)
def bank_private_key(id):
assert isinstance(id, int)
assert id >= 0
base = 1000 # So bank keys don't collide with user keys ...
return SigningKey.from_secret_exponent(base + id, curve=SECP256k1)
def derive_from_seed(self, offset):
""" Derive private key using "generate_from_seed" method.
Here, the key itself serves as a `seed`, and `offset`
is expected to be a sha256 digest.
"""
seed = int(hexlify(py23_bytes(self)).decode('ascii'), 16)
z = int(hexlify(offset).decode('ascii'), 16)
order = ecdsa.SECP256k1.order
secexp = (seed + z) % order
secret = "%0x" % secexp
return PrivateKey(secret, prefix=self.pubkey.prefix)