user100690: Assuming the multisig 2 of 3, if I do have extended public keys (xpub) for all 3 cosigners (with proper derivations), can I somewhat get the result address (P2SH)? How to calculate the "script" out of these public keys?
Would be cool to have scheme for doing so or a code/pseudocode in python if possible.

user137501: Let's start from multisig 2-of-3 redeem_script template:
OP_2 OP_DATA_33 key1.public_key OP_DATA_33 key2.public_key OP_DATA_33 key3.public_key OP_3 OP_CHECKMULTISIG
which ties the keys together.
All you need to do here is generate some 3 keys (33 bytes each) from those xpubs (BIP-0032) and concatenate the keys with opcode bytes.
Raw bytes of this script would be (note: the + operator below indicates byte concatenation):
0x52 + 0x21 + key1.public_key + 0x21 + key2.public_key + 0x21 + key3.public_key + 0x53 + 0xae
From that, you compute:
redeem_script_hash = ripemd160(sha256(redeem_script))
Then, you can construct the raw output locking_script:
OP_HASH160 OP_DATA_20 redeem_script_hash OP_EQUAL
by again concatenating the raw bytes:
0xa9 + 0x14 + redeem_script_hash + 0x87
That's it!
You can either use the raw bytes directly if you're constructing a raw transaction yourself, or encode it into a Bitcoin address so other wallets can pay into it.
When time comes to spend, the input script will be of the form:
OP_0 OP_DATA_71 key1.signature OP_DATA_71 key2.signature OP_PUSHDATA1 105 redeem_script
and its raw bytes will be:
0x00 + 0x47 + key1.signature + 0x47 + key2.signature + 0x4c + 0x69 + redeem_script
Here's is a great reference for P2SH & multisig: https://btcinformation.org/en/developer-guide#multisig

100690: <p>Assuming the multisig 2 of 3, if I do have extended public keys (xpub) for all 3 cosigners (with proper derivations), can I somewhat get the result address (P2SH)? How to calculate the &quot;script&quot; out of these public keys?</p>
<p>Would be cool to have scheme for doing so or a code/pseudocode in python if possible.</p>
 137501: <p>Let's start from multisig 2-of-3 <code>redeem_script</code> template:</p>
<p><code>OP_2 OP_DATA_33 key1.public_key OP_DATA_33 key2.public_key OP_DATA_33 key3.public_key OP_3 OP_CHECKMULTISIG</code></p>
<p>which ties the keys together.
All you need to do here is generate some 3 keys (33 bytes each) from those xpubs (<a href="https://en.bitcoin.it/wiki/BIP_0032" rel="nofollow noreferrer">BIP-0032</a>) and concatenate the keys with opcode bytes.</p>
<p>Raw bytes of this script would be (note: the + operator below indicates byte concatenation):</p>
<p><code>0x52 + 0x21 + key1.public_key + 0x21 + key2.public_key + 0x21 + key3.public_key + 0x53 + 0xae</code></p>
<p>From that, you compute:</p>
<p><code>redeem_script_hash = ripemd160(sha256(redeem_script))</code></p>
<p>Then, you can construct the raw output <code>locking_script</code>:</p>
<p><code>OP_HASH160 OP_DATA_20 redeem_script_hash OP_EQUAL</code></p>
<p>by again concatenating the raw bytes:</p>
<p><code>0xa9 + 0x14 + redeem_script_hash + 0x87</code></p>
<p>That's it!
You can either use the raw bytes directly if you're constructing a raw transaction yourself, or <a href="https://en.bitcoin.it/wiki/Base58Check_encoding#Encoding_a_Bitcoin_address" rel="nofollow noreferrer">encode it into a Bitcoin address</a> so other wallets can pay into it.</p>
<p>When time comes to spend, the input script will be of the form:</p>
<p><code>OP_0 OP_DATA_71 key1.signature OP_DATA_71 key2.signature OP_PUSHDATA1 105 redeem_script</code></p>
<p>and its raw bytes will be:</p>
<p><code>0x00 + 0x47 + key1.signature + 0x47 + key2.signature + 0x4c + 0x69 + redeem_script</code></p>
<p>Here's is a great reference for P2SH &amp; multisig: <a href="https://btcinformation.org/en/developer-guide#multisig" rel="nofollow noreferrer">https://btcinformation.org/en/developer-guide#multisig</a></p>
