Browse Source

test

release/v0.1
jl777 9 years ago
parent
commit
7edc53a9e7
  1. 18
      iguana/exchanges777.h
  2. 2
      iguana/iguana_instantdex.c
  3. 129
      iguana/swaps/iguana_BTCswap.c

18
iguana/exchanges777.h

@ -24,20 +24,13 @@
#define INSTANTDEX_DECKSIZE 777
#define INSTANTDEX_HOPS 2
#define INSTANTDEX_DURATION 30
#define INSTANTDEX_DURATION 60
#define INSTANTDEX_ORDERSTATE_IDLE 0
#define INSTANTDEX_ORDERSTATE_HAVEOTHERFEE 1
#define INSTANTDEX_ORDERSTATE_HAVEDEPOSIT 2
#define INSTANTDEX_ORDERSTATE_HAVEPAYMENT 4
#define INSTANTDEX_ORDERSTATE_HAVEALTPAYMENT 8
/*#define INSTANTDEX_ORDERSTATE_PENDING 1
#define INSTANTDEX_ORDERSTATE_BOBSENTDEPOSIT 2
#define INSTANTDEX_ORDERSTATE_ALICESENTALT 3
#define INSTANTDEX_ORDERSTATE_BOBSENTBTC 4
#define INSTANTDEX_ORDERSTATE_ALICECLAIMED 5
#define INSTANTDEX_ORDERSTATE_BOBCLAIMED 6
#define INSTANTDEX_ORDERSTATE_CANCELLED 7*/
#define INSTANTDEX_ORDERSTATE_ORDERIDMASK (~(uint64_t)15)
#define INSTANTDEX_INSURANCEDIV ((7 * INSTANTDEX_DECKSIZE) >> 3)
@ -48,7 +41,7 @@
#define INSTANTDEX_BTCD "RThtXup6Zo7LZAi8kRWgjAyi1s4u6U9Cpf"
#define INSTANTDEX_MINPERC 50
#define INSTANTDEX_OFFERDURATION 300
#define INSTANTDEX_OFFERDURATION 30
#define INSTANTDEX_LOCKTIME 3600
#define EXCHANGES777_MINPOLLGAP 1
@ -162,15 +155,16 @@ struct bitcoin_swapinfo
{
struct bitcoin_swapinfo *next,*prev; portable_mutex_t mutex;
queue_t eventsQ; struct bitcoin_eventitem *pollevent;
bits256 privkeys[INSTANTDEX_DECKSIZE+2],mypubs[2],otherpubs[2],privAm,pubAm,privBn,pubBn;
bits256 privkeys[INSTANTDEX_DECKSIZE+2],mypubs[2],otherpubs[2],pubA0,pubB0,pubB1,privAm,pubAm,privBn,pubBn;
bits256 myorderhash,otherorderhash,mypubkey,othertrader,bothorderhash;
uint64_t otherdeck[INSTANTDEX_DECKSIZE][2],deck[INSTANTDEX_DECKSIZE][2];
uint64_t altsatoshis,BTCsatoshis,insurance,altinsurance;
int32_t choosei,otherchoosei,cutverified,otherverifiedcut,numpubs,havestate,otherhavestate;
struct bitcoin_statetx *deposit,*payment,*altpayment,*myfee,*otherfee;
char expectedcmdstr[16],status[16],waitfortx[16];
struct instantdex_stateinfo *state; uint32_t expiration,dead,reftime,btcconfirms,altconfirms;
struct instantdex_accept mine,other; struct iguana_info *coinbtc,*altcoin;
struct instantdex_stateinfo *state; struct instantdex_accept mine,other;
struct iguana_info *coinbtc,*altcoin; uint8_t secretAm[20],secretBn[20];
uint32_t expiration,dead,reftime,btcconfirms,altconfirms,locktime;
};
struct instantdex_event { char cmdstr[24],sendcmd[16]; int16_t nextstateind; };

2
iguana/iguana_instantdex.c

