Skip to content

Add address field to PaymentKind::Onchain #684

Open

Description

@joschisan

When I receive a onchain payment it would be useful to know the address the payment way received to. This way I can attribute incoming unchain payments to specific users when I am building an account system on top of LDK Node.

It appears to me that the right way to do this is to add an address field to PaymentKind::Onchain.

Activity

  1. tnull commented on Nov 7, 2025

    @tnull
    Collaborator

    This might not be as trivial, because many things could happen at the same time in a single transaction, i.e., also multiple (change) addresses could be in the mix. Our current model is that we classify transactions as 'inbound'/'outbound' based on whether they send more to foreign addresses than to wallet-owned addresses, which works decently well, but I'm not quite sure if there's currently a good way for us to access this information or expose it reasonably (short of listing out all details).

  2. hash-money commented on Jul 24, 2026

    @hash-money

    We hit this from a different angle and did some digging that might help unblock
    the two doubts in the previous comment.

    Use case. We're building a graduated-custody wallet on LDK Node (via
    orange-sdk). Our Receive screen shows a BIP21 request and flips to a "paid"
    state when the payment lands — we match Lightning receives on
    PaymentKind::Bolt11 { hash }, which works well. On-chain receives can't be
    matched at all: PaymentKind::Onchain carries only txid, so we can't tell
    whether the payment that just arrived is the request the user is looking at or
    an unrelated deposit. There's no local workaround short of independently
    querying a chain backend and re-deriving ownership, which duplicates work the
    node already did and is backend-specific for us.

    On "not sure there's currently a good way for us to access this
    information":
    the data appears to already be in hand at the point the record
    is built. Wallet::create_payment_from_tx (src/wallet/mod.rs:1444) receives
    both the full tx: &Transaction and the locked_wallet, and
    onchain_payment_fields_locked right above it (:1417) already calls
    locked_wallet.sent_and_received(tx) (:1421) — which walks the outputs
    against the keychain index to compute amount/direction. So the per-output
    ownership determination is happening already; the script just isn't retained.

    On "expose it reasonably (short of listing out all details)" + the
    change-address concern:
    bdk_wallet exposes
    derivation_of_spk(spk) -> Option<(KeychainKind, u32)>, documented as only
    returning Some(_) for spks the wallet has given out. Filtering on
    KeychainKind::External therefore excludes change outputs by construction,
    which we think is exactly the distinction that makes this tractable — you'd
    expose only the spks that were handed out, not "all details". Something like:

    let received_spks: Vec<ScriptBuf> = tx.output.iter()
        .filter(|o| locked_wallet.derivation_of_spk(o.script_pubkey.clone())
            .is_some_and(|(k, _)| k == KeychainKind::External))
        .map(|o| o.script_pubkey.clone())
        .collect();

    A Vec also handles the "many things at once" case honestly rather than
    pretending there's a single address.

    Back-compat looks like it has an in-repo precedent: tx_type was added to
    this same variant as an optional field, with the documented "records written
    before it existed read back as None" behavior and a round-trip test
    (known_onchain_tx_type_survives_unknown_update). The same shape should work
    here.

    We're aware this isn't free — PaymentKind is derive(uniffi::Enum), so a new
    field lands in the Swift/Kotlin/Python bindings too and needs a representation
    chosen for it.

    Happy to write the PR if the shape above (external-only, Vec, optional field)
    is the direction you'd want. Equally happy to be told a different shape is
    preferred, or that this isn't worth the bindings churn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions