Python rlp.encode() Examples
The following are 30
code examples of rlp.encode().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
rlp
, or try the search function
.
Example #1
Source File: base.py From clove with GNU General Public License v3.0 | 6 votes |
def get_raw_transaction(transaction: Transaction) -> str: ''' Get raw_transaction by encoding Transaction object Args: transaction (`ethereum.transactions.Transaction`): Ethereum transaction object Returns: str: raw transaction hex string Example: >>> from clove.network import EthereumTestnet >>> network = EthereumTestnet() >>> transaction = network.deserialize_raw_transaction('0xf8f28201f4843b9aca008302251694ce07ab9477bc20790b88b398a2a9e0f626c7d26387b1a2bc2ec50000b8c47337c993000000000000000000000000000000000000000000000000000000005bd564819d3e84874c199ca4656d434060ec1a393750ab74000000000000000000000000000000000000000000000000d867f293ba129629a9f9355fa285b8d3711a9092000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000808080') # noqa: E501 >>> network.get_raw_transaction(transaction) '0xf8f28201f4843b9aca008302251694ce07ab9477bc20790b88b398a2a9e0f626c7d26387b1a2bc2ec50000b8c47337c993000000000000000000000000000000000000000000000000000000005bd564819d3e84874c199ca4656d434060ec1a393750ab74000000000000000000000000000000000000000000000000d867f293ba129629a9f9355fa285b8d3711a9092000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000808080' # noqa: E501 ''' return Web3.toHex(rlp.encode(transaction))
Example #2
Source File: evm.py From manticore with GNU Affero General Public License v3.0 | 6 votes |
def calculate_new_address(sender=None, nonce=None): if sender is None: # Just choose a random address for regular accounts: new_address = random.randint(100, pow(2, 160)) elif issymbolic(sender): # TODO(Evan Sultanik): In the interim before we come up with a better solution, # consider breaking Yellow Paper comability and just returning # a random contract address here raise EthereumError( "Manticore does not yet support contracts with symbolic addresses creating new contracts" ) else: if nonce is None: # assume that the sender is a contract account, which is initialized with a nonce of 1 nonce = 1 new_address = int(sha3.keccak_256(rlp.encode([sender, nonce])).hexdigest()[24:], 16) return new_address
Example #3
Source File: auth.py From pyquarkchain with MIT License | 6 votes |
def create_auth_message(self, nonce: bytes) -> bytes: ecdh_shared_secret = ecies.ecdh_agree(self.privkey, self.remote.pubkey) secret_xor_nonce = sxor(ecdh_shared_secret, nonce) S = self.ephemeral_privkey.sign_msg_hash(secret_xor_nonce).to_bytes() if self.use_eip8: data = rlp.encode( [S, self.pubkey.to_bytes(), nonce, SUPPORTED_RLPX_VERSION], sedes=eip8_auth_sedes, ) return _pad_eip8_data(data) else: # S || H(ephemeral-pubk) || pubk || nonce || 0x0 return ( S + keccak(self.ephemeral_pubkey.to_bytes()) + self.pubkey.to_bytes() + nonce + b"\x00" )
Example #4
Source File: discovery.py From pyquarkchain with MIT License | 6 votes |
def _get_max_neighbours_per_packet() -> int: # As defined in https://github.com/ethereum/devp2p/blob/master/rlpx.md, the max size of a # datagram must be 1280 bytes, so when sending neighbours packets we must include up to # _max_neighbours_per_packet and if there's more than that split them across multiple # packets. # Use an IPv6 address here as we're interested in the size of the biggest possible node # representation. addr = kademlia.Address("::1", 30303, 30303) node_data = addr.to_endpoint() + [b"\x00" * (kademlia.k_pubkey_size // 8)] neighbours = [node_data] expiration = rlp.sedes.big_endian_int.serialize(_get_msg_expiration()) payload = rlp.encode([neighbours] + [expiration]) while HEAD_SIZE + len(payload) <= 1280: neighbours.append(node_data) payload = rlp.encode([neighbours] + [expiration]) return len(neighbours) - 1
Example #5
Source File: discovery.py From pyquarkchain with MIT License | 6 votes |
def recv_ping_v5( self, node: kademlia.Node, payload: Tuple[Any, ...], message_hash: Hash32, _: bytes, ) -> None: # version, from, to, expiration, topics _, _, _, _, topics = payload self.logger.trace("<<< ping(v5) from %s, topics: %s", node, topics) self.process_ping(node, message_hash) topic_hash = keccak(rlp.encode(topics)) ticket_serial = self.topic_table.issue_ticket(node) # TODO: Generate wait_periods list according to spec. wait_periods = [60] * len(topics) # : List[int] self.send_pong_v5(node, message_hash, topic_hash, ticket_serial, wait_periods)
Example #6
Source File: protocol.py From pyquarkchain with MIT License | 6 votes |
def encode(self, data: PayloadType) -> Tuple[bytes, bytes]: payload = self.encode_payload(data) enc_cmd_id = rlp.encode(self.cmd_id, sedes=rlp.sedes.big_endian_int) frame_size = len(enc_cmd_id) + len(payload) if frame_size.bit_length() > 24: raise ValueError("Frame size has to fit in a 3-byte integer") # Drop the first byte as, per the spec, frame_size must be a 3-byte int. header = struct.pack(">I", frame_size)[1:] # All clients seem to ignore frame header data, so we do the same, although I'm not sure # why geth uses the following value: # https://github.com/ethereum/go-ethereum/blob/master/p2p/rlpx.go#L556 zero_header = b"\xc2\x80\x80" header += zero_header header = _pad_to_16_byte_boundary(header) body = _pad_to_16_byte_boundary(enc_cmd_id + payload) return header, body
Example #7
Source File: protocol.py From pyquarkchain with MIT License | 6 votes |
def encode_payload(self, data: Union[PayloadType, sedes.CountableList]) -> bytes: if isinstance(data, dict): # convert dict to ordered list if not isinstance(self.structure, list): raise ValueError("Command.structure must be a list when data is a dict") expected_keys = sorted(name for name, _ in self.structure) data_keys = sorted(data.keys()) if data_keys != expected_keys: raise ValueError( "Keys in data dict ({}) do not match expected keys ({})".format(data_keys, expected_keys) ) data = [data[name] for name, _ in self.structure] if isinstance(self.structure, sedes.CountableList): encoder = self.structure else: encoder = sedes.List([type_ for _, type_ in self.structure]) return rlp.encode(data, sedes=encoder)
Example #8
Source File: test_discovery.py From pyquarkchain with MIT License | 6 votes |
def test_ping_pong_v5(): alice = get_discovery_protocol(b"alice") bob = get_discovery_protocol(b"bob") # Connect alice's and bob's transports directly so we don't need to deal with the complexities # of going over the wire. link_transports(alice, bob) # Collect all pongs received by alice in a list for later inspection. received_pongs = [] alice.recv_pong_v5 = lambda node, payload, hash_, _: received_pongs.append( (node, payload) ) topics = [b"foo", b"bar"] token = alice.send_ping_v5(bob.this_node, topics) assert len(received_pongs) == 1 node, payload = received_pongs[0] _, reply_token, _, topic_hash, _, _ = payload assert node.id == bob.this_node.id assert token == reply_token assert topic_hash == keccak(rlp.encode(topics))
Example #9
Source File: test_hexary_trie.py From py-trie with MIT License | 6 votes |
def test_hexary_trie_avoid_over_pruning(): db = {} trie = HexaryTrie(db, prune=True) def _insert(trie, index, val): index_key = rlp.encode(index, sedes=rlp.sedes.big_endian_int) trie[index_key] = val return index_key inserted_keys = [] for index, val in enumerate([b'\0' * 32] * 129): new_key = _insert(trie, index, val) inserted_keys.append(new_key) # poke the trie to make sure all nodes are still present for key in inserted_keys: # If there's a problem, this will raise a MissingTrieNode trie.get(key) verify_ref_count(trie)
Example #10
Source File: test_iter.py From py-trie with MIT License | 6 votes |
def test_iter_nodes(trie_keys, min_value_length): trie, contents = trie_from_keys(trie_keys, min_value_length) visited = set() for prefix, node in NodeIterator(trie).nodes(): # Save a copy of the encoded node to check against the database visited.add(rlp.encode(node.raw)) # Verify that navigating to the node directly returns the same node as this iterator assert node == trie.traverse(prefix) # Double-check that if the node stores a value, then the implied key matches if node.value: iterated_key = nibbles_to_bytes(prefix + node.suffix) assert node.value == contents[iterated_key] # All nodes should be visited # Note that because of node embedding, the node iterator will return more nodes # than actually exist in the underlying DB (it returns embedded nodes as if they # were not embedded). So we can't simply test that trie.db.values() equals visited here. assert set(trie.db.values()) - visited == set()
Example #11
Source File: state.py From pyquarkchain with MIT License | 6 votes |
def to_dict(self): odict = self.storage_trie.to_dict() for k, v in self.storage_cache.items(): odict[utils.encode_int(k)] = rlp.encode(utils.encode_int(v)) return { "token_balances": self.token_balances.to_dict(), "nonce": str(self.nonce), "code": "0x" + encode_hex(self.code), "storage": { "0x" + encode_hex(key.lstrip(b"\x00") or b"\x00"): "0x" + encode_hex(rlp.decode(val)) for key, val in odict.items() }, } # from ethereum.state import State
Example #12
Source File: refcount_db.py From indy-plenum with Apache License 2.0 | 6 votes |
def dec_refcount(self, k): # raise Exception("WHY AM I CHANGING A REFCOUNT?!:?") node_object = rlp.decode(self._keyValueStorage.get(b'r:' + k)) refcount = utils.decode_int(node_object[0]) if self.logging: sys.stderr.write('decreasing %s to: %d\n' % ( utils.encode_hex(k), refcount - 1)) if not refcount > 0: raise ValueError( "node object for key {} has {} number " "of references, expected > 0" .format(k, refcount) ) self.journal.append([node_object[0], k]) new_refcount = utils.encode_int(refcount - 1) self._keyValueStorage.put( b'r:' + k, rlp.encode([new_refcount, node_object[1]])) if new_refcount == ZERO_ENCODED: self.death_row.append(k)
Example #13
Source File: state.py From pyquarkchain with MIT License | 6 votes |
def serialize(self): # Make sure is committed check(self._not_using_trie or (self.token_trie and self._balances == {})) # Return trie hash if possible if self.token_trie: return b"\x01" + self.token_trie.root_hash # Serialize in-memory balance representation as an array if len(self._balances) == 0: return b"" # Don't serialize 0 balance ls = [TokenBalancePair(k, v) for k, v in self._balances.items() if v > 0] # Sort by token id to make token balances serialization deterministic ls.sort(key=lambda b: b.token_id) return b"\x00" + rlp.encode(ls)
Example #14
Source File: eth_service.py From pyethapp with BSD 3-Clause "New" or "Revised" License | 6 votes |
def on_receive_status(self, proto, eth_version, network_id, chain_difficulty, chain_head_hash, genesis_hash): log.debug('status received', proto=proto, eth_version=eth_version) assert eth_version == proto.version, (eth_version, proto.version) if network_id != self.config['eth'].get('network_id', proto.network_id): log.warn("invalid network id", remote_network_id=network_id, expected_network_id=self.config['eth'].get('network_id', proto.network_id)) raise eth_protocol.ETHProtocolError('wrong network_id') # check genesis if genesis_hash != self.chain.genesis.hash: log.warn("invalid genesis hash", remote_id=proto, genesis=genesis_hash.encode('hex')) raise eth_protocol.ETHProtocolError('wrong genesis block') # request chain self.synchronizer.receive_status(proto, chain_head_hash, chain_difficulty) # send transactions transactions = self.chain.get_transactions() if transactions: log.debug("sending transactions", remote_id=proto) proto.send_transactions(*transactions) # transactions
Example #15
Source File: transactions.py From minter-sdk with MIT License | 6 votes |
def generate_signature(self, private_key): """ Create signature for transaction Args: private_key (str): private key to sign with Returns: hex_signature (str) """ # Get structure populated with instance data and rlp encoded tx_struct = self.generate_tx_rlp() # Create keccak hash tx_rlp = rlp.encode(list(tx_struct.values())) keccak = MinterHelper.keccak_hash(tx_rlp) # Create signature signature = ECDSA.sign(keccak, private_key) signature = rlp.encode(signature).hex() return signature
Example #16
Source File: jsonrpc.py From pyethapp with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _get_block_before_tx(self, txhash): tx, blk, i = self.app.services.chain.chain.index.get_transaction(txhash) # get the state we had before this transaction test_blk = ethereum.blocks.Block.init_from_parent(blk.get_parent(), blk.coinbase, extra_data=blk.extra_data, timestamp=blk.timestamp, uncles=blk.uncles) pre_state = test_blk.state_root for i in range(blk.transaction_count): tx_lst_serialized, sr, _ = blk.get_transaction(i) if sha3(rlp.encode(tx_lst_serialized)) == tx.hash: break else: pre_state = sr test_blk.state.root_hash = pre_state return test_blk, tx, i
Example #17
Source File: jsonrpc.py From pyethapp with BSD 3-Clause "New" or "Revised" License | 6 votes |
def submitWork(self, nonce, mining_hash, mix_digest): print 'submitting work' h = self.chain.chain.head_candidate print 'header: %s' % encode_hex(rlp.encode(h)) if h.header.mining_hash != mining_hash: return False print 'mining hash: %s' % encode_hex(mining_hash) print 'nonce: %s' % encode_hex(nonce) print 'mixhash: %s' % encode_hex(mix_digest) print 'seed: %s' % encode_hex(h.header.seed) h.header.nonce = nonce h.header.mixhash = mix_digest if not h.header.check_pow(): print 'PoW check false' return False print 'PoW check true' self.chain.chain.add_block(h) self.chain.broadcast_newblock(h) print 'Added: %d' % h.header.number return True
Example #18
Source File: check.py From minter-sdk with MIT License | 6 votes |
def proof(cls, address, passphrase=''): """ Create proof Args: address (str) passphrase (str) Returns: str """ # Get address hash address = MinterHelper.prefix_remove(address) address = bytes.fromhex(address) address_hash = cls.__hash(data=[address]) # Create SHA256 from passphrase sha = hashlib.sha256() sha.update(passphrase.encode()) passphrase = sha.hexdigest() # Get signature signature = ECDSA.sign(message=address_hash, private_key=passphrase) return cls.__lockfromsignature(signature).hex()
Example #19
Source File: fast_rlp.py From indy-plenum with Apache License 2.0 | 6 votes |
def main(): import time import state.trie.pruning_trie as trie def run(): st = time.time() x = trie.Trie(KeyValueStorageInMemory()) for i in range(10000): x.update(str(i), str(i**3)) print('elapsed', time.time() - st) return x.root_hash trie.rlp_encode = _encode_optimized print('trie.rlp_encode = encode_optimized') r3 = run() trie.rlp_encode = rlp.codec.encode_raw print('trie.rlp_encode = rlp.codec.encode_raw') r2 = run() assert r2 == r3 trie.rlp_encode = rlp.encode print('trie.rlp_encode = rlp.encode') r = run() assert r == r2
Example #20
Source File: refcount_db.py From indy-plenum with Apache License 2.0 | 6 votes |
def revert_refcount_changes(self, epoch): timeout_epoch = epoch + self.ttl # Delete death row additions try: self._keyValueStorage.remove('deathrow:' + str(timeout_epoch)) except BaseException: pass # Revert journal changes try: journal = rlp.decode( self._keyValueStorage.get('journal:' + str(epoch))) for new_refcount, hashkey in journal[::-1]: node_object = rlp.decode( self._keyValueStorage.get(b'r:' + hashkey)) k = b'r:' + hashkey v = rlp.encode([new_refcount, node_object[1]]) self._keyValueStorage.put(k, v) except BaseException: pass
Example #21
Source File: refcount_db.py From indy-plenum with Apache License 2.0 | 6 votes |
def inc_refcount(self, k, v): # raise Exception("WHY AM I CHANGING A REFCOUNT?!:?") try: node_object = rlp.decode(self._keyValueStorage.get(b'r:' + k)) refcount = utils.decode_int(node_object[0]) self.journal.append([node_object[0], k]) if refcount >= DEATH_ROW_OFFSET: refcount = 0 new_refcount = utils.encode_int(refcount + 1) self._keyValueStorage.put(b'r:' + k, rlp.encode([new_refcount, v])) if self.logging: sys.stderr.write('increasing %s %r to: %d\n' % ( utils.encode_hex(k), v, refcount + 1)) except BaseException: self._keyValueStorage.put(b'r:' + k, rlp.encode([ONE_ENCODED, v])) self.journal.append([ZERO_ENCODED, k]) if self.logging: sys.stderr.write('increasing %s %r to: %d\n' % ( utils.encode_hex(k), v, 1))
Example #22
Source File: evm.py From manticore with GNU Affero General Public License v3.0 | 5 votes |
def block_hash(self, block_number=None, force_recent=True): """ Calculates a block's hash :param block_number: the block number for which to calculate the hash, defaulting to the most recent block :param force_recent: if True (the default) return zero for any block that is in the future or older than 256 blocks :return: the block hash """ if block_number is None: block_number = self.block_number() - 1 # We are not maintaining an actual -block-chain- so we just generate # some hashes for each virtual block value = sha3.keccak_256((repr(block_number) + "NONCE").encode()).hexdigest() value = int(value, 16) if force_recent: # 0 is left on the stack if the looked for block number is greater or equal # than the current block number or more than 256 blocks behind the current # block. (Current block hash is unknown from inside the tx) bnmax = Operators.ITEBV(256, self.block_number() > 256, 256, self.block_number()) value = Operators.ITEBV( 256, Operators.OR(block_number >= self.block_number(), block_number < bnmax), 0, value, ) return value
Example #23
Source File: transactions.py From minter-sdk with MIT License | 5 votes |
def get_sender_address(cls, tx): """ Get sender address from tx. Recover public key from tx and then get address from public key, if tx has single signature type, or get decoded sender address, if tx has multi signature type. Args: tx (dict): transaction dict Returns: Minter address (string) """ # Remember signature data and remove it from tx signature_data = tx.pop('signature_data') # If there is sender address in signature data (multi signature tx), # return it if signature_data.get('from_mx'): return signature_data['from_mx'] # Otherwise (single signature tx), recover public key and get # address from public key # Unhexlify hexdigit dict values to bytes tx = MinterHelper.hex2bin_recursive(tx) # Encode tx data to RLP tx['data'] = rlp.encode(list(tx['data'].values())) # Message tx_rlp = rlp.encode(list(tx.values())) _keccak = MinterHelper.keccak_hash(tx_rlp) # Recover public key public_key = MinterHelper.prefix_add( ECDSA.recover(_keccak, tuple(signature_data.values())), PREFIX_PUBKEY ) return MinterWallet.get_address_from_public_key(public_key)
Example #24
Source File: address.py From eth-tester with MIT License | 5 votes |
def generate_contract_address(address, nonce): next_account_hash = keccak(rlp.encode([address, nonce])) return to_canonical_address(next_account_hash[-20:])
Example #25
Source File: conftest.py From casper with The Unlicense | 5 votes |
def mk_logout_msg_signed(w3): def mk_logout_msg_signed(validator_index, epoch, validation_key): msg_hash = Web3.sha3(rlp.encode([validator_index, epoch])) signed = w3.eth.account.signHash(msg_hash, validation_key) sig = encode_int32(signed.v) + encode_int32(signed.r) + encode_int32(signed.s) return rlp.encode([validator_index, epoch, sig]) return mk_logout_msg_signed
Example #26
Source File: base.py From py-evm with MIT License | 5 votes |
def import_block(self, block: BlockAPI) -> BlockAndMetaWitness: if self.get_block().number != block.number: raise ValidationError( f"This VM can only import blocks at number #{self.get_block().number}," f" the attempted block was #{block.number}" ) self._block = self.get_block().copy( header=self.configure_header( coinbase=block.header.coinbase, difficulty=block.header.difficulty, gas_limit=block.header.gas_limit, timestamp=block.header.timestamp, extra_data=block.header.extra_data, mix_hash=block.header.mix_hash, nonce=block.header.nonce, uncles_hash=keccak(rlp.encode(block.uncles)), ), uncles=block.uncles, ) execution_context = self.create_execution_context( block.header, self.previous_hashes, self.chain_context) header = self.get_header() # we need to re-initialize the `state` to update the execution context. self._state = self.get_state_class()(self.chaindb.db, execution_context, header.state_root) # run all of the transactions. new_header, receipts, _ = self.apply_all_transactions(block.transactions, header) self._block = self.set_block_transactions( self.get_block(), new_header, block.transactions, receipts, ) return self.mine_block()
Example #27
Source File: serializers.py From eth-tester with MIT License | 5 votes |
def serialize_block(block, full_transaction, is_pending): if full_transaction: transaction_serializer = serialize_transaction else: transaction_serializer = serialize_transaction_hash transactions = [ transaction_serializer(block, transaction, index, is_pending) for index, transaction in enumerate(block.transactions) ] if block.uncles: raise NotImplementedError("Uncle serialization has not been implemented") return { "number": block.header.block_number, "hash": block.header.hash, "parent_hash": block.header.parent_hash, "nonce": block.header.nonce, "sha3_uncles": block.header.uncles_hash, "logs_bloom": block.header.bloom, "transactions_root": block.header.transaction_root, "receipts_root": block.header.receipt_root, "state_root": block.header.state_root, "miner": block.header.coinbase, "difficulty": block.header.difficulty, "total_difficulty": block.header.difficulty, # TODO: actual total difficulty "size": len(rlp.encode(block)), "extra_data": pad32(block.header.extra_data), "gas_limit": block.header.gas_limit, "gas_used": block.header.gas_used, "timestamp": block.header.timestamp, "transactions": transactions, "uncles": [uncle.hash for uncle in block.uncles], }
Example #28
Source File: conftest.py From casper with The Unlicense | 5 votes |
def mk_vote(w3): def mk_vote(validator_index, target_hash, target_epoch, source_epoch, validation_key): msg_hash = w3.sha3( rlp.encode([validator_index, target_hash, target_epoch, source_epoch]) ) signed = w3.eth.account.signHash(msg_hash, validation_key) sig = encode_int32(signed.v) + encode_int32(signed.r) + encode_int32(signed.s) return rlp.encode([validator_index, target_hash, target_epoch, source_epoch, sig]) return mk_vote
Example #29
Source File: transactions.py From minter-sdk with MIT License | 5 votes |
def generate_tx_rlp(self): """ Create structure from instance and prepare rlp structure Returns: tx_struct (dict) """ # Get structure populated with instance data tx_struct = self._structure_from_instance() # Remove signature data, it's not needed before getting Keccak tx_struct.pop('signature_data') # Encode tx data to RLP tx_struct['data'] = rlp.encode(list(tx_struct['data'].values())) return tx_struct
Example #30
Source File: transactions.py From minter-sdk with MIT License | 5 votes |
def generate_signed_tx(tx_struct): """ Generate signed tx hash from it's structure Args: tx_struct (dict): populated tx structure dict Returns: signed_tx hash (str) """ tx_rlp = rlp.encode(list(tx_struct.values())) return tx_rlp.hex()