def test_wif_key(self): priv_key1 = Secp256k1PrivateKey.from_wif(KEY1_PRIV_WIF) self.assertEqual(priv_key1.get_algorithm_name(), "secp256k1") self.assertEqual(priv_key1.as_hex(), KEY1_PRIV_HEX) priv_key2 = Secp256k1PrivateKey.from_wif(KEY2_PRIV_WIF) self.assertEqual(priv_key2.get_algorithm_name(), "secp256k1") self.assertEqual(priv_key2.as_hex(), KEY2_PRIV_HEX)
def __init__(self, base_url, keyfile=None): self._base_url = base_url if keyfile is None: self._signer = None return try: with open(keyfile) as fd: private_key_str = fd.read().strip() except OSError as err: raise XoException('Failed to read private key {}: {}'.format( keyfile, str(err))) try: private_key = Secp256k1PrivateKey.from_hex(private_key_str) except ParseError: try: private_key = Secp256k1PrivateKey.from_wif(private_key_str) except ParseError as e: raise XoException('Unable to load private key: {}'.format( str(e))) self._signer = CryptoFactory(create_context('secp256k1')) \ .new_signer(private_key)
def _read_signer(key_filename): """Reads the given file as a hex, or (as a fallback) a WIF formatted key. Args: key_filename: The filename where the key is stored. If None, defaults to the default key for the current user. Returns: Signer: the signer Raises: CliException: If unable to read the file. """ filename = key_filename if filename is None: filename = os.path.join(config.get_key_dir(), 'validator.priv') try: with open(filename, 'r') as key_file: signing_key = key_file.read().strip() except IOError as e: raise CliException('Unable to read key file: {}'.format(str(e))) try: private_key = Secp256k1PrivateKey.from_hex(signing_key) except ParseError: try: private_key = Secp256k1PrivateKey.from_wif(signing_key) except ParseError: raise CliException('Unable to read key in file: {}'.format(str(e))) context = create_context('secp256k1') crypto_factory = CryptoFactory(context) return crypto_factory.new_signer(private_key)
def setUpClass(cls): super().setUpClass() context = create_context('secp256k1') private_key = Secp256k1PrivateKey.from_wif(PRIVATE) signer = CryptoFactory(context).new_signer(private_key) cls.factory = ValidatorRegistryMessageFactory(signer=signer)
def __init__(self, base_url, keyfile, wait=None): """ Member variables: _base_url _private_key _public_key _transaction_family _family_version _wait """ self._base_url = base_url try: with open(keyfile) as fd: private_key_str = fd.read().strip() except OSError as err: raise IOError("Failed to read keys: {}.".format(str(err))) try: private_key = Secp256k1PrivateKey.from_hex(private_key_str) except ParseError: try: private_key = Secp256k1PrivateKey.from_wif(private_key_str) except ParseError as e: raise BattleshipException( 'Unable to load private key: {}'.format(str(e))) self._signer = CryptoFactory( create_context('secp256k1')).new_signer(private_key) self._transaction_family = "battleship" self._family_version = "1.0" self._wait = wait
def load_identity_signer(key_dir, key_name): """Loads a private key from the key directory, based on a validator's identity. Args: key_dir (str): The path to the key directory. key_name (str): The name of the key to load. Returns: Signer: the cryptographic signer for the key """ key_path = os.path.join(key_dir, '{}.priv'.format(key_name)) if not os.path.exists(key_path): raise LocalConfigurationError( "No such signing key file: {}".format(key_path)) if not os.access(key_path, os.R_OK): raise LocalConfigurationError( "Key file is not readable: {}".format(key_path)) LOGGER.info('Loading signing key: %s', key_path) try: with open(key_path, 'r') as key_file: private_key_str = key_file.read().strip() except IOError as e: raise LocalConfigurationError("Could not load key file: {}".format( str(e))) try: private_key = Secp256k1PrivateKey.from_hex(private_key_str) except signing.ParseError: try: private_key = Secp256k1PrivateKey.from_wif(private_key_str) except signing.ParseError: raise LocalConfigurationError("Invalid key in file {}: {}".format( key_path, str(e))) context = signing.create_context('secp256k1') crypto_factory = CryptoFactory(context) return crypto_factory.new_signer(private_key)
def __init__(self, url, keyfile=None): self.url = url if keyfile is not None: try: with open(keyfile) as fd: private_key_str = fd.read().strip() fd.close() except OSError as err: raise IntkeyClientException( 'Failed to read private key: {}'.format(str(err))) try: private_key = Secp256k1PrivateKey.from_hex(private_key_str) except ParseError: try: private_key = Secp256k1PrivateKey.from_wif(private_key_str) except ParseError as e: raise IntkeyClientException( 'Unable to load private key: {}'.format(str(e))) self._signer = CryptoFactory( create_context('secp256k1')).new_signer(private_key)