karask / python-litecoin-utils

Library to interact with the Litecoin network. Based on karask/python-bitcoin-utils.
MIT License
24 stars 10 forks source link

Can you provide clear examples on how to create a bip44, bip49 and bip84 tx? #10

Closed tajelp closed 5 months ago

tajelp commented 5 months ago

Hello there, I looked at the examples but it's still not very clear to me how to generate the code for these 3 scenarios:

I am trying to create a function with this signature create_litecoin_tx(sender_address, sender_pkey, dest_address, inputs, outputs, values_by_vout).

The function will return (signed_tx_hex, error).

The params are:

Can you please help me create a working function? This is what I have come up with so far:

def get_address_type(address):
    if address.startswith('L') or address.startswith('1'):
        return 'bip44'
    elif address.startswith('M') or address.startswith('3'):
        return 'bip49'
    elif address.startswith('ltc1'):
        return 'bip84'

def create_litecoin_tx(sender_address, sender_pkey, dest_address, inputs, outputs, values_by_vout):
    try:
        sender_address_type = get_address_type(sender_address)
        dest_address_type = get_address_type(dest_address)

        setup('mainnet')

        sk = PrivateKey(sender_pkey)
        pub = sk.get_public_key()

        tx_inputs = [TxInput(i['txid'], i['vout']) for i in inputs]
        # TODO

        tx_outputs = []
        for output in outputs:
            for address, ltc_amount in output.items():
                # TODO

                tx_output = TxOutput(...) # TODO

                tx_outputs.append(tx_output)

        tx = Transaction(tx_inputs, tx_outputs, has_segwit=has_segwit)

        has_segwit = sender_address_type in ('bip49', 'bip84')

        if has_segwit:
            # TODO

        return tx.serialize(), None
    except Exception as e:
        traceback.print_exc()
        return None, e
karask commented 5 months ago

Hi @tajelp

I do not support or updated this library for years. It was a spin-off of my python-bitcoin-utils library. The examples should work, although they are using bitcoin testnet addresses. If you use their respective litecoin address they should work.

I remember litecoin added some new addresses that are not support.

I would suggest you try to make it work only with legacy addresses first and then move on to the rest (don't do all of they at once).

The following example spends a P2PKH and sends the funds to another P2PKH. Study it, update with appropriate litecoin addresses / UTXOs and try to make it run, etc.

https://github.com/karask/python-litecoin-utils/blob/master/examples/p2pkh_transaction.py

I hope the above helps!