diff --git a/iguana/iguana777.h b/iguana/iguana777.h index ac99dec08..14b8fd19c 100755 --- a/iguana/iguana777.h +++ b/iguana/iguana777.h @@ -992,7 +992,7 @@ char *iguana_pollrawtx(queue_t *Q,cJSON **vinsp,uint32_t rawtxtag,double expirat bits256 scrypt_blockhash(const void *input); bits256 iguana_calcblockhash(int32_t (*hashalgo)(uint8_t *blockhashp,uint8_t *serialized,int32_t len),uint8_t *serialized,int32_t len); uint32_t iguana_targetbits(struct iguana_info *coin,struct iguana_block *hwmchain,struct iguana_block *prev,struct iguana_block *prev2,int32_t PoSflag); -struct bitcoin_eventitem *instantdex_event(char *cmdstr,cJSON *newjson,cJSON *argjson,uint8_t *serdata,int32_t serdatalen); +struct bitcoin_eventitem *instantdex_event(char *cmdstr,cJSON *argjson,cJSON *newjson,uint8_t *serdata,int32_t serdatalen); void instantdex_eventfree(struct bitcoin_eventitem *ptr); extern int32_t HDRnet,netBLOCKS; diff --git a/iguana/iguana_instantdex.c b/iguana/iguana_instantdex.c index 73428cb01..c3ad9f903 100755 --- a/iguana/iguana_instantdex.c +++ b/iguana/iguana_instantdex.c @@ -1181,21 +1181,6 @@ struct bitcoin_swapinfo *bitcoin_swapinit(struct supernet_info *myinfo,struct ex return(swap); } -struct bitcoin_eventitem *instantdex_event(char *cmdstr,cJSON *argjson,cJSON *newjson,uint8_t *serdata,int32_t serdatalen) -{ - struct bitcoin_eventitem *ptr; - ptr = calloc(1,sizeof(*ptr) + serdatalen); - strcpy(ptr->cmd,cmdstr); - ptr->newjson = jduplicate(newjson); - ptr->argjson = jduplicate(argjson); - if ( serdatalen != 0 ) - { - memcpy(ptr->serdata,serdata,serdatalen); - ptr->serdatalen = serdatalen; - } - return(ptr); -} - char *instantdex_checkoffer(struct supernet_info *myinfo,int32_t *addedp,uint64_t *txidp,struct exchange_info *exchange,struct instantdex_accept *ap,cJSON *argjson) { struct instantdex_accept *otherap,*tmp; struct bitcoin_swapinfo *swap; cJSON *newjson; int32_t isbob = 0; char *retstr = 0; diff --git a/iguana/swaps/iguana_BTCswap.c b/iguana/swaps/iguana_BTCswap.c index 453bff995..ad9b9cae7 100755 --- a/iguana/swaps/iguana_BTCswap.c +++ b/iguana/swaps/iguana_BTCswap.c @@ -15,34 +15,34 @@ #include "../exchanges/bitcoin.h" /* https://bitcointalk.org/index.php?topic=1340621.msg13828271#msg13828271 - https://bitcointalk.org/index.php?topic=1364951 -Tier Nolan's approach is followed with the following changes: - a) instead of cutting 1000 keypairs, only INSTANTDEX_DECKSIZE are a - b) instead of sending the entire 256 bits, it is truncated to 64 bits. With odds of collision being so low, it is dwarfed by the ~0.1% insurance factor. - c) D is set to 100x the insurance rate of 1/777 12.87% + BTC amount - d) insurance is added to Bob's payment, which is after the deposit and bailin - e) BEFORE Bob broadcasts deposit, Alice broadcasts BTC denominated fee in cltv so if trade isnt done fee is reclaimed -*/ + https://bitcointalk.org/index.php?topic=1364951 + Tier Nolan's approach is followed with the following changes: + a) instead of cutting 1000 keypairs, only INSTANTDEX_DECKSIZE are a + b) instead of sending the entire 256 bits, it is truncated to 64 bits. With odds of collision being so low, it is dwarfed by the ~0.1% insurance factor. + c) D is set to 100x the insurance rate of 1/777 12.87% + BTC amount + d) insurance is added to Bob's payment, which is after the deposit and bailin + e) BEFORE Bob broadcasts deposit, Alice broadcasts BTC denominated fee in cltv so if trade isnt done fee is reclaimed + */ /* -both fees are standard payments: OP_DUP OP_HASH160 FEE_RMD160 OP_EQUALVERIFY OP_CHECKSIG - -Alice altpayment: OP_2 OP_2 OP_CHECKMULTISIG - + both fees are standard payments: OP_DUP OP_HASH160 FEE_RMD160 OP_EQUALVERIFY OP_CHECKSIG + + Alice altpayment: OP_2 OP_2 OP_CHECKMULTISIG + Bob deposit: if ( (swap->deposit= instantdex_bobtx(myinfo,coinbtc,&swap->deposittxid,swap->otherpubs[0],swap->mypubs[0],swap->privkeys[swap->choosei],reftime,swap->satoshis[1],1)) != 0 ) -OP_IF - OP_CLTV OP_DROP OP_CHECKSIG -OP_ELSE - OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG -OP_ENDIF + OP_IF + OP_CLTV OP_DROP OP_CHECKSIG + OP_ELSE + OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG + OP_ENDIF Bob paytx: if ( (swap->payment= instantdex_bobtx(myinfo,coinbtc,&swap->deposittxid,swap->mypubs[1],swap->otherpubs[0],swap->privkeys[swap->otherschoosei],reftime,swap->satoshis[1],0)) != 0 ) -OP_IF - OP_CLTV OP_DROP OP_CHECKSIG -OP_ELSE - OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG -OP_ENDIF -*/ + OP_IF + OP_CLTV OP_DROP OP_CHECKSIG + OP_ELSE + OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG + OP_ENDIF + */ int32_t instantdex_bobscript(uint8_t *script,int32_t n,int32_t *secretstartp,uint32_t locktime,bits256 cltvpub,uint8_t secret160[20],bits256 destpub) { @@ -50,13 +50,13 @@ int32_t instantdex_bobscript(uint8_t *script,int32_t n,int32_t *secretstartp,uin memcpy(pubkeyA+1,cltvpub.bytes,sizeof(cltvpub)), pubkeyA[0] = 0x02; memcpy(pubkeyB+1,destpub.bytes,sizeof(destpub)), pubkeyB[0] = 0x03; script[n++] = SCRIPT_OP_IF; - n = bitcoin_checklocktimeverify(script,n,locktime); - n = bitcoin_pubkeyspend(script,n,pubkeyA); + n = bitcoin_checklocktimeverify(script,n,locktime); + n = bitcoin_pubkeyspend(script,n,pubkeyA); script[n++] = SCRIPT_OP_ELSE; if ( secretstartp != 0 ) *secretstartp = n + 2; - n = bitcoin_revealsecret160(script,n,secret160); - n = bitcoin_pubkeyspend(script,n,pubkeyB); + n = bitcoin_revealsecret160(script,n,secret160); + n = bitcoin_pubkeyspend(script,n,pubkeyB); script[n++] = SCRIPT_OP_ENDIF; return(n); } @@ -670,6 +670,7 @@ cJSON *BOB_waitfeefunc(struct supernet_info *myinfo,struct exchange_info *exchan coinbtc = iguana_coinfind("BTC"); *serdatap = 0, *serdatalenp = 0; strcpy(swap->waitfortx,"fee"); + printf("BOB wait other.%p\n",swap->otherfee); if ( coinbtc != 0 && swap->otherfee != 0 )//swap->deposit == 0 && (retstr= BTC_txconfirmed(myinfo,coinbtc,swap,newjson,swap->otherfee->txid,&swap->otherfee->numconfirms,"feefound",0)) != 0 ) { retstr = swap->otherfee->txbytes; @@ -745,6 +746,7 @@ cJSON *ALICE_waitfeefunc(struct supernet_info *myinfo,struct exchange_info *exch coinbtc = iguana_coinfind("BTC"); *serdatap = 0, *serdatalenp = 0; strcpy(swap->waitfortx,"fee"); + printf("ALICE wait other.%p\n",swap->otherfee); if ( coinbtc != 0 && swap->otherfee != 0 )//&& (retstr= BTC_txconfirmed(myinfo,coinbtc,swap,newjson,swap->otherfee->txid,&swap->otherfee->numconfirms,"feefound",0)) != 0 ) { retstr = swap->otherfee->txbytes; @@ -893,7 +895,7 @@ struct instantdex_stateinfo *BTC_initFSM(int32_t *n) s = instantdex_statecreate(s,n,"BOB_depclaimed",BOB_reclaimfunc,0,0,0,0); // deposit back instantdex_addevent(s,*n,"BOB_depclaimed","brefound","poll","BTC_cleanup"); instantdex_addevent(s,*n,"BOB_depclaimed","poll","poll","BOB_depclaimed"); - + s = instantdex_statecreate(s,n,"BOB_claimedalt",BOB_claimaltfunc,0,0,0,0); instantdex_addevent(s,*n,"BOB_claimedalt","bclfound","poll","BOB_depclaimed"); instantdex_addevent(s,*n,"BOB_claimedalt","poll","poll","BOB_claimedalt"); @@ -906,12 +908,12 @@ struct instantdex_stateinfo *BTC_initFSM(int32_t *n) s = instantdex_statecreate(s,n,"BOB_reclaimed",BOB_reclaimfunc,0,0,0,0); // deposit back instantdex_addevent(s,*n,"BOB_reclaimed","brefound","poll","BOB_feereclaimed"); instantdex_addevent(s,*n,"BOB_reclaimed","poll","poll","BOB_reclaimed"); - + // if things go wrong, alice reclaims her altpayment or claims the deposit and then fee - s = instantdex_statecreate(s,n,"ALICE_feereclaimed",ALICE_feereclaimfunc,0,0,0,0); + s = instantdex_statecreate(s,n,"ALICE_feereclaimed",ALICE_feereclaimfunc,0,0,0,0); instantdex_addevent(s,*n,"ALICE_feereclaimed","afrfound","poll","BTC_cleanup"); instantdex_addevent(s,*n,"ALICE_feereclaimed","poll","poll","ALICE_feereclaimed"); - + s = instantdex_statecreate(s,n,"ALICE_reclaimed",ALICE_reclaimfunc,0,0,0,0); // altpayment instantdex_addevent(s,*n,"ALICE_reclaimed","arefound","poll","ALICE_feereclaimed"); instantdex_addevent(s,*n,"ALICE_reclaimed","poll","poll","ALICE_reclaimed"); @@ -934,7 +936,7 @@ struct instantdex_stateinfo *BTC_initFSM(int32_t *n) s = instantdex_statecreate(s,n,"ALICE_sentalt",ALICE_waitBTCpaytxfunc,0,"ALICE_reclaimed",0,0); s = instantdex_statecreate(s,n,"ALICE_waitconfirms",ALICE_waitconfirmsfunc,0,"ALICE_reclaimed",0,0); s = instantdex_statecreate(s,n,"ALICE_checkbobreclaim",ALICE_checkbobreclaimfunc,0,"ALICE_reclaimed",0,0); - + if ( 0 ) // following are implicit states and events handled externally to setup datastructures { instantdex_addevent(s,*n,"BOB_idle","usrorder","BTCoffer","BOB_sentoffer"); // send deck @@ -944,7 +946,7 @@ struct instantdex_stateinfo *BTC_initFSM(int32_t *n) instantdex_addevent(s,*n,"BOB_idle","BTCoffer","BTCdeckC","BOB_gotoffer"); // send deck + Chose s = instantdex_statecreate(s,n,"ALICE_idle",BTC_idlerecvfunc,0,"BTC_cleanup",0,1); instantdex_addevent(s,*n,"ALICE_idle","BTCoffer","BTCdeckC","ALICE_gotoffer"); - + // after offer is sent, wait for other side to choose and sent their deck, then send privs s = instantdex_statecreate(s,n,"BOB_sentoffer",BTC_waitdeckCfunc,0,"BTC_cleanup",0,1); s = instantdex_statecreate(s,n,"ALICE_sentoffer",BTC_waitdeckCfunc,0,"BTC_cleanup",0,1); @@ -962,18 +964,18 @@ struct instantdex_stateinfo *BTC_initFSM(int32_t *n) instantdex_addevent(s,*n,"BOB_sentprivs","BTCprivs","poll","BOB_waitfee"); instantdex_addevent(s,*n,"BOB_sentprivs","BTCdeckC","BTCprivs","BOB_sentprivs"); instantdex_addevent(s,*n,"BOB_sentprivs","poll","BTCdeckC","BOB_sentprivs"); - + s = instantdex_statecreate(s,n,"ALICE_sentprivs",BTC_waitprivsfunc,0,"BTC_cleanup",0,0); instantdex_addevent(s,*n,"ALICE_sentprivs","BTCprivs","poll","Alice_waitfee"); instantdex_addevent(s,*n,"ALICE_sentprivs","BTCdeckC","BTCprivs","Alice_waitfee"); instantdex_addevent(s,*n,"ALICE_sentprivs","poll","BTCdeckC","ALICE_sentprivs"); - + // [BLOCKING: fee] Bob waits for fee and sends deposit when it appears s = instantdex_statecreate(s,n,"BOB_waitfee",BOB_waitfeefunc,0,"BTC_cleanup",0,0); instantdex_addevent(s,*n,"BOB_waitfee","feefound","BTCdeptx","BOB_sentdeposit"); instantdex_addevent(s,*n,"BOB_waitfee","BTCdeckC","BTCprivs","BOB_waitfee"); instantdex_addevent(s,*n,"BOB_waitfee","BTCprivs","poll","BOB_waitfee"); - + // [BLOCKING: fee and deposit] Alice waits for fee and then waits for deposit to confirm and sends altpayment s = instantdex_statecreate(s,n,"Alice_waitfee",ALICE_waitfeefunc,0,"BTC_cleanup",0,0); instantdex_addevent(s,*n,"Alice_waitfee","feefound","poll","ALICE_waitdeposit"); @@ -983,11 +985,11 @@ struct instantdex_stateinfo *BTC_initFSM(int32_t *n) s = instantdex_statecreate(s,n,"ALICE_waitdeposit",ALICE_waitdepositfunc,0,"BTC_cleanup",0,0); instantdex_addevent(s,*n,"ALICE_waitdeposit","depfound","BTCalttx","ALICE_sentalt"); instantdex_addevent(s,*n,"ALICE_waitdeposit","poll","poll","ALICE_waitdeposit"); - + // [BLOCKING: BTCalttx and altfound] now Bob's turn to make sure altpayment is confirmed and send real payment s = instantdex_statecreate(s,n,"BOB_sentdeposit",BOB_waitBTCalttxfunc,0,"BOB_reclaimed",0,0); instantdex_addevent(s,*n,"BOB_sentdeposit","BTCalttx","poll","BOB_altconfirm"); - + s = instantdex_statecreate(s,n,"BOB_altconfirm",BOB_waitaltconfirmfunc,0,"BOB_reclaimed",0,0); instantdex_addevent(s,*n,"BOB_altconfirm","altfound","BTCpaytx","BOB_sentpayment"); instantdex_addevent(s,*n,"BOB_altconfirm","poll","poll","BOB_altconfirm"); @@ -1017,6 +1019,21 @@ struct instantdex_stateinfo *BTC_initFSM(int32_t *n) return(s); } +struct bitcoin_eventitem *instantdex_event(char *cmdstr,cJSON *argjson,cJSON *newjson,uint8_t *serdata,int32_t serdatalen) +{ + struct bitcoin_eventitem *ptr; + ptr = calloc(1,sizeof(*ptr) + serdatalen); + strcpy(ptr->cmd,cmdstr); + ptr->newjson = jduplicate(newjson); + ptr->argjson = jduplicate(argjson); + if ( serdatalen != 0 ) + { + memcpy(ptr->serdata,serdata,serdatalen); + ptr->serdatalen = serdatalen; + } + return(ptr); +} + void instantdex_eventfree(struct bitcoin_eventitem *ptr) { if ( ptr != 0 )