From c20eacccb64e43d1081934b0d2782685b0c1d940 Mon Sep 17 00:00:00 2001 From: jl777 Date: Mon, 25 Apr 2016 17:25:31 -0500 Subject: [PATCH] test --- {iguana => deprecated}/iguana_pubkeys.c | 0 iguana/SuperNET.c | 8 +- iguana/SuperNET_keys.c | 11 +- iguana/exchanges/bitcoin.c | 246 +++++++++++------------- iguana/iguana.sources | 2 +- iguana/iguana777.h | 3 +- iguana/iguana_bundles.c | 6 +- iguana/iguana_ramchain.c | 4 +- iguana/iguana_wallet.c | 18 +- iguana/peggy_tx.c | 8 +- 10 files changed, 146 insertions(+), 160 deletions(-) rename {iguana => deprecated}/iguana_pubkeys.c (100%) diff --git a/iguana/iguana_pubkeys.c b/deprecated/iguana_pubkeys.c similarity index 100% rename from iguana/iguana_pubkeys.c rename to deprecated/iguana_pubkeys.c diff --git a/iguana/SuperNET.c b/iguana/SuperNET.c index c00fbf5d8..400eeac6a 100755 --- a/iguana/SuperNET.c +++ b/iguana/SuperNET.c @@ -898,7 +898,7 @@ cJSON *SuperNET_rosettajson(bits256 privkey,int32_t showprivs) RS_encode(str,nxt64bits); jaddstr(retjson,"RS",str); jadd64bits(retjson,"NXT",nxt64bits); - btc_priv2pub(pub,privkey.bytes); + bitcoin_pubkey33(pub,privkey); init_hexbytes_noT(str,pub,33); jaddstr(retjson,"btcpubkey",str); calc_OP_HASH160(str2,rmd160,str); @@ -908,7 +908,7 @@ cJSON *SuperNET_rosettajson(bits256 privkey,int32_t showprivs) jaddstr(retjson,"BTC",addr); if ( showprivs != 0 ) { - btc_priv2wif(wifbuf,privkey.bytes,128); + bitcoin_priv2wif(wifbuf,privkey,128); jaddstr(retjson,"BTCwif",wifbuf); } } @@ -917,7 +917,7 @@ cJSON *SuperNET_rosettajson(bits256 privkey,int32_t showprivs) jaddstr(retjson,"BTCD",addr); if ( showprivs != 0 ) { - btc_priv2wif(wifbuf,privkey.bytes,188); + bitcoin_priv2wif(wifbuf,privkey,188); jaddstr(retjson,"BTCDwif",wifbuf); } } @@ -1257,7 +1257,7 @@ THREE_STRINGS(SuperNET,survey,category,subcategory,message) STRING_ARG(SuperNET,wif2priv,wif) { bits256 privkey; char str[65]; uint8_t privkeytype; cJSON *retjson = cJSON_CreateObject(); - if ( btc_wif2priv(&privkeytype,privkey.bytes,wif) == sizeof(privkey) ) + if ( bitcoin_wif2priv(&privkeytype,&privkey,wif) == sizeof(privkey) ) { jaddstr(retjson,"result","success"); jaddstr(retjson,"privkey",bits256_str(str,privkey)); diff --git a/iguana/SuperNET_keys.c b/iguana/SuperNET_keys.c index e843a3166..bf7ab287c 100755 --- a/iguana/SuperNET_keys.c +++ b/iguana/SuperNET_keys.c @@ -255,7 +255,7 @@ int32_t _SuperNET_encryptjson(char *destfname,char *passphrase,int32_t passsize, void SuperNET_setkeys(struct supernet_info *myinfo,void *pass,int32_t passlen,int32_t dosha256) { - char pubkeystr[128]; uint8_t pubkey33[33]; bits256 hash; + uint8_t pubkey33[33]; bits256 hash; if ( dosha256 != 0 ) { memcpy(myinfo->secret,pass,passlen+1); @@ -269,10 +269,11 @@ void SuperNET_setkeys(struct supernet_info *myinfo,void *pass,int32_t passlen,in myinfo->myaddr.nxt64bits = hash.txid; } RS_encode(myinfo->myaddr.NXTADDR,myinfo->myaddr.nxt64bits); - btc_priv2pub(pubkey33,myinfo->persistent_priv.bytes); - init_hexbytes_noT(pubkeystr,pubkey33,33); - btc_coinaddr(myinfo->myaddr.BTC,0,pubkeystr); - btc_coinaddr(myinfo->myaddr.BTCD,60,pubkeystr); + bitcoin_pubkey33(pubkey33,myinfo->persistent_priv); + //btc_priv2pub(pubkey33,myinfo->persistent_priv.bytes); + //init_hexbytes_noT(pubkeystr,pubkey33,33); + bitcoin_address(myinfo->myaddr.BTC,0,pubkey33,33); + bitcoin_address(myinfo->myaddr.BTCD,60,pubkey33,33); } void SuperNET_parsemyinfo(struct supernet_info *myinfo,cJSON *msgjson) diff --git a/iguana/exchanges/bitcoin.c b/iguana/exchanges/bitcoin.c index 620d10abd..11bf7e0e9 100755 --- a/iguana/exchanges/bitcoin.c +++ b/iguana/exchanges/bitcoin.c @@ -146,80 +146,6 @@ out: return(be_sz); } -int32_t bitcoin_addr2rmd160(uint8_t *addrtypep,uint8_t rmd160[20],char *coinaddr) -{ - bits256 hash; uint8_t *buf,_buf[25]; int32_t len; - memset(rmd160,0,20); - *addrtypep = 0; - buf = _buf; - if ( (len= bitcoin_base58decode(buf,coinaddr)) >= 4 ) - { - // validate with trailing hash, then remove hash - hash = bits256_doublesha256(0,buf,len - 4); - *addrtypep = *buf; - memcpy(rmd160,buf+1,20); - 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] ) - { - //printf("coinaddr.(%s) valid checksum\n",coinaddr); - return(20); - } - else - { - char hexaddr[64]; - btc_convaddr(hexaddr,coinaddr); - //for (i=0; ichain->pubtype && addrtype != coin->chain->p2shtype ) - return(-1); - else if ( bitcoin_address(checkaddr,addrtype,rmd160,sizeof(rmd160)) != checkaddr || strcmp(checkaddr,coinaddr) != 0 ) - return(-1); - return(0); -} - EC_KEY *bitcoin_privkeyset(uint8_t *oddevenp,bits256 *pubkeyp,bits256 privkey) { BIGNUM *bn; BN_CTX *ctx = NULL; uint8_t *ptr,tmp[33]; EC_POINT *pub_key = NULL; const EC_GROUP *group; @@ -265,67 +191,6 @@ bits256 bitcoin_pubkey33(uint8_t *data,bits256 privkey) return(pubkey); } -int32_t bitcoin_priv2wif(char *wifstr,bits256 privkey,uint8_t addrtype) -{ - uint8_t data[128]; bits256 hash; int32_t i; - memcpy(data,privkey.bytes,sizeof(privkey)); - data[32] = 1; - data[0] = addrtype; - hash = bits256_doublesha256(0,data,33); - for (i=0; i<4; i++) - data[33+i] = hash.bytes[31-i]; - if ( bitcoin_base58encode(wifstr,data,33+4) == 0 ) - return(-1); - char str[65]; printf("(%s) -> wif.(%s) addrtype.%02x\n",bits256_str(str,privkey),wifstr,addrtype); - return(0); -} - -/*int32_t bitcoin_wif2priv(uint8_t *addrtypep,uint8_t privkey[32],char *wifstr) -{ - int32_t len = -1; bits256 hash; uint8_t buf[64]; - if ( (len= bitcoin_base58decode(buf,wifstr)) >= 4 ) - { - // validate with trailing hash, then remove hash - hash = bits256_doublesha256(0,buf,len - 4); - *addrtypep = *buf; - memcpy(rmd160,buf+1,20); - 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] ) - { - //printf("coinaddr.(%s) valid checksum\n",coinaddr); - return(20); - } - } - if ( (cstr= base58_decode_check(addrtypep,(const char *)wifstr)) != 0 ) - { - init_hexbytes_noT((void *)privkey,(void *)cstr->str,cstr->len); - if ( cstr->str[cstr->len-1] == 0x01 ) - cstr->len--; - memcpy(privkey,cstr->str,cstr->len); - len = (int32_t)cstr->len; - char tmp[138]; - bitcoin_priv2wif(tmp,privkey,*addrtypep); - printf("addrtype.%02x wifstr.(%llx) len.%d\n",*addrtypep,*(long long *)privkey,len); - cstr_free(cstr,true); - } - return(len); -}*/ - -struct iguana_waddress *iguana_waddresscalc(uint8_t pubtype,uint8_t wiftype,struct iguana_waddress *addr,bits256 privkey) -{ - memset(addr,0,sizeof(*addr)); - addr->privkey = privkey; - bitcoin_pubkey33(addr->pubkey,addr->privkey); - calc_rmd160_sha256(addr->rmd160,addr->pubkey,33); - bitcoin_address(addr->coinaddr,pubtype,addr->rmd160,sizeof(addr->rmd160)); - if ( bitcoin_priv2wif(addr->wifstr,addr->privkey,wiftype) == 0 ) - { - addr->wiftype = wiftype; - addr->type = pubtype; - return(addr); - } - return(0); -} - int32_t bitcoin_sign(uint8_t *sig,int32_t maxlen,uint8_t *data,int32_t datalen,bits256 privkey) { uint32_t siglen; EC_KEY *KEY; uint8_t oddeven; bits256 pubkey; int32_t retval = -1; @@ -380,6 +245,113 @@ int32_t bitcoin_verify(uint8_t *sig,int32_t siglen,uint8_t *data,int32_t datalen return(retval); } +int32_t bitcoin_addr2rmd160(uint8_t *addrtypep,uint8_t rmd160[20],char *coinaddr) +{ + bits256 hash; uint8_t *buf,_buf[25]; int32_t len; + memset(rmd160,0,20); + *addrtypep = 0; + buf = _buf; + if ( (len= bitcoin_base58decode(buf,coinaddr)) >= 4 ) + { + // validate with trailing hash, then remove hash + hash = bits256_doublesha256(0,buf,len - 4); + *addrtypep = *buf; + memcpy(rmd160,buf+1,20); + 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] ) + { + //printf("coinaddr.(%s) valid checksum\n",coinaddr); + return(20); + } + else + { + //char hexaddr[64]; + //btc_convaddr(hexaddr,coinaddr); + //for (i=0; ichain->pubtype && addrtype != coin->chain->p2shtype ) + return(-1); + else if ( bitcoin_address(checkaddr,addrtype,rmd160,sizeof(rmd160)) != checkaddr || strcmp(checkaddr,coinaddr) != 0 ) + return(-1); + return(0); +} + +int32_t bitcoin_priv2wif(char *wifstr,bits256 privkey,uint8_t addrtype) +{ + uint8_t data[128]; bits256 hash; int32_t i; + memcpy(data,privkey.bytes,sizeof(privkey)); + data[32] = 1; + data[0] = addrtype; + hash = bits256_doublesha256(0,data,33); + for (i=0; i<4; i++) + data[33+i] = hash.bytes[31-i]; + if ( bitcoin_base58encode(wifstr,data,33+4) == 0 ) + return(-1); + char str[65]; printf("(%s) -> wif.(%s) addrtype.%02x\n",bits256_str(str,privkey),wifstr,addrtype); + return((int32_t)strlen(wifstr)); +} + +int32_t bitcoin_wif2priv(uint8_t *addrtypep,bits256 *privkeyp,char *wifstr) +{ + int32_t len = -1; bits256 hash; uint8_t buf[64]; + if ( (len= bitcoin_base58decode(buf,wifstr)) >= 4 ) + { + // validate with trailing hash, then remove hash + hash = bits256_doublesha256(0,buf,len - 4); + *addrtypep = *buf; + memcpy(privkeyp,buf+1,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] ) + { + //printf("coinaddr.(%s) valid checksum\n",coinaddr); + return(32); + } + } + return(-1); +} + int32_t iguana_parsevoutobj(struct iguana_info *coin,uint8_t *serialized,int32_t maxsize,struct iguana_msgvout *vout,cJSON *voutobj) { int32_t len = 0; cJSON *skey; char *hexstr; @@ -1286,7 +1258,7 @@ uint64_t bitcoin_parseunspent(struct iguana_info *coin,struct bitcoin_unspent *u wifstr = bitcoind_RPC(0,coin->symbol,coin->chain->serverport,coin->chain->userpass,"dumpprivkey",args); if ( wifstr != 0 ) { - btc_wif2priv(&addrtype,unspent->privkeys[0].bytes,wifstr); + bitcoin_wif2priv(&addrtype,&unspent->privkeys[0],wifstr); //printf("wifstr.(%s) -> %s\n",wifstr,bits256_str(str,unspent->privkeys[0])); free(wifstr); } else fprintf(stderr,"error (%s) cant find privkey\n",coinaddr); @@ -1576,7 +1548,7 @@ char *_setVsigner(struct iguana_info *coin,struct vin_info *V,int32_t ind,char * { uint8_t addrtype; decode_hex(V->signers[ind].pubkey,(int32_t)strlen(pubstr)/2,pubstr); - btc_wif2priv(&addrtype,V->signers[ind].privkey.bytes,wifstr); + bitcoin_wif2priv(&addrtype,&V->signers[ind].privkey,wifstr); if ( addrtype != coin->chain->pubtype ) return(clonestr("{\"error\":\"invalid wifA\"}")); else return(0); diff --git a/iguana/iguana.sources b/iguana/iguana.sources index 6bc54d575..da7362e5e 100755 --- a/iguana/iguana.sources +++ b/iguana/iguana.sources @@ -2,4 +2,4 @@ #SOURCES := iguana_rpc.c SuperNET.c SuperNET_keys.c SuperNET_category.c SuperNET_hexmsg.c iguana_exchanges.c iguana_tradebots.c iguana_instantdex.c pangea_api.c pangea_bets.c cards777.c pangea_summary.c pangea_json.c pangea_hand.c poker.c ramchain_api.c iguana_tx.c iguana_wallet.c iguana_scripts.c iguana_pubkeys.c iguana_unspents.c iguana_recv.c iguana_bundles.c iguana_msg.c iguana777.c iguana_chains.c iguana_peers.c iguana_accept.c iguana_bitmap.c iguana_init.c iguana_ramchain.c iguana_blocks.c iguana_json.c main.c peggy.c peggy_consensus.c peggy_price.c peggy_update.c peggy_accts.c peggy_tx.c peggy_txind.c peggy_ramkv.c peggy_serdes.c -SOURCES := SuperNET.c iguana_bundles.c iguana_pubkeys.c main.c peggy_price.c SuperNET_category.c iguana_chains.c iguana_ramchain.c pangea_api.c peggy_ramkv.c SuperNET_hexmsg.c iguana_exchanges.c iguana_recv.c pangea_bets.c peggy_serdes.c SuperNET_keys.c iguana_html.c iguana_rpc.c pangea_hand.c peggy_tx.c cards777.c iguana_init.c iguana_scripts.c pangea_json.c peggy_txind.c iguana777.c iguana_instantdex.c iguana_tradebots.c pangea_summary.c peggy_update.c iguana_accept.c iguana_json.c iguana_tx.c peggy.c poker.c iguana_bitmap.c iguana_msg.c iguana_unspents.c peggy_accts.c ramchain_api.c iguana_blocks.c iguana_peers.c iguana_wallet.c peggy_consensus.c databases/iguana_DB.c \ No newline at end of file +SOURCES := SuperNET.c iguana_bundles.c main.c peggy_price.c SuperNET_category.c iguana_chains.c iguana_ramchain.c pangea_api.c peggy_ramkv.c SuperNET_hexmsg.c iguana_exchanges.c iguana_recv.c pangea_bets.c peggy_serdes.c SuperNET_keys.c iguana_html.c iguana_rpc.c pangea_hand.c peggy_tx.c cards777.c iguana_init.c iguana_scripts.c pangea_json.c peggy_txind.c iguana777.c iguana_instantdex.c iguana_tradebots.c pangea_summary.c peggy_update.c iguana_accept.c iguana_json.c iguana_tx.c peggy.c poker.c iguana_bitmap.c iguana_msg.c iguana_unspents.c peggy_accts.c ramchain_api.c iguana_blocks.c iguana_peers.c iguana_wallet.c peggy_consensus.c databases/iguana_DB.c \ No newline at end of file diff --git a/iguana/iguana777.h b/iguana/iguana777.h index 8078b9527..602e8e3ab 100755 --- a/iguana/iguana777.h +++ b/iguana/iguana777.h @@ -795,7 +795,8 @@ void iguana_iAkill(struct iguana_info *coin,struct iguana_peer *addr,int32_t mar cJSON *SuperNET_bits2json(uint8_t *serialized,int32_t datalen); int32_t SuperNET_sendmsg(struct supernet_info *myinfo,struct iguana_info *coin,struct iguana_peer *addr,bits256 destpub,bits256 mypriv,bits256 mypub,uint8_t *msg,int32_t len,uint8_t *data,int32_t delaymillis); int32_t category_peer(struct supernet_info *myinfo,struct iguana_peer *addr,bits256 category,bits256 subhash); -int32_t btc_wif2priv(uint8_t *addrtypep,uint8_t privkey[32],char *wifstr); +int32_t bitcoin_wif2priv(uint8_t *addrtypep,bits256 *privkeyp,char *wifstr); +int32_t bitcoin_priv2wif(char *wifstr,bits256 privkey,uint8_t addrtype); bits256 iguana_chaingenesis(bits256 genesishash,char *genesisblock,char *hashalgostr,int32_t version,uint32_t timestamp,uint32_t bits,uint32_t nonce,bits256 merkle_root); int32_t iguana_send_ConnectTo(struct iguana_info *coin,struct iguana_peer *addr); cJSON *iguana_txjson(struct iguana_info *coin,struct iguana_txid *tx,int32_t height,struct vin_info *V); diff --git a/iguana/iguana_bundles.c b/iguana/iguana_bundles.c index 6900c0f6e..5af4ca920 100755 --- a/iguana/iguana_bundles.c +++ b/iguana/iguana_bundles.c @@ -419,13 +419,13 @@ struct iguana_txid *iguana_bundletx(struct iguana_info *coin,struct iguana_bundl void iguana_bundlepurgefiles(struct iguana_info *coin,struct iguana_bundle *bp) { static const bits256 zero; - char fname[1024]; FILE *fp; int32_t hdrsi,m,j; uint32_t ipbits; + char fname[1024]; FILE *fp; int32_t hdrsi,m = 0; uint32_t ipbits = 0; if ( bp->purgetime == 0 && time(NULL) > bp->emitfinish+30 ) { //printf("purged hdrsi.[%d] lag.%ld\n",bp->hdrsi,time(NULL) - bp->emitfinish); - for (j=m=0; jpeers.active)/sizeof(*coin->peers.active); j++) + //for (j=m=0; jpeers.active)/sizeof(*coin->peers.active); j++) { - if ( (ipbits= (uint32_t)coin->peers.active[j].ipbits) != 0 ) + //if ( (ipbits= (uint32_t)coin->peers.active[j].ipbits) != 0 ) { if ( iguana_peerfname(coin,&hdrsi,GLOBAL_TMPDIR,fname,ipbits,bp->hashes[0],zero,1,1) >= 0 ) { diff --git a/iguana/iguana_ramchain.c b/iguana/iguana_ramchain.c index 8acae9c93..5ae654374 100755 --- a/iguana/iguana_ramchain.c +++ b/iguana/iguana_ramchain.c @@ -513,8 +513,8 @@ uint32_t iguana_ramchain_addspend(struct iguana_info *coin,RAMCHAIN_FUNC,bits256 //for (i=0; isequenceid = sequence; s->external = external, s->spendtxidind = txidind, s->prevout = prev_vout; diff --git a/iguana/iguana_wallet.c b/iguana/iguana_wallet.c index 744602383..8017144ea 100755 --- a/iguana/iguana_wallet.c +++ b/iguana/iguana_wallet.c @@ -15,6 +15,22 @@ #include "iguana777.h" +struct iguana_waddress *iguana_waddresscalc(uint8_t pubtype,uint8_t wiftype,struct iguana_waddress *addr,bits256 privkey) +{ + memset(addr,0,sizeof(*addr)); + addr->privkey = privkey; + bitcoin_pubkey33(addr->pubkey,addr->privkey); + calc_rmd160_sha256(addr->rmd160,addr->pubkey,33); + bitcoin_address(addr->coinaddr,pubtype,addr->rmd160,sizeof(addr->rmd160)); + if ( bitcoin_priv2wif(addr->wifstr,addr->privkey,wiftype) > 0 ) + { + addr->wiftype = wiftype; + addr->type = pubtype; + return(addr); + } + return(0); +} + void iguana_walletlock(struct supernet_info *myinfo) { memset(&myinfo->persistent_priv,0,sizeof(myinfo->persistent_priv)); @@ -273,7 +289,7 @@ bits256 iguana_str2priv(struct iguana_info *coin,char *str) n = (int32_t)strlen(str) >> 1; if ( n == sizeof(bits256) && is_hexstr(str,sizeof(bits256)) > 0 ) decode_hex(privkey.bytes,sizeof(privkey),str); - else if ( btc_wif2priv(&addrtype,privkey.bytes,str) != sizeof(bits256) ) + else if ( bitcoin_wif2priv(&addrtype,&privkey,str) != sizeof(bits256) ) { if ( (wacct= iguana_waddressfind(coin,&ind,str)) != 0 ) privkey = wacct->waddrs[ind].privkey; diff --git a/iguana/peggy_tx.c b/iguana/peggy_tx.c index 630830b81..098ec4586 100755 --- a/iguana/peggy_tx.c +++ b/iguana/peggy_tx.c @@ -301,13 +301,9 @@ int32_t peggy_univ2addr(char *coinaddr,struct peggy_univaddr *ua) int32_t peggy_addr2univ(struct peggy_univaddr *ua,char *coinaddr,char *coin) { - char hexstr[512]; uint8_t hex[21]; - if ( btc_convaddr(hexstr,coinaddr) == 0 ) + memset(ua,0,sizeof(*ua)); + if ( bitcoin_addr2rmd160(&ua->addrtype,ua->rmd160,coinaddr) > 0 ) { - decode_hex(hex,21,hexstr); - memset(ua,0,sizeof(*ua)); - ua->addrtype = hex[0]; - memcpy(ua->rmd160,hex+1,20); strncpy(ua->coin,coin,sizeof(ua->coin)-1); return(0); }