
To illustrate why this is important, consider if the times were reversed. Refund her Bitcoin Cash from Mailbox “B”. Ifīob thinks Alice is going to flake out, he can refund his Bitcoin from Mailbox “A” long before Alice can
Qt bitcoin trader scripts code#
Game doesn’t really begin until she reveals her secret code so we make her refund much more cumbersome. In our story,Īlice’s time lock should be longer (say like a week) and Bob’s lock should be much shorter (like a day).īroadly speaking this is because Alice knows the hash lock secret and therefore has a major advantage. The length of time on each lock is important to ensure that the game can only be played fairly. If the swap never happens,Įveryone can take back the coins they started with after the time expires. To give each counterparty a chance to redeem the swap without interference. When the locks are put on the HTLC mailboxes, the time locks are set Reason we use time locks for this is to eliminate the possibility that one person ends up with both coins, The time locks allow both parties to refund themselves in case the swap does not succeed as planned. Bob needs a way to get his original Bitcoin BACK Her secret code, Bob can’t get the Bitcoin Cash he wanted. Let’s say Bob put his Bitcoin into Mailbox “A” but then never heard from Alice again. But what if things don’t go according to plan? Alice and Bob have just successfully swapped coins on two separate blockchains withoutĮver trusting each other or any third-party. Voila! Bob opens Bitcoin Cash Mailbox “B”Īnd the coin is his. Now Bob knows the code too! He goes over to Bitcoin Cash Mailbox “B”, puts in his Key “B”Īnd enters the secret code he JUST LEARNED from watching Alice. Meanwhile, Bob is watching Bitcoin Mailbox “A” and SEES ALICE ENTER With the key and the code entered, the mailbox is She puts in her Key “A” andĮnters the secret code she came up with earlier. Once the coins are both deposited, Alice goes over to Bitcoin Mailbox “A”. Her Bitcoin Cash into Mailbox “B” and Bob deposits his Bitcoin into Mailbox “A”. Alice knows the secret code but keeps it a secret for now.Īlice puts Hash Lock “A” on a Bitcoin mailbox, and Hash Lock “B” on a Bitcoin Cash mailbox. However, one requires her Key “A”īut the other requires Bob’s Key “B”. Swap the coins they have for the coins they want.Īlice makes two hash locks, that both require the EXACT SAME secret code. Let’s say Alice wants Bitcoin and Bob wants Bitcoin Cash. As we'll see later on, the time locks are only used as refunds in order Let's swap!įor now we’ll ignore the time locks, but just keep in mind they are there. The secret code, it is visible for the entire world to see, like the screen on a calculator. Hash locks require the right key but they ALSO require a SECRET CODE to be entered, like the PIN on an ATM.Įven if you have the key for this lock, you also need to enter the right code to open it. You have the key for this lock, you still need to wait until the time “expires” before you can open it. Time locks still require the right key but they ALSO require a certain amount of time to pass. They also use much fancier locks than usual. First of all, they have TWO locks, and EITHER lock can open the mailbox andĮxpose the coin. WhenĪlice is ready to spend the coin or move it somewhere else, she uses her Key “A” to open the lock and

He puts 0.5 BTC in the slot of Mailbox “A” which is locked by Lock “A”. When you want to receive Bitcoinįrom someone, you tell them which mailbox to put the coin into.īob decides to pay Alice. HTLCsīlockchains are like a stack of locked mailboxes with deposit slots. Machine at the core of the atomic swap is called the Hash Time-Locked Contract, or HTLC.

It's a bit ofĪ game: the rules can't be broken, but you still have to pay attention. In this guide we'llīe focusing on just Bitcoin and Bitcoin Cash, using the bcoinĪnd bcash libraries respectively. To hash a blob of data and check against a given hash.

For the scheme to work, bothĬhains need some kind of relative timelock operation (like OP_CHECKSEQUENCEVERIFY), as well as the ability To protect both users, there must be no scenario in which one person can control both coins at the same time.Ītomic swaps can be executed between many blockchains, but not all. We say the swaps are "atomic" because they must be Like trading dollars for pesos, it's a process in which two people can exchange one cryptocurrency forĪnother, but without trust or third-party moderation.