@ -657,7 +657,6 @@ cJSON *instantdex_statemachinejson(struct bitcoin_swapinfo *swap)
jaddbits256(retjson,"pubAm",swap->pubAm);
jaddbits256(retjson,"privBn",swap->privBn);
jaddbits256(retjson,"pubBn",swap->pubBn);
jaddbits256(retjson,"myorderhash",swap->myorderhash);
jaddnum(retjson,"choosei",swap->choosei);
jaddnum(retjson,"cutverified",swap->cutverified);
@ -1166,6 +1165,7 @@ struct bitcoin_swapinfo *bitcoin_swapinit(struct supernet_info *myinfo,struct ex
swap->state = instantdex_statefind(BTC_states,BTC_numstates,statename);
swap->mine = *myap, swap->other = *otherap;
swap->expiration = (otherap->offer.expiration < myap->offer.expiration) ? otherap->offer.expiration : myap->offer.expiration;
swap->locktime = swap->expiration + INSTANTDEX_LOCKTIME;
swap->choosei = swap->otherchoosei = -1;
strcpy(swap->status,"pending");
vcalc_sha256(0,swap->myorderhash.bytes,(void *)&swap->mine.offer,sizeof(swap->mine.offer));

129
iguana/swaps/iguana_BTCswap.c

@ -25,32 +25,59 @@
*/
/*
both fees are standard payments: OP_DUP OP_HASH160 FEE_RMD160 OP_EQUALVERIFY OP_CHECKSIG
Alice altpayment: OP_2 <alice_pubM> <bob_pubN> 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
<now + INSTANTDEX_LOCKTIME*2> OP_CLTV OP_DROP <alice_pubA0> OP_CHECKSIG
OP_ELSE
OP_HASH160 <hash(bob_privN)> OP_EQUALVERIFY <bob_pubB0> 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
<now + INSTANTDEX_LOCKTIME> OP_CLTV OP_DROP <bob_pubB1> OP_CHECKSIG
OP_ELSE
OP_HASH160 <hash(alice_privM)> OP_EQUALVERIFY <alice_pubA0> OP_CHECKSIG
OP_ENDIF
*/
both fees are standard payments: OP_DUP OP_HASH160 FEE_RMD160 OP_EQUALVERIFY OP_CHECKSIG
Alice altpayment: OP_2 <alice_pubM> <bob_pubN> OP_2 OP_CHECKMULTISIG
Bob deposit:
OP_IF
<now + INSTANTDEX_LOCKTIME*2> OP_CLTV OP_DROP <alice_pubA0> OP_CHECKSIG
OP_ELSE
OP_HASH160 <hash(bob_privN)> OP_EQUALVERIFY <bob_pubB0> 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)
Bob paytx:
OP_IF
<now + INSTANTDEX_LOCKTIME> OP_CLTV OP_DROP <bob_pubB1> OP_CHECKSIG
OP_ELSE
OP_HASH160 <hash(alice_privM)> OP_EQUALVERIFY <alice_pubA0> OP_CHECKSIG
OP_ENDIF
Naming convention are pubAi are alice's pubkeys (seems only pubA0 and not pubA1)
pubBi are Bob's pubkeys
privN is Bob's privkey from the cut and choose deck as selected by Alice
privM is Alice's counterpart
pubN and pubM are the corresponding pubkeys for these chosen privkeys
Alice timeout event is triggered if INSTANTDEX_LOCKTIME elapses from the start of a FSM instance. Bob timeout event is triggered after INSTANTDEX_LOCKTIME*2
*/
int32_t instantdex_bobscript(uint8_t *script,int32_t n,uint32_t *locktimep,int32_t *secretstartp,struct bitcoin_swapinfo *swap,int32_t depositflag)
{
uint8_t pubkeyA[33],pubkeyB[33];
memcpy(pubkeyA+1,cltvpub.bytes,sizeof(cltvpub)), pubkeyA[0] = 0x02;
memcpy(pubkeyB+1,destpub.bytes,sizeof(destpub)), pubkeyB[0] = 0x03;
uint8_t pubkeyA[33],pubkeyB[33],*secret160; bits256 cltvpub,destpub;
*locktimep = swap->locktime;
if ( depositflag != 0 )
{
*locktimep += INSTANTDEX_LOCKTIME;
cltvpub = swap->pubA0;
destpub = swap->pubB0;
secret160 = swap->secretBn;
pubkeyA[0] = 0x02;
pubkeyB[0] = 0x03;
}
else
{
cltvpub = swap->pubB1;
destpub = swap->pubA0;
secret160 = swap->secretAm;
pubkeyA[0] = 0x03;
pubkeyB[0] = 0x02;
}
memcpy(pubkeyA+1,cltvpub.bytes,sizeof(cltvpub));
memcpy(pubkeyB+1,destpub.bytes,sizeof(destpub));
script[n++] = SCRIPT_OP_IF;
n = bitcoin_checklocktimeverify(script,n,locktime);
n = bitcoin_checklocktimeverify(script,n,*locktimep);
n = bitcoin_pubkeyspend(script,n,pubkeyA);
script[n++] = SCRIPT_OP_ELSE;
if ( secretstartp != 0 )
@ -232,7 +259,7 @@ 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)
{
int32_t n,secretstart; struct bitcoin_statetx *ptr = 0; uint8_t script[1024],secret[20]; uint32_t locktime; int64_t satoshis; char scriptstr[512];
int32_t n,secretstart; struct bitcoin_statetx *ptr = 0; uint8_t script[1024]; uint32_t locktime; int64_t satoshis; char scriptstr[512];
if ( coin == 0 )
return(0);
if ( bits256_nonz(pub1) == 0 || bits256_nonz(pub2) == 0 )
@ -240,10 +267,9 @@ struct bitcoin_statetx *instantdex_bobtx(struct supernet_info *myinfo,struct bit
printf("instantdex_bobtx null pub1.%llx or pub2.%llx\n",(long long)pub1.txid,(long long)pub2.txid);
return(0);
}
locktime = (uint32_t)(reftime + INSTANTDEX_LOCKTIME * (1 + depositflag));
calc_rmd160_sha256(secret,priv.bytes,sizeof(priv));
n = instantdex_bobscript(script,0,&secretstart,locktime,pub1,secret,pub2);
satoshis = amount + depositflag*swap->insurance*100;
n = instantdex_bobscript(script,0,&locktime,&secretstart,swap,depositflag);
printf("locktime.%u amount %.8f satoshis %.8f\n",locktime,dstr(amount),dstr(satoshis));
init_hexbytes_noT(scriptstr,script,n);
if ( (ptr= instantdex_signtx(depositflag != 0 ? "deposit" : "payment",myinfo,coin,locktime,scriptstr,satoshis,coin->txfee,swap->mine.minconfirms,swap->mine.offer.myside)) != 0 )
{
@ -256,20 +282,16 @@ struct bitcoin_statetx *instantdex_bobtx(struct supernet_info *myinfo,struct bit
int32_t instantdex_paymentverify(struct supernet_info *myinfo,struct iguana_info *coin,struct bitcoin_swapinfo *swap,cJSON *argjson,int32_t depositflag)
{
cJSON *txobj; bits256 txid; uint32_t n,locktime; int32_t i,secretstart,retval = -1; uint64_t x;
struct iguana_msgtx msgtx; uint8_t script[512],rmd160[20]; int64_t amount;
if ( coin != 0 && jstr(argjson,depositflag != 0 ? "deposit" : "payment") != 0 )
struct iguana_msgtx msgtx; uint8_t script[512]; int64_t amount;
if ( coin != 0 && swap->deposit != 0 )
{
amount = swap->BTCsatoshis + depositflag*swap->insurance*100;
if ( swap->deposit != 0 && (txobj= bitcoin_hex2json(coin,&txid,&msgtx,swap->deposit->txbytes)) != 0 )
if ( (txobj= bitcoin_hex2json(coin,&txid,&msgtx,swap->deposit->txbytes)) != 0 )
{
locktime = swap->expiration;
if ( depositflag == 0 )
memset(rmd160,0,sizeof(rmd160));
else calc_rmd160_sha256(rmd160,swap->privkeys[0].bytes,sizeof(rmd160));
n = instantdex_bobscript(script,0,&secretstart,locktime,swap->mypubs[0],rmd160,swap->otherpubs[0]);
n = instantdex_bobscript(script,0,&locktime,&secretstart,swap,depositflag);
printf("locktime.%u amount %.8f satoshis %.8f\n",locktime,dstr(amount),dstr(amount));
if ( msgtx.lock_time == locktime && msgtx.vouts[0].value == amount && n == msgtx.vouts[0].pk_scriptlen )
{
memcpy(&script[secretstart],&msgtx.vouts[0].pk_script[secretstart],20);
if ( memcmp(script,msgtx.vouts[0].pk_script,n) == 0 )
{
iguana_rwnum(0,&script[secretstart],sizeof(x),&x);
@ -467,7 +489,6 @@ void instantdex_privkeyextract(struct supernet_info *myinfo,struct bitcoin_swapi
{
if ( otherpubkey[0] == 3 )
{
//swap->privBn = swap->privkeys[i];
swap->pubBn = bitcoin_pubkey33(myinfo->ctx,pubkey,swap->privkeys[i]);
} else printf("wrong first byte.%02x\n",otherpubkey[0]);
}
@ -475,7 +496,6 @@ void instantdex_privkeyextract(struct supernet_info *myinfo,struct bitcoin_swapi
{
if ( otherpubkey[0] == 2 )
{
//swap->privAm = swap->privkeys[i];
swap->pubAm = bitcoin_pubkey33(myinfo->ctx,pubkey,swap->privkeys[i]);
} else printf("wrong first byte.%02x\n",otherpubkey[0]);
}
@ -547,7 +567,7 @@ void instantdex_swapbits256update(bits256 *txidp,cJSON *argjson,char *fieldname)
void instantdex_newjson(struct supernet_info *myinfo,struct bitcoin_swapinfo *swap,cJSON *newjson)
{
uint8_t pubkey[33]; int32_t deckflag;
uint8_t pubkey[33],*secret160; int32_t deckflag; char secretstr[41],*field;
deckflag = (newjson != 0 && swap->otherchoosei < 0) ? 1 : 0;
if ( instantdex_pubkeyargs(myinfo,swap,2 + deckflag*INSTANTDEX_DECKSIZE,myinfo->persistent_priv,swap->myorderhash,0x02+instantdex_isbob(swap)) != 2 + deckflag*INSTANTDEX_DECKSIZE )
printf("ERROR: couldnt generate pubkeys deckflag.%d\n",deckflag);
@ -555,7 +575,22 @@ void instantdex_newjson(struct supernet_info *myinfo,struct bitcoin_swapinfo *sw
if ( swap->choosei >= 0 )
jaddnum(newjson,"mychoosei",swap->choosei);
if ( swap->otherchoosei >= 0 )
{
jaddnum(newjson,"otherchoosei",swap->otherchoosei);
if ( instantdex_isbob(swap) != 0 )
{
secret160 = swap->secretBn;
field = "secretBn";
}
else
{
secret160 = swap->secretAm;
field = "secretAm";
}
calc_rmd160_sha256(secret160,swap->privkeys[swap->otherchoosei].bytes,sizeof(swap->privkeys[swap->otherchoosei]));
init_hexbytes_noT(secretstr,secret160,20);
jaddstr(newjson,field,secretstr);
}
if ( swap->myfee != 0 && jobj(newjson,"feetx") == 0 && (swap->otherhavestate & INSTANTDEX_ORDERSTATE_HAVEOTHERFEE) == 0 )
{
jaddbits256(newjson,"feetxid",swap->myfee->txid);
@ -572,6 +607,8 @@ void instantdex_newjson(struct supernet_info *myinfo,struct bitcoin_swapinfo *sw
}
jaddbits256(newjson,"A0",swap->mypubs[0]);
jaddbits256(newjson,"A1",swap->mypubs[1]);
swap->pubA0 = swap->mypubs[0];
//swap->pubA1 = swap->mypubs[1];
if ( bits256_nonz(swap->pubAm) == 0 && swap->otherchoosei >= 0 && bits256_nonz(swap->privkeys[swap->otherchoosei]) != 0 )
swap->pubAm = bitcoin_pubkey33(myinfo->ctx,pubkey,swap->privkeys[swap->otherchoosei]);
}
@ -581,6 +618,8 @@ void instantdex_newjson(struct supernet_info *myinfo,struct bitcoin_swapinfo *sw
swap->pubBn = bitcoin_pubkey33(myinfo->ctx,pubkey,swap->privkeys[swap->otherchoosei]);
jaddbits256(newjson,"B0",swap->mypubs[0]);
jaddbits256(newjson,"B1",swap->mypubs[1]);
swap->pubB0 = swap->mypubs[0];
swap->pubB1 = swap->mypubs[1];
if ( (swap->otherhavestate & INSTANTDEX_ORDERSTATE_HAVEDEPOSIT) == 0 && swap->deposit != 0 && jobj(newjson,"deposit") == 0 )
{
jaddbits256(newjson,"deposittxid",swap->deposit->txid);
@ -612,16 +651,28 @@ cJSON *instantdex_parseargjson(struct supernet_info *myinfo,struct exchange_info
{
if ( instantdex_isbob(swap) != 0 )
{
if ( jobj(argjson,"secretAm") != 0 )
decode_hex(swap->secretAm,20,jstr(argjson,"secretAm"));
instantdex_swapbits256update(&swap->otherpubs[0],argjson,"A0");
instantdex_swapbits256update(&swap->otherpubs[1],argjson,"A1");
if ( bits256_nonz(swap->otherpubs[0]) != 0 )
swap->pubA0 = swap->otherpubs[0];
//if ( bits256_nonz(swap->otherpubs[1]) != 0 )
// swap->pubA1 = swap->otherpubs[1];
instantdex_swapbits256update(&swap->pubAm,argjson,"pubAm");
instantdex_swapbits256update(&swap->privAm,argjson,"privAm");
swap->havestate |= instantdex_swaptxupdate(swap->altcoin,&swap->altpayment,argjson,"altpayment","altpaymenttxid");
}
else
{
if ( jobj(argjson,"secretBn") != 0 )
decode_hex(swap->secretAm,20,jstr(argjson,"secretBn"));
instantdex_swapbits256update(&swap->otherpubs[0],argjson,"B0");
instantdex_swapbits256update(&swap->otherpubs[1],argjson,"B1");
if ( bits256_nonz(swap->otherpubs[0]) != 0 )
swap->pubB0 = swap->otherpubs[0];
if ( bits256_nonz(swap->otherpubs[1]) != 0 )
swap->pubB1 = swap->otherpubs[1];
instantdex_swapbits256update(&swap->pubBn,argjson,"pubBn");
instantdex_swapbits256update(&swap->privBn,argjson,"privBn");
swap->havestate |= instantdex_swaptxupdate(swap->coinbtc,&swap->deposit,argjson,"deposit","deposittxid");

Loading…
Cancel
Save