<div dir="ltr"><div><div><div>Hello there,<br><br></div>I&#39;ve been thinking about how we could implement the idea some brilliant people had here : <a href="https://bitcointalk.org/index.php?topic=1134319.msg11966748#msg11966748">https://bitcointalk.org/index.php?topic=1134319.msg11966748#msg11966748</a>.<br><br></div><div>Basically it would allow to securely move funds in/out an existing channel.</div><div><br></div><div>The main use cases would include:<br></div><div>1) funding balanced channels<br></div><div>2) provide a way for merchants to seamlessy cash out their funds<br></div><div><br></div>I can&#39;t see why we couldn&#39;t use the existing htlc contracts to implement this, it is just another payment after all. But I am certainly missing something ;-)<br><br></div>Here is what it would look like with rusty-style protobuf messages (swap in/out are pretty much symmetrical) :<br><br><div>* SWAP IN *<br><br>// Whoever wishes to increase her balance in the channel sends this<br>swap_in_offer {<br>    // How much I would like to increase my balance<br>    required uint32 amount = 1;<br>    // The hash that will be used to do the atomic swap<br>    required sha256_hash r_hash = 2;<br>    // txid of the swap tx, should that be mutated the sender would only be screwing over himself<br>    required sha256_hash txid = 3;<br>}<br><br>swap_in_accept {<br>    // Maybe we should specify a mindepth here<br>}<br><br>swap_in_decline {<br>    oneof reason {<br>        // the other party needs to be able to compensate the swap in the channel<br>        bool cannot_swap_more_than_balance = 1;<br>    };<br>}<br><br>regular swap in flow :<br><br>Bob -&gt; Alice : swap_in_offer<br>Alice -&gt; Bob : swap_in_accept<br>(Bob publishes the swap tx, which pays &lt;amount&gt; BTC to (&lt;alice_final_key&gt; &amp; r) OR (refund &amp; TIMEOUT))<br>(tx reaches mindepth)<br>(alice makes sure the scriptpubkey of this tx indeeds pays her providing r)<br>Alice -&gt; Bob : update_add_htlc (with same amount and r_hash)<br>Bob -&gt; Alice : update_accept<br>Bob -&gt; Alice : update_fulfill_htlc  (reveals the r)<br>(Alice spends the swap tx)<br><br>----------------------------------------------------------<br><br>* SWAP OUT *<br><br>// Whoever wishes to move funds outside the channel sends this<br>swap_out_offer {<br>    // I would like to cash out this amount<br>    required uint32 amount = 1;<br>}<br><br>swap_out_accept {<br>    // The hash that will be used to do the atomic swap<br>    required sha256_hash r_hash = 1;<br>    // txid of the swap tx, should that be mutated the sender would only be screwing over himself<br>    required sha256_hash txid = 2;<br>}<br><br>swap_out_decline {<br>    oneof reason {<br>        // you can&#39;t get more than your balance !<br>        bool cannot_swap_more_than_balance = 1;<br>        // the other party needs to have corresponding funds available outside of the channel (meaning it has in fact twice the funds)<br>        bool funds_not_available = 2;<br>  };<br>}<br><br>regular swap out flow :<br><br>Bob -&gt; Alice : swap_out_offer<br>Alice -&gt; Bob : swap_out_accept<br>(Alice publishes the swap tx, which pays &lt;amount&gt; BTC to (&lt;bob_final_key&gt; &amp; r) OR (refund &amp; TIMEOUT))<br>(tx reached mindepth)<br>(bob makes sure the scriptpubkey of this tx indeeds pays him providing r)<br>Bob -&gt; Alice : update_add_htlc (with same amount and r_hash)<br>Alice -&gt; Bob : update_accept<br>Alice -&gt; Bob : update_fulfill_htlc (reveals the r)<br>(Bob spends the swap tx)<br><br><br></div><div>Cheers,<br><br></div><div>Pierre<br></div></div>