int32_t smartaddress_add(struct supernet_info *myinfo,bits256 privkey,char *BTCaddr,char *KMDaddr) { struct smartaddress *ap; int32_t i; if ( myinfo->numsmartaddrs < sizeof(myinfo->smartaddrs)/sizeof(*myinfo->smartaddrs) ) { for (i=0; i<myinfo->numsmartaddrs; i++) if ( bits256_cmp(myinfo->smartaddrs[i].privkey,privkey) == 0 ) return(-1); ap = &myinfo->smartaddrs[myinfo->numsmartaddrs++]; ap->privkey = privkey; bitcoin_pubkey33(myinfo->ctx,ap->pubkey33,privkey); calc_rmd160_sha256(ap->rmd160,ap->pubkey33,33); ap->pubkey = curve25519(privkey,curve25519_basepoint9()); char coinaddr[64]; uint8_t addrtype,rmd160[20]; bitcoin_address(coinaddr,0,ap->pubkey33,33); for (i=0; i<20; i++) printf("%02x",ap->rmd160[i]); bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr); printf(", "); for (i=0; i<20; i++) printf("%02x",rmd160[i]); printf (" <- rmd160 for %d %s vs %s\n",myinfo->numsmartaddrs,coinaddr,BTCaddr); return(myinfo->numsmartaddrs); } printf("too many smartaddresses %d vs %d\n",myinfo->numsmartaddrs,(int32_t)(sizeof(myinfo->smartaddrs)/sizeof(*myinfo->smartaddrs))); return(-1); }
uint64_t bitcoin_parseunspent(struct iguana_info *coin,struct bitcoin_unspent *unspent,double minconfirms,char *account,cJSON *item) { char *hexstr,coinaddr[64]; memset(unspent,0,sizeof(*unspent)); if ( jstr(item,"address") != 0 ) { safecopy(coinaddr,jstr(item,"address"),sizeof(coinaddr)); bitcoin_addr2rmd160(&unspent->addrtype,unspent->rmd160,coinaddr); /*sprintf(args,"[\"%s\"]",coinaddr); wifstr = bitcoind_RPC(0,coin->symbol,coin->chain->serverport,coin->chain->userpass,"dumpprivkey",args); if ( wifstr != 0 ) { 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);*/ } if ( (account == 0 || jstr(item,"account") == 0 || strcmp(account,jstr(item,"account")) == 0) && (minconfirms <= 0 || juint(item,"confirmations") >= minconfirms-SMALLVAL) ) { if ( (hexstr= jstr(item,"scriptPubKey")) != 0 ) { unspent->spendlen = (int32_t)strlen(hexstr) >> 1; if ( unspent->spendlen < sizeof(unspent->spendscript) ) decode_hex(unspent->spendscript,unspent->spendlen,hexstr); } unspent->txid = jbits256(item,"txid"); unspent->value = SATOSHIDEN * jdouble(item,"amount"); unspent->vout = jint(item,"vout"); //char str[65]; printf("(%s) -> %s %.8f scriptlen.%d\n",jprint(item,0),bits256_str(str,unspent->txid),dstr(unspent->value),unspent->scriptlen); } else printf("skip.(%s) minconfirms.%f\n",jprint(item,0),minconfirms);
cJSON *gecko_paymentsobj(struct supernet_info *myinfo,cJSON *txjson,cJSON *valsobj,int32_t reusedaddrs) { cJSON *item,*array; char *coinaddr; uint64_t satoshis; uint8_t addrtype,pubkey33[33],rmd160[20],outputscript[512]; int32_t i,n,scriptlen; uint32_t locktime,txversion; struct iguana_waddress *waddr; struct iguana_waccount *wacct; locktime = jint(valsobj,"locktime"); if ( (txversion= juint(valsobj,"txversion")) == 0 ) txversion = (locktime == 0) ? IGUANA_NORMAL_TXVERSION : IGUANA_LOCKTIME_TXVERSION; if ( txjson == 0 ) txjson = bitcoin_txcreate(1,locktime,txversion); if ( (array= jarray(&n,valsobj,"payments")) != 0 && n > 0 ) { for (i=0; i<n; i++) { item = jitem(array,i); if ( (coinaddr= jfieldname(item)) != 0 && (satoshis= j64bits(item,coinaddr)) > 0 ) { printf("payment.%s <- %.8f\n",coinaddr,dstr(satoshis)); bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr); scriptlen = 0; if ( reusedaddrs == 0 ) { if ( (waddr= iguana_waddresssearch(myinfo,&wacct,coinaddr)) != 0 ) { if ( bitcoin_pubkeylen(waddr->pubkey) > 0 ) scriptlen = bitcoin_pubkeyspend(outputscript,0,pubkey33); } } if ( scriptlen == 0 ) scriptlen = bitcoin_standardspend(outputscript,0,rmd160); bitcoin_txoutput(txjson,outputscript,scriptlen,satoshis); } } } return(txjson); }
int32_t bitcoin_validaddress(struct iguana_info *coin,char *coinaddr) { uint8_t rmd160[20],addrtype; char checkaddr[128]; if ( coin == 0 || coinaddr == 0 || coinaddr[0] == 0 ) return(-1); else if ( bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr) < 0 ) return(-1); else if ( addrtype != coin->chain->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 smartaddress(struct supernet_info *myinfo,bits256 *privkeyp,char *coinaddr) { int32_t i; uint8_t addrtype,rmd160[20]; memset(privkeyp,0,sizeof(*privkeyp)); bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr); for (i=0; i<myinfo->numsmartaddrs; i++) if ( memcmp(myinfo->smartaddrs[i].rmd160,rmd160,20) == 0 ) { *privkeyp = myinfo->smartaddrs[i].privkey; printf("MATCHED %s\n",coinaddr); return(i); } for (i=0; i<20; i++) printf("%02x",rmd160[i]); printf(" <- rmd160 smartaddress cant find (%s) of %d\n",coinaddr,myinfo->numsmartaddrs); return(-1); }
TWO_STRINGS_AND_TWO_DOUBLES(iguana,balance,activecoin,address,lastheightd,minconfd) { int32_t lastheight,minconf,maxconf=SATOSHIDEN; int64_t total=0; uint8_t rmd160[20],pubkey33[33],addrtype; struct iguana_pkhash *P; cJSON *array,*retjson = cJSON_CreateObject(); if ( activecoin != 0 && activecoin[0] != 0 ) coin = iguana_coinfind(activecoin); if ( coin != 0 ) { if ( (minconf= minconfd) <= 0 ) minconf = 1; lastheight = lastheightd; jaddstr(retjson,"address",address); if ( bitcoin_validaddress(coin,address) < 0 ) { jaddstr(retjson,"error","illegal address"); return(jprint(retjson,1)); } if ( bitcoin_addr2rmd160(&addrtype,rmd160,address) < 0 ) { jaddstr(retjson,"error","cant convert address"); return(jprint(retjson,1)); } memset(pubkey33,0,sizeof(pubkey33)); P = calloc(coin->bundlescount,sizeof(*P)); array = cJSON_CreateArray(); //printf("Start %s balance.(%s) height.%d\n",coin->symbol,address,lastheight); if ( lastheight == 0 ) lastheight = IGUANA_MAXHEIGHT; iguana_pkhasharray(myinfo,coin,array,minconf,maxconf,&total,P,coin->bundlescount,rmd160,address,pubkey33,lastheight,0,0,0); free(P); jadd(retjson,"unspents",array); jaddnum(retjson,"balance",dstr(total)); if ( lastheight > 0 ) jaddnum(retjson,"lastheight",lastheight); } return(jprint(retjson,1)); }