def _encode_abi(cls, abi, arguments, data=None): argument_types = get_abi_input_types(abi) if not check_if_arguments_can_be_encoded(abi, arguments, {}): raise TypeError( "One or more arguments could not be encoded to the necessary " "ABI type. Expected types are: {0}".format( ', '.join(argument_types), ) ) try: encoded_arguments = encode_abi( argument_types, force_obj_to_bytes(arguments), ) except EncodingError as e: raise TypeError( "One or more arguments could not be encoded to the necessary " "ABI type: {0}".format(str(e)) ) if data: return add_0x_prefix( force_bytes(remove_0x_prefix(data)) + force_bytes(remove_0x_prefix(encode_hex(encoded_arguments))) ) else: return encode_hex(encoded_arguments)
def importRawKey(self, private_key, passphrase): if len(private_key) == 66: private_key = remove_0x_prefix(private_key) elif len(private_key) == 32: private_key = remove_0x_prefix(encode_hex(private_key)) elif len(private_key) == 64: pass else: raise ValueError("Unknown private key format") return self.web3._requestManager.request_blocking( "personal_importRawKey", [private_key, passphrase], )
def geth_node_config(miner_pkey, p2p_port, rpc_port): address = privatekey_to_address(miner_pkey) pub = remove_0x_prefix(encode_hex(privtopub(miner_pkey))) config = { 'nodekey': miner_pkey, 'nodekeyhex': remove_0x_prefix(encode_hex(miner_pkey)), 'pub': pub, 'address': address, 'port': p2p_port, 'rpcport': rpc_port, 'enode': f'enode://{pub}@127.0.0.1:{p2p_port}', } return config
def hex_encode_abi_type(abi_type, value, force_size=None): """ Encodes value into a hex string in format of abi_type """ validate_abi_type(abi_type) validate_abi_value(abi_type, value) data_size = force_size or size_of_type(abi_type) if is_array_type(abi_type): sub_type = sub_type_of_array_type(abi_type) return "".join([remove_0x_prefix(hex_encode_abi_type(sub_type, v, 256)) for v in value]) elif is_bool_type(abi_type): return to_hex_with_size(value, data_size) elif is_uint_type(abi_type): return to_hex_with_size(value, data_size) elif is_int_type(abi_type): return to_hex_twos_compliment(value, data_size) elif is_address_type(abi_type): return pad_hex(value, data_size) elif is_bytes_type(abi_type): if is_bytes(value): return encode_hex(value) else: return value elif is_string_type(abi_type): return encode_hex(value) else: raise ValueError( "Unsupported ABI type: {0}".format(abi_type) )
def __init__( self, private_key: str = None, key_password_path: str = None, channel_manager_address: str = None, web3: Web3 = None ) -> None: is_hex_key = is_hex(private_key) and len(remove_0x_prefix(private_key)) == 64 is_path = os.path.exists(private_key) assert is_hex_key or is_path, 'Private key must either be a hex key or a file path.' # Load private key from file if none is specified on command line. if is_path: private_key = get_private_key(private_key, key_password_path) assert private_key is not None, 'Could not load private key from file.' self.channels = [] # type: List[Channel] # Create web3 context if none is provided, either by using the proxies' context or creating # a new one. if not web3: web3 = Web3(HTTPProvider(WEB3_PROVIDER_DEFAULT)) channel_manager_address = to_checksum_address( channel_manager_address or NETWORK_CFG.CHANNEL_MANAGER_ADDRESS ) self.context = Context(private_key, web3, channel_manager_address) self.sync_channels()
def deploy_contract_web3( contract_name: str, deploy_client: JSONRPCClient, num_confirmations: int = None, constructor_arguments: typing.Tuple[typing.Any, ...] = (), ) -> typing.Address: contract_interface = CONTRACT_MANAGER.get_contract(contract_name) contract = deploy_client.web3.eth.contract( abi=contract_interface['abi'], bytecode=contract_interface['bin'], ) transaction = contract.constructor(*constructor_arguments).buildTransaction() transaction['nonce'] = deploy_client.nonce() signed_txn = deploy_client.web3.eth.account.signTransaction( transaction, deploy_client.privkey, ) tx_hash = deploy_client.web3.eth.sendRawTransaction(signed_txn.rawTransaction) deploy_client.poll(transaction_hash=tx_hash, confirmations=num_confirmations) receipt = deploy_client.get_transaction_receipt(tx_hash) if receipt.get('status', 0) == 0: raise RuntimeError('contract was not sucessfully deployed') return typing.Address( unhexlify(remove_0x_prefix(receipt['contractAddress'])), )
def send_transaction( self, to: Address, value: int = 0, data: bytes = b'', startgas: int = None, gasprice: int = None, ): """ Helper to send signed messages. This method will use the `privkey` provided in the constructor to locally sign the transaction. This requires an extended server implementation that accepts the variables v, r, and s. """ if to == to_canonical_address(NULL_ADDRESS): warnings.warn('For contract creation the empty string must be used.') transaction = dict( nonce=self.nonce(), gasPrice=gasprice or self.gasprice(), gas=self.check_startgas(startgas), value=value, data=data, ) # add the to address if not deploying a contract if to != b'': transaction['to'] = to_checksum_address(to) signed_txn = self.web3.eth.account.signTransaction(transaction, self.privkey) result = self.web3.eth.sendRawTransaction(signed_txn.rawTransaction) encoded_result = encode_hex(result) return remove_0x_prefix(encoded_result)
def create_BIP122_uri(chain_id, resource_type, resource_identifier): """ See: https://github.com/bitcoin/bips/blob/master/bip-0122.mediawiki """ if resource_type not in {BLOCK, TRANSACTION}: raise ValueError("Invalid resource_type. Must be one of 'block' or 'transaction'") elif not is_block_or_transaction_hash(resource_identifier): raise ValueError("Invalid resource_identifier. Must be a hex encoded 32 byte value") elif not is_block_or_transaction_hash(chain_id): raise ValueError("Invalid chain_id. Must be a hex encoded 32 byte value") return parse.urlunsplit([ 'blockchain', remove_0x_prefix(chain_id), "{0}/{1}".format(resource_type, remove_0x_prefix(resource_identifier)), '', '', ])
def faucet_private_key(request, faucet_password_path: str) -> str: private_key = request.config.getoption('faucet_private_key') if is_hex(private_key): assert len(remove_0x_prefix(private_key)) == 64 return private_key else: private_key = get_private_key(private_key, faucet_password_path) assert private_key is not None, 'Error loading faucet private key from file.' return private_key
def test_personal_importRawKey_as_hex_without_0x(web3, account_private_key, account_password, account_public_key): address = web3.personal.importRawKey(remove_0x_prefix(encode_32bytes(account_private_key)), account_password) # sanity check assert is_same_address(address, account_public_key) assert web3.personal.unlockAccount(address, account_password) is True
def test_get_chain_definition(web3): block_0 = web3.eth.getBlock(0) chain_definition = get_chain_definition(web3) assert remove_0x_prefix(block_0['hash']) in chain_definition assert '0x' not in chain_definition _, _, anchor_block_hash = chain_definition.rpartition('/') anchor_block = web3.eth.getBlock(anchor_block_hash) assert anchor_block['hash'] == add_0x_prefix(anchor_block_hash)
def find_placeholder_locations(bytecode): """ Given bytecode, this will return all of the linked references from within the bytecode. """ unprefixed_bytecode = remove_0x_prefix(bytecode) for match in re.finditer(DEPENDENCY_RE, unprefixed_bytecode): start = match.start() length = match.end() - start placeholder = unprefixed_bytecode[start:start + length] yield (remove_dunderscore_wrapper(placeholder), start, length)
def sign(privkey: str, msg: bytes, v=0) -> bytes: assert isinstance(msg, bytes) assert isinstance(privkey, str) pk = PrivateKey.from_hex(remove_0x_prefix(privkey)) assert len(msg) == 32 sig = pk.sign_recoverable(msg, hasher=None) assert len(sig) == 65 sig = sig[:-1] + bytes([sig[-1] + v]) return sig
def test_contract_htdf_faucet_deposit(conftest_args): with open('sol/htdf_faucet_sol_HtdfFaucet.abi', 'r') as abifile: # abi = abifile.readlines() abijson = abifile.read() # print(abijson) abi = json.loads(abijson) assert len(htdf_faucet_contract_address) > 0 contract_address = Address(htdf_faucet_contract_address[0]) htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) hc = HtdfContract(rpc=htdfrpc, address=contract_address, abi=abi) deposit_amount = htdf_to_satoshi(10) deposit_tx = hc.functions.deposit().buildTransaction_htdf() data = remove_0x_prefix(deposit_tx['data']) from_addr = Address(conftest_args['ADDRESS']) private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=contract_address, amount_satoshi=deposit_amount, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=200000, data=data, memo='htdf_faucet.deposit()').build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) assert tx['logs'][0]['success'] == True time.sleep(8) # wait for chain state update contract_acc = htdfrpc.get_account_info(address=contract_address.address) assert contract_acc is not None assert contract_acc.balance_satoshi == deposit_amount pass
def sign_hash_to_bytes( provider: BaseProvider, signer_address: str, hash_hex: str ) -> bytes: """Sign a message with the given hash, and return the signature. >>> provider = Web3.HTTPProvider("http://127.0.0.1:8545") >>> sign_hash_to_bytes( ... provider, ... Web3(provider).geth.personal.listAccounts()[0], ... '0x34decbedc118904df65f379a175bb39ca18209d6ce41d5ed549d54e6e0a95004', ... ).decode(encoding='utf_8') '1b117902c86dfb95fe0d1badd983ee166ad259b27acb220174cbb4460d872871137feabdfe76e05924b484789f79af4ee7fa29ec006cedce1bbf369320d034e10b03' """ # noqa: E501 (line too long) return remove_0x_prefix( sign_hash(provider, signer_address, hash_hex) ).encode(encoding="utf_8")
def soliditySha3(self, abi_types, values): """ Executes sha3 (keccak256) exactly as Solidity does. Takes list of abi_types as inputs -- `[uint24, int8[], bool]` and list of corresponding values -- `[20, [-1, 5, 0], True]` """ if len(abi_types) != len(values): raise ValueError( "Length mismatch between provided abi types and values. Got " "{0} types and {1} values.".format(len(abi_types), len(values))) hex_string = add_0x_prefix(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, values))) return self.sha3(hexstr=hex_string)
def to_bytes(primitive=None, hexstr=None, text=None): assert_one_val(primitive, hexstr=hexstr, text=text) if is_boolean(primitive): return b'\x01' if primitive else b'\x00' elif isinstance(primitive, bytes): return primitive elif is_integer(primitive): return to_bytes(hexstr=to_hex(primitive)) elif hexstr is not None: if len(hexstr) % 2: hexstr = '0x0' + remove_0x_prefix(hexstr) return decode_hex(hexstr) elif text is not None: return text.encode('utf-8') raise TypeError("expected an int in first arg, or keyword of hexstr or text")
def random_marker(): """ A random marker used to identify a pytest run. Some tests will spawn a private chain, the private chain will be one or more ethereum nodes on a new subprocesss. These nodes may fail to start on concurrent test runs, mostly because of port number conflicts, but even though the test fails to start its private chain it may run interacting with the geth process from a different test run! This leads to unreasonable test errors. This fixture creates a random marker used to distinguish pytest runs and avoid test failures. Note this could fail for other reasons and fail to detect unwanted interations if the user sets the PYTHONHASHSEED to the same value. """ return remove_0x_prefix(HexStr(hex(random.getrandbits(100))))
def encrypt_document(self, document_id, content, threshold=0): """ encrypt string data using the DID as an secret store id, if secret store is enabled then return the result from secret store encryption None for no encryption performed :param document_id: hex str id of document to use for encryption session :param content: str to be encrypted :param threshold: int :return: None -- if encryption failed hex str -- the encrypted document """ return self._secret_store_client(self._account).publish_document( remove_0x_prefix(document_id), content, threshold)
def decrypt_document(self, document_id, encrypted_content): """ Decrypt a previously encrypted content using the secret store keys identified by document_id. Note that decryption requires permission already granted to the consumer account. :param document_id: hex str id of document to use for encryption session :param encrypted_content: hex str -- the encrypted content from a previous `encrypt_document` operation :return: None -- if decryption failed str -- the original content that was encrypted previously """ return self._secret_store_client(self._account).decrypt_document( remove_0x_prefix(document_id), encrypted_content)
def publish(): """Encrypt document using the SecretStore and keyed by the given documentId. swagger_from_file: docs/publish.yml """ required_attributes = [ 'documentId', 'signature', 'document', 'publisherAddress' ] data = request.json if 'signedDocumentId' in data and 'signature' not in data: data['signature'] = data['signedDocumentId'] msg, status = check_required_attributes(required_attributes, data, 'publish') if msg: return msg, status did = data.get('documentId') signature = data.get('signature') document = json.dumps(json.loads(data.get('document')), separators=(',', ':')) publisher_address = data.get('publisherAddress') try: if not verify_signature(keeper_instance(), publisher_address, signature, did): msg = f'Invalid signature {signature} for ' \ f'publisherAddress {publisher_address} and documentId {did}.' raise ValueError(msg) print('Document: ' + document) print('DID: ' + remove_0x_prefix(did)) encrypted_document, public_key = rsa_encryption_from_file( document, get_rsa_public_key_file()) logger.debug(f'encrypted urls {encrypted_document}, ' f'publisher {publisher_address}, ' f'documentId {did}') return encrypted_document, 201 except (RPCError, Exception) as e: logger.error( f'Encryption Error: {e}. \n' f'providerAddress={provider_acc.address}\n' f'Payload was: documentId={did}, ' f'publisherAddress={publisher_address},' f'signature={signature}', exc_info=1) return f'Error: {str(e)}', 500
def next_address_list(tcd_address, is_active, key, height): slot = "0" * 63 + "2" if is_active else "0" * 63 + "3" location = "0x" + remove_0x_prefix(key).rjust(64, "0") + slot raw_output = requests.post( BASE_URL, json={ "jsonrpc": "2.0", "method": "eth_getStorageAt", "params": [ tcd_address, "0x" + keccak(hexstr=location).hex(), height, ], "id": 12, }, ) return raw_output.json()["result"]
def eth_node_config(miner_pkey: PrivateKey, p2p_port: Port, rpc_port: Port, **extra_config: Dict[str, Any]) -> Dict[str, Any]: address = privatekey_to_address(miner_pkey) pub = privatekey_to_publickey(miner_pkey).hex() config = extra_config.copy() config.update({ "nodekey": miner_pkey, "nodekeyhex": remove_0x_prefix(encode_hex(miner_pkey)), "pub": pub, "address": address, "port": p2p_port, "rpcport": rpc_port, "enode": f"enode://{pub}@127.0.0.1:{p2p_port}", }) return config
def pack_data(abi_types, values) -> bytes: """Normalize data and pack them into a byte array""" warnings.warn( 'eth_recover from raiden-libs is deprecated. ' 'Function is now moved in the raiden client', DeprecationWarning, ) if len(abi_types) != len(values): raise ValueError( "Length mismatch between provided abi types and values. Got " "{0} types and {1} values.".format(len(abi_types), len(values)), ) normalized_values = map_abi_data([abi_address_to_hex], abi_types, values) return decode_hex(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values)))
def generate_address(cls, private_key: str) -> str: """ create an asimov address with the given private key :param private_key: the private key :return: an hex address generated from private key, without 0x prefix. .. code-block:: python >>> from asimov import AccountFactory >>> AccountFactory.generate_address("bba692e559fda550d0157669b101bafddb23e7f57aeeb5cef5494e7a41a1f056") '66c17b951f0c85b860c9f7f0d811c77ea78f2d2e3a' """ public_key = cls.private2compressed_public( remove_0x_prefix(private_key)) address = cls.__public2address(public_key) return address
def random_marker(): """ A random marker used to identify a pytest run. Some tests will spawn a private chain, the private chain will be one or more ethereum nodes on a new subprocesss. These nodes may fail to start on concurrent test runs, mostly because of port number conflicts, but even though the test fails to start its private chain it may run interacting with the geth process from a different test run! This leads to unreasonable test errors. This fixture creates a random marker used to distinguish pytest runs and avoid test failures. Note this could fail for other reasons and fail to detect unwanted interations if the user sets the PYTHONHASHSEED to the same value. """ random_hex = hex(random.getrandbits(100)) return remove_0x_prefix(random_hex)
def test_locked(self): keystore = self.keystore uuid = self.uuid account = Account(keystore) assert account.locked assert account.address.hex() == remove_0x_prefix(keystore['address']) assert account.privkey is None assert account.pubkey is None assert account.uuid == uuid keystore2 = keystore.copy() keystore2.pop('address') account = Account(keystore2) assert account.locked assert account.address is None assert account.privkey is None assert account.pubkey is None assert account.uuid == uuid
def hexstr_if_str(to_type, hexstr_or_primitive): ''' Convert to a type, assuming that strings can be only hexstr (not unicode text) @param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text), eg~ to_bytes, to_text, to_hex, to_int, etc @param text_or_primitive in bytes, str, or int. ''' if isinstance(hexstr_or_primitive, str): (primitive, hexstr) = (None, hexstr_or_primitive) if remove_0x_prefix(hexstr) and not is_hex(hexstr): raise ValueError( "when sending a str, it must be a hex string. Got: {0!r}". format(hexstr_or_primitive, )) else: (primitive, hexstr) = (hexstr_or_primitive, None) return to_type(primitive, hexstr=hexstr)
def fill_order( self, order: Order, taker_amount: int, signature: str, tx_params: Optional[TxParams] = None, view_only: bool = False, ) -> Union[HexBytes, bytes]: """Fill a signed order with given amount of taker asset. This is the most basic way to fill an order. All of the other methods call fillOrder under the hood with additional logic. This function will attempt to fill the amount specified by the caller. However, if the remaining fillable amount is less than the amount specified, the remaining amount will be filled. Partial fills are allowed when filling orders. See the specification docs for `fillOrder <https://github.com/0xProject/0x-protocol-specification/blob/master /v2/v2-specification.md#fillorder>`_. :param order: instance of :class:`zero_ex.order_utils.Order` :param taker_amount: integer taker amount in Wei (1 Wei is 10e-18 ETH) :param signature: str or hexstr or bytes of order hash signature :param tx_params: default None, :class:`TxParams` transaction params :param view_only: default False, boolean of whether to transact or view only :returns: transaction hash """ assert_valid(order_to_jsdict(order, self.address), "/orderSchema") is_valid_signature( self._provider, generate_order_hash_hex(order, self.address), signature, order["makerAddress"], ) # safeguard against fractional inputs taker_fill_amount = int(taker_amount) normalized_signature = bytes.fromhex(remove_0x_prefix(signature)) func = self._exchange.functions.fillOrder(order, taker_fill_amount, normalized_signature) return self._invoke_function_call(func=func, tx_params=tx_params, view_only=view_only)
def first_pass_check_bytes_arr(cls, bytes_param_lst, param_name, base_param_type, conversion_errors): error_flag = False ret = bytes_param_lst for idx, bytes_param in enumerate(bytes_param_lst): if eth_utils.is_0x_prefixed(bytes_param): pc = eth_utils.remove_0x_prefix(bytes_param) pc_to_bytes = bytes.fromhex(pc) ret[idx] = pc_to_bytes else: try: ret[idx] = bytes_param.encode('utf-8') except: error_msg = "Expected type: {0}. Supplied argument not a valid byte object.".format(base_param_type) conversion_errors[param_name]["message"].append(error_msg) conversion_errors[param_name]["failed_indexes"].append(idx) error_flag = True ret[idx] = "".encode('utf-8') return (ret, error_flag)
def decode_single(typ, data): if is_hex_encoded_value(data): data = decode_hex(remove_0x_prefix(data)) if not is_bytes(data): raise TypeError( "The `data` value must be of bytes type. Got {0}".format( type(data))) if isinstance(typ, str): type_str = typ else: type_str = collapse_type(*typ) decoder = registry.get_decoder(type_str) stream = ContextFramesBytesIO(data) return decoder(stream)
def get_request_bytes_representation(payee_id_addresses, amounts, payer, ipfs_hash=None): """ Return the bytes representation of the given Request data. The JS version uses lower-cased addresses but web3.py expects checksum addresses. To work around this the encoded result is converted to lowercase. address(creator) address(payer) uint8(number_of_payees) [ address(main_payee_address) int256(main_payee_expected_amount) address(second_payee_address) int256(second_payee_expected_amount) ... ] uint8(data_string_size) size(data) :return: """ ipfs_hash = ipfs_hash if ipfs_hash else '' payer = payer if payer else EMPTY_BYTES_20 parts = [(payee_id_addresses[0], 'address'), (payer, 'address'), (len(payee_id_addresses), 'uint8')] for i in range(0, len(payee_id_addresses)): parts.append((payee_id_addresses[i], 'address')) parts.append((amounts[i], 'int256')) parts.append((len(ipfs_hash), 'uint8')) parts.append((ipfs_hash, 'string')) values, abi_types = zip(*parts) # Taken from `Web3.soliditySha3` normalized_values = map_abi_data([abi_ens_resolver(w3)], abi_types, values) return add_0x_prefix(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values))).lower()
def main() -> None: tmpdir = tempfile.mkdtemp() geth_nodes = [] for i in range(NUM_GETH_NODES): is_miner = i == 0 node_key = PrivateKey(sha3(f"node:{i}".encode())) p2p_port = Port(START_PORT + i) rpc_port = Port(START_RPCPORT + i) description = EthNodeDescription( private_key=node_key, rpc_port=rpc_port, p2p_port=p2p_port, miner=is_miner, extra_config={}, ) geth_nodes.append(description) rpc_endpoint = f"http://127.0.0.1:{START_RPCPORT}" web3 = Web3(HTTPProvider(rpc_endpoint)) random_marker = remove_0x_prefix(hex(random.getrandbits(100))) genesis_description = GenesisDescription( prefunded_accounts=DEFAULT_ACCOUNTS, random_marker=random_marker, chain_id=ChainID(NETWORKNAME_TO_ID["smoketest"]), ) private_chain: ContextManager[ List[JSONRPCExecutor]] = run_private_blockchain( web3=web3, eth_nodes=geth_nodes, base_datadir=tmpdir, log_dir=tmpdir, verbosity="info", genesis_description=genesis_description, ) with private_chain: from IPython import embed embed()
def _verify_deployed_code(address: str, expected_bytecode: str) -> bool: actual_bytecode = web3.eth.getCode(address).hex()[2:] expected_bytecode = remove_0x_prefix(expected_bytecode) # type: ignore if expected_bytecode.startswith( "730000000000000000000000000000000000000000"): # special case for Solidity libraries return (actual_bytecode.startswith(f"73{address[2:].lower()}") and actual_bytecode[42:] == expected_bytecode[42:]) if "_" in expected_bytecode: for marker in re.findall("_{1,}[^_]*_{1,}", expected_bytecode): idx = expected_bytecode.index(marker) actual_bytecode = actual_bytecode[:idx] + actual_bytecode[idx + 40:] expected_bytecode = expected_bytecode[:idx] + expected_bytecode[ idx + 40:] return actual_bytecode == expected_bytecode
def geth_process(geth_binary, datadir, genesis, ipc_file, open_port, keystore): init_datadir_command = ( geth_binary, '--datadir', str(datadir), 'init', str(genesis.path), ) subprocess.check_output( init_datadir_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) run_geth_command = ( geth_binary, '--datadir', str(datadir), '--ipcpath', str(ipc_file), '--nodiscover', '--port', open_port, '--networkid', str(genesis.data['config']['chainId']), '--etherbase', remove_0x_prefix(tests.go_ethereum.common.COINBASE), '--gasprice', '0', # Gas price threshold for the cpu miner '--targetgaslimit', genesis.data['gasLimit']) print(' '.join(run_geth_command)) try: proc = get_process(run_geth_command) yield proc finally: tests.go_ethereum.common.kill_proc_gracefully(proc) output, errors = proc.communicate() print("Geth Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ))
def get_private_key(pk_file, pw_file=None): """Open a JSON-encoded private key and return it If a password file is provided, uses it to decrypt the key. If not, the password is asked interactively. Raw hex-encoded private keys are supported, but deprecated.""" is_hex_key = is_hex(pk_file) and len(remove_0x_prefix(pk_file)) == 64 is_path = os.path.exists(pk_file) assert is_hex_key or is_path, 'Private key must either be a hex key or a file path.' # Load private key from file if none is specified on command line. if is_path: private_key = load_pk(pk_file, pw_file) assert private_key is not None, 'Could not load private key from file.' return private_key else: # TODO make sure '0x' return pk_file
def sign_active_state(self, address, private_key): raw_checksum = self.checksum() vrs = sign_message(m=raw_checksum, k=private_key) Wallet = apps.get_model('ledger', 'Wallet') try: operator_wallet = Wallet.objects.get( token=self.wallet.token, address=remove_0x_prefix(address)) except Wallet.DoesNotExist: raise LookupError( 'Signing wallet {} is not yet registered'.format(address)) Signature = apps.get_model('ledger', 'Signature') operator_signature = Signature.objects.create( wallet=operator_wallet, checksum=hex_value(raw_checksum), value=encode_signature(vrs)) return operator_signature
def hexstr_if_str(to_type, hexstr_or_primitive): ''' Convert to a type, assuming that strings can be only hexstr (not unicode text) @param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text), eg~ to_bytes, to_text, to_hex, to_int, etc @param text_or_primitive in bytes, str, or int. ''' if isinstance(hexstr_or_primitive, str): (primitive, hexstr) = (None, hexstr_or_primitive) if remove_0x_prefix(hexstr) and not is_hex(hexstr): raise ValueError( "when sending a str, it must be a hex string. Got: {0!r}".format( hexstr_or_primitive, ) ) else: (primitive, hexstr) = (hexstr_or_primitive, None) return to_type(primitive, hexstr=hexstr)
def main(): tmpdir = tempfile.mkdtemp() geth_nodes = [] for i in range(NUM_GETH_NODES): is_miner = i == 0 node_key = sha3(f'node:{i}'.encode()) p2p_port = START_PORT + i rpc_port = START_RPCPORT + i description = EthNodeDescription( private_key=node_key, rpc_port=rpc_port, p2p_port=p2p_port, miner=is_miner, extra_config={}, ) geth_nodes.append(description) rpc_endpoint = f'http://127.0.0.1:{START_RPCPORT}' web3 = Web3(HTTPProvider(rpc_endpoint)) verbosity = 0 random_marker = remove_0x_prefix(hex(random.getrandbits(100))) genesis_description = GenesisDescription( prefunded_accounts=DEFAULT_ACCOUNTS, random_marker=random_marker, chain_id=NETWORKNAME_TO_ID['smoketest'], ) private_chain = run_private_blockchain( # NOQA web3=web3, eth_nodes=geth_nodes, base_datadir=tmpdir, log_dir=tmpdir, verbosity=verbosity, genesis_description=genesis_description, ) with private_chain: from IPython import embed embed()
def find_link_references(bytecode, full_reference_names): """ Given bytecode, this will return all of the linked references from within the bytecode. """ unprefixed_bytecode = remove_0x_prefix(bytecode) expand_fn = functools.partial( expand_shortened_reference_name, full_reference_names=full_reference_names, ) link_references = tuple((LinkReference( reference_name=remove_dunderscore_wrapper(match.group()), full_name=expand_fn(remove_dunderscore_wrapper(match.group())), offset=match.start(), length=match.end() - match.start(), ) for match in re.finditer(DEPENDENCY_RE, unprefixed_bytecode))) return link_references
def send_transaction( self, to: Address, value: int = 0, data: bytes = b'', startgas: int = None, ): """ Helper to send signed messages. This method will use the `privkey` provided in the constructor to locally sign the transaction. This requires an extended server implementation that accepts the variables v, r, and s. """ if to == b'' and data.isalnum(): warnings.warn( 'Verify that the data parameter is _not_ hex encoded, if this is the case ' 'the data will be double encoded and result in unexpected ' 'behavior.' ) if to == to_canonical_address(NULL_ADDRESS): warnings.warn('For contract creation the empty string must be used.') transaction = dict( nonce=self.nonce(), gasPrice=self.gasprice(), gas=self.check_startgas(startgas), value=value, data=data, ) # add the to address if not deploying a contract if to != b'': transaction['to'] = to_checksum_address(to) signed_txn = self.web3.eth.account.signTransaction(transaction, self.privkey) result = self.web3.eth.sendRawTransaction(signed_txn.rawTransaction) encoded_result = encode_hex(result) return remove_0x_prefix(encoded_result)
def get_asset_url_at_index(url_index, asset, account, auth_method='PSK-RSA'): logger.debug( f'get_asset_url_at_index(): url_index={url_index}, did={asset.did}, provider=' f'{account.address}') try: if auth_method == 'SecretStore': files_str = do_secret_store_decrypt( remove_0x_prefix(asset.asset_id), asset.encrypted_files, account, get_config()) elif auth_method == 'PSK-RSA': files_str = rsa_decryption_aes(asset.encrypted_files, get_rsa_private_key_file()) elif auth_method == 'PSK-ECDSA': files_str = ecdsa_decryption(asset.encrypted_files, get_provider_key_file(), get_provider_password()) logger.debug(f'Got decrypted files str {files_str}') files_list = json.loads(files_str) if not isinstance(files_list, list): raise TypeError(f'Expected a files list, got {type(files_list)}.') if url_index >= len(files_list): raise ValueError(f'url index "{url_index}"" is invalid.') file_meta_dict = files_list[url_index] if not file_meta_dict or not isinstance(file_meta_dict, dict): raise TypeError( f'Invalid file meta at index {url_index}, expected a dict, got a ' f'{type(file_meta_dict)}.') if 'url' not in file_meta_dict: raise ValueError( f'The "url" key is not found in the ' f'file dict {file_meta_dict} at index {url_index}.') return file_meta_dict['url'] except Exception as e: logger.error( f'Error decrypting url at index {url_index} for asset {asset.did}: {str(e)}' ) raise
def decode_abi(types, data): if is_hex_encoded_value(data): warnings.warn( DeprecationWarning( "Automatic inference of hex encoded data has been deprecated. " "Please adjust your code to ensure that the data argument for " "`decode_single` is a byte string")) data = decode_hex(remove_0x_prefix(data)) if is_text(data): warnings.warn( DeprecationWarning( "Automatic conversion of encoded data to bytes has been deprecated. " "Please adjust your code to ensure that the data argument for " "`decode_abi` is a byte string")) data = force_bytes(data) processed_types = tuple(process_type(_type) for _type in types) decoder = get_multi_decoder(processed_types) stream = BytesIO(data) return decoder(stream)
def eth_node_config( miner_pkey: bytes, p2p_port: int, rpc_port: int, **extra_config: Dict[str, Any], ) -> Dict[str, Any]: address = privatekey_to_address(miner_pkey) pub = privatekey_to_publickey(miner_pkey).hex() config = extra_config.copy() config.update({ 'nodekey': miner_pkey, 'nodekeyhex': remove_0x_prefix(encode_hex(miner_pkey)), 'pub': pub, 'address': address, 'port': p2p_port, 'rpcport': rpc_port, 'enode': f'enode://{pub}@127.0.0.1:{p2p_port}', }) return config
def hexstr_if_str(to_type, hexstr_or_primitive): ''' Convert to a type, assuming that strings can be only hexstr (not unicode text) @param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text), eg~ to_bytes, to_text, to_hex, to_int, etc @param text_or_primitive in bytes, str, or int. (or unicode in py2) In Python 2, a bytes, unicode or str object will be interpreted as hexstr In Python 3, only a str object will be interpreted as hexstr ''' if isinstance(hexstr_or_primitive, str) or ( sys.version_info.major < 3 and isinstance(hexstr_or_primitive, unicode) # noqa: F821 ): (primitive, hexstr) = (None, hexstr_or_primitive) if remove_0x_prefix(hexstr) and not is_hex(hexstr): raise ValueError( "when sending this str, it must be a hex string. Got: %r" % hexstr_or_primitive ) else: (primitive, hexstr) = (hexstr_or_primitive, None) return to_type(primitive, hexstr=hexstr)
def dump(self, include_address=True, include_id=True) -> str: """Dump the keystore for later disk storage. The result inherits the entries `'crypto'` and `'version`' from `account.keystore`, and adds `'address'` and `'id'` in accordance with the parameters `'include_address'` and `'include_id`'. If address or id are not known, they are not added, even if requested. Args: include_address: flag denoting if the address should be included or not include_id: flag denoting if the id should be included or not """ d = { 'crypto': self.keystore['crypto'], 'version': self.keystore['version'], } if include_address and self.address is not None: d['address'] = remove_0x_prefix(encode_hex(self.address)) if include_id and self.uuid is not None: d['id'] = self.uuid return json.dumps(d)
def hexstr_if_str(to_type, hexstr_or_primitive): ''' Convert to a type, assuming that strings can be only hexstr (not unicode text) @param to_type is a function that takes the arguments (primitive, hexstr=hexstr, text=text), eg~ to_bytes, to_text, to_hex, to_int, etc @param text_or_primitive in bytes, str, or int. (or unicode in py2) In Python 2, a bytes, unicode or str object will be interpreted as hexstr In Python 3, only a str object will be interpreted as hexstr ''' if isinstance(hexstr_or_primitive, str) or ( sys.version_info.major < 3 and isinstance(hexstr_or_primitive, unicode) # noqa: F821 ): (primitive, hexstr) = (None, hexstr_or_primitive) if remove_0x_prefix(hexstr) and not is_hex(hexstr): raise ValueError( "when sending a str, it must be a hex string. Got: {0!r}".format( hexstr_or_primitive, ) ) else: (primitive, hexstr) = (hexstr_or_primitive, None) return to_type(primitive, hexstr=hexstr)
def soliditySha3(cls, abi_types, values): """ Executes sha3 (keccak256) exactly as Solidity does. Takes list of abi_types as inputs -- `[uint24, int8[], bool]` and list of corresponding values -- `[20, [-1, 5, 0], True]` """ if len(abi_types) != len(values): raise ValueError( "Length mismatch between provided abi types and values. Got " "{0} types and {1} values.".format(len(abi_types), len(values)) ) if isinstance(cls, type): w3 = None else: w3 = cls normalized_values = map_abi_data([abi_ens_resolver(w3)], abi_types, values) hex_string = add_0x_prefix(''.join( remove_0x_prefix(hex_encode_abi_type(abi_type, value)) for abi_type, value in zip(abi_types, normalized_values) )) return cls.sha3(hexstr=hex_string)
def main(): tmpdir = tempfile.mkdtemp() geth_nodes = [] for i in range(NUM_GETH_NODES): is_miner = i == 0 node_key = sha3(f'node:{i}'.encode()) p2p_port = START_PORT + i rpc_port = START_RPCPORT + i description = GethNodeDescription( node_key, rpc_port, p2p_port, is_miner, ) geth_nodes.append(description) rpc_endpoint = f'http://127.0.0.1:{START_RPCPORT}' web3 = Web3(HTTPProvider(rpc_endpoint)) verbosity = 0 random_marker = remove_0x_prefix(hex(random.getrandbits(100))) geth_processes = geth_run_private_blockchain( # NOQA web3, DEFAULT_ACCOUNTS, geth_nodes, tmpdir, NETWORKNAME_TO_ID['smoketest'], verbosity, random_marker, ) from IPython import embed embed()
def deployer_privkey(): return remove_0x_prefix(encode_hex(keys[3]))
def is_hex_encoded_block_hash(value): if not is_string(value): return False return len(remove_0x_prefix(value)) == 64 and is_hex(value)
def address_to_reverse_domain(address): lower_unprefixed_address = remove_0x_prefix(to_normalized_address(address)) return lower_unprefixed_address + '.' + REVERSE_REGISTRAR_DOMAIN
def create_block_uri(chain_id, block_identifier): if is_integer(block_identifier): return create_BIP122_uri(chain_id, 'block', str(block_identifier)) else: return create_BIP122_uri(chain_id, 'block', remove_0x_prefix(block_identifier))
def insert_link_value(bytecode, value, offset): return add_0x_prefix(''.join(( remove_0x_prefix(bytecode)[:offset], remove_0x_prefix(value), remove_0x_prefix(bytecode)[offset + len(remove_0x_prefix(value)):] )))
def privkey_to_addr(privkey: str) -> str: return to_checksum_address( pubkey_to_addr(PrivateKey.from_hex(remove_0x_prefix(privkey)).public_key) )
def test_contract_constructor_encoding_encoding(WithConstructorArgumentsContract, bytes_arg): deploy_data = WithConstructorArgumentsContract._encode_constructor_data([1234, bytes_arg]) encoded_args = '0x00000000000000000000000000000000000000000000000000000000000004d26162636400000000000000000000000000000000000000000000000000000000' # noqa: E501 expected_ending = encode_hex(encode_abi(['uint256', 'bytes32'], [1234, b'abcd'])) assert expected_ending == encoded_args assert deploy_data.endswith(remove_0x_prefix(expected_ending))
def data_encoder(data: bytes, length: int = 0) -> str: data = remove_0x_prefix(encode_hex(data)) return add_0x_prefix( data.rjust(length * 2, b'0').decode(), )
def pad_hex(value, bit_size): """ Pads a hex string up to the given bit_size """ value = remove_0x_prefix(value) return add_0x_prefix(value.zfill(int(bit_size / 4)))
def safe_lstrip_hex(val): if isinstance(val, str): return remove_0x_prefix(val) return val