⚠️ This repository is a work in progress, it has not been audited or tested thoroughly. Use at your own risk.
ZK HTLC is an experimental implementation of the HTLC protocol built with SnarkyJS. Allowing users to trustlessly and atomicaly swap assets/tokens. It supports both the native MINA token, and (previously experimental) protocol built-in custom tokens. More about HTLC can be found here.
- Supports native MINA token
- Supports custom protocol built-in tokens
- Currently only supports Poseidon as the hashing function
💡 Both
HTLCPoseidonNative
andHTLCPoseidonExperimentalToken
can be used interchangably, depending on the tokens/assets being exchanged.
🔒 Initial setup
- Alice wants to atomically swap 10 MINA for 10 TOKEN with Bob
- Alice deploys an instance of
HTLCPoseidonNative
- Alice calls
HTLCPoseidonNative.lock(...)
, specifying Bob as the recipient and providing 10 MINA to the contract as the amount being locked. At this point Alice also chooses/generates a secret, and provides a hash of the secret (hashlock
) to the contract. - Bob deploys an instance of
HTLCPoseidonExperimentalToken
, with the respectivetokenId
representing the token being handled in this case. - Bob calls
HTLCPoseidonExperimentalToken.lock()
, specifying Alice as the recipient and providing 10 TOKEN to the contract as the amount being locked. Bob also provides the same hashlock as Alice did to hers instance.
🔓 Unlocking locked assets/tokens
- Alice calls Bob's
HTLCPoseidonExperimentalToken.unlock()
, providing the secret used to generate the hashlock used to deploy both of their contracts. This reveals the secret to Bob automatically and transfers the funds (10 TOKEN) from the contract to Alice. The secret is stored on-chain within the contract. - Bob calls Alice's
HTLCPoseidonNative.unlock()
, using the secret he obtained from Alice. This transfers the funds (10 MINA) from the contract to Bob. - Done!
📆 Edge cases
For a case when one of the participants fails to unlock the locked tokens in a given timelock period, the creator of each contract can refund their locked tokens using the .refund()
method.
Special thanks to Gregor, Jack and O1 Labs for their work on SnarkyJS.