The development process and compilation environment of the digital currency wallet with isolated authentication

Source: Internet
Author: User
Tags hash serialization

Blockchain Enthusiast (qq:53016353)

Digital currency programming

There are 2 ways to implement digital currencies:


Native Digital currency script


The native digital currency script is defined as a scriptpubkey with a 1-byte push instruction (Op_0, op_1, ..., op_16) followed by 2 to 32 bytes of data.


Nested in P2sh


A digital currency script nested in P2sh is a redeemscript that holds a digital currency script and is also a scriptpubkey with a 1-byte push instruction (Op_0, op_1, ..., op_16) followed by 2 to 32 bytes of data.


Transaction serialization


If a transaction does not contain digital currency data, the serialization format uses the previous format.


If a transaction contains digital currency data, a new serialization format must be used:


[Nversion] [Marker] [Flag] [Txins] [Txouts] [Witness] [Nlocktime]


These fields include Nversion, Txins, Txouts, and Nlocktime, and the previous definitions are consistent.


The marker field must be 0x00.


The flag must be a 1-byte non-0 value. The current must be 0x01.


Witness is the serialization of all witness data for a transaction. Each txin is associated to a witness field. The witness field starts with Var_int, and the Var_int value represents the number of stacks that the txin needs to occupy, followed by the stack's member values. Each stack member begins with Var_int. Witness data is not a script or a 520-byte compression stack limit.


A non-witness txin must be associated with an empty witness field, represented as 0x00. If all txin do not have a witness program, then the transaction must be serialized using the old format (exception: Coinbase transaction).


Transaction ID
Transaction ID


2 IDs per transaction


Txid meaning unchanged, or 2 times SHA256 hash value of the transaction serialized data


A new WTXID is defined as a 2-SHA256 hash value that contains the serialized data of the new witness data. If a transaction does not have any witness data, then Wtxid equals Txid.


Txid still represents the ID of the transaction, especially the output that is also used to point to the previous trade in Txin.


Standard script Types
Standard script Type


Pay-to-public-key-hash (P2PKH)


P2pkh is the most commonly used template scriptpubkey is defined by Satoshi, allowing simple payment to a single public key. The format is:


Scriptpubkey (bytes): Op_dup op_hash160 < 20-byte-pubkey-hash > op_equalverify op_checksig


The output of P2pkh is spent, the scriptsig format is


Scriptsig: < sig > < PubKey >


RIPEMD160 (SHA256 (PubKey)) equals 20-byte-pubkey-hash in Scriptpubkey.


Pay-to-script-hash (P2SH)


P2SH is defined in BIP16. It allows payment to any complex script with a modified length of scriptpubkey. Format:


Scriptpubkey (bytes): op_hash160 <20-byte-script-hash> op_equal


The output of P2sh is spent, the scriptsig format is


Scriptsig: <...> <...> <...> < redeemscript >


RIPEMD160 (SHA256 (redeemscript)) equals 20-byte-script-hash in Scriptpubkey. The Redeemscript is deserialized and is treated as the remainder of the data in Scriptsig.


Pay-to-witness-public-key-hash (P2WPKH)


P2wpkh is a new definition of BIP141. Like P2pkh, it allows simple payment to a public key in the form of:


Scriptpubkey (bytes): Op_0 < 20-byte-pubkey-hash >


To spend the P2WPKH output, the scriptsig must be empty, and witness is


Witness: < sig > < PubKey >


RIPEMD160 (SHA256 (PubKey)) equals 20-byte-pubkey-hash in Scriptpubkey.


P2wpkh in P2sh (P2SH-P2WPKH)


P2sh-p2wpkh is a p2sh that uses P2wpkh scripts as redeemscript. The P2sh-p2wpkh Scriptpubkey looks the same as the usual P2SH:


Scriptpubkey (bytes): op_hash160 < 20-byte-script-hash > op_equal


To spend P2SH-P2WPKH output, scriptsig must contain only one redeemscript, and witness is the same as P2wpkh:


Scriptsig (bytes): < Op_0 < 20-byte-pubkey-hash > >
Witness: < sig > < PubKey >


RIPEMD160 (SHA256 (PubKey)) equals 20-byte-pubkey-hash, and RIPEMD160 (0x0014{20-byte-pubkey-hash}) equals 20-byte-script-hash.


Pay-to-witness-script-hash (P2WSH)


P2sh-p2wpkh is another new script format defined by BIP141, similar to P2sh. It allows payment to any complex script. The format is:


Scriptpubkey (bytes): Op_0 < 32-byte-script-hash >


To spend the p2wsh output, the scriptsig must be empty, and witness is


Witness: <...> <...> <...> < witnessscript >


RIPEMD160 (SHA256 (witnessscript)) equals 32-byte-script-hash in Scriptpubkey, Witnessscript is deserialized and is treated as the remaining data in witness.


P2wsh in P2sh (P2SH-P2WSH)


P2sh-p2wpkh is a redeemscript that uses a p2wsh script as a p2sh. P2sh-p2wpkh's scriptpubkey look the same as P2sh:


Scriptpubkey (bytes): op_hash160 <20-byte-script-hash> op_equal


To spend P2SH-P2WPKH output, scriptsig must contain only one redeemscript, and witness is the same as p2wsh:


Scriptsig (bytes): < Op_0 < 32-byte-script-hash > > \
Witness: <...> <...> <...> < witnessscript >


SHA256 (witnessscript) equals 32-byte-script-hash, and RIPEMD160 (0x0020{32-byte-pubkey-hash}) equals 20-byte-script-hash.


A new Signature algorithm


The output of a witness program is spent, and a new signature algorithm must be used when generating the ECDSA signature, a detailed example can be found in BIP143.


New Payment Address


2 new payment address types are defined. The complete specification can be found in the BIP142.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.