Browse Source

Pubkey sig

beta
jl777 7 years ago
parent
commit
1d8a3fd504
  1. 12
      iguana/exchanges/LP_include.h
  2. 3
      iguana/exchanges/LP_nativeDEX.c
  3. 45
      iguana/exchanges/LP_prices.c
  4. 77
      iguana/exchanges/LP_signatures.c
  5. 4
      iguana/exchanges/LP_utxo.c

12
iguana/exchanges/LP_include.h

@ -275,6 +275,18 @@ struct basilisk_swap
}; };
#define LP_MAXPRICEINFOS 256
struct LP_pubkeyinfo
{
UT_hash_handle hh;
bits256 pubkey;
double matrix[LP_MAXPRICEINFOS][LP_MAXPRICEINFOS];
uint32_t timestamp,istrusted,numerrors;
uint8_t rmd160[20],sig[76],pubsecp[33],siglen;
};
void LP_pubkey_sigcheck(struct LP_pubkeyinfo *pubp,cJSON *item);
int32_t LP_pubkey_sigadd(cJSON *item,bits256 priv,bits256 pub,uint8_t *rmd160,uint8_t *pubsecp);
void LP_swap_coinaddr(struct iguana_info *coin,char *coinaddr,uint64_t *valuep,uint8_t *data,int32_t datalen,int32_t vout); void LP_swap_coinaddr(struct iguana_info *coin,char *coinaddr,uint64_t *valuep,uint8_t *data,int32_t datalen,int32_t vout);
void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx); void basilisk_dontforget_update(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx);
uint32_t basilisk_requestid(struct basilisk_request *rp); uint32_t basilisk_requestid(struct basilisk_request *rp);

3
iguana/exchanges/LP_nativeDEX.c

