Resource exchange method, device, equipment and readable storage medium based on blockchain

By adding hash locks and time locks to the blockchain, the redemption sequence problem between blockchains is solved, the security and efficiency of resource redemption are improved, and the redemption risk is reduced.

CN113987526BActive Publication Date: 2024-12-03HAINAN HUOCHAIN TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202111129641.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-09-26
Publication Date
2024-12-03
Estimated Expiration
2041-09-26

Smart Images

  • Figure CN113987526B_ABST
    Figure CN113987526B_ABST
Patent Text Reader

Abstract

The present application discloses a resource exchange method, device, equipment and readable storage medium based on blockchain, which relates to the field of Internet technology. A hash lock and a time lock are added to the resource to be exchanged to temporarily lock the resource. After the verification based on the random number is passed, the cross-chain resource is exchanged to avoid the risk of resource exchange caused by asynchronous resource exchange, thereby improving the efficiency of resource exchange. The method includes: obtaining an encrypted hash value and a lock time value; determining a first resource for exchange, adding a hash lock and a time lock to the first resource, and calling a smart contract to publish the added first resource; when a random number sent by a third user is received, the current time is determined, and when the current time satisfies the time lock, the hash lock is decrypted, and the first resource is sent to the third user; the random number is sent to the second user; the second resource returned by the second user is received, and the second resource is stored in the current blockchain, and the resource information of the current blockchain is updated.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of Internet technology, and in particular to a resource exchange method, device, equipment and readable storage medium based on blockchain. Background Art

[0002] With the continuous advancement of computer technology, some platforms will establish resource malls such as points and gold coins, and give users virtual resources so that users can exchange them in the resource mall, thereby increasing user activity and making the connection between the platform and users more stable and close. At present, resource exchange has become one of the common service types in the platform. Different platforms correspond to different types of resources. When users do not have enough resources on the target platform for exchange, they can exchange other types of resources they have for the resource types of the target platform for exchange on the target platform.

[0003] In the related technology, when cross-chain exchange of resources is carried out, the exchange will first be carried out on the first blockchain and then on the second blockchain. When the users proposing resource exchange do not match each other, more users need to enter for joint exchange.

[0004] In the process of implementing this application, the applicant found that the related technology has at least the following problems:

[0005] There will be a sequence of exchanges when exchanging between different blockchains. Such a sequence will cause the risk of resource loss in resource exchange. If the user who exchanges later does not acknowledge the exchange, the user who exchanges first will lose the resources that have been spent, resulting in low efficiency of resource exchange and high exchange risk. Summary of the invention

[0006] In view of this, the present application provides a resource exchange method, device, equipment and readable storage medium based on blockchain, the main purpose of which is to solve the problem that there is a different order of exchange when exchanging on different blockchains, which makes resource exchange generate the risk of resource loss, resulting in low efficiency of resource exchange and high exchange risk.

[0007] According to the first aspect of the present application, a resource exchange method based on blockchain is provided, the method comprising:

[0008] Obtaining an encrypted hash value and a lock time value, where the encrypted hash value and the lock time value are generated by the blockchain where the first user is located or obtained by the blockchain where the first user is located from content published by the blockchain where the second user is located based on a smart contract, and the resource type of the resources held by the blockchain where the second user is located matches the resource type of the resources required by the blockchain where the first user is located;

[0009] Determine a first resource for exchange, add a hash lock and a time lock to the first resource, and call a smart contract to publish the added first resource, where the value of the hash lock is equal to the encrypted hash value, and the value of the time lock is equal to the lock time value;

[0010] When receiving a random number sent by the blockchain where the third user is located, determining the current time, and when the current time satisfies the time lock, decrypting the hash lock, and sending the first resource to the blockchain where the third user is located;

[0011] Sending the random number to the blockchain where the second user is located, so that the blockchain where the second user is located determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource, where the second resource is provided by the blockchain where the second user is located and the resource type matches the resource type of the resource required by the blockchain where the first user is located;

[0012] Receive the second resource returned by the blockchain where the second user is located, store the second resource in the current blockchain, and update the resource information of the current blockchain, where the second resource is decrypted and returned when the blockchain where the second user is located determines that the time satisfies the time lock corresponding to the second resource.

[0013] Optionally, obtaining the encrypted hash value and the lock time value includes:

[0014] Generate the random number, perform hash calculation on the random number, obtain the hash value of the random number as the encrypted hash value, and determine the number of users participating in the resource exchange and the operation time according to the multi-party atomic exchange protocol, and generate the lock time value based on the number of users and the operation time; or,

[0015] Read the content published by the blockchain where the second user is located based on the smart contract, obtain the encrypted hash value and the historical time lock set by the blockchain where the second user is located in the content, determine the historical lock time value of the historical time lock, reduce the historical lock time value, and obtain the lock time value.

[0016] Optionally, the determining a first resource for exchange, and adding a hash lock and a time lock to the first resource includes:

[0017] Identify the resource exchange request of the blockchain where the first user is located, and determine the target amount of resources for exchange;

[0018] Extracting the first resource whose resource amount satisfies the target resource amount from all resources of the current blockchain;

[0019] A hash lock whose value is the encrypted hash value is added to the first resource, a time lock whose value is the locking time value is added to the first resource, and the first resource is locked.

[0020] Optionally, when the current time satisfies the time lock, decrypting the hash lock and sending the first resource to the blockchain where the third user is located includes:

[0021] Determine the release time of the first resource added by the blockchain where the first user is located, and count the time interval between the current time and the release time;

