2

I'm trying to extract the bech32 address associated to an input or output using the txinwitness field included in every pure SegWit transaction.

An example with transaction 0x6b6167a3d35efb043639837bbac853903d2c800ba1e324be0fffb2858e251da0 would return this output:

$ bitcoin-cli decoderawtransaction "$TX_DATA" [...] "vin": [ { "txid": "22b3f827d5574045f4b9b1dea448d66d92b2f334741687b8a3d778672530cfea", "vout": 1, "scriptSig": { "asm": "", "hex": "" }, "txinwitness": [ "30450221008695b35cee8bd73fe870868b9f281ffd6f375b6082369310c910f35d524536fe022050c7c8442823af465acec6034283adb13b9481d57741a2be668d59322ea50b3701", "02959a2410df7168279d9c8372bbd7b7e2d27959987dd3fae64b10e9c2d62b2e75" ], "sequence": 4294967295 }, [...]

I'm using the API in Bitcoin Core 0.16 but I haven't find any way of querying the Segregated Witness tree with that txinwitness data. The documentation or Google hits about this are almost non-existent, other than people commenting that sites like e.g. blockchain.info don't use Bitcoin Core and they have other ways of getting that information (because they show the bech32 address in the transaction info page).

1

I'm trying to extract the bech32 address associated to an input or output using the txinwitness field included in every pure SegWit transaction.

This is not entirely possible unless you're willing to make assumptions that will almost certainly break in the future. The problem is that part of the data contained in a segwit address is not part of the txinwitness field---specifically, the witness program number that allows upgrading segwit to new script rules in the future is included only in the scriptPubKey of the output being spent.

That means, to be safe, you need to use an RPC like getrawtransaction with the transaction index enabled to retrieve the output being spent in order to get the address. This is unfortunately slow if you need this information for a lot of inputs.

The unsafe assumption you can make is that the witness program number is 0, as is the case with all standard segwit transactions right now. Then you can just hash the final entry in the txinwitness per the BIP141 rules, prefix its length (either 0x14 or 0x20), and prefix 0x00 to indicate it's witness program 0. But if you make that assumption, you will print the wrong address for transactions that use a different witness program number in the future, which could cause users of your system to lose money, so I highly recommend against that.

  • That is what I thought. Many thanks, David! – Ender Jul 30 '18 at 0:48

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.