def __init__(self, shell=None, key=None): if shell is None: shell = default_shell if isinstance(shell, str): networks = { 'mainnet': mainnet, 'babylonnet': babylonnet, 'zeronet': zeronet, 'sandboxnet': localhost.sandboxnet } if shell in networks: self.shell = networks[shell] else: self.shell = ShellQuery(node=RpcNode(uri=shell)) elif isinstance(shell, ShellQuery): self.shell = shell else: raise NotImplementedError(shell) if key is None: key = default_key if isinstance(key, str): if is_key(key): self.key = Key.from_encoded_key(key) elif exists(expanduser(key)): self.key = Key.from_faucet(key) else: self.key = Key.from_alias(key) elif isinstance(key, Key): self.key = key else: raise NotImplementedError(key)
def test_deterministic_signatures(self, sk, msg, sig): """ See RFC6979 for explanation https://tools.ietf.org/html/rfc6979#section-3.2 """ key = Key(sk) signature = key.sign(msg) self.assertEqual(sig, signature)
def sign(self, key): if isinstance(key, str): key = Key(key) if not isinstance(key, Key): raise ValueError('Base58 encoded secret key or Key instance required.') self._data['signature'] = key.sign(self.unsigned_bytes(), generic=True) return self._data['signature']
def test_derive_key_data(self, sk, pk, hash): public_key = Key.from_encoded_key(pk) self.assertFalse(public_key.is_secret) self.assertEqual(pk, public_key.public_key()) self.assertEqual(hash, public_key.public_key_hash()) secret_key = Key.from_encoded_key(sk) self.assertTrue(secret_key.is_secret) self.assertEqual(pk, secret_key.public_key()) self.assertEqual(sk, secret_key.secret_key())
def get_key(self, name) -> Key: self.reload() value = next(item['value'] for item in self._secret_keys if item['name'] == name) prefix, key = value.split(':', maxsplit=1) if prefix == 'encrypted': password = getpass(f'Please, enter passphrase for `{name}`:\n') key = Key(key, passphrase=password) else: key = Key(key) return key
def key(self, message): key = Key.from_alias("fy", tezos_client_dir=".") pk = key.public_key() pkh = key.public_key_hash() content = code_notice(f"pkh = {pkh}") return [content]
def __init__(self, shell=None, key=None): if shell is None: shell = default_shell if isinstance(shell, str): networks = { 'mainnet': mainnet, 'babylonnet': babylonnet, 'carthagenet': carthagenet, 'zeronet': zeronet, 'sandboxnet': localhost.sandboxnet, 'bbbox': localhost.bbbox, 'labnet': labnet, 'mainnet-pool': pool.mainnet } if shell in networks: self.shell = networks[shell] else: self.shell = ShellQuery(node=RpcNode(uri=shell)) elif isinstance(shell, ShellQuery): self.shell = shell else: raise NotImplementedError(shell) if key is None: key = default_key if is_installed() else default_key_hash if isinstance(key, str): keys = { 'alice': alice_key } if key in keys: self.key = keys[key] elif is_key(key): self.key = Key.from_encoded_key(key) elif is_pkh(key): self.key = KeyHash(key) elif exists(expanduser(key)): self.key = Key.from_faucet(key) else: self.key = Key.from_alias(key) elif isinstance(key, Key): self.key = key else: raise NotImplementedError(key)
def __init__(self, key, interval=5, shell=mainnet): """ :param key: secret key (encrypted/unencrypted), public key or public key hash, all base58 encoded :param interval: number of blocks to check (tolerance) :param shell: ShellQuery instance """ if not isinstance(key, Key): if is_pkh(key): key = shell.public_key(key) key = Key.from_encoded_key(key) self._key = key self._interval = interval self._shell = shell
def address(self): return Key.from_encoded_key(self.public_key).public_key_hash()
def test_sign_and_verify(self, sk, msg): key = Key.from_encoded_key(sk) sig = key.sign(msg) key.verify(sig, msg) self.assertRaises(ValueError, key.verify, sig, b'fake')
def test_verify_ext_signatures(self, pk, msg, sig): key = Key.from_encoded_key(pk) key.verify(sig, msg) self.assertRaises(ValueError, key.verify, sig, b'fake')
import logging from os.path import join, dirname from unittest import TestCase from decimal import Decimal from pytezos.crypto import Key from pytezos import ContractInterface, MichelsonRuntimeError for handler in logging.root.handlers[:]: logging.root.removeHandler(handler) logging.basicConfig(filename='token_log.log', level=logging.DEBUG) alice = Key.from_encoded_key( "edskS3LMw4BFQjbutGWtLG9MtPhcoSsR6BfkLzrHhf5ZB36TfVqQsDxUXkrLaj6tULpoSnyWDF5HsNZvLJv2wL7W6YAahZPqwa" ) bob = Key.from_encoded_key( "edskRqA1izJkgC8BAzXSnbhtcJdmZqN9YLDHLm7t4SLcVfds4hNGESLK6MH4Fvi8V4sviwMLyDbLRSenQscR4vJUMcoCAT7Pjm" ) alice_address = alice.public_key_hash() bob_address = bob.public_key_hash() total_supply = 1000 total_staked = 0 reward_per_share = 0 last_update_time = 0 initial_storage = { "totalSupply": total_supply, "totalStaked": total_staked, "rewardPerShare": reward_per_share, "lastUpdateTime": last_update_time,
def test_regression_p256_short_sig(self): key = Key.from_encoded_key( 'p2sk3xPfYsoExTVi7bGSH2KoHgpxFNqewUczHkLtQvr1bwnbhzGM9Y') key.sign('try25')
def test_encrypted_keys(self, sk, passphrase, salt, pk): key = Key.from_encoded_key(sk, passphrase=passphrase) self.assertEqual(pk, key.public_key()) with patch('pytezos.crypto.pysodium.randombytes', return_value=salt): self.assertEqual(sk, key.secret_key(passphrase))
def verify_signature(self): pk = self.get_public_key(self.metadata.get('baker')) Key(pk).verify(self.header.get('signature'), self.header.unsigned_bytes())
def sign(self, message): key = Key.from_alias("fy", tezos_client_dir=".") signature = key.sign(message["body"]) content = code_notice(pprint.pformat(signature)) return [content]
def get_tezos_key_object(public_key): try: return Key.from_encoded_key(public_key) except ValueError as e: print("Something is wrong with the public_key provided:", e) abort(401)
def verify_signature(self): pk = self.get_public_key(self.source()) Key(pk).verify(self.get('signature'), self.unsigned_bytes())