Ejemplo n.º 1
0
def _encode_ballot(w: bytearray, ballot):
    ballot_tag = 6

    write_uint8(w, ballot_tag)
    write_bytes(w, ballot.source)
    write_uint32_be(w, ballot.period)
    write_bytes(w, ballot.proposal)
    write_uint8(w, ballot.ballot)
Ejemplo n.º 2
0
def write_string(w, s: str):
    buf = s.encode()
    write_uint32(w, len(buf))
    write_bytes(w, buf)
    # if len isn't a multiple of 4, add padding bytes
    reminder = len(buf) % 4
    if reminder:
        write_bytes(w, bytes([0] * (4 - reminder)))
Ejemplo n.º 3
0
def encode_cancel_lease_tx_to_bytes(tx):
    w = bytearray()
    write_uint8(w, tx.transactionType)
    write_uint64_be(w, tx.fee)
    write_uint16_be(w, tx.feeScale)
    write_uint64_be(w, helpers.convert_to_nano_sec(tx.timestamp))
    write_bytes(w, base58.decode(tx.txId))
    return w
Ejemplo n.º 4
0
def write_tx_input_check(w, i: TxInputType):
    write_bytes(w, i.prev_hash)
    write_uint32(w, i.prev_index)
    write_uint32(w, i.script_type)
    write_uint32(w, len(i.address_n))
    for n in i.address_n:
        write_uint32(w, n)
    write_uint32(w, i.sequence)
    write_uint64(w, i.amount or 0)
Ejemplo n.º 5
0
def _encode_proposal(w: bytearray, proposal):
    proposal_tag = 5

    write_uint8(w, proposal_tag)
    write_bytes(w, proposal.source)
    write_uint32_be(w, proposal.period)
    write_uint32_be(w, len(proposal.proposals) * PROPOSAL_LENGTH)
    for proposal_hash in proposal.proposals:
        write_bytes(w, proposal_hash)
Ejemplo n.º 6
0
def write_variant32(w: Writer, value: int) -> None:
    variant = bytearray()
    while True:
        b = value & 0x7F
        value >>= 7
        b |= (value > 0) << 7
        variant.append(b)
        if value == 0:
            break
    write_bytes(w, bytes(variant))
Ejemplo n.º 7
0
def write_string(w, s: AnyStr) -> None:
    """Write XDR string padded to a multiple of 4 bytes."""
    if isinstance(s, str):
        buf = s.encode()
    else:
        buf = s
    write_uint32(w, len(buf))
    write_bytes(w, buf)
    # if len isn't a multiple of 4, add padding bytes
    reminder = len(buf) % 4
    if reminder:
        write_bytes(w, bytes([0] * (4 - reminder)))
Ejemplo n.º 8
0
def _encode_common(w: bytearray, operation, str_operation):
    operation_tags = {
        "reveal": 107,
        "transaction": 108,
        "origination": 109,
        "delegation": 110,
    }
    write_uint8(w, operation_tags[str_operation])
    write_bytes(w, operation.source)
    _encode_zarith(w, operation.fee)
    _encode_zarith(w, operation.counter)
    _encode_zarith(w, operation.gas_limit)
    _encode_zarith(w, operation.storage_limit)
Ejemplo n.º 9
0
def encode_payment_tx_to_bytes(tx):
    w = bytearray()
    write_uint8(w, tx.transactionType)
    write_uint64_be(w, helpers.convert_to_nano_sec(tx.timestamp))
    write_uint64_be(w, tx.amount)
    write_uint64_be(w, tx.fee)
    write_uint16_be(w, tx.feeScale)
    write_bytes(w, base58.decode(tx.recipient))
    try:
        attachment_bytes = base58.decode(tx.attachment)
    except Exception:
        attachment_bytes = bytes(tx.attachment, 'utf-8')
    write_uint16_be(w, len(attachment_bytes))
    write_bytes(w, attachment_bytes)
    return w
