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; 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' )
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 ( 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 )
238 if ( ConstrainVout(tx.vout[0],1,cp->unspendableCCaddr,0) == 0 )
239 return eval->Invalid("mismatched vout0 AssetsCCaddr for fillbuy");
242 fprintf(stderr,"fillbuy validated\n");
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");
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 )
268 else if ( ConstrainVout(tx.vout[0],1,CCaddr,assetoshis) == 0 )
269 return eval->Invalid("invalid vout for cancel");
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]
289 if ( AssetExactAmounts(cp,inputs,1,outputs,eval,tx,assetid2) == false )
290 eval->Invalid("asset2 inputs != outputs");
292 if ( (assetoshis= AssetValidateSellvin(cp,eval,totalunits,tmporigpubkey,CCaddr,origaddr,tx,assetid)) == 0 )
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");
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 )
310 if ( ConstrainVout(tx.vout[0],1,(char *)cp->unspendableCCaddr,0) == 0 )
311 return eval->Invalid("mismatched vout0 AssetsCCaddr for fill");
314 fprintf(stderr,"fill validated\n");
317 return(PreventCC(eval,tx,preventCCvins,numvins,preventCCvouts,numvouts));