Title: Get UTXOs of a wallet... Post by: sudonims on March 28, 2022, 01:47:34 PM I'm trying to work on something and I need a programmatic way to get UTXOs related to a certain wallet...
e.g, if I run a btc-core node with wallet A having addresses [A1,...,A10], then I want a programmatic way to get a set of all UTXOs related to wallet A i.e for addresses [A1,...,A10]... Preferrebly in C++... Thanks in advance... Title: Re: Get UTXOs of a wallet... Post by: BlackHatCoiner on March 28, 2022, 01:53:35 PM In Bitcoin Core you can use:
Code: bitcoin-cli listunspent in3rsha has also written this: bitcoin-utxo-dump (https://github.com/in3rsha/bitcoin-utxo-dump). (In Go language) Title: Re: Get UTXOs of a wallet... Post by: n0nce on March 28, 2022, 01:56:19 PM I'm trying to work on something and I need a programmatic way to get UTXOs related to a certain wallet... I can't provide you code right now, but these are the RPC commands that you should look into:e.g, if I run a btc-core node with wallet A having addresses [A1,...,A10], then I want a programmatic way to get a set of all UTXOs related to wallet A i.e for addresses [A1,...,A10]... Preferrebly in C++... Thanks in advance... Wallet RPCs (https://developer.bitcoin.org/reference/rpc/#wallet-rpcs) I also found this resource (https://upcoder.com/7/bitcoin-rpc-from-python/) that shows how to call RPC methods from Python using simple network requests. Should be easy to follow along and code it in C++. Code: (https://upcoder.com/7/bitcoin-rpc-from-python/) ~snip~ Title: Re: Get UTXOs of a wallet... Post by: sudonims on March 28, 2022, 01:57:42 PM In Bitcoin Core you can use: Code: bitcoin-cli listunspent in3rsha has also written this: bitcoin-utxo-dump (https://github.com/in3rsha/bitcoin-utxo-dump). (In Go language) Thanks... I will check it out... Title: Re: Get UTXOs of a wallet... Post by: n0nce on March 28, 2022, 02:46:13 PM Okay, I had a few minutes and threw something together using this one-file HTTP lib for C++ (https://github.com/yhirose/cpp-httplib/) and the example in Python I linked to, earlier (https://upcoder.com/7/bitcoin-rpc-from-python/).
It's not fully working yet, but it should be a good starting point. :) It might actually run, honestly, just don't have RPC credentials handy right now. This leverages the built-in local HTTP API that Bitcoin Core offers. If you want to go through the RPC interface, that should also work, but I believe this is much easier by avoiding permission issues to the RPC interface for instance. Code: #define CPPHTTPLIB_OPENSSL_SUPPORT Compile & link right libraries: g++ -lssl -lcrypto bitcointest.cpp -o bitcointest Edit: There's some issues with this, especially if your node is configured to use a cookie file like mine. I checked the bitcoin-cli source code and we can see that the cookie content is passed in the HTTP request header. This function call (https://github.com/bitcoin/bitcoin/blob/3c565302aaa91ae46aa09b8aeb95206711d9d8a6/src/bitcoin-cli.cpp#L759) retrieves the cookie's contents and stores them in strRPCUserColonPass. This line (https://github.com/bitcoin/bitcoin/blob/3c565302aaa91ae46aa09b8aeb95206711d9d8a6/src/bitcoin-cli.cpp#L771) adds its BASE64-encoding to the Authentication header. Code: evhttp_add_header(output_headers, "Authorization", (std::string("Basic ") + EncodeBase64(strRPCUserColonPass)).c_str()); When comparing to the Python project I linked earlier, make sure not to use a GET request, but POST instead, as evidenced by this line (https://github.com/bitcoin/bitcoin/blob/3c565302aaa91ae46aa09b8aeb95206711d9d8a6/src/bitcoin-cli.cpp#L790) (EVHTTP_REQ_POST). Title: Re: Get UTXOs of a wallet... Post by: n0nce on March 30, 2022, 05:29:33 PM Alright guys, I got it working with the .cookie file.
As we can see from Bitcoin Core code (https://github.com/bitcoin/bitcoin/blob/3c565302aaa91ae46aa09b8aeb95206711d9d8a6/src/bitcoin-cli.cpp#L771), the cookie file specifies a HTTP Basic Auth user named __cookie__ and a random password that changes after each restart of Bitcoin Core. It is also explained quite well here on StackOverflow (https://bitcoin.stackexchange.com/questions/46782/rpc-cookie-authentication/80969#80969): __cookie__ is the basic auth username and abc123 is a randomly generated password. Therefore, it's enough to add those values to a normal HTTP request. A MVP example using cURL would be (replacing xxxxx with actual content from the .cookie file): Code: curl --user __cookie__:xxxxx -X POST -H 'Content-Type: application/json' -H 'X-Auth-Token: undefined' --data-raw '{"id":1, "jsonrpc":"2.0", "method":"getblockchaininfo"}' http://127.0.0.1:8332 It correctly returns: Code: { Working Python3 code: Code: import requests, json My C++ version which also works fine and just requires that httplib.h file to be downloaded and placed in the same folder: Code: #include "./httplib.h" Code: $~/test/cppbitcointest> g++ -l:libcrypto.so.1.1 bitcointest.cpp -o bitcointest && ./bitcointest I believe the RPC you're looking for is listunspent (https://developer.bitcoin.org/reference/rpc/listunspent.html). By replacing the body in the above code examples with the following, it should return what you're looking for. Code: '{"jsonrpc": "1.0", "id": "curltest", "method": "listunspent", "params": [6, 9999999]}' |