Solidity - EIP-712 Typed Structured Data Hashing and Signing
This article explains the EIP-712 standard and how to use Solidity to implement EIP-712 in smart contract. IntroductionIn the previous article, we introduced some methods of signing and verification. In practice, we may sign many data. For example: 123456789function permit(address owner, address spender, uint value, uint nonce, uint deadline, uint8 v, bytes32 r, bytes32 s) external { require(deadline >= block.timestamp, "signature expired"); require(!usedNonces[nonce], " ...
Solidity - Signature Malleability
The previous article introduced the usage of ecrecover, but the built-in ecrecover has a signature malleability problem. This article will explain this problem and how to solve it. ProblemExamples from the previous article: 1234567function test() external view returns(address) { bytes32 hash = 0xc1af4b94166cd32fc49b7b926cbb91ee421de2d04450e8ae57857b9b56ac7e53; uint8 v = 0x1b; bytes32 r = 0xe1077fb9321c187d8a43926896abac5455ce6add269e098f855ff059d6b846a3; bytes32 s = 0x56320be5f6d79c4d0e ...
Solidity - ecrecover
IntroductionThe ecrecover function in the smart contract can be used to verify the signature of the wallet. The verifier can confirm the identity of the signer without the signer’s private key. It is often used for off-chain authorization, and then used on-chain by a third party or for simple identity verification. Function1function ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) hash: The hash of the signed message. v: v value of signature. r: r value of signature. s: ...
Solidity - Custom Error
This article introduces the usage of custom error in Solidity, including ways to define errors, usage, and error handling. UsageThis feature was added after Solidity v0.8.4, which can use a custom error to return the error reason. In the past, we usually used the following methods to throw errors: 1234567require(shouldPass, "MyError");// orif (!shouldPass) { revert("MyError");} Custom error is used as follows: 123456789101112// Define custom errorerror MyError();co ...
Solidity Gas Optimizations - Variable Ordering
DescriptionYou may not know that the order of declaration of variables will also affect the consumption of Gas.Since the EVM operations are all performed in units of 32 bytes, the compiler will attempt to package the variables into a 32 bytes set for access, so as to reduce access times.However, the compiler is not smart enough to automatically optimize the grouping of variables. It will group the statically-sized variables in groups of 32 bytes. For example: 12345678910111213contract MyContract ...
Solidity Gas Optimizations - Data Compression
In the process of developing smart contract, because we want to be able to achieve matching function, we have implemented a function that can input multiple orders. As more and more features are added to our function and more and more arguments are required to ensure contractual fairness, such as miners’ fees, taker’s fee, maker’s fee and payment method of fee. We encounter to the variable too much and can not compile, and a lot of data consumed gas has also become a problem. In our matching fun ...
Solidity Gas Optimizations - Function Name
DescriptionYou may not think that the name of the function will also have an impact on gas consumption. In fact, in the worst case, there may be more than a thousand of gas impacts. Let’s take a look at the following code: 1234contract Test { function b() public { }} The above execution b() will consume 125 gas, then change to the following: 1234567contract Test { function a() public { } function b() public { }} This time b() becomes 147 ga ...
Solidity Gas Optimizations
A smart contract development business have its costs come from the transactions of smart contracts; the gas consumption has a direct impact on the company’s costs structure. Writing contracts in an energy-saving way are essential. This article will analyze some of the factors that affect gas consumption in smart contracts. Outlining what is the most expensive instruction and uses it with caution. Gas consumption can refer to the following two tables: Table 1 Table 2 The gas used in Table 1 is ...