Browse Source

test

release/v0.1
jl777 9 years ago
parent
commit
91baa9f10a
  1. 4
      basilisk/basilisk.c
  2. 138
      basilisk/basilisk_swap.c
  3. 21
      iguana/iguana_interpreter.c
  4. 2
      iguana/iguana_payments.c
  5. 18
      iguana/iguana_sign.c

4
basilisk/basilisk.c

@ -914,7 +914,9 @@ void basilisks_loop(void *arg)
} }
} }
portable_mutex_unlock(&myinfo->messagemutex); portable_mutex_unlock(&myinfo->messagemutex);
sleep(2); if ( myinfo->RELAYID >= 0 )
usleep(100000);
else sleep(1);
} }
} }

138
basilisk/basilisk_swap.c

@ -119,16 +119,19 @@ int32_t basilisk_rawtx_spend(struct supernet_info *myinfo,struct basilisk_swap *
if ( dest == &swap->aliceclaim ) if ( dest == &swap->aliceclaim )
locktime = swap->locktime + INSTANTDEX_LOCKTIME; locktime = swap->locktime + INSTANTDEX_LOCKTIME;
V.signers[0].privkey = privkey; V.signers[0].privkey = privkey;
bitcoin_pubkey33(myinfo->ctx,V.signers[0].pubkey,privkey);
privkeys = cJSON_CreateArray(); privkeys = cJSON_CreateArray();
bitcoin_priv2wif(wifstr,privkey,rawtx->coin->chain->wiftype); bitcoin_priv2wif(wifstr,privkey,rawtx->coin->chain->wiftype);
jaddistr(privkeys,wifstr); jaddistr(privkeys,wifstr);
if ( privkey2 != 0 ) if ( privkey2 != 0 )
{ {
V.signers[1].privkey = *privkey2; V.signers[1].privkey = *privkey2;
bitcoin_pubkey33(myinfo->ctx,V.signers[1].pubkey,*privkey2);
bitcoin_priv2wif(wifstr,*privkey2,rawtx->coin->chain->wiftype); bitcoin_priv2wif(wifstr,*privkey2,rawtx->coin->chain->wiftype);
jaddistr(privkeys,wifstr); 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; V.suppress_pubkeys = dest->suppress_pubkeys;
if ( userdata != 0 && userdatalen > 0 ) 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 // need to scan blockchain for alicespend of bobpayment
// search for swap->bobpayment.actualtxid in spends // 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); 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 // need to scan blockchain for bobrefund
// search for swap->bobrefund.actualtxid in spends // 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); return(-1);
} }
// end of coin protocol dependent // end of coin protocol dependent
@ -714,27 +780,6 @@ int32_t basilisk_verify_otherdeck(struct supernet_info *myinfo,struct basilisk_s
return(0); 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 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; 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); 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) 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 ) 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->sleeptime = 1;
swap->statebits |= basilisk_swapdata_rawtxsend(myinfo,swap,0x1000,data,maxlen,&swap->alicepayment,0x800); 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 // [BLOCKING: payfound] make sure payment is confrmed and send in spend or see bob's reclaim and claim
else if ( (swap->statebits & 0x8000) == 0 ) else if ( (swap->statebits & 0x8000) == 0 )
@ -1125,7 +1139,7 @@ void basilisk_swaploop(void *_swap)
swap->sleeptime = 1; swap->sleeptime = 1;
swap->statebits |= 0x8000; 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->sleeptime = 1;
swap->statebits |= 0x8000; swap->statebits |= 0x8000;
@ -1176,7 +1190,11 @@ void basilisk_swaploop(void *_swap)
} }
if ( swap->iambob != 0 ) 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"); printf("Bob submit error getting refund of deposit\n");
} }