[0022] If the time interval is less than or equal to the lock time value, it is determined that the current time satisfies the time lock, and the random number is used to decrypt the hash lock;

[0023] The first resource is sent to the blockchain where the third user is located, the first resource is deleted from all resources of the current blockchain, and the resource information of the current blockchain is updated.

[0024] Optionally, the method further comprises:

[0025] If the statistically obtained time interval is greater than the lock time value, it is determined that the current time does not satisfy the time lock, and the hash lock is kept locked. The time interval is the time interval between the release time of the first resource added by the blockchain where the first user is located and the current time;

[0026] A timeout message is sent to the blockchain where other users participating in the resource exchange are located to cancel the resource exchange operation. The timeout message is used to notify the blockchain where the other users are located that the resource exchange operation is canceled.

[0027] Optionally, after sending the random number to the blockchain where the second user is located, the method further includes:

[0028] If a timeout message is received from the blockchain where the second user is located, it is determined that the resource exchange operation is cancelled;

[0029] Receive the first resource returned by the blockchain where the third user is located, add the first resource to the current blockchain, and update the resource information of the current blockchain.

[0030] Optionally, after receiving the second resource returned by the blockchain where the second user is located, storing the second resource in the current blockchain, and updating the resource information of the blockchain where the first user is located, the method further includes:

[0031] If the timeout information of the blockchain where the fourth user is located is received, the second resource is extracted from the current blockchain, the second resource is returned to the blockchain where the second user is located, and the resource information of the current blockchain is updated, and the fourth user is any user among all the users participating in the resource exchange this time;

[0032] Receive the first resource returned by the blockchain where the third user is located, add the first resource to the current blockchain, and update the resource information of the current blockchain.

[0033] According to the second aspect of the present application, a resource exchange device based on blockchain is provided, the device comprising:

[0034] an acquisition module, configured to acquire an encrypted hash value and a lock time value, wherein the encrypted hash value and the lock time value are generated by the blockchain where the first user is located or acquired by the blockchain where the first user is located from content published by the blockchain where the second user is located based on a smart contract, and the resource type of the resources held by the blockchain where the second user is located matches the resource type of the resources required by the blockchain where the first user is located;

[0035] A determination module, used to determine a first resource for exchange, add a hash lock and a time lock to the first resource, and call a smart contract to publish the added first resource, wherein the value of the hash lock is equal to the encrypted hash value, and the value of the time lock is equal to the lock time value;

[0036] a decryption module, configured to determine the current time when receiving a random number sent by the blockchain where the third user is located, and when the current time satisfies the time lock, decrypt the hash lock and send the first resource to the blockchain where the third user is located;

[0037] A first transmission module, configured to send the random number to the blockchain where the second user is located, so that the blockchain where the second user is located determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource, where the second resource is provided by the blockchain where the second user is located and the resource type matches the resource type of the resource required by the blockchain where the first user is located;

[0038] The receiving module is used to receive the second resource returned by the blockchain where the second user is located, store the second resource in the current blockchain, and update the resource information of the current blockchain, wherein the second resource is decrypted and returned when the blockchain where the second user is located determines that the time satisfies the time lock corresponding to the second resource.

[0039] Optionally, the acquisition module is used to generate the random number, perform hash calculation on the random number, obtain the hash value of the random number as the encrypted hash value, and determine the number of users participating in the resource exchange and the operation time according to the multi-party atomic exchange protocol, and generate the lock time value based on the number of users and the operation time; or, read the content published by the blockchain where the second user is located based on the smart contract, obtain the encrypted hash value and the historical time lock set by the blockchain where the second user is located in the content, determine the historical lock time value of the historical time lock, reduce the historical lock time value, and obtain the lock time value.

[0040] Optionally, the determination module is used to identify the resource exchange request of the blockchain where the first user is located, determine the target resource amount for exchange; extract the first resource whose resource amount meets the target resource amount from all the resources of the current blockchain; add the hash lock with the value of the encrypted hash value to the first resource, add the time lock with the value of the lock time value to the first resource, and lock the first resource.

[0041] Optionally, the decryption module is used to determine the release time of the first resource after being added by the blockchain where the first user is located, and to count the time interval between the current time and the release time; if the time interval is less than or equal to the lock time value, it is determined that the current time satisfies the time lock, and the random number is used to decrypt the hash lock; the first resource is sent to the blockchain where the third user is located, the first resource is deleted from all resources of the current blockchain, and the resource information of the current blockchain is updated.

[0042] Optionally, the device further comprises:

[0043] The decryption module is further configured to determine that the current time does not satisfy the time lock and maintain the locked state of the hash lock if the statistically obtained time interval is greater than the lock time value, and the time interval is the time interval between the release time of the first resource added by the blockchain where the first user is located and the current time;

[0044] The second transmission module is used to send a timeout message to the blockchain where other users participating in the resource exchange are located to cancel the resource exchange operation. The timeout message is used to notify the blockchain where the other users are located that the resource exchange operation is canceled.

[0045] Optionally, the receiving module is further used to determine that the resource exchange operation is cancelled if a timeout information is received from the blockchain where the second user is located; receive the first resource returned by the blockchain where the third user is located, add the first resource to the current blockchain, and update the resource information of the current blockchain.

[0046] Optionally, the receiving module is further used to extract the second resource from the current blockchain if timeout information of the blockchain where the fourth user is located is received, return the second resource to the blockchain where the second user is located, and update the resource information of the current blockchain, where the fourth user is any user among all users participating in the resource exchange this time; receive the first resource returned by the blockchain where the third user is located, add the first resource to the current blockchain, and update the resource information of the current blockchain.

