def test_encode_unsigned_integer(integer_value, value_bit_size, data_byte_size):
    if value_bit_size > data_byte_size * 8:
        with pytest.raises(ValueError) as exception_info:
            UnsignedIntegerEncoder(
                value_bit_size=value_bit_size,
                data_byte_size=data_byte_size,
            )
        return

    encoder = UnsignedIntegerEncoder(
        value_bit_size=value_bit_size,
        data_byte_size=data_byte_size,
    )
    lower_bound, upper_bound = compute_unsigned_integer_bounds(value_bit_size)

    if not is_integer(integer_value):
        with pytest.raises(EncodingTypeError) as exception_info:
            encoder(integer_value)
        assert 'UnsignedInteger' in str(exception_info.value)
        return
    elif integer_value < lower_bound or integer_value > upper_bound:
        with pytest.raises(ValueOutOfBounds):
            encoder(integer_value)
        return

    if integer_value >= 0:
        expected_value = zpad(int_to_big_endian(integer_value), data_byte_size)
    else:
        expected_value = fpad(int_to_big_endian(integer_value), data_byte_size)

    encoded_value = encoder(integer_value)

    assert encoded_value == expected_value
Beispiel #2
0
 def _block_to_dict(self, block):
     logs_bloom = encode_hex(int_to_big_endian(block.header.bloom))[2:]
     logs_bloom = '0x' + logs_bloom.rjust(512, '0')
     return {
         "difficulty": hex(block.header.difficulty),
         "extraData": encode_hex(block.header.extra_data),
         "gasLimit": hex(block.header.gas_limit),
         "gasUsed": hex(block.header.gas_used),
         "hash": encode_hex(block.header.hash),
         "logsBloom": logs_bloom,
         "mixHash": encode_hex(block.header.mix_hash),
         "nonce": encode_hex(block.header.nonce),
         "number": hex(block.header.block_number),
         "parentHash": encode_hex(block.header.parent_hash),
         "receiptsRoot": encode_hex(block.header.receipt_root),
         "sha3Uncles": encode_hex(block.header.uncles_hash),
         "stateRoot": encode_hex(block.header.state_root),
         "timestamp": hex(block.header.timestamp),
         "totalDifficulty": hex(self.chain.chaindb.get_score(block.hash)),
         "transactions": [encode_hex(tx.hash) for tx in block.transactions],
         "transactionsRoot": encode_hex(block.header.transaction_root),
         "uncles": [encode_hex(uncle.hash) for uncle in block.uncles],
         "size": hex(len(rlp.encode(block))),
         "miner": encode_hex(block.header.coinbase),
     }
Beispiel #3
0
    def getStorageAt(self, address, position, at_block):
        if not is_integer(position) or position < 0:
            raise TypeError("Position of storage must be a whole number, but was: %r" % position)

        with state_at_block(self._chain, at_block) as state:
            stored_val = state.get_storage(address, position)
        return encode_hex(int_to_big_endian(stored_val))
    def encode_fn(self, value):
        with decimal.localcontext(abi_decimal_context):
            scaled_value = value * TEN ** self.frac_places
            integer_value = int(scaled_value)

        unsigned_integer_value = integer_value % (2 ** self.value_bit_size)

        return int_to_big_endian(unsigned_integer_value)
def test_decode_array_of_unsigned_integers(array_size, array_values):
    size_bytes = zpad32(int_to_big_endian(array_size))
    values_bytes = b''.join((
        zpad32(int_to_big_endian(v)) for v in array_values
    ))
    stream_bytes = size_bytes + values_bytes

    decoder = DynamicArrayDecoder(
        item_decoder=UnsignedIntegerDecoder(value_bit_size=256),
    )
    stream = ContextFramesBytesIO(stream_bytes)

    if len(array_values) < array_size:
        with pytest.raises(InsufficientDataBytes):
            decoder(stream)
        return

    actual_values = decoder(stream)
    assert actual_values == array_values[:array_size]
Beispiel #6
0
def to_text(primitive=None, hexstr=None, text=None):
    assert_one_val(primitive, hexstr=hexstr, text=text)

    if hexstr is not None:
        return to_bytes(hexstr=hexstr).decode('utf-8')
    elif text is not None:
        return text
    elif isinstance(primitive, str):
        return to_text(hexstr=primitive)
    elif isinstance(primitive, bytes):
        return primitive.decode('utf-8')
    elif is_integer(primitive):
        byte_encoding = int_to_big_endian(primitive)
        return to_text(byte_encoding)
    raise TypeError("Expected an int, bytes or hexstr.")
