From e4011e2ab4c0ac460d749fd5ef15294d13487387 Mon Sep 17 00:00:00 2001 From: jl777 Date: Tue, 12 Sep 2017 17:36:18 +0200 Subject: [PATCH] Test --- iguana/exchanges/LP_include.h | 1 + iguana/exchanges/LP_remember.c | 2 +- iguana/exchanges/LP_rpc.c | 149 +++++++++++++++++++++++++++--- iguana/exchanges/LP_swap.c | 4 +- iguana/exchanges/LP_transaction.c | 8 +- 5 files changed, 142 insertions(+), 22 deletions(-) diff --git a/iguana/exchanges/LP_include.h b/iguana/exchanges/LP_include.h index 861ed28f7..0786d81d3 100644 --- a/iguana/exchanges/LP_include.h +++ b/iguana/exchanges/LP_include.h @@ -261,6 +261,7 @@ struct basilisk_swap }; +void LP_swap_coinaddr(struct iguana_info *coin,char *coinaddr,uint8_t *data,int32_t datalen,int32_t vout); void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx); uint32_t basilisk_requestid(struct basilisk_request *rp); uint32_t basilisk_quoteid(struct basilisk_request *rp); diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index f880bcead..c21a65dce 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -47,7 +47,7 @@ void basilisk_dontforget(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx fprintf(fp,"\",\"txid\":\"%s\"",bits256_str(str,bits256_doublesha256(0,rawtx->txbytes,rawtx->I.datalen))); if ( rawtx == &swap->bobdeposit || rawtx == &swap->bobpayment ) { - LP_swap_coinaddr(swap,&swap->bobcoin,coinaddr,rawtx->txbytes,rawtx->I.datalen); + LP_swap_coinaddr(&swap->bobcoin,coinaddr,rawtx->txbytes,rawtx->I.datalen,0); if ( coinaddr[0] != 0 ) { LP_importaddress(swap->bobcoin.symbol,coinaddr); diff --git a/iguana/exchanges/LP_rpc.c b/iguana/exchanges/LP_rpc.c index 70109a70c..d5051a47c 100644 --- a/iguana/exchanges/LP_rpc.c +++ b/iguana/exchanges/LP_rpc.c @@ -197,6 +197,8 @@ cJSON *LP_assethbla(char *assetid) int32_t LP_getheight(struct iguana_info *coin) { cJSON *retjson; char *method = "getinfo"; int32_t height = coin->height; + if ( coin == 0 ) + return(-1); if ( coin->electrum == 0 && time(NULL) > coin->heighttime+60 ) { if ( strcmp(coin->symbol,"BTC") == 0 ) @@ -214,6 +216,8 @@ int32_t LP_getheight(struct iguana_info *coin) cJSON *LP_getmempool(char *symbol) { struct iguana_info *coin = LP_coinfind(symbol); + if ( coin == 0 ) + return(cJSON_Parse("{\"error\":\"no coin\"}")); return(bitcoin_json(coin,"getrawmempool","[]")); } @@ -226,16 +230,12 @@ cJSON *LP_paxprice(char *fiat) return(bitcoin_json(coin,"paxprice",buf)); } -cJSON *LP_gettxout(char *symbol,bits256 txid,int32_t vout) -{ - char buf[128],str[65]; struct iguana_info *coin = LP_coinfind(symbol); - sprintf(buf,"[\"%s\", %d, true]",bits256_str(str,txid),vout); - return(bitcoin_json(coin,"gettxout",buf)); -} - cJSON *LP_gettx(char *symbol,bits256 txid) { - char buf[128],str[65]; struct iguana_info *coin = LP_coinfind(symbol); + char buf[128],str[65],*hexstr; int32_t len; bits256 checktxid; cJSON *retjson; struct iguana_info *coin; struct iguana_msgtx msgtx; uint8_t extraspace[8192],*serialized; + coin = LP_coinfind(symbol); + if ( coin == 0 ) + return(cJSON_Parse("{\"error\":\"no coin\"}")); if ( coin->electrum == 0 ) { sprintf(buf,"[\"%s\", 1]",bits256_str(str,txid)); @@ -244,13 +244,82 @@ cJSON *LP_gettx(char *symbol,bits256 txid) else { sprintf(buf,"[\"%s\"]",bits256_str(str,txid)); - return(bitcoin_json(coin,"blockchain.transaction.get",buf)); + if ( (retjson= bitcoin_json(coin,"blockchain.transaction.get",buf)) != 0 ) + { + hexstr = jprint(retjson,1); + if ( (len= is_hexstr(hexstr,0)) > 2 ) + { + memset(&msgtx,0,sizeof(msgtx)); + len >>= 1; + serialized = malloc(len); + decode_hex(serialized,len,hexstr); + retjson = bitcoin_data2json(coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,coin->height,&checktxid,&msgtx,extraspace,sizeof(extraspace),serialized,len,0,0); + free(serialized); + printf("TX.(%s) match.%d\n",jprint(retjson,0),bits256_cmp(txid,checktxid)); + return(retjson); + } + return(cJSON_Parse("{\"error\":\"non hex transaction\"}")); + } + return(cJSON_Parse("{\"error\":\"no transaction bytes\"}")); + } +} + +cJSON *LP_gettxout(char *symbol,bits256 txid,int32_t vout) +{ + char buf[128],str[65],coinaddr[64],*hexstr; uint8_t *serialized; cJSON *item,*array,*listjson,*retjson=0; int32_t i,n,v,len; bits256 t; struct iguana_info *coin; + coin = LP_coinfind(symbol); + if ( coin == 0 ) + return(cJSON_Parse("{\"error\":\"no coin\"}")); + if ( coin->electrum == 0 ) + { + sprintf(buf,"[\"%s\", %d, true]",bits256_str(str,txid),vout); + return(bitcoin_json(coin,"gettxout",buf)); + } + else + { + sprintf(buf,"[\"%s\"]",bits256_str(str,txid)); + if ( (retjson= bitcoin_json(coin,"blockchain.transaction.get",buf)) != 0 ) + { + hexstr = jprint(retjson,1); + if ( (len= is_hexstr(hexstr,0)) > 2 ) + { + len >>= 1; + serialized = malloc(len); + decode_hex(serialized,len,hexstr); + LP_swap_coinaddr(coin,coinaddr,serialized,len,0); + if ( (listjson= electrum_address_listunspent(coin->symbol,0,0,coinaddr)) != 0 ) + { + if ( (array= jarray(&n,listjson,"result")) != 0 ) + { + for (i=0; isymbol,txid)) != 0 ) { *timestampp = juint(txobj,"locktime"); @@ -277,6 +348,8 @@ int32_t LP_txheight(uint32_t *timestampp,uint32_t *blocktimep,struct iguana_info cJSON *LP_getblockhashstr(char *symbol,char *blockhashstr) { char buf[128]; struct iguana_info *coin = LP_coinfind(symbol); + if ( coin == 0 ) + return(cJSON_Parse("{\"error\":\"no coin\"}")); sprintf(buf,"[\"%s\"]",blockhashstr); return(bitcoin_json(coin,"getblock",buf)); } @@ -284,6 +357,8 @@ cJSON *LP_getblockhashstr(char *symbol,char *blockhashstr) cJSON *LP_listunspent(char *symbol,char *coinaddr) { char buf[128]; struct iguana_info *coin = LP_coinfind(symbol); + if ( coin == 0 ) + return(cJSON_Parse("{\"error\":\"no coin\"}")); if ( coin->electrum == 0 ) { sprintf(buf,"[0, 99999999, [\"%s\"]]",coinaddr); @@ -299,6 +374,8 @@ cJSON *LP_listunspent(char *symbol,char *coinaddr) cJSON *LP_listtransactions(char *symbol,char *coinaddr,int32_t count,int32_t skip) { char buf[128]; struct iguana_info *coin = LP_coinfind(symbol); + if ( coin == 0 ) + return(cJSON_Parse("{\"error\":\"no coin\"}")); if ( count == 0 ) count = 100; sprintf(buf,"[\"%s\", %d, %d, true]",coinaddr,count,skip); @@ -307,9 +384,37 @@ cJSON *LP_listtransactions(char *symbol,char *coinaddr,int32_t count,int32_t ski cJSON *LP_validateaddress(char *symbol,char *address) { - char buf[512]; struct iguana_info *coin = LP_coinfind(symbol); - sprintf(buf,"[\"%s\"]",address); - return(bitcoin_json(coin,"validateaddress",buf)); + char buf[512],coinaddr[64],checkaddr[64],script[128]; int32_t i; uint8_t rmd160[20],addrtype; cJSON *retjson; struct iguana_info *coin = LP_coinfind(symbol); + if ( coin == 0 ) + return(cJSON_Parse("{\"error\":\"no coin\"}")); + if ( coin != 0 && coin->electrum != 0 ) + { + retjson = cJSON_CreateObject(); + jaddstr(retjson,"address",address); + bitcoin_addr2rmd160(coin->taddr,&addrtype,rmd160,address); + bitcoin_address(checkaddr,coin->taddr,addrtype,rmd160,20); + jadd(retjson,"isvalid",strcmp(address,checkaddr)==0? cJSON_CreateTrue() : cJSON_CreateFalse()); + if ( addrtype == coin->pubtype ) + { + strcpy(script,"76a914"); + for (i=0; i<20; i++) + sprintf(&script[i*2+6],"%02x",rmd160[i]); + script[i*2+6] = 0; + strcat(script,"88ac"); + jaddstr(retjson,"scriptPubKey",script); + } + bitcoin_address(coinaddr,coin->taddr,coin->pubtype,LP_myrmd160,20); + if ( strcmp(address,coinaddr) == 0 ) + jadd(retjson,"ismine",cJSON_CreateTrue()); + jadd(retjson,"iswatchonly",cJSON_CreateFalse()); + jadd(retjson,"isscript",addrtype == coin->p2shtype ? cJSON_CreateTrue() : cJSON_CreateFalse()); + return(retjson); + } + else + { + sprintf(buf,"[\"%s\"]",address); + return(bitcoin_json(coin,"validateaddress",buf)); + } } cJSON *LP_importprivkey(char *symbol,char *wifstr,char *label,int32_t flag) @@ -317,6 +422,10 @@ cJSON *LP_importprivkey(char *symbol,char *wifstr,char *label,int32_t flag) static void *ctx; char buf[512],address[64]; cJSON *retjson; struct iguana_info *coin; int32_t doneflag = 0; coin = LP_coinfind(symbol); + if ( coin == 0 ) + return(cJSON_Parse("{\"error\":\"no coin\"}")); + if ( coin->electrum != 0 ) + return(cJSON_Parse("{\"result\":\"electrum should have local wallet\"}")); if ( ctx == 0 ) ctx = bitcoin_ctx(); bitcoin_wif2addr(ctx,coin->wiftaddr,coin->taddr,coin->pubtype,address,wifstr); @@ -341,9 +450,18 @@ cJSON *LP_importprivkey(char *symbol,char *wifstr,char *label,int32_t flag) int32_t LP_importaddress(char *symbol,char *address) { - char buf[1024],*retstr; cJSON *validatejson; int32_t isvalid=0,doneflag = 0; struct iguana_info *coin = LP_coinfind(symbol); + char buf[1024],*retstr; cJSON *validatejson,*retjson; int32_t isvalid=0,doneflag = 0; struct iguana_info *coin = LP_coinfind(symbol); if ( coin == 0 ) return(-2); + if ( coin->electrum != 0 ) + { + if ( (retjson= electrum_address_subscribe(symbol,0,0,address)) != 0 ) + { + printf("importaddress.(%s) -> %s\n",address,jprint(retjson,0)); + free_json(retjson); + } + return(0); + } if ( (validatejson= LP_validateaddress(symbol,address)) != 0 ) { if ( (isvalid= is_cJSON_True(jobj(validatejson,"isvalid")) != 0) != 0 ) @@ -369,12 +487,13 @@ int32_t LP_importaddress(char *symbol,char *address) double LP_getestimatedrate(struct iguana_info *coin) { char buf[512],*retstr; double rate = 20; - if ( coin != 0 && (strcmp(coin->symbol,"BTC") == 0 || coin->txfee == 0) ) + if ( coin == 0 ) + return(0.0001); + if ( (strcmp(coin->symbol,"BTC") == 0 || coin->txfee == 0) ) { if ( coin->rate == 0. || time(NULL) > coin->ratetime+60 ) { sprintf(buf,"[%d]",3); - printf("estimate fee\n"); if ( (retstr= LP_apicall(coin,coin->electrum==0?"estimatefee" : "blockchain.estimatefee",buf)) != 0 ) { if ( retstr[0] != '-' ) diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 89edc2ce6..860ed0459 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -533,7 +533,7 @@ int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct ba printf("%02x",rawtx->redeemscript[i]); bitcoin_address(redeemaddr,rawtx->coin->taddr,rawtx->coin->p2shtype,rawtx->redeemscript,rawtx->I.redeemlen); printf(" received redeemscript.(%s) %s taddr.%d\n",redeemaddr,rawtx->coin->symbol,rawtx->coin->taddr); - LP_swap_coinaddr(swap,rawtx->coin,checkaddr,data,datalen); + LP_swap_coinaddr(rawtx->coin,checkaddr,data,datalen,0); if ( strcmp(redeemaddr,checkaddr) != 0 ) { printf("REDEEMADDR MISMATCH??? %s != %s\n",redeemaddr,checkaddr); @@ -592,7 +592,7 @@ int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct ba if ( rawtx == &swap->otherfee ) { char str[65]; - LP_swap_coinaddr(swap,rawtx->coin,rawtx->p2shaddr,data,datalen); + LP_swap_coinaddr(rawtx->coin,rawtx->p2shaddr,data,datalen,0); printf("got %s txid.%s (%s) -> %s\n",rawtx->name,bits256_str(str,rawtx->I.signedtxid),jprint(txobj,0),rawtx->p2shaddr); } else bitcoin_address(rawtx->p2shaddr,rawtx->coin->taddr,rawtx->coin->p2shtype,rawtx->spendscript,hexlen); } diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index f36804248..d96adc219 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -1160,7 +1160,7 @@ int32_t basilisk_bobdeposit_refund(struct basilisk_swap *swap,int32_t delay) return(-1); } -void LP_swap_coinaddr(struct basilisk_swap *swap,struct iguana_info *coin,char *coinaddr,uint8_t *data,int32_t datalen) +void LP_swap_coinaddr(struct iguana_info *coin,char *coinaddr,uint8_t *data,int32_t datalen,int32_t v) { cJSON *txobj,*vouts,*vout,*addresses,*item,*skey; uint8_t extraspace[8192]; bits256 signedtxid; struct iguana_msgtx msgtx; char *addr; int32_t n,m,suppress_pubkeys = 0; if ( (txobj= bitcoin_data2json(coin->taddr,coin->pubtype,coin->p2shtype,coin->isPoS,coin->longestchain,&signedtxid,&msgtx,extraspace,sizeof(extraspace),data,datalen,0,suppress_pubkeys)) != 0 ) @@ -1168,7 +1168,7 @@ void LP_swap_coinaddr(struct basilisk_swap *swap,struct iguana_info *coin,char * //char str[65]; printf("got txid.%s (%s)\n",bits256_str(str,signedtxid),jprint(txobj,0)); if ( (vouts= jarray(&n,txobj,"vout")) != 0 && n > 0 ) { - vout = jitem(vouts,0); + vout = jitem(vouts,v); //printf("VOUT.(%s)\n",jprint(vout,0)); if ( (skey= jobj(vout,"scriptPubKey")) != 0 && (addresses= jarray(&m,skey,"addresses")) != 0 ) { @@ -1217,7 +1217,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i printf("%02x",swap->bobpayment.redeemscript[j]); printf(" <- redeem.%d\n",swap->bobpayment.I.redeemlen); printf(" <- GENERATED BOB PAYMENT.%d destaddr.(%s)\n",swap->bobpayment.I.datalen,swap->bobpayment.I.destaddr); - LP_swap_coinaddr(swap,&swap->bobcoin,checkaddr,swap->bobpayment.txbytes,swap->bobpayment.I.datalen); + LP_swap_coinaddr(&swap->bobcoin,checkaddr,swap->bobpayment.txbytes,swap->bobpayment.I.datalen,0); if ( strcmp(swap->bobpayment.I.destaddr,checkaddr) != 0 ) { printf("BOBPAYMENT REDEEMADDR MISMATCH??? %s != %s\n",swap->bobpayment.I.destaddr,checkaddr); @@ -1251,7 +1251,7 @@ int32_t basilisk_bobscripts_set(struct basilisk_swap *swap,int32_t depositflag,i for (j=0; jbobdeposit.I.datalen; j++) printf("%02x",swap->bobdeposit.txbytes[j]); printf(" <- GENERATED BOB DEPOSIT.%d (%s)\n",swap->bobdeposit.I.datalen,swap->bobdeposit.I.destaddr); - LP_swap_coinaddr(swap,&swap->bobcoin,checkaddr,swap->bobdeposit.txbytes,swap->bobdeposit.I.datalen); + LP_swap_coinaddr(&swap->bobcoin,checkaddr,swap->bobdeposit.txbytes,swap->bobdeposit.I.datalen,0); if ( strcmp(swap->bobdeposit.I.destaddr,checkaddr) != 0 ) { printf("BOBDEPOSIT REDEEMADDR MISMATCH??? %s != %s\n",swap->bobdeposit.I.destaddr,checkaddr);