Author

Topic: i need Help in learning how p2sh addresses and tx works. (Read 407 times)

sr. member
Activity: 257
Merit: 343
Andreas has a very good explanation on Bitcoin Transactions in his book "Mastering Bitcoin", which is also online readable: file:///Data/Crypto_CCY/Mastering_Bitcoin_2nd.pdf

There is page 141ff for the tx part, and beginning with 151ff he explains the use case for P2SH in detail for multisig purposes.
HCP
legendary
Activity: 2086
Merit: 4363
You have totally missed the point regarding "P2SH" aka "Pay to Script Hash" addresses and how they work.

These addresses are effectively built from hashes of a "redeem script". They are NOT hashes of a public key (generated from the private key). To spend UTXOs controlled by a P2SH, you need to be able to provide the full redeem script, and any signatures, such that when the script is executed, it does not return false.

You might want to read here: https://bitcoin.org/en/developer-guide#p2sh-scripts

especially this part:
When Bob wants to spend the output, he provides his signature along with the full (serialized) redeem script in the signature script. The peer-to-peer network ensures the full redeem script hashes to the same value as the script hash Alice put in her output; it then processes the redeem script exactly as it would if it were the primary pubkey script, letting Bob spend the output if the redeem script does not return false.


Also, you should probably note this warning in the developer examples:
Warning: You must not lose the redeem script, especially if you don’t have a record of which public keys you used to create the P2SH multisig address. You need the redeem script to spend any bitcoins sent to the P2SH address. If you lose the redeem script, you can recreate it by running the same command above, with the public keys listed in the same order. However, if you lose both the redeem script and even one of the public keys, you will never be able to spend satoshis sent to that P2SH address.


Effectively, if you lose the redeem script, you will have NO WAY to spend any coins sent to a P2SH address unless you have some method to recreate it (in the case of a MultiSig Address, if you have all the public keys, you could recreate the redeem script)... you would of course still need any private keys associated with the P2SH address to be able to sign the transaction (if required).
sr. member
Activity: 434
Merit: 270
i have had discussion here , https://bitcointalksearch.org/topic/elliptic-curve-operations-in-bitcoin-2294855

ill resume it here in this thread.

i was under impression that address starting with 3 needs more than 1 private keys to send funds. but looks like that is not the case

from :  https://bitcointalksearch.org/topic/elliptic-curve-operations-in-bitcoin-2294855.msg23374174

post id 11 : https://bitcointalksearch.org/topic/elliptic-curve-operations-in-bitcoin-2294855.msg23374174#msg23374174


from 1 sha256 private key i can create WIF private key and public kay pair which is
5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf  1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm

how would i get p2sh address for something like that ?

or am i missing again something .,

does the p2sh address's private key also falls in

int range
1 - 115792089237316195423570985008687907852837564279074904382605163141518161494336

if so then how can i generate a pair ?

lets say , so far i have these

input                        : 1
network                      : Bitcoin mainnet
netcode                      : BTC
secret exponent              : 1
 hex                         : 1
wif                          : KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn
 uncompressed                : 5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf
public pair x                : 55066263022277343669578718895168534326250603453777594175500187360389116729240
public pair y                : 32670510020758816978083085130507043184471273380659243275938904335757337482424
 x as hex                    : 79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
 y as hex                    : 483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
y parity                     : even
key pair as sec              : 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
 uncompressed                : 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\
                                 483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
hash160                      : 751e76e8199196d454941c45d1b3a323f1433bd6
 uncompressed                : 91b24bf9f5288532960ac687abb035127b1d28a5
Bitcoin address              : 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
Bitcoin address uncompressed : 1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm
Bitcoin segwit address       : p2y59b9U5YTUAYEDBr5zwSHFoM8pn3ozAhRD






post id 12 : https://bitcointalksearch.org/topic/elliptic-curve-operations-in-bitcoin-2294855.msg23375280#msg23375280



from 1 sha256 private key i can create WIF private key and public kay pair which is
5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf  1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm

That is NOT a public key.

That is a bitcoin address.  Specifically, that is an uncompressed key P2PKH bitcoin address.

From that SAME WIF private key, you could also generate a compressed key P2PKH bitcoin address:
1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH

Furthermore, you could create 2160 different P2SH bitcoin addresses.  Here are a few of them:
38fEX6RbBBMmpu3nbbuULku1xyrrzqqqnE
3Q2iKGxFppUJEZUTUMmahjwUoLNRqDY9o3
3CZj1DTSHD4vhr3zALL3pgs6acyw1deYDt

how would i get p2sh address for something like that ?

Build a bitcoin output script.  Hash it.  Concatenate a byte value of 0x05 in front of the hash value. Encode the result with base64check encoding.

or am i missing again something .

Probably.

does the p2sh address's private key also falls in

int range
1 - 115792089237316195423570985008687907852837564279074904382605163141518161494336

if so then how can i generate a pair ?

P2SH means "pay to script hash".  It doesn't necessarily need a key, but is it is safer to build a script that requires a signature (in which case a signature would be required).


so how can i spend this output https://blockchain.info/unspent?active=3LmzcN7f4M8dnvTnoHV3BF8n8i1mLE1udr&format=html


Code:
{
    
    "unspent_outputs":[
    
        {
            "tx_hash":"3435869ad5a272e7d14306e73ea563d763ac65f28fc7672d1e16e47497950253",
            "tx_hash_big_endian":"5302959774e4161e2d67c78ff265ac63d763a53ee70643d1e772a2d59a863534",
            "tx_index":284855121,
            "tx_output_n": 17,
            "script":"a914d15b5f90ef9d3120c3704dac91055b35d180c36b87",
            "value": 109619902000,
            "value_hex": "1985daf630",
            "confirmations":7624
        }
      
    ]
}

Code:
tx_hash_big_endian : 5302959774e4161e2d67c78ff265ac63d763a53ee70643d1e772a2d59a863534
tx_output_n : 17
value : 109619902000

out address : 1P6oVB6r1KKZYNQQVS1QqJy6tFDrHMLCt6
createrawtransaction i get this

Code:
{
"result": "02000000013435869ad5a272e7d14306e73ea563d763ac65f28fc7672d1e16e474979502531100000000ffffffff0100e87648170000001976a914f26ba6c2e4fe4bc4a1cac44d0e2c9f525195cb0388ac00000000",
"error": null,
"id": null
}

if i decode tx i get this

Code:
{
   "lock_time":0,
   "size":85,
   "inputs":[
      {
         "prev_out":{
            "index":17,
            "hash":"5302959774e4161e2d67c78ff265ac63d763a53ee70643d1e772a2d59a863534"
         },
         "script":""
      }
   ],
   "version":2,
   "vin_sz":1,
   "hash":"2b05faacb73e30da3166e5c932f429c8b34c78ed6a0981c6366fb6357f2bfc45",
   "vout_sz":1,
   "out":[
      {
         "script_string":"OP_DUP OP_HASH160 f26ba6c2e4fe4bc4a1cac44d0e2c9f525195cb03 OP_EQUALVERIFY OP_CHECKSIG",
         "address":"1P6oVB6r1KKZYNQQVS1QqJy6tFDrHMLCt6",
         "value":100000000000,
         "script":"76a914f26ba6c2e4fe4bc4a1cac44d0e2c9f525195cb0388ac"
      }
   ]
}

what next ?
is this correct so far ?

Thanks for your time.
Jump to: