rust-bitcoin / rust-miniscript

Support for Miniscript and Output Descriptors for rust-bitcoin
Creative Commons Zero v1.0 Universal
349 stars 136 forks source link

Provide a simple way to get the sighash_type for any input in a transaction. #690

Open iota9star opened 5 months ago

iota9star commented 5 months ago

Related to issue https://github.com/rust-bitcoin/rust-bitcoin/issues/2788

apoelstra commented 5 months ago

cc @sanket1729 I think the crux of this issue is that the interpreter API should be better documented?

"List the sighash types used in the transaction" actually seems like a nice example program to add to the docs.

apoelstra commented 5 months ago

BTW @iota9star can you say more about what you're trying to do here? If you need this information when constructing a transaction, you would be better off extracting it from a PSBT, which is a data structure designed to make such information easily accessible during construction.

If you need this information for an already-complete transaction, that's where the interpreter API comes in. But as far as I'm aware, the only real use for the interpreter API has been debugging Miniscript itself.

iota9star commented 5 months ago

@apoelstra I want to implement an indexer to analyze on-chain confirmed transactions to determine if they are buy/sell transactions. My idea is to use sighash_type and other conditions to identify such transactions. Do you think this is the correct approach?

apoelstra commented 5 months ago

If you define "buy" and "sell" purely in terms of sighashes, then sure, it's the right approach. But if you expect these to correspond to some economic reality, then no, you cannot determine this in general since there are many ways that people can do atomic trades on-chain or across chains. And even the sighash-based ways may be "erased" if both parties construct an explicit on-chain transaction then decide instead to publish a 2-of-2 MuSig transaction which cannot be distinguished from any other transaction. (And of course, most trades happen on centralized exchanges with no blockchain footprint.)

This certainly sounds like interesting data in its own right. But if you're hoping to learn something about the Bitcoin markets, I'm doubtful that you'll get much.

Anyway, this is exactly the sort of "blockchain forensics" task that the interpreter API should be good for, and we should document it to help with this usecase.

iota9star commented 5 months ago

@apoelstra For most on-chain exchanges, the majority of transactions are constructed using single + anyonecanpay. This is sufficient for my purposes. Additionally, I will index other types of data, such as transactions related to the Runes protocol or other protocol transfers and mints.

iota9star commented 5 months ago

@apoelstra I don't know how to use the interpreter API. Can you provide more information to help me? 🙈

sanket1729 commented 4 months ago

@iota9star, here is an example iterating through all the satisfied signatures in the transactions.