Hash lock

in hash •  4 years ago 

Content

Suppose Alice wants to exchange her ADA for Bob's BTC, the specific application process is as follows:

1 Alice creates a random password s, and calculates the hash value h of the password, that is, h = hash(s). Alice sends this hash value h to Bob.

2 Alice and Bob jointly lock each other's assets through a smart contract (Alice locks it first, and Bob locks it again), and the following logic is implemented in the smart contract:

Condition 1: If anyone can provide a random value s'to the smart contract within H hours, once the contract verifies that hash(s') == h (when s'is equal to the original password s), then Bob's BTC will be automatically transferred Give it to Alice, otherwise send it back to Bob after the timeout.

Condition 2: If anyone sends the original password s to the smart contract within 2H hours, Alice's ADA will be automatically transferred to Bob, otherwise it will be transferred back to Alice.

3 The above condition 1 is formulated for Alice. In order to get Bob’s BTC, Alice will inevitably provide her own random password s to the smart contract before the timeout. The contract verification will surely pass smoothly and transfer Bob’s assets To Alice. While this transaction was successful, the original password s provided by Alice was also publicly broadcast and recorded on the blockchain. At this point, Bob can take the disclosed password s and send it to the smart contract. According to condition 2, he can obtain the ADA locked by Alice in the smart contract. In theory, Bob has 1H to 2H hours of sufficient time to complete the operation (depending on How quickly Alice can complete her operation). So everyone was happy.

Of course, Alice has the initiative in hash-locked transactions and is the more advantageous party. In more complicated transactions, such as the introduction of a fluctrate exchange rate, Alice can choose to wait until a more satisfactory exchange rate appears before triggering the exchange, and Bob can only passively accept it. Of course, Alice may also wait until the end to find that she missed the best ratio before, but in general, Alice has gained more options and took advantage of the transaction.

Cross-chain hash locking is usually paired with the "State channel" to make the transaction faster, thereby avoiding the above-mentioned Alice advantage problem.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!