[0047] According to a third aspect of the present application, there is provided a storage device on which a computer program is stored, and when the program is executed by a processor, the steps of any one of the methods described in the first aspect are implemented.

[0048] According to the fourth aspect of the present application, a blockchain-based resource exchange device is provided, comprising a storage device, a processor, and a computer program stored on the storage device and executable on the processor, wherein when the processor executes the program, the steps of any one of the methods described in the first aspect are implemented.

[0049] By means of the above technical scheme, the present application provides a resource exchange method, device, equipment and readable storage medium based on blockchain. The present application obtains an encrypted hash value and a lock time value, determines a first resource for exchange, adds a hash lock whose hash value is equal to the encrypted hash value and a time lock whose value is the lock time value to the first resource, and calls a smart contract to publish the added first resource, so that when the blockchain where the first user is located receives a random number sent by the blockchain where the third user is located, the current time is determined, and when the current time satisfies the time lock, the hash lock is decrypted and the first resource is sent to the blockchain where the third user is located. Next, the blockchain where the first user is located sends the random number to the blockchain where the second user is located, so that the blockchain where the second user is located determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource. When the blockchain where the second user is located determines that the time satisfies the time lock corresponding to the second resource, it receives the second resource returned by the blockchain where the second user is located, stores the second resource in the blockchain where the first user is located, updates the resource information of the blockchain where the first user is located, temporarily locks the resource to be exchanged by adding a hash lock and a time lock, and actually exchanges the resource only after the verification based on the random number is passed, thereby realizing cross-chain resource exchange among multiple users, avoiding the risk of resource exchange caused by asynchronous resource exchange, and having high security for resource exchange, thereby improving the efficiency of resource exchange.

[0050] The above description is only an overview of the technical solution of the present application. In order to more clearly understand the technical means of the present application, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present application more obvious and easy to understand, the specific implementation methods of the present application are listed below. BRIEF DESCRIPTION OF THE DRAWINGS

[0051] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present application. Also, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:

[0052] Figure 1 A schematic diagram of a resource exchange method based on blockchain provided in an embodiment of the present application is shown;

[0053] Figure 2A A schematic diagram of a resource exchange method based on blockchain provided in an embodiment of the present application is shown;

[0054] Figure 2B A schematic diagram of a resource exchange method based on blockchain provided in an embodiment of the present application is shown;

[0055] Figure 3A A schematic diagram of the structure of a resource exchange device based on blockchain provided in an embodiment of the present application is shown;

[0056] Figure 3B A schematic diagram of the structure of a resource exchange device based on blockchain provided in an embodiment of the present application is shown;

[0057] Figure 4 A schematic diagram of the device structure of a computer device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION

[0058] The exemplary embodiments of the present application will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present application are shown in the accompanying drawings, it should be understood that the present application can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present application and to fully convey the scope of the present application to those skilled in the art.

[0059] The present application embodiment provides a resource exchange method based on blockchain, such as Figure 1 As shown, the method includes:

[0060] 101. Obtain an encrypted hash value and a lock time value, where the encrypted hash value and the lock time value are generated by the blockchain where the first user is located or obtained by the blockchain where the first user is located from the content published based on the smart contract on the blockchain where the second user is located, and the resource type of the resources held by the blockchain where the second user is located matches the resource type of the resources required by the first user.

[0061] 102. Determine a first resource for exchange, add a hash lock and a time lock to the first resource, and call the smart contract to publish the added first resource. The value of the hash lock is equal to the encrypted hash value, and the value of the time lock is equal to the lock time value.

[0062] 103. When a random number is received from the blockchain where the third user is located, the current time is determined. When the current time satisfies the time lock, the hash lock is decrypted, and the first resource is sent to the blockchain where the third user is located.

[0063] 104. Send the random number to the blockchain where the second user is located, so that the blockchain where the second user is located determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource. The second resource is provided by the blockchain where the second user is located and the resource type matches the resource type of the resource required by the blockchain where the first user is located.

[0064] 105. Receive the second resource returned by the blockchain where the second user is located, store the second resource in the current blockchain, and update the resource information of the current blockchain. The second resource is decrypted and returned when the blockchain where the second user is located determines that the time meets the time lock corresponding to the second resource.

[0065] The method provided by the embodiment of the present application obtains an encrypted hash value and a lock time value, determines a first resource for exchange, adds a hash lock with a hash value equal to the encrypted hash value and a time lock with a value of the lock time value to the first resource, and calls a smart contract to publish the added first resource, so that when the first user receives a random number sent by a third user, the current time is determined, and when the current time satisfies the time lock, the hash lock is decrypted, and the first resource is sent to the third user. Next, the first user sends the random number to the second user, so that the second user determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource, and when the second user determines that the time satisfies the time lock corresponding to the second resource, the second resource returned by the second user is received, and the second resource is stored in the current blockchain, and the resource information of the current blockchain is updated. The resource is temporarily locked by adding a hash lock and a time lock to the resource to be exchanged, and the resource is actually exchanged only after the verification based on the random number is passed, so as to realize the resource exchange of multiple users, avoid the risk of resource exchange caused by asynchronous resource exchange, and the resource exchange security is high, and the efficiency of resource exchange is improved.

[0066] The present application embodiment provides a resource exchange method based on blockchain, such as Figure 2A As shown, the method includes:

[0067] 201. Obtain the encrypted hash value and lock time value.

