From 2fd999a2f674168cb34947f012f5f640871928cd Mon Sep 17 00:00:00 2001 From: Mihailo Milenkovic Date: Fri, 26 Oct 2018 13:00:16 +0200 Subject: [PATCH] Multisig withdraw fix --- src/cc/CCGateways.h | 3 +- src/cc/dapps/oraclefeed.c | 112 +++++++++++++++++++++++--------------- src/cc/gateways.cpp | 85 +++++++++++++++++++++-------- src/rpcserver.cpp | 1 + src/rpcserver.h | 1 + src/wallet/rpcwallet.cpp | 34 +++++++++--- 6 files changed, 162 insertions(+), 74 deletions(-) diff --git a/src/cc/CCGateways.h b/src/cc/CCGateways.h index b584ba5d0..70c50dcdb 100644 --- a/src/cc/CCGateways.h +++ b/src/cc/CCGateways.h @@ -27,7 +27,8 @@ std::string GatewaysClaim(uint64_t txfee,uint256 bindtxid,std::string refcoin,ui std::string GatewaysWithdraw(uint64_t txfee,uint256 bindtxid,std::string refcoin,std::vector withdrawpub,int64_t amount); UniValue GatewaysPendingWithdraws(uint256 bindtxid,std::string refcoin); std::string GatewaysMarkdone(uint64_t txfee,uint256 withdrawtxid,std::string refcoin,uint256 cointxid); -std::string GatewaysMultisig(uint64_t txfee,std::string refcoin,uint256 bindtxid,uint256 withdrawtxid,char *txidaddr); +std::string GatewaysMultisig(char *txidaddr); +std::string GatewaysPartialSign(uint64_t txfee,char* txidaddr,std::string refcoin, std::string hex); // CCcustom UniValue GatewaysInfo(uint256 bindtxid); diff --git a/src/cc/dapps/oraclefeed.c b/src/cc/dapps/oraclefeed.c index d9bda7309..e85206071 100644 --- a/src/cc/dapps/oraclefeed.c +++ b/src/cc/dapps/oraclefeed.c @@ -615,6 +615,8 @@ char *createmultisig(char *refcoin,char *acname,char *depositaddr,char *signerad } else if ( txstr == 0 ) printf("createmultisig: null txstr and JSON2\n"); + free(tmpA); + free(tmpB); free(argA); free(argB); } @@ -646,10 +648,10 @@ cJSON *addmultisignature(char *refcoin,char *acname,char *signeraddr,char *rawtx return(0); } -char *get_gatewaysmultisig(char *refcoin,char *acname,char *bindtxidstr,char *withtxidstr,char *txidaddr) +char *get_gatewaysmultisig(char *refcoin,char *acname,char *txidaddr) { char *retstr,*hexstr,*hex=0; cJSON *retjson; - if ( (retjson= get_komodocli("KMD",&retstr,acname,"gatewaysmultisig",bindtxidstr,refcoin,withtxidstr,txidaddr)) != 0 ) + if ( (retjson= get_komodocli("KMD",&retstr,acname,"gatewaysmultisig",txidaddr,"","","")) != 0 ) { if ( (hexstr= jstr(retjson,"hex")) != 0 ) hex = clonestr(hexstr); @@ -658,6 +660,21 @@ char *get_gatewaysmultisig(char *refcoin,char *acname,char *bindtxidstr,char *wi return(hex); } +int32_t gatewayspartialsign(char *refcoin,char *acname,char *txidaddr,char *hex) +{ + char str[65],str2[65],*retstr; cJSON *retjson; + if ( (retjson= get_komodocli(refcoin,&retstr,acname,"gatewayspartialsign",txidaddr,refcoin,hex,"")) != 0 ) + { + komodobroadcast(refcoin,acname,retjson); + return(jint(retjson,"rank")); + } + else if ( retstr != 0 ) + { + printf("error parsing gatewaysmarkdone.(%s)\n",retstr); + free(retstr); + } +} + void gatewaysmarkdone(char *refcoin,char *acname,bits256 withtxid,char *coin,bits256 cointxid) { char str[65],str2[65],*retstr; cJSON *retjson; @@ -733,13 +750,20 @@ int32_t tx_has_voutaddress(char *refcoin,char *acname,bits256 txid,char *coinadd return(retval); } -int32_t coinaddrexists(char *refcoin,char *acname,char *coinaddr) +int32_t sendfromthisnode(char *refcoin,char *acname,char *coinaddr) { - cJSON *array; bits256 txid; int32_t i,n,num=0; + cJSON *array,*item; bits256 txid; int32_t i,n,num=0; char *tmptxid,*retstr; if ( (array= get_addressutxos(refcoin,acname,coinaddr)) != 0 ) { - num = cJSON_GetArraySize(array); - free_json(array); + for (i=0; i","amount":0.0001},{"address":"","amount":}]' - txid = sendtoaddress("KMD",acname,txidaddr,10000); - if ( bits256_nonz(txid) != 0 && coinaddrexists("KMD",acname,txidaddr) > 0 ) + if ( (satoshis= jdouble(item,"amount")*SATOSHIDEN) != 0 && sendfromthisnode("KMD",acname,txidaddr) == 0) + { + // the actual withdraw + if ( strcmp(depositaddr,signeraddr) == 0 ) { - // the actual withdraw - if ( strcmp(depositaddr,signeraddr) == 0 ) + txid= sendtoaddress("KMD",acname,txidaddr,10000); + if (bits256_nonz(txid) != 0) { cointxid = sendtoaddress(refcoin,"",withdrawaddr,satoshis); - if ( bits256_nonz(cointxid) != 0 ) + if ( bits256_nonz(cointxid) != 0) { fprintf(stderr,"withdraw %s %s %s %.8f processed\n",refcoin,bits256_str(str,cointxid),withdrawaddr,(double)satoshis/SATOSHIDEN); gatewaysmarkdone("KMD",acname,origtxid,refcoin,cointxid); @@ -812,36 +835,39 @@ void update_gatewayspending(char *refcoin,char *acname,char *bindtxidstr,int32_t } else { - if ( (rawtx= get_gatewaysmultisig(refcoin,acname,bindtxidstr,bits256_str(str,origtxid),txidaddr)) == 0 ) - { - rawtx = createmultisig(refcoin,"",depositaddr,signeraddr,withdrawaddr,satoshis); - } - if ( rawtx != 0 ) - { - if ( (clijson= addmultisignature(refcoin,"",signeraddr,rawtx)) != 0 ) - { - if ( is_cJSON_True(jobj(clijson,"complete")) != 0 ) - { - cointxid = komodobroadcast(refcoin,"",clijson); - if ( bits256_nonz(cointxid) != 0 ) - { - fprintf(stderr,"withdraw %s M.%d N.%d %s %s %.8f processed\n",refcoin,M,N,bits256_str(str,cointxid),withdrawaddr,(double)satoshis/SATOSHIDEN); - gatewaysmarkdone("KMD",acname,origtxid,refcoin,cointxid); - } - } - else if ( jint(clijson,"partialtx") != 0 ) - { - // 10000 + ith -> txidaddr - txid = komodobroadcast("KMD",acname,clijson); - fprintf(stderr,"%s M.%d of N.%d partialtx %s sent\n",refcoin,M,N,bits256_str(str,txid)); - } - free_json(clijson); - } - processed++; - free(rawtx); - } else fprintf(stderr,"couldnt create msig rawtx\n"); + fprintf(stderr,"ERROR sending withdraw marker %s %s to %s %.8f processed\n",refcoin,bits256_str(str,cointxid),txidaddr,(double)10000/SATOSHIDEN); } } + else + { + if ( (rawtx= get_gatewaysmultisig(refcoin,acname,txidaddr)) == 0 ) + { + rawtx = createmultisig(refcoin,"",depositaddr,signeraddr,withdrawaddr,satoshis); + } + if ( rawtx != 0 ) + { + if ( (clijson= addmultisignature(refcoin,"",signeraddr,rawtx)) != 0 ) + { + if ( is_cJSON_True(jobj(clijson,"complete")) != 0 ) + { + cointxid = komodobroadcast(refcoin,"",clijson); + if ( bits256_nonz(cointxid) != 0 ) + { + fprintf(stderr,"withdraw %s M.%d N.%d %s %s %.8f processed\n",refcoin,M,N,bits256_str(str,cointxid),withdrawaddr,(double)satoshis/SATOSHIDEN); + gatewaysmarkdone("KMD",acname,origtxid,refcoin,cointxid); + } + } + else if ( jint(clijson,"partialtx") != 0 ) + { + K=gatewayspartialsign(refcoin,acname,txidaddr,jstr(retjson,"hex")); + fprintf(stderr,"%d of %d partialtx %s sent\n",K,N,bits256_str(str,txid)); + } + free_json(clijson); + } + processed++; + free(rawtx); + } else fprintf(stderr,"couldnt create msig rawtx\n"); + } } else if ( retval > 0 ) { diff --git a/src/cc/gateways.cpp b/src/cc/gateways.cpp index d2c43ad56..6d1bff5c9 100644 --- a/src/cc/gateways.cpp +++ b/src/cc/gateways.cpp @@ -178,6 +178,18 @@ uint8_t DecodeGatewaysOpRet(const CScript &scriptPubKey,std::string &coin,uint25 return(0); } +uint8_t DecodeGatewaysPartialOpRet(const CScript &scriptPubKey,int32_t &K, CPubKey &signerpk, std::string &coin,std::string &hex) +{ + std::vector vopret; uint8_t *script,e,f; + GetOpReturnData(scriptPubKey, vopret); + script = (uint8_t *)vopret.data(); + if ( vopret.size() > 2 && E_UNMARSHAL(vopret,ss >> e; ss >> f; ss >> K; ss >> signerpk; ss >> coin; ss >> hex) != 0 ) + { + return(f); + } + return(0); +} + uint8_t DecodeGatewaysBindOpRet(char *depositaddr,const CScript &scriptPubKey,std::string &coin,uint256 &tokenid,int64_t &totalsupply,uint256 &oracletxid,uint8_t &M,uint8_t &N,std::vector &pubkeys,uint8_t &taddr,uint8_t &prefix,uint8_t &prefix2) { std::vector vopret; uint8_t *script,e,f; @@ -873,31 +885,60 @@ UniValue GatewaysPendingWithdraws(uint256 bindtxid,std::string refcoin) return(result); } -std::string GatewaysMultisig(uint64_t txfee,std::string refcoin,uint256 bindtxid,uint256 withdrawtxid,char *txidaddr) +std::string GatewaysMultisig(char *txidaddr) { - UniValue result(UniValue::VOBJ); std::string coin,hex; char str[67],numstr[65],depositaddr[64],gatewaysassets[64]; uint8_t M,N; std::vector pubkeys; uint8_t taddr,prefix,prefix2; uint256 tokenid,oracletxid,hashBlock; CTransaction tx; CPubKey Gatewayspk,mypk; struct CCcontract_info *cp,C; int32_t i,n,complete,partialtx; int64_t totalsupply; - cp = CCinit(&C,EVAL_GATEWAYS); - if ( txfee == 0 ) - txfee = 10000; - complete = partialtx = 0; - mypk = pubkey2pk(Mypubkey()); - Gatewayspk = GetUnspendable(cp,0); - _GetCCaddress(gatewaysassets,EVAL_GATEWAYS,Gatewayspk); - if ( GetTransaction(bindtxid,tx,hashBlock,false) != 0 ) + std::string parthex,hex,refcoin; uint256 txid,hashBlock; CTransaction tx; int32_t i,maxK,K; CPubKey signerpk; + std::vector > unspentOutputs; + + SetCCunspents(unspentOutputs,txidaddr); + if (unspentOutputs.size()==0) return (""); + maxK=0; + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) { - depositaddr[0] = 0; - if ( tx.vout.size() > 0 && DecodeGatewaysBindOpRet(depositaddr,tx.vout[tx.vout.size()-1].scriptPubKey,coin,tokenid,totalsupply,oracletxid,M,N,pubkeys,taddr,prefix,prefix2) != 0 && M <= N && N > 1 && coin == refcoin ) + txid = it->first.txhash; + if (GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && DecodeGatewaysPartialOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,K,signerpk,refcoin,hex) == 'P' && K>maxK ) { - // need a decentralized way to add signatures to msig tx - n = pubkeys.size(); - for (i=0; i0) return(parthex); + else return (""); +} + +std::string GatewaysPartialSign(uint64_t txfee,char* txidaddr,std::string refcoin, std::string hex) +{ + CMutableTransaction mtx; CScript opret; CPubKey mypk,gatewayspk,signerpk; struct CCcontract_info *cp,C; CTransaction tx; + std::vector > unspentOutputs; + int32_t maxK,K; uint256 txid,parttxid,hashBlock; + cp = CCinit(&C,EVAL_GATEWAYS); + if ( txfee == 0 ) + txfee = 5000; + mypk = pubkey2pk(Mypubkey()); + gatewayspk = GetUnspendable(cp,0); + SetCCunspents(unspentOutputs,txidaddr); + if (unspentOutputs.size()==0) + { + if (AddNormalinputs(mtx,mypk,2*txfee,2)==0) + fprintf(stderr,"error adding funds for partialsign\n"); + } + else + { + maxK=0; + for (std::vector >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) + { + txid = it->first.txhash; + if (GetTransaction(txid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && DecodeGatewaysPartialOpRet(tx.vout[tx.vout.size()-1].scriptPubKey,K,signerpk,refcoin,hex) == 'P' && K>maxK ) + { + maxK=K; + parttxid=txid; + } + } + if (maxK>0) mtx.vin.push_back(CTxIn(parttxid,0,CScript())); + else fprintf(stderr,"Error finding previous partial tx\n"); + } + + mtx.vout.push_back(CTxOut(5000,CScript() << ParseHex(HexStr(gatewayspk)) << OP_CHECKSIG)); + opret << OP_RETURN << E_MARSHAL(ss << cp->evalcode << 'P' << K << mypk << refcoin << hex); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); } diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 7d2eab1e3..e4dcdbbbd 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -426,6 +426,7 @@ static const CRPCCommand vRPCCommands[] = { "gateways", "gatewayspending", &gatewayspending, true }, { "gateways", "gatewaysmultisig", &gatewaysmultisig, true }, { "gateways", "gatewaysmarkdone", &gatewaysmarkdone, true }, + { "gateways", "gatewayspartialsign", &gatewayspartialsign, true }, /* dice */ { "dice", "dicelist", &dicelist, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index a78f1b6fc..cf23f6496 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -253,6 +253,7 @@ extern UniValue gatewayswithdraw(const UniValue& params, bool fHelp); extern UniValue gatewayspending(const UniValue& params, bool fHelp); extern UniValue gatewaysmarkdone(const UniValue& params, bool fHelp); extern UniValue gatewaysmultisig(const UniValue& params, bool fHelp); +extern UniValue gatewayspartialsign(const UniValue& params, bool fHelp); extern UniValue channelsinfo(const UniValue& params, bool fHelp); extern UniValue channelsopen(const UniValue& params, bool fHelp); extern UniValue channelspayment(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 59f0b6ba7..014f8973e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5592,18 +5592,36 @@ UniValue gatewayspending(const UniValue& params, bool fHelp) UniValue gatewaysmultisig(const UniValue& params, bool fHelp) { - UniValue result(UniValue::VOBJ); uint256 bindtxid,withtxid; std::string coin,hex; char *txidaddr; - if ( fHelp || params.size() != 4 ) - throw runtime_error("gatewaysmultisig bindtxid coin withtxid txidaddr\n"); + UniValue result(UniValue::VOBJ); std::string hex; char *txidaddr; + if ( fHelp || params.size() != 1 ) + throw runtime_error("gatewaysmultisig txidaddr\n"); if ( ensure_CCrequirements() < 0 ) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); const CKeyStore& keystore = *pwalletMain; - LOCK2(cs_main, pwalletMain->cs_wallet); - bindtxid = Parseuint256((char *)params[0].get_str().c_str()); + LOCK2(cs_main, pwalletMain->cs_wallet); + txidaddr = (char *)params[0].get_str().c_str(); + hex = GatewaysMultisig(txidaddr); + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex",hex)); + } else ERR_RESULT("couldnt gatewaysmultisig"); + return(result); +} + +UniValue gatewayspartialsign(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); std::string coin,parthex,hex; char *txidaddr; + if ( fHelp || params.size() != 3 ) + throw runtime_error("gatewayspartialsign txidaddr refcoin\n"); + if ( ensure_CCrequirements() < 0 ) + throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); + const CKeyStore& keystore = *pwalletMain; + LOCK2(cs_main, pwalletMain->cs_wallet); + txidaddr = (char *)params[0].get_str().c_str(); coin = params[1].get_str(); - withtxid = Parseuint256((char *)params[2].get_str().c_str()); - txidaddr = (char *)params[3].get_str().c_str(); - hex = GatewaysMultisig(0,coin,bindtxid,withtxid,txidaddr); + coin = params[2].get_str(); + hex = GatewaysPartialSign(0,txidaddr,coin,parthex); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success"));