Ejemplo n.º 1
0
def test_get_x_coordinate(message, domain):
    x_coordinate = _get_x_coordinate(message, domain)
    domain_in_bytes = domain.to_bytes(8, 'big')
    assert x_coordinate == FQ2(
        [
            big_endian_to_int(hash_eth2(message + domain_in_bytes + b'\x01')),
            big_endian_to_int(hash_eth2(message + domain_in_bytes + b'\x02')),
        ]
    )
Ejemplo n.º 2
0
def _get_x_coordinate(message_hash: Hash32, domain: int) -> FQ2:
    domain_in_bytes = domain.to_bytes(8, 'big')

    # Initial candidate x coordinate
    x_re = big_endian_to_int(hash_eth2(message_hash + domain_in_bytes + b'\x01'))
    x_im = big_endian_to_int(hash_eth2(message_hash + domain_in_bytes + b'\x02'))
    x_coordinate = FQ2([x_re, x_im])  # x_re + x_im * i

    return x_coordinate
Ejemplo n.º 3
0
 def compress_public_key_bytes(
         self, uncompressed_public_key_bytes: bytes) -> bytes:
     validate_uncompressed_public_key_bytes(uncompressed_public_key_bytes)
     point = (
         big_endian_to_int(uncompressed_public_key_bytes[:32]),
         big_endian_to_int(uncompressed_public_key_bytes[32:]),
     )
     public_key = self.keys.PublicKey.from_point(*point)
     return public_key.format(compressed=True)
Ejemplo n.º 4
0
def ecsign(rawhash, key):
    if coincurve and hasattr(coincurve, 'PrivateKey'):
        pk = coincurve.PrivateKey(key)
        signature = pk.sign_recoverable(rawhash, hasher=None)
        v = safe_ord(signature[64]) + 27
        r = big_endian_to_int(signature[0:32])
        s = big_endian_to_int(signature[32:64])
    else:
        v, r, s = ecdsa_raw_sign(rawhash, key)
    return v, r, s
Ejemplo n.º 5
0
def pubkey_to_g2(pub: Pubkey) -> G2Point:
    g2 = (
        FQ2([big_endian_to_int(pub[:32]),
             big_endian_to_int(pub[32:64])]),
        FQ2([big_endian_to_int(pub[64:96]),
             big_endian_to_int(pub[96:])]),
        FQ2.one(),
    )
    assert is_valid_g2_point(g2)
    return g2
Ejemplo n.º 6
0
def get_publickey_from_address(web3, address):
    _hash = Web3.sha3(text='verify signature.')
    signature = split_signature(web3, web3.eth.sign(address, _hash))
    signature_vrs = Signature(signature.v % 27, big_endian_to_int(signature.r),
                              big_endian_to_int(signature.s))
    prefixed_hash = prepare_prefixed_hash(_hash)
    pub_key = KeyAPI.PublicKey.recover_from_msg_hash(
        prefixed_hash, KeyAPI.Signature(vrs=signature_vrs))
    assert pub_key.to_checksum_address(
    ) == address, 'recovered address does not match signing address.'
    return pub_key
Ejemplo n.º 7
0
def parse_collation_added_log(log):
    # `shard_id` is the first indexed entry,hence the second entry in topics
    shard_id_bytes32 = log['topics'][1]
    data_bytes = decode_hex(log['data'])
    header_bytes = shard_id_bytes32 + data_bytes[:-64]
    is_new_head = bool(big_endian_to_int(data_bytes[-64:-32]))
    score = big_endian_to_int(data_bytes[-32:])
    collation_header = CollationHeader.from_bytes(header_bytes)
    yield 'header', collation_header
    yield 'is_new_head', is_new_head
    yield 'score', score
Ejemplo n.º 8
0
def test_delete_ranges(blank_state, mock_accts):
    state = make_simple_state(blank_state, mock_accts)
    assert [0, 0] == [
        big_endian_to_int(r[8:40]) for r in state.get_ranges(0, 0, 9)
    ]
    ranges_to_delete = state.get_ranges(0, 10, 29)
    assert [10, 10, 20,
            20] == [big_endian_to_int(r[8:40]) for r in ranges_to_delete]
    state.delete_ranges(ranges_to_delete)
    new_ranges = state.get_ranges(0, 10, 29)
    assert [0, 0] == [big_endian_to_int(r[8:40]) for r in new_ranges]
