How to read transaction comments in BitcoinJ?

78 views
Skip to first unread message

Dmitrii Pisarenko

unread,
Dec 9, 2021, 5:16:06 AM12/9/21
to bitcoinj
Hello!

I have a addCoinsReceivedEventListener which is invoked whenever I send Bitcoin to the correct address via bitcoin-cli (using a call like `bitcoin-cli -regtest sendtoaddress 2MwnQZ9zqU7hH9h4DVWiudGeuPS1GbQRGrx 1`):

kit.wallet().addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) -> {
        // I want to read transaction comment here                
});

I want to be able to add a comment to a transaction.

When I send Bitcoin, I use this command:

bitcoin-cli -regtest sendtoaddress 2MwnQZ9zqU7hH9h4DVWiudGeuPS1GbQRGrx 1 "Comment 4"

Questions:

1. Is it possible to read the comment ("Comment 4" in this case) in the addCoinsReceivedEventListener? If yes, how?

2. Am using the correct bitcoin-cli command to send Bitcoin with comment?

Thanks in advance

Dmitrii Pisarenko

P. S.: Below you can find the full source code of the wallet listener.

import org.bitcoinj.core.Address;
import org.bitcoinj.core.Coin;
import org.bitcoinj.kits.WalletAppKit;
import org.bitcoinj.utils.BriefLogFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.Optional;

@Component
public class WalletObserver {
    @Autowired
    ClojureService clojureService;

    @Value("${accounts.btc.exchange.address}")
    String exchangeAddress;

    @PostConstruct
    public void init() {
        BriefLogFormatter.init();
        final LocalTestNetParams netParams = new LocalTestNetParams();
        netParams.setPort(18444);

        try {
            final WalletAppKit kit = new WalletAppKit(netParams, new File("."), "_minimalCryptoExchangeBtcWallet");
            kit.connectToLocalHost();
            kit.startAsync();
            kit.awaitRunning();

            kit.wallet().addWatchedAddress(Address.fromString(netParams,  exchangeAddress));

            kit.wallet().addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) -> {
                                // I want to read transaction comment here                
            });
        }
        catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

Andreas Schildbach

unread,
Dec 15, 2021, 11:41:24 AM12/15/21
to bitc...@googlegroups.com
Comments are local to your wallet. They are not transmitted via the
Bitcoin P2P network. Also if you replay your wallet, they will currently
be lost.

Apart from that, you can just get/set the field.
> --
> You received this message because you are subscribed to the Google
> Groups "bitcoinj" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to bitcoinj+u...@googlegroups.com
> <mailto:bitcoinj+u...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bitcoinj/ea6325e0-5303-46cb-b503-66ca9d234788n%40googlegroups.com
> <https://groups.google.com/d/msgid/bitcoinj/ea6325e0-5303-46cb-b503-66ca9d234788n%40googlegroups.com?utm_medium=email&utm_source=footer>.


Reply all
Reply to author
Forward
0 new messages