From 3ccea559584e4067cd40963e03bfaa72d6982d98 Mon Sep 17 00:00:00 2001 From: DeckerSU Date: Fri, 21 Sep 2018 01:53:31 +0300 Subject: [PATCH] First approach to make iguana work with 2 bytes prefix coins, like HUSH Main additions: + bitcoin_address_ex + bitcoin_addr2rmd160_ex These functions with support taddr param. For example, for HUSH prefix is { 0x1C, 0xB8 }, where is taddr = 0x1C and addrtype = 0xB8. All functions that used bitcoin_address and bitcoin_addr2rmd160 should be rewritten with new *_ex analogs. currently it's done only for functions used in split funds. Also, if we want to support other type of such coins, we should change condition "if (strcmp(coin->chain->symbol, "HUSH") == 0)" on more universal. For example we can taddr in struct iguana_chain, but this needs to rewrite all logic, for example, init structures, init static struct iguana_chain Chains[] array and other things. Adding prefix byte causes global changes in iguana. p.s. With these changes HUSH splitfund in iguana works fine, but we need to test notarizations on testnet (!) first, and change these functions calls: bitcoin_address -> bitcoin_address_ex bitcoin_addr2rmd160 -> bitcoin_addr2rmd160_ex everywhere it used. This is not finished yet. --- basilisk/basilisk_bitcoin.c | 7 +++- iguana/exchanges/bitcoin.c | 81 +++++++++++++++++++++++++++++++++++++ iguana/exchanges/bitcoin.h | 1 + iguana/iguana_payments.c | 7 +++- iguana/iguana_wallet.c | 54 +++++++++++++++++++------ includes/iguana_funcs.h | 1 + 6 files changed, 137 insertions(+), 14 deletions(-) diff --git a/basilisk/basilisk_bitcoin.c b/basilisk/basilisk_bitcoin.c index 4d15df781..be78a05e2 100755 --- a/basilisk/basilisk_bitcoin.c +++ b/basilisk/basilisk_bitcoin.c @@ -584,7 +584,12 @@ char *iguana_utxoduplicates(struct supernet_info *myinfo,struct iguana_info *coi *completedp = 0; if ( signedtxidp != 0 ) memset(signedtxidp,0,sizeof(*signedtxidp)); - bitcoin_address(changeaddr,coin->chain->pubtype,myinfo->persistent_pubkey33,33); + + if (strcmp(coin->chain->symbol, "HUSH") == 0) + bitcoin_address_ex(coin->chain->symbol, changeaddr, 0x1c, coin->chain->pubtype, myinfo->persistent_pubkey33, 33); + else + bitcoin_address(changeaddr, coin->chain->pubtype, myinfo->persistent_pubkey33, 33); + txfee = (coin->txfee + duplicates*coin->txfee/10); if ( strcmp(coin->symbol,"GAME") == 0 ) printf("GAME txfee %.8f\n",dstr(txfee)); diff --git a/iguana/exchanges/bitcoin.c b/iguana/exchanges/bitcoin.c index 62c0ed592..f7a5661fa 100755 --- a/iguana/exchanges/bitcoin.c +++ b/iguana/exchanges/bitcoin.c @@ -22,6 +22,51 @@ char *bitcoind_passthru(char *coinstr,char *serverport,char *userpass,char *meth return(bitcoind_RPC(0,coinstr,serverport,userpass,method,params,0)); } + +int32_t bitcoin_addr2rmd160_ex(char *symbol, uint8_t taddr, uint8_t *addrtypep, uint8_t rmd160[20], char *coinaddr) +{ + bits256 hash; uint8_t *buf, _buf[26], data5[128], rmd21[21]; char prefixaddr[64], hrp[64]; int32_t len, len5, offset; + *addrtypep = 0; + memset(rmd160, 0, 20); + if (coinaddr == 0 || coinaddr[0] == 0) + return(0); + if (coinaddr[0] == '0' && coinaddr[1] == 'x' && is_hexstr(coinaddr + 2, 0) == 40) // for ETH + { + decode_hex(rmd160, 20, coinaddr + 2); // not rmd160 hash but hopefully close enough; + return(20); + } + + offset = 1 + (taddr != 0); + 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, 20 + offset); + + *addrtypep = (taddr == 0) ? *buf : buf[1]; + memcpy(rmd160, buf + offset, 20); + if ((buf[20 + offset] & 0xff) == hash.bytes[31] && (buf[21 + offset] & 0xff) == hash.bytes[30] && (buf[22 + offset] & 0xff) == hash.bytes[29] && (buf[23 + offset] & 0xff) == hash.bytes[28]) + { + //printf("coinaddr.(%s) valid checksum addrtype.%02x\n",coinaddr,*addrtypep); + return(20); + } + else if ((strcmp(symbol, "GRS") == 0 || strcmp(symbol, "SMART") == 0) && (buf[20 + offset] & 0xff) == hash.bytes[0] && (buf[21 + offset] & 0xff) == hash.bytes[1] && (buf[22 + offset] & 0xff) == hash.bytes[2] && (buf[23 + offset] & 0xff) == hash.bytes[3]) + return(20); + else if (strcmp(symbol, "BTC") != 0 || *addrtypep == 0 || *addrtypep == 5) + { + int32_t i; + //if ( len > 20 ) + // hash = bits256_calcaddrhash(symbol,buf,len); + for (i = 0; isymbol, "HUSH") == 0) + bitcoin_addr2rmd160_ex(coin->symbol, 0x1c, &addrtype, rmd160, changeaddr); + else + bitcoin_addr2rmd160(&addrtype,rmd160,changeaddr); + spendlen = bitcoin_standardspend(spendscript,0,rmd160); bitcoin_txoutput(txobj,spendscript,spendlen,change); if ( opreturn != 0 ) diff --git a/iguana/iguana_wallet.c b/iguana/iguana_wallet.c index d633ff569..3c46ff1fb 100755 --- a/iguana/iguana_wallet.c +++ b/iguana/iguana_wallet.c @@ -321,7 +321,10 @@ cJSON *iguana_getaddressesbyaccount(struct supernet_info *myinfo,struct iguana_i { HASH_ITER(hh,subset->waddr,waddr,tmp2) { - bitcoin_address(coinaddr,coin->chain->pubtype,waddr->rmd160,20); + if (strcmp(coin->chain->symbol, "HUSH") == 0) + bitcoin_address_ex(coin->chain->symbol, coinaddr, 0x1c, coin->chain->pubtype, waddr->rmd160, 20); + else + bitcoin_address(coinaddr, coin->chain->pubtype, waddr->rmd160, 20); printf("%s ",coinaddr); jaddistr(array,coinaddr); } @@ -329,12 +332,20 @@ cJSON *iguana_getaddressesbyaccount(struct supernet_info *myinfo,struct iguana_i } else { - bitcoin_address(refaddr,coin->chain->pubtype,myinfo->persistent_pubkey33,33); - HASH_ITER(hh,myinfo->wallet,subset,tmp) + if (strcmp(coin->chain->symbol, "HUSH") == 0) + bitcoin_address_ex(coin->chain->symbol, refaddr, 0x1c, coin->chain->pubtype, myinfo->persistent_pubkey33, 33); + else + bitcoin_address(refaddr, coin->chain->pubtype, myinfo->persistent_pubkey33, 33); + + HASH_ITER(hh,myinfo->wallet,subset,tmp) { HASH_ITER(hh,subset->waddr,waddr,tmp2) { - bitcoin_address(coinaddr,coin->chain->pubtype,waddr->rmd160,20); + if (strcmp(coin->chain->symbol, "HUSH") == 0) + bitcoin_address_ex(coin->chain->symbol, coinaddr, 0x1c, coin->chain->pubtype, waddr->rmd160, 20); + else + bitcoin_address(coinaddr, coin->chain->pubtype, waddr->rmd160, 20); + jaddistr(array,coinaddr); if ( strcmp(coinaddr,refaddr) == 0 ) refaddr[0] = 0; @@ -359,17 +370,36 @@ int32_t iguana_addressvalidate(struct iguana_info *coin,uint8_t *addrtypep,char char checkaddr[64]; uint8_t rmd160[20]; *addrtypep = 0; memset(rmd160,0,sizeof(rmd160)); - bitcoin_addr2rmd160(addrtypep,rmd160,address); + + if (strcmp(coin->symbol, "HUSH") == 0) + bitcoin_addr2rmd160_ex(coin->symbol, 0x1c, addrtypep, rmd160, address); + else + bitcoin_addr2rmd160(addrtypep,rmd160,address); + //int32_t i; for (i=0; i<20; i++) // printf("%02x",rmd160[i]); // 764692cd5473f62ffa8a93e55d876f567623de07 //printf(" rmd160 addrtype.%02x\n",*addrtypep); - if ( bitcoin_address(checkaddr,*addrtypep,rmd160,20) == checkaddr && strcmp(address,checkaddr) == 0 && (*addrtypep == coin->chain->pubtype || *addrtypep == coin->chain->p2shtype) ) - return(0); - else - { - //printf(" checkaddr.(%s) address.(%s) type.%02x vs (%02x %02x)\n",checkaddr,address,*addrtypep,coin->chain->pubtype,coin->chain->p2shtype); - return(-1); - } + + if (strcmp(coin->symbol, "HUSH") == 0) + { + if (bitcoin_address_ex(coin->symbol, checkaddr, 0x1c, *addrtypep, rmd160, 20) == checkaddr && strcmp(address, checkaddr) == 0 && (*addrtypep == coin->chain->pubtype || *addrtypep == coin->chain->p2shtype)) + return(0); + else + { + //printf(" checkaddr.(%s) address.(%s) type.%02x vs (%02x %02x)\n",checkaddr,address,*addrtypep,coin->chain->pubtype,coin->chain->p2shtype); + return(-1); + } + } + else + { + if (bitcoin_address(checkaddr, *addrtypep, rmd160, 20) == checkaddr && strcmp(address, checkaddr) == 0 && (*addrtypep == coin->chain->pubtype || *addrtypep == coin->chain->p2shtype)) + return(0); + else + { + //printf(" checkaddr.(%s) address.(%s) type.%02x vs (%02x %02x)\n",checkaddr,address,*addrtypep,coin->chain->pubtype,coin->chain->p2shtype); + return(-1); + } + } } cJSON *iguana_waddressjson(struct iguana_info *coin,cJSON *item,struct iguana_waddress *waddr) diff --git a/includes/iguana_funcs.h b/includes/iguana_funcs.h index b0fa20b84..946584592 100755 --- a/includes/iguana_funcs.h +++ b/includes/iguana_funcs.h @@ -560,6 +560,7 @@ bits256 calc_categoryhashes(bits256 *subhashp,char *category,char *subcategory); struct gecko_chain *category_find(bits256 categoryhash,bits256 subhash); void *category_subscribe(struct supernet_info *myinfo,bits256 category,bits256 keyhash); char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len); +char *bitcoin_address_ex(char *symbol, char *coinaddr, uint8_t taddr, uint8_t addrtype, uint8_t *pubkey_or_rmd160, int32_t len); char *SuperNET_JSON(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *json,char *remoteaddr,uint16_t port); int64_t iguana_txdetails(struct supernet_info *myinfo,struct iguana_info *coin,cJSON *item,bits256 txid,int32_t vout,int32_t height); int32_t iguana_txidheight(struct supernet_info *myinfo,struct iguana_info *coin,bits256 txid);