This contract implements hashed timelock contract which allows user to defer token transfer and process it only when valid preimage is provided.
It requires eosio.cdt v1.6.1 or higher. Compiled wasm and abi will be generated under ./build
.
$ ./build.sh
$ cleos set account permission eoshtlc active --add-code
$ cleos set contract eoshtlc build/eoshtlc
It requires deployed eosio.token contract or other contracts which owns transfer
action compatible with eosio.token
.
$ cleos push action eoshtlc newcontract '["alice", "sendeos2bob", "bob", {"quantity": "100.0000 EOS", "contract": "eosio.token"}, "32_BYTES_HEX_STRING_WHICH_YOU_WANT_TO_USE_AS_HASHLOCK", "2020-01-01T00:00:00"]' -p alice@active
$ cleos push action eosio.token transfer '["alice, "eoshtlc", "100.0000 EOS", "sendeos2bob"]' -p alice@active
You can check generated hashed timelock by the next command.
$ cleos get table eoshtlc alice htlc
bob
can withdraw token by providing valid preimage. (requires hashlock == sha256(preimage)
)
$ cleos push action eoshtlc withdraw '["alice", "sendeos2bob", "32_BYTES_HEX_STRING_PREIMAGE_WHOSE_HASH_VALUE_IS_SAME_TO_HASHLOCK"]' -p bob@active
When hashed timelock contract is expired (it passes timelock point), contract owner can cancel it. If hashed timelock contract was activated by proper token transfer, not processed token amount will be returned to contract owner
$ cleos push action eoshtlc cancel '["alice", "sendeos2bob"]' -p alice@active