Monday, 20 April 2020

Calculate txn_id from raw txn_hex

I'm trying to calculate a txn_id from raw txn_hex. The procedure works fine for legacy TXNs but gets non-expected results on Segwit TXNs. I compared this snippet of code to what txn_id was produced by Electrum and the blockchain.com TXN decoder:

  1. Take in TXN in hex
  2. Convert the hex to binarray
  3. Double hash binarray
  4. Reverse the resultant digest because of endianness
  5. Display in hex.

t0 is my legacy testnet TXN and t1 is my segwit testnet TXN.

Thoughts?

Update: Perhaps BIP-140

```python from hashlib import sha256

def txid_from_tx(tx): bin = bytes.fromhex(tx) txid = sha256(sha256(bin).digest()).digest()[::-1].hex() return txid

t0 = ('0200000001cd3b93f5b24ae190ce5141235091cd93fbb2908e24e5b9ff6776ae' 'c11b0e04e5000000006b4830450221009f156db3585c19fe8e294578edbf5b5e' '4159a7afc3a7a00ebaab080dc25ecb9702202581f8ae41d7ade2f06c9bb9869e' '42e9091bafe39290820438b97931dab61e140121030e669acac1f280d1ddf441' 'cd2ba5e97417bf2689e4bbec86df4f831bf9f7ffd0fdffffff010005d9010000' '00001976a91485eb47fe98f349065d6f044e27a4ac541af79ee288ac00000000')

t1 = ('0200000000010100ff121dd31ead0f06e3014d9192be8485afd6459e36b09179' 'd8c372c1c494e20000000000fdffffff013ba3bf070000000017a914051877a0' 'cc43165e48975c1e62bdef3b6c942a38870247304402205644234fa352d1ddbe' 'c754c863638d2c26abb9381966358ace8ad7c52dda4250022074d8501460f4e4' 'f5ca9788e60afafa1e1bcbf93e51529defa48317ad83e069dd012103adc58245' 'cf28406af0ef5cc24b8afba7f1be6c72f279b642d85c48798685f86200000000')

print(f"t0: {txid_from_tx(t0)}\nt1: {txid_from_tx(t1)}")

From this code

t0: cb33472bcaed59c66fae30d7802b6ea2ca97dc33c6aad76ce2e553b1b4a4e017

t1: b11fdde7e3e635c7f15863a9399cca42d46b5a42d87f4e779dfd4806af2401ce

From Electrum

t0: cb33472bcaed59c66fae30d7802b6ea2ca97dc33c6aad76ce2e553b1b4a4e017

t1: d360581ee248be29da9636b3d2e9470d8852de1afcf3c3644770c1005d415b30

```

submitted by /u/brianddk
[link] [comments]

No comments:

Post a Comment