This article uses the Bitcoin test network (bitcoin-testnet) as a development test environment, analyzes the data structure of Bitcoin transactions (Transaction), and uses Node.js as an example to illustrate how to organize specific transaction data by yourself, sign it, and broadcast it, which will eventually be confirmed and effective by the miner node.
Transaction is the information carrier and the smallest unit of the Bitcoin system. Block is to pack several such basic units of "transaction" into boxes and affix seals. Then, these blocks are connected in series according to a certain mechanism and sequence to form a blockchain. For application development based on Bitcoin blockchain, "Transaction" is the most directly used and most critical data structure. In addition to "Transaction", you also need to master the meaning of some basic terms related to Bitcoin block volume, including wallet private key, public key and address, block, blockchain, etc. There are many books and online materials that have relatively clear descriptions. Here I recommend you to read this online book "Mastering Bitcoin" (http://zhibimo.com/read/wang-miao/mastering-bitcoin/index.html). Here we focus on an in-depth analysis and understanding of the data structure of "Transaction". A Bitcoin transaction is a data structure containing input and output values, which contains code information for transferring funds from an initial point (input value) to a target address (output value). A Bitcoin transaction contains some fields, as shown in the following table. Size Field Name Data Type Description Note: The locktime field defines the earliest transaction time that can be added to the blockchain. In most transactions, it is set to 0 by default, which is used to indicate immediate execution. If the locktime is greater than 0 and less than 500 million, it is treated as a block height, meaning that the transaction is not included in the blockchain before this specified block height. If the locktime is greater than 500 million, it is treated as a Unix epoch timestamp (the number of seconds since January 1, 1970), and the transaction will not be included in the blockchain before this specified point in time. The use of locktime is equivalent to delaying the validity time of a paper check. Application development based on blockchain technology is actually mainly about working on the output data structure of transactions to carry specific business logic. For example, the ODIN open source project embeds identification attribute data into Bitcoin's multiple transaction output data blocks in a certain format. 2. Example Analysis of Transaction Records The following is an example of the raw data of a Bitcoin transaction (the raw binary data is output as hexadecimal ASCII code by byte for easy analysis): 0100000002eb2121e4e727bdb28525e79d39a90bd711b9e8413c054b29ffc4bb4775e69f82010000006b483045022100df82 The above message can be decomposed into fields according to the protocol rules as follows: 01000000 // Version number, UINT32 02 // Number of Tx inputs, variable length INT. 0×02 = 2 inputs. /*** Next is the first group of Input Tx ***/ 3045022100df82cf6c95b4eb64e4e9cee3af88a94c65fa81650e824d515f089192b7e3c09c022011 21 // Corresponding public key data length, 0×21 = 33 bytes /*** The second group of Input Tx. Same as above, omitting the decomposition***/ c9f3b07ebfca68fd1a6339d0808fbb013c90c6095fc93901ea77410103489ab7010000008a473044 03 // Tx output transaction quantity, variable length INT type. 0×03=3 inputs. /*** The first group of output ***/ /*** The second group of output ***/ 00000000 // Lock time, UINT32, fixed 4 bytes Through the above analysis, after understanding and mastering the common organizational format of Bitcoin transaction records, you can make full use of the data content blocks corresponding to the blue font part to embed some customized binary data content, and then develop and implement your own specific business logic. 3. Run the sample program The following sample program embeds a string of specific content into the remark data block of a Bitcoin transaction in a certain format, so that it can be stored on the Bitcoin blockchain. Before running the following sample program, please make sure that the Docker runtime environment of the Bitcoin test network (bitcoin-testnet) has been installed. If it has not been installed, you can refer to the instructions in the previous article "Bitcoin Blockchain Development from Beginner to Advanced Guide 2" to install it (http://www.8btc.com/ppkpub_blockchain_develope_lesson_2). Copy and save the following sample code to the test environment of the bitcoin test network (bitcoin-testnet) (save the file name as OpreturnTestnet.js), and enter the following command in the command line to run and see the running results: Note: Each time you run the test code, you need to enter "make generate BLOCKS=10" in the command line of the Docker runtime environment to simulate the generation of new block records so that the transaction records generated by the test code can be effectively confirmed. The source code of the sample program OpreturnTestnet.js is as follows: /********************* Starting point of sample code*************************/ |
>>: Research and practice of blockchain in various countries around the world
The face of a promiscuous man In ancient times, i...
Having good fortune can naturally make money more...
Women with round chins are good wives and mothers...
Wu Qu, which belongs to Yin Metal, is the sixth s...
The moles on the face will more or less affect ou...
Recently, Chia has become very popular in the min...
Is it good for a girl to have a mole on her back?...
People with protruding forehead If a person has a...
Dear Antminer users: In order to provide better a...
Wang He, executive vice president of PICC Propert...
Everyone has different moles on their body, but n...
Women with red lips and white teeth look very gen...
The body is the capital of revolution. Without a g...
In life, benevolence and righteousness are the mo...
In fact, information such as a woman's fortun...