In the above protocol, I missed one critical point: After step #6, Bob is able to make TX
btc void by double-spending his other input(s) with higher fees, then he can simply wait for 7 days and take the premium value (x
BTC) without committing to the exchange process anymore.
To mitigate this bug, I'd Improve the protocol by requiring Bob to lock his funds (v
BTC+fee) for a very short period of time (like 12 blocks) in another multi-sig address and using this transaction as the second input to TX
btc.
So the new version would be:
0- Bob, asks x
BTC for the premium of a put option, exchanging his v bitcoins with w Xcoin within 48 hours where x << v. Alice accepts the trade:
1- Alice generates a (P2WSH) TX
p sending (in its first output) x
BTC to the hash of following script:
IF
<7 days> CHECKLOCKTIMEVERIFY DROP
<Bob'e Pubkey> CHECKSIGVERIFY
ELSE
2 <Alice's Pubkey> <Bob'e Pubkey> 2 CHECKMULTISIG
ENDIF
Basically it locks the value of put option (x
BTC) for a week meanwhile it is spendable with a 2 of 2 signed transaction.
2- Bob generates a (P2WSH/P2SH) TX
btc0 sending (in its first output) (v+fees)
BTC to the hash of following script:
IF
<12 blocks> CHECKSEQUENCEVERIFY DROP
<Bob'e Pubkey> CHECKSIGVERIFY
ELSE
2 <Alice's Pubkey> <Bob'e Pubkey> 2 CHECKMULTISIG
ENDIF
It locks Bob's coins (v
BTC) for a very short period of time meanwhile it is spendable with a 2 of 2 signed transaction.
3- Alice, gives Bob an unsigned version of TX
p and the redeem script behind it, letting him compute ID(TX
p). Note that the segregation of witness data (Alice's signature) plays a vital role here for Bob being able to compute the transaction Id without having an opportunity to seize the funds and getting away without further commitment to the process.
4- Alice generates and publishes TX
Xcoin on the Alt-chain paying w to Bob's XCoin address through an HTLC contract locked on SHA256(u) for 72 hours
5- Bob genrates TX
btc which is:
a) The first input spends TX
p (x
BTC)
b) The second input spends TX
btc0 (fee
BTC + v
BTC)
c) The first output is Bob'e receivig address and gets x
BTC (the premium)
d) The second output is Alice's btc receiving address and gets v
BTC, it is HTLC locked for 48 hours on SHA256(u)
6- Bob provides his signatures for TX
btc sending it to Alice to provide her signature for the 2-of-2 requirements of both inputs.
7- Giving a heads-up to Alice, Bob publishes TX
btc0 and remains online scanning the network for a while
8- In the short period of time after TX
btc0 is published by Bob and confirmed by the blockchain, Alice has a decision to make:
1) She could choose to ignore the whole process, waiting for the 72 hours lock time on TX
Xcoin to become void, redeeming her Xcoins, while her x
BTC payment is not (and won't be ever) published.
XOR
2) She can follow the trade by publishing both TX
p and TX
btc .
In either case, Bob is safe as either Alice is not offered any premium at all or she has paid for it. He could decide to prevent TX
btc from being confirmed, if Alice hesitates to publish it for too long, by double-spending its second input once it becomes unlocked after 1-2 hours.
9- Given the process is not interrupted by any party and all of the transactions (three bitcoin txns and one Xcoin txn) are properly published and confirmed, Alice has another decision to make within 48 hours:
1) providing hash(u) while spending her input from TX
btc.
2) doing nothing and waiting for locks to expire.
In case 1, Bob claims w Xcoins in exchange for his v
BTC which is already claimed by Alice.
In case 2, Bob reclaims his bitcoins after 48 hours.
In both cases, he receives x
BTC for the premium he has provided to Alice.