Query Blockchain: Reference previous transaction in "txin"

1

I'm currently working with a postgresql database I've downloaded here which has the following schema and has probably been parsed using Bitcoin-ABE (I added some indexes and redundant columns): enter image description here

Now I'm stuck trying to make a connection between a transaction input and the previous output. According to the API that belongs to the sql dump, the field prev_out references "the output that this input spends". My problem is, that I can't find any of the txin.prev_out values in tx.hash. Is this line of thinking correct, or am I connecting the wrong fields?

FYI: As you can see both fields are of the type bytea. I was able to use the function encode(hash, 'hex') to get the actual transaction hash and find it at blockchain.info. For the field txin.prev_out however, the hash cannot be found at blockchain.info.

Juergen

Posted 2014-11-30T23:31:54.620

Reputation: 25

Answers

1

txin.prev_out contains the txid in internal byte order. This is the opposite of tx.hash

You need to reverse the bytes. Here's a short python program that will do that:

import binascii
hash = '32d7724fb0d244f50281a500f505818bb655bc029158fd59d477f4445a6ed64a'
def reverse_hash(hash):
    hash = binascii.unhexlify(hash)
    hash = hash[::-1]
    return binascii.hexlify(hash)

print reverse_hash(hash)

Nick ODell

Posted 2014-11-30T23:31:54.620

Reputation: 26 536

I just checked and encode(prev_out,'hex') is 64 hex-characters long, which is 32 bytes, right? Also there is already an extra field for the index of the previous output transaction: prev_out_index. An example would be "32d7724fb0d244f50281a500f505818bb655bc029158fd59d477f4445a6ed64a" which is "2\327rO\260\322D\365\002\201\245\000\365\005\201\213\266U\274\002\221X\375Y\324w\364DZn\326J" in bytea format.Juergen 2014-12-01T00:30:19.127

@Juergen Hmmm. Try reversing the order of the bytes in the prev_out field. Bitcoin uses two endianesses. (One internally, and one for showing to the user.)Nick ODell 2014-12-01T00:32:34.623

Thanks, it worked for me. Unbelievable, I would have never thought of that. I used the reverse function for byteas from here: http://stackoverflow.com/a/26494661/4166885. Why would you say that doesn't work?

Juergen 2014-12-01T00:43:11.030

@Juergen I was reversing the hash without converting it to a sequence of bytes. Whoops. You're right, it does work.Nick ODell 2014-12-01T00:52:49.343