]> Git Repo - VerusCoin.git/blob - src/cc/assets.cpp
Merge pull request #792 from jl777/jl777
[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; int64_t 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,remaining_price,origpubkey)) == 0 )
141         return eval->Invalid("Invalid opreturn payload");
142     fprintf(stderr,"AssetValidate (%c)\n",funcid);
143     if ( funcid != 'o' && funcid != 'x' && eval->GetTxUnconfirmed(assetid,createTx,hashBlock) == 0 )
144         return eval->Invalid("cant find asset create txid");
145     else if ( funcid != 'o' && funcid != 'x' && 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 ( nValue != tx.vout[0].nValue+tx.vout[1].nValue )
231                     return eval->Invalid("locked value doesnt match vout0+1 fillbuy");
232                 else if ( tx.vout[3].scriptPubKey.IsPayToCryptoCondition() != 0 )
233                 {
234                     if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 )
235                         return eval->Invalid("vout2 doesnt go to origpubkey fillbuy");
236                     else if ( inputs != tx.vout[2].nValue+tx.vout[3].nValue )
237                         return eval->Invalid("asset inputs doesnt match vout2+3 fillbuy");
238                 }
239                 else if ( ConstrainVout(tx.vout[2],1,CCaddr,inputs) == 0 )
240                     return eval->Invalid("vout2 doesnt match inputs fillbuy");
241                 else if ( ConstrainVout(tx.vout[1],0,0,0) == 0 )
242                     return eval->Invalid("vout1 is CC for fillbuy");
243                 else if ( ValidateBidRemainder(remaining_price,tx.vout[0].nValue,nValue,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false )
244                     return eval->Invalid("mismatched remainder for fillbuy");
245                 else if ( remaining_price != 0 )
246                 {
247                     if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) == 0 )
248                         return eval->Invalid("mismatched vout0 AssetsCCaddr for fillbuy");
249                 }
250             }
251             fprintf(stderr,"fillbuy validated\n");
252             break;
253             
254         case 's': // selloffer
255         case 'e': // exchange
256             //vin.0: normal input
257             //vin.1+: valid CC output for sale
258             //vout.0: vin.1 assetoshis output to CC to unspendable
259             //vout.1: normal output for change (if any)
260             //'s'.vout.n-1: opreturn [EVAL_ASSETS] ['s'] [assetid] [amount of native coin required] [origpubkey]
261             //'e'.vout.n-1: opreturn [EVAL_ASSETS] ['e'] [assetid] [assetid2] [amount of asset2 required] [origpubkey]
262             if ( remaining_price == 0 )
263                 return eval->Invalid("illegal null remaining_price for selloffer");
264             else if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,inputs) == 0 )
265                 return eval->Invalid("mismatched vout0 AssetsCCaddr for selloffer");
266             //fprintf(stderr,"remaining.%d for sell\n",(int32_t)remaining_price);
267             preventCCvouts = 1;
268             break;
269             
270         case 'x': // cancel
271             //vin.0: normal input
272             //vin.1: unspendable.(vout.0 from exchange or selloffer) sellTx/exchangeTx.vout[0] inputTx
273             //vout.0: vin.1 assetoshis to original pubkey CC sellTx/exchangeTx.vout[0].nValue -> [origpubkey]
274             //vout.1: normal output for change (if any)
275             //vout.n-1: opreturn [EVAL_ASSETS] ['x'] [assetid]
276             if ( (assetoshis= AssetValidateSellvin(cp,eval,tmpprice,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 )
277                 return(false);
278             else if ( ConstrainVout(tx.vout[0],1,CCaddr,assetoshis) == 0 )
279                 return eval->Invalid("invalid vout for cancel");
280             preventCCvins = 2;
281             preventCCvouts = 1;
282             break;
283             
284         case 'S': // fillsell
285             //vin.0: normal input
286             //vin.1: unspendable.(vout.0 assetoshis from selloffer) sellTx.vout[0]
287             //'S'.vin.2+: normal output that satisfies selloffer (*tx.vin[2])->nValue
288             //vout.0: remaining assetoshis -> unspendable
289             //vout.1: vin.1 assetoshis to signer of vin.2 sellTx.vout[0].nValue -> any
290             //'S'.vout.2: vin.2 value to original pubkey [origpubkey]
291             //vout.3: normal output for change (if any)
292             //'S'.vout.n-1: opreturn [EVAL_ASSETS] ['S'] [assetid] [amount of coin still required] [origpubkey]
293             if ( (assetoshis= AssetValidateSellvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 )
294                 return(false);
295             else if ( numvouts < 3 )
296                 return eval->Invalid("not enough vouts for fillask");
297             else if ( tmporigpubkey != origpubkey )
298                 return eval->Invalid("mismatched origpubkeys for fillask");
299             else
300             {
301                 if ( assetoshis != tx.vout[0].nValue+tx.vout[1].nValue )
302                     return eval->Invalid("locked value doesnt match vout0+1 fillask");
303                 if ( ValidateAskRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false )
304                     return eval->Invalid("mismatched remainder for fillask");
305                 else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 )
306                     return eval->Invalid("normal vout1 for fillask");
307                 else if ( ConstrainVout(tx.vout[2],0,origaddr,0) == 0 )
308                     return eval->Invalid("normal vout1 for fillask");
309                 else if ( remaining_price != 0 )
310                 {
311                     if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 )
312                         return eval->Invalid("mismatched vout0 AssetsCCaddr for fill");
313                 }
314             }
315             fprintf(stderr,"fill validated\n");
316             break;
317         case 'E': // fillexchange
318             //vin.0: normal input
319             //vin.1: unspendable.(vout.0 assetoshis from selloffer) sellTx.vout[0]
320             //vin.2+: valid CC assetid2 output that satisfies exchange (*tx.vin[2])->nValue
321             //vout.0: remaining assetoshis -> unspendable
322             //vout.1: vin.1 assetoshis to signer of vin.2 sellTx.vout[0].nValue -> any
323             //vout.2: vin.2+ assetoshis2 to original pubkey [origpubkey]
324             //vout.3: CC output for asset2 change (if any)
325             //vout.3/4: normal output for change (if any)
326             //vout.n-1: opreturn [EVAL_ASSETS] ['E'] [assetid vin0+1] [assetid vin2] [remaining asset2 required] [origpubkey]
327             if ( AssetExactAmounts(cp,inputs,1,outputs,eval,tx,assetid2) == false )
328                 eval->Invalid("asset2 inputs != outputs");
329             if ( (assetoshis= AssetValidateSellvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 )
330                 return(false);
331             else if ( numvouts < 3 )
332                 return eval->Invalid("not enough vouts for fillex");
333             else if ( tmporigpubkey != origpubkey )
334                 return eval->Invalid("mismatched origpubkeys for fillex");
335             else
336             {
337                 if ( assetoshis != tx.vout[0].nValue+tx.vout[1].nValue )
338                     return eval->Invalid("locked value doesnt match vout0+1 fillex");
339                 else if ( tx.vout[3].scriptPubKey.IsPayToCryptoCondition() != 0 )
340                 {
341                     if ( ConstrainVout(tx.vout[2],1,CCaddr,0) == 0 )
342                         return eval->Invalid("vout2 doesnt go to origpubkey fillex");
343                     else if ( inputs != tx.vout[2].nValue+tx.vout[3].nValue )
344                         return eval->Invalid("asset inputs doesnt match vout2+3 fillex");
345                 }
346                 else if ( ConstrainVout(tx.vout[2],1,CCaddr,inputs) == 0 )
347                     return eval->Invalid("vout2 doesnt match inputs fillex");
348                 else if ( ConstrainVout(tx.vout[1],0,0,0) == 0 )
349                     return eval->Invalid("vout1 is CC for fillex");
350                 fprintf(stderr,"assets vout0 %llu, vin1 %llu, vout2 %llu -> orig, vout1 %llu, total %llu\n",(long long)tx.vout[0].nValue,(long long)assetoshis,(long long)tx.vout[2].nValue,(long long)tx.vout[1].nValue,(long long)totalunits);
351                 if ( ValidateSwapRemainder(remaining_price,tx.vout[0].nValue,assetoshis,tx.vout[1].nValue,tx.vout[2].nValue,totalunits) == false )
352                     return eval->Invalid("mismatched remainder for fillex");
353                 else if ( ConstrainVout(tx.vout[1],1,0,0) == 0 )
354                     return eval->Invalid("normal vout1 for fillex");
355                 else if ( remaining_price != 0 )
356                 {
357                     if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 )
358                         return eval->Invalid("mismatched vout0 AssetsCCaddr for fillex");
359                 }
360             }
361             fprintf(stderr,"fill validated\n");
362             break;
363     }
364     return(PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts));
365 }
366
367
This page took 0.04621 seconds and 4 git commands to generate.