Ejemplo n.º 10
0
def _encode_manager_delegation(w: bytearray, delegate):
    MICHELSON_LENGTH = 42  # length is fixed this time(no variable length fields)

    _encode_manager_common(w, MICHELSON_LENGTH, "PUSH")
    write_bytes(w, delegate)
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["SOME"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["SET_DELEGATE"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"])
Ejemplo n.º 11
0
def write_auth(w: bytearray, auth: EosAuthorization) -> int:
    write_uint32_le(w, auth.threshold)
    write_variant32(w, len(auth.keys))
    for key in auth.keys:
        write_variant32(w, key.type)
        write_bytes(w, key.key)
        write_uint16_le(w, key.weight)

    write_variant32(w, len(auth.accounts))
    for account in auth.accounts:
        write_uint64_le(w, account.account.actor)
        write_uint64_le(w, account.account.permission)
        write_uint16_le(w, account.weight)

    write_variant32(w, len(auth.waits))
    for wait in auth.waits:
        write_uint32_le(w, wait.wait_sec)
        write_uint16_le(w, wait.weight)
Ejemplo n.º 12
0
def _get_operation_bytes(w: bytearray, msg):
    write_bytes(w, msg.branch)

    # when the account sends first operation in lifetime,
    # we need to reveal its publickey
    if msg.reveal is not None:
        _encode_common(w, msg.reveal, "reveal")
        write_bytes(w, msg.reveal.public_key)

    # transaction operation
    if msg.transaction is not None:
        _encode_common(w, msg.transaction, "transaction")
        _encode_zarith(w, msg.transaction.amount)
        _encode_contract_id(w, msg.transaction.destination)
        _encode_data_with_bool_prefix(w, msg.transaction.parameters)
    # origination operation
    elif msg.origination is not None:
        _encode_common(w, msg.origination, "origination")
        write_bytes(w, msg.origination.manager_pubkey)
        _encode_zarith(w, msg.origination.balance)
        _encode_bool(w, msg.origination.spendable)
        _encode_bool(w, msg.origination.delegatable)
        _encode_data_with_bool_prefix(w, msg.origination.delegate)
        _encode_data_with_bool_prefix(w, msg.origination.script)
    # delegation operation
    elif msg.delegation is not None:
        _encode_common(w, msg.delegation, "delegation")
        _encode_data_with_bool_prefix(w, msg.delegation.delegate)
Ejemplo n.º 13
0
def _get_operation_bytes(w: bytearray, msg):
    write_bytes(w, msg.branch)

    # when the account sends first operation in lifetime,
    # we need to reveal its public key
    if msg.reveal is not None:
        _encode_common(w, msg.reveal, "reveal")
        write_bytes(w, msg.reveal.public_key)

    # transaction operation
    if msg.transaction is not None:
        _encode_common(w, msg.transaction, "transaction")
        _encode_zarith(w, msg.transaction.amount)
        _encode_contract_id(w, msg.transaction.destination)

        # support delegation and transfer from the old scriptless contracts (now with manager.tz script)
        if msg.transaction.parameters_manager is not None:
            parameters_manager = msg.transaction.parameters_manager

            if parameters_manager.set_delegate is not None:
                _encode_manager_delegation(w, parameters_manager.set_delegate)
            elif parameters_manager.cancel_delegate is not None:
                _encode_manager_delegation_remove(w)
            elif parameters_manager.transfer is not None:
                if (parameters_manager.transfer.destination.tag ==
                        TezosContractType.Implicit):
                    _encode_manager_to_implicit_transfer(
                        w, parameters_manager.transfer)
                else:
                    _encode_manager_to_manager_transfer(
                        w, parameters_manager.transfer)
        else:
            _encode_data_with_bool_prefix(w, msg.transaction.parameters)
    # origination operation
    elif msg.origination is not None:
        _encode_common(w, msg.origination, "origination")
        _encode_zarith(w, msg.origination.balance)
        _encode_data_with_bool_prefix(w, msg.origination.delegate)
        write_bytes(w, msg.origination.script)

    # delegation operation
    elif msg.delegation is not None:
        _encode_common(w, msg.delegation, "delegation")
        _encode_data_with_bool_prefix(w, msg.delegation.delegate)
    elif msg.proposal is not None:
        _encode_proposal(w, msg.proposal)
    elif msg.ballot is not None:
        _encode_ballot(w, msg.ballot)
Ejemplo n.º 14
0
def write_tx_output(w, o: TxOutputBinType):
    write_uint64(w, o.amount)
    if o.decred_script_version is not None:
        write_uint16(w, o.decred_script_version)
    write_varint(w, len(o.script_pubkey))
    write_bytes(w, o.script_pubkey)
Ejemplo n.º 15
0
def write_tx_input_decred_witness(w, i: TxInputType):
    write_uint64(w, i.amount or 0)
    write_uint32(w, 0)  # block height fraud proof
    write_uint32(w, 0xFFFFFFFF)  # block index fraud proof
    write_varint(w, len(i.script_sig))
    write_bytes(w, i.script_sig)
Ejemplo n.º 16
0
def write_pubkey(w, address: str):
    # first 4 bytes of an address are the type, there's only one type (0)
    write_uint32(w, 0)
    write_bytes(w, public_key_from_address(address))
Ejemplo n.º 17
0
def write_tx_input(w, i: TxInputType):
    write_bytes_reversed(w, i.prev_hash)
    write_uint32(w, i.prev_index)
    write_varint(w, len(i.script_sig))
    write_bytes(w, i.script_sig)
    write_uint32(w, i.sequence)
Ejemplo n.º 18
0
def _encode_manager_to_manager_transfer(w: bytearray, manager_transfer):
    MICHELSON_LENGTH = 77

    value_natural = bytearray()
    _encode_natural(value_natural, manager_transfer.amount)
    sequence_length = MICHELSON_LENGTH + len(value_natural)

    _encode_manager_common(w, sequence_length, "PUSH", to_contract=True)
    _encode_contract_id(w, manager_transfer.destination)
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONTRACT"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["unit"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["ASSERT_SOME"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["PUSH"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["mutez"])
    _encode_natural(w, manager_transfer.amount)
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["UNIT"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["TRANSFER_TOKENS"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"])
Ejemplo n.º 19
0
def _encode_manager_delegation_remove(w: bytearray):
    MICHELSON_LENGTH = 14  # length is fixed this time(no variable length fields)
    _encode_manager_common(w, MICHELSON_LENGTH, "NONE")
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["SET_DELEGATE"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"])
Ejemplo n.º 20
0
def _encode_manager_to_implicit_transfer(w: bytearray, manager_transfer):
    MICHELSON_LENGTH = 48

    value_natural = bytearray()
    _encode_natural(value_natural, manager_transfer.amount)
    sequence_length = MICHELSON_LENGTH + len(value_natural)

    _encode_manager_common(w, sequence_length, "PUSH")
    write_bytes(w, manager_transfer.destination.hash)
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["IMPLICIT_ACCOUNT"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["PUSH"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["mutez"])
    _encode_natural(w, manager_transfer.amount)
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["UNIT"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["TRANSFER_TOKENS"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"])
Ejemplo n.º 21
0
def _encode_contract_id(w: bytearray, contract_id):
    write_uint8(w, contract_id.tag)
    write_bytes(w, contract_id.hash)
Ejemplo n.º 22
0
def write_action_transfer(w: bytearray, msg: EosActionTransfer):
    write_uint64_le(w, msg.sender)
    write_uint64_le(w, msg.receiver)
    write_asset(w, msg.quantity)
    write_variant32(w, len(msg.memo))
    write_bytes(w, msg.memo)
Ejemplo n.º 23
0
def write_bytes_with_len(w, buf: bytes):
    write_uint32_le(w, len(buf))
    write_bytes(w, buf)
Ejemplo n.º 24
0
def _encode_data_with_bool_prefix(w: bytearray, data):
    if data:
        _encode_bool(w, True)
        write_bytes(w, data)
    else:
        _encode_bool(w, False)
Ejemplo n.º 25
0
def _encode_manager_common(w: bytearray,
                           sequence_length,
                           operation,
                           to_contract=False):
    IMPLICIT_ADDRESS_LENGTH = 21
    SMART_CONTRACT_ADDRESS_LENGTH = 22

    # 5 = tag and sequence_length (1 byte + 4 bytes)
    argument_length = sequence_length + 5

    helpers.write_bool(w, True)
    write_uint8(w, helpers.DO_ENTRYPOINT_TAG)
    write_uint32_be(w, argument_length)
    write_uint8(w, helpers.MICHELSON_SEQUENCE_TAG)
    write_uint32_be(w, sequence_length)
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["DROP"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["NIL"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["operation"])
    write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES[operation])
    if to_contract is True:
        write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["address"])
    else:
        write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["key_hash"])
    if operation == "PUSH":
        write_bytes(w, bytes([10]))  # byte sequence
        if to_contract is True:
            write_uint32_be(w, SMART_CONTRACT_ADDRESS_LENGTH)
        else:
            write_uint32_be(w, IMPLICIT_ADDRESS_LENGTH)
Ejemplo n.º 26
0
def write_bytes_with_length(w, buf: bytearray):
    write_varint(w, len(buf))
    write_bytes(w, buf)
Ejemplo n.º 27
0
def write_tx_output(w, o: TxOutputBinType):
    write_uint64(w, o.amount)
    write_varint(w, len(o.script_pubkey))
    write_bytes(w, o.script_pubkey)