Example #1
0
def serialize_step(step: t.Mapping) -> bytes:
    blob = bytearray([0])
    if 'account' in step:
        blob[0] |= 0x01
        blob.extend(DEFAULT_CODEC.decode_address(step['account']))
    if 'currency' in step:
        blob[0] |= 0x10
        blob.extend(serialize_currency(step['currency']))
    if 'issuer' in step:
        blob[0] |= 0x20
        blob.extend(DEFAULT_CODEC.decode_address(step['issuer']))
    return bytes(blob)
Example #2
0
def serialize_amount(amount: Amount) -> bytes:
    """
    Serialize an Amount.

    An XRP Amount comes as a string. It must be serialized to 64 bits:
    1 zero bit, 1 sign bit (zero for negative, one for positive), 62 bits of
    absolute value.

    A non-XRP Amount comes as a dictionary. It must be serialized thus:
    64 bits of unsigned value; 160 bit currency code; 160 bit issuer
    AccountID.
    """
    if isinstance(amount, str):
        value = int(amount)
        sign = int(value > 0)
        magnitude = abs(value)
        assert magnitude <= 10**17
        return to_bytes(sign << 62 | magnitude, 8)
    if isinstance(amount, dict):
        value_bytes = serialize_amount_non_xrp(amount['value'])
        currency_bytes = serialize_currency(amount['currency'])
        address_bytes = DEFAULT_CODEC.decode_address(
            t.cast(Address, amount['issuer']))
        return value_bytes + currency_bytes + address_bytes
    raise ValueError('Amount must be `str` or `{value, currency, issuer}`')
Example #3
0
def serialize_account_id(address: str) -> bytes:
    return vl_encode(DEFAULT_CODEC.decode_address(t.cast(Address, address)))
Example #4
0
def test_decode_address(account_id_hex, address):
    account_id = bytes.fromhex(account_id_hex)
    assert codec.decode_address(address) == account_id