def test_get_private_key_writable_password_file(): """ get_private_key() should return None on a password path with wrong permissions """ with tempfile.NamedTemporaryFile() as keyfile: with tempfile.NamedTemporaryFile() as password_file: orig = os.stat(password_file.name).st_mode os.chmod(password_file.name, stat.S_IWGRP | orig) assert get_private_key(key_path=keyfile.name, password_path=password_file.name) is None
def __init__( self, rpc_url: URI, private_key: Path, password: Optional[Path] = None, wait: int = 10 ): self.web3 = Web3(HTTPProvider(rpc_url)) self.private_key = get_private_key(private_key, password) assert self.private_key is not None self.owner = private_key_to_address(self.private_key) self.wait = wait self.web3.middleware_onion.add(construct_sign_and_send_raw_middleware(self.private_key)) self.web3.eth.defaultAccount = self.owner # type: ignore self.web3.middleware_onion.inject(geth_poa_middleware, layer=0)
def main(rpc_url: URI, private_key: Path, token_address: ChecksumAddress, amount: int) -> None: web3 = Web3(HTTPProvider(rpc_url)) privkey = get_private_key(private_key) assert privkey is not None owner = private_key_to_address(privkey) web3.middleware_onion.add(construct_sign_and_send_raw_middleware(privkey)) token_code = web3.eth.get_code(token_address, "latest") assert token_code != HexBytes("") token_contract = ContractManager(contracts_precompiled_path()).get_contract( CONTRACT_CUSTOM_TOKEN ) token_proxy = web3.eth.contract(address=token_address, abi=token_contract["abi"]) tx_hash = token_proxy.functions.mint(amount).transact({"from": owner}) print(f"Minting tokens for address {owner}") print(f"Transaction hash {encode_hex(tx_hash)}") balance = token_proxy.functions.balanceOf(owner).call() print(f"Balance of {owner}: {balance}")
def setup_ctx( ctx: click.Context, private_key: str, password_file: Optional[Path], rpc_provider: URI, wait: int, gas_price: int, gas_limit: int, contracts_version: Optional[str] = None, ) -> None: """Set up deployment context according to common options (shared among all subcommands). """ logging.basicConfig(level=logging.DEBUG) logging.getLogger("web3").setLevel(logging.INFO) logging.getLogger("urllib3").setLevel(logging.INFO) web3 = Web3(HTTPProvider(rpc_provider, request_kwargs={"timeout": 60})) web3.middleware_onion.inject(geth_poa_middleware, layer=0) print("Web3 provider is", web3.provider) private_key_string = get_private_key( Path(private_key).expanduser(), password_file) if not private_key_string: raise RuntimeError("Could not access the private key.") owner = private_key_to_address(private_key_string) # pylint: disable=E1101 if web3.eth.get_balance(owner) == 0: raise RuntimeError("Account with insufficient funds.") deployer = ContractDeployer( web3=web3, private_key=private_key_string, gas_limit=gas_limit, gas_price=gas_price, wait=wait, contracts_version=contracts_version, ) ctx.obj = { "deployer": deployer, "deployed_contracts": {}, "token_type": "CustomToken", "wait": wait, }
def setup_ctx( ctx: click.Context, private_key: str, rpc_provider: str, wait: int, gas_price: int, gas_limit: int, contracts_version: None = None, ): """Set up deployment context according to common options (shared among all subcommands). """ if private_key is None: return logging.basicConfig(level=logging.DEBUG) logging.getLogger('web3').setLevel(logging.INFO) logging.getLogger('urllib3').setLevel(logging.INFO) web3 = Web3(HTTPProvider(rpc_provider, request_kwargs={'timeout': 60})) web3.middleware_stack.inject(geth_poa_middleware, layer=0) print('Web3 provider is', web3.providers[0]) private_key = get_private_key(private_key) assert private_key is not None owner = private_key_to_address(private_key) # pylint: disable=E1101 assert web3.eth.getBalance(owner) > 0, 'Account with insuficient funds.' deployer = ContractDeployer( web3=web3, private_key=private_key, gas_limit=gas_limit, gas_price=gas_price, wait=wait, contracts_version=contracts_version, ) ctx.obj = {} ctx.obj['deployer'] = deployer ctx.obj['deployed_contracts'] = {} ctx.obj['token_type'] = 'CustomToken' ctx.obj['wait'] = wait
def test_get_private_key_writable_keyfile(): """ get_private_key() should return None on a key path with wrong permissions """ with tempfile.NamedTemporaryFile() as tmpfile: orig = os.stat(tmpfile.name).st_mode os.chmod(tmpfile.name, stat.S_IWGRP | orig) assert get_private_key(tmpfile.name) is None
def test_get_private_key_nonexistent(): """ get_private_key() should return None on a nonexistent file path """ assert get_private_key('ggg') is None
def test_get_private_key_empty_path(): """ get_private_key() should raise AssertionFailure on an empty key path """ with pytest.raises(AssertionError): get_private_key('')
def test_get_private_key_nonexistent() -> None: """ get_private_key() should return None on a nonexistent file path """ assert get_private_key(Path("ggg")) is None
def test_get_private_key_empty_path() -> None: """ get_private_key() should raise AssertionFailure on an empty key path """ with pytest.raises(AssertionError): get_private_key(None) # type: ignore
def test_get_private_key_empty_path() -> None: """get_private_key() should return None on a None key path""" assert get_private_key(None) is None # type: ignore