[0068] With the continuous advancement of computer technology, some platforms will establish resource malls such as points and gold coins, and give users virtual resources so that users can exchange them in the resource mall, thereby increasing user activity and making the connection between the platform and users more stable and close. At present, resource exchange has become one of the common service types in the platform. Different platforms correspond to different types of resources. When users do not have enough resources on the target platform for exchange, they can exchange other types of resources they have for the resource types of the target platform for exchange in the target platform. At present, when cross-chain resource exchange is carried out, it will be exchanged on the first blockchain first, and then on the second blockchain. When the users who propose resource exchange do not match each other, more users need to enter for joint exchange. However, the applicant recognizes that there will be a sequence of exchange in different blockchains. Such a sequence will cause the risk of resource loss in resource exchange. If the user who exchanges later does not recognize the exchange, the user who exchanges first will lose the resources that have been spent, resulting in low efficiency of resource exchange and high exchange risk.

[0069] Considering that the use of hash time lock as a contract algorithm can lock the funds used for exchange, and due to the limitation of the time lock, it can ensure that the contract can only be executed within a predefined time range, therefore, the present application proposes a resource exchange method based on blockchain. The present application provides a resource exchange method, device, equipment and readable storage medium based on blockchain. The present application obtains an encrypted hash value and a lock time value, determines a first resource for exchange, adds a hash lock whose hash value is equal to the encrypted hash value and a time lock whose value is the lock time value to the first resource, and calls a smart contract to publish the added first resource, so that when the blockchain where the first user is located receives a random number sent by the blockchain where the third user is located, the current time is determined, and when the current time satisfies the time lock, the hash lock is decrypted and the first resource is sent to the blockchain where the third user is located. Next, the blockchain where the first user is located sends the random number to the blockchain where the second user is located, so that the blockchain where the second user is located determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource. When the blockchain where the second user is located determines that the time satisfies the time lock corresponding to the second resource, it receives the second resource returned by the blockchain where the second user is located, stores the second resource in the blockchain where the first user is located, updates the resource information of the blockchain where the first user is located, temporarily locks the resource to be exchanged by adding a hash lock and a time lock, and actually exchanges the resource only after the verification based on the random number is passed, thereby realizing cross-chain resource exchange among multiple users, avoiding the risk of resource exchange caused by asynchronous resource exchange, and having high security for resource exchange, thereby improving the efficiency of resource exchange.

[0070] Among them, the users participating in resource exchange in this application are in different blockchains. Specifically, there is no specific limit on the number of users participating in resource exchange. Based on the contract algorithm, that is, the hash time lock, a hash lock with the encrypted hash value and a time lock with the lock time value are added to the resource used for exchange. In the process of executing resource exchange, if the time when all users unlock the hash lock meets the corresponding time lock, the exchange can be successfully completed. If there is a user who unlocks the hash value and the time does not meet the corresponding time lock, the exchange is canceled, that is, the exchange fails, and the exchange request needs to be re-initiated for resource exchange. The specific process of obtaining the hash value and the lock time value is as follows:

[0071] In actual application scenarios, there are two situations for obtaining hash values ​​and lock time values. The first situation is that when the first user is the initiator of the exchange, the hash value and lock time value are generated by the first user. First, the current blockchain where the first user is located will randomly generate a numerical value, that is, a random number. Based on the hash algorithm, the random number is hashed to obtain an encrypted hash value. Then, according to the multi-party atomic exchange protocol, the number of users participating in this resource exchange and the operation time are determined, where the operation time is the time when the first user changes the contract status and the second user can detect the change in the contract status, that is, the time interval between the first user publishing the first resource with a hash time lock and the second user detecting the published content. Generate a lock time value based on the number of users and the operation time. Specifically, the lock time value can be a multiple of the product of the number of users and the operation time, such as 2 times, 3 times, etc. This application does not specifically limit the setting size of the lock time value. For example, the time interval from the first user A publishing the first resource to the second user B being able to detect the published content is T0. There are four users participating in this exchange. At this time, the product of the number of users and the operation time is 4T0. The first user A can set the lock time value to twice the product, that is, 8T0.

[0072] Another situation is that when the first user is the recipient of the exchange, the first user needs to obtain the hash value and the historical lock time value by reading the contract content published by the second user, and calculate the current lock time value based on the historical lock time value. Specifically, read the content published by the second user based on the smart contract, then obtain the encrypted hash value and the historical time lock set by the second user in the content, and determine the historical lock time value of the historical time lock. Finally, reduce the historical lock time value to obtain the lock time value. The reduced time can be set by the staff according to the actual application scenario, or it can be a default value. This application does not specifically limit the setting method of the reduced time. For example, the lock time value set by the second user is 8T0, and the first user can reduce it by 1 T0 and set the lock time value to 7T0.

[0073] Through the above steps, users participating in this resource exchange can obtain the hash value and lock time value required for this resource exchange, and add hash locks and time locks to the resources used for exchange based on the hash value and lock time value, thereby ensuring the security of cross-chain resource exchange of multiple blockchains and reducing the risk of resource loss.

[0074] 202. Determine a first resource for exchange, add a hash lock and a time lock to the first resource, and call a smart contract to publish the added first resource.

[0075] In an embodiment of the present application, after obtaining the hash value and the lock time value, the hash value can be used to add a hash lock to the first resource to encrypt the first resource, and the lock time value can be used to add a time lock to the first resource to ensure that the smart contract can only be executed within the preset time lock range. Subsequently, the smart contract is called to publish the added first resource as well as the hash value and the lock time value.