def test_decode_strings(_strings, pad_size):
    size_bytes = zpad32(int_to_big_endian(len(_strings.encode("utf-8"))))
    padded_bytes = _strings.encode("utf-8") + b'\x00' * pad_size
    stream_bytes = size_bytes + padded_bytes
    stream = ContextFramesBytesIO(stream_bytes)

    decoder = StringDecoder()

    if len(padded_bytes) < ceil32(len(_strings.encode("utf-8"))):
        with pytest.raises(InsufficientDataBytes):
            decoder(stream)
        return

    decoded_value = decoder(stream)
    assert decoded_value == _strings
def test_decode_strings_raises(_bytes, pad_size):
    size_bytes = zpad32(int_to_big_endian(len(_bytes)))
    padded_bytes = _bytes + b'\x00' * pad_size
    stream_bytes = size_bytes + padded_bytes
    stream = ContextFramesBytesIO(stream_bytes)

    decoder = StringDecoder()

    if len(padded_bytes) < ceil32(len(_bytes)):
        with pytest.raises(InsufficientDataBytes):
            decoder(stream)
        return

    with pytest.raises(DecodingError):
        decoder(stream)
Beispiel #9
0
def to_text(primitive=None, hexstr=None, text=None):
    if bytes is str:
        # must be able to tell the difference between bytes and a hexstr
        raise NotImplementedError("This method only works in Python 3+.")

    assert_one_val(primitive, hexstr=hexstr, text=text)

    if hexstr is not None:
        return to_bytes(hexstr=hexstr).decode('utf-8')
    elif text is not None:
        return text
    elif isinstance(primitive, str):
        return to_text(hexstr=primitive)
    elif isinstance(primitive, bytes):
        return primitive.decode('utf-8')
    elif is_integer(primitive):
        byte_encoding = int_to_big_endian(primitive)
        return to_text(byte_encoding)
    raise TypeError("Expected an int, bytes or hexstr.")
Beispiel #10
0
def generate_privkey():
    """Generate a new SECP256K1 private key and return it"""
    privkey = ec.generate_private_key(CURVE, default_backend())
    return keys.PrivateKey(pad32(int_to_big_endian(privkey.private_numbers().private_value)))
