Function createUnlockHTLC
Creates an unlock for the HTLC generated with createLockHTLC
.
Parameters
Name | Description |
---|---|
sig | the signature |
secret | either the preimage, or an invalid value to switch to the ELSE branch of the HTLC lock script |
Returns
an HTLC unlock script
Example
import agora .script .Engine;
import std .stdio;
const Transaction bad_tx = Transaction([Input .init], [Output .init], Height(99));
const Transaction tx = Transaction([Input .init], [Output .init], Height(100));
const Hash wrong_secret = hashFull(99);
const Hash secret = hashFull(42);
auto hash = hashFull(secret);
auto send_kp = Pair .random();
auto recv_kp = Pair .random();
auto lock_height = Height(100);
scope engine = new Engine();
auto lock_script = createLockHTLC(hash, lock_height, send_kp .V, recv_kp .V);
auto send_sig = sign(send_kp, tx .getChallenge());
auto recv_sig = sign(recv_kp, tx .getChallenge());
auto bad_tx_send_sig = sign(send_kp, bad_tx .getChallenge());
assert(engine .execute(
lock_script,
createUnlockHTLC(recv_sig, secret), tx, Input .init)
is null); // receiver can unlock with secret + signature
assert(engine .execute(
lock_script,
createUnlockHTLC(send_sig, secret), tx, Input .init)
== "Script failed"); // wrong signature (expected receiver sig)
assert(engine .execute(
lock_script,
createUnlockHTLC(send_sig, wrong_secret), tx, Input .init)
is null); // sender can unlock with ELSE branch + timelock + signature
assert(engine .execute(
lock_script,
createUnlockHTLC(recv_sig, wrong_secret), tx, Input .init)
== "Script failed"); // wrong signature (expected sender key)
assert(engine .execute(
lock_script,
createUnlockHTLC(bad_tx_send_sig, wrong_secret), bad_tx, Input .init)
== "VERIFY_LOCK_HEIGHT height lock of transaction is too low"); // timelock is wrong