21
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)); memset(args,0,sizeof(args));
numargs = 0; numargs = 0;
script[k++] = op->opcode;
if ( (op->flags & IGUANA_CONTROLFLAG) != 0 ) 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 ) switch ( op->opcode )
{ {
case IGUANA_OP_IF: case IGUANA_OP_NOTIF: 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) ) if ( iguana_isnonz(args[0]) == (op->opcode == IGUANA_OP_IF) )
{ {
val = 1; val = 1;
printf("OP_IF enabled depth.%d\n",stacks->stackdepth); //printf("OP_IF enabled depth.%d\n",stacks->stackdepth);
} }
else else
{ {
val = -1; 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; stacks->lastpath[++stacks->ifdepth] = val;
} }
@ -1172,7 +1173,7 @@ int32_t bitcoin_assembler(struct iguana_info *coin,cJSON *logarray,uint8_t scrip
errs++; errs++;
} }
stacks->lastpath[stacks->ifdepth] *= -1; 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; break;
case IGUANA_OP_ENDIF: case IGUANA_OP_ENDIF:
@ -1182,7 +1183,7 @@ int32_t bitcoin_assembler(struct iguana_info *coin,cJSON *logarray,uint8_t scrip
errs++; errs++;
} }
stacks->ifdepth--; 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; break;
case IGUANA_OP_VERIFY: case IGUANA_OP_VERIFY:
break; break;
@ -1199,16 +1200,15 @@ int32_t bitcoin_assembler(struct iguana_info *coin,cJSON *logarray,uint8_t scrip
{ {
if ( stacks->lastpath[stacks->ifdepth] < 0 ) 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 ) if ( stacks->logarray )
jaddistr(stacks->logarray,"skip"); jaddistr(stacks->logarray,"skip");
continue; 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 ) if ( op->opcode <= IGUANA_OP_16 || ++numops <= MAX_OPS_PER_SCRIPT )
{ {
script[k++] = op->opcode;
if ( (op->flags & IGUANA_ALWAYSILLEGAL) != 0 ) if ( (op->flags & IGUANA_ALWAYSILLEGAL) != 0 )
{ {
printf("disabled opcode.%s at offset.%ld\n",str,(long)str-(long)asmstr); 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 ) 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 ) if ( errs == 0 )
jadd(interpreter,"result",jtrue()); jadd(interpreter,"result",jtrue());
else jadd(interpreter,"result",jfalse()); 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 ) //if ( stacks->logarray != 0 )
// printf("LOG.(%s)\n",jprint(stacks->logarray,0)); // printf("LOG.(%s)\n",jprint(stacks->logarray,0));
if ( numargs > 0 ) if ( numargs > 0 )

2
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)); memset(&msgtx,0,sizeof(msgtx));
if ( V == 0 ) if ( V == 0 )
V = calloc(numinputs,sizeof(*V)), flagV = 1; 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 ( V != 0 )
{ {
if ( iguana_signrawtransaction(myinfo,coin,&msgtx,&signedtx,signedtxidp,V,numinputs,rawtx,vins,privkeys) > 0 ) if ( iguana_signrawtransaction(myinfo,coin,&msgtx,&signedtx,signedtxidp,V,numinputs,rawtx,vins,privkeys) > 0 )

18
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 ) if ( (plen= bitcoin_pubkeylen(vp->signers[0].pubkey)) > 0 )
bitcoin_address(vp->coinaddr,coin->chain->pubtype,vp->signers[0].pubkey,plen); bitcoin_address(vp->coinaddr,coin->chain->pubtype,vp->signers[0].pubkey,plen);
} }
int32_t j; for (j=0; j<vp->spendlen; j++) if ( vp->M == 0 && vp->N == 0 )
printf("%02x",vp->spendscript[j]); vp->M = vp->N = 1;
printf(" <- spendscript vin.%d\n",i);
if ( vp->coinaddr[i] != 0 && (waddr= iguana_waddresssearch(myinfo,&wacct,vp->coinaddr)) != 0 ) if ( vp->coinaddr[i] != 0 && (waddr= iguana_waddresssearch(myinfo,&wacct,vp->coinaddr)) != 0 )
{ {
vp->signers[0].privkey = waddr->privkey; 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); memcpy(vp->signers[0].pubkey,waddr->pubkey,plen);
} }
} }
if ( vp->M == 0 && vp->N == 0 )
vp->M = vp->N = 1;
} }
} }
/*for (i=0; i<msgtx->tx_out; i++) /*for (i=0; i<msgtx->tx_out; i++)
@ -972,8 +969,8 @@ int32_t iguana_interpreter(struct iguana_info *coin,cJSON *logarray,int64_t nLoc
for (vini=0; vini<numvins; vini++) for (vini=0; vini<numvins; vini++)
{ {
spendscript = iguana_spendasm(coin,V[vini].spendscript,V[vini].spendlen); spendscript = iguana_spendasm(coin,V[vini].spendscript,V[vini].spendlen);
printf("interpreter.(%s)\n",jprint(spendscript,0)); //printf("interpreter.(%s)\n",jprint(spendscript,0));
if ( (scriptlen= bitcoin_assembler(coin,logarray,script,spendscript,1,nLockTime,&V[vini])) <= 0 ) if ( (scriptlen= bitcoin_assembler(coin,logarray,script,spendscript,1,nLockTime,&V[vini])) < 0 )
{ {
errs++; errs++;
} }
@ -988,7 +985,7 @@ int32_t iguana_interpreter(struct iguana_info *coin,cJSON *logarray,int64_t nLoc
init_hexbytes_noT(str,script,scriptlen); init_hexbytes_noT(str,script,scriptlen);
jaddstr(item,"reconstructed",str); jaddstr(item,"reconstructed",str);
jaddi(logarray,item); jaddi(logarray,item);
} } else printf("scriptlen mismatch.%d vs %d or miscompare\n",scriptlen,V[vini].spendlen);
errs++; errs++;
} }
} }
@ -1115,9 +1112,10 @@ int32_t iguana_signrawtransaction(struct supernet_info *myinfo,struct iguana_inf
finalized = iguana_vininfo_create(myinfo,coin,serialized2,maxsize,msgtx,vins,numinputs,V); finalized = iguana_vininfo_create(myinfo,coin,serialized2,maxsize,msgtx,vins,numinputs,V);
if ( (complete= bitcoin_verifyvins(coin,signedtxidp,&signedtx,msgtx,serialized3,maxsize,V,SIGHASH_ALL,1)) > 0 && signedtx != 0 ) if ( (complete= bitcoin_verifyvins(coin,signedtxidp,&signedtx,msgtx,serialized3,maxsize,V,SIGHASH_ALL,1)) > 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; complete = 0;
} }
} }

Loading…
Cancel
Save