White list verification method and device of smart contract, electronic equipment and storage medium
By using Merkle trees in smart contracts to build whitelist verification paths, the problems of high centralized trust costs, insufficient transparency and high storage costs of the whitelist mechanism in smart contracts are solved, achieving efficient and secure whitelist verification and reducing Reduce on-chain resource consumption.
Patent Information
- Application Number
- CN202410751163.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-12
- Publication Date
- 2024-09-27
- Estimated Expiration
- 2044-06-12
AI Technical Summary
The whitelist mechanism in smart contracts has the problems of high centralized trust costs, insufficient transparency, and high storage costs. Especially when the whitelist scale is large, it results in low system security and transparency, and high read and write costs.
A whitelist verification method that combines on-chain and off-chain is used to obtain the public key list, perform sorting and hash operations, build a Merkle tree, recursively determine sibling nodes and parent nodes, generate a verification path, and use the verification path to calculate step by step Combine the hash values and compare the final hash value with the root hash value. If they are equal, the verification passes, otherwise the verification fails, reducing the cost of centralized trust and optimizing storage and reading and writing costs.
It improves the security and transparency of the system, reduces the cost of centralized trust, reduces the consumption of resources on the chain, implements an efficient whitelist verification process, and ensures the non-tamperability and transparency of data.
Smart Images

