Popular science: creating and signing Ethereum transactions

Popular science: creating and signing Ethereum transactions

In our previous article, we have explained the role of creating a private key and how to create a private key. Specifically, in the first part, we learned that the key is a large random number, nothing else; and in the second part, we learned how to use a large random number to create an asset that can hold Ethereum wallet. In this last part, we are going to learn how to use such random numbers to sign and manipulate your own virtual assets.

Transaction-the old goes out, the new comes in

Blockchain transactions are not particularly different from bank transactions. Nowadays, most banks support you to use digital means to complete most of the operations: transfer money to others, deposit money in your own savings account, or even borrow money, and so on. Internet technology allows us to complete most operations without having to be there in person.

Although these operations are easy for you, they are neither easy nor cheap for the bank. Behind the scenes, there are multiple third parties involved in the settlement, verification, and confirmation of your bank operations. If these are not enough, in order to comply with banking regulations, financial institutions must also perform cumbersome measures to determine that you (the person who initiated the operation) is the person who has the right to operate your account. All these levels will incur heavy costs, which is why gateways like Visa and Mastercard charge a fee every time they provide a service, usually based on the amount of money transferred. And, of course, any operation you initiate at any time can be frozen or even revoked.

Popular science: creating and signing Ethereum transactions

In 2017, the European Union required financial institutions to implement Payment Services Directive 2 (Directive 2015/2366), which means that banks must implement security regulations including SCA (Strong Identity Verification). Due to the cost of PSD2+SCA, only 59% of European banks successfully met the regulatory requirements in March 2019, delaying the ultimatum by another year. –

The rules of blockchain transactions are different

Because of the distributed and permission-free nature of the public chain, anyone can sign and broadcast transactions in the network. Based on the characteristics of the blockchain, you need to allocate a part of the funds as a handling fee so that your transaction can be “mined” (that is, packaged on the blockchain by a miner), but the level of the handling fee is Looking at the user’s demand for the use of the blockchain has nothing to do with the amount of transferred value. For example, using an Ethereum account to transfer 1 USD to another account, and transfer 1 million USD, the fee to be paid is the same. As long as the fees are the same, the two transactions will be treated equally by the miners, and will be packaged into a valid block and spread to the entire network.

Popular science: creating and signing Ethereum transactions

A block contains a series of transactions, and the block is one after another, only increasing without decreasing. Because part of the data of the new block comes from the previous block, these “blocks” will form an ordered chain one after another. This is the origin of the name “blockchain”. The blockchain also uses a data structure that is easy to process and verify, called a “merkel tree”, which is part of the reason why it is almost impossible to forge a certain transaction or a certain block on the blockchain— — It is too easy to identify. –

In addition, blockchain transactions do not require verification by any authoritative group. To send a valid transaction, you only need to use the private key corresponding to the digital signature algorithm (DSA) of the blockchain system to generate a signature for the transaction. The Ethereum and Bitcoin blockchains use the ECDSA algorithm, while Cardano and Polkadot use the EdDSA algorithm. Both of these algorithms rely on elliptic curve cryptography, but the latter uses a twisted Edwards curve, which is an improvement to universal digital signatures. Any account can use a private key signature to initiate a transaction, but only transactions initiated by addresses with sufficient balance can be successfully executed.

Popular science: creating and signing Ethereum transactions

The elliptic curve signature algorithm is based on the “discrete logarithm problem”. Its classic form is that given an integer k such that a^k ≡ b (mod p), where p is a prime number, find k. Unlike other public-key cryptography algorithms such as RSA, it is only based on the factorization problem of large exponents (this has recently become the target of lattice-based cryptography). Today, there is no efficient way to calculate k (it can be described as given points P and Q on the elliptic curve). This is also the reason that although the curves and signature algorithms used by different blockchains are different, they are all based on elliptic curves. –

Once a transaction is signed, sent to the network along with the signature, and packaged in a successfully mined block, the transaction can no longer be revoked. Unlike bank operations, a successfully packaged and chained transaction cannot be undone, nor can it be restored to the state where the transaction has not been executed . Most public chains make the content of transactions completely visible, so the blockchain used by these exchanges is the ultimate source of truth for these assets.

The structure of Ethereum transactions

Now that we have fully understood the characteristics of blockchain transactions, we are ready to create an Ethereum transaction. We start with a simple transfer transaction: transfer 0.1 ETH to an address  0x17A98d2b11Dfb784e63337d2170e21cf5DD04631. The transaction can be expressed in JavaScript Object Notation (JSON) format. When creating this transaction, we can see this information on MyEtherWallet (used after logging in  send offline):


(Editor’s note: The biggest purpose of the MyEtherWallet website is to help you generate an Ethereum account, but using it directly to send transactions may require you to expose your private key or mnemonic, which is a very insecure practice and is highly not recommended. .If you want to use MyEtherWallet, please use their browser plug-in or mobile wallet instead of using their website directly.)

