How do you scan the UTXO set for multisig UTXOs that you have the keys to sign (or partially sign)?
In other words, how do the wallets know they are part of a multisig transaction?
Bitcoin Stack Exchange is a question and answer site for Bitcoin users, developers, and enthusiasts. It only takes a minute to sign up.
Sign up to join this communityIt depends on whether its a "Pay to Script Hash" (see BIP 16) multi-signature transaction or just a bare multi-signature transaction.
If it's a "Pay to Script Hash", you need to know all the other public keys involved including your own so you can figure out the script hash.
If it's a bare transaction, than you just need to know your own public key as each public key involved in the multi-signature transaction is listed in the output script.
I did this before - move through the blockchain doing getblock() on each block.. For each tx, decode it, and loop through each output. Analyze the scriptPubKey of each output - if it matches 'OP_HASH160 OP_EQUAL' then you know that the output is paying to a pay-to-script-hash address.
An important note - scripts aren't revealed in the funding transaction, so you don't know if the tx IS a multisig just by looking at the funding output, only that it's pay-to-script-hash.
If it's for a wallet: A wallet can only realize multisig coins belong to it when it has all the public keys, and can produce the necessary script. Wallets are simply scrapers for Pay-to-pubkey-hash or Pay-to-script-hash transactions where you have a pubkey hash or script hash respectively, in your wallet already.
If you're researching these tx's: You can learn the redeemScript when the funds are redeemed. I think there were 14000 multisig outputs that weren't spam, but only 7000 of these were ever redeemed. So, you can only learn if these 7000 were multisig.
http://www.coindesk.com/year-multisig-so-far/ has some graphs on my analysis. I can give you PHP code to repeat this, although DB queries slowed it down massively - I needed to log all txids with multisig outputs, and then check every input in every transaction if they spent a tx I knew to be a P2SH. I aborted and asked someone else to produce the data...