Last week, I used my own WeChat public account [lawup2003] to create a blockchain engraving service, and it is free. Every user who follows the public account has three opportunities to permanently engrave a piece of content not exceeding 40 bytes on the Bitcoin blockchain. Once released, it immediately attracted many colleagues in and outside the industry. ( The server expires on June 22, and there is no plan to renew it. The engraving service will no longer be provided, but the content engraved on the blockchain before will exist permanently. All the engraving content during this period is in the OP_RETURN of the transaction at this address: https://blockchain.info/address/1MGP1ZNBkxpcgLf2zF6WK48Nd8owfkoEFU I am going to study new things such as Ethereum! In fact, you can also make an identical blockchain engraving service according to this article! )
Teacher Shentu Qingchun, who had taught us just the week before, commented after experiencing the project: "It's a very good idea! And strong execution ability!"
After the project was released in the blockchain WeChat discussion group, some group members liked it and gave red envelopes as rewards. One group member said: "Thank you for your hard work! It gave me an intuitive feeling of blockchain!"
Da Hongfei, CEO of Ant Financial, also believes that “this application is quite interesting and simplifies the threshold for using blockchain.” Of course, he also reminds that “at present, this is not suitable for commercial projects” because “it is too simple and has no barriers!”
Indeed, as a business tycoon, "Uncle Da" has a good vision. However, in the past few days, when I patiently explained the technical details to the group members one by one, I found that everyone's understanding of blockchain and technical ability are actually uneven. How to give guidance to blockchain technology enthusiasts, especially those who are just getting started but have no idea where to start when faced with a pile of dense documents, may be a small contribution that my small experimental project should make to China's blockchain industry. In particular, another classmate mentioned that he felt that using WeChat to connect to the blockchain greatly enhanced the ease of use and reduced the user's fear of unfamiliar things, so he planned to make innovative applications such as classroom attendance based on my project.
So, without further ado, let's dissect this little "sparrow"! Beginners, follow me step by step, and you can also set up an identical WeChat engraving service. Step 1: Learn to write in Bitcoin script Please note that, by default, those who read this article are beginners like the author, so the screenshots are taken on the Windows platform. As for those who use Linux or Mac, it proves that you are no longer a beginner or have already mastered a certain level of technical skills. Please switch by yourself (it doesn’t matter actually).
1. Download and install the Bitcoin Core client, run bitcoin-qt (a client with a UI) and synchronize all blocks (the current block size has reached 75G, so you need a hard drive that is large enough);
2. After the synchronization is completed, click [Request Payment] in the top menu of the client, then click [Request Payment], the system will create a Bitcoin wallet address for you, copy your Bitcoin wallet address;
3. You need to go to the exchange market to buy some Bitcoin (because mining is another new big problem for beginners, so just buy it directly. Of course, don’t buy too much. Buying 0.01 Bitcoin, which is about RMB 30, is enough for our project to use for a long time), and then withdraw the coins to the address you just copied, which is the address of your Bitcoin wallet;
4. After the withdrawn coins are received, you will see that your available balance on the client will no longer be 0. At this time, click [Help → Debug Window → Console] in the menu at the top of the client to open the place where you can enter script operations, and follow the steps below:
(1) Type the following and press Enter (1MGP1ZNBkxpcgLf2zF6WK48Nd8owfkoEFU is my Bitcoin wallet address, please replace it with your address when operating): listunspent 0 100 '["1MGP1ZNBkxpcgLf2zF6WK48Nd8owfkoEFU"]' Get the transaction ID (txid), as shown below: This step is to check the amount of money that has not been spent in this wallet. The amount is followed by the current amount of bitcoins. Of course, here we mainly want to get the transaction ID (txid), and also write down the number after vout in order to complete the following steps;
(2) Find an online website that can convert strings into hexadecimal (STR2HEX), and encode the words you want to write into the blockchain. For example, if I want to write the sentence "Code is Law" into the blockchain, the result after HEX is "436f6465206973204c6177". After a quick calculation, it does not exceed 40 characters, so you can continue (the place where we want to engrave the external data this time generally cannot exceed 40 characters, but there are also cases where this length limit can be exceeded, which is another story);
(3) Return to the Bitcoin client's [Console], type the following and press Enter: createrawtransaction '[{"txid":"70c66f61ab38482f4193ea78cee68a54685382b04a46d2ee68f6467ca392b1fb","vout":0}]' '{"1LUpUZ5bTTczJa1BGx5DBm6Mo6V8vnjPtz":0.0001,"1MGP1ZNBkxpcgLf2zF6WK48Nd8owfkoEFU":0.01378807,"data":"436f6465206973204c6177″}' This script is to create a transaction (we will use Bitcoin transactions to engrave the sentence we want to write to the blockchain as a transaction note on the blockchain): Notice:
① The transaction ID (txid) should be the one we got when we queried the unspent amount before;
②vout refers to the address number in your Bitcoin client wallet. Mine is 0, indicating the latest address. If you have done what I said before and have not created more addresses, it should also be 0;
③ The first address and amount at the end indicate the address and amount of the transaction receiving the payment. Because our purpose is to engrave, I only transfer a small amount (0.0001 bitcoins, which is already the "dust amount". If it is less, it may not be confirmed by the Bitcoin network) to my other Bitcoin wallet address (theoretically, you can fill in the same wallet address as the sending wallet, but in the client, it may be to avoid obvious "errors", so entering the script command in this way is not allowed, but when we use RPC to implement it later, I can directly transfer from the same account to myself; if you don’t have a second Bitcoin wallet, you can use the previous method to generate a receiving address and set that address as the receiving address in the script. However, it should be noted that the vout of your sending address will become 1, because the newly generated address will be the latest, and its vout will be 0);
④ The second address and amount are the change address and amount (please learn about the change address and the operating mechanism of Bitcoin transactions by yourself, which will not be discussed here). The change address and amount can be left unspecified. If not specified, the system will automatically generate a new address in your wallet for change, and the amount will be the amount after deducting the default handling fee (the default handling fee can be seen in the lower left corner of the client [Send] interface). The reason why Bitcoin needs to generate a new address for change is mainly to protect the privacy of the transferor, but it also brings inconvenience to novices. In the system, every time a transaction is sent without a specified change address, the system will use a change address, and after every 100 addresses are used, they will be collectively discarded, and 100 new addresses will be generated in the same batch. If you use one of the previous batch of addresses as the receiving address, the amount received after the new 100 addresses are generated will not be usable. Therefore, to avoid trouble, I suggest that it is best to set the change address to your own wallet address. Of course, this will face a new problem, how to fill in the amount behind the change address, the formula is: unspent amount - self-transferred amount - transaction fee, so here is: 0.01398807–0.0001–0.0001= 0.01378807;
⑤In the data field, fill in the string after the HEX of the sentence to be engraved into the blockchain; You get a long string, copy it, and use it next;
(4) Type the following and press Enter: Add the long string obtained in the previous step after signrawtransaction Here, we sign the transaction we created before (sign it with your unique Bitcoin wallet private key). The hex value we get is the result after signing. If complete is true, it means the signature is successful! Copy the hex value and we will use it later.
(5) Type the following and press Enter: sendrawtransaction plus the long string of signatures in hexadecimal format Here, the transaction is sent or broadcasted. The result is the transaction ID. Copy this transaction ID.
5. The broadcast execution of the transaction is completed instantly, and confirmation synchronization may take 10-30 minutes (RP problem, haha), but this does not affect some websites that provide blockchain information query to view the transaction. For example, we open: https://blockchain.info/tx/8d2933db168aaead7e654b4d76805c2de933584242f6c6d5bd1589e7d0517ae4 The latter part of the URL is the transaction ID we obtained earlier. Move to the bottom of the page. The OP_RETURN item is the external data we have engraved (some query websites do not display scripts by default. You have to click [Show Scripts] to see it): The screenshots are as follows: Step 2: Learn to cooperate with Bitcoin RPC and operate with bitcoin-cli
1. Configure Bitcoin PRC
(1) Turn off bitcoin-qt (be careful when turning it off, and wait a few seconds after turning it off to avoid unpredictable errors, which may damage the blockchain that has been synchronized with great difficulty and require resynchronization);
(2) Go to the C:\Users\username\AppData\Roaming\Bitcoin folder (this is the default configuration file folder after your Bitcoin installation, which is automatically generated after installing the Bitcoin program or running the Bitcoin program for the first time), create a new file bitcoin.conf, and add the following content to it (the brackets and the words in brackets are instructions, do not write them into the file): rpcuser=123456 (Please change 123456 to your RPC user name) rpcpassword=123456 (Please change 123456 to your RPC password) rpcport=8338 (RPC port, the default is 8332, but I may have installed other software under Windows that made port 8332 unusable, so I changed it to 8338) rpcallowip=127.0.0.1 (For security reasons, RPC only allows local connections) rpcconnect=127.0.0.1 (For security reasons, only connect to the local node) Please note that the above content is in plain text format, one paragraph per line, with no semicolon or other symbols at the end, and a line break directly.
(3) Open the Command Prompt In the terminal, first enter the deamon folder under the bitcoin installation directory, for example, mine is [C:\Bitcoin\daemon], enter bitcoind -server and press Enter Since we did not specify any other parameters, if there is no error, then bitcoind is running! Keep this [Command Prompt] window open and do not close it, because once you close it, bitcoind will also be closed.
(4) Open another [Command Prompt] Also in the terminal, enter the deamon folder under the bitcoin installation directory. For example, I entered cd c:\bitcoin\daemon Input: bitcoin-cli -rpcuser=123456 -rpcuserpassword=123456 getinfo Press Enter and the result is as follows: What is returned here is an overview of the account situation; If the test of bitcoin-cli is normal, then the operation is the same as before in the [Console] of bitcoin-qt. The only difference is two points: First, add bitcoin-cli in front; Second, pay attention to escaping quotes. All quotes in JSON must be escaped with backslashes. OK, let's try the createrawtransaction statement. Type the following and press Enter: bitcoin-cli createrawtransaction "[{\"txid\":\"8d2933db168aaead7e654b4d76805c2de933584242f6c6d5bd1589e7d0517ae4\",\"vout\":0}]" "{\"1LUpUZ5bTTczJa1BGx5DBm6Mo6V8vnjPtz\":0.0001}" The execution result was obtained successfully. Well, the rest is omitted, just change the place to enter the command, the execution result will be the same, just follow the previous steps! Step 3: Call RPC operation using PHP or Python or other languages
On the wiki (https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)), it is found that many languages can call bitcoin's RPC, so that you can complete the operation of engraving on the Bitcoin blockchain in your existing program. Of course, we don't have to reinvent the wheel, because there are too many details to pay attention to. We already know the basic principles, and now we have to pursue pragmatism (if you don't understand the basic principles, you won't know how to change some personalized places later, so the basic principles are also necessary). If you know php or python, you can stand on the shoulders of predecessors to implement the engraving function.
1. Find php-OP_RETURN on github (this is for php, if you use python, you can also find python-OP_RETURN, the author is coinspark, the usage is almost the same), deploy php-OP_RETURN to your php running directory (on windows, you can use phpstudy to build a php environment with one click);
2. Modify the OP_RETURN.php file and configure the rpc information:
3. Create a PHP file (for example, run.php) in the same directory and put the following code in it (also according to the readme): Then run the file http://127.0.0.1/run.php in the browser to get the transaction ID (the previous complicated process is completed so simply. If you don’t understand the principle, you will think it is very simple, haha). Just like before, take the transaction ID to websites such as blockchain.info to check, and you will see the content you engraved in OP_RETURN at the bottom. Note that I can use the same wallet address to send money to myself, and it succeeds. This means that there is a limitation only in bitcoin-qt, but it is actually possible to transfer money by itself at the underlying layer.
4. As mentioned before, a new batch of addresses will be automatically replaced after sending transactions 100 times. If you use the original php-OP_RETURN code, the change address will be automatically generated. When it reaches 100 times, the coins transferred from the original address will not be received (because the old address belongs to the 100 addresses that have been discarded, and the newly received coins will no longer be able to be used due to the lack of the key corresponding to this address. Although the number is small, it is also money. We don’t want to waste money meaninglessly). Therefore, we need to comment out the section that generates a new address in the original php-OP_RETURN code, and then change the change address in createrawtransaction to our own (the reason why the "online wallet" service can always use one address, regardless of whether it sends transactions more than 100 times, is the same as this), open the OP_RETURN.php file, and modify these two places: Of course, you can also have some other ideas, such as calculating the new address through RPC first, and then sending the amount minus the transaction fee to the new address to achieve 0 change, which is also an idea, but this may encounter problems: when the previous engraving transaction has not been confirmed, the next transaction may be regarded as "double spending" and fail, because the self-transferred coins have not been confirmed enough. Of course, this idea can be improved. The purpose of writing this paragraph is to express that if you think my method is not good, you can also open up your mind and build a better process so that the system can run automatically with less manual input.
5. Now run http://127.0.0.1/run.php to realize the self-transfer and self-change transaction. This kind of transaction is displayed on blockchain.info as follows:
6. Of course, the most important thing to add is that OP_RETURN is engraved on the Bitcoin blockchain through the PHP program (or Python or other programs), which makes it possible to engrave through the website web. Step 4: Connect the function to WeChat developer mode
We are mainly talking about bitcoin and blockchain here, so if you want to learn more about the various functions of the developer mode of WeChat official accounts, you can search [Fangbei Studio], where you can find everything about WeChat developer mode.
Register and log in to the backend of the WeChat public account, turn off the edit mode in [Auto Reply], then go to [Basic Configuration] to turn on the developer mode, create a PHP network file on your server according to the specifications of [Developer Tools → Developer Documentation], and put the URL in [Server Configuration]: Through this network file, it is possible to receive messages from WeChat users, determine their types, obtain their contents, and then automatically reply based on the type and content. This is the basic process of a WeChat user sending a message, and then the WeChat public account replies to the user with the engraved transaction address!
Here is a special consideration. My bitcoin is deployed and run on Alibaba Cloud, and the RPC configuration only allows 127.0.0.1 local calls, so I set up a PHP environment on this machine in Alibaba Cloud, and then use a special port to receive and feedback results; the PHP program running WeChat developer mode is actually on SAE Sina Cloud, and only the PHP program on SAE knows the host and port of the RPC that calls bitcoin. At the same time, a simple authentication is required for communication between the two. To put it bluntly, there is an extra layer in the middle. There are two purposes for doing this: one is to ensure that the operation of the bitcoin network and the network operation of the WeChat public account do not affect each other. For example, when bitcoin occupies more resources, it will not happen that the WeChat public account user has not responded to the content for a long time; of course, the second reason is actually the key point, that is, except for the SAE program, no one knows the host address of my bitcoin operation, which can better protect the security of my bitcoin (although not much, but it is also money)! Because I heard that hackers who steal bitcoins are very powerful, I am a beginner, and in addition to cold wallet backup, I have not mastered the security skills of bitcoin hot wallets! So I just had to try to hide myself as much as possible. If I didn't show my face, I wouldn't get beaten. This seems to be the "Dark Forest Law" in "The Three-Body Problem", haha~
So, let’s experience this WeChat work in action. Scan the QR code to follow [lawup2003] ( valid until June 22 ): By the way, I said at the beginning of the article that if you follow me step by step, you can also make an identical WeChat, so I will provide everything I can without reservation (even though I am actually a liberal arts student, my code is very bad, and the database settings are not very scientific, but I am not afraid to show my ugliness): I packaged the network files and MySQL database files bound to the WeChat developer mode and accessed them on Baidu Cloud. Friends who need them can download them by themselves. It should be noted that the WeChat configuration file and the place where it communicates with the Alibaba Cloud host must be modified by everyone according to their needs (I have noted "//modify" after each line that needs to be modified, you can quickly find it through search), because I believe that for students who have understood this article, it is not difficult to supplement these places.
That's it! The download address of WeChat developer mode PHP file and database file is: http://pan.baidu.com/s/1mhWlDsG
Finally, if you think the project is good, or have any ideas, questions or complaints, you can add my WeChat: Lawup2. Thank you~ |
On November 1, 2008, a mysterious person named Sa...
Some statements about moles can actually give peo...
Traditional physiognomy covers a wide range, among...
Our body and hair are from our parents. Although ...
In physiognomy, the mouth is related to a person&...
Now more and more people have noticed that their ...
Cryptocurrency trading is currently very popular ...
When it comes to personality issues, most people ...
For women, being unlucky in marriage and having c...
During this cycle, we witness so many changes or ...
Many analysts believe that the minimum price of B...
A person's personality traits can be observed...
When we watch TV or read novels, we often hear fo...
Getting married is the beginning of a woman's...
Side income is a gift in addition to the regular ...