From 91baa9f10aec27898f4d6876e27b27c72c89a3d9 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 7 Jul 2016 10:12:15 -0300 Subject: [PATCH] test --- basilisk/basilisk.c | 4 +- basilisk/basilisk_swap.c | 138 ++++++++++++++++++++---------------- iguana/iguana_interpreter.c | 21 +++--- iguana/iguana_payments.c | 2 +- iguana/iguana_sign.c | 18 +++-- 5 files changed, 100 insertions(+), 83 deletions(-) diff --git a/basilisk/basilisk.c b/basilisk/basilisk.c index 1d4c641c3..b1256c4fb 100755 --- a/basilisk/basilisk.c +++ b/basilisk/basilisk.c @@ -914,7 +914,9 @@ void basilisks_loop(void *arg) } } portable_mutex_unlock(&myinfo->messagemutex); - sleep(2); + if ( myinfo->RELAYID >= 0 ) + usleep(100000); + else sleep(1); } } diff --git a/basilisk/basilisk_swap.c b/basilisk/basilisk_swap.c index 8a5b6d7ae..eaa1f776f 100755 --- a/basilisk/basilisk_swap.c +++ b/basilisk/basilisk_swap.c @@ -119,16 +119,19 @@ int32_t basilisk_rawtx_spend(struct supernet_info *myinfo,struct basilisk_swap * if ( dest == &swap->aliceclaim ) locktime = swap->locktime + INSTANTDEX_LOCKTIME; V.signers[0].privkey = privkey; + bitcoin_pubkey33(myinfo->ctx,V.signers[0].pubkey,privkey); privkeys = cJSON_CreateArray(); bitcoin_priv2wif(wifstr,privkey,rawtx->coin->chain->wiftype); jaddistr(privkeys,wifstr); if ( privkey2 != 0 ) { V.signers[1].privkey = *privkey2; + bitcoin_pubkey33(myinfo->ctx,V.signers[1].pubkey,*privkey2); bitcoin_priv2wif(wifstr,*privkey2,rawtx->coin->chain->wiftype); jaddistr(privkeys,wifstr); - printf("add second privkey.(%s)\n",jprint(privkeys,0)); - } + V.N = V.M = 2; + char str[65]; printf("add second privkey.(%s) %s\n",jprint(privkeys,0),bits256_str(str,*privkey2)); + } else V.N = V.M = 1; V.suppress_pubkeys = dest->suppress_pubkeys; if ( userdata != 0 && userdatalen > 0 ) { @@ -302,15 +305,78 @@ int32_t basilisk_privAm_extract(struct supernet_info *myinfo,struct basilisk_swa { // need to scan blockchain for alicespend of bobpayment // search for swap->bobpayment.actualtxid in spends - //basilisk_alicepayment_spend(myinfo,swap,&swap->bobspend); + if ( bits256_nonz(swap->privAm) != 0 && swap->bobspend.txbytes == 0 ) + { + char str[65]; printf("have privAm.%s\n",bits256_str(str,swap->privAm)); + return(basilisk_alicepayment_spend(myinfo,swap,&swap->bobspend)); + } return(-1); } -int32_t basilisk_privBn_extract(struct supernet_info *myinfo,struct basilisk_swap *swap) +int32_t basilisk_verify_pubpair(int32_t *wrongfirstbytep,struct basilisk_swap *swap,int32_t ind,uint8_t pub0,bits256 pubi,uint64_t txid) +{ + if ( pub0 != (swap->iambob ^ 1) + 0x02 ) + { + (*wrongfirstbytep)++; + printf("wrongfirstbyte[%d] %02x\n",ind,pub0); + return(-1); + } + else if ( swap->otherdeck[ind][1] != pubi.txid ) + { + printf("otherdeck[%d] priv ->pub mismatch %llx != %llx\n",ind,(long long)swap->otherdeck[ind][1],(long long)pubi.txid); + return(-1); + } + else if ( swap->otherdeck[ind][0] != txid ) + { + printf("otherdeck[%d] priv mismatch %llx != %llx\n",ind,(long long)swap->otherdeck[ind][0],(long long)txid); + return(-1); + } + return(0); +} + +int32_t basilisk_verify_privi(struct supernet_info *myinfo,struct basilisk_swap *swap,uint8_t *data,int32_t datalen) +{ + int32_t j,wrongfirstbyte,len = 0; bits256 privkey,pubi; uint8_t secret160[20],pubkey33[33]; uint64_t txid; + if ( datalen == sizeof(bits256) ) + { + for (j=0; j<32; j++) + privkey.bytes[j] = data[len++]; + calc_rmd160_sha256(secret160,privkey.bytes,sizeof(privkey)); + memcpy(&txid,secret160,sizeof(txid)); + pubi = bitcoin_pubkey33(myinfo->ctx,pubkey33,privkey); + char str[65]; printf("verify privi %s\n",bits256_str(str,privkey)); + if ( basilisk_verify_pubpair(&wrongfirstbyte,swap,swap->choosei,pubkey33[0],pubi,txid) == 0 ) + { + if ( swap->iambob != 0 ) + swap->privAm = privkey; + else swap->privBn = privkey; + printf("privi verified\n"); + return(0); + } + } + return(-1); +} + +int32_t basilisk_swapget(struct supernet_info *myinfo,struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t maxlen,int32_t (*basilisk_verify_func)(struct supernet_info *myinfo,struct basilisk_swap *swap,uint8_t *data,int32_t datalen)) +{ + int32_t datalen; + if ( (datalen= basilisk_channelget(myinfo,myinfo->myaddr.persistent,swap->req.quoteid,msgbits,data,maxlen)) > 0 ) + return((*basilisk_verify_func)(myinfo,swap,data,datalen)); + else return(-1); +} + +int32_t basilisk_privBn_extract(struct supernet_info *myinfo,struct basilisk_swap *swap,uint8_t *data,int32_t maxlen) { // need to scan blockchain for bobrefund // search for swap->bobrefund.actualtxid in spends - basilisk_alicepayment_spend(myinfo,swap,&swap->alicereclaim); + if ( basilisk_swapget(myinfo,swap,0x80000000,data,maxlen,basilisk_verify_privi) == 0 ) + { + if ( bits256_nonz(swap->privBn) != 0 && swap->alicereclaim.txbytes == 0 ) + { + char str[65]; printf("have privBn.%s\n",bits256_str(str,swap->privBn)); + return(basilisk_alicepayment_spend(myinfo,swap,&swap->alicereclaim)); + } + } return(-1); } // end of coin protocol dependent @@ -714,27 +780,6 @@ int32_t basilisk_verify_otherdeck(struct supernet_info *myinfo,struct basilisk_s return(0); } -int32_t basilisk_verify_pubpair(int32_t *wrongfirstbytep,struct basilisk_swap *swap,int32_t ind,uint8_t pub0,bits256 pubi,uint64_t txid) -{ - if ( pub0 != (swap->iambob ^ 1) + 0x02 ) - { - (*wrongfirstbytep)++; - printf("wrongfirstbyte[%d] %02x\n",ind,pub0); - return(-1); - } - else if ( swap->otherdeck[ind][1] != pubi.txid ) - { - printf("otherdeck[%d] priv ->pub mismatch %llx != %llx\n",ind,(long long)swap->otherdeck[ind][1],(long long)pubi.txid); - return(-1); - } - else if ( swap->otherdeck[ind][0] != txid ) - { - printf("otherdeck[%d] priv mismatch %llx != %llx\n",ind,(long long)swap->otherdeck[ind][0],(long long)txid); - return(-1); - } - return(0); -} - int32_t basilisk_verify_privkeys(struct supernet_info *myinfo,struct basilisk_swap *swap,uint8_t *data,int32_t datalen) { int32_t i,j,wrongfirstbyte=0,errs=0,len = 0; bits256 otherpriv,pubi; uint8_t secret160[20],otherpubkey[33]; uint64_t txid; @@ -776,37 +821,6 @@ int32_t basilisk_verify_privkeys(struct supernet_info *myinfo,struct basilisk_sw return(errs); } -int32_t basilisk_verify_privi(struct supernet_info *myinfo,struct basilisk_swap *swap,uint8_t *data,int32_t datalen) -{ - int32_t j,wrongfirstbyte,len = 0; bits256 privkey,pubi; uint8_t secret160[20],pubkey33[33]; uint64_t txid; - if ( datalen == sizeof(bits256) ) - { - for (j=0; j<32; j++) - privkey.bytes[j] = data[len++]; - calc_rmd160_sha256(secret160,privkey.bytes,sizeof(privkey)); - memcpy(&txid,secret160,sizeof(txid)); - pubi = bitcoin_pubkey33(myinfo->ctx,pubkey33,privkey); - printf("verify privi\n"); - if ( basilisk_verify_pubpair(&wrongfirstbyte,swap,swap->choosei,pubkey33[0],pubi,txid) == 0 ) - { - if ( swap->iambob != 0 ) - swap->privAm = privkey; - else swap->privBn = privkey; - printf("privi verified\n"); - return(0); - } - } - return(-1); -} - -int32_t basilisk_swapget(struct supernet_info *myinfo,struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t maxlen,int32_t (*basilisk_verify_func)(struct supernet_info *myinfo,struct basilisk_swap *swap,uint8_t *data,int32_t datalen)) -{ - int32_t datalen; - if ( (datalen= basilisk_channelget(myinfo,myinfo->myaddr.persistent,swap->req.quoteid,msgbits,data,maxlen)) > 0 ) - return((*basilisk_verify_func)(myinfo,swap,data,datalen)); - else return(-1); -} - uint32_t basilisk_swapsend(struct supernet_info *myinfo,struct basilisk_swap *swap,uint32_t msgbits,uint8_t *data,int32_t datalen,uint32_t nextbits) { if ( basilisk_channelsend(myinfo,swap->otherhash,swap->req.quoteid,msgbits,data,datalen) == 0 ) @@ -1114,7 +1128,7 @@ void basilisk_swaploop(void *_swap) { swap->sleeptime = 1; swap->statebits |= basilisk_swapdata_rawtxsend(myinfo,swap,0x1000,data,maxlen,&swap->alicepayment,0x800); - basilisk_alicepayment_spend(myinfo,swap,&swap->alicereclaim); + //basilisk_alicepayment_spend(myinfo,swap,&swap->alicereclaim); } // [BLOCKING: payfound] make sure payment is confrmed and send in spend or see bob's reclaim and claim else if ( (swap->statebits & 0x8000) == 0 ) @@ -1125,7 +1139,7 @@ void basilisk_swaploop(void *_swap) swap->sleeptime = 1; swap->statebits |= 0x8000; } - else if ( basilisk_privBn_extract(myinfo,swap) == 0 ) + else if ( basilisk_privBn_extract(myinfo,swap,data,maxlen) == 0 ) { swap->sleeptime = 1; swap->statebits |= 0x8000; @@ -1176,7 +1190,11 @@ void basilisk_swaploop(void *_swap) } if ( swap->iambob != 0 ) { - if ( basilisk_swapdata_rawtxsend(myinfo,swap,0,data,maxlen,&swap->bobrefund,0) == 0 ) // use secretBn + for (j=datalen=0; j<32; j++) + data[datalen++] = swap->privBn.bytes[j]; + basilisk_swapsend(myinfo,swap,0x80000000,data,datalen,0x80000000); + printf("BOB reclaims refund\n"); + if ( basilisk_swapdata_rawtxsend(myinfo,swap,0,data,maxlen,&swap->bobrefund,0x80000000) == 0 ) // use secretBn { printf("Bob submit error getting refund of deposit\n"); } diff --git a/iguana/iguana_interpreter.c b/iguana/iguana_interpreter.c index 5f09dc0fb..e1517fad5 100755 --- a/iguana/iguana_interpreter.c +++ b/iguana/iguana_interpreter.c @@ -1129,9 +1129,10 @@ int32_t bitcoin_assembler(struct iguana_info *coin,cJSON *logarray,uint8_t scrip } memset(args,0,sizeof(args)); numargs = 0; + script[k++] = op->opcode; if ( (op->flags & IGUANA_CONTROLFLAG) != 0 ) { - printf("control opcode depth.%d\n",stacks->stackdepth); + //printf("control opcode depth.%d\n",stacks->stackdepth); switch ( op->opcode ) { case IGUANA_OP_IF: case IGUANA_OP_NOTIF: @@ -1150,12 +1151,12 @@ int32_t bitcoin_assembler(struct iguana_info *coin,cJSON *logarray,uint8_t scrip if ( iguana_isnonz(args[0]) == (op->opcode == IGUANA_OP_IF) ) { val = 1; - printf("OP_IF enabled depth.%d\n",stacks->stackdepth); + //printf("OP_IF enabled depth.%d\n",stacks->stackdepth); } else { val = -1; - printf("OP_IF disabled depth.%d\n",stacks->stackdepth); + //printf("OP_IF disabled depth.%d\n",stacks->stackdepth); } stacks->lastpath[++stacks->ifdepth] = val; } @@ -1172,7 +1173,7 @@ int32_t bitcoin_assembler(struct iguana_info *coin,cJSON *logarray,uint8_t scrip errs++; } stacks->lastpath[stacks->ifdepth] *= -1; - printf("OP_ELSE status.%d depth.%d\n",stacks->lastpath[stacks->ifdepth],stacks->stackdepth); + //printf("OP_ELSE status.%d depth.%d\n",stacks->lastpath[stacks->ifdepth],stacks->stackdepth); } break; case IGUANA_OP_ENDIF: @@ -1182,7 +1183,7 @@ int32_t bitcoin_assembler(struct iguana_info *coin,cJSON *logarray,uint8_t scrip errs++; } stacks->ifdepth--; - printf("OP_ENDIF status.%d depth.%d\n",stacks->lastpath[stacks->ifdepth],stacks->stackdepth); + //printf("OP_ENDIF status.%d depth.%d\n",stacks->lastpath[stacks->ifdepth],stacks->stackdepth); break; case IGUANA_OP_VERIFY: break; @@ -1199,16 +1200,15 @@ int32_t bitcoin_assembler(struct iguana_info *coin,cJSON *logarray,uint8_t scrip { if ( stacks->lastpath[stacks->ifdepth] < 0 ) { - printf("SKIP opcode.%02x depth.%d\n",op->opcode,stacks->stackdepth); + //printf("SKIP opcode.%02x depth.%d\n",op->opcode,stacks->stackdepth); if ( stacks->logarray ) jaddistr(stacks->logarray,"skip"); continue; } - printf("conditional opcode.%02x stackdepth.%d\n",op->opcode,stacks->stackdepth); + //printf("conditional opcode.%02x stackdepth.%d\n",op->opcode,stacks->stackdepth); } if ( op->opcode <= IGUANA_OP_16 || ++numops <= MAX_OPS_PER_SCRIPT ) { - script[k++] = op->opcode; if ( (op->flags & IGUANA_ALWAYSILLEGAL) != 0 ) { printf("disabled opcode.%s at offset.%ld\n",str,(long)str-(long)asmstr); @@ -1510,12 +1510,11 @@ int32_t bitcoin_assembler(struct iguana_info *coin,cJSON *logarray,uint8_t scrip } else if ( iguana_isnonz(stacks->stack[--stacks->stackdepth]) != 0 ) { - printf("Evaluate true, depth.%d errs.%d\n",stacks->stackdepth,errs); + printf("Evaluate true, depth.%d errs.%d k.%d\n",stacks->stackdepth,errs,k); if ( errs == 0 ) jadd(interpreter,"result",jtrue()); else jadd(interpreter,"result",jfalse()); - } - else printf("Evaluate FALSE, depth.%d errs.%d\n",stacks->stackdepth,errs); + } else printf("Evaluate FALSE, depth.%d errs.%d\n",stacks->stackdepth,errs); //if ( stacks->logarray != 0 ) // printf("LOG.(%s)\n",jprint(stacks->logarray,0)); if ( numargs > 0 ) diff --git a/iguana/iguana_payments.c b/iguana/iguana_payments.c index 24c29fe42..96eb8e955 100755 --- a/iguana/iguana_payments.c +++ b/iguana/iguana_payments.c @@ -289,7 +289,7 @@ char *iguana_signrawtx(struct supernet_info *myinfo,struct iguana_info *coin,bit memset(&msgtx,0,sizeof(msgtx)); if ( V == 0 ) V = calloc(numinputs,sizeof(*V)), flagV = 1; - //printf("SIGN.(%s) priv.(%s)\n",jprint(vins,0),jprint(privkeys,0)); + printf("SIGN.(%s) priv.(%s) %llx %llx\n",jprint(vins,0),jprint(privkeys,0),(long long)V->signers[0].privkey.txid,(long long)V->signers[1].privkey.txid); if ( V != 0 ) { if ( iguana_signrawtransaction(myinfo,coin,&msgtx,&signedtx,signedtxidp,V,numinputs,rawtx,vins,privkeys) > 0 ) diff --git a/iguana/iguana_sign.c b/iguana/iguana_sign.c index 2f9617def..29a412fba 100755 --- a/iguana/iguana_sign.c +++ b/iguana/iguana_sign.c @@ -757,9 +757,8 @@ int32_t iguana_vininfo_create(struct supernet_info *myinfo,struct iguana_info *c if ( (plen= bitcoin_pubkeylen(vp->signers[0].pubkey)) > 0 ) bitcoin_address(vp->coinaddr,coin->chain->pubtype,vp->signers[0].pubkey,plen); } - int32_t j; for (j=0; jspendlen; j++) - printf("%02x",vp->spendscript[j]); - printf(" <- spendscript vin.%d\n",i); + if ( vp->M == 0 && vp->N == 0 ) + vp->M = vp->N = 1; if ( vp->coinaddr[i] != 0 && (waddr= iguana_waddresssearch(myinfo,&wacct,vp->coinaddr)) != 0 ) { vp->signers[0].privkey = waddr->privkey; @@ -769,8 +768,6 @@ int32_t iguana_vininfo_create(struct supernet_info *myinfo,struct iguana_info *c memcpy(vp->signers[0].pubkey,waddr->pubkey,plen); } } - if ( vp->M == 0 && vp->N == 0 ) - vp->M = vp->N = 1; } } /*for (i=0; itx_out; i++) @@ -972,8 +969,8 @@ int32_t iguana_interpreter(struct iguana_info *coin,cJSON *logarray,int64_t nLoc for (vini=0; vini 0 && signedtx != 0 ) { - if ( iguana_interpreter(coin,0,iguana_lockval(finalized,jint(txobj,"locktime")),V,numinputs) < 0 ) + int32_t tmp; + if ( (tmp= iguana_interpreter(coin,0,iguana_lockval(finalized,jint(txobj,"locktime")),V,numinputs)) < 0 ) { - printf("iguana_interpreter error.(%s)\n",signedtx); + printf("iguana_interpreter %d error.(%s)\n",tmp,signedtx); complete = 0; } }