[0076] First, identify the resource exchange request of the first user and determine the target resource quantity for exchange. The resource exchange request can be issued by the first user based on the client. Specifically, any user with resource exchange requirements can initiate a resource exchange request on the client, enter the type and quantity of resources to be exchanged, match the user's needs through the smart contract, and initiate the same exchange process for multiple users. After obtaining the type and quantity of resources, the target resource quantity for exchange can be determined.

[0077] Subsequently, the first resource whose resource quantity meets the target resource quantity is extracted from all the resources in the current blockchain. In a specific application scenario, all the resources owned by the first user are stored in the blockchain where the first user is located. These existing resources may include multiple resource types. Therefore, after obtaining the target resource quantity, it is necessary to extract the first resource whose resource quantity meets the target resource quantity in the blockchain. Among them, the target resource type of the target resource quantity must be included in the existing resource types stored in the current blockchain, and the target resource quantity must be less than or equal to the existing resource quantity corresponding to the target resource type in the blockchain. If the corresponding target resource type does not exist in the blockchain or the existing resource quantity corresponding to the target resource type is insufficient, the resource exchange will not continue.

[0078] Finally, a hash lock whose value is the encrypted hash value and a time lock whose value is the lock time value are added to the first resource to lock the first resource.

[0079] For example, user A is the initiator of the redemption. A generates a random number s, calculates the hash value h for the random number s based on the hash algorithm, sets the lock time value to 8T0, calls the smart contract on the A chain, locks the A points to be transferred to user B, adds a hash lock with a hash value of h to the A points, and locks a time lock with a time value of 8T0.

[0080] Adding a hash lock to the resources used for exchange can lock the use of funds, ensure the security of the exchange, and prevent the resources from being tampered with. Adding a time lock can ensure that the contract can only be executed within a predefined time range, speed up the exchange efficiency, and make the resource exchange involving multiple users consistent, either all users exchange successfully, or all users exchange unsuccessfully.

[0081] 203. When receiving the random number sent by the blockchain where the third user is located, determine the current time. When the current time satisfies the time lock, execute the following step 204. When the current time does not satisfy the time lock, execute the following step 206.

[0082] In the embodiment of the present application, when the first user receives the random number sent by the third user, it means that the third user has delivered the third resource locked by him and wants to unlock the hash lock locked by the first user and obtain the first resource. When the first user receives the random number, it means that the third user has met the conditions for releasing the hash lock. At this time, it is necessary to determine whether the unlocking time of the third user has timed out.

[0083] Specifically, if it is determined that the current time when the third user unlocks satisfies the time lock, the hash lock can be decrypted and the first resource can be sent to the third user, that is, the following step 204 is executed. If it is determined that the current time when the third user unlocks does not satisfy the time lock, the hash lock is kept locked and a timeout message is sent, that is, the following step 206 is executed. For example, user A receives a random number s sent by user B, determines the current time, calculates the time interval between the current time and when user A calls the smart contract to release the first resource, and compares the time interval with 8T0.

[0084] Each time a resource is exchanged, a new random number is generated. The smart contract can calculate the encrypted hash value of this resource exchange based on the new random number. Different users call the same smart contract and add a hash lock with the same value. Therefore, the random number sent by the third user is the password for decrypting the hash lock of this resource exchange. Subsequently, the first user can send the random number to the second user to obtain the second resource.

[0085] 204. When the current time satisfies the time lock, the hash lock is decrypted and the first resource is sent to the blockchain where the third user is located.

[0086] In the embodiment of the present application, when the current time satisfies the time lock, the first user can unlock the first resource and send it to the third user, and the process of unlocking the first resource can also verify the correctness of the random number. If there is no random number transmission error, the first user can send the decrypted first resource to the third user, and send the random number to the second user to decrypt the hash lock of the second resource.

[0087] Specifically, determine the publishing time of the first resource added by the first user, count the time interval between the current time and the publishing time, if the time interval is less than or equal to the lock time value, determine that the current time satisfies the time lock, use a random number to decrypt the hash lock, and finally, send the first resource to the third user, delete the first resource from all resources in the current blockchain, and update the resource information of the current blockchain.

[0088] In actual application scenarios, there may be a situation where a random number is transmitted incorrectly. If a random number is transmitted incorrectly, the first user can generate a random number error prompt message and send it to the third user, so that the third user can resend the random number for decryption. The current time when the random number is received is re-determined to determine whether the current time meets the time lock.

[0089] 205. Send the random number to the blockchain where the second user is located, receive the exchange result returned by the blockchain where the second user is located, and update the resource information of the blockchain where the first user is located based on the exchange result.

[0090] In an embodiment of the present application, after the first user sends the random number to the second user, the second user will perform the same judgment operation as the first user to determine whether the first user can unlock the hash lock of the second resource. Therefore, there are also two situations. One is that the time when the first user sends the random number to unlock meets the time lock of the second resource, and the second resource can be decrypted to obtain the second resource sent by the second user. The other is that the time when the random number is sent for decryption does not meet the time lock of the second resource, and the second resource cannot be decrypted, and the exchange result obtained is a timeout information.

[0091] If the received result is the second resource, the second resource is stored in the current blockchain and the resource information of the current blockchain is updated. If the timeout information returned by the second user is received, the resource exchange operation is determined to be cancelled, the first resource returned by the third user is received, the first resource is added to the current blockchain, and the resource information of the current blockchain is updated.

