Explanation of the script P2SH, how is Bitcoin Core executed?

This question is a continuation of my post yesterday.

Today I have a question about P2SH execution, so this should be an example of the script

OP_0   OP_2   
 OP_3 OP_CHECKMULTISIG
OP_HASH160  OP_EQUAL

or should the P2SH key be used this is (i think something in this script on the left to convert the hash160 within the P2SH key, right?)

OP_0   OP_2   
 OP_3 OP_CHECKMULTISIG
OP_HASH160  OP_EQUAL

Well, if I understood it well, the simulation of the execution is divided into two phases

  1. The hash scriptSig is equal to the hash within the scirptPubKey

simulation


  1. The script sing is executed like the script multi-signature

simulation

My questions are:

  • With the P2SH key is the execution the same? if so, in which other form is the P2SH with the P2SH key?

I also read this post, but I do not believe it contains that answer.

Unfortunately, if my sample script is wrong, I'll have to make a correction

Bitcoin Core – Create P2SH address does not match the address stored on the RPC node

I have a private key and I'm trying to create a P2SH address on testnet how my node was created, but they are not identical. Please help me a bit, thank you.

Private key: c5964e19634e473e9c8fed3b149c4d44b6d62d62724035aad3eb911a1495ae27

P2SH Address of 1 Threshold and 1 Pubkey:
2N4GriyLchUgwwCBonzYHgsCHNhYRHW45w7

P2WPKH address: 2NGA8uGdnaQ21phNpVyV5uye5cbUtudxBWh || tb1qld2fzw9gj30wxjamsz73fug62das7cyq0em4mn

Address from the node: 2N8qKVKuR3QAy2cA5fF2QbuCpBGfGCmbUHx

Here's more detail from getaddressinfo:

{
    "result": {
        "address": "2N8qKVKuR3QAy2cA5fF2QbuCpBGfGCmbUHx",
        "scriptPubKey": "a914aafc9f09b99cbb1ba46001ec4868ba4fb32543ca87",
        "ismine": true,
        "solvable": true,
        "desc": "sh(wpkh((a1696bdc/0'/0'/375509')02a7065a210c28d27a591e55ac6be092111349fc4b23927bb4d13c46a683bcfff7))#hrgzj3gs",
        "iswatchonly": false,
        "isscript": true,
        "iswitness": false,
        "script": "witness_v0_keyhash",
        "hex": "00146da71bc5de0c0b59bbf73520df7e7d4048d02594",
        "pubkey": "02a7065a210c28d27a591e55ac6be092111349fc4b23927bb4d13c46a683bcfff7",
        "embedded": {
            "isscript": false,
            "iswitness": true,
            "witness_version": 0,
            "witness_program": "6da71bc5de0c0b59bbf73520df7e7d4048d02594",
            "pubkey": "02a7065a210c28d27a591e55ac6be092111349fc4b23927bb4d13c46a683bcfff7",
            "address": "tb1qdkn3h3w7ps94nwlhx5sd7lnagpydqfv5pyqurf",
            "scriptPubKey": "00146da71bc5de0c0b59bbf73520df7e7d4048d02594"
        },
        "label": "test",
        "ischange": false,
        "timestamp": 1561438016,
        "hdkeypath": "m/0'/0'/375509'",
        "hdseedid": "e2e9dd17501f967b789c28c4be1dbf64897ad8d2",
        "hdmasterfingerprint": "a1696bdc",
        "labels": (
            {
                "name": "test",
                "purpose": "receive"
            }
        )
    },
    "error": null,
    "id": 1
}

Bitcoin Core – What is P2PK, P2PKH, P2SH, P2WPKH – ELI5

This is how transactions are carried out. You request from the sender a valid signature (from the private key) and a public key. The transaction output script uses the signature and the public key and verifies that they match the public key hash based on some cryptographic functions. If so, the funds are spent. This method hides your public key in the form of a hash for additional security.


This is similar to P2PKH. The difference is that your public key is not hidden. Anyone sending money over the P2P network using this method displays their public key in the transaction details.


The outputs of a transaction are only scripts that, when executed with certain parameters, give a Boolean value of true or false, When a miner executes and returns the output script with the given parameters truethe money will be sent to your desired issue. P2SH is used for multi-signature wallets, where the output script logic is checked for multiple signatures before accepting the transaction. P2SH can also be used to enable anyone or anyone to spend the funds. If the output script of a P2SH transaction is even 1 If true, the attempt to output without specifying parameters will only result 1 Spend the money for anyone who tries. This also applies to returned scripts 0, so that no one can output the output.

This can also be used for puzzles like this.


This was a feature of segwit What Segregated Witness stands for. Instead of using scriptSig Parameters to check the validity of the transaction, there is a new part of the transaction called witness where the validity occurs.


What's the difference between BTC in P2SH and Unspent P2SH output here?

This means that of all P2SH Transactions (transactions that can be issued by anyone with a set of parameters (scriptSig) that lead to the execution of scriptPubkey) true) those with unissued expenses were not redeemed.

