1 /******************************************************************************
2 * Copyright © 2014-2018 The SuperNET Developers. *
4 * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
5 * the top-level directory of this distribution for the individual copyright *
6 * holder information and the developer policies on copyright and licensing. *
8 * Unless otherwise agreed in a custom licensing agreement, no part of the *
9 * SuperNET software, including this file may be copied, modified, propagated *
10 * or distributed except according to the terms contained in the LICENSE file *
12 * Removal or modification of this copyright notice is prohibited. *
14 ******************************************************************************/
16 #include "CCPayments.h"
19 Payments CC is a catchall CC, supported invoices, zpayments, automated funds allocation, including token based revshare
24 // start of consensus code
26 int64_t IsPaymentsvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v)
29 if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 )
31 if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 )
32 return(tx.vout[v].nValue);
37 bool PaymentsExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee)
39 static uint256 zerohash;
40 CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; int64_t inputs=0,outputs=0,assetoshis;
41 numvins = tx.vin.size();
42 numvouts = tx.vout.size();
43 for (i=0; i<numvins; i++)
45 //fprintf(stderr,"vini.%d\n",i);
46 if ( (*cp->ismyvin)(tx.vin[i].scriptSig) != 0 )
48 //fprintf(stderr,"vini.%d check mempool\n",i);
49 if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 )
50 return eval->Invalid("cant find vinTx");
53 //fprintf(stderr,"vini.%d check hash and vout\n",i);
54 if ( hashBlock == zerohash )
55 return eval->Invalid("cant Payments from mempool");
56 if ( (assetoshis= IsPaymentsvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 )
61 for (i=0; i<numvouts; i++)
63 //fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts);
64 if ( (assetoshis= IsPaymentsvout(cp,tx,i)) != 0 )
65 outputs += assetoshis;
67 if ( inputs != outputs+txfee )
69 fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs);
70 return eval->Invalid("mismatched inputs != outputs + txfee");
75 bool PaymentsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn)
77 int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,numblocks; bool retval; uint256 txid; uint8_t hash[32]; char str[65],destaddr[64];
79 std::vector<std::pair<CAddressIndexKey, CAmount> > txids;
80 numvins = tx.vin.size();
81 numvouts = tx.vout.size();
82 preventCCvins = preventCCvouts = -1;
84 return eval->Invalid("no vouts");
87 for (i=0; i<numvins; i++)
89 if ( IsCCInput(tx.vin[0].scriptSig) == 0 )
91 return eval->Invalid("illegal normal vini");
94 //fprintf(stderr,"check amounts\n");
95 if ( PaymentsExactAmounts(cp,eval,tx,1,10000) == false )
97 fprintf(stderr,"Paymentsget invalid amount\n");
103 memcpy(hash,&txid,sizeof(hash));
104 retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts);
106 fprintf(stderr,"Paymentsget validated\n");
107 else fprintf(stderr,"Paymentsget invalid\n");
112 // end of consensus code
114 // helper functions for rpc calls in rpcwallet.cpp
116 int64_t AddPaymentsInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs)
118 char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t vout,n = 0;
119 std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs;
120 GetCCaddress(cp,coinaddr,pk);
121 SetCCunspents(unspentOutputs,coinaddr);
122 for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++)
124 txid = it->first.txhash;
125 vout = (int32_t)it->first.index;
126 // no need to prevent dup
127 if ( GetTransaction(txid,vintx,hashBlock,false) != 0 )
129 if ( (nValue= IsPaymentsvout(cp,vintx,vout)) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 )
131 if ( total != 0 && maxinputs != 0 )
132 mtx.vin.push_back(CTxIn(txid,vout,CScript()));
133 nValue = it->second.satoshis;
134 totalinputs += nValue;
136 if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) )
144 std::string PaymentsGet(uint64_t txfee,int64_t nValue)
146 CMutableTransaction mtx,tmpmtx; CPubKey mypk,Paymentspk; int64_t inputs,CCchange=0; struct CCcontract_info *cp,C; std::string rawhex; uint32_t j; int32_t i,len; uint8_t buf[32768]; bits256 hash;
147 cp = CCinit(&C,EVAL_PAYMENTS);
150 Paymentspk = GetUnspendable(cp,0);
151 mypk = pubkey2pk(Mypubkey());
152 if ( (inputs= AddPaymentsInputs(cp,mtx,Paymentspk,nValue+txfee,60)) > 0 )
154 if ( inputs > nValue )
155 CCchange = (inputs - nValue - txfee);
157 mtx.vout.push_back(MakeCC1vout(EVAL_PAYMENTS,CCchange,Paymentspk));
158 mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG));
159 fprintf(stderr,"start at %u\n",(uint32_t)time(NULL));
160 j = rand() & 0xfffffff;
161 for (i=0; i<1000000; i++,j++)
164 rawhex = FinalizeCCTx(-1LL,cp,tmpmtx,mypk,txfee,CScript() << OP_RETURN << E_MARSHAL(ss << (uint8_t)EVAL_PAYMENTS << (uint8_t)'G' << j));
165 if ( (len= (int32_t)rawhex.size()) > 0 && len < 65536 )
168 decode_hex(buf,len,(char *)rawhex.c_str());
169 hash = bits256_doublesha256(0,buf,len);
170 if ( (hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0 )
172 fprintf(stderr,"found valid txid after %d iterations %u\n",i,(uint32_t)time(NULL));
175 //fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]);
178 fprintf(stderr,"couldnt generate valid txid %u\n",(uint32_t)time(NULL));
180 } else fprintf(stderr,"cant find Payments inputs\n");
184 std::string PaymentsFund(uint64_t txfee,int64_t funds)
186 CMutableTransaction mtx; CPubKey mypk,Paymentspk; CScript opret; struct CCcontract_info *cp,C;
187 cp = CCinit(&C,EVAL_PAYMENTS);
190 mypk = pubkey2pk(Mypubkey());
191 Paymentspk = GetUnspendable(cp,0);
192 if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 )
194 mtx.vout.push_back(MakeCC1vout(EVAL_PAYMENTS,funds,Paymentspk));
195 return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret));
200 UniValue PaymentsInfo()
202 UniValue result(UniValue::VOBJ); char numstr[64];
203 CMutableTransaction mtx; CPubKey Paymentspk; struct CCcontract_info *cp,C; int64_t funding;
204 result.push_back(Pair("result","success"));
205 result.push_back(Pair("name","Payments"));
206 cp = CCinit(&C,EVAL_PAYMENTS);
207 Paymentspk = GetUnspendable(cp,0);
208 funding = AddPaymentsInputs(cp,mtx,Paymentspk,0,0);
209 sprintf(numstr,"%.8f",(double)funding/COIN);
210 result.push_back(Pair("funding",numstr));