WebOfTrustInfo / btcr-hackathon-2017

Virtual hackathon to create spec and code for Bitcoin-based Decentralized Identifiers (DIDs)
https://weboftrustinfo.github.io/btcr-hackathon-2017/
14 stars 7 forks source link

txid2bech32 code and libbtc giving different answers #34

Closed ChristopherA closed 7 years ago

ChristopherA commented 7 years ago

@shannona —

When I bitcoin-cli gettransaction for testnet transaction f8cdaff3ebd9e862ed5885f8975489090595abe1470397f79780ead1c7528107 I get a named JSON value "blockindex": 1,

You can see the transaction here: https://live.blockcypher.com/btc-testnet/tx/f8cdaff3ebd9e862ed5885f8975489090595abe1470397f79780ead1c7528107/

You can see the block the transaction is in here: https://live.blockcypher.com/btc-testnet/block/00000000b3487880b2814da8c0a6b545453d88945dc29a7b700f653cd7e9cdc7/

Note that it is block 1152194 and it is the 2nd transaction (index 1 as index 0 is the coin base reward)

When I use the txid2bech32 tool, I get:

./txid2bech32 f8cdaff3ebd9e862ed5885f8975489090595abe1470397f79780ead1c7528107
tx1-xxyv-xxxx-fpmf-u0

When I use the latest rev of https://github.com/libbtc/libbtc, and correct the txref to the latest format for testnet, (c/tx1/testtx/) and run it against that tool (which does not talk to bitcoind), I get:

$ ./bitcoin-txref testtx-xxyv-xxxx-fpmf-u0
Height: 4200205
Position: 0
Magic: 0
Notice: different network

Note this is not the height nor index above:

If do use the libbtc tool to do the reverse, given height and index:

./bitcoin-txref -t 1152194 1
Height: 1152194
Position: 1
Network: testnet3
txtest1-xyv2-xzyq-qqm5-tyke

Clearly something is wrong. It is slightly possible that there is a bug in @jonasschnelli code in libbtc as it was just added a few days ago, but I wouldn't think so given the testing code he uses.

cc: @kimdhamilton

jonasschnelli commented 7 years ago

I recently updated the txref code to confirm with the BIP. Testnet must use txtest as human readable part and we have higher bounds for max height and pos (this why it's two bytes longer).

I think my implementation (use newest master either libbtc or the txref repo) is correct.

jonasschnelli commented 7 years ago

The testnet support was just added, the last time I reviewed @shannona txid2bench32 it was using mainnet logic for testnet (my reference implementation was not ready for testnet during the time @shannona had copied it over). See the BIP for testnet: https://github.com/bitcoin/bips/pull/555

shannona commented 7 years ago

So your newest txref code might be all that's needed? I'll give it a shot.

jonasschnelli commented 7 years ago

@shannona: I think so. You will need some adoption on the caller side. Make sure you have large enough buffers.

shannona commented 7 years ago

Right. Got it. Will take a look in the morn. Thank you!

shannona commented 7 years ago

I've updated to the newest version of txref_code.c (and .h), with the testnet support, and now I get similar, but maddeningly different results. It's maddening because as far as I can tell I'm providing the same input. The blockheight and index check out:

./txid2bech32 f8cdaff3ebd9e862ed5885f8975489090595abe1470397f79780ead1c7528107
Txid: f8cdaff3ebd9e862ed5885f8975489090595abe1470397f79780ead1c7528107
Height: 1152194
Position: 1
Network: testnet3
HRP: txtest
Magic: 
Non-Standard: 1
Txref: txtest1-xxyv-xzxz-qqmq-tyke

Versus:

./bitcoin-txref -t 1152194 1
Height: 1152194
Position: 1
Network: testnet3
HRP: txtest
Magic: 
Non-Standard: 1
Txref: txtest1-xyv2-xzyq-qqm5-tyke

This is my invocation for the testnet case:

success = btc_txref_encode(encoded_txref, TXREF_BECH32_HRP_TESTNET, TXREF_MAGIC_BTC_TESTNET, blockheight, blockindex, 1);

The updated files are here. Any idea what mistake I'm making?

shannona commented 7 years ago

Unshockingly, the correct reference decodes, and mine does not at all:

$ ./bitcoin-txref txtest1-xyv2-xzyq-qqm5-tyke
Height: 1152194
Position: 1
Magic: 6
Notice: different network
$ ./bitcoin-txref txtest1-xxyv-xzxz-qqmq-tyke
Error decoding transaction reference.
kimdhamilton commented 7 years ago

@shannona, FYI when I run my playground tool with txid f8cdaff3ebd9e862ed5885f8975489090595abe1470397f79780ead1c7528107, I get tx1-xyv2-xxqq-fpmp-u0.

I haven't attempted to run the c code yet, and I should output more details (intermediate steps like block height and index). I'll probably get to that tomorrow.

shannona commented 7 years ago

Which doesn't match Jonas' results. In fact, it's apparently not the correct style for a testnet txref, which should start with 'txtest1'. I suspect you're having the same problem I did, which is using the version of his tools prior to three days ago, before he introduced testnet support. (But as you can see, even with the code I updated today, I still can't quite get my results to match his.)

shannona commented 7 years ago

@jonasschnelli if you had any insight into why I can't quite get the right answer (see the 3rd and 4th answers above), it'd be quite welcome.

shannona commented 7 years ago

Full explanation of latter problem here. It looks like you get the right results if you compile -O2 and there's a memcpy error if you don't, which is presumably a bug in the library.

Closing this as a duplicate for that thread.