def test_key( filename, testname, testdata, ): logger.debug('running test:%r in %r' % (testname, filename)) assert keys.check_keystore_json(testdata["json"]) privkey = keys.decode_keystore_json(testdata["json"], testdata["password"]) assert utils.encode_hex(privkey) == testdata["priv"]
def load(cls, path, password=None): """ :param path: :param password: :return: """ with open(path) as f: keystore = json.load(f) if not keys.check_keystore_json(keystore): raise ValueError('Invalid keystore file') return Account(keystore, password, path=path)
def load(cls, path, password=None): """Load an account from a keystore file. :param path: full path to the keyfile :param password: the password to decrypt the key file or `None` to leave it encrypted """ with open(path) as f: keystore = json.load(f) if not keys.check_keystore_json(keystore): raise ValueError('Invalid keystore file') return Account(keystore, password, path=path)
def load(cls, path, password=None): """Load an account from a keystore file. Args: path: full path to the keyfile password: the password to decrypt the key file or `None` to leave it encrypted """ with open(path) as f: keystore = json.load(f) if not keys.check_keystore_json(keystore): raise ValueError('Invalid keystore file') return Account(keystore, password, path=path)
def load_keyfile(keyfile, password): """加载钱包keyfile :param keyfile: :param password: 密码 :param filename: keyfile路径 """ with open(keyfile, 'r') as f: keystore = json.loads(f.read()) if not keys.check_keystore_json(keystore): raise ValueError('invalid keyfile format') return keys.decode_keystore_json(keystore, password)
def load_keyfile(keyfile, password): """Loads a private key from the given keyfile. :param str keyfile: keyfile path :param str password: keyfile password :returns: private key :rtype: str :raises: NotImplementedError: when using Python 3 :raises: IOError: if the file is not found :raises: ValueError: if the keyfile format is invalid """ if sys.version_info.major >= 3: raise NotImplementedError('keyfile usage is only supported in python2') with open(keyfile, 'r') as f: keystore = json.load(f) from ethereum.tools import keys as keys if not keys.check_keystore_json(keystore): raise ValueError('invalid keyfile format') return keys.decode_keystore_json(keystore, password)
def __init__(self, keyfile='', password='', private_key='', provider='', provider_endpoint_uri='http://159.89.240.147:8545', contract_address=KIN_CONTRACT_ADDRESS, contract_abi=KIN_ABI): """Create a new instance of the KIN SDK. The SDK needs a JSON-RPC provider, contract definitions and the wallet private key. The user may pass either a provider or a provider endpoint URI, in which case a default `web3:providers:HTTPProvider` will be created. If private_key is not provided, the SDK can still be used in "anonymous" mode with only the following functions available: - get_address_ether_balance - get_transaction_status - monitor_ether_transactions :param str private_key: a private key to initialize the wallet with. If either private key or keyfile are not provided, the wallet will not be initialized and methods needing the wallet will raise exception. :param str keyfile: the path to the keyfile to initialize to wallet with. Usually you will also need to supply a password for this keyfile. :param str password: a password for the keyfile. :param provider: JSON-RPC provider to work with. If not provided, a default `web3:providers:HTTPProvider` is used, inited with provider_endpoint_uri. :type provider: :class:`web3:providers:BaseProvider` :param str provider_endpoint_uri: a URI to use with a default HTTPProvider. If not provided, a default endpoint will be used. :param str contract_address: the address of the token contract. If not provided, a default KIN contract address will be used. :param dict contract_abi: The contract ABI. If not provided, a default KIN contract ABI will be used. :returns: An instance of the SDK. :rtype: :class:`~kin.TokenSDK` :raises: :class:`~kin.exceptions.SdkConfigurationError` if some of the configuration parameters are invalid. """ if not provider and not provider_endpoint_uri: raise SdkConfigurationError( 'either provider or provider endpoint must be provided') if not contract_address: raise SdkConfigurationError('token contract address not provided') try: validate_address(contract_address) except ValueError: raise SdkConfigurationError('invalid token contract address') if not contract_abi: raise SdkConfigurationError('token contract abi not provided') try: validate_abi(contract_abi) except Exception as e: raise SdkConfigurationError('invalid token contract abi: ' + str(e)) if provider: self.web3 = Web3(provider) else: self.web3 = Web3(HTTPProvider(provider_endpoint_uri)) if not self.web3.isConnected(): raise SdkConfigurationError('cannot connect to provider endpoint') self.token_contract = self.web3.eth.contract( contract_address, abi=contract_abi, ContractFactoryClass=Contract) self.private_key = None self.address = None if keyfile: with open(keyfile, 'r') as f: try: keystore = json.load(f) except Exception as e: raise SdkConfigurationError( 'invalid json in keystore file') from ethereum.tools import keys as ekeys if not ekeys.check_keystore_json(keystore): raise SdkConfigurationError('invalid keystore file') try: self.private_key = ekeys.decode_keystore_json( keystore, password) except ValueError as e: raise SdkConfigurationError('keyfile decode error: ' + str(e)) elif private_key: self.private_key = private_key if self.private_key: try: private_key_bytes = hexstr_if_str(to_bytes, self.private_key) pk = keys.PrivateKey(private_key_bytes) except ValidationError as e: raise SdkConfigurationError('cannot load private key: ' + str(e)) self.address = self.web3.eth.defaultAccount = pk.public_key.to_checksum_address( ) # monitoring filters self._pending_tx_filter = None self._new_block_filter = None