@ -37,6 +37,7 @@ int32_t num_Reserved_msgs,max_Reserved_msgs;
struct LP_peerinfo *LP_peerinfos,*LP_mypeer; struct LP_peerinfo *LP_peerinfos,*LP_mypeer;
struct LP_forwardinfo *LP_forwardinfos; struct LP_forwardinfo *LP_forwardinfos;
struct iguana_info *LP_coins; struct iguana_info *LP_coins;
struct LP_pubkeyinfo *LP_pubkeyinfos;
#include "LP_network.c" #include "LP_network.c"
char *activecoins[] = { "BTC", "KMD" }; char *activecoins[] = { "BTC", "KMD" };
@ -497,7 +498,7 @@ int32_t LP_mainloop_iter(void *ctx,char *myipaddr,struct LP_peerinfo *mypeer,int
if ( IAMLP == 0 ) if ( IAMLP == 0 )
continue; continue;
} }
if ( now > peer->lastpeers+60 )//|| (rand() % 10000) == 0 ) if ( now > peer->lastpeers+60 || (rand() % 10000) == 0 )
{ {
if ( strcmp(peer->ipaddr,myipaddr) != 0 ) if ( strcmp(peer->ipaddr,myipaddr) != 0 )
{ {

45
iguana/exchanges/LP_prices.c

@ -20,7 +20,6 @@
struct LP_orderbookentry { bits256 pubkey; double price; uint64_t minsatoshis,maxsatoshis; uint32_t timestamp; int32_t numutxos; char coinaddr[64]; }; struct LP_orderbookentry { bits256 pubkey; double price; uint64_t minsatoshis,maxsatoshis; uint32_t timestamp; int32_t numutxos; char coinaddr[64]; };
#define LP_MAXPRICEINFOS 256
struct LP_priceinfo struct LP_priceinfo
{ {
char symbol[16]; char symbol[16];
@ -48,15 +47,6 @@ struct LP_cacheinfo
uint32_t timestamp; uint32_t timestamp;
} *LP_cacheinfos; } *LP_cacheinfos;
struct LP_pubkeyinfo
{
UT_hash_handle hh;
bits256 pubkey;
double matrix[LP_MAXPRICEINFOS][LP_MAXPRICEINFOS];
uint32_t timestamp,istrusted,numerrors;
uint8_t rmd160[20],pubsecp[33];
} *LP_pubkeyinfos;
int32_t LP_pricevalid(double price) int32_t LP_pricevalid(double price)
{ {
if ( price > SMALLVAL && isnan(price) == 0 && price < SATOSHIDEN ) if ( price > SMALLVAL && isnan(price) == 0 && price < SATOSHIDEN )
@ -258,7 +248,7 @@ uint64_t LP_unspents_metric(struct iguana_info *coin,char *coinaddr)
cJSON *LP_pubkeyjson(struct LP_pubkeyinfo *pubp) cJSON *LP_pubkeyjson(struct LP_pubkeyinfo *pubp)
{ {
int32_t baseid,relid,i,j; char *base,hexstr[67],hexstr2[67]; double price; cJSON *item,*array,*obj; int32_t baseid,relid,i,j; char *base,hexstr[67],hexstr2[67],sigstr[256]; double price; cJSON *item,*array,*obj;
obj = cJSON_CreateObject(); obj = cJSON_CreateObject();
array = cJSON_CreateArray(); array = cJSON_CreateArray();
for (baseid=0; baseid<LP_numpriceinfos; baseid++) for (baseid=0; baseid<LP_numpriceinfos; baseid++)
@ -290,7 +280,13 @@ cJSON *LP_pubkeyjson(struct LP_pubkeyinfo *pubp)
{ {
init_hexbytes_noT(hexstr2,pubp->pubsecp,sizeof(pubp->pubsecp)); init_hexbytes_noT(hexstr2,pubp->pubsecp,sizeof(pubp->pubsecp));
jaddstr(obj,"pubsecp",hexstr2); jaddstr(obj,"pubsecp",hexstr2);
if ( pubp->siglen > 0 )
{
init_hexbytes_noT(sigstr,pubp->sig,pubp->siglen);
jaddstr(obj,"sig",sigstr);
}
//printf("nonz rmd160 (%s %s)\n",hexstr,hexstr2); //printf("nonz rmd160 (%s %s)\n",hexstr,hexstr2);
//LP_pubkey_sigadd(obj,pubp->pubkey,pubp->rmd160,pubp->pubsecp);
break; break;
} }
} }
@ -316,32 +312,19 @@ char *LP_prices()
void LP_prices_parse(struct LP_peerinfo *peer,cJSON *obj) void LP_prices_parse(struct LP_peerinfo *peer,cJSON *obj)
{ {
static uint8_t zeroes[20]; struct LP_pubkeyinfo *pubp; struct LP_priceinfo *basepp,*relpp; uint32_t timestamp; bits256 pubkey; cJSON *asks,*item; uint8_t rmd160[20]; int32_t i,n,relid,mismatch; char *base,*rel,*hexstr; double askprice; uint32_t now;
struct LP_pubkeyinfo *pubp; struct LP_priceinfo *basepp,*relpp; uint32_t timestamp; bits256 pubkey; cJSON *asks,*item; uint8_t rmd160[20]; int32_t i,n,relid,mismatch; char *base,*rel,*hexstr,*pubsecpstr; double askprice; uint32_t now;
now = (uint32_t)time(NULL); now = (uint32_t)time(NULL);
pubkey = jbits256(obj,"pubkey"); pubkey = jbits256(obj,"pubkey");
if ( bits256_nonz(pubkey) != 0 && (pubp= LP_pubkeyadd(pubkey)) != 0 ) if ( bits256_nonz(pubkey) != 0 && (pubp= LP_pubkeyadd(pubkey)) != 0 )
{ {
if ( (hexstr= jstr(obj,"rmd160")) != 0 && strlen(hexstr) == 2*sizeof(rmd160) ) if ( (hexstr= jstr(obj,"rmd160")) != 0 && strlen(hexstr) == 2*sizeof(rmd160) )
{
decode_hex(rmd160,sizeof(rmd160),hexstr); decode_hex(rmd160,sizeof(rmd160),hexstr);
if ( memcmp(pubp->rmd160,rmd160,sizeof(rmd160)) != 0 ) if ( memcmp(pubp->rmd160,rmd160,sizeof(rmd160)) != 0 )
mismatch = 1; mismatch = 1;
else mismatch = 0; else mismatch = 0;
if ( bits256_cmp(pubkey,G.LP_mypub25519) == 0 && mismatch == 0 ) if ( bits256_cmp(pubkey,G.LP_mypub25519) == 0 && mismatch == 0 )
peer->needping = 0; peer->needping = 0;
if ( mismatch != 0 && memcmp(zeroes,rmd160,sizeof(rmd160)) != 0 ) LP_pubkey_sigcheck(pubp,obj);
{
for (i=0; i<20; i++)
printf("%02x",pubp->rmd160[i]);
memcpy(pubp->rmd160,rmd160,sizeof(pubp->rmd160));
if ( (pubsecpstr= jstr(obj,"pubsecp")) != 0 && is_hexstr(pubsecpstr,0) == 66 )
{
decode_hex(pubp->pubsecp,sizeof(pubp->pubsecp),pubsecpstr);
char str[65]; printf(" -> rmd160.(%s) for %s (%s)\n",hexstr,bits256_str(str,pubkey),pubsecpstr);
}
}
}
timestamp = juint(obj,"timestamp"); timestamp = juint(obj,"timestamp");
if ( timestamp > now ) if ( timestamp > now )
timestamp = now; timestamp = now;

77
iguana/exchanges/LP_signatures.c

@ -328,6 +328,40 @@ char *LP_postprice_recv(cJSON *argjson)
return(clonestr("{\"error\":\"missing fields in posted price\"}")); return(clonestr("{\"error\":\"missing fields in posted price\"}"));
} }
bits256 LP_pubkey_sighash(bits256 pub,uint8_t *rmd160,uint8_t *pubsecp)
{
uint8_t buf[sizeof(pub) + 20 + 33]; bits256 sighash;
memcpy(buf,pub.bytes,sizeof(pub));
memcpy(&buf[sizeof(pub)],rmd160,20);
memcpy(&buf[sizeof(pub)+20],pubsecp,33);
vcalc_sha256(0,sighash.bytes,buf,sizeof(buf));
return(sighash);
}
int32_t LP_pubkey_sigadd(cJSON *item,bits256 priv,bits256 pub,uint8_t *rmd160,uint8_t *pubsecp)
{
static void *ctx;
uint8_t sig[128]; int32_t siglen=0; bits256 sighash; char sigstr[256];
sighash = LP_pubkey_sighash(pub,rmd160,pubsecp);
if ( ctx == 0 )
ctx = bitcoin_ctx();
if ( (siglen= bitcoin_sign(ctx,"sigadd",sig,sighash,priv,0)) > 0 && siglen < 76 )
{
init_hexbytes_noT(sigstr,sig,siglen);
jaddstr(item,"sig",sigstr);
return(siglen);
} else return(0);
}
int32_t _LP_pubkey_sigcheck(uint8_t *sig,int32_t siglen,bits256 pub,uint8_t *rmd160,uint8_t *pubsecp)
{
static void *ctx;
bits256 sighash = LP_pubkey_sighash(pub,rmd160,pubsecp);
if ( ctx == 0 )
ctx = bitcoin_ctx();
return(bitcoin_verify(ctx,sig,siglen,sighash,pubsecp,33));
}
void LP_notify_pubkeys(void *ctx,int32_t pubsock) void LP_notify_pubkeys(void *ctx,int32_t pubsock)
{ {
bits256 zero; char secpstr[67]; cJSON *reqjson = cJSON_CreateObject(); bits256 zero; char secpstr[67]; cJSON *reqjson = cJSON_CreateObject();
@ -338,25 +372,48 @@ void LP_notify_pubkeys(void *ctx,int32_t pubsock)
jaddbits256(reqjson,"pub",G.LP_mypub25519); jaddbits256(reqjson,"pub",G.LP_mypub25519);
init_hexbytes_noT(secpstr,G.LP_pubsecp,33); init_hexbytes_noT(secpstr,G.LP_pubsecp,33);
jaddstr(reqjson,"pubsecp",secpstr); jaddstr(reqjson,"pubsecp",secpstr);
LP_pubkey_sigadd(reqjson,G.LP_mypriv25519,G.LP_mypub25519,G.LP_myrmd160,G.LP_pubsecp);
LP_reserved_msg("","",zero,jprint(reqjson,1)); LP_reserved_msg("","",zero,jprint(reqjson,1));
} }
char *LP_notify_recv(cJSON *argjson) void LP_pubkey_sigcheck(struct LP_pubkeyinfo *pubp,cJSON *item)
{ {
char *rmd160str,*secpstr; bits256 pub; struct LP_pubkeyinfo *pubp; //double millis = OS_milliseconds(); int32_t i,siglen,len; uint8_t rmd160[20],pubsecp[33],sig[128],zeroes[20]; char *sigstr,*hexstr,*pubsecpstr;
pub = jbits256(argjson,"pub"); if ( (hexstr= jstr(item,"rmd160")) != 0 && strlen(hexstr) == 2*sizeof(rmd160) )
// LP_checksig
if ( bits256_nonz(pub) != 0 && (rmd160str= jstr(argjson,"rmd160")) != 0 && strlen(rmd160str) == 40 )
{ {
if ( (pubp= LP_pubkeyadd(pub)) != 0 ) decode_hex(rmd160,sizeof(rmd160),hexstr);
memset(zeroes,0,sizeof(zeroes));
if ( memcmp(zeroes,rmd160,sizeof(rmd160)) != 0 )
{ {
decode_hex(pubp->rmd160,20,rmd160str); if ( (pubsecpstr= jstr(item,"pubsecp")) != 0 && is_hexstr(pubsecpstr,0) == 66 )
if ( (secpstr= jstr(argjson,"pubsecp")) != 0 )
{ {
decode_hex(pubp->pubsecp,sizeof(pubp->pubsecp),secpstr); decode_hex(pubsecp,sizeof(pubsecp),pubsecpstr);
//printf("got pubkey.(%s)\n",secpstr); if ( (sigstr= jstr(item,"sig")) != 0 && (len= is_hexstr(sigstr,0)) > 70*2 && len < 76*2 )
{
siglen = len >> 1;
decode_hex(sig,siglen,sigstr);
if ( _LP_pubkey_sigcheck(sig,siglen,pubp->pubkey,rmd160,pubsecp) == 0 )
{
for (i=0; i<20; i++)
printf("%02x",pubp->rmd160[i]);
memcpy(pubp->rmd160,rmd160,sizeof(pubp->rmd160));
memcpy(pubp->pubsecp,pubsecp,sizeof(pubp->pubsecp));
char str[65]; printf(" -> rmd160.(%s) for %s (%s) sig.%s\n",hexstr,bits256_str(str,pubp->pubkey),pubsecpstr,sigstr);
} else printf("sig error\n");
}
} }
} }
}
}
char *LP_notify_recv(cJSON *argjson)
{
bits256 pub; struct LP_pubkeyinfo *pubp;
pub = jbits256(argjson,"pub");
if ( bits256_nonz(pub) != 0 )
{
if ( (pubp= LP_pubkeyadd(pub)) != 0 )
LP_pubkey_sigcheck(pubp,argjson);
//char str[65]; printf("%.3f NOTIFIED pub %s rmd160 %s\n",OS_milliseconds()-millis,bits256_str(str,pub),rmd160str); //char str[65]; printf("%.3f NOTIFIED pub %s rmd160 %s\n",OS_milliseconds()-millis,bits256_str(str,pub),rmd160str);
} }
return(clonestr("{\"result\":\"success\",\"notify\":\"received\"}")); return(clonestr("{\"result\":\"success\",\"notify\":\"received\"}"));

4
iguana/exchanges/LP_utxo.c

@ -787,8 +787,8 @@ uint64_t LP_txvalue(char *coinaddr,char *symbol,bits256 txid,int32_t vout)
int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol,bits256 txid,int32_t vout,uint64_t satoshis,bits256 txid2,int32_t vout2) int32_t LP_iseligible(uint64_t *valp,uint64_t *val2p,int32_t iambob,char *symbol,bits256 txid,int32_t vout,uint64_t satoshis,bits256 txid2,int32_t vout2)
{ {
//struct LP_utxoinfo *utxo; //struct LP_utxoinfo *utxo; struct LP_address_utxo *up;
struct LP_address_utxo *up; uint64_t val,val2=0,txfee,threshold=0; int32_t bypass = 0; char destaddr[64],destaddr2[64],str[65]; struct iguana_info *coin = LP_coinfind(symbol); uint64_t val,val2=0,txfee,threshold=0; int32_t bypass = 0; char destaddr[64],destaddr2[64],str[65]; struct iguana_info *coin = LP_coinfind(symbol);
if ( bits256_nonz(txid) == 0 || bits256_nonz(txid2) == 0 ) if ( bits256_nonz(txid) == 0 || bits256_nonz(txid2) == 0 )
{ {
printf("null txid not eligible\n"); printf("null txid not eligible\n");

Loading…
Cancel
Save