]> Git Repo - VerusCoin.git/blob - src/cc/assets.cpp
Fix dustvout
[VerusCoin.git] / src / cc / assets.cpp
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 "CCassets.h"
17
18 /*
19  Assets can be created or transferred.
20  
21  native coins are also locked in the EVAL_ASSETS address, so we need a strict rule on when utxo in the special address are native coins and when they are assets. The specific rule that must not be violated is that vout0 for 'b'/'B' funcid are native coins. All other utxo locked in the special address are assets.
22  
23  To create an asset use CC EVAL_ASSETS to create a transaction where vout[0] funds the assets. Externally each satoshi can be interpreted to represent 1 asset, or 100 million satoshis for one asset with 8 decimals, and the other decimals in between. The interpretation of the number of decimals is left to the higher level usages.
24  
25  Once created, the assetid is the txid of the create transaction and using the assetid/0 it can spend the assets to however many outputs it creates. The restriction is that the last output must be an opreturn with the assetid. The sum of all but the first output needs to add up to the total assetoshis input. The first output is ignored and used for change from txfee.
26  
27  What this means is that vout 0 of the creation txid and vouts 1 ... n-2 for transfer vouts are assetoshi outputs.
28  
29  There is a special type of transfer to an unspendable address, that locks the asset and creates an offer for all. The details specified in the opreturn. In order to be compatible with the signing restrictions, the "unspendable" address is actually an address whose privkey is known to all. Funds sent to this address can only be spent if the swap parameters are fulfilled, or if the original pubkey cancels the offer by spending it.
30  
31  Types of transactions:
32  create name:description -> txid for assetid
33  transfer <pubkey> <assetid> -> [{address:amount}, ... ] // like withdraw api
34  selloffer <pubkey> <txid/vout> <amount> -> cancel or fillsell -> mempool txid or rejected, might not confirm
35  buyoffer <amount> <assetid> <required> -> cancelbuy or fillbuy -> mempool txid or rejected, might not confirm
36  exchange <pubkey> <txid/vout> <required> <required assetid> -> cancel or fillexchange -> mempool txid or rejected, might not confirm
37  
38  assetsaddress <pubkey> // all assets end up in a special address for each pubkey
39  assetbalance <pubkey> <assetid>
40  assetutxos <pubkey> <assetid>
41  assetsbalances <pubkey>
42  asks <assetid>
43  bids <assetid>
44  swaps <assetid>
45  
46  valid CC output: create or transfer or buyoffer or selloffer or exchange or cancel or fill
47  
48  create
49  vin.0: normal input
50  vout.0: issuance assetoshis to CC
51  vout.1: tag sent to normal address of AssetsCCaddress
52  vout.2: normal output for change (if any)
53  vout.n-1: opreturn [EVAL_ASSETS] ['c'] [origpubkey] "<assetname>" "<description>"
54  
55  transfer
56  vin.0: normal input
57  vin.1 .. vin.n-1: valid CC outputs
58  vout.0 to n-2: assetoshis output to CC
59  vout.n-2: normal output for change (if any)
60  vout.n-1: opreturn [EVAL_ASSETS] ['t'] [assetid]
61  
62  buyoffer:
63  vins.*: normal inputs (bid + change)
64  vout.0: amount of bid to unspendable
65  vout.1: normal output for change (if any)
66  vout.n-1: opreturn [EVAL_ASSETS] ['b'] [assetid] [amount of asset required] [origpubkey]
67
68  cancelbuy:
69  vin.0: normal input
70  vin.1: unspendable.(vout.0 from buyoffer) buyTx.vout[0]
71  vout.0: vin.1 value to original pubkey buyTx.vout[0].nValue -> [origpubkey]
72  vout.1: normal output for change (if any)
73  vout.n-1: opreturn [EVAL_ASSETS] ['o'] [assetid]
74  
75  fillbuy:
76  vin.0: normal input
77  vin.1: unspendable.(vout.0 from buyoffer) buyTx.vout[0]
78  vin.2+: valid CC output satisfies buyoffer (*tx.vin[2])->nValue
79  vout.0: remaining amount of bid to unspendable
80  vout.1: vin.1 value to signer of vin.2
81  vout.2: vin.2 assetoshis to original pubkey
82  vout.3: CC output for assetoshis change (if any)
83  vout.4: normal output for change (if any)
84  vout.n-1: opreturn [EVAL_ASSETS] ['B'] [assetid] [remaining asset required] [origpubkey]
85
86  selloffer:
87  vin.0: normal input
88  vin.1: valid CC output for sale
89  vout.0: vin.1 assetoshis output to CC to unspendable
90  vout.1: CC output for change (if any)
91  vout.2: normal output for change (if any)
92  vout.n-1: opreturn [EVAL_ASSETS] ['s'] [assetid] [amount of native coin required] [origpubkey]
93  
94  exchange:
95  vin.0: normal input
96  vin.1: valid CC output
97  vout.0: vin.1 assetoshis output to CC to unspendable
98  vout.1: CC output for change (if any)
99  vout.2: normal output for change (if any)
100  vout.n-1: opreturn [EVAL_ASSETS] ['e'] [assetid] [assetid2] [amount of asset2 required] [origpubkey]
101  
102  cancel:
103  vin.0: normal input
104  vin.1: unspendable.(vout.0 from exchange or selloffer) sellTx/exchangeTx.vout[0] inputTx
105  vout.0: vin.1 assetoshis to original pubkey CC sellTx/exchangeTx.vout[0].nValue -> [origpubkey]
106  vout.1: normal output for change (if any)
107  vout.n-1: opreturn [EVAL_ASSETS] ['x'] [assetid]
108  
109  fillsell:
110  vin.0: normal input
111  vin.1: unspendable.(vout.0 assetoshis from selloffer) sellTx.vout[0]
112  vin.2+: normal output that satisfies selloffer (*tx.vin[2])->nValue
113  vout.0: remaining assetoshis -> unspendable
114  vout.1: vin.1 assetoshis to signer of vin.2 sellTx.vout[0].nValue -> any
115  vout.2: vin.2 value to original pubkey [origpubkey]
116  vout.3: CC asset for change (if any)
117  vout.4: CC asset2 for change (if any) 'E' only
118  vout.5: normal output for change (if any)
119  vout.n-1: opreturn [EVAL_ASSETS] ['S'] [assetid] [amount of coin still required] [origpubkey]
120  
121  fillexchange:
122  vin.0: normal input
123  vin.1: unspendable.(vout.0 assetoshis from exchange) exchangeTx.vout[0]
124  vin.2+: valid CC assetid2 output that satisfies exchange (*tx.vin[2])->nValue
125  vout.0: remaining assetoshis -> unspendable
126  vout.1: vin.1 assetoshis to signer of vin.2 exchangeTx.vout[0].nValue -> any
127  vout.2: vin.2 assetoshis2 to original pubkey [origpubkey]
128  vout.3: normal output for change (if any)
129  vout.n-1: opreturn [EVAL_ASSETS] ['E'] [assetid vin0+1] [assetid vin2] [remaining asset2 required] [origpubkey]
130 */
131
132 bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx)
133 {
134     static uint256 zero;
135     CTxDestination address; CTransaction vinTx,createTx; uint256 hashBlock,assetid,assetid2; int32_t i,starti,numvins,numvouts,preventCCvins,preventCCvouts; uint64_t amount,remaining_price,nValue,assetoshis,outputs,inputs,tmpprice,totalunits,ignore; std::vector<uint8_t> origpubkey,tmporigpubkey,ignorepubkey; uint8_t funcid; char destaddr[64],origaddr[64],CCaddr[64];
136     numvins = tx.vin.size();
137     numvouts = tx.vout.size();
138     outputs = inputs = 0;
139     preventCCvins = preventCCvouts = -1;
140     if ( (funcid= DecodeAssetOpRet(tx.vout[numvouts-1].scriptPubKey,assetid,assetid2,amount,origpubkey)) == 0 )
141         return eval->Invalid("Invalid opreturn payload");
142     fprintf(stderr,"AssetValidate (%c)\n",funcid);
143     if ( eval->GetTxUnconfirmed(assetid,createTx,hashBlock) == 0 )
144         return eval->Invalid("cant find asset create txid");
145     else if ( assetid2 != zero && eval->GetTxUnconfirmed(assetid2,createTx,hashBlock) == 0 )
146         return eval->Invalid("cant find asset2 create txid");
147     else if ( IsCCInput(tx.vin[0].scriptSig) != 0 )
148         return eval->Invalid("illegal asset vin0");
149     else if ( numvouts < 1 )
150         return eval->Invalid("no vouts");
151     else if ( funcid != 'c' )
152     {
153         if ( funcid == 't' )
154             starti = 0;
155         else starti = 1;
156         if ( assetid == zero )
157             return eval->Invalid("illegal assetid");
158         else if ( AssetExactAmounts(cp,inputs,starti,outputs,eval,tx,assetid) == false )
159             return eval->Invalid("asset inputs != outputs");
160     }
161     switch ( funcid )
162     {
163         case 'c': // create wont be called to be verified as it has no CC inputs
164             //vin.0: normal input
165             //vout.0: issuance assetoshis to CC
166             //vout.1: normal output for change (if any)
167             //vout.n-1: opreturn [EVAL_ASSETS] ['c'] [{"<assetname>":"<description>"}]
168             return eval->Invalid("unexpected AssetValidate for createasset");
169             break;
170             
171         case 't': // transfer
172             //vin.0: normal input
173             //vin.1 .. vin.n-1: valid CC outputs
174             //vout.0 to n-2: assetoshis output to CC
175             //vout.n-2: normal output for change (if any)
176             //vout.n-1: opreturn [EVAL_ASSETS] ['t'] [assetid]
177             if ( inputs == 0 )
178                 return eval->Invalid("no asset inputs for transfer");
179             fprintf(stderr,"transfer validated %.8f -> %.8f\n",(double)inputs/COIN,(double)outputs/COIN);
180             break;
181             
182         case 'b': // buyoffer
183             //vins.*: normal inputs (bid + change)
184             //vout.0: amount of bid to unspendable
185             //vout.1: normal output for change (if any)
186             // vout.n-1: opreturn [EVAL_ASSETS] ['b'] [assetid] [amount of asset required] [origpubkey]
187             if ( remaining_price == 0 )
188                 return eval->Invalid("illegal null amount for buyoffer");
189             else if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) == 0 )
190                 return eval->Invalid("invalid vout for buyoffer");
191             preventCCvins = 1;
192             preventCCvouts = 1;
193             fprintf(stderr,"buy offer validated to destaddr.(%s)\n",cp->unspendableCCaddr);
194             break;
195             
196         case 'o': // cancelbuy
197             //vin.0: normal input
198             //vin.1: unspendable.(vout.0 from buyoffer) buyTx.vout[0]
199             //vout.0: vin.1 value to original pubkey buyTx.vout[0].nValue -> [origpubkey]
200             //vout.1: normal output for change (if any)
201             //vout.n-1: opreturn [EVAL_ASSETS] ['o']
202             if ( (nValue= AssetValidateBuyvin(cp,eval,tmpprice,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 )
203                 return(false);
204             else if ( ConstrainVout(tx.vout[0],0,origaddr,nValue) == 0 )
205                 return eval->Invalid("invalid refund for cancelbuy");
206             preventCCvins = 2;
207             preventCCvouts = 0;
208             fprintf(stderr,"cancelbuy validated to origaddr.(%s)\n",origaddr);
209             break;
210             
211         case 'B': // fillbuy:
212             //vin.0: normal input
213             //vin.1: unspendable.(vout.0 from buyoffer) buyTx.vout[0]
214             //vin.2+: valid CC output satisfies buyoffer (*tx.vin[2])->nValue
215             //vout.0: remaining amount of bid to unspendable
216             //vout.1: vin.1 value to signer of vin.2
217             //vout.2: vin.2 assetoshis to original pubkey
218             //vout.3: CC output for assetoshis change (if any)
219             //vout.4: normal output for change (if any)
220             //vout.n-1: opreturn [EVAL_ASSETS] ['B'] [assetid] [remaining asset required] [origpubkey]
221             preventCCvouts = 4;
222             if ( (nValue= AssetValidateBuyvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 )
223                 return(false);
224             else if ( numvouts < 3 )
225                 return eval->Invalid("not enough vouts for fillbuy");
226             else if ( tmporigpubkey != origpubkey )
227                 return eval->Invalid("mismatched origpubkeys for fillbuy");
228             else
229             {
230                 if ( ConstrainVout(tx.vout[1],0,0,0) == 0 )
231                     return eval->Invalid("vout1 is CC for fillbuy");
232                 else if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 )
233                     return eval->Invalid("vout2 is normal for fillbuy");
234                 else if ( ValidateAssetRemainder(0,remaining_price,tx.vout[0].nValue,nValue,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false )
235                     return eval->Invalid("mismatched remainder for fillbuy");
236                 else if ( remaining_price != 0 )
237                 {
238                     if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) == 0 )
239                         return eval->Invalid("mismatched vout0 AssetsCCaddr for fillbuy");
240                 }
241             }
242             fprintf(stderr,"fillbuy validated\n");
243             break;
244             
245         case 's': // selloffer
246         case 'e': // exchange
247             //vin.0: normal input
248             //vin.1: valid CC output for sale
249             //vout.0: vin.1 assetoshis output to CC to unspendable
250             //vout.1: normal output for change (if any)
251             //'s'.vout.n-1: opreturn [EVAL_ASSETS] ['s'] [assetid] [amount of native coin required] [origpubkey]
252             //'e'.vout.n-1: opreturn [EVAL_ASSETS] ['e'] [assetid] [assetid2] [amount of asset2 required] [origpubkey]
253             if ( remaining_price == 0 )
254                 return eval->Invalid("illegal null remaining_price for selloffer");
255             else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 )
256                 return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer");
257             preventCCvouts = 1;
258             break;
259             
260         case 'x': // cancel
261             //vin.0: normal input
262             //vin.1: unspendable.(vout.0 from exchange or selloffer) sellTx/exchangeTx.vout[0] inputTx
263             //vout.0: vin.1 assetoshis to original pubkey CC sellTx/exchangeTx.vout[0].nValue -> [origpubkey]
264             //vout.1: normal output for change (if any)
265             //vout.n-1: opreturn [EVAL_ASSETS] ['x'] [assetid]
266             if ( (assetoshis= AssetValidateSellvin(cp,eval,tmpprice,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 )
267                 return(false);
268             else if ( ConstrainVout(tx.vout[0],1,CCaddr,assetoshis) == 0 )
269                 return eval->Invalid("invalid vout for cancel");
270             preventCCvins = 2;
271             preventCCvouts = 1;
272             break;
273             
274         case 'S': // fillsell
275         case 'E': // fillexchange
276             //vin.0: normal input
277             //vin.1: unspendable.(vout.0 assetoshis from selloffer) sellTx.vout[0]
278             //'S'.vin.2+: normal output that satisfies selloffer (*tx.vin[2])->nValue
279             //'E'.vin.2+: valid CC assetid2 output that satisfies exchange (*tx.vin[2])->nValue
280             //vout.0: remaining assetoshis -> unspendable
281             //vout.1: vin.1 assetoshis to signer of vin.2 sellTx.vout[0].nValue -> any
282             //'S'.vout.2: vin.2 value to original pubkey [origpubkey]
283             //'E'.vout.2: vin.2 assetoshis2 to original pubkey [origpubkey]
284             //vout.3: normal output for change (if any)
285             //'S'.vout.n-1: opreturn [EVAL_ASSETS] ['S'] [assetid] [amount of coin still required] [origpubkey]
286             //'E'.vout.n-1: opreturn [EVAL_ASSETS] ['E'] [assetid vin0+1] [assetid vin2] [remaining asset2 required] [origpubkey]
287             if ( funcid == 'E' )
288             {
289                 if ( AssetExactAmounts(cp,inputs,1,outputs,eval,tx,assetid2) == false )
290                     eval->Invalid("asset2 inputs != outputs");
291             }
292             if ( (assetoshis= AssetValidateSellvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 )
293                 return(false);
294             else if ( numvouts < 3 )
295                 return eval->Invalid("not enough vouts for fill");
296             else if ( tmporigpubkey != origpubkey )
297                 return eval->Invalid("mismatched origpubkeys for fill");
298             else
299             {
300                 if ( ValidateAssetRemainder(1,remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false )
301                     return eval->Invalid("mismatched remainder for fill");
302                 else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 )
303                     return eval->Invalid("normal vout1 for fillask");
304                 else if ( funcid == 'E' && ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 )
305                     return eval->Invalid("normal vout2 for fillask");
306                 else if ( funcid == 'S' && ConstrainVout(tx.vout[2],0,origaddr,0) == 0 )
307                     return eval->Invalid("CC vout2 for fillask");
308                 else if ( remaining_price != 0 )
309                 {
310                     if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 )
311                         return eval->Invalid("mismatched vout0 AssetsCCaddr for fill");
312                 }
313             }
314             fprintf(stderr,"fill validated\n");
315             break;
316     }
317     return(PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts));
318 }
319
320
This page took 0.04183 seconds and 4 git commands to generate.