Time：2020-11-24

In the bitcoin white paper, there are three key words that explain what transactions are.

1. Although digital signatures partly solve the problem of e-cash system, it still needs the support of the third party to prevent double payment, then the system will lose its value of existence.
2. An electronic currency is defined as a chain of digital signatures. E-money is transferred from the owner to the next person. The owner uses the private key to digitally sign transactions and the public key of the next person, and the digital signature is attached to the back of the electronic currency (digital signature chain). The receiver can verify whether he has received the electronic currency by checking the digital signature.
3. At the end of each transaction, the e-money will be recovered by the mint, and the mint will issue a new e-money; only the e-money directly issued by the mint will be counted as effective, which can prevent double spend.

The first paragraph makes it clear that the soil of electronic currency transaction is digital signature.

In the second paragraph, it explains clearly what electronic currency in bitcoin is and how to trade it.

In the third paragraph, the process of creating and destroying bitcoin e-money is explained clearly to ensure the uniqueness of e-money.

## digital signature

In cryptography, I like to use Alice and Bob as examples. Alice stands for passer-by A and Bob stands for passer-by B. It will be used frequently later.

In the real world, the transaction is based on the signature in written form. For example, after Alice signs a check “100 yuan from Alice to Bob”, she sends the signed check to Bob through the secretary. Bob finds an expert to verify the signature and confirms the transaction. On the Internet, transactions are based on digital signatures.

Digital signatures are based on asymmetric encryption. Let’s take a look at Wikipedia’s definition of asymmetric encryption

Asymmetric cryptography is an algorithm of cryptography. It needs two keys, one is [public key] (), the other is [private key] (); one is used for encryption, the other is used for decryption. The ciphertext obtained by encrypting plaintext with one key can only be decrypted by the corresponding other key; even the key originally used for encryption cannot be used for decryption.

Digital signature is an application of asymmetric encryption. Mathematically, it can be expressed by a simple formula. The general flow of the whole digital signature is as follows:

``````hash:    x = hash(data)
send:    c(x) and data
------- network -------
verify:  d(c(x)) = x = hash(data)``````
1. Alice needs to transfer 100 yuan e-money to Bob. Bob needs a way to verify that the transaction is from Alice and that Alice can’t deny it.
2. Alice writes transaction information data: Alice gives Bob 100 yuan of electronic currency.
3. Alice uses the hash algorithm to generate the hash value x = hash (data) of the transaction information.
4. Alice generates a pair of secret keys through random number generator, one of which is the public key D and the other is the private key C.
5. Alice uses the private key C to sign the transaction information X and gets the digital signature C (x).
6. Alice uses the Internet to pass the digital signature C (x) and transaction information data to Bob.
7. Bob uses the public key d to decrypt the digital signature D (C (x)) to get the transaction information hash value X.
8. Bob uses the public key d to decrypt the hash value x of the value of the transaction information of the digital signature C (x).
9. Bob uses hash algorithm to generate hash (data) of transaction information.
10. If the hash value hash (data) of the transaction information generated by Bob is equal to the hash value x of the transaction information generated by Alice, then Alice has the private key; otherwise, Alice has no private key.
11. If Alice denies it, anyone can repeat Bob’s verification steps and confirm the transaction data: Alice gives Bob 100 yuan of electronic currency.

## electronic money

So how to define the 100 yuan electronic currency? Back to the bitcoin white paper.

``An electronic currency is defined as a chain of digital signatures. E-money is transferred from the owner to the next person. The owner uses the private key to digitally sign transactions and the public key of the next person, and the digital signature is attached to the back of the electronic currency (digital signature chain). The receiver can verify whether he has received the electronic currency by checking the digital signature.``

In the bitcoin source code, the electronic currency is defined as a digital signature chain. As long as Alice signs after the e-money, it is equivalent to that the e-money belongs to Bob. This process is based on the digital signature mentioned above.

So far, the overall idea of bitcoin trading has been basically clarified. There are many details to go deep into the source code level, such as

• How to divide e-money? For example, Alice gives Bob 100 yuan and Bob gives Alice 90 yuan.
• How to integrate e-money? For example, if you want to change an electronic currency into an electronic currency of 50 yuan, you can exchange it for two yuan.
• How to count the existing effective e-money? For example, how much e-money does Alice and Bob have.
• How to create and destroy electronic currency? For example, after Alice pays Bob 100 yuan, Alice’s e-money will be destroyed, and Bob’s e-money needs to be created.

## Build HTTP service with C + + Mongoose

Mongoose source code address:https://github.com/cesanta/mo… Mongoose user manual:https://www.cesanta.com/devel… Mngoose set up HTTP service #include <string> #include “mongoose.h” using namespace std; static const char *s_http_port = “8000”; static void ev_handler(mg_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; if (ev == MG_EV_HTTP_REQUEST) { std::string uri; if (hm->uri.p && hm->uri.p[0] == ‘/’) […]