Beispiel #11
0
 def send_find_node_v4(self, node: NodeAPI, target_node_id: int) -> None:
     node_id = int_to_big_endian(
         target_node_id).rjust(constants.KADEMLIA_PUBLIC_KEY_SIZE // 8, b'\0')
     self.logger.debug2('>>> find_node to %s', node)
     message = _pack_v4(CMD_FIND_NODE.id, tuple([node_id]), self.privkey)
     self.send(node, message)
Beispiel #12
0
 def process(self, data):
     swap_completed = data
     log.debug('Offer {} has been successfully traded'.format(
         pex(int_to_big_endian(swap_completed.offer_id))))
     self.trades.report_completed(swap_completed.offer_id,
                                  swap_completed.timestamp)
Beispiel #13
0
def random_private_key(bound):
    """Randomly gnerate a private key smaller than a certain bound."""
    n = random.randint(1, bound)  # nosec
    private_key = encode_hex(pad_left(int_to_big_endian(n), 32, '\0'))
    return private_key
Beispiel #14
0
def generate_random_keypair():
    key_object = keys.PrivateKey(
        pad32(int_to_big_endian(random.getrandbits(8 * 32))))
    return key_object.to_bytes(), key_object.public_key.to_canonical_address()
Beispiel #15
0
 def send_find_node_v4(self, node: NodeAPI, target_node_id: int) -> None:
     expiration = _get_msg_expiration()
     node_id = int_to_big_endian(target_node_id).rjust(
         constants.KADEMLIA_PUBLIC_KEY_SIZE // 8, b'\0')
     self.logger.debug2('>>> find_node to %s', node)
     self.send(node, CMD_FIND_NODE, (node_id, expiration))
Beispiel #16
0
def random_pubkey():
    pk = int_to_big_endian(random.getrandbits(kademlia.k_pubkey_size))
    return keys.PublicKey(b'\x00' * (kademlia.k_pubkey_size // 8 - len(pk)) +
                          pk)
Beispiel #17
0
DATA_LOGGING_CONTRACT_CODE = (
    b"\x36\x60\x00\x60\x00\x37\x36\x60\x00\x20\x60\x00\x52\x60\x20\x60\x00\xa0"
)
DATA_LOGGING_CONTRACT_ADDRESS = generate_CREATE2_contract_address(b"", DATA_LOGGING_CONTRACT_CODE)

HELPER_CONTRACTS = {
    ACCOUNT_ADDRESS: ACCOUNT_CODE,
    NOOP_CONTRACT_ADDRESS: NOOP_CONTRACT_CODE,
    FAILING_CONTRACT_ADDRESS: FAILING_CONTRACT_CODE,
    GAS_LOGGING_CONTRACT_ADDRESS: GAS_LOGGING_CONTRACT_CODE,
    DATA_LOGGING_CONTRACT_ADDRESS: DATA_LOGGING_CONTRACT_CODE,
}


DESTINATION_ADDRESS = b"\xbb" * 20
ECRECOVER_ADDRESS = zpad_left(int_to_big_endian(ECRECOVER_ADDRESS_INT), 20)

DEFAULT_BASE_TX_PARAMS = {
    "chain_id": 1,
    "shard_id": 1,
    "to": ACCOUNT_ADDRESS,
    "gas": 500000,
    "access_list": [
        [ACCOUNT_ADDRESS, b"\x00" * 32],
        [ECRECOVER_ADDRESS],
    ],
    "code": b"",
    "salt": b"\x00" * 32,
}

DEFAULT_TX_PARAMS = merge(
Beispiel #18
0
def encode_raw_public_key(raw_public_key: Tuple[int, int]) -> bytes:
    left, right = raw_public_key
    return b''.join((
        pad32(int_to_big_endian(left)),
        pad32(int_to_big_endian(right)),
    ))
Beispiel #19
0
def int_to_hex(n: int, byte_length: int = None) -> str:
    byte_value = int_to_big_endian(n)
    if byte_length:
        byte_value = byte_value.rjust(byte_length, b'\x00')
    return encode_hex(byte_value)
Beispiel #20
0
 def send_find_node(self, node: kademlia.Node, target_node_id: int) -> None:
     node_id = int_to_big_endian(target_node_id).rjust(
         kademlia.k_pubkey_size // 8, b'\0')
     self.logger.debug('>>> find_node to %s', node)
     message = _pack(CMD_FIND_NODE.id, [node_id], self.privkey)
     self.send(node, message)
Beispiel #21
0
def random_pubkey():
    pk = int_to_big_endian(random.getrandbits(kademlia.k_pubkey_size))
    return keys.PublicKey(b'\x00' * (kademlia.k_pubkey_size // 8 - len(pk)) + pk)
Beispiel #22
0
def _mk_private_key_bytes() -> bytes:
    return int_to_big_endian(secrets.randbits(256)).rjust(32, b"\x00")
Beispiel #23
0
def to_receipt_response(receipt: ReceiptAPI, transaction: SignedTransactionAPI,
                        index: int, header: BlockHeaderAPI,
                        tx_gas_used: int) -> RpcReceiptResponse:

    if transaction.to == CREATE_CONTRACT_ADDRESS:
        contract_address = encode_hex(
            generate_contract_address(transaction.sender, transaction.nonce))
    else:
        contract_address = None

    block_hash = encode_hex(header.hash)
    block_number = hex(header.block_number)
    receipt_and_transaction_index = hex(index)
    transaction_hash = encode_hex(transaction.hash)

    return {
        "blockHash":
        block_hash,
        "blockNumber":
        block_number,
        "contractAddress":
        contract_address,
        "cumulativeGasUsed":
        hex(receipt.gas_used),
        "from":
        encode_hex(transaction.sender),
        'gasUsed':
        hex(tx_gas_used),
        "logs": [
            {
                "address":
                encode_hex(log.address),
                "data":
                encode_hex(log.data),
                "blockHash":
                block_hash,
                "blockNumber":
                block_number,
                "logIndex":
                receipt_and_transaction_index,
                # We only serve receipts from transactions that ended up in the canonical chain
                # which means this can never be `True`
                "removed":
                False,
                "topics":
                [encode_hex(int_to_big_endian(topic)) for topic in log.topics],
                "transactionHash":
                transaction_hash,
                "transactionIndex":
                receipt_and_transaction_index,
            } for log in receipt.logs
        ],
        "logsBloom":
        format_bloom(receipt.bloom),
        "root":
        encode_hex(receipt.state_root),
        "to":
        encode_hex(transaction.to),
        "transactionHash":
        transaction_hash,
        "transactionIndex":
        receipt_and_transaction_index,
    }
Beispiel #24
0
 def bloom(self, value: int) -> None:
     self._bloom = int_to_big_endian(value)
 def encode_fn(self, value):
     scaled_value = value * 2**self.low_bit_size
     integer_value = int(scaled_value)
     return int_to_big_endian(integer_value)
Beispiel #26
0
 def __repr__(self):
     return "Offer<pex(id)={} amount={} price={} type={} timeout={}>".format(
         pex(int_to_big_endian(self.offer_id)), self.base_amount,
         self.price, self.type, to_str_repr(self.timeout_date))
Beispiel #27
0
def get_nonce(vm):
    computation, _ = vm.apply_transaction(ShardingTransaction(**merge(DEFAULT_BASE_TX_PARAMS, {
        "data": int_to_big_endian(NONCE_GETTER_ID),
    })))
    return big_endian_to_int(computation.output)
Beispiel #28
0
 def __bytes__(self) -> bytes:
     vb = int_to_byte(self.v)
     rb = pad32(int_to_big_endian(self.r))
     sb = pad32(int_to_big_endian(self.s))
     return b''.join((rb, sb, vb))
Beispiel #29
0
def generate_privkey() -> datatypes.PrivateKey:
    """Generate a new SECP256K1 private key and return it"""
    privkey = ec.generate_private_key(CURVE, default_backend())
    return keys.PrivateKey(
        pad32(int_to_big_endian(privkey.private_numbers().private_value)))
Beispiel #30
0
 def to_bytes(self) -> bytes:
     return b"".join(
         (int_to_big_endian(self.message_type), rlp.encode(self)))
Beispiel #31
0
def _generate_dummy_address(idx):
    return to_canonical_address(
        decode_hex('0xabbacadaba') + zpad(int_to_big_endian(idx), 15)
    )
def hashes():
    """A generator that produces an infinite, non-repeatable sequence of hashes."""
    return (int_to_big_endian(12345).rjust(32, b"\x00") for counter in count())
Beispiel #33
0
def _create_v3_keyfile_json(private_key, password, kdf, work_factor=None):
    salt = Random.get_random_bytes(16)

    if work_factor is None:
        work_factor = get_default_work_factor_for_kdf(kdf)

    if kdf == 'pbkdf2':
        derived_key = _pbkdf2_hash(
            password,
            hash_name='sha256',
            salt=salt,
            iterations=work_factor,
            dklen=DKLEN,
        )
        kdfparams = {
            'c': work_factor,
            'dklen': DKLEN,
            'prf': 'hmac-sha256',
            'salt': encode_hex_no_prefix(salt),
        }
    elif kdf == 'scrypt':
        derived_key = _scrypt_hash(
            password,
            salt=salt,
            buflen=DKLEN,
            r=SCRYPT_R,
            p=SCRYPT_P,
            n=work_factor,
        )
        kdfparams = {
            'dklen': DKLEN,
            'n': work_factor,
            'r': SCRYPT_R,
            'p': SCRYPT_P,
            'salt': encode_hex_no_prefix(salt),
        }
    else:
        raise NotImplementedError("KDF not implemented: {0}".format(kdf))

    iv = big_endian_to_int(Random.get_random_bytes(16))
    encrypt_key = derived_key[:16]
    ciphertext = encrypt_aes_ctr(private_key, encrypt_key, iv)
    mac = keccak(derived_key[16:32] + ciphertext)

    address = keys.PrivateKey(private_key).public_key.to_address()

    return {
        'address': remove_0x_prefix(address),
        'crypto': {
            'cipher': 'aes-128-ctr',
            'cipherparams': {
                'iv': encode_hex_no_prefix(int_to_big_endian(iv)),
            },
            'ciphertext': encode_hex_no_prefix(ciphertext),
            'kdf': kdf,
            'kdfparams': kdfparams,
            'mac': encode_hex_no_prefix(mac),
        },
        'id': str(uuid.uuid4()),
        'version': 3,
    }
Beispiel #34
0
def int_to_bytes(value):
    if isinstance(value, bytes):
        return value
    return int_to_big_endian(value)
Beispiel #35
0
 def set(self, slot: int, value: int) -> None:
     key = int_to_big_endian(slot)
     if value:
         self._journal_storage[key] = rlp.encode(value)
     else:
         del self._journal_storage[key]
 def encode_fn(self, value):
     return int_to_big_endian(value % (2**self.value_bit_size))
 def encode_fn(self, value):
     return int_to_big_endian(value % (2 ** self.value_bit_size))
    def encode_fn(self, value):
        with decimal.localcontext(abi_decimal_context):
            scaled_value = value * TEN**self.frac_places
            integer_value = int(scaled_value)

        return int_to_big_endian(integer_value)
Beispiel #39
0
def encode_int(v):
    """encodes an integer into serialization"""
    if not is_numeric(v) or v < 0 or v >= TT256:
        raise Exception("Integer invalid or out of range: %r" % v)
    return int_to_big_endian(v)
Beispiel #40
0
def format_bloom(bloom: int) -> str:
    formatted_bloom = encode_hex(int_to_big_endian(bloom))[2:]
    formatted_bloom = '0x' + formatted_bloom.rjust(512, '0')
    return formatted_bloom
Beispiel #41
0
def encode_int256(v):
    return zpad(int_to_big_endian(v), 256)
Beispiel #42
0
def fixed_len_byte(value, fixed_len=8):
    int_bytes = eth_utils.int_to_big_endian(value)
    # print(value, list(int_bytes))
    result_bytes = bytearray(0 for _ in range(fixed_len - len(int_bytes)))
    result_bytes.extend(int_bytes)
    return result_bytes