Ejemplo n.º 9
0
def ecdsa_raw_sign(msg_hash, private_key_bytes):
    z = big_endian_to_int(msg_hash)
    k = deterministic_generate_k(msg_hash, private_key_bytes)

    r, y = fast_multiply(G, k)
    s_raw = inv(k, N) * (z + r * big_endian_to_int(private_key_bytes)) % N

    v = 27 + ((y % 2) ^ (0 if s_raw * 2 < N else 1))
    s = s_raw if s_raw * 2 < N else N - s_raw

    return v - 27, r, s
Ejemplo n.º 10
0
    def get_trx_receipts(self, unsigned_msg, signature):
        trx = rlp.decode(unsigned_msg, EthTrx)

        v = int(signature[64]) + 35 + 2 * trx[6]
        r = big_endian_to_int(signature[0:32])
        s = big_endian_to_int(signature[32:64])

        trx_raw = rlp.encode(EthTrx(trx[0], trx[1], trx[2], trx[3], trx[4], trx[5], v, r, s), EthTrx)
        eth_signature = '0x' + sha3(trx_raw).hex()
        from_address = w3.eth.account.recover_transaction(trx_raw).lower()

        return (trx_raw.hex(), eth_signature, from_address)
Ejemplo n.º 11
0
def mk_transaction():
    return BaseTransactionFields(
        nonce=0,
        gas=21000,
        gas_price=1,
        to=os.urandom(20),
        value=random.randint(0, 100),
        data=b'',
        v=27,
        r=big_endian_to_int(os.urandom(32)),
        s=big_endian_to_int(os.urandom(32)),
    )
Ejemplo n.º 12
0
def _compute_adjusted_exponent_length(exponent_length: int,
                                      first_32_exponent_bytes: bytes) -> int:
    exponent = big_endian_to_int(first_32_exponent_bytes)

    if exponent_length <= 32 and exponent == 0:
        return 0
    elif exponent_length <= 32:
        return get_highest_bit_index(exponent)
    else:
        first_32_bytes_as_int = big_endian_to_int(first_32_exponent_bytes)
        return (8 * (exponent_length - 32) +
                get_highest_bit_index(first_32_bytes_as_int))
Ejemplo n.º 13
0
def _extract_lengths(data: bytes) -> Tuple[int, int, int]:
    # extract argument lengths
    base_length_bytes = pad32r(data[:32])
    base_length = big_endian_to_int(base_length_bytes)

    exponent_length_bytes = pad32r(data[32:64])
    exponent_length = big_endian_to_int(exponent_length_bytes)

    modulus_length_bytes = pad32r(data[64:96])
    modulus_length = big_endian_to_int(modulus_length_bytes)

    return base_length, exponent_length, modulus_length
Ejemplo n.º 14
0
def decode_receipt_event(data):
    data = to_bytes(data)

    # Fixed position parameters
    success = big_endian_to_int(data[:32]) != 0
    result_start = big_endian_to_int(data[32:64])
    result_size = big_endian_to_int(data[result_start:result_start + 32])

    # Dynamic position parameters
    result = from_bytes(data[result_start + 32:result_start + 32 +
                             result_size])

    return {"success": success, "result": result}
Ejemplo n.º 15
0
def _ecmull(data: bytes) -> Tuple[bn128.FQ, bn128.FQ]:
    x_bytes = pad32r(data[:32])
    y_bytes = pad32r(data[32:64])
    m_bytes = pad32r(data[64:96])

    x = big_endian_to_int(x_bytes)
    y = big_endian_to_int(y_bytes)
    m = big_endian_to_int(m_bytes)

    p = validate_point(x, y)

    result = bn128.normalize(bn128.multiply(p, m))
    return result
Ejemplo n.º 16
0
    def sign(self, message):
        msg_hash = bytes(bytearray.fromhex(remove_0x_prefix(message)))

        signature_bytes = self.private_key.sign_recoverable(msg_hash,
                                                            hasher=None)

        assert len(signature_bytes) == 65

        r = "0x" + format(big_endian_to_int(signature_bytes[0:32]), "x")
        s = "0x" + format(big_endian_to_int(signature_bytes[32:64]), "x")
        v = hex(ord(signature_bytes[64:65]) + 27)

        return {"r": r, "s": s, "v": v}
