def single_sign_transaction(input_private_key: str, tx: Transaction):
    data = tx.serialize_unsigned()
    signature = keytool.ecdsa_sign(bytes.fromhex(input_private_key), data)
    r = b""
    r = serialize.write_var_bytes(r, signature)
    tx.programs[0].parameter = r
    tx.hash()
    return tx
def create_active_transaction(node_private_key: str, node_public_key: str):
    pub_key = bytes.fromhex(node_public_key)
    pri_key = bytes.fromhex(node_private_key)
    activate_producer = ProcessProducer(pub_key, pri_key)

    tx = Transaction()
    tx.version = Transaction.TX_VERSION_09
    tx.tx_type = Transaction.ACTIVATE_PRODUCER
    tx.payload = activate_producer
    tx.attributes = []
    tx.inputs = []
    tx.outputs = []
    tx.programs = list()
    tx.lock_time = 0

    return tx
def create_transaction(input_private_key: str, output_addresses: list,
                       amount: int, rpc_port: int):
    account = Account(input_private_key)
    # check output
    if output_addresses is None or len(output_addresses) == 0:
        Logger.error("Invalid output addresses")
        return None

    # create outputs
    outputs, total_amount = create_normal_outputs(
        output_addresses=output_addresses,
        amount=amount,
        fee=util.TX_FEE,
        output_lock=0)

    # create inputs
    inputs, change_outputs = create_normal_inputs(account.address(),
                                                  total_amount, rpc_port)

    if inputs is None or change_outputs is None:
        Logger.error("Create normal inputs failed")
        return None
    outputs.extend(change_outputs)

    # create program
    programs = list()
    redeem_script = bytes.fromhex(account.redeem_script())
    program = Program(code=redeem_script, params=None)
    programs.append(program)

    # create attributes
    attributes = list()
    attribute = Attribute(usage=Attribute.NONCE,
                          data=bytes("attributes".encode()))
    attributes.append(attribute)

    tx = Transaction()
    tx.version = Transaction.TX_VERSION_09
    tx.tx_type = Transaction.TRANSFER_ASSET
    tx.payload = Payload(Payload.DEFAULT_VERSION)
    tx.attributes = attributes
    tx.inputs = inputs
    tx.outputs = outputs
    tx.lock_time = 0
    tx.programs = programs

    return tx
def create_cross_chain_asset(input_private_key: str, lock_address: str,
                             cross_chain_address: str, amount: int,
                             recharge: bool, rpc_port: int):
    if lock_address is None or lock_address is "":
        Logger.error("Invalid lock address")
        return None

    if cross_chain_address is None or cross_chain_address is "":
        Logger.error("Invalid cross chain address")
        return None

    account = Account(input_private_key)
    # create outputs:
    outputs, total_amount = create_normal_outputs(
        output_addresses=[lock_address],
        amount=amount,
        fee=util.TX_FEE,
        output_lock=0)

    # create inputs:
    inputs, change_outputs = create_normal_inputs(account.address(),
                                                  total_amount, rpc_port)
    if inputs is None or change_outputs is None:
        Logger.error("Create normal inputs failed")
        return None
    outputs.extend(change_outputs)

    # create program
    programs = list()
    redeem_script = bytes.fromhex(account.redeem_script())
    program = Program(code=redeem_script, params=None)
    programs.append(program)

    # create attributes
    attributes = list()
    attribute = Attribute(usage=Attribute.NONCE,
                          data=bytes("attributes".encode()))
    attributes.append(attribute)

    cross_chain_asset = TransferCrossChainAsset()
    cross_chain_asset.cross_chain_addresses = [cross_chain_address]
    cross_chain_asset.output_indexes = [0]
    cross_chain_asset.cross_chain_amounts = [amount - util.TX_FEE]

    tx = Transaction()
    if recharge:
        tx.version = Transaction.TX_VERSION_09
    else:
        tx.version = Transaction.TX_VERSION_DEFAULT

    Logger.debug("transaction version {}".format(tx.version))
    tx.tx_type = Transaction.TRANSFER_CROSS_CHAIN_ASSET
    tx.payload = cross_chain_asset
    tx.attributes = attributes
    tx.inputs = inputs
    tx.outputs = outputs
    tx.lock_time = 0
    tx.programs = programs

    return tx
