<p><code>txin.prev_out</code> contains the txid in internal byte order. This is the opposite of <code>tx.hash</code></p>

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

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

print reverse_hash(hash)
</code></pre>

CC BY-SA 3.0
2014-12-01T00:30:19.127Z
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
CC BY-SA 3.0
2014-12-01T00:32:34.623Z
@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
CC BY-SA 3.0
2014-12-01T00:43:11.030Z
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
CC BY-SA 3.0
2014-12-01T00:52:49.343Z
@Juergen I was reversing the hash without converting it to a sequence of bytes. Whoops. You're right, it does work.
Nick ODell
CC BY-SA 3.0
2014-11-30T23:57:32.833Z
2014-12-01T00:51:35.170Z
2014-12-01T00:51:35.170Z
Nick ODell
<p>I'm currently working with a postgresql database I've downloaded <a href="http://dumps.webbtc.com/bitcoin/" rel="nofollow noreferrer">here</a> which has the following schema and has probably been parsed using Bitcoin-ABE (I added some indexes and redundant columns):
<img src="https://i.sstatic.net/KtNLV.png" alt="enter image description here"></p>

<p>Now I'm stuck trying to make a connection between a transaction input and the previous output. According to the <a href="http://webbtc.com/api/tx" rel="nofollow noreferrer">API</a> that belongs to the sql dump, the field <em>prev_out</em> references "the output that this input spends". My problem is, that I can't find any of the <em>txin.prev_out</em> values in <em>tx.hash</em>. Is this line of thinking correct, or am I connecting the wrong fields?</p>

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

<blockchain><abe>
Query Blockchain: Reference previous transaction in "txin"
Juergen
# Query Blockchain: Reference previous transaction in "txin"

(Asked by: username_0 on 2014-11-30)

I'm currently working with a postgresql database I've downloaded [here](http://dumps.webbtc.com/bitcoin/) which has the following schema and has probably been parsed using Bitcoin-ABE (I added some indexes and redundant columns):
![enter image description here](https://i.sstatic.net/KtNLV.png)

Now I'm stuck trying to make a connection between a transaction input and the previous output. According to the [API](http://webbtc.com/api/tx) 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.

**Tags:** blockchain, abe


---

## 1 answers

### Answer by username_1 on 2014-11-30. Score: 1 | Accepted answer

`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)

```

> **Comments:**
> **username_0**: 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.  
> **username_1**: @username_0 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.)  
> **username_0**: 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?  
> **username_1**: @username_0 I was reversing the hash without converting it to a sequence of bytes. Whoops. You're right, it does work.  

---

