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 ******************************************************************************/
19 Assets can be created or transferred.
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.
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.
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.
27 What this means is that vout 0 of the creation txid and vouts 1 ... n-2 for transfer vouts are assetoshi outputs.
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.
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
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>
46 valid CC output: create or transfer or buyoffer or selloffer or exchange or cancel or fill
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>"
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]
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]
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]
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]
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]
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]
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]
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]
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]
132 bool AssetsValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx)
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' )
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");
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");
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]
178 return eval->Invalid("no asset inputs for transfer");
179 fprintf(stderr,"transfer validated %.8f -> %.8f\n",(double)inputs/COIN,(double)outputs/COIN);
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");
193 fprintf(stderr,"buy offer validated to destaddr.(%s)\n",cp->unspendableCCaddr);
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 )
204 else if ( ConstrainVout(tx.vout[0],0,origaddr,nValue) == 0 )
205 return eval->Invalid("invalid refund for cancelbuy");
208 fprintf(stderr,"cancelbuy validated to origaddr.(%s)\n",origaddr);
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]
222 if ( (nValue= AssetValidateBuyvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 )
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");
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 )
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");
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 )
247 if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) == 0 )
248 return eval->Invalid("mismatched vout0 AssetsCCaddr for fillbuy");
251 fprintf(stderr,"fillbuy validated\n");
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);
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 )
278 else if ( ConstrainVout(tx.vout[0],1,CCaddr,assetoshis) == 0 )
279 return eval->Invalid("invalid vout for cancel");
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 )
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");
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 )
311 if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 )
312 return eval->Invalid("mismatched vout0 AssetsCCaddr for fill");
315 fprintf(stderr,"fill validated\n");
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 )
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");
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 )
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");
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 )
357 if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 )
358 return eval->Invalid("mismatched vout0 AssetsCCaddr for fillex");
361 fprintf(stderr,"fill validated\n");
364 return(PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts));