18

I am playing around with Bitcoin-qt. I want to iterate all blocks and all their transactions.

So far I've been able to get a list of all tx's in the blocks I want to. Now I want to get information about the transaction: Amount, confirmations and so forth.

However, when I use the gettransaction method of the Bitcoin-qt API, it seems I can only get transactions from my own wallet.

For instance, I cannot say, because I get the error "Invalid or non-wallet transaction id (code -5) ":

gettransaction 0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098

However, when I have a TX of one of my own transactions, I get a correct response.

My code

public void AddBtcTransaction(string hash)
        {
            List<string> parameters = new List<string>();
            parameters.Add(hash);

            var data = RequestServer("gettransaction", parameters);
            int i = 0;
        }

        public string RequestServer(string methodName, List<string> parameters)
        {
            string respVal = string.Empty;

            var rawRequest = GetRawRequest();
            JObject joe = new JObject();
            joe.Add(new JProperty("jsonrpc", "1.0"));
            joe.Add(new JProperty("id", "1"));
            joe.Add(new JProperty("method", methodName));

            JArray props = new JArray();
            foreach (var parameter in parameters)
            {
                props.Add(parameter);
            }

            joe.Add(new JProperty("params", props));

            // serialize json for the request
            string s = JsonConvert.SerializeObject(joe);
            byte[] byteArray = Encoding.UTF8.GetBytes(s);
            rawRequest.ContentLength = byteArray.Length;
            Stream dataStream = rawRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            StreamReader streamReader = null;
            try
            {
                WebResponse webResponse = rawRequest.GetResponse();

                streamReader = new StreamReader(webResponse.GetResponseStream(), true);

                respVal = streamReader.ReadToEnd();
                var data = JsonConvert.DeserializeObject(respVal).ToString();
                return data;
            }
            catch (Exception exp)
            {

            }
            finally
            {
                if (streamReader != null)
                {
                    streamReader.Close();        
                }

            }
            return string.Empty;
        }

        private HttpWebRequest GetRawRequest()
        {

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ServerIp);
            webRequest.Credentials = new NetworkCredential(UserName, Password);
            /// important, otherwise the service can't desirialse your request properly
            webRequest.ContentType = "application/json-rpc";
            webRequest.Method = "POST";

            return webRequest;
        }

Any idea about how I would get information about transactions outside my wallet?

22

Enable txindex=1 in your bitcoin.conf (You'll need to rebuild the database as the transaction index is normally not maintained, start using -reindex to do so), and use the getrawtransaction call to request information about any transaction (it won't work for the genesis block's coinbase transaction though, it's a special case).

Note that this will only give you raw transaction data (you can add a 1 argument after the call to have it decoded instead of in hexadecimal notation), but it cannot know the payment amount or fees, for example.

  • Just to be clear: You cannot get information about payment amount or fees - at all? :-) Because that sounds quite wrong (Blockchain.info does this). I am reindexing the blockchain now :-) – Lars Holdgaard Jun 21 '13 at 17:01
  • 1
    You can compute the fee yourself by subtracting the amounts of all outputs from the sum of the amounts of all inputs (which you'll have to look up through getrawtransaction too). You can of course see the amount of a particular output, but unless the address it is to is yours, you can't know for example which is change and which is the payment. Wallets are designed to do all that for you, if you bypass them, you need to do things manually. – Pieter Wuille Jun 21 '13 at 17:26
  • Thanks a lot. I also want other people to look at "decoderawtransaction" -> GREAT function. Thanks again :-) – Lars Holdgaard Jun 21 '13 at 20:03
  • 1
    I was able to use bitcoin-cli decoderawtransaction $(bitcoin-cli getrawtransaction txid) to get transaction details. Seems that the answer to use txindex=1 is out of date? – jangorecki Aug 26 '15 at 12:55
  • 1
    jangorecki: getrawtransaction always works on transactions that are not yet completely spent. – Pieter Wuille Aug 26 '15 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.