I recommend this video by Andreas Antonopoulos about bitcoin scripts.

bitcoin cash – To create a p2sh whose script contains a signature

I created a P2SH address, funded it, and tried to move the funds by bank transfer.

The RedeemScript for sending requires the TX signature (OP_CHECKSIGVERIFY), but to create this signature, the TX PreImage must be created and hashed. However, the preImage for a P2SH contains the TX RedeemScript itself. This seems to create a loop in which this functionality is not possible.

Am I doing something wrong or are I thinking about things?

bitcoind – Method for creating a P2SH address

I'm trying to manually create an HTML Smart contract by writing a script and finding the hex with the BTCC compiler. Now node A must create a transaction with this hex and lock money in that p2sh address for up to 150 blocks. If Node B can provide the secret value before 150 blocks, he can request the coins, and if not, he goes back to Node A. I am new to this concept, and wherever I read about creating transactions , they are explained in a theoretical way and I could not understand what to do with the redemption script hex and how money is blocked and how node B claims it before 150 blocks and how node A's raw transaction will be accessible to node B. Can you please provide a reference or follow steps to understand and implement it? I would like to start from scratch with the step-by-step instructions.

Which documentation should I refer to and which commands should be used and how should it be used?

How to create the p2sh address. And another problem I face is that every time I get another hash on another website. Not sure which is correct. Please lead me.

S. I am using Bitcoin in repeat test mode.
collapse

wallet – How do I create a valid P2SH address with the Bitwasp library that can be used with Electrum?

