Skip to content

Outputs

A transaction's outputs are the new UTXOs it creates. Each carries a value (in sompi) and a locking script — the conditions a future spender must satisfy.

Types involved

TransactionOutput
  value (sompi)
  script_public_key: ScriptPublicKey

ScriptPublicKey
  version (int)
  script  (hex bytes — the lockup conditions)

TransactionOutput pairs the amount with the script that locks it. ScriptPublicKey is the script itself: a version byte plus the encoded program a future spender must satisfy.

Build an output

Pay-to-address is the common case. Build the lockup script with pay_to_address_script:

from kaspa import Address, TransactionOutput, pay_to_address_script

recipient = Address("kaspa:qz...")
out = TransactionOutput(
    value=500_000_000,                              # 5 KAS in sompi
    script_public_key=pay_to_address_script(recipient),
)

For the inverse — recovering the address an output pays to — use address_from_script_public_key. It needs a network argument because the script doesn't carry the prefix:

from kaspa import NetworkType, address_from_script_public_key

addr = address_from_script_public_key(out.script_public_key, NetworkType.Mainnet)

Pay-to-script-hash

For multisig and other custom scripts, the locking side uses a script hash. See examples/transactions/multisig.py for the full P2SH flow (address creation, multi-cosigner signing, submission). Build the lockup with pay_to_script_hash_script:

from kaspa import pay_to_script_hash_script

spk = pay_to_script_hash_script(redeem_script_bytes)
out = TransactionOutput(value=amount, script_public_key=spk)

Change outputs

When selected inputs sum to more than outputs + fee, the leftover goes to a change output you control:

outputs = [
    TransactionOutput(value=amount,        script_public_key=pay_to_address_script(recipient)),
    TransactionOutput(value=change_amount, script_public_key=pay_to_address_script(change_addr)),
]

The Generator (see Transaction Generator) computes change_amount for you (selected_total − outputs − fee) and writes change last. Manually, do the arithmetic yourself — and re-check after update_transaction_mass if the fee shifted.

If a tiny change output would inflate storage mass more than it's worth, fold it into the fee instead. See Mass & fees for sizing.

Sompi vs KAS

Every value in the transaction API is a sompi int. 1 KAS = 100_000_000 sompi. Convert with kaspa_to_sompi and sompi_to_kaspa:

from kaspa import kaspa_to_sompi, sompi_to_kaspa

kaspa_to_sompi(1.5)            # 150_000_000
sompi_to_kaspa(150_000_000)    # 1.5

Convert only at the UI boundary — don't store KAS as a float.

Reading outputs back

for out in tx.outputs:
    print(out.value)                          # sompi
    print(out.script_public_key.version)
    print(out.script_public_key.script)       # hex