I’m having trouble with SIGHASH_SINGLE | ANYONECANPAY (S|A) flag when trying to combine transactions. I thought it would be straightforward, but it’s not working for me at all. Created two simple transactions on testnet4, each one using only one input and output, both P2PKH. Here’s the first transaction: 0100000001bd20399c1f2d841f1e147ad2e03df1635f48fc5b8cdbb55ea3a86b14225e274300000 0006a473.
Issues with SIGHASH_SINGLE | ANYONECANPAY in Transactions
4 replies 403 views
When you use SIGHASH_SINGLE, things get tricky. Even though you're signing just one output, the digest differs depending on the output count. The output count is part of the signing process. For instance, if you're signing the first input with one output, but the second input has two outputs, it changes everything. You have to make sure to write -1 for each output you aren't signing, which complicates matters a bit.
Thanks for clarifying that! There are some sources that point to combining S|A transactions as the main reason to use this flag. I get now that:
1. You can’t just combine two S|A transactions that don’t know about each other.
2. But, you can sign an S|A transaction first and then add more inputs/outputs later using flags like ALL.
It’s not just a simple yes or no. If you really wanna get to the bottom of this, create precise examples like you did earlier. Use reliable code to generate sighash values and check if they match. Using actual transactions for analysis can help us understand if it works or fails. Take, for example, tx1, tx2, and txCombined. Just experimenting with those gives more insight.
Thanks for the advice! After your explanation, I added a dummy output to my second transaction during serialization, and guess what? It worked!
But I have another question: when there are more inputs than outputs, and I’m using SIGHASH_SINGLE | ANYONECANPAY for all inputs, how do you serialize the outputs for those extra inputs? Like in this transaction I found: https://mempool.space/tx/1395dde8ec0152f4cb090f4d4e8dcb14cc0d8bbba7.