From 6915c400e38eac4d53022ccc0dc0f2ebb3d3576a Mon Sep 17 00:00:00 2001 From: Mihailo Milenkovic Date: Fri, 18 Jan 2019 11:14:40 +0100 Subject: [PATCH 1/2] Fix ChannelsPayment when destpub issues payment,add check_signing_pubkey --- src/cc/CCinclude.h | 1 + src/cc/CCutils.cpp | 31 +++++++++++++++++++++++++++++++ src/cc/channels.cpp | 9 +++++---- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index 3f8d8f672..982f5aa77 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -222,6 +222,7 @@ std::vector Mypubkey(); bool Myprivkey(uint8_t myprivkey[]); int64_t CCduration(int32_t &numblocks,uint256 txid); bool komodo_txnotarizedconfirmed(uint256 txid); +CPubKey check_signing_pubkey(CScript scriptSig); // CCtx bool SignTx(CMutableTransaction &mtx,int32_t vini,int64_t utxovalue,const CScript scriptPubKey); std::string FinalizeCCTx(uint64_t skipmask,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret); diff --git a/src/cc/CCutils.cpp b/src/cc/CCutils.cpp index 5d72b6b53..e9c30acc1 100644 --- a/src/cc/CCutils.cpp +++ b/src/cc/CCutils.cpp @@ -599,3 +599,34 @@ bool komodo_txnotarizedconfirmed(uint256 txid) return (true); return (false); } + +CPubKey check_signing_pubkey(CScript scriptSig) +{ + + bool found = false; + CPubKey pubkey; + + auto findEval = [](CC *cond, struct CCVisitor _) { + bool r = false; + + if (cc_typeId(cond) == CC_Secp256k1) { + *(CPubKey*)_.context=buf2pk(cond->publicKey); + r = true; + } + // false for a match, true for continue + return r ? 0 : 1; + }; + + CC *cond = GetCryptoCondition(scriptSig); + + if (cond) { + CCVisitor visitor = { findEval, (uint8_t*)"", 0, &pubkey }; + bool out = !cc_visit(cond, visitor); + cc_free(cond); + + if (pubkey.IsValid()) { + return pubkey; + } + } + return CPubKey(); +} diff --git a/src/cc/channels.cpp b/src/cc/channels.cpp index 2a7ebc931..824707520 100644 --- a/src/cc/channels.cpp +++ b/src/cc/channels.cpp @@ -222,6 +222,7 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & //vout.3: normal output of payment amount to receiver pubkey //vout.n-2: normal change //vout.n-1: opreturn - 'P' opentxid senderspubkey receiverspubkey depth numpayments secret + check_signing_pubkey(tx.vin[1].scriptSig); if (komodo_txnotarizedconfirmed(opentxid) == 0) return eval->Invalid("channelOpen is not yet confirmed(notarised)!"); else if ( IsCCInput(tx.vin[0].scriptSig) != 0 ) @@ -422,8 +423,8 @@ int64_t AddChannelsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx, C if ( (int32_t)it->first.index==0 && GetTransaction(it->first.txhash,tx,hashBlock,false) != 0 && (numvouts=tx.vout.size()) > 0) { if (DecodeChannelsOpRet(tx.vout[numvouts-1].scriptPubKey,tokenid,tmp_txid,srcpub,destpub,param1,param2,param3)!=0 && - (tmp_txid==openTx.GetHash() || tx.GetHash()==openTx.GetHash()) && - (totalinputs=IsChannelsvout(cp,tx,srcpub,destpub,0)+IsChannelsMarkervout(cp,tx,srcpub,marker))>0) + (tmp_txid==openTx.GetHash() || tx.GetHash()==openTx.GetHash()) && IsChannelsMarkervout(cp,tx,marker==1?srcpub:destpub,marker)>0 && + (totalinputs=IsChannelsvout(cp,tx,srcpub,destpub,0))>0) { txid = it->first.txhash; break; @@ -443,7 +444,7 @@ int64_t AddChannelsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx, C tmp_txid==openTx.GetHash() && param1 < mindepth) { txid=hash; - totalinputs=txmempool.vout[0].nValue+txmempool.vout[1].nValue; + totalinputs=txmempool.vout[0].nValue; mindepth=param1; } } @@ -543,7 +544,7 @@ std::string ChannelPayment(uint64_t txfee,uint256 opentxid,int64_t amount, uint2 } if (AddNormalinputs(mtx,mypk,2*txfee,3) > 0) { - if ((funds=AddChannelsInputs(cp,mtx,channelOpenTx,prevtxid,mypk)) !=0 && (change=funds-amount-txfee)>=0) + if ((funds=AddChannelsInputs(cp,mtx,channelOpenTx,prevtxid,mypk)) !=0 && (change=funds-amount)>=0) { numpayments=amount/payment; if (GetTransaction(prevtxid,prevTx,hashblock,false) != 0 && (numvouts=prevTx.vout.size()) > 0 && From 6ab5254887ed1b88d82f1114d1b2a19498353caf Mon Sep 17 00:00:00 2001 From: Mihailo Milenkovic Date: Fri, 18 Jan 2019 11:18:38 +0100 Subject: [PATCH 2/2] Fix --- src/cc/channels.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cc/channels.cpp b/src/cc/channels.cpp index 824707520..a375edbda 100644 --- a/src/cc/channels.cpp +++ b/src/cc/channels.cpp @@ -222,7 +222,6 @@ bool ChannelsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction & //vout.3: normal output of payment amount to receiver pubkey //vout.n-2: normal change //vout.n-1: opreturn - 'P' opentxid senderspubkey receiverspubkey depth numpayments secret - check_signing_pubkey(tx.vin[1].scriptSig); if (komodo_txnotarizedconfirmed(opentxid) == 0) return eval->Invalid("channelOpen is not yet confirmed(notarised)!"); else if ( IsCCInput(tx.vin[0].scriptSig) != 0 )