]>
Commit | Line | Data |
---|---|---|
d96ce6e9 | 1 | /****************************************************************************** |
2 | * Copyright © 2014-2018 The SuperNET Developers. * | |
3 | * * | |
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. * | |
7 | * * | |
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 * | |
11 | * * | |
12 | * Removal or modification of this copyright notice is prohibited. * | |
13 | * * | |
14 | ******************************************************************************/ | |
15 | ||
16 | #include "CCMofN.h" | |
17 | ||
18 | /* | |
6ea86373 | 19 | The idea of MofN CC is to allow non-interactive multisig, preferably in a cross chain compatible way, ie. for actual bitcoin multisig. |
20 | ||
21 | full redeemscript in an initial tx with opreturn | |
22 | ability to post partial signatures and construct a full transaction from M such partial signatures | |
23 | a new transaction would refer to the initialtx and other partial would refer to both | |
d7a84c74 | 24 | |
366625ca | 25 | There is no need for a CC contract to use it for normal multisig as normal multisig transactions are already supported. |
26 | ||
27 | In order to take advantage of CC powers, we can create a more powerful multisig using shamir's secret MofN (up to 255) algo to allow spends. Using the same non-interactive partial signing is possible. also, in addition to spending, data payload can have additional data that is also revealed when the funds are spent. | |
28 | ||
29 | rpc calls needed: | |
30 | 1) create msig address (normal or shamir) | |
31 | 2) post payment with partial sig | |
32 | 3) add partial sig to 2) | |
33 | 4) combine and submit M partial sigs | |
34 | ||
d96ce6e9 | 35 | */ |
36 | ||
37 | // start of consensus code | |
38 | ||
39 | int64_t IsMofNvout(struct CCcontract_info *cp,const CTransaction& tx,int32_t v) | |
40 | { | |
41 | char destaddr[64]; | |
42 | if ( tx.vout[v].scriptPubKey.IsPayToCryptoCondition() != 0 ) | |
43 | { | |
44 | if ( Getscriptaddress(destaddr,tx.vout[v].scriptPubKey) > 0 && strcmp(destaddr,cp->unspendableCCaddr) == 0 ) | |
45 | return(tx.vout[v].nValue); | |
46 | } | |
47 | return(0); | |
48 | } | |
49 | ||
50 | bool MofNExactAmounts(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx,int32_t minage,uint64_t txfee) | |
51 | { | |
52 | static uint256 zerohash; | |
53 | CTransaction vinTx; uint256 hashBlock,activehash; int32_t i,numvins,numvouts; int64_t inputs=0,outputs=0,assetoshis; | |
54 | numvins = tx.vin.size(); | |
55 | numvouts = tx.vout.size(); | |
56 | for (i=0; i<numvins; i++) | |
57 | { | |
58 | //fprintf(stderr,"vini.%d\n",i); | |
59 | if ( (*cp->ismyvin)(tx.vin[i].scriptSig) != 0 ) | |
60 | { | |
61 | //fprintf(stderr,"vini.%d check mempool\n",i); | |
62 | if ( eval->GetTxUnconfirmed(tx.vin[i].prevout.hash,vinTx,hashBlock) == 0 ) | |
63 | return eval->Invalid("cant find vinTx"); | |
64 | else | |
65 | { | |
66 | //fprintf(stderr,"vini.%d check hash and vout\n",i); | |
67 | if ( hashBlock == zerohash ) | |
68 | return eval->Invalid("cant MofN from mempool"); | |
69 | if ( (assetoshis= IsMofNvout(cp,vinTx,tx.vin[i].prevout.n)) != 0 ) | |
70 | inputs += assetoshis; | |
71 | } | |
72 | } | |
73 | } | |
74 | for (i=0; i<numvouts; i++) | |
75 | { | |
76 | //fprintf(stderr,"i.%d of numvouts.%d\n",i,numvouts); | |
77 | if ( (assetoshis= IsMofNvout(cp,tx,i)) != 0 ) | |
78 | outputs += assetoshis; | |
79 | } | |
80 | if ( inputs != outputs+txfee ) | |
81 | { | |
82 | fprintf(stderr,"inputs %llu vs outputs %llu\n",(long long)inputs,(long long)outputs); | |
83 | return eval->Invalid("mismatched inputs != outputs + txfee"); | |
84 | } | |
85 | else return(true); | |
86 | } | |
87 | ||
4ecaf167 | 88 | bool MofNValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, uint32_t nIn, bool fulfilled) |
d96ce6e9 | 89 | { |
90 | int32_t numvins,numvouts,preventCCvins,preventCCvouts,i,numblocks; bool retval; uint256 txid; uint8_t hash[32]; char str[65],destaddr[64]; | |
91 | return(false); | |
92 | std::vector<std::pair<CAddressIndexKey, CAmount> > txids; | |
93 | numvins = tx.vin.size(); | |
94 | numvouts = tx.vout.size(); | |
95 | preventCCvins = preventCCvouts = -1; | |
96 | if ( numvouts < 1 ) | |
97 | return eval->Invalid("no vouts"); | |
98 | else | |
99 | { | |
100 | for (i=0; i<numvins; i++) | |
101 | { | |
102 | if ( IsCCInput(tx.vin[0].scriptSig) == 0 ) | |
103 | { | |
104 | return eval->Invalid("illegal normal vini"); | |
105 | } | |
106 | } | |
107 | //fprintf(stderr,"check amounts\n"); | |
108 | if ( MofNExactAmounts(cp,eval,tx,1,10000) == false ) | |
109 | { | |
110 | fprintf(stderr,"mofnget invalid amount\n"); | |
111 | return false; | |
112 | } | |
113 | else | |
114 | { | |
115 | txid = tx.GetHash(); | |
116 | memcpy(hash,&txid,sizeof(hash)); | |
117 | retval = PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts); | |
118 | if ( retval != 0 ) | |
119 | fprintf(stderr,"mofnget validated\n"); | |
120 | else fprintf(stderr,"mofnget invalid\n"); | |
121 | return(retval); | |
122 | } | |
123 | } | |
124 | } | |
125 | // end of consensus code | |
126 | ||
127 | // helper functions for rpc calls in rpcwallet.cpp | |
128 | ||
129 | int64_t AddMofNInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,int64_t total,int32_t maxinputs) | |
130 | { | |
131 | char coinaddr[64]; int64_t nValue,price,totalinputs = 0; uint256 txid,hashBlock; std::vector<uint8_t> origpubkey; CTransaction vintx; int32_t vout,n = 0; | |
132 | std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > unspentOutputs; | |
133 | GetCCaddress(cp,coinaddr,pk); | |
134 | SetCCunspents(unspentOutputs,coinaddr); | |
135 | for (std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> >::const_iterator it=unspentOutputs.begin(); it!=unspentOutputs.end(); it++) | |
136 | { | |
137 | txid = it->first.txhash; | |
138 | vout = (int32_t)it->first.index; | |
139 | // no need to prevent dup | |
140 | if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) | |
141 | { | |
142 | if ( (nValue= IsMofNvout(cp,vintx,vout)) > 1000000 && myIsutxo_spentinmempool(txid,vout) == 0 ) | |
143 | { | |
144 | if ( total != 0 && maxinputs != 0 ) | |
145 | mtx.vin.push_back(CTxIn(txid,vout,CScript())); | |
146 | nValue = it->second.satoshis; | |
147 | totalinputs += nValue; | |
148 | n++; | |
149 | if ( (total > 0 && totalinputs >= total) || (maxinputs > 0 && n >= maxinputs) ) | |
150 | break; | |
151 | } | |
152 | } | |
153 | } | |
154 | return(totalinputs); | |
155 | } | |
156 | ||
1d0f0222 | 157 | std::string MofNGet(uint64_t txfee,int64_t nValue) |
d96ce6e9 | 158 | { |
159 | CMutableTransaction mtx,tmpmtx; CPubKey mypk,mofnpk; 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; | |
160 | cp = CCinit(&C,EVAL_MOFN); | |
161 | if ( txfee == 0 ) | |
162 | txfee = 10000; | |
163 | mofnpk = GetUnspendable(cp,0); | |
164 | mypk = pubkey2pk(Mypubkey()); | |
1d0f0222 | 165 | if ( (inputs= AddMofNInputs(cp,mtx,mofnpk,nValue+txfee,60)) > 0 ) |
d96ce6e9 | 166 | { |
167 | if ( inputs > nValue ) | |
168 | CCchange = (inputs - nValue - txfee); | |
169 | if ( CCchange != 0 ) | |
170 | mtx.vout.push_back(MakeCC1vout(EVAL_MOFN,CCchange,mofnpk)); | |
171 | mtx.vout.push_back(CTxOut(nValue,CScript() << ParseHex(HexStr(mypk)) << OP_CHECKSIG)); | |
172 | fprintf(stderr,"start at %u\n",(uint32_t)time(NULL)); | |
173 | j = rand() & 0xfffffff; | |
174 | for (i=0; i<1000000; i++,j++) | |
175 | { | |
176 | tmpmtx = mtx; | |
177 | rawhex = FinalizeCCTx(-1LL,cp,tmpmtx,mypk,txfee,CScript() << OP_RETURN << E_MARSHAL(ss << (uint8_t)EVAL_MOFN << (uint8_t)'G' << j)); | |
178 | if ( (len= (int32_t)rawhex.size()) > 0 && len < 65536 ) | |
179 | { | |
180 | len >>= 1; | |
181 | decode_hex(buf,len,(char *)rawhex.c_str()); | |
182 | hash = bits256_doublesha256(0,buf,len); | |
183 | if ( (hash.bytes[0] & 0xff) == 0 && (hash.bytes[31] & 0xff) == 0 ) | |
184 | { | |
185 | fprintf(stderr,"found valid txid after %d iterations %u\n",i,(uint32_t)time(NULL)); | |
186 | return(rawhex); | |
187 | } | |
188 | //fprintf(stderr,"%02x%02x ",hash.bytes[0],hash.bytes[31]); | |
189 | } | |
190 | } | |
191 | fprintf(stderr,"couldnt generate valid txid %u\n",(uint32_t)time(NULL)); | |
192 | return(""); | |
193 | } else fprintf(stderr,"cant find mofn inputs\n"); | |
194 | return(""); | |
195 | } | |
196 | ||
197 | std::string MofNFund(uint64_t txfee,int64_t funds) | |
198 | { | |
199 | CMutableTransaction mtx; CPubKey mypk,mofnpk; CScript opret; struct CCcontract_info *cp,C; | |
200 | cp = CCinit(&C,EVAL_MOFN); | |
201 | if ( txfee == 0 ) | |
202 | txfee = 10000; | |
203 | mypk = pubkey2pk(Mypubkey()); | |
204 | mofnpk = GetUnspendable(cp,0); | |
205 | if ( AddNormalinputs(mtx,mypk,funds+txfee,64) > 0 ) | |
206 | { | |
207 | mtx.vout.push_back(MakeCC1vout(EVAL_MOFN,funds,mofnpk)); | |
208 | return(FinalizeCCTx(0,cp,mtx,mypk,txfee,opret)); | |
209 | } | |
210 | return(""); | |
211 | } | |
212 | ||
213 | UniValue MofNInfo() | |
214 | { | |
215 | UniValue result(UniValue::VOBJ); char numstr[64]; | |
216 | CMutableTransaction mtx; CPubKey mofnpk; struct CCcontract_info *cp,C; int64_t funding; | |
217 | result.push_back(Pair("result","success")); | |
218 | result.push_back(Pair("name","MofN")); | |
219 | cp = CCinit(&C,EVAL_MOFN); | |
220 | mofnpk = GetUnspendable(cp,0); | |
221 | funding = AddMofNInputs(cp,mtx,mofnpk,0,0); | |
222 | sprintf(numstr,"%.8f",(double)funding/COIN); | |
223 | result.push_back(Pair("funding",numstr)); | |
224 | return(result); | |
225 | } | |
226 |