Bitcoin allows clients to run five standard transaction scripts , Respectively P2PKH、P2PK、P2SH、MS and OP_Return, Different scripts correspond to different features and uses .
< Bitcoin transaction script stack based execution language >
1. P2PKH（Pay to Public Key Hash）
This script is the trading script used by most exchanges in bitcoin network . The script contains a lock script , Lock in the trading output , That is, the public key and the corresponding public key hash （PKH）. The output locked by the script is unlocked by typing the public key and the digital signature created by the corresponding private key .
If Bob to Alice Transfer accounts 0.1 BTC,Bob An output script is created in the transaction ：
OP_DUP OP_HASH160 < Alice Public Key Hash > OP_EQUAL OP_CHECKSIG
The script needs to provide the signature and public key for the output transaction unlocking condition , Because the signature is generated by the user's private key , So only Alice The ability to create a verification signature to unlock the script .
Alice Spend the deal 0.1 BTC when , Provide the corresponding unlock script ：
< Alice Signature > < Alice Public Key >
Script merge , Unlock script first , Lock script after ：
< Alice Signature > < Alice Public Key > OP_DUP OP_HASH160 < Alice Public Key Hash > OP_EQUAL OP_CHECKSIG
This script evaluates against an inverse Polish expression
Script execution steps
- Alice Signature, Alice Public Key Push to stack ;
- OP_DUP Copy the top of the stack elements and push them into the top of the stack ;
- OP_HASH160 Calculate the hash value of the top of the stack element , And push the results to the top of the stack ;
- Alice Public Key Hash The operands are pushed directly to the top of the stack ;
- OP_EQUAL Judge two operands , To be equal is to pass ;
- OP_CHECKSIG Check the public key and signature , The confirmation signature is generated by the private key corresponding to the public key , After execution, the result is pushed into the stack .
Only if the unlock script matches the lock script , The execution result of the combined script will be True , That is, the unlock script provides Alice A valid signature for .
2. P2PK（Pay to Public Key）
This mode is a relatively simple transaction script mode . be relative to P2PKH , The public key is not hashed , So it's possible to disclose the user's public key .Coinbase This mode is often used in the transaction of .
The locking script takes the form of ：
< Public Key > OP_CHECKSIG
The unlock script contains only one operand , That is, the user's signature
< Signature of A >
The composition script takes the form of ：
< Signature of A > < Public Key > OP_CHECKSIG
call OP_CHECKSIG The operator , Yes, through the user A The signature and A Verify the public key corresponding to the private key , If the result is true, it will pass the verification .
3. P2SH（Pay to Script Hash）
The script only records 20 Script hash of bytes , So as to protect the specific script details . You need to use the UTXO when , Show the original script corresponding to the hash value , And make sure that the result of the script is true .
The locking script takes the form of ：
HASH160 PUSHDATA ( Target script hash ) EQUAL
Provide the corresponding target script when unlocking .
4. Multisignature （Multi-Signature）
Multi signature provides unlocking scenarios ： In the relevant N Public key , Need to provide M A signature corresponding to a public key , Only then can the corresponding UTXO To unlock . This script is more effective in scenarios involving multi-party negotiation transactions . among N Is the total number of public keys ,M For at least the number of signatures provided .
The multi signature locking script takes the form of ：
M < Public Key 1 > < Public Key 2 > ... < Public Key N > N OP_CHECKMULTISIG
The unlocking script takes the form of ：
OP_0 < Signature k > < Signature j > ...
Lock scripts and unlock scripts are combined , You can verify the provided signature , Achieve the purpose of multi signature locking .
5. Data record output （OP_Return）
The data record output script is mainly used to expand the functions of bitcoin . Developers can add 80 Bytes of non trading data information .
stay P2PKH(pay to pubKey hash) In the transaction , Can be output Replace the validation script in with other data . Doing so will result in the bitcoin in the deal being no longer spent , Because it's hard to find a calling script that meets the conditions ( Need to reverse hash ). The disadvantage is that , An unfunded transaction (UTXO) Will be stored in memory , So this kind of transaction takes up a lot of memory space , Affect the efficiency of bitcoin networks .
therefore , We can use bitcoin OP_RETURN Instructions . The transaction that contains this order is also not expendable , But nodes can safely move it out UTXO aggregate , The deal can be made in OP_RETURN Store a certain number of bytes in .
Reference material ：
《 Blockchain technology and application 》—— Huawei blockchain technology development team