Figure CN118710280A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a whitelist verification method, device, electronic device and storage medium for a smart contract. Background Art
[0002] With the development of blockchain technology, smart contracts have been widely used in finance, supply chain management and other fields. Smart contracts achieve transparency and immutability of transactions by automatically executing preset contract terms. In these application scenarios, it is usually necessary to limit specific users to execute certain business logic, which requires a whitelist mechanism to control user permissions.
[0003] At present, there are two main ways to implement the whitelist mechanism in smart contracts:
[0004] The first method uses a centralized database off-chain:
[0005] In this approach, whitelist information is stored in a centralized database off-chain. When a user attempts to execute a smart contract, the system queries the database to verify whether the user is on the whitelist. Centralized databases are at risk of replay attacks, where attackers can capture and repeatedly send legitimate requests, causing the system to repeat operations. The use of centralized databases reduces the transparency of the system because whitelist data is not publicly available on the blockchain and relies on a single trusted entity, increasing the cost of trust.
[0006] The second method uses the mapping storage structure of smart contracts:
[0007] In this way, the whitelist information is directly stored in the mapping structure of the smart contract. When the user executes the smart contract, the smart contract reads the mapping structure to verify the user's permissions. The storage and read-write costs of the mapping structure are high, especially when the whitelist size is large, which will lead to high consumption of blockchain resources and high transaction fees. Summary of the invention
[0008] In view of this, the embodiments of the present application provide a whitelist verification method, device, electronic device and storage medium for a smart contract to solve the problems of high centralized trust cost and high read and write cost, and low system security and transparency in the prior art.
[0009] In a first aspect of an embodiment of the present application, a whitelist verification method for a smart contract is provided, comprising: obtaining a public key whitelist to obtain a public key list; sorting the elements in the public key list, and processing the public key list according to the number of elements in the public key list to obtain an ordered public key list; performing a hash operation on each pair of public keys in the ordered public key list to generate a corresponding hash value, recursively performing a combined hash operation on the hash value generated in the previous layer until a root hash value is obtained, and storing the root hash value in the state of the smart contract; determining the sibling nodes and parent nodes of the public key in a binary tree, and recursively determining the sibling nodes in each level to construct a verification path; performing a hash operation on the public key, and calculating the combined hash value level by level using the verification path until a final hash value is obtained, comparing the final hash value with the root hash value, and if they are equal, the verification passes, otherwise the verification fails; in response to a user calling an operation of a smart contract, obtaining a verification path for the user's corresponding public key, and passing the verification path as a parameter, and calling the verification method before a business method that needs to verify the whitelist, and if the verification passes, continuing to execute the business logic, otherwise terminating the execution.
[0010] According to a second aspect of the embodiment of the present application, a whitelist verification device for a smart contract is provided, comprising: an acquisition module, configured to acquire a public key whitelist to obtain a public key list; a processing module, configured to sort the elements in the public key list, and process the public key list according to the number of elements in the public key list to obtain an ordered public key list; an operation module, configured to perform a hash operation on each pair of public keys in the ordered public key list to generate a corresponding hash value, and recursively perform a combined hash operation on the hash values generated in the previous layer until a root hash value is obtained, and the root hash value is stored in the state of the smart contract; a construction module, configured to determine the public key whitelist. The key is in the brother node and parent node of the binary tree, and the brother nodes in each level are recursively determined to construct a verification path; the comparison module is configured to perform hash operation on the public key, and use the verification path to calculate the combined hash value level by level until the final hash value is obtained, and the final hash value is compared with the root hash value. If they are equal, the verification passes, otherwise the verification fails; the verification module is configured to respond to the user's operation of calling the smart contract, obtain the verification path of the user's corresponding public key, and pass the verification path as a parameter, and call the verification method before the business method that needs to verify the whitelist. If the verification passes, the business logic continues to execute, otherwise the execution is terminated.
[0011] According to a third aspect of an embodiment of the present application, an electronic device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the steps of the above method are implemented when the processor executes the computer program.
[0012] According to a fourth aspect of an embodiment of the present application, a computer-readable storage medium is provided, which stores a computer program, and when the computer program is executed by a processor, the steps of the above method are implemented.
[0013] At least one of the above technical solutions adopted in the embodiments of the present application can achieve the following beneficial effects:
[0014] Obtain a public key list by obtaining a public key whitelist; sort the elements in the public key list, and process the public key list according to the number of elements in the public key list to obtain an ordered public key list; perform hash operations on each pair of public keys in the ordered public key list to generate corresponding hash values, recursively perform combined hash operations on the hash values generated in the previous layer until the root hash value is obtained, and store the root hash value in the state of the smart contract; determine the sibling nodes and parent nodes of the public key in the binary tree, and recursively determine the sibling nodes in each level to build a verification path; perform hash operations on the public key, and use the verification path to calculate the combined hash value step by step until the final hash value is obtained, and compare the final hash value with the root hash value. If they are equal, the verification passes, otherwise the verification fails; in response to the user calling the smart contract operation, obtain the verification path of the user's corresponding public key, and pass the verification path as a parameter, and call the verification method before the business method that needs to verify the whitelist. If the verification passes, continue to execute the business logic, otherwise terminate the execution. This application reduces the centralized trust cost and read and write cost, and improves the security and transparency of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0016] Figure 1 It is a flowchart of a whitelist verification method for a smart contract provided in an embodiment of the present application;
[0017] Figure 2 It is a schematic diagram of the construction process of a binary tree in an actual scenario provided by an embodiment of the present application;
[0018] Figure 3 It is a schematic diagram of the construction process of the verification path in the actual scenario provided by the embodiment of the present application;
[0019] Figure 4 It is a structural diagram of a whitelist verification device for a smart contract provided in an embodiment of the present application;
[0020] Figure 5It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0021] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application may also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present application.
[0022] Smart contracts are programs deployed on the blockchain that automatically execute contract terms and ensure the transparency and immutability of transactions. Smart contracts are widely used in finance, supply chain management and other fields. In these application scenarios, it is often necessary to restrict specific users from executing certain business logic, which requires the use of a whitelist mechanism to limit the user range.
[0023] At present, there are two main ways to implement the whitelist mechanism in smart contracts:
[0024] The first method uses a centralized database off-chain:
[0025] In this method, the whitelist information is stored in a centralized database off-chain. When a user tries to execute a smart contract, the system queries the centralized database to verify whether the user is on the whitelist.
[0026] The first method has the following disadvantages:
[0027] There is a risk of replay attacks, where an attacker can capture and resend legitimate requests, causing the system to perform duplicate operations.
[0028] The use of a centralized database reduces the transparency of the entire system as whitelist data is not publicly available on the blockchain.
[0029] The second method uses the mapping storage structure of smart contracts:
[0030] This method stores the whitelist information directly in the mapping structure of the smart contract.
[0031] Whenever a user executes a smart contract, the smart contract reads the mapping structure to verify the user's permissions.
[0032] The second method has the following disadvantages:
[0033] The reading and writing cost of the mapping structure is high, especially when the whitelist size is large, which will lead to a large amount of blockchain resource consumption and high transaction fees.
[0034] Therefore, the existing whitelist mechanism has the following technical problems:
[0035] 1. High centralized trust cost: The whitelist mechanism using a centralized database relies on a single trusted entity, which increases the trust cost and security risk of the system.
[0036] 2. Lack of transparency: The whitelist data of the centralized database is not public, which reduces the transparency and credibility of the system.
[0037] 3. High storage cost: Using the mapping storage structure of smart contracts will result in higher read and write costs, especially when the whitelist is large, which will increase the consumption of blockchain resources and transaction fees.
[0038] In view of the above-mentioned deficiencies in the prior art, this application proposes a whitelist verification method that combines on-chain and off-chain, aiming to reduce the centralized trust cost and the high read and write cost of the mapping storage structure, while improving the security and transparency of the system.
[0039] The contents of the technical solution of the present application are described in detail below in conjunction with the accompanying drawings and specific embodiments.
[0040] Figure 1 It is a flowchart of the whitelist verification method for the smart contract provided in the embodiment of the present application. Figure 1 The whitelist verification method of the smart contract can be executed by the application. Figure 1 As shown, the whitelist verification method of the smart contract may specifically include:
[0041] S101, obtaining a public key whitelist to obtain a public key list;
[0042] S102, sorting the elements in the public key list, and processing the public key list according to the number of elements in the public key list to obtain an ordered public key list;
[0043] S103, performing a hash operation on each pair of public keys in the ordered public key list to generate a corresponding hash value, recursively performing a combined hash operation on the hash values generated in the previous layer until a root hash value is obtained, and storing the root hash value in the state of the smart contract;
[0044] S104, determining the sibling nodes and the parent node of the public key in the binary tree, and recursively determining the sibling nodes in each level to construct a verification path;
[0045] S105, performing a hash operation on the public key, and using the verification path to calculate the combined hash value step by step until a final hash value is obtained, and comparing the final hash value with the root hash value. If they are equal, the verification passes, otherwise the verification fails;
[0046] S106, in response to the user's call to the smart contract, obtain the verification path of the user's corresponding public key, pass the verification path as a parameter, and call the verification method before the business method that needs to verify the whitelist. If the verification passes, continue to execute the business logic, otherwise terminate the execution.
[0047] In some embodiments, obtaining a public key whitelist to obtain a public key list includes:
[0048] Generate a public-private key pair using the elliptic curve algorithm, set the public-private key pair to be visible to the business, and organize and generate a public key whitelist based on the business requirements provided by the business party to obtain a public key list.
[0049] Specifically, the user uses the elliptic curve algorithm to generate a pair of public and private keys. In practical applications, the elliptic curve algorithm (such as SECP256k1) is a commonly used public key encryption algorithm with high security and efficiency. The length of the public key generated by the user is 256 bits, and the public key is set to be visible to the business, so that subsequent business operations can use the public key for verification.
[0050] Furthermore, according to the business requirements provided by the business party, a public key whitelist is sorted and generated. The business party will collect the public keys of users who have the right to execute smart contracts according to the needs of the actual business scenario to form a public key list S. The list S contains the public keys of all authorized users.
[0051] Furthermore, the business party organizes all collected public keys into a public key list S. The list S contains the public keys of all authorized users. For example, the organized public key list S is as follows: [Public Key 1, Public Key 2, Public Key 3]
[0052] Through the above method of this embodiment, this embodiment discloses how to use the elliptic curve algorithm to generate a public-private key pair, set the generated public key to be visible to the business, and organize and generate a public key whitelist according to business needs. This process ensures that the whitelist verification method of the smart contract can effectively control user permissions and achieve secure and efficient access management.
[0053] In some embodiments, the elements in the public key list are sorted, and the public key list is processed according to the number of elements in the public key list to obtain an ordered public key list, including:
[0054] Arrange the elements in the public key list in ascending order and determine the number of elements in the public key list. When the number of elements in the public key list is an odd number, copy the last element in the public key list to the public key list so that the length of the public key list is an even number, thereby obtaining an ordered public key list, wherein the length of the ordered public key list is an even number.
[0055] Specifically, the business party compiles a public key whitelist according to business requirements to obtain a public key list S. For example, in a specific example, it is assumed that the public key list S contains the following public keys:
[0056] Public key 1: 04bfcab98d765ec1acb23dc3a23...
[0057] Public key 2: 048f97d7c82b7a1dfda12b1d4f8...
[0058] Public key 3: 04a6b8d7c83c7a5eda24b3c9d3e...
[0059] Furthermore, the elements in the public key list S are arranged in ascending order. Assume that the public key list after ascending order is:
[0060] Public key 2: 048f97d7c82b7a1dfda12b1d4f8...
[0061] Public key 3: 04a6b8d7c83c7a5eda24b3c9d3e...
[0062] Public key 1: 04bfcab98d765ec1acb23dc3a23...
[0063] Furthermore, the number of elements in the public key list S is determined. If the number of elements is an odd number, the last element in the public key list S is copied to the list to make the length of the public key list an even number. Since the length of the current list S is 3 (an odd number), the last element (public key 1) is copied to the list to obtain an ordered public key list L:
[0064] Public key 2: 048f97d7c82b7a1dfda12b1d4f8...
[0065] Public key 3: 04a6b8d7c83c7a5eda24b3c9d3e...
[0066] Public key 1: 04bfcab98d765ec1acb23dc3a23...
[0067] Public key 1: 04bfcab98d765ec1acb23dc3a23...
[0068] At this time, the length of the list L is 4 (an even number). The height H of the binary tree (such as a Merkle tree) can be further calculated using the formula H=log2(N), where N is the length of the ordered public key list L. In this embodiment, N=4, so H=log2(4)=2.
[0069] Through the above method of this embodiment, this embodiment discloses how to sort the public key list and adjust the list length according to the number of elements to obtain an ordered public key list. This process ensures that the length of the public key list is an even number, thereby laying the foundation for the subsequent construction of the Merkle tree and realizing the whitelist verification method of the smart contract.
[0070] In some embodiments, performing a hash operation on each pair of public keys in the ordered public key list to generate a corresponding hash value, and recursively performing a combined hash operation on the hash values generated in the previous layer until a root hash value is obtained, including:
[0071] Traverse the ordered public key list, perform hash operations on each pair of adjacent public keys in turn according to the preset step size, obtain the hash value of each pair of public keys, and store the hash values in the hash value list; perform combined hash operations on each pair of hash values in the hash value list to generate a new hash value list, and perform recursive combined hash operations on each pair of hash values in the new hash value list until the length of the generated new hash value list is 1, and then obtain the root hash value.
[0072] Specifically, the ordered public key list L is traversed, and each pair of adjacent public keys is hashed in turn according to a preset step size (for example, the step size is 2) to generate a hash value for each pair of public keys. The generated hash values are stored in the first-level hash value list. For example, for each pair of public keys in the list L, their hash values are calculated respectively and a corresponding hash value list is generated.
[0073] Furthermore, a combined hash operation is performed on each pair of hash values in the first-layer hash value list to generate a new hash value list. Specifically, a combined hash operation is performed on the i-th and i+1-th hash values in the first-layer hash value list to obtain a new hash value (hash(i,i+1)), which is stored in the new hash value list.
[0074] Furthermore, the above steps are repeated to perform a recursive combined hash operation on each pair of hash values in the new hash value list. After each recursive operation, the generated hash value is stored in the updated hash value list, and the combined hash operation is continued on the updated hash value list until the length of the hash value list is 1.
[0075] Furthermore, when the length of the hash value list is 1, the last hash value is the root hash value. This root hash value is stored in the state of the smart contract for subsequent verification and validation.
[0076] Through the above method of this embodiment, this embodiment discloses how to perform initial hash calculation, combined hash calculation, and recursive calculation on an ordered public key list until a root hash value is generated. This process achieves efficient management and verification of the public key list by constructing a Merkle tree, providing a basic guarantee for whitelist verification of smart contracts.
[0077] The construction process of a binary tree (such as a Merkle tree) is described in detail below with reference to the accompanying drawings and specific embodiments. Figure 2 is a schematic diagram of the construction process of a binary tree in an actual scenario provided by an embodiment of the present application, such as Figure 2 As shown, the process of constructing a binary tree in the embodiment of the present application may include the following contents:
[0078] The Merkle tree is a tree data structure in which each leaf node stores the hash value of the original data, and non-leaf nodes store the combined hash of their child node hash values. Ultimately, the root node (root) of the tree stores the root hash value of the entire tree. List Sh is a collection of hash values of nodes at each level in the process of building the Merkle tree.
[0079] Initial state of Sh: In the process of constructing the Merkle tree, firstly, a hash operation is performed on each pair of public keys in the ordered public key list L, and the results are stored in the list Sh. At this time, Sh stores the hash values of the leaf nodes of the Merkle tree.
[0080] Recursive generation of Sh: Next, a new list Sh is generated by recursively performing combined hash operations on each pair of hash values in Sh. Each new layer of Sh represents the node hash value of the upper layer in the Merkle tree until the root hash value is generated.
[0081] Hierarchical representation of Sh: Sh can be represented as Sh0, Sh1, Sh2, ..., until S0, where Sh0 is the hash value of the leaf node and S0 is the root hash value.
[0082] For example, in a specific example, the construction process of the Merkle tree is as follows:
[0083] First, the initial public key hash:
[0084] List L: Ordered list of public keys.
[0085] List Sh: Hash each pair of public keys in L and store the result, i.e. Sh0.
[0086] Then, recursively hash:
[0087] List Sh1: Perform a combined hash operation on each pair of hash values in Sh0 to obtain Sh1.
[0088] List Sh2: Perform a combined hash operation on each pair of hash values in Sh1 to obtain Sh2.
[0089] And so on, until the root hash value (S0) is obtained.
[0090] Finally, the root hash value is stored in S0, the root node of the Merkle tree.
[0091] List Sh is a collection of hash values of nodes at each layer in the process of building the Merkle tree. The leaf node hash value list Sh0 is obtained by performing an initial hash calculation on the public key list L, and then the hash value lists of each layer (Sh1, Sh2, ...) are recursively calculated to finally obtain the root hash value (S0) of the Merkle tree. This ensures data integrity and verification efficiency.
[0092] In some embodiments, determining the sibling nodes and parent nodes of the public key in the binary tree, and recursively determining the sibling nodes in each level to construct a verification path includes:
[0093] Traverse the hash value list of nodes at each layer of the binary tree, determine the current traversal index, and determine the sibling node index of the current node in the current layer;
[0094] Determine the parent node index of the current node in the previous layer, and determine the sibling node index of the parent node in its layer;
[0095] Add the hash values of the sibling nodes of the current layer and the hash values of the sibling nodes of the parent node of the previous layer to the verification path array in sequence, and recursively reach the binary tree layer corresponding to the root hash value to build a complete verification path array.
[0096] Specifically, the process of constructing the verification path is described in detail below with reference to the accompanying drawings and specific embodiments. Figure 3 is a schematic diagram of the construction process of the verification path in the actual scenario provided by the embodiment of the present application, such as Figure 3 As shown, the process of constructing a verification path in the embodiment of the present application may include the following contents:
[0097] Traverse the hash value list Sh of nodes at each level corresponding to the binary tree, and the current traversal index is i. Assume that Sh represents the hash value list of the current level.
[0098] Further, determine the sibling node index of the current node: calculate the sibling node index b of the current node in the current layer according to the currently traversed index i, and the calculation formula is b=i+1-2*(i%2). This formula can be used to determine the sibling node index of the current node, thereby obtaining the hash value of the sibling node.
[0099] Further, determine the index of the parent node: calculate the parent node index p of the current node in the previous layer, and the calculation formula is p = i / / 2. This formula can be used to determine the parent node index of the current node in the previous layer, thereby obtaining the hash value of the parent node.
[0100] Further, determine the sibling node index of the parent node: calculate the sibling node index Pb of the parent node in its layer, and the calculation formula is Pb=p+1-2*(p%2). This formula can be used to determine the sibling node index of the parent node, thereby obtaining the hash value of the sibling node of the parent node.
[0101] Furthermore, a verification path array is constructed: the hash value Sh[b] of the sibling node of the current layer and the hash value Sh-1[Pb] of the sibling node of the parent node of the previous layer are added to the verification path array proof in sequence.
[0102] Recursively execute the above steps and perform index calculations on the nodes in each layer in turn until the binary tree layer corresponding to the root hash value is reached.
[0103] Furthermore, a complete verification path is generated:
[0104] Through recursive calculation, we finally get the complete verification path array proof, which contains all necessary hash values from the current node to the root hash value. The verification path array proof is used in the subsequent verification process to ensure that the validity of the public key can be correctly verified.
[0105] For example, in one example, assuming that in a certain level Sh list of a binary tree, the currently traversed index is i, the specific steps are as follows:
[0106] Calculate the sibling node index of the current node b = i + 1 - 2 * (i% 2);
[0107] Calculate the parent node index of the current node in the previous layer p = i / / 2;
[0108] Calculate the sibling node index Pb of the parent node in its layer = p + 1-2*(p%2);
[0109] Add Sh[b] and Sh-1[Pb] to the verification path array proof;
[0110] Recursively execute the above steps until the binary tree layer corresponding to the root hash value is reached, and finally a complete verification path array proof is generated.
[0111] Through the above method of this embodiment, this embodiment discloses how to determine the sibling nodes and parent nodes of the public key in each layer by traversing the hash value list of nodes in each layer of the binary tree, and recursively construct a verification path. This process ensures that the validity of the public key can be effectively verified, and the whitelist verification function of the smart contract is realized.
[0112] In some embodiments, a hash operation is performed on the public key, and a combined hash value is calculated step by step using a verification path until a final hash value is obtained. The final hash value is compared with the root hash value. If they are equal, the verification passes, otherwise the verification fails, including:
[0113] Define a verification method in the smart contract, where the return value of the verification method is a Boolean type;
[0114] The caller's public key is used as the input parameter of the verification method, and the hash value of the public key is calculated to obtain the initial hash value;
[0115] Perform a combined hash operation on the initial hash value and the first hash value in the verification path to obtain a first combined hash value;
[0116] Recursively perform a combined hash operation on the generated first combined hash value and the next hash value in the verification path until all hash values in the verification path are used to obtain a final hash value;
[0117] Compare the final hash value with the root hash value stored in the smart contract state. If they are equal, the verification passes and returns true; if they are not equal, the verification fails and returns false.
[0118] Specifically, a verification method (verify method) is defined in the smart contract, and the return value of this method is Boolean type. This verification method is used to receive the caller's public key and calculate its hash value, perform recursive calculation through the verification path, and finally verify whether the public key is in the whitelist.
[0119] Further, receiving the public key and calculating the initial hash value: The verification method receives the caller's public key (pub) as an input parameter. The hash value of the public key is calculated to obtain the initial hash value (h).
[0120] Further, combined hash operation: the initial hash value is combined with the first hash value in the verification path (proof[0]) to obtain a first combined hash value. Recursively combine the generated first combined hash value with the next hash value in the verification path.
[0121] Further, recursively calculate the final hash value: According to the above steps, recursively calculate each combined hash value and the next hash value in the verification path in turn until all hash values in the verification path are used. The hash value finally generated is the final hash value (finalhash).
[0122] Furthermore, the final hash value is compared with the root hash value: the final hash value is compared with the root hash value (root) stored in the smart contract state. If the final hash value is equal to the root hash value, the verification is passed and true is returned; if they are not equal, the verification fails and false is returned.
[0123] For example, in one example, the implementation process of the verify method defined in the smart contract is as follows:
[0124] The verify method (verify) receives the caller's public key (pub) as a parameter and calculates the hash value (h) of the public key.
[0125] Perform a combined hash operation on h and the first hash value (proof[0]) in the verification path (proof) to obtain the combined hash value h(pub,proof[0]).
[0126] Recursively calculate h(h(pub,proof[0]),proof[1]), performing combined hash operations in sequence until all hash values in the verification path are used up.
[0127] The final generated hash value (finalhash) is compared with the root hash value (root), and true is returned if they are equal, otherwise false is returned.
[0128] Through the method of the above-mentioned embodiment, this embodiment discloses how to define a verification method in a smart contract, by performing a hash operation on the public key and calculating the combined hash value step by step using the verification path, and finally verifying the validity of the public key, thereby realizing the whitelist verification function of the smart contract.
[0129] In some embodiments, in response to a user invoking an operation of a smart contract, a verification path of the user's corresponding public key is obtained, and the verification path is passed in as a parameter, and a verification method is called before a business method that needs to verify the whitelist. If the verification passes, the business logic continues to be executed, otherwise the execution is terminated, including:
[0130] When a user calls a smart contract, a request is sent to the server, the user's public key is used to obtain the corresponding verification path, and the obtained verification path is passed into the smart contract as a parameter;
[0131] Use smart contracts to call the verification method before each business method that needs to verify the whitelist, so as to verify according to the user's public key and verification path;
[0132] The user’s public key is hashed using the verification method, and the combined hash value is calculated step by step through the verification path until the final hash value is obtained, and the final hash value is compared with the root hash value stored in the smart contract state;
[0133] If the final hash value is equal to the root hash value, the verification is passed and the smart contract continues to execute the business logic. If the final hash value is not equal to the root hash value, the verification fails and the smart contract terminates execution.
[0134] Specifically, when a user calls a smart contract, he first sends a request to the server and uses the user's public key to obtain the corresponding verification path (proof). The server generates the corresponding verification path from the whitelist based on the user's public key and returns the verification path to the user.
[0135] Furthermore, when the user calls the smart contract, the obtained proof is passed into the smart contract as a parameter. For example, when the user calls the smart contract method, the proof is included in the parameter so that the smart contract can obtain the proof for verification.
[0136] Furthermore, the smart contract calls the verification method (verify method) before each business method that needs to verify the whitelist, so as to verify it according to the user's public key and verification path. The verification method receives the user's public key and verification path as input parameters, performs hash calculation and verification.
[0137] Furthermore, the verification method hashes the user's public key to generate an initial hash value. The initial hash value is hashed together with the first hash value in the verification path to obtain a combined hash value. The generated combined hash value is recursively hashed together with the next hash value in the verification path until all hash values in the verification path are used to obtain a final hash value. The final hash value is compared with the root hash value stored in the smart contract state.
[0138] Furthermore, if the final hash value is equal to the root hash value, the verification is passed, true is returned, and the smart contract continues to execute the business logic. If the final hash value is not equal to the root hash value, the verification fails, false is returned, and the smart contract terminates execution.
[0139] For example, in one example, the implementation of the verification method (verify) in the smart contract is as follows:
[0140] When a user calls a contract, he first requests the server to obtain a verification path (proof) using his public key.
[0141] When a user calls a smart contract, the verification path (proof) is passed into the smart contract as a parameter.
[0142] The contract calls the verification method (verify) before each business method that needs to verify the whitelist.
[0143] The verify method receives the caller's public key (pub) as a parameter, calculates its hash value (h), and then combines h with the first hash value in the verification path (proof[0]) to calculate the hash value to obtain the combined hash value.
[0144] Recursively calculate the combined hash value until all hash values in the verification path are used up to obtain the final hash value (finalhash).
[0145] Compare the final hash value with the root hash value (root). If they are equal, return true and continue to execute the business logic; if they are not equal, return false and terminate the execution of the business logic.
[0146] Through the above-mentioned method of this embodiment, this embodiment discloses how to respond to the user's operation of calling a smart contract, obtain the verification path and perform verification, ensure that only verified users can execute the business logic in the smart contract, and realize the whitelist verification function of the smart contract.
[0147] According to the technical solution provided in the embodiments of the present application, the technical solution of the present application has at least the following advantages:
[0148] 1. Improve security and transparency:
[0149] By storing the root hash value of the whitelist on the chain, the on-chain evidence of whitelist verification is realized, ensuring the immutability and high transparency of the data. The public-private key pair and the Merkle tree structure are used to verify the integrity of the data, further improving the security of the system.
[0150] 2. Reduce centralized trust costs:
[0151] This application reduces the reliance on centralized databases and trust costs by storing whitelist proofs locally off-chain and combining them with on-chain verification. This method avoids the problem of arbitrary tampering of centralized databases, prevents over-issuance and random issuance of whitelists, and improves the credibility of the system.
[0152] 3. Optimize storage and read and write costs:
[0153] By storing the whitelist proof locally off-chain and only storing the root hash value on-chain, the read and write costs of on-chain storage are significantly reduced. Compared with the traditional mapping structure storage method, the method of this application reduces the consumption of on-chain resources and improves the operating efficiency of the system.
[0154] 4. Efficient verification process:
[0155] This application describes in detail the specific implementation steps from generating public and private key pairs, organizing public key lists, building Merkle trees to the verification process, ensuring the efficiency and accuracy of the verification process. When a user calls a smart contract, by passing in a verification path (proof) and using the verification method for verification, it can quickly verify whether the user is in the whitelist, ensuring the efficient operation of the system.
[0156] Therefore, in summary, this application uses an innovative whitelist verification method combined with on-chain and off-chain verification mechanisms to not only improve the security and transparency of the system and reduce the centralized trust cost, but also optimizes the storage and read-write costs, ensuring the efficiency and reliability of smart contracts in practical applications.
[0157] The following is an embodiment of the device of the present application, which can be used to execute the embodiment of the method of the present application. For details not disclosed in the embodiment of the device of the present application, please refer to the embodiment of the method of the present application.
[0158] Figure 4 Schematic diagram of the structure of the whitelist verification device of the smart contract provided in the embodiment of the present application. Figure 4 As shown, the whitelist verification device of the smart contract includes:
[0159] The acquisition module 401 is configured to acquire a public key whitelist to obtain a public key list;
[0160] The processing module 402 is configured to sort the elements in the public key list and process the public key list according to the number of elements in the public key list to obtain an ordered public key list;
[0161] The operation module 403 is configured to perform a hash operation on each pair of public keys in the ordered public key list to generate a corresponding hash value, recursively perform a combined hash operation on the hash values generated in the previous layer until a root hash value is obtained, and store the root hash value in the state of the smart contract;
[0162] A construction module 404 is configured to determine the sibling nodes and the parent node of the public key in the binary tree, and recursively determine the sibling nodes in each level to construct a verification path;
[0163] The comparison module 405 is configured to perform a hash operation on the public key, and use the verification path to calculate the combined hash value step by step until a final hash value is obtained, and compare the final hash value with the root hash value. If they are equal, the verification passes, otherwise the verification fails;
[0164] The verification module 406 is configured to respond to the user's call to the smart contract operation, obtain the verification path of the user's corresponding public key, pass the verification path as a parameter, and call the verification method before the business method that needs to verify the whitelist. If the verification passes, the business logic continues to execute, otherwise the execution is terminated.
[0165] In some embodiments, Figure 4 The acquisition module 401 generates a public-private key pair using an elliptic curve algorithm, sets the public-private key pair to be visible to the business, and sorts and generates a public key whitelist according to business requirements provided by the business party to obtain a public key list.
[0166] In some embodiments, Figure 4 The processing module 402 arranges the elements in the public key list in ascending order and determines the number of elements in the public key list. When the number of elements in the public key list is an odd number, the last element in the public key list is copied to the public key list to make the length of the public key list an even number, thereby obtaining an ordered public key list, wherein the length of the ordered public key list is an even number.
[0167] In some embodiments, Figure 4 The operation module 403 traverses the ordered public key list, performs hash operation on each pair of adjacent public keys in turn according to a preset step length, obtains the hash value of each pair of public keys, and stores the hash value in the hash value list; performs a combined hash operation on each pair of hash values in the hash value list to generate a new hash value list, and performs a recursive combined hash operation on each pair of hash values in the new hash value list until the length of the generated new hash value list is 1, and the root hash value is obtained.
[0168] In some embodiments, Figure 4 The construction module 404 traverses the hash value list of nodes in each layer corresponding to the binary tree, determines the current traversal index, and determines the sibling node index of the current node in the current layer; determines the parent node index of the current node in the previous layer, and determines the sibling node index of the parent node in its layer; adds the hash value of the sibling node of the current layer and the hash value of the sibling node of the parent node in the previous layer to the verification path array in sequence, and recursively reaches the binary tree layer corresponding to the root hash value to construct a complete verification path array.
[0169] In some embodiments, Figure 4 The comparison module 405 defines a verification method in the smart contract, wherein the return value of the verification method is a Boolean type; the caller's public key is used as an input parameter of the verification method, and the hash value of the public key is calculated to obtain an initial hash value; the initial hash value is combined with the first hash value in the verification path to obtain a first combined hash value; the generated first combined hash value is recursively combined with the next hash value in the verification path until all hash values in the verification path are used to obtain a final hash value; the final hash value is compared with the root hash value stored in the smart contract state, if they are equal, the verification passes and true is returned; if they are not equal, the verification fails and false is returned.
[0170] In some embodiments, Figure 4 When the user calls the smart contract, the verification module 406 sends a request to the server, uses the user's public key to obtain the corresponding verification path, and passes the obtained verification path as a parameter to the smart contract; uses the smart contract to call the verification method before each business method that needs to verify the whitelist, so as to verify according to the user's public key and the verification path; uses the verification method to perform a hash operation on the user's public key, and calculates the combined hash value step by step through the verification path until the final hash value is obtained, and compares the final hash value with the root hash value stored in the smart contract state; if the final hash value is equal to the root hash value, the verification is passed, and the smart contract continues to execute the business logic; if the final hash value is not equal to the root hash value, the verification fails, and the smart contract terminates execution.
[0171] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0172] Figure 5 Schematic diagram of the structure of the electronic device 5 provided in the embodiment of the present application. Figure 5 As shown, the electronic device 5 of this embodiment includes: a processor 501, a memory 502, and a computer program 503 stored in the memory 502 and executable on the processor 501. When the processor 501 executes the computer program 503, the steps in the above-mentioned method embodiments are implemented. Alternatively, when the processor 501 executes the computer program 503, the functions of the modules / units in the above-mentioned device embodiments are implemented.
[0173] Exemplarily, the computer program 503 may be divided into one or more modules / units, which are stored in the memory 502 and executed by the processor 501 to complete the present application. The one or more modules / units may be a series of computer program instruction segments capable of completing specific functions, which are used to describe the execution process of the computer program 503 in the electronic device 5.
[0174] The electronic device 5 may be a desktop computer, a notebook, a PDA, a cloud server, or other electronic device. The electronic device 5 may include, but is not limited to, a processor 501 and a memory 502. Those skilled in the art will appreciate that Figure 5 It is only an example of the electronic device 5 and does not constitute a limitation of the electronic device 5. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the electronic device may also include input and output devices, network access devices, buses, etc.
[0175] The processor 501 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc.
[0176] The memory 502 may be an internal storage unit of the electronic device 5, for example, a hard disk or memory of the electronic device 5. The memory 502 may also be an external storage device of the electronic device 5, for example, a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the electronic device 5. Further, the memory 502 may also include both an internal storage unit of the electronic device 5 and an external storage device. The memory 502 is used to store computer programs and other programs and data required by the electronic device. The memory 502 may also be used to temporarily store data that has been output or is to be output.
[0177] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.
[0178] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0179] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0180] In the embodiments provided in the present application, it should be understood that the disclosed devices / computer equipment and methods can be implemented in other ways. For example, the device / computer equipment embodiments described above are only schematic. For example, the division of modules or units is only a logical function division. There may be other division methods in actual implementation. Multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0181] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0182] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0183] If the integrated module / unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. The computer program may include computer program code, and the computer program code may be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying computer program code, recording medium, U disk, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, the computer-readable medium does not include electric carrier signals and telecommunication signals.
[0184] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.
Claims
1. A whitelist verification method for a smart contract, characterized in that: include: Get the public key whitelist to get the public key list; Sorting the elements in the public key list, and processing the public key list according to the number of elements in the public key list to obtain an ordered public key list; Perform hash operation on each pair of public keys in the ordered public key list to generate a corresponding hash value, recursively perform combined hash operation on the hash values generated in the previous layer until a root hash value is obtained, and store the root hash value in the state of the smart contract; Determine the sibling nodes and parent nodes of the public key in the binary tree, and recursively determine the sibling nodes in each level to build a verification path; Performing a hash operation on the public key, and calculating the combined hash value step by step using the verification path until a final hash value is obtained, and comparing the final hash value with the root hash value. If they are equal, verification passes, otherwise verification fails; In response to the user calling the smart contract operation, the verification path of the public key corresponding to the user is obtained, and the verification path is passed in as a parameter. The verification method is called before the business method that needs to verify the whitelist. If the verification passes, the business logic continues to execute, otherwise the execution is terminated.
2. The method according to claim 1, characterized in that The public key whitelist is obtained to obtain a public key list, including: The public-private key pair is generated by using an elliptic curve algorithm, and the public-private key pair is set to be visible to the business. The public key whitelist is sorted and generated according to the business requirements provided by the business party to obtain the public key list.
3. The method according to claim 1, characterized in that: The step of sorting the elements in the public key list and processing the public key list according to the number of elements in the public key list to obtain an ordered public key list includes: Arrange the elements in the public key list in ascending order, and judge the number of elements in the public key list. When the number of elements in the public key list is an odd number, copy the last element in the public key list to the public key list so that the length of the public key list is an even number, thereby obtaining the ordered public key list, wherein the length of the ordered public key list is an even number.
4. The method according to claim 1, characterized in that: The step of performing a hash operation on each pair of public keys in the ordered public key list to generate a corresponding hash value, and recursively performing a combined hash operation on the hash values generated in the previous layer until a root hash value is obtained, includes: The ordered public key list is traversed, and hash operations are performed on each pair of adjacent public keys in turn according to a preset step size to obtain hash values of each pair of public keys, and the hash values are stored in a hash value list; a combined hash operation is performed on each pair of hash values in the hash value list to generate a new hash value list, and a recursive combined hash operation is performed on each pair of hash values in the new hash value list until the length of the generated new hash value list is 1, and the root hash value is obtained.
5. The method according to claim 1, characterized in that The step of determining the sibling nodes and the parent node of the public key in the binary tree and recursively determining the sibling nodes in each level to construct a verification path includes: Traversing the hash value lists of nodes at each layer corresponding to the binary tree, determining the index of the current traversal, and determining the index of the sibling nodes of the current node in the current layer; Determine the parent node index of the current node in the previous layer, and determine the sibling node index of the parent node in its layer; Add the hash values of the sibling nodes of the current layer and the hash values of the sibling nodes of the parent node of the previous layer to the verification path array in sequence, and recursively reach the binary tree layer corresponding to the root hash value to build a complete verification path array.
6. The method according to claim 1, characterized in that The hash operation is performed on the public key, and the combined hash value is calculated step by step using the verification path until a final hash value is obtained, and the final hash value is compared with the root hash value. If they are equal, the verification passes, otherwise the verification fails, including: A verification method is defined in the smart contract, wherein the return value of the verification method is a Boolean type; Using the public key of the caller as an input parameter of the verification method, calculating the hash value of the public key to obtain an initial hash value; Performing a combined hash operation on the initial hash value and the first hash value in the verification path to obtain a first combined hash value; recursively performing a combined hash operation on the generated first combined hash value and the next hash value in the verification path until all hash values in the verification path are used to obtain the final hash value; The final hash value is compared with the root hash value stored in the smart contract state. If they are equal, the verification passes and true is returned; if they are not equal, the verification fails and false is returned.
7. The method according to claim 6, characterized in that In response to the user calling the smart contract operation, the verification path of the user's corresponding public key is obtained, and the verification path is passed in as a parameter, and the verification method is called before the business method that needs to verify the whitelist. If the verification passes, the business logic continues to be executed, otherwise the execution is terminated, including: When a user calls a smart contract, a request is sent to the server, the corresponding verification path is obtained using the user's public key, and the obtained verification path is passed into the smart contract as a parameter; Using the smart contract to call the verification method before each business method that needs to verify the whitelist, so as to verify according to the public key and verification path of the user; Performing a hash operation on the user's public key using the verification method, and calculating the combined hash value step by step through the verification path until a final hash value is obtained, and comparing the final hash value with the root hash value stored in the smart contract state; If the final hash value is equal to the root hash value, the verification is passed and the smart contract continues to execute the business logic. If the final hash value is not equal to the root hash value, the verification fails and the smart contract terminates execution.
8. A whitelist verification device for a smart contract, characterized in that: include: An acquisition module is configured to acquire a public key whitelist to obtain a public key list; a processing module, configured to sort the elements in the public key list, and process the public key list according to the number of elements in the public key list to obtain an ordered public key list; A calculation module is configured to perform a hash operation on each pair of public keys in the ordered public key list to generate a corresponding hash value, recursively perform a combined hash operation on the hash values generated in the previous layer until a root hash value is obtained, and store the root hash value in the state of the smart contract; A construction module, configured to determine the sibling nodes and the parent node of the public key in the binary tree, and recursively determine the sibling nodes in each level to construct a verification path; A comparison module is configured to perform a hash operation on the public key, and use the verification path to calculate the combined hash value step by step until a final hash value is obtained, and compare the final hash value with the root hash value. If they are equal, the verification passes, otherwise the verification fails; The verification module is configured to respond to the user's call to the smart contract operation, obtain the verification path of the user's corresponding public key, pass the verification path as a parameter, and call the verification method before the business method that needs to verify the whitelist. If the verification passes, continue to execute the business logic, otherwise terminate the execution.
9. An electronic device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Method for relating digital certificates through one-way hash function
CN104717070A
Method and device for safely loading files in white list library
CN108270574A
Parallel execution of transactions in a blockchain network based on smart contract whitelists
CN110300985A
Blockchain-Assisted Hash-Based Data Signature System and Method
US20200052886A1