[0092] By first receiving a random number, unlocking the first resource locked by oneself and sending it to a third user, and then sending the obtained random number to the second user to obtain the second resource, the password is transmitted in sequence in the direction of resource acquisition, so that the user first delivers the resource and then obtains the resource. This can achieve direct and real-time resource exchange without the need for the supervisory party to manage the exchange, avoiding the situation where the user does not recognize the exchange after obtaining the resource, and enhancing the security of resource exchange.

[0093] 206. When the current time does not satisfy the time lock, the hash lock is kept locked and a timeout message is sent.

[0094] In an embodiment of the present application, when a random number sent by a third user is received, the current time is determined to determine whether the current time satisfies the hash lock of the first resource. If the hash lock is not satisfied, it means that the time when the third user sent the random number to unlock exceeds the preset time range. Therefore, the first resource cannot be sent to the third user.

[0095] Specifically, the time interval between the current time and the publishing time of the first resource added by the first user is determined, and the time interval is compared with the lock time value in the time lock. If the time interval is greater than the lock time value, it is determined that the current time does not meet the time lock, and the hash lock is kept locked. Subsequently, a timeout message is sent to other users participating in the resource exchange to cancel the resource exchange operation, wherein the timeout message is used to notify other users that the resource exchange operation is canceled.

[0096] In another implementation scenario, if the first user receives a timeout message from a fourth user after successfully sending resources to a third user and receiving a second resource sent by a second user, that is, the time when the fourth user receives the random number does not satisfy the time lock, and the locked resource is not successfully sent out, then the fourth user will send a timeout message to other users participating in the resource exchange. For the first user who has completed the resource exchange, the first user needs to return the received second resource to the second user and receive the resource returned by the third user at the same time.

[0097] Specifically, if the timeout information of the fourth user is received, the second resource is extracted from the current blockchain, the second resource is returned to the second user, and the resource information of the current blockchain is updated, wherein the fourth user is any user among all the users participating in the resource exchange this time, and then, the first resource returned by the third user is received, the first resource is added to the current blockchain, and the resource information of the current blockchain is updated.

[0098] That is to say, once a user's exchange unlocking timeout occurs during the resource exchange process, other users need to cancel the previously completed operations after receiving the timeout information, that is, receive the delivered resources and return the received resources to achieve consistency in resource exchange. Either the exchange is completely successful and each user completes the delivery and acquisition of resources, or the exchange is completely canceled and the number of resources for each user remains the same as before the exchange.

[0099] In summary, taking four users participating in resource exchange as an example, the resource exchange process based on blockchain in this application is summarized as follows:

[0100] See also Figure 2BFirst, the first user generates an encrypted hash value and a lock time value, then determines the first resource for exchange, adds a hash lock with a hash value equal to the encrypted hash value and a time lock with a value of the lock time value to the first resource, and calls the smart contract to publish the added first resource. When the random number sent by the third user is received, the current time is determined. When the current time satisfies the time lock, the hash lock is decrypted and the first resource is sent to the third user. Next, the random number is sent to the second user so that the second user determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource. When the second user determines that the time satisfies the time lock corresponding to the second resource, the second resource returned by the second user is received, the second resource is stored in the current blockchain, and the resource information of the current blockchain is updated.

[0101] The method provided by the embodiment of the present application obtains an encrypted hash value and a lock time value, determines a first resource for exchange, adds a hash lock with a hash value equal to the encrypted hash value and a time lock with a value of the lock time value to the first resource, and calls a smart contract to publish the added first resource, so that when the first user receives a random number sent by a third user, the current time is determined, and when the current time satisfies the time lock, the hash lock is decrypted, and the first resource is sent to the third user. Next, the first user sends the random number to the second user, so that the second user determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource, and when the second user determines that the time satisfies the time lock corresponding to the second resource, the second resource returned by the second user is received, and the second resource is stored in the current blockchain, and the resource information of the current blockchain is updated. The resource is temporarily locked by adding a hash lock and a time lock to the resource to be exchanged, and the resource is actually exchanged only after the verification based on the random number is passed, so as to realize the resource exchange of multiple users, avoid the risk of resource exchange caused by asynchronous resource exchange, and the resource exchange security is high, and the efficiency of resource exchange is improved.

[0102] Further, as Figure 1 The specific implementation of the method, the embodiment of the present application provides a resource exchange device based on blockchain, such as Figure 3A As shown, the device includes: an acquisition module 301, a determination module 302, a decryption module 303, a first transmission module 304, and a receiving module 305.

[0103] The acquisition module 301 is used to acquire an encrypted hash value and a lock time value, wherein the encrypted hash value and the lock time value are generated by the first user or acquired by the first user from content published by the second user based on a smart contract, and the resource type of the resource held by the second user matches the resource type of the resource required by the first user;

[0104] The determination module 302 is used to determine a first resource for exchange, add a hash lock and a time lock to the first resource, and call a smart contract to publish the added first resource, where the value of the hash lock is equal to the encrypted hash value, and the value of the time lock is equal to the lock time value;

[0105] The decryption module 303 is used to determine the current time when receiving the random number sent by the third user, and when the current time satisfies the time lock, decrypt the hash lock and send the first resource to the third user;

[0106] The first transmission module 304 is used to send the random number to the second user, so that the second user determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource, the second resource is provided by the second user and the resource type matches the resource type of the resource required by the first user;

[0107] The receiving module 305 is used to receive the second resource returned by the second user, store the second resource in the current blockchain, and update the resource information of the current blockchain. The second resource is decrypted and returned when the second user determines that the time satisfies the time lock corresponding to the second resource.