Here I would immediately jump out of several noncevalues: gasLimitgasPrice, , data and  chainId. None of them are related to the content of the transaction, but they are all related to the way the transaction is executed. This is because, in order to send a transaction on Ethereum, you have to define some additional parameters in advance to tell the miners how to process your transaction. Both parameters of the transaction are related to “gas”. Gas itself is a unit used to measure the computational burden of a transaction, as a basis for payment to Ethereum miners. One parameter is  gasPricethe unit price that the user is willing to pay for each unit of gas, in Gwei (Gwei is 1/10^9 of ETH); the other is  gasLimitthe maximum amount of gas that the user can consume for his transaction. These parameters can be used by the Ethereum node to estimate a reasonable value. Generally speaking, the wallet service provider will fill in for you.

Popular science: creating and signing Ethereum transactions

-The value in Ethereum is usually in wei. Wei is the smallest unit of value on the Ethereum blockchain, which is 1/10^18 of ETH. Gas Price is usually in giga wei (abbreviated as Gwei) as the unit. Gas Price is a complicated issue on the Ethereum network because it often floats. EIP-1559 is a recently approved network change that may be launched at the end of this year, which may ease the dramatic fluctuations in Gas Price. In addition to wei and Gwei, there are other units used to express value. To learn about these units, please visit eth-converter.com-

(Editor’s note: The author’s article was published in April 2021. It has been confirmed that the core developers of Ethereum have decided to include 1559 in the “London” upgrade in August 2021. The 1559 changes to the user experience are reflected in the fact that users are no longer direct Specify Gas Price. Instead, specify two values: MaxFeePerGas and MaxPriorityFeePerGas. The former indicates the maximum price you are willing to pay for a unit of Gas, while the latter indicates the maximum price you are willing to pay to miners for a unit of Gas; in fact, you pay for the gas. The unit price is min (MaxFeePerGas, MaxPriorityFeePerGas + BaseFee) (the smaller of the two) and BaseFee is a value that automatically fluctuates with the full load rate of the previous block of the network. The unit price obtained by the miner is your Gas unit price Subtract BaseFee; and the BaseFee part will be destroyed. It is worth noting that in the foreseeable future, users can still use the traditional transaction format to send transactions, but this format may eventually be abandoned at the protocol layer, that is, The format of Ethereum transactions explained in this article may one day be out of date.)

In addition to the gas parameter, you also need to specify on which network the transaction will be executed. The Ethereum network includes not only the mainnet with chainId 1, but also other testnets. You can also send transactions on the testnet without losing any money, because the ETH on the testnet can be obtained for free. Generally speaking, if you want to deploy a dApp, the first step should be to debug it on the local network, then to deploy to the testnet, and the last step to deploy to the mainnet.

Finally, and very importantly, we have  data and  nonce . If you want to add additional data to the transaction, you need to use  data fields. When you interact with the smart contract, the data field can contain the instructions you give to the contract. And  nonce (“number used only once”) is a value used by the Ethereum network to track your account status and avoid multiple payment and replay attacks (can be understood as a serial number). When your transaction cannot be packaged temporarily because the gas fee is too low, you can use the same nonce but specify a higher gas price to “replace” your stuck transaction; once the transaction is “faster” The transaction can be packaged on the chain, and the transaction you originally signed (because the same nonce is used) will be discarded by the Ethereum node.

Sign an Ethereum transaction

After forming the JSON encoding mentioned above, we can go down and start the signing process. As we mentioned, the ECDSA algorithm is used in this process. Here we need to use a commonly used library ethers.js, which has already encapsulated the elliptic curve required by the ECDSA algorithm to generate the signature on the secp256k1 curve.


Sign now and send it later

The signature process mentioned above can be called “offline signature”. Because we create the signature first, and then find a way to broadcast it. But many online wallets combine signatures and broadcasts (such as Metamask and Portis), and broadcast them immediately after signing. However, offline signatures are very useful for many applications (such as state channels). The so-called state channel is a smart contract that monitors the accounts of the two, and completes the settlement of funds between the two accounts based on the signature transactions submitted by the two accounts. Offline signatures are also a common practice on decentralized exchanges: buy orders and sell orders are stored off-chain, and only when the match is successful will the two transactions be settled on the chain. It also plays an important role in Layer 2 solutions such as zkRollup and Optimistic Rollup.

With Portis, you can interact with the Gas Station Network (GSN) using signed transactions. Portis has subscribed to a group of relayers in GSN, and they can help you pay gas fees for your transactions. These relayers are associated with a decentralized contract (such as this one on the Ropsten testnet), and Portis can request them to relay your transaction. You still need to sign your transaction, but Portis will do everything else for you, so that even if you use a brand new wallet and don’t have a balance to pay for gas, you can still interact with the smart contract. Try it on our cryptopuppers app! If you want to know more, here is the specification of GSN (EIP-1613). Also, you can see a demonstration of the linkage between the TabooKey team and Portis’ beloved cryptopuppers application.

After talking about the signing process, our series on private keys is about to say goodbye to everyone. If you don’t fall for one period, you should be able to understand how the Ethereum account came from, and what the Ethereum transaction is all about.

Posted by:CoinYuppie,Reprinted with attribution to:https://coinyuppie.com/popular-science-creating-and-signing-ethereum-transactions/
Coinyuppie is an open information publishing platform, all information provided is not related to the views and positions of coinyuppie, and does not constitute any investment and financial advice. Users are expected to carefully screen and prevent risks.

Like (0)
Donate Buy me a coffee Buy me a coffee
Previous 2021-07-26 12:47
Next 2021-07-26 12:48

Related articles