def create_redeem_transaction(payload: ProducerInfo, output_address: str,
                              amount: int, rpc_port: int):

    # create outputs
    outputs, total_amount = create_normal_outputs(
        output_addresses=[output_address],
        amount=amount,
        fee=util.TX_FEE,
        output_lock=0)

    # create inputs

    deposit_address = payload.get_deposit_address()
    inputs, change_outputs = create_normal_inputs(deposit_address,
                                                  total_amount, rpc_port)
    if inputs is None or change_outputs is None:
        Logger.error("Create normal inputs failed")
        return None
    outputs.extend(change_outputs)

    # create program
    programs = list()
    redeem_script = bytes.fromhex(payload.owner_account.redeem_script())
    program = Program(code=redeem_script, params=None)
    programs.append(program)

    # create attributes
    attributes = list()
    attribute = Attribute(usage=Attribute.NONCE,
                          data=bytes("attributes".encode()))
    attributes.append(attribute)

    tx = Transaction()
    tx.version = Transaction.TX_VERSION_09
    tx.tx_type = Transaction.RETURN_DEPOSIT_CHAIN
    tx.payload_version = 0
    tx.payload = Payload(Payload.DEFAULT_VERSION)
    tx.attributes = attributes
    tx.inputs = inputs
    tx.outputs = outputs
    tx.lock_time = 0
    tx.programs = programs

    return tx
def create_cancel_transaction(input_private_key: str, payload: ProducerInfo,
                              rpc_port: int):

    # create inputs
    account = Account(input_private_key)
    inputs, change_outputs = create_normal_inputs(account.address(),
                                                  util.TX_FEE, rpc_port)
    if inputs is None or change_outputs is None:
        Logger.error("Create normal inputs failed")
        return None

    # create outputs
    outputs = list()
    outputs.extend(change_outputs)

    # create program
    programs = list()
    redeem_script = bytes.fromhex(account.redeem_script())
    program = Program(code=redeem_script, params=None)
    programs.append(program)

    # create attributes
    attributes = list()
    attribute = Attribute(usage=Attribute.NONCE,
                          data=bytes("attributes".encode()))
    attributes.append(attribute)

    payload = ProcessProducer(
        bytes.fromhex(payload.owner_account.public_key()),
        bytes.fromhex(payload.owner_account.private_key()))
    tx = Transaction()
    tx.version = Transaction.TX_VERSION_09
    tx.tx_type = Transaction.CANCEL_PRODUCER
    tx.payload_version = 0
    tx.payload = payload
    tx.attributes = attributes
    tx.inputs = inputs
    tx.outputs = outputs
    tx.lock_time = 0
    tx.programs = programs

    return tx
def create_register_transaction(input_private_key: str, amount: int,
                                payload: ProducerInfo, rpc_port: int):

    # create outputs
    outputs, total_amount = create_normal_outputs(
        output_addresses=[payload.get_deposit_address()],
        amount=amount,
        fee=util.TX_FEE,
        output_lock=0)

    # create inputs
    account = Account(input_private_key)
    inputs, change_outputs = create_normal_inputs(account.address(),
                                                  total_amount, rpc_port)
    if inputs is None or change_outputs is None:
        Logger.error("Create normal inputs failed")
        return None
    outputs.extend(change_outputs)

    # create program
    programs = list()
    redeem_script = bytes.fromhex(account.redeem_script())
    program = Program(code=redeem_script, params=None)
    programs.append(program)

    # create attributes
    attributes = list()
    attribute = Attribute(usage=Attribute.NONCE,
                          data=bytes("attributes".encode()))
    attributes.append(attribute)

    tx = Transaction()
    tx.version = Transaction.TX_VERSION_09
    tx.tx_type = Transaction.REGISTER_PRODUCER
    tx.payload_version = 0
    tx.payload = payload
    tx.attributes = attributes
    tx.inputs = inputs
    tx.outputs = outputs
    tx.lock_time = 0
    tx.programs = programs

    return tx