[0108] In a specific application scenario, the acquisition module 301 is used to generate the random number, perform hash calculation on the random number, obtain the hash value of the random number as the encrypted hash value, and determine the number of users participating in the resource exchange and the operation time according to the multi-party atomic exchange protocol, and generate the lock time value based on the number of users and the operation time; or, read the content published by the second user based on the smart contract, obtain the encrypted hash value and the historical time lock set by the second user in the content, determine the historical lock time value of the historical time lock, reduce the historical lock time value, and obtain the lock time value.

[0109] In a specific application scenario, the determination module 302 is used to identify the resource exchange request of the first user and determine the target resource amount for exchange; extract the first resource whose resource amount meets the target resource amount from all the resources of the current blockchain; add the hash lock with the value of the encrypted hash value to the first resource, add the time lock with the value of the lock time value to the first resource, and lock the first resource.

[0110] In a specific application scenario, the decryption module 303 is used to determine the publishing time when the first user publishes the first resource after adding it, and to count the time interval between the current time and the publishing time; if the time interval is less than or equal to the lock time value, it is determined that the current time satisfies the time lock, and the hash lock is decrypted using the random number; the first resource is sent to the third user, the first resource is deleted from all resources in the current blockchain, and the resource information of the current blockchain is updated.

[0111] In specific application scenarios, such as Figure 3B As shown, the device further includes: a second transmission module 306.

[0112] The decryption module 303 is further configured to determine that the current time does not satisfy the time lock and maintain the locked state of the hash lock if the statistically obtained time interval is greater than the lock time value, and the time interval is the time interval between the publishing time when the first user publishes the first resource after adding and the current time;

[0113] The second transmission module 306 is used to send a timeout message to other users participating in the resource exchange to cancel the resource exchange operation. The timeout message is used to notify the other users that the resource exchange operation is canceled.

[0114] In a specific application scenario, the receiving module 305 is also used to determine that the resource exchange operation is cancelled if a timeout information returned by the second user is received; receive the first resource returned by the third user, add the first resource to the current blockchain, and update the resource information of the current blockchain.

[0115] In a specific application scenario, the receiving module 305 is also used to extract the second resource from the current blockchain if timeout information from the fourth user is received, return the second resource to the second user, and update the resource information of the current blockchain, where the fourth user is any user among all users participating in the resource exchange this time; receive the first resource returned by the third user, add the first resource to the current blockchain, and update the resource information of the current blockchain.

[0116] The device provided by the embodiment of the present application obtains an encrypted hash value and a lock time value, determines a first resource for exchange, adds a hash lock with a hash value equal to the encrypted hash value and a time lock with a value of the lock time value to the first resource, and calls a smart contract to publish the added first resource, so that when the first user receives a random number sent by a third user, the current time is determined, and when the current time satisfies the time lock, the hash lock is decrypted, and the first resource is sent to the third user. Next, the first user sends the random number to the second user, so that the second user determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource, and when the second user determines that the time satisfies the time lock corresponding to the second resource, the second resource returned by the second user is received, and the second resource is stored in the current blockchain, and the resource information of the current blockchain is updated. The resource is temporarily locked by adding a hash lock and a time lock to the resource to be exchanged, and the resource is actually exchanged only after the verification based on the random number is passed, so as to realize the resource exchange of multiple users, avoid the risk of resource exchange caused by asynchronous resource exchange, and the resource exchange security is high, and the efficiency of resource exchange is improved.

[0117] It should be noted that for other corresponding descriptions of the functional units involved in the resource exchange device based on blockchain provided in the embodiment of the present application, please refer to Figure 1 and FIG. 2A to FIG. 2B The corresponding description in will not be repeated here.

[0118] In an exemplary embodiment, see Figure 4 , and also provides a device, which includes a communication bus, a processor, a memory and a communication interface, and may also include an input and output interface and a display device, wherein each functional unit can communicate with each other through the bus. The memory stores a computer program, and the processor is used to execute the program stored in the memory and execute the resource exchange method based on blockchain in the above embodiment.

[0119] A computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of the resource exchange method based on blockchain.

[0120] Through the description of the above implementation methods, those skilled in the art can clearly understand that the present application can be implemented by hardware, or by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.), including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each implementation scenario of the present application.

[0121] Those skilled in the art will appreciate that the accompanying drawings are merely schematic diagrams of a preferred implementation scenario, and the modules or processes in the accompanying drawings are not necessarily required for implementing the present application.

[0122] Those skilled in the art will appreciate that the modules in the device in the implementation scenario can be distributed in the device in the implementation scenario according to the implementation scenario description, or can be changed accordingly and located in one or more devices different from the implementation scenario. The modules in the above implementation scenario can be combined into one module, or can be further split into multiple sub-modules.

[0123] The above application serial numbers are for description only and do not represent the advantages or disadvantages of the implementation scenarios.

[0124] The above disclosure only discloses several specific implementation scenarios of the present application. However, the present application is not limited thereto, and any changes that can be conceived by technicians in this field should fall within the scope of protection of the present application.

Claims