Ejemplo n.º 17
0
Archivo: pow.py Proyecto: sjyi/py-evm
def check_pow(block_number: int, mining_hash: Hash32, mix_hash: Hash32,
              nonce: bytes, difficulty: int) -> None:
    validate_length(mix_hash, 32, title="Mix Hash")
    validate_length(mining_hash, 32, title="Mining Hash")
    validate_length(nonce, 8, title="POW Nonce")
    cache = get_cache(block_number)
    mining_output = hashimoto_light(block_number, cache, mining_hash,
                                    big_endian_to_int(nonce))
    if mining_output[b'mix digest'] != mix_hash:
        raise ValidationError("mix hash mismatch; {0} != {1}".format(
            encode_hex(mining_output[b'mix digest']), encode_hex(mix_hash)))
    result = big_endian_to_int(mining_output[b'result'])
    validate_lte(result, 2**256 // difficulty, title="POW Difficulty")
def mk_transaction():
    return LegacyTransactionFactory(
        nonce=0,
        gas=21000,
        gas_price=1,
        to=os.urandom(20),
        value=random.randint(0, 100),
        data=b'',
        vrs=(
            27,
            big_endian_to_int(os.urandom(32)),
            big_endian_to_int(os.urandom(32)),
        ),
    )
Ejemplo n.º 19
0
 def _parse_perpetual_update_position_event_logs(self, block_number):
     event_filter_params = {
         'topics': [self._perpetual_position_topic],
         'address': [self._perpetual_address],
         'fromBlock': block_number,
         'toBlock': block_number,
     }
     event_data = []
     logs = self._perpetual.web3.eth.getLogs(event_filter_params)
     for log in logs:
         parsed = {}
         parsed['blockNumber'] = log['blockNumber']
         parsed['blockHash'] = log['blockHash']
         parsed['transactionIndex'] = log['transactionIndex']
         parsed['logIndex'] = log['logIndex']
         parsed['transactionHash'] = log['transactionHash'].hex()
         parsed['trader'] = '0x' + log['topics'][1].hex()[26:].lower()
         data = HexBytes(log['data'])
         if len(data) != 32 * 8:
             raise Exception(f'malformed event: {parsed}')
         parsed['side'] = big_endian_to_int(data[32*0:32*1])
         parsed['size'] = big_endian_to_int(data[32*1:32*2])
         parsed['entryValue'] = big_endian_to_int(data[32*2:32*3])
         parsed['entrySocialLoss'] = big_endian_to_int(data[32*3:32*4])
         parsed['entryFundingLoss'] = big_endian_to_int(data[32*4:32*5])
         parsed['cashBalance'] = big_endian_to_int(data[32*5:32*6])
         parsed['perpetualTotalSize'] = big_endian_to_int(data[32*6:32*7])
         parsed['price'] = big_endian_to_int(data[32*7:32*8])
         event_data.append(parsed)
     return event_data
Ejemplo n.º 20
0
def test_get_ranges(blank_state, mock_accts):
    state = make_simple_state(blank_state, mock_accts)
    assert [0, 0] == [
        big_endian_to_int(r[8:40]) for r in state.get_ranges(0, 0, 9)
    ]
    assert [10, 10, 20, 20] == [
        big_endian_to_int(r[8:40]) for r in state.get_ranges(0, 10, 29)
    ]
    assert [0, 0, 10, 10] == [
        big_endian_to_int(r[8:40]) for r in state.get_ranges(0, 5, 19)
    ]
    assert [0, 0, 10, 10, 20, 20] == [
        big_endian_to_int(r[8:40]) for r in state.get_ranges(0, 5, 29)
    ]
Ejemplo n.º 21
0
 def add_transfer(self, transfer, affected_ranges):
     sender, recipient, token_id, start, offset = self.get_converted_parameters(
         addresses=(transfer.sender, transfer.recipient),
         bytes8s=(transfer.token_id, ),
         bytes32s=(transfer.start, transfer.offset))
     end = token_id + int_to_big_endian32(
         big_endian_to_int(start) +
         big_endian_to_int(offset)) + OFFSET_SUFFIX
     # Shorten first range if needed
     if get_start_to_offset_key(token_id, start) != affected_ranges[0]:
         # TODO: Add
         self.db.put(
             affected_ranges[0],
             int_to_big_endian32(
                 big_endian_to_int(start) -
                 big_endian_to_int(affected_ranges[0][8:40])))
         print(
             'setting new end offset to:',
             big_endian_to_int(start) -
             big_endian_to_int(affected_ranges[0][8:40]))
         del affected_ranges[0:2]
     # Shorten last range if needed
     if len(affected_ranges) != 0 and end != affected_ranges[-1]:
         self.db.put(affected_ranges[-2], start + offset)
         print('setting new start to:',
               big_endian_to_int(start) + big_endian_to_int(offset))
         del affected_ranges[-2:]
Ejemplo n.º 22
0
def verify(public_key: str,
           signature: str,
           message: Optional[str] = None,
           message_hash: Optional[str] = None) -> bool:
    """
    Verify XinFin signature by public key.

    :param public_key: XinFin public key.
    :type public_key: str.
    :param signature: Signed message data.
    :type signature: str.
    :param message: Message data, default to ``None``.
    :type message: str.
    :param message_hash: Message data hash, default to ``None``.
    :type message_hash: str.

    :return: bool -- Verified signature.

    >>> from pyxdc.signature import verify
    >>> verify(public_key="03d8799336beacc6b2e7f86f46bce4ad5cabf1ec7a0d6241416985e3b29fe1cc85", message="meherett", signature="74ad07a84b87fa3efa2f0e825506fb8bbee41021ca77a30e8ffa2bd66d47d99917d4a0587185e78a051a9cb80ebf65c7d62dbeedb7f9a029f961d70b52a10dc001")
    True
    >>> verify(public_key="03d8799336beacc6b2e7f86f46bce4ad5cabf1ec7a0d6241416985e3b29fe1cc85", message_hash="4bbbfd0c33fea618f4a9aa75c02fe76e50fa59798af021bc34f7856f3259c685", signature="74ad07a84b87fa3efa2f0e825506fb8bbee41021ca77a30e8ffa2bd66d47d99917d4a0587185e78a051a9cb80ebf65c7d62dbeedb7f9a029f961d70b52a10dc001")
    True
    """

    if message:
        message_bytes = curried.to_bytes(primitive=None,
                                         hexstr=None,
                                         text=message)
        msg_length = str(len(message_bytes)).encode('utf-8')
        joined = b'\x19' + b'E' + b'thereum Signed Message:\n' + msg_length + message_bytes
        keccak_256_message = keccak_256()
        keccak_256_message.update(joined)
        message_hash = keccak_256_message.digest()
    elif message_hash:
        message_hash = unhexlify(message_hash)
    else:
        raise ValueError("Message data or hash is required to sign.")

    signature = unhexlify(signature)
    validate_recoverable_signature_bytes(signature)
    r = big_endian_to_int(signature[0:32])
    s = big_endian_to_int(signature[32:64])
    v = ord(signature[64:65])
    validate_compressed_public_key_bytes(unhexlify(public_key))
    uncompressed_public_key = decompress_public_key(unhexlify(public_key))

    return ecdsa_raw_verify(msg_hash=message_hash,
                            rs=(r, s),
                            public_key_bytes=uncompressed_public_key)
Ejemplo n.º 23
0
 def _parse_value(self, *, val_type: str,
                  val: bytes) -> Union[bool, Address, bytes, int]:
     if val_type == 'bool':
         return bool(big_endian_to_int(val))
     elif val_type == 'address':
         return to_canonical_address(val[-20:])
     elif val_type == 'bytes32':
         return val
     elif 'int' in val_type:
         return big_endian_to_int(val)
     else:
         raise LogParsingError(
             "Error parsing the type of given value. Expect bool/address/bytes32/int*"
             "but get {}.".format(val_type))
Ejemplo n.º 24
0
def signature_to_G2(signature: Signature) -> G2Point:
    g2 = (
        FQ2([
            big_endian_to_int(signature[:32]),
            big_endian_to_int(signature[32:64])
        ]),
        FQ2([
            big_endian_to_int(signature[64:96]),
            big_endian_to_int(signature[96:])
        ]),
        FQ2.one(),
    )
    assert is_g2_on_curve(g2)
    return g2
Ejemplo n.º 25
0
    def public_key_from_signature(message: str, signature: str) -> PublicKey:
        if signature.startswith("0x"):
            signature = signature[2:]
        bytes_sig = bytes.fromhex(signature)
        if not len(bytes_sig) == 65:
            raise ValidationError(param='signature', type_name='65 bytes')

        vrs = (
            big_endian_to_int(bytes_sig[64:65]) - 27,
            big_endian_to_int(bytes_sig[0:32]),
            big_endian_to_int(bytes_sig[32:64]),
        )
        sig = eth_keys.keys.Signature(vrs=vrs)
        pub_key = eth_keys.keys.ecdsa_recover(Web3.keccak(text=message), sig)
        return pub_key
Ejemplo n.º 26
0
def ec_recover(message: str, signed_message: str) -> str:
    """
    This method does not prepend the message with the prefix `\x19Ethereum Signed Message:\n32`.
    The caller should add the prefix to the msg/hash before calling this if the signature was
    produced for an ethereum-prefixed message.

    :param message:
    :param signed_message:
    :return:
    """
    v, r, s = split_signature(Web3.toBytes(hexstr=signed_message))
    signature_object = SignatureFix(vrs=(v, big_endian_to_int(r),
                                         big_endian_to_int(s)))
    return Account.recoverHash(message,
                               signature=signature_object.to_hex_v_hacked())
Ejemplo n.º 27
0
    def decode(self, rlp_sign: bytes, rlp_data: bytes) -> NeonTxInfo:
        self._set_defaults()

        try:
            utx = EthTx.fromString(rlp_data)

            uv = int(rlp_sign[64]) + 35 + 2 * utx.v
            ur = big_endian_to_int(rlp_sign[0:32])
            us = big_endian_to_int(rlp_sign[32:64])

            tx = EthTx(utx.nonce, utx.gasPrice, utx.gasLimit, utx.toAddress, utx.value, utx.callData, uv, ur, us)
            self.init_from_eth_tx(tx)
        except Exception as e:
            self.error = e
        return self
Ejemplo n.º 28
0
def receipt_to_r_s_v(receipt):
    assert (len(receipt) == 132)
    r = '0x' + receipt[2:66]
    r = decode_hex(r)
    r = big_endian_to_int(r)

    s = '0x' + receipt[66:130]
    s = decode_hex(s)
    s = big_endian_to_int(s)

    v = int('0x' + receipt[130:132], 16)
    if v not in [27, 28]:
        v += 27

    return r, s, v
Ejemplo n.º 29
0
    def __init__(self, contractInfo):
        super().__init__(contractInfo)
        self.name = contractInfo["name"]

        abi = contractInfo["abi"]
        func_abis = [
            func_abi for func_abi in abi if func_abi["type"] == "function"
        ]
        event_abis = [
            event_abi for event_abi in abi if event_abi["type"] == "event"
        ]

        self.funcs = {}
        for func_abi in func_abis:
            id_bytes = eth_utils.function_abi_to_4byte_selector(func_abi)
            self.funcs[id_bytes] = func_abi

        for func_id, func_abi in self.funcs.items():
            setattr(
                ContractABI,
                func_abi["name"],
                generate_func(func_id, func_abi, self.address),
            )
            setattr(ContractABI, "_" + func_abi["name"],
                    generate_func2(func_id, func_abi))

        self.events = {}
        for event_abi in event_abis:
            id_bytes = eth_utils.event_abi_to_log_topic(event_abi)
            event_id = eth_utils.big_endian_to_int(id_bytes)
            self.events[event_id] = event_abi
Ejemplo n.º 30
0
def _decode_keyfile_json_v3(keyfile_json, password):
    crypto = keyfile_json['crypto']
    kdf = crypto['kdf']

    # Derive the encryption key from the password using the key derivation
    # function.
    if kdf == 'pbkdf2':
        derived_key = _derive_pbkdf_key(crypto, password)
    elif kdf == 'scrypt':
        derived_key = _derive_scrypt_key(crypto, password)
    else:
        raise TypeError("Unsupported key derivation function: {0}".format(kdf))

    # Validate that the derived key matchs the provided MAC
    ciphertext = decode_hex(crypto['ciphertext'])
    mac = keccak(derived_key[16:32] + ciphertext)

    expected_mac = decode_hex(crypto['mac'])

    if not hmac.compare_digest(mac, expected_mac):
        raise ValueError("MAC mismatch")

    # Decrypt the ciphertext using the derived encryption key to get the
    # private key.
    encrypt_key = derived_key[:16]
    cipherparams = crypto['cipherparams']
    iv = big_endian_to_int(decode_hex(cipherparams['iv']))

    private_key = decrypt_aes_ctr(ciphertext, encrypt_key, iv)

    return private_key
Ejemplo n.º 31
0
def normalize_block_header(header):
    normalized_header = {
        'bloom': big_endian_to_int(decode_hex(header['bloom'])),
        'coinbase': to_canonical_address(header['coinbase']),
        'difficulty': to_int(header['difficulty']),
        'extraData': decode_hex(header['extraData']),
        'gasLimit': to_int(header['gasLimit']),
        'gasUsed': to_int(header['gasUsed']),
        'hash': decode_hex(header['hash']),
        'mixHash': decode_hex(header['mixHash']),
        'nonce': decode_hex(header['nonce']),
        'number': to_int(header['number']),
        'parentHash': decode_hex(header['parentHash']),
        'receiptTrie': decode_hex(header['receiptTrie']),
        'stateRoot': decode_hex(header['stateRoot']),
        'timestamp': to_int(header['timestamp']),
        'transactionsTrie': decode_hex(header['transactionsTrie']),
        'uncleHash': decode_hex(header['uncleHash']),
    }
    if 'blocknumber' in header:
        normalized_header['blocknumber'] = to_int(header['blocknumber'])
    if 'chainname' in header:
        normalized_header['chainname'] = header['chainname']
    if 'chainnetwork' in header:
        normalized_header['chainnetwork'] = header['chainnetwork']
    return normalized_header
Ejemplo n.º 32
0
    def decoder_fn(self, data):
        value = big_endian_to_int(data)

        with decimal.localcontext(abi_decimal_context):
            decimal_value = decimal.Decimal(value) / TEN ** self.frac_places

        return decimal_value
Ejemplo n.º 33
0
def decint(n):
    if isinstance(n, str):
        n = to_string(n)
    if is_numeric(n) and n < 2**256 and n > -2**255:
        return n
    elif is_numeric(n):
        raise EncodingError("Number out of range: %r" % n)
    elif is_string(n) and len(n) == 40:
        return big_endian_to_int(decode_hex(n))
    elif is_string(n) and len(n) <= 32:
        return big_endian_to_int(n)
    elif is_string(n) and len(n) > 32:
        raise EncodingError("String too long: %r" % n)
    elif n is True:
        return 1
    elif n is False or n is None:
        return 0
    else:
        raise EncodingError("Cannot encode integer: %r" % n)
Ejemplo n.º 34
0
def decode_single(typ, data):
    base, sub, _ = typ
    if base == 'address':
        return encode_hex(data[12:])
    elif base == 'string' or base == 'bytes' or base == 'hash':
        return data[:int(sub)] if len(sub) else data
    elif base == 'uint':
        return big_endian_to_int(data)
    elif base == 'int':
        o = big_endian_to_int(data)
        return getSignedNumber(o, int(sub))
        # return (o - 2**int(sub)) if o >= 2**(int(sub) - 1) else o
    elif base == 'ureal':
        high, low = [int(x) for x in sub.split('x')]
        return big_endian_to_int(data) * 1.0 / 2**low
    elif base == 'real':
        high, low = [int(x) for x in sub.split('x')]
        return (big_endian_to_int(data) * 1.0 / 2**low) % 2**high
    elif base == 'bool':
        return bool(int(data.encode('hex'), 16))
Ejemplo n.º 35
0
    def decoder_fn(self, data):
        value = big_endian_to_int(data)
        if value >= 2 ** (self.value_bit_size - 1):
            signed_value = value - 2 ** self.value_bit_size
        else:
            signed_value = value

        with decimal.localcontext(abi_decimal_context):
            decimal_value = decimal.Decimal(signed_value) / TEN ** self.frac_places

        return decimal_value
Ejemplo n.º 36
0
def normalize_account_state(account_state):
    return {
        to_canonical_address(address): {
            'balance': to_int(state['balance']),
            'code': decode_hex(state['code']),
            'nonce': to_int(state['nonce']),
            'storage': {
                to_int(slot): big_endian_to_int(decode_hex(value))
                for slot, value in state['storage'].items()
            },
        } for address, state in account_state.items()
    }
Ejemplo n.º 37
0
def dec(typ, arg):
    base, sub, arrlist = typ
    sz = get_size(typ)
    # Dynamic-sized strings are encoded as <len(str)> + <str>
    if base in ('string', 'bytes') and not sub:
        L = big_endian_to_int(arg[:32])
        assert len(arg[32:]) == ceil32(L), "Wrong data size for string/bytes object"
        return arg[32:][:L]
    # Dynamic-sized arrays
    elif sz is None:
        L = big_endian_to_int(arg[:32])
        subtyp = base, sub, arrlist[:-1]
        subsize = get_size(subtyp)
        # If children are dynamic, use the head/tail mechanism. Fortunately,
        # here the code is simpler since we do not have to worry about
        # mixed dynamic and static children, as we do in the top-level multi-arg
        # case
        if subsize is None:
            assert len(arg) >= 32 + 32 * L, "Not enough data for head"
            start_positions = [big_endian_to_int(arg[32 + 32 * i: 64 + 32 * i])
                               for i in range(L)] + [len(arg)]
            outs = [arg[start_positions[i]: start_positions[i + 1]]
                    for i in range(L)]
            return [dec(subtyp, out) for out in outs]
        # If children are static, then grab the data slice for each one and
        # sequentially decode them manually
        else:
            return [dec(subtyp, arg[32 + subsize * i: 32 + subsize * (i + 1)])
                    for i in range(L)]
    # Static-sized arrays: decode piece-by-piece
    elif len(arrlist):
        L = arrlist[-1][0]
        subtyp = base, sub, arrlist[:-1]
        subsize = get_size(subtyp)
        return [dec(subtyp, arg[subsize * i:subsize * (i + 1)])
                for i in range(L)]
    else:
        return decode_single(typ, arg)
def test_decode_signed_int(integer_bit_size, stream_bytes, data_byte_size):
    if integer_bit_size % 8 != 0:
        with pytest.raises(ValueError):
            SignedIntegerDecoder(
                value_bit_size=integer_bit_size,
                data_byte_size=data_byte_size,
            )
        return
    elif integer_bit_size > data_byte_size * 8:
        with pytest.raises(ValueError):
            SignedIntegerDecoder(
                value_bit_size=integer_bit_size,
                data_byte_size=data_byte_size,
            )
        return
    else:
        decoder = SignedIntegerDecoder(
            value_bit_size=integer_bit_size,
            data_byte_size=data_byte_size,
        )

    stream = ContextFramesBytesIO(stream_bytes)

    padding_bytes = data_byte_size - integer_bit_size // 8

    raw_value = big_endian_to_int(stream_bytes[padding_bytes:data_byte_size])
    if raw_value >= 2 ** (integer_bit_size - 1):
        actual_value = raw_value - 2 ** integer_bit_size
    else:
        actual_value = raw_value

    if len(stream_bytes) < data_byte_size:
        with pytest.raises(InsufficientDataBytes):
            decoder(stream)
        return
    elif (
        (actual_value >= 0 and not all_bytes_equal(stream_bytes[:padding_bytes], 0)) or
        (actual_value < 0 and not all_bytes_equal(stream_bytes[:padding_bytes], 255))
    ):
        with pytest.raises(NonEmptyPaddingBytes):
            decoder(stream)
        return
    else:
        decoded_value = decoder(stream)

    assert decoded_value == actual_value
Ejemplo n.º 39
0
def decode_abi(types, data):
    # Process types
    proctypes = [process_type(typ) for typ in types]
    # Get sizes of everything
    sizes = [get_size(typ) for typ in proctypes]
    # Initialize array of outputs
    outs = [None] * len(types)
    # Initialize array of start positions
    start_positions = [None] * len(types) + [len(data)]
    # If a type is static, grab the data directly, otherwise record
    # its start position
    pos = 0
    for i, typ in enumerate(types):
        if sizes[i] is None:
            start_positions[i] = big_endian_to_int(data[pos:pos + 32])
            j = i - 1
            while j >= 0 and start_positions[j] is None:
                start_positions[j] = start_positions[i]
                j -= 1
            pos += 32
        else:
            outs[i] = data[pos:pos + sizes[i]]
            pos += sizes[i]
    # We add a start position equal to the length of the entire data
    # for convenience.
    j = len(types) - 1
    while j >= 0 and start_positions[j] is None:
        start_positions[j] = start_positions[len(types)]
        j -= 1
    assert pos <= len(data), "Not enough data for head"
    # Grab the data for tail arguments using the start positions
    # calculated above
    for i, typ in enumerate(types):
        if sizes[i] is None:
            offset = start_positions[i]
            next_offset = start_positions[i + 1]
            outs[i] = data[offset:next_offset]
    # Recursively decode them all
    return [dec(proctypes[i], outs[i]) for i in range(len(outs))]
def test_decode_unsigned_int(integer_bit_size, stream_bytes, data_byte_size):
    if integer_bit_size % 8 != 0:
        with pytest.raises(ValueError):
            UnsignedIntegerDecoder(
                value_bit_size=integer_bit_size,
                data_byte_size=data_byte_size,
            )
        return
    elif integer_bit_size > data_byte_size * 8:
        with pytest.raises(ValueError):
            UnsignedIntegerDecoder(
                value_bit_size=integer_bit_size,
                data_byte_size=data_byte_size,
            )
        return
    else:
        decoder = UnsignedIntegerDecoder(
            value_bit_size=integer_bit_size,
            data_byte_size=data_byte_size,
        )

    stream = ContextFramesBytesIO(stream_bytes)
    actual_value = big_endian_to_int(stream_bytes[:data_byte_size])

    if len(stream_bytes) < data_byte_size:
        with pytest.raises(InsufficientDataBytes):
            decoder(stream)
        return
    elif actual_value > 2 ** integer_bit_size - 1:
        with pytest.raises(NonEmptyPaddingBytes):
            decoder(stream)
        return
    else:
        decoded_value = decoder(stream)

    assert decoded_value == actual_value
Ejemplo n.º 41
0
def to_int(value=None, hexstr=None, text=None):
    """
    Converts value to it's integer representation.

    Values are converted this way:

     * value:
       * bytes: big-endian integer
       * bool: True => 1, False => 0
     * hexstr: interpret hex as integer
     * text: interpret as string of digits, like '12' => 12
    """
    assert_one_val(value, hexstr=hexstr, text=text)

    if hexstr is not None:
        return int(hexstr, 16)
    elif text is not None:
        return int(text)
    elif isinstance(value, bytes):
        return big_endian_to_int(value)
    elif isinstance(value, str):
        raise TypeError("Pass in strings with keyword hexstr or text")
    else:
        return int(value)
Ejemplo n.º 42
0
 def decoder_fn(self, data):
     value = big_endian_to_int(data)
     if value >= 2 ** (self.value_bit_size - 1):
         return value - 2 ** self.value_bit_size
     else:
         return value
Ejemplo n.º 43
0
 def __init__(self, pubkey: datatypes.PublicKey, address: Address) -> None:
     self.pubkey = pubkey
     self.address = address
     self.id = big_endian_to_int(keccak(pubkey.to_bytes()))
Ejemplo n.º 44
0
 def from_endpoint(cls, ip: str, udp_port: str, tcp_port: str = '\x00\x00') -> 'Address':
     return cls(ip, big_endian_to_int(udp_port), big_endian_to_int(tcp_port))
Ejemplo n.º 45
0
def aes_ctr_decrypt(text, key, params):
    iv = big_endian_to_int(decode_hex(params["iv"]))
    ctr = Counter.new(128, initial_value=iv,  allow_wraparound=True)
    mode = AES.MODE_CTR
    encryptor = AES.new(key, mode, counter=ctr)
    return encryptor.decrypt(text)
def is_non_empty_non_null_byte_string(value):
    return value and big_endian_to_int(value) != 0
Ejemplo n.º 47
0
 def __hash__(self):
     return big_endian_to_int(self.hash)