There is something I did not understand about different address formats. In short: I create a master key, import it via xprv key in Electrum, generate P2SH and P2WSH addresses, send him coins, but they do not appear.

  1. I created an extended key in the test network with Bitwasp: tprv8ZgxMBicQKsPd1Euv7Q428UserpbDf6mxhRtjevgfPcL4XwU8aNAXqMp6QAQjPU5bCeskWBUtqZYS1UHRYQuYzrcZFppbsS2JgWjUzgK65,

  2. I imported this key in Electrum, which also runs on testnet.

  3. When I click on "Receive", I get the following address: n28hWyXh8VPBCV6seKty19dW5ZNbqapB58
  4. I sent some Testnet coins to this address, which became very visible in Electrum.

  5. Using the BitWasp library, I wrote the following code (inspired by its own example code:

                                $ network = Bitcoin :: setNetwork (NetworkFactory :: bitcoinTestnet ());

$ random = new Random ();
$ hdFactory = new HierarchicalKeyFactory ();

$ master = $ hdFactory-> fromExtended ("tprv8ZgxMBicQKsPd1Euv7Q428UserpbDf6mxhRtjevgfPcL4XwU8aNAXqMp6QAQjPU5bCeskWBUtqZYS1UHRYQuYzrc


$ addrReader = new AddressCreator ();
$ publicKey = $ master-> getPublicKey ();
$ helper = new P2pkhScriptDataFactory ();
$ scriptData = $ helper-> convertKey ($ publicKey);
$ script = $ scriptData-> getScriptPubKey ();

echo "key hash types  n";
$ p2pkh = $ scriptData-> getAddress ($ addrReader);
echo "* p2pkh address: {$ p2pkh-> getAddress ()}  n";

Echo " nScript hash types:  n";

$ redeemScript = new P2shScript ($ p2pkh-> getScriptPubKey ());
$ p2shAddr = $ redeemScript-> getAddress ();
echo "* p2sh: {$ p2shAddr-> getAddress ()}  n";
$ p2wshScript = new WitnessScript ($ p2pkh-> getScriptPubKey ());
$ p2wshAddr = $ p2wshScript-> getAddress ();
echo "* p2wsh: {$ p2wshAddr-> getAddress ()}  n";
$ p2shP2wshScript = new P2shScript (new WitnessScript ($ p2pkh-> getScriptPubKey ()));
$ p2shP2wshAddr = $ p2shP2wshScript-> getAddress ();
echo "* p2sh | p2wsh: {$ p2shP2wshAddr-> getAddress ()}  n";

It gives me the following output:

WIF: cUhtBXtsVhSikXiD46B5dKsCTjKqZGTWEADxSMNknP1V6VZfm1zR
Key hash types
* p2pkh address: mjCptm7yxr3c7DpA6i17ude5ZkiHQMz7Rr

Script hash types:
* p2sh: 2MsjH4FEdpcwmwaTtbW3HFnx8aw1DCfN74G
* p2wsh: tb1qnwsk39m5yw4m3h36ddvw9euvl0vxl7vg8mrmyj777pwl74crkassxgg7l2
* p2sh | p2wsh: 2NFS1fXMuAJuJWaGAwMi7puE86n5XnbLEPo
  1. Why is the reception address in Electrum so different from the one I generated with the Bitwasp library? None of the generated addresses corresponds to the address from Electrum
  2. If you send coins to Electrum's address, the coins received will be displayed
  3. Send coins eg to P2shScript address (2MsjH4FEdpcwmwaTtbW3HFnx8aw1DCfN74G) will never appear in the Electrum wallet, even if they are based on the same private key
    I also tried to send coins to the p2wsh-address (tb1qnwsk39m5yw4m3h36ddvw9euvl0vxl7vg8mrmyj777pwl74crkassxgg7l2), which never gets coins to which was sent.

Where is my mistake?

after the answer of @Abdussamad I have tried the following: derivePath and then send coins to this address. I also imported the XPRV of the derived key in Electrum and sent some Satoshis to its address. Nothing came in.

Additional code:

$ purposePriv = $ master-> derivePath ("{$ purpose} # / 0 / / 0 # / 0/0");
echo "m / {$ purpose} & # 39; / 0 & # 39; / 0/0/0 & # 39 ;:". $ purposePriv-> toExtendedPrivateKey (). PHP_EOL;
echo "M / {$ purpose} & # 39; / 0 & # 39; / 0/0/0 & # 39 ;:". $ purposePriv-> toExtendedPublicKey (). PHP_EOL;

$ purposePriv = $ master-> derivePath ("{$ purpose} & # 39; / 0 & # 39; / 0 & # 39;");

$ masterAddr = new PayToPubKeyHashAddress ($ purposePriv-> getPublicKey () -> getPubKeyHash ());
Echo "Address:". $ masterAddr-> getAddress (). " n  n";

When you send coins to the address, they are sent to a different location, not your wallet.

I tried the following code that was suggested here:

$ mnemonic = "blahblahblah";
$ seed = (new Bip39SeedGenerator ()) -> getSeed ($ mnemonik);
$ purpose = 44;
$ factory = new HierarchicalKeyFactory ();
$ root = $ factory-> fromEntropy ($ seed);
$ accountPriv = $ root-> derivePath ("{$ purpose} & # 39; / 0 & # 39; / 0 & # 39;"); // m / 44 & # 39; / 0 & # 39; / 0 & # 39; (change and regular addresses come from here)
echo "public key:". $ accountPriv-> toExtendedPublicKey (). PHP_EOL;
$ addressPriv = $ accountPriv-> derivePath ("0/0"); // // m / 44 & # 39; / 0 & # 39; / 0 & # 39; / 0/0 <- 0'th non-change address
$masterAddr = new PayToPubKeyHashAddress($accountPriv->getPublicKey () -> getPubKeyHash ());
Echo "Address:". $ masterAddr-> getAddress (). " n  n";

I have:

Public key: xpub6CXUv9gQCfQNNksArMAnKyu14vji7cK3V5wG7gYSoVntmtXTwzmhz4tFPzAiwagcdTYXXXjKCuFAQcUXM67k7cZL2sPbBEuZjp3Gdgw
Address: 15e5vNCSE9iBUbRW6kdHx84XFEcQgY4zfC

After importing the xpub key into Electrum, the receiving address is displayed: 144aHwG4JKtpCjpJLKcB7aZq8nEidrBaKN

p2sh – How to see if an address is multisig?

Once an address has been issued, the script used to create the P2SH address is displayed in the payout transaction. All addresses that contain the & # 39; m-of-n & # 39; information on this page are addresses that have been issued in the past.

Interestingly, @PieterWuille has just released a GDP for something called Taproot, which allows coins issued to multi-sig-addresses to be issued as it is Not Sacrifice privacy by revealing the complete blocking script in the withdrawal transaction. Using the Taproot design (which also requires Schnorr signatures), if the keyholders spend the money in a manner they agreed during the creation of the script (address), the data published in the blockchain will be displayed like a normal P2PKH output, instead of a P2SH output. This is good for privacy, but it can also save on transaction fees, as more complicated and data-intensive scripts can be used without paying more than a standard P2PKH transaction. ).

So when Schnorr and Taproot are adopted, it's harder to achieve such values.

Blockchain – Can the payment from a P2SH address change?

In the gavinandresen Multisig test, the change was redirected to the original test 3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC P2SH address for multisig payments?

In Mastering Bitcoin by Andreas M. Antonopoulos, Chapter 5. Transactions, the following statement is made and I would like to know if my above question becomes invalid: "Note that you can not insert P2SH into a P2SH redemption script because the P2SH specification is not recursive."

Litecoin – P2SH to LTC address with M

The reason you get a different address is because you attach the same checksum that you use with the 03 Prefix before the new one Prefix + payload, You have to double the new hash Prefix + payload and then attach the first four bytes and then encode them in base58.

When I decode your result MANDhrctLRAygo3dFckfWvEaWeQiti143C to hexadecimal I get 321b0e61ac671bde7fb52e2f3d6e2dc213609d9de5020cb31b, In this hexadecimal string is your prefix 32Payload is 1b0e61ac671bde7fb52e2f3d6e2dc213609d9de5and checksum is 020cb31b,

The checksum you would get after double hashing the new one Prefix + payload is e29f4248,