1. A resource exchange method based on blockchain, characterized in that: The method is applied to a blockchain where a first user is located, and the method includes: Obtaining an encrypted hash value and a lock time value, where the encrypted hash value and the lock time value are generated by the blockchain where the first user is located or obtained by the blockchain where the first user is located from content published by the blockchain where the second user is located based on a smart contract, and the resource type of the resources held by the blockchain where the second user is located matches the resource type of the resources required by the blockchain where the first user is located; Determine a first resource for exchange, add a hash lock and a time lock to the first resource, and call a smart contract to publish the added first resource, where the value of the hash lock is equal to the encrypted hash value, and the value of the time lock is equal to the lock time value; When receiving a random number sent by the blockchain where the third user is located, determining the current time, and when the current time satisfies the time lock, decrypting the hash lock, and sending the first resource to the blockchain where the third user is located; Sending the random number to the blockchain where the second user is located, so that the blockchain where the second user is located determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource, where the second resource is provided by the blockchain where the second user is located and the resource type matches the resource type of the resource required by the blockchain where the first user is located; Receive the second resource returned by the blockchain where the second user is located, store the second resource in the current blockchain, and update the resource information of the current blockchain, where the second resource is decrypted and returned when the blockchain where the second user is located determines that the time satisfies the time lock corresponding to the second resource.

2. The method according to claim 1, characterized in that The obtaining of the encrypted hash value and the lock time value includes: Generate the random number, perform hash calculation on the random number, obtain the hash value of the random number as the encrypted hash value, and determine the number of users participating in the resource exchange and the operation time according to the multi-party atomic exchange protocol, and generate the lock time value based on the number of users and the operation time; or, Read the content published by the blockchain where the second user is located based on the smart contract, obtain the encrypted hash value and the historical time lock set by the blockchain where the second user is located in the content, determine the historical lock time value of the historical time lock, reduce the historical lock time value, and obtain the lock time value.

3. The method according to claim 1, characterized in that The determining a first resource for exchange, and adding a hash lock and a time lock to the first resource, comprises: Identify the resource exchange request of the blockchain where the first user is located, and determine the target amount of resources for exchange; Extracting the first resource whose resource amount satisfies the target resource amount from all resources of the current blockchain; A hash lock whose value is the encrypted hash value is added to the first resource, a time lock whose value is the locking time value is added to the first resource, and the first resource is locked.

4. The method according to claim 1, characterized in that: When the current time satisfies the time lock, decrypting the hash lock and sending the first resource to the blockchain where the third user is located includes: Determine the release time of the first resource added by the blockchain where the first user is located, and count the time interval between the current time and the release time; If the time interval is less than or equal to the lock time value, it is determined that the current time satisfies the time lock, and the random number is used to decrypt the hash lock; The first resource is sent to the blockchain where the third user is located, the first resource is deleted from all resources of the current blockchain, and the resource information of the current blockchain is updated.

5. The method according to claim 1, characterized in that The method further comprises: If the statistically obtained time interval is greater than the lock time value, it is determined that the current time does not satisfy the time lock, and the hash lock is kept locked. The time interval is the time interval between the release time of the first resource added by the blockchain where the first user is located and the current time; A timeout message is sent to the blockchain where other users participating in the resource exchange are located to cancel the resource exchange operation. The timeout message is used to notify the blockchain where the other users are located that the resource exchange operation is canceled.

6. The method according to claim 1, characterized in that After sending the random number to the blockchain where the second user is located, the method further includes: If a timeout message is received from the blockchain where the second user is located, it is determined that the resource exchange operation is cancelled; Receive the first resource returned by the blockchain where the third user is located, add the first resource to the current blockchain, and update the resource information of the current blockchain.

7. The method according to claim 1, characterized in that After receiving the second resource returned by the blockchain where the second user is located, storing the second resource in the current blockchain, and updating the resource information of the current blockchain, the method further includes: If the timeout information of the blockchain where the fourth user is located is received, the second resource is extracted from the current blockchain, the second resource is returned to the blockchain where the second user is located, and the resource information of the current blockchain is updated, and the fourth user is any user among all the users participating in the resource exchange this time; Receive the first resource returned by the blockchain where the third user is located, add the first resource to the current blockchain, and update the resource information of the current blockchain.

8. A resource exchange device based on blockchain, characterized in that: The device is applied to a blockchain where a first user is located, and the device includes: an acquisition module, configured to acquire an encrypted hash value and a lock time value, wherein the encrypted hash value and the lock time value are generated by the blockchain where the first user is located or acquired by the blockchain where the first user is located from content published by the blockchain where the second user is located based on a smart contract, and the resource type of the resources held by the blockchain where the second user is located matches the resource type of the resources required by the blockchain where the first user is located; A determination module, used to determine a first resource for exchange, add a hash lock and a time lock to the first resource, and call a smart contract to publish the added first resource, wherein the value of the hash lock is equal to the encrypted hash value, and the value of the time lock is equal to the lock time value; a decryption module, configured to determine the current time when receiving a random number sent by the blockchain where the third user is located, and when the current time satisfies the time lock, decrypt the hash lock and send the first resource to the blockchain where the third user is located; A first transmission module, configured to send the random number to the blockchain where the second user is located, so that the blockchain where the second user is located determines the time when the random number is received and determines whether the time satisfies the time lock corresponding to the second resource, where the second resource is provided by the blockchain where the second user is located and the resource type matches the resource type of the resource required by the blockchain where the first user is located; The receiving module is used to receive the second resource returned by the blockchain where the second user is located, store the second resource in the current blockchain, and update the resource information of the current blockchain, wherein the second resource is decrypted and returned when the blockchain where the second user is located determines that the time satisfies the time lock corresponding to the second resource.

9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: 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 having a computer program stored thereon, 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

  • Block chain-based data processing method and device

    CN108520415A

  • Resource transfer method and device based on blockchain, node equipment and storage medium

    CN111401867A

  • Block chain asset coupon payment method and system based on legal digital currency

    CN112488684A