From 3d385ca7ef2be00f39461964b28a7b6c2f48bf08 Mon Sep 17 00:00:00 2001 From: jl777 Date: Wed, 18 May 2016 01:37:15 -0500 Subject: [PATCH] test --- iguana/SuperNET_category.c | 6 +-- iguana/iguana777.h | 1 + iguana/iguana_instantdex.c | 4 +- iguana/iguana_payments.c | 59 +++++++++++++--------- iguana/iguana_wallet.c | 31 ++++++------ iguana/swaps/iguana_BTCswap.c | 93 +++++++++++++++++++++++------------ 6 files changed, 120 insertions(+), 74 deletions(-) diff --git a/iguana/SuperNET_category.c b/iguana/SuperNET_category.c index 2a2d32375..511323c7a 100755 --- a/iguana/SuperNET_category.c +++ b/iguana/SuperNET_category.c @@ -207,7 +207,7 @@ char *bitcoin_hexmsg(struct supernet_info *myinfo,struct category_info *cat,void char *method="",*agent="",*retstr = 0; int32_t i,j; cJSON *json,*valsobj; struct iguana_info *coin=0; struct iguana_peer *addr; if ( (json= cJSON_Parse(ptr)) != 0 ) { - //printf("bitcoinprocess.(%s)\n",jprint(json,0)); + printf("bitcoinprocess.(%s)\n",jprint(json,0)); agent = jstr(json,"agent"); method = jstr(json,"method"); valsobj = jobj(json,"vals"); @@ -231,7 +231,7 @@ char *bitcoin_hexmsg(struct supernet_info *myinfo,struct category_info *cat,void } if ( retstr == 0 ) return(0); - //printf("RELAY will return.(%s)\n",retstr); + printf("RELAY will return.(%s)\n",retstr); for (j=0; jmyfee = instantdex_feetx(myinfo,&swap->mine,swap,coinbtc); //queue_enqueue("acceptableQ",&exchange->acceptableQ,&swap->DL,0); instantdex_statemachineadd(exchange,swap); if ( (retstr= instantdex_choosei(swap,newjson,argjson,serdata,serdatalen)) != 0 ) diff --git a/iguana/iguana_payments.c b/iguana/iguana_payments.c index 122f9aa68..8cdecb561 100755 --- a/iguana/iguana_payments.c +++ b/iguana/iguana_payments.c @@ -393,9 +393,36 @@ cJSON *iguana_requestjson(struct supernet_info *myinfo,cJSON *hexjson) return(retjson); } +char *iguana_pollrawtx(queue_t *Q,cJSON **vinsp,uint32_t rawtxtag,double expiration) +{ + struct rawtx_queue *ptr; char *rawtx; + while ( OS_milliseconds() < expiration ) + { + if ( (ptr= queue_dequeue(Q,0)) != 0 ) + { + if ( rawtxtag == ptr->rawtxtag ) + { + rawtx = clonestr(ptr->rawtx); + //printf("got RAWTX.(%s)\n",rawtx); + if ( vinsp != 0 ) + *vinsp = ptr->vins; + else free_json(ptr->vins); + free(ptr); + return(rawtx); + } + else + { + free_json(ptr->vins); + free(ptr); + } + } + } + return(0); +} + char *iguana_request_andwait(struct supernet_info *myinfo,queue_t *Q,cJSON **vinsp,cJSON *reqjson,uint32_t rawtxtag,int32_t timeout) { - struct rawtx_queue *ptr; int32_t i,j,n; struct iguana_peer *addr; double expiration; struct iguana_info *coin; char *rawtx,*reqstr; cJSON *tmpjson; + struct rawtx_queue *ptr; int32_t i,j,n; struct iguana_peer *addr; double expiration; struct iguana_info *coin; char *reqstr; cJSON *tmpjson; if ( vinsp != 0 ) *vinsp = 0; tmpjson = iguana_requestjson(myinfo,reqjson); @@ -421,28 +448,7 @@ char *iguana_request_andwait(struct supernet_info *myinfo,queue_t *Q,cJSON **vin } } } - while ( OS_milliseconds() < expiration ) - { - if ( (ptr= queue_dequeue(Q,0)) != 0 ) - { - if ( rawtxtag == ptr->rawtxtag ) - { - rawtx = clonestr(ptr->rawtx); - //printf("got RAWTX.(%s)\n",rawtx); - if ( vinsp != 0 ) - *vinsp = ptr->vins; - else free_json(ptr->vins); - free(ptr); - return(rawtx); - } - else - { - free_json(ptr->vins); - free(ptr); - } - } - } - return(0); + return(iguana_pollrawtx(Q,vinsp,rawtxtag,expiration)); } char *iguana_rawtxissue(struct supernet_info *myinfo,uint32_t rawtxtag,char *symbol,cJSON **vinsp,uint32_t locktime,uint64_t satoshis,char *changeaddr,uint64_t txfee,cJSON *addresses,int32_t minconf,char *spendscriptstr,int32_t timeout) @@ -652,7 +658,12 @@ STRING_ARRAY_OBJ_STRING(iguana,rawtx,changeaddr,addresses,vals,spendscriptstr) jaddstr(valsobj,"coin",symbol); jadd(hexjson,"vals",valsobj); retjson = iguana_requestjson(myinfo,hexjson); - } else jaddstr(retjson,"result",rawtx); + } + else + { + jaddstr(retjson,"result",rawtx); + jadd(retjson,"vins",vins); + } free(rawtx); } else jaddstr(retjson,"error","couldnt create rawtx"); } diff --git a/iguana/iguana_wallet.c b/iguana/iguana_wallet.c index 4f6065da4..65b24fdaf 100755 --- a/iguana/iguana_wallet.c +++ b/iguana/iguana_wallet.c @@ -268,7 +268,7 @@ uint8_t *iguana_walletrmds(struct supernet_info *myinfo,struct iguana_info *coin cJSON *iguana_getaddressesbyaccount(struct supernet_info *myinfo,struct iguana_info *coin,char *account) { - struct iguana_waccount *subset,*tmp; struct iguana_waddress *waddr,*tmp2; cJSON *retjson,*array; + struct iguana_waccount *subset,*tmp; char coinaddr[64]; struct iguana_waddress *waddr,*tmp2; cJSON *retjson,*array; retjson = cJSON_CreateObject(); array = cJSON_CreateArray(); if ( account == 0 ) @@ -279,7 +279,8 @@ cJSON *iguana_getaddressesbyaccount(struct supernet_info *myinfo,struct iguana_i { HASH_ITER(hh,subset->waddr,waddr,tmp2) { - jaddistr(array,waddr->coinaddr); + bitcoin_address(coinaddr,coin->chain->pubtype,waddr->rmd160,20); + jaddistr(array,coinaddr); } } else jaddstr(retjson,"result","cant find account"); } @@ -289,7 +290,8 @@ cJSON *iguana_getaddressesbyaccount(struct supernet_info *myinfo,struct iguana_i { HASH_ITER(hh,subset->waddr,waddr,tmp2) { - jaddistr(array,waddr->coinaddr); + bitcoin_address(coinaddr,coin->chain->pubtype,waddr->rmd160,20); + jaddistr(array,coinaddr); } } } @@ -322,12 +324,13 @@ int32_t iguana_addressvalidate(struct iguana_info *coin,uint8_t *addrtypep,char } } -cJSON *iguana_waddressjson(cJSON *item,struct iguana_waddress *waddr) +cJSON *iguana_waddressjson(struct iguana_info *coin,cJSON *item,struct iguana_waddress *waddr) { - char str[256],redeemScript[4096]; + char str[256],redeemScript[4096],coinaddr[64]; if ( item == 0 ) item = cJSON_CreateObject(); - jaddstr(item,"address",waddr->coinaddr); + bitcoin_address(coinaddr,coin->chain->pubtype,waddr->rmd160,20); + jaddstr(item,"address",coinaddr); //jaddstr(item,"privkey",bits256_str(str,waddr->privkey)); //jaddstr(item,"wif",waddr->wifstr); init_hexbytes_noT(str,waddr->rmd160,20); @@ -375,7 +378,7 @@ char *getaccount(struct supernet_info *myinfo,struct iguana_info *coin,char *coi return(clonestr("{\"result\":\"no account for address\"}")); if ( wacct != 0 ) { - retjson = iguana_waddressjson(0,waddr); + retjson = iguana_waddressjson(coin,0,waddr); jaddstr(retjson,"account",wacct->account); jaddstr(retjson,"result","success"); return(jprint(retjson,1)); @@ -432,7 +435,7 @@ int32_t iguana_payloadupdate(struct supernet_info *myinfo,struct iguana_info *co if ( waddr != 0 && (valuestr= iguana_walletvalue(valuebuf,waddr)) != 0 ) { init_hexbytes_noT(rmdstr,waddr->rmd160,20); - if ( (accountobj= jobj(payload,account)) != 0 && jobj(accountobj,waddr->coinaddr) != 0 && jobj(accountobj,rmdstr) != 0 ) + if ( (accountobj= jobj(payload,account)) != 0 && jobj(accountobj,rmdstr) != 0 ) { free_json(retjson); free_json(payload); @@ -497,7 +500,7 @@ cJSON *iguana_walletadd(struct supernet_info *myinfo,struct iguana_waddress **wa } else { - retjson = iguana_waddressjson(retjson,waddr); + retjson = iguana_waddressjson(coin,retjson,waddr); jaddstr(retjson,"account",account); jaddstr(retjson,"result","success"); } @@ -686,7 +689,7 @@ uint8_t iguana_waddrvalidate(struct supernet_info *myinfo,struct iguana_info *co cJSON *iguana_walletiterate(struct supernet_info *myinfo,struct iguana_info *coin,int32_t flag,cJSON *array,int32_t *goodp,int32_t *badp,int32_t *errors) { - struct iguana_waccount *wacct,*tmp; struct iguana_waddress *waddr=0,*tmp2; uint8_t errorflags; int32_t i,good=0,bad=0,_errors[8]; cJSON *item; + struct iguana_waccount *wacct,*tmp; struct iguana_waddress *waddr=0,*tmp2; uint8_t errorflags; int32_t i,good=0,bad=0,_errors[8]; cJSON *item; char coinaddr[64]; if ( errors == 0 ) errors = _errors; HASH_ITER(hh,myinfo->wallet,wacct,tmp) @@ -718,7 +721,8 @@ cJSON *iguana_walletiterate(struct supernet_info *myinfo,struct iguana_info *coi bad++; if ( array != 0 && (item= cJSON_CreateObject()) != 0 ) { - jaddnum(item,waddr->coinaddr,errorflags); + bitcoin_address(coinaddr,coin->chain->pubtype,waddr->rmd160,20); + jaddnum(item,coinaddr,errorflags); jaddi(array,item); } } else good++; @@ -909,7 +913,6 @@ STRING_ARG(bitcoinrpc,validateaddress,address) if ( iguana_addressvalidate(coin,&addrtype,address) < 0 ) return(clonestr("{\"error\":\"invalid coin address\"}")); bitcoin_addr2rmd160(&addrtype,rmd160,address); - retjson = cJSON_CreateObject(); jaddstr(retjson,"result","success"); jaddnum(retjson,"addrtype",addrtype); @@ -1020,7 +1023,7 @@ struct iguana_waddress *iguana_getaccountaddress(struct supernet_info *myinfo,st } if ( waddr != 0 ) { - strcpy(coinaddr,waddr->coinaddr); + bitcoin_address(coinaddr,coin->chain->pubtype,waddr->rmd160,20); return(waddr); } return(0); @@ -1037,7 +1040,7 @@ STRING_ARG(bitcoinrpc,getaccountaddress,account) if ( account != 0 && account[0] != 0 ) { if ( (waddr= iguana_getaccountaddress(myinfo,coin,json,remoteaddr,coinaddr,account)) != 0 ) - retjson = iguana_waddressjson(0,waddr); + retjson = iguana_waddressjson(coin,0,waddr); else return(clonestr("{\"error\":\"couldnt create address\"}")); jaddstr(retjson,"account",account); jaddstr(retjson,"result","success"); diff --git a/iguana/swaps/iguana_BTCswap.c b/iguana/swaps/iguana_BTCswap.c index dbc0d85b6..0c6a7e130 100755 --- a/iguana/swaps/iguana_BTCswap.c +++ b/iguana/swaps/iguana_BTCswap.c @@ -107,48 +107,81 @@ void iguana_addinputs(struct iguana_info *coin,struct bitcoin_spend *spend,cJSON } } -struct bitcoin_statetx *instantdex_signtx(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *txobj,int64_t satoshis,int64_t txfee,int32_t minconf,int32_t myside) +struct bitcoin_statetx *instantdex_signtx(struct supernet_info *myinfo,struct iguana_info *coin,uint32_t locktime,char *scriptstr,int64_t satoshis,int64_t txfee,int32_t minconf,int32_t myside) { - struct iguana_waddress *waddr; struct bitcoin_statetx *tx=0; uint8_t pubkey33[33]; char coinaddr[64]; char *rawtx,*signedtx; cJSON *vins; bits256 signedtxid; int32_t completed; cJSON *addresses = cJSON_CreateArray(); + struct iguana_waddress *waddr; struct bitcoin_statetx *tx=0; uint8_t pubkey33[33]; char coinaddr[64]; char *rawtx,*signedtx,*retstr; bits256 signedtxid; uint32_t rawtxtag; int32_t flag,completed; cJSON *valsobj,*vins,*retjson,*argjson,*addresses = cJSON_CreateArray(); if ( coin->changeaddr[0] == 0 ) { if ( (waddr= iguana_getaccountaddress(myinfo,coin,0,0,coin->changeaddr,"change")) == 0 ) return(0); - strcpy(coin->changeaddr,waddr->coinaddr); + bitcoin_address(coin->changeaddr,coin->chain->pubtype,waddr->rmd160,20); } bitcoin_pubkey33(myinfo->ctx,pubkey33,myinfo->persistent_priv); bitcoin_address(coinaddr,coin->chain->pubtype,pubkey33,33); - printf("%s persistent.(%s) (%s)\n",coin->symbol,myinfo->myaddr.BTC,coinaddr); + printf("%s persistent.(%s) (%s) change.(%s) scriptstr.(%s)\n",coin->symbol,myinfo->myaddr.BTC,coinaddr,coin->changeaddr,scriptstr); jaddistr(addresses,coinaddr); - if ( (rawtx= iguana_calcrawtx(myinfo,coin,&vins,txobj,satoshis,coin->changeaddr,txfee,addresses,minconf)) != 0 && vins != 0 ) + valsobj = cJSON_CreateObject(); + jaddstr(valsobj,"coin",coin->symbol); + jadd64bits(valsobj,"amount",satoshis); + jadd64bits(valsobj,"txfee",txfee); + jaddnum(valsobj,"minconf",minconf); + rawtxtag = (uint32_t)rand(); + jaddnum(valsobj,"rawtxtag",rawtxtag); + jaddnum(valsobj,"locktime",locktime); + argjson = cJSON_CreateObject(); + jaddnum(argjson,"timeout",15000); + if ( (retstr= iguana_rawtx(myinfo,coin,argjson,0,coin->changeaddr,addresses,valsobj,scriptstr)) != 0 ) { - if ( (signedtx= iguana_signrawtx(myinfo,coin,&signedtxid,&completed,vins,rawtx)) != 0 ) + printf("feetx got.(%s)\n",retstr); + flag = 0; + if ( (retjson= cJSON_Parse(retstr)) != 0 ) { - iguana_unspentslock(myinfo,coin,vins); - //avail = iguana_availunspents(myinfo,&unspents,&num,coin,coin->chain->minconfirms,"*",coin->blockspace,sizeof(coin->blockspace)); - //if ( (feetx= iguana_signunspents(myinfo,coin,&signedtxid,&completed,txobj,swap->insurance,coin->changeaddr,coin->txfee,unspents,num)) != 0 ) - //{ - tx = calloc(1,sizeof(*tx) + strlen(rawtx) + 1); - strcpy(tx->txbytes,signedtx); - tx->txid = signedtxid; - printf("%s feetx.%s\n",myside != 0 ? "BOB" : "ALICE",signedtx); - //disp_tx(myinfo,coin,"feetx",feetx); - free(signedtx); - } free(rawtx); + if ( (rawtx= jstr(retjson,"result")) != 0 && (vins= jobj(retjson,"vins")) != 0 ) + flag = 1; + } + if ( flag == 0 ) + { + vins = 0; + if ( (rawtx= iguana_pollrawtx(&myinfo->rawtxQ,&vins,rawtxtag,OS_milliseconds() + 10000)) != 0 ) + { + if ( vins != 0 ) + flag = 2; + } + } + if ( flag != 0 ) + { + if ( (signedtx= iguana_signrawtx(myinfo,coin,&signedtxid,&completed,vins,rawtx)) != 0 ) + { + iguana_unspentslock(myinfo,coin,vins); + tx = calloc(1,sizeof(*tx) + strlen(rawtx) + 1); + strcpy(tx->txbytes,signedtx); + tx->txid = signedtxid; + printf("%s feetx.%s\n",myside != 0 ? "BOB" : "ALICE",signedtx); + free(signedtx); + } + } + if ( retjson != 0 ) + free_json(retjson); + if ( flag == 2 ) + { + free_json(vins); + free(rawtx); + } + free(retstr); } else printf("error creating %s feetx\n",myside != 0 ? "BOB" : "ALICE"); free_json(addresses); + free_json(argjson); return(tx); } struct bitcoin_statetx *instantdex_feetx(struct supernet_info *myinfo,struct instantdex_accept *A,struct bitcoin_swapinfo *swap,struct iguana_info *coin) { - int32_t n; cJSON *txobj; uint8_t paymentscript[128]; struct bitcoin_statetx *ptr = 0; uint64_t r; + int32_t n; uint8_t paymentscript[128]; char scriptstr[512]; struct bitcoin_statetx *ptr = 0; uint64_t r; r = swap->mine.orderid ^ swap->other.orderid; n = instantdex_outputinsurance(paymentscript,swap->insurance,r,r * (strcmp("BTC",coin->symbol) == 0)); - txobj = bitcoin_txcreate(coin,0); - bitcoin_txoutput(coin,txobj,paymentscript,n,swap->insurance); - ptr = instantdex_signtx(myinfo,coin,txobj,swap->insurance,coin->txfee,swap->mine.minconfirms,A->offer.myside); - free_json(txobj); + init_hexbytes_noT(scriptstr,paymentscript,n); + printf("feetx %.8f (%s)\n",dstr(swap->insurance),scriptstr); + ptr = instantdex_signtx(myinfo,coin,0,scriptstr,swap->insurance,coin->txfee,swap->mine.minconfirms,A->offer.myside); return(ptr); } @@ -193,17 +226,15 @@ int32_t instantdex_feetxverify(struct supernet_info *myinfo,struct iguana_info * struct bitcoin_statetx *instantdex_bobtx(struct supernet_info *myinfo,struct bitcoin_swapinfo *swap,struct iguana_info *coin,bits256 pub1,bits256 pub2,bits256 priv,uint32_t reftime,int64_t amount,int32_t depositflag) { - cJSON *txobj; int32_t n,secretstart; struct bitcoin_statetx *ptr = 0; uint8_t script[1024],secret[20]; uint32_t locktime; int64_t satoshis; + int32_t n,secretstart; struct bitcoin_statetx *ptr = 0; uint8_t script[1024],secret[20]; uint32_t locktime; int64_t satoshis; char scriptstr[512]; if ( coin == 0 ) return(0); locktime = (uint32_t)(reftime + INSTANTDEX_LOCKTIME * (1 + depositflag)); - txobj = bitcoin_txcreate(coin,locktime); calc_rmd160_sha256(secret,priv.bytes,sizeof(priv)); n = instantdex_bobscript(script,0,&secretstart,locktime,pub1,secret,pub2); satoshis = amount + depositflag*swap->insurance*100; - bitcoin_txoutput(coin,txobj,script,n,satoshis); - ptr = instantdex_signtx(myinfo,coin,txobj,satoshis,coin->txfee,swap->mine.minconfirms,swap->mine.offer.myside); - free_json(txobj); + init_hexbytes_noT(scriptstr,script,n); + ptr = instantdex_signtx(myinfo,coin,locktime,scriptstr,satoshis,coin->txfee,swap->mine.minconfirms,swap->mine.offer.myside); return(ptr); } @@ -284,14 +315,12 @@ int32_t instantdex_altpaymentverify(struct supernet_info *myinfo,struct iguana_i struct bitcoin_statetx *instantdex_alicetx(struct supernet_info *myinfo,struct iguana_info *altcoin,char *msigaddr,bits256 pubAm,bits256 pubBn,int64_t amount,struct bitcoin_swapinfo *swap) { - cJSON *txobj; int32_t n; uint8_t script[1024]; struct bitcoin_statetx *ptr = 0; + int32_t n; uint8_t script[1024]; char scriptstr[2048]; struct bitcoin_statetx *ptr = 0; if ( altcoin != 0 ) { - txobj = bitcoin_txcreate(altcoin,0); n = instantdex_alicescript(script,0,msigaddr,altcoin->chain->p2shtype,pubAm,pubBn); - bitcoin_txoutput(altcoin,txobj,script,n,amount); - ptr = instantdex_signtx(myinfo,altcoin,txobj,amount,altcoin->txfee,swap->mine.minconfirms,swap->mine.offer.myside); - free_json(txobj); + init_hexbytes_noT(scriptstr,script,n); + ptr = instantdex_signtx(myinfo,altcoin,0,scriptstr,amount,altcoin->txfee,swap->mine.minconfirms,swap->mine.offer.myside); } return(ptr); }