Browse Source

Merge pull request #537 from jl777/spvdex

Spvdex
etomic
jl777 7 years ago
committed by GitHub
parent
commit
4d2fea0b63
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 43
      iguana/exchanges/LP_bitcoin.c
  2. 2
      iguana/exchanges/LP_commands.c
  3. 4
      iguana/exchanges/LP_include.h
  4. 2
      iguana/exchanges/LP_nativeDEX.c
  5. 59
      iguana/exchanges/LP_privkey.c
  6. 2
      iguana/exchanges/coins
  7. 5
      iguana/exchanges/coins.json
  8. 1
      iguana/exchanges/mm.c
  9. 5
      iguana/exchanges/prices/autoprice
  10. 2
      iguana/exchanges/stats.c
  11. 252
      iguana/mini-gmp.c

43
iguana/exchanges/LP_bitcoin.c

@ -2137,30 +2137,46 @@ int32_t base58encode_checkbuf(uint8_t taddr,uint8_t addrtype,uint8_t *data,int32
int32_t bitcoin_wif2priv(uint8_t wiftaddr,uint8_t *addrtypep,bits256 *privkeyp,char *wifstr)
{
int32_t offset,len = -1; bits256 hash; uint8_t buf[256];
int32_t offset,len = -1; bits256 hash; uint8_t pbuf[38],buf[256],*ptr;
offset = 1 + (wiftaddr != 0);
memset(buf,0,sizeof(buf));
memset(privkeyp,0,sizeof(*privkeyp));
if ( (len= bitcoin_base58decode(buf,wifstr)) >= 4 )
{
// validate with trailing hash, then remove hash
if ( len < 38 )
if ( len >= 32+offset )
memcpy(privkeyp,buf+offset,32);
else
{
//printf("wif %s -> buf too short len.%d\n",wifstr,len);
return(-1);
}
ptr = buf;
/*if ( len < 38 )
{
memset(pbuf,0,sizeof(pbuf));
memcpy(pbuf,buf,len-4);
memcpy(&pbuf[34],&buf[len-4],4);
ptr = pbuf;
int32_t i; for (i=0; i<38; i++)
printf("%02x ",pbuf[i]);
printf("pbuf from %d\n",len);
len = 38;
hash = bits256_doublesha256(0,buf,len - 4);
*addrtypep = (wiftaddr == 0) ? *buf : buf[1];
memcpy(privkeyp,buf+offset,32);
if ( (buf[len - 4]&0xff) == hash.bytes[31] && (buf[len - 3]&0xff) == hash.bytes[30] &&(buf[len - 2]&0xff) == hash.bytes[29] && (buf[len - 1]&0xff) == hash.bytes[28] )
}*/
hash = bits256_doublesha256(0,ptr,len - 4);
*addrtypep = (wiftaddr == 0) ? *ptr : ptr[1];
if ( (ptr[len - 4]&0xff) == hash.bytes[31] && (ptr[len - 3]&0xff) == hash.bytes[30] &&(ptr[len - 2]&0xff) == hash.bytes[29] && (ptr[len - 1]&0xff) == hash.bytes[28] )
{
//int32_t i; for (i=0; i<len; i++)
// printf("%02x ",buf[i]);
//printf(" buf, hash.%02x %02x %02x %02x ",hash.bytes[28],hash.bytes[29],hash.bytes[30],hash.bytes[31]);
// printf("%02x ",ptr[i]);
//printf(" ptr, hash.%02x %02x %02x %02x ",hash.bytes[28],hash.bytes[29],hash.bytes[30],hash.bytes[31]);
//printf("wifstr.(%s) valid len.%d\n",wifstr,len);
return(32);
}
else
else if ( 0 ) // gets errors when len is 37
{
int32_t i; for (i=0; i<len; i++)
printf("%02x ",buf[i]);
printf(" buf, hash.%02x %02x %02x %02x\n",hash.bytes[28],hash.bytes[29],hash.bytes[30],hash.bytes[31]);
printf(" buf, hash.%02x %02x %02x %02x len.%d\n",hash.bytes[28],hash.bytes[29],hash.bytes[30],hash.bytes[31],len);
}
}
return(-1);
@ -2180,12 +2196,15 @@ int32_t bitcoin_wif2addr(void *ctx,uint8_t wiftaddr,uint8_t taddr,uint8_t pubtyp
int32_t bitcoin_priv2wif(uint8_t wiftaddr,char *wifstr,bits256 privkey,uint8_t addrtype)
{
uint8_t data[128]; int32_t offset,len = 32;
memcpy(data+1,privkey.bytes,sizeof(privkey));
offset = 1 + (wiftaddr != 0);
memcpy(data+offset,privkey.bytes,sizeof(privkey));
data[offset + len++] = 1;
len = base58encode_checkbuf(wiftaddr,addrtype,data,len);
if ( bitcoin_base58encode(wifstr,data,len) == 0 )
{
char str[65]; printf("error making wif from %s\n",bits256_str(str,privkey));
return(-1);
}
if ( 1 )
{
uint8_t checktype; bits256 checkpriv; char str[65],str2[65];

2
iguana/exchanges/LP_commands.c

@ -431,7 +431,7 @@ instantdex_claim()\n\
if ( strcmp(ptr->symbol,"KMD") == 0 )
LP_importaddress("KMD",BOTS_BONDADDRESS);
}
if ( strcmp(coin,"BCH") == 0 )
if ( 0 && strcmp(coin,"BCH") == 0 )
test_validate(ptr,"010000000110b365ea6b8a9f2d56dc12de868e382dc787b2e29355f9b357dcf764c5e29cb1010000006b483045022100c605b993f1db5f31046ebb9065bea0a047f478342bbad8fcfc6af81d05236bd502206e9993a737a8814b935b5e522e750c915e7d37e3bd8367f087d4510f66acac47412102ebc786cb83de8dc3922ab83c21f3f8a2f3216940c3bf9da43ce39e2a3a882c92ffffffff014bc22900000000001976a91459fdba29ea85c65ad90f6d38f7a6646476b26b1688ac00000000");
array = cJSON_CreateArray();
jaddi(array,LP_coinjson(ptr,0));

4
iguana/exchanges/LP_include.h

@ -22,7 +22,9 @@
#define LP_INCLUDE_H
#ifdef FROMGUI
#define printf(...)
#define printf dontprintf
voind dontprintf(char *formatstr,...) {}
#endif
#define LP_MAJOR_VERSION "0"

2
iguana/exchanges/LP_nativeDEX.c

@ -17,9 +17,7 @@
// LP_nativeDEX.c
// marketmaker
//
// command_rpcloop elapsed 11369.68 millis > threshold 1000.00, ave 34.99 millis, count.645639 broadcast
// compress packets
// https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki for signing BCH/BTG
// portfolio to set prices from historical
// portfolio value based on ask?
// else claim path

59
iguana/exchanges/LP_privkey.c

@ -202,18 +202,30 @@ char *LP_secretaddresses(void *ctx,char *prefix,char *passphrase,int32_t n,uint8
return(jprint(retjson,1));
}
static const char base58_chars[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
int32_t LP_wifstr_valid(char *wifstr)
{
bits256 privkey; uint8_t wiftype; char cmpstr[128]; int32_t iter;
for (iter=0; iter<2; iter++)
bits256 privkey,cmpkey; uint8_t wiftype; char cmpstr[128],cmpstr2[128]; int32_t i;
memset(privkey.bytes,0,sizeof(privkey));
memset(cmpkey.bytes,0,sizeof(cmpkey));
for (i=0; wifstr[i]!=0; i++)
if ( strchr(base58_chars,wifstr[i]) == 0 )
return(0);
bitcoin_wif2priv(0,&wiftype,&privkey,wifstr);
bitcoin_priv2wif(0,cmpstr,privkey,wiftype);
if ( strcmp(cmpstr,wifstr) == 0 )
{
bitcoin_wif2priv(0,&wiftype,&privkey,wifstr);
bitcoin_priv2wif(0,cmpstr,privkey,wiftype);
if ( strcmp(cmpstr,wifstr) == 0 )
{
//printf("%s is valid wif\n",wifstr);
printf("%s is valid wif\n",wifstr);
return(1);
}
else if ( bits256_nonz(privkey) != 0 )
{
bitcoin_wif2priv(0,&wiftype,&cmpkey,cmpstr);
bitcoin_priv2wiflong(0,cmpstr2,privkey,wiftype);
char str[65],str2[65]; printf("mismatched wifstr %s -> %s -> %s %s %s\n",wifstr,bits256_str(str,privkey),cmpstr,bits256_str(str2,cmpkey),cmpstr2);
if ( bits256_cmp(privkey,cmpkey) == 0 )
return(1);
} //else printf("invalid wifstr.(%s) wiftype.%d cmpstr.%s\n",wifstr,wiftype,cmpstr);
}
return(0);
}
@ -235,13 +247,23 @@ bits256 LP_privkeycalc(void *ctx,uint8_t *pubkey33,bits256 *pubkeyp,struct iguan
//vcalc_sha256(0,checkkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase));
//printf("SHA256.(%s) ",bits256_str(pstr,checkkey));
//printf("privkey.(%s)\n",bits256_str(pstr,privkey));
bitcoin_priv2wif(coin->wiftaddr,tmpstr,privkey,coin->wiftype);
bitcoin_wif2priv(coin->wiftaddr,&tmptype,&checkkey,tmpstr);
if ( bits256_cmp(privkey,checkkey) != 0 )
{
char str[65],str2[65]; printf("mismatched privkeys from wif conversion: %s -> %s -> %s\n",bits256_str(str,privkey),tmpstr,bits256_str(str2,checkkey));
exit(1);
}
}
else
{
bitcoin_wif2priv(coin->wiftaddr,&tmptype,&privkey,wifstr);
bitcoin_priv2wif(coin->wiftaddr,tmpstr,privkey,tmptype);
if ( strcmp(tmpstr,wifstr) != 0 )
printf("converted %s != %s\n",tmpstr,wifstr);
{
printf("error reproducing the wifstr, likely edge case like non-supported uncompressed pubkey\n");
exit(1);
}
tmpkey = privkey;
nxtaddr = conv_NXTpassword(tmpkey.bytes,pubkeyp->bytes,0,0);
RS_encode(G.LP_NXTaddr,nxtaddr);
@ -352,4 +374,21 @@ int32_t LP_passphrase_init(char *passphrase,char *gui)
return(0);
}
void LP_privkey_tests()
{
char wifstr[64],str[65],str2[65]; bits256 privkey,checkkey; int32_t i; uint8_t tmptype;
for (i=0; i<200000000; i++)
{
privkey = rand256(0);
bitcoin_priv2wif(0,wifstr,privkey,0xff);
bitcoin_wif2priv(0,&tmptype,&checkkey,wifstr);
if ( bits256_cmp(privkey,checkkey) != 0 )
{
printf("i.%d: %s vs %s\n",i,bits256_str(str,privkey),bits256_str(str2,checkkey));
exit(-1);
}
if ( (i % 1000000) == 0 )
fprintf(stderr,"%.1f%% ",100.*(double)i/200000000);
}
printf("%d privkeys checked\n",i);
}

2
iguana/exchanges/coins

File diff suppressed because one or more lines are too long

5
iguana/exchanges/coins.json

File diff suppressed because one or more lines are too long

1
iguana/exchanges/mm.c

@ -122,6 +122,7 @@ int main(int argc, const char * argv[])
#else
if ( argc == 1 )
{
//LP_privkey_tests();
LP_NXT_redeems();
sleep(3);
return(0);

5
iguana/exchanges/prices/autoprice

@ -3,8 +3,9 @@ margin=0.02
source userpass
./auto_chipskmd
./auto_chipsbtc
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"BTC\",\"rel\":\"KMD\",\"fixed\":0.00025,\"margin\":0.01}"
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"KMD\",\"rel\":\"BTC\",\"fixed\":4000,\"margin\":0.01}"
curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"KMD\",\"rel\":\"BTC\",\"margin\":0.01,\"refbase\":\"komodo\",\"refrel\":\"coinmarketcap\"}"
#curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"BTC\",\"rel\":\"KMD\",\"fixed\":0.00025,\"margin\":0.01}"
#curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"KMD\",\"rel\":\"BTC\",\"fixed\":4000,\"margin\":0.01}"
#curl --url "http://127.0.0.1:7783" --data "{\"userpass\":\"$userpass\",\"method\":\"autoprice\",\"base\":\"KMD\",\"rel\":\"MNZ\",\"offset\":0.0,\"refbase\":\"KMD\",\"refrel\":\"BTC\",\"factor\":15000,\"margin\":-0.2}"
source crypto

2
iguana/exchanges/stats.c

@ -714,7 +714,7 @@ void LP_rpc_processreq(void *_ptr)
response = malloc(strlen(retstr)+1024+1+1);
//printf("alloc response.%p\n",response);
}
sprintf(hdrs,"HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Credentials: true\r\nAccess-Control-Allow-Methods: GET, POST\r\nCache-Control : no-cache, no-store, must-revalidate\r\n%sContent-Length : %8d\r\n\r\n",content_type,(int32_t)strlen(retstr));
sprintf(hdrs,"HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Credentials: true\r\nAccess-Control-Allow-Methods: GET, POST\r\nCache-Control : no-cache, no-store, must-revalidate\r\n%sContent-Length : %8d\r\n\r\n",content_type,(int32_t)strlen(retstr)+1);
response[0] = '\0';
strcat(response,hdrs);
strcat(response,retstr);

252
iguana/mini-gmp.c

@ -2836,89 +2836,90 @@ static int gmp_detect_endian (void)
void *mpz_export (void *r, size_t *countp, int order, size_t size, int endian,size_t nails, const mpz_t u)
{
size_t count;
mp_size_t un;
if (nails != 0)
gmp_die ("mpz_import: Nails not supported.");
assert (order == 1 || order == -1);
assert (endian >= -1 && endian <= 1);
assert (size > 0 || u->_mp_size == 0);
un = u->_mp_size;
count = 0;
if (un != 0)
{
size_t k;
uint8_t *p;
ptrdiff_t word_step;
/* The current (partial) limb. */
mp_limb_t limb;
/* The number of bytes left to to in this limb. */
size_t bytes;
/* The index where the limb was read. */
mp_size_t i;
un = GMP_ABS (un);
/* Count bytes in top limb. */
limb = u->_mp_d[un-1];
assert (limb != 0);
k = 0;
do {
k++; limb >>= CHAR_BIT;
} while (limb != 0);
count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size;
if (!r)
r = malloc (count * size);
if (endian == 0)
endian = gmp_detect_endian ();
p = (uint8_t *) r;
word_step = (order != endian) ? 2 * size : 0;
/* Process bytes from the least significant end, so point p at the
least significant word. */
if (order == 1)
{
p += size * (count - 1);
word_step = - word_step;
}
/* And at least significant byte of that word. */
if (endian == 1)
p += (size - 1);
for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step)
{
size_t j;
for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
{
if (bytes == 0)
{
if (i < un)
limb = u->_mp_d[i++];
bytes = sizeof (mp_limb_t);
}
*p = limb;
limb >>= CHAR_BIT;
bytes--;
}
}
assert (i == un);
assert (k == count);
size_t count;
mp_size_t un;
if (nails != 0)
gmp_die ("mpz_import: Nails not supported.");
assert (order == 1 || order == -1);
assert (endian >= -1 && endian <= 1);
assert (size > 0 || u->_mp_size == 0);
un = u->_mp_size;
count = 0;
if (un != 0)
{
size_t k;
uint8_t *p;
ptrdiff_t word_step;
/* The current (partial) limb. */
mp_limb_t limb;
/* The number of bytes left to to in this limb. */
size_t bytes;
/* The index where the limb was read. */
mp_size_t i;
un = GMP_ABS (un);
/* Count bytes in top limb. */
limb = u->_mp_d[un-1];
assert (limb != 0);
k = 0;
do {
k++; limb >>= CHAR_BIT;
} while (limb != 0);
count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size;
if (!r)
r = malloc (count * size);
if (endian == 0)
endian = gmp_detect_endian ();
p = (uint8_t *) r;
word_step = (order != endian) ? 2 * size : 0;
/* Process bytes from the least significant end, so point p at the
least significant word. */
if (order == 1)
{
p += size * (count - 1);
word_step = - word_step;
}
/* And at least significant byte of that word. */
if (endian == 1)
p += (size - 1);
for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step)
{
size_t j;
for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
{
if (bytes == 0)
{
if (i < un)
limb = u->_mp_d[i++];
bytes = sizeof (mp_limb_t);
}
*p = limb;
//printf("{%02x} ",*p);
limb >>= CHAR_BIT;
bytes--;
}
}
assert (i == un);
assert (k == count);
}
if (countp)
*countp = count;
return r;
if (countp)
*countp = count;
//printf("mpz_export.%d\n",(int32_t)count);
return r;
}
/////////////////////////////////
@ -4360,44 +4361,6 @@ char *bitcoin_base58encode(char *coinaddr,uint8_t *data,int32_t datalen)
return(coinaddr);
}
int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr)
{
uint32_t zeroes,be_sz=0; size_t count; const char *p,*p1; mpz_t bn58,bn;
mpz_init_set_ui(bn58,58);
mpz_init_set_ui(bn,0);
while ( isspace((uint32_t)(*coinaddr & 0xff)) )
coinaddr++;
for (p=coinaddr; *p; p++)
{
p1 = strchr(base58_chars,*p);
if ( p1 == 0 )
{
while (isspace((uint32_t)*p))
p++;
if ( *p != '\0' )
{
mpz_clear(bn), mpz_clear(bn58);
return(-1);
}
break;
}
mpz_mul(bn,bn,bn58);
mpz_add_ui(bn,bn,(int32_t)(p1 - base58_chars));
}
zeroes = 0;
for (p=coinaddr; *p==base58_chars[0]; p++)
data[zeroes++] = 0;
mpz_export(data+zeroes,&count,1,sizeof(data[0]),-1,0,bn);
if ( count >= 2 && data[count - 1] == 0 && data[count - 2] >= 0x80 )
count--;
be_sz = (uint32_t)count + (uint32_t)zeroes;
//memset(data,0,be_sz);
//for (i=0; i<count; i++)
// data[i+zeroes] = revdata[count - 1 - i];
//printf(" count.%d len.%d be_sz.%d zeroes.%d data[0] %02x %02x\n",(int32_t)count,be_sz+zeroes,be_sz,zeroes,data[0],data[1]);
mpz_clear(bn), mpz_clear(bn58);
return(be_sz);
}
#include "../includes/curve25519.h"
@ -4423,6 +4386,55 @@ bits256 mpz_to_bits256(mpz_t bn)
return(x);
}
int32_t bitcoin_base58decode(uint8_t *data,char *coinaddr)
{
uint32_t zeroes,be_sz=0; size_t count; const char *p,*p1; mpz_t bn58,bn; int32_t nonz=0;
mpz_init_set_ui(bn58,58);
mpz_init_set_ui(bn,0);
while ( isspace((uint32_t)(*coinaddr & 0xff)) )
coinaddr++;
for (p=coinaddr; *p; p++)
{
p1 = strchr(base58_chars,*p);
if ( p1 == 0 )
{
while (isspace((uint32_t)*p))
p++;
if ( *p != '\0' )
{
printf("bitcoin_base58decode error: p %02x != 0x00\n",*p);
mpz_clear(bn), mpz_clear(bn58);
return(-1);
}
break;
}
mpz_mul(bn,bn,bn58);
mpz_add_ui(bn,bn,(int32_t)(p1 - base58_chars));
//printf("%d ",(int32_t)(p1 - base58_chars));
nonz++;
}
//printf("nonz.%d\n",nonz);
zeroes = 0;
for (p=coinaddr; *p==base58_chars[0]; p++)
data[zeroes++] = 0;
mpz_export(data+zeroes,&count,1,sizeof(data[0]),-1,0,bn);
/*if ( 0 )
{
bits256 privkey; char *bits256_str(char *str,bits256 privkey);
privkey = mpz_to_bits256(bn);
char str[65]; printf("bn -> %s\n",bits256_str(str,privkey));
}*/
if ( count >= 2 && data[count - 1] == 0 && data[count - 2] >= 0x80 )
count--;
be_sz = (uint32_t)count + (uint32_t)zeroes;
//memset(data,0,be_sz);
//for (i=0; i<count; i++)
// data[i+zeroes] = revdata[count - 1 - i];
//printf(" count.%d len.%d be_sz.%d zeroes.%d data[0] %02x %02x\n",(int32_t)count,be_sz+zeroes,be_sz,zeroes,data[0],data[1]);
mpz_clear(bn), mpz_clear(bn58);
return(be_sz);
}
bits256 mpz_muldivcmp(bits256 oldval,int32_t mulval,int32_t divval,bits256 targetval)
{
mpz_t bn,target; bits256 newval;

Loading…
Cancel
Save