Major updates integration from all upstreams
[VerusCoin.git] / src / cc / CCinclude.h
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 #ifndef CC_INCLUDE_H
17 #define CC_INCLUDE_H
18
19 /*
20 there are only a very few types in bitcoin. pay to pubkey, pay to pubkey hash and pay to script hash
21 p2pk, p2pkh, p2sh
22 there are actually more that are possible, but those three are 99%+ of bitcoin transactions
23 so you can pay to a pubkey, or to its hash. or to a script's hash. the last is how most of the more complex scripts are invoked. to spend a p2sh vout, you need to provide the redeemscript, this script's hash is what the p2sh address was.
24 all of the above are the standard bitcoin vout types and there should be plenty of materials about it
25 Encrypted by a verified device
26 what I did with the CC contracts is created a fourth type of vout, the CC vout. this is using the cryptoconditions standard and it is even a different signature mechanism. ed25519 instead of secp256k1. it is basically a big extension to the bitcoin script. There is a special opcode that is added that says it is a CC script.
27  
28 but it gets more interesting
29 each CC script has an evalcode
30 this is just an arbitrary number. but what it does is allows to create a self-contained universe of CC utxo that all have the same evalcode and that is how a faucet CC differentiates itself from a dice CC, the eval code is different
31
32 one effect from using a different eval code is that even if the rest of the CC script is the same, the bitcoin address that is calculated is different. what this means is that for each pubkey, there is a unique address for each different eval code!
33 and this allows efficient segregation of one CC contracts transactions from another
34 the final part that will make it all clear how the funds can be locked inside the contract. this is what makes a contract, a contract. I put both the privkey and pubkey for a randomly chosen address and associate it with each CC contract. That means anybody can sign outputs for that privkey. However, it is a CC output, so in addition to the signature, whatever constraints a CC contract implements must also be satistifed. This allows funds to be locked and yet anybody is able to spend it, assuming they satisfy the CC's rules
35
36 one other technical note is that komodod has the insight-explorer extensions built in. so it can lookup directly all transactions to any address. this is a key performance boosting thing as if it wasnt there, trying to get all the utxo for an address not in the wallet is quite time consuming
37 */
38
39 #include <cc/eval.h>
40 #include <script/cc.h>
41 #include <script/script.h>
42 #include <cryptoconditions.h>
43 #include "../script/standard.h"
44 #include "../base58.h"
45 #include "../core_io.h"
46 #include "../script/sign.h"
47 #include "../wallet/wallet.h"
48 #include <univalue.h>
49 #include <exception>
50 #include "../komodo_defs.h"
51
52 extern int32_t KOMODO_CONNECTING,KOMODO_CCACTIVATE;
53 extern uint32_t ASSETCHAINS_CC;
54 extern std::string CCerror;
55
56 #define SMALLVAL 0.000000000000001
57 union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; };
58 typedef union _bits256 bits256;
59
60 struct CC_utxo
61 {
62     uint256 txid;
63     int64_t nValue;
64     int32_t vout;
65 };
66
67 struct CCcontract_info
68 {
69     uint256 prevtxid;
70     char unspendableCCaddr[64],CChexstr[72],normaladdr[64],unspendableaddr2[64],unspendableaddr3[64];
71     uint8_t CCpriv[32],unspendablepriv2[32],unspendablepriv3[32];
72     CPubKey unspendablepk2,unspendablepk3;
73     bool (*validate)(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx);
74     bool (*ismyvin)(CScript const& scriptSig);
75     uint8_t evalcode,evalcode2,evalcode3,didinit;
76 };
77 struct CCcontract_info *CCinit(struct CCcontract_info *cp,uint8_t evalcode);
78
79 struct oracleprice_info
80 {
81     CPubKey pk;
82     std::vector <uint8_t> data;
83     int32_t height;
84 };
85
86 #ifdef ENABLE_WALLET
87 extern CWallet* pwalletMain;
88 #endif
89 bool GetAddressUnspent(uint160 addressHash, int type,std::vector<std::pair<CAddressUnspentKey,CAddressUnspentValue> > &unspentOutputs);
90
91 static const uint256 zeroid;
92 bool myGetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock);
93 int32_t is_hexstr(char *str,int32_t n);
94 bool myAddtomempool(CTransaction &tx);
95 //uint64_t myGettxout(uint256 hash,int32_t n);
96 bool myIsutxo_spentinmempool(uint256 txid,int32_t vout);
97 int32_t myIsutxo_spent(uint256 &spenttxid,uint256 txid,int32_t vout);
98 bool mySendrawtransaction(std::string res);
99 int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex);
100 int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp);
101 int32_t iguana_rwbignum(int32_t rwflag,uint8_t *serialized,int32_t len,uint8_t *endianedp);
102 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
103 int64_t CCaddress_balance(char *coinaddr);
104 CPubKey CCtxidaddr(char *txidaddr,uint256 txid);
105
106 int64_t OraclePrice(int32_t height,uint256 reforacletxid,char *markeraddr,char *format);
107 uint8_t DecodeOraclesCreateOpRet(const CScript &scriptPubKey,std::string &name,std::string &description,std::string &format);
108 uint256 OracleMerkle(int32_t height,uint256 reforacletxid,char *format,std::vector<struct oracle_merklepair>publishers);
109 uint256 OraclesBatontxid(uint256 oracletxid,CPubKey pk);
110 int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,int64_t total,int32_t maxinputs);
111 bool DecodeHexTx(CTransaction& tx, const std::string& strHexTx);
112 bool DecodeAssetCreateOpRet(const CScript &scriptPubKey,std::vector<uint8_t> &origpubkey,std::string &name,std::string &description);
113 uint8_t DecodeAssetOpRet(const CScript &scriptPubKey,uint256 &assetid,uint256 &assetid2,int64_t &price,std::vector<uint8_t> &origpubkey);
114 uint8_t DecodeOraclesData(const CScript &scriptPubKey,uint256 &oracletxid,uint256 &batontxid,CPubKey &pk,std::vector <uint8_t>&data);
115 int32_t oracle_format(uint256 *hashp,int64_t *valp,char *str,uint8_t fmt,uint8_t *data,int32_t offset,int32_t datalen);
116 CScript EncodeAssetOpRet(uint8_t funcid,uint256 assetid,uint256 assetid2,int64_t price,std::vector<uint8_t> origpubkey);
117
118 // CCcustom
119 CPubKey GetUnspendable(struct CCcontract_info *cp,uint8_t *unspendablepriv);
120
121 // CCutils
122 CPubKey buf2pk(uint8_t *buf33);
123 void endiancpy(uint8_t *dest,uint8_t *src,int32_t len);
124 uint256 DiceHashEntropy(uint256 &entropy,uint256 _txidpriv);
125 CTxOut MakeCC1vout(uint8_t evalcode,CAmount nValue,CPubKey pk);
126 CTxOut MakeCC1of2vout(uint8_t evalcode,CAmount nValue,CPubKey pk,CPubKey pk2);
127 CC *MakeCCcond1(uint8_t evalcode,CPubKey pk);
128 CC* GetCryptoCondition(CScript const& scriptSig);
129 void CCaddr2set(struct CCcontract_info *cp,uint8_t evalcode,CPubKey pk,uint8_t *priv,char *coinaddr);
130 void CCaddr3set(struct CCcontract_info *cp,uint8_t evalcode,CPubKey pk,uint8_t *priv,char *coinaddr);
131 bool IsCCInput(CScript const& scriptSig);
132 int32_t unstringbits(char *buf,uint64_t bits);
133 uint64_t stringbits(char *str);
134 uint256 revuint256(uint256 txid);
135 char *uint256_str(char *dest,uint256 txid);
136 char *pubkey33_str(char *dest,uint8_t *pubkey33);
137 uint256 Parseuint256(char *hexstr);
138 CPubKey pubkey2pk(std::vector<uint8_t> pubkey);
139 int64_t CCfullsupply(uint256 tokenid);
140 int64_t CCtoken_balance(char *destaddr,uint256 tokenid);
141 bool _GetCCaddress(char *destaddr,uint8_t evalcode,CPubKey pk);
142 bool GetCCaddress(struct CCcontract_info *cp,char *destaddr,CPubKey pk);
143 bool GetCCaddress1of2(struct CCcontract_info *cp,char *destaddr,CPubKey pk,CPubKey pk2);
144 bool ConstrainVout(CTxOut vout,int32_t CCflag,char *cmpaddr,int64_t nValue);
145 bool PreventCC(Eval* eval,const CTransaction &tx,int32_t preventCCvins,int32_t numvins,int32_t preventCCvouts,int32_t numvouts);
146 bool Getscriptaddress(char *destaddr,const CScript &scriptPubKey);
147 std::vector<uint8_t> Mypubkey();
148 bool Myprivkey(uint8_t myprivkey[]);
149 int64_t CCduration(int32_t &numblocks,uint256 txid);
150
151 // CCtx
152 std::string FinalizeCCTx(uint64_t skipmask,struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey mypk,uint64_t txfee,CScript opret);
153 void SetCCunspents(std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &unspentOutputs,char *coinaddr);
154 void SetCCtxids(std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,char *coinaddr);
155 int64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,int64_t total,int32_t maxinputs);
156 int64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout);
157
158 // curve25519 and sha256
159 bits256 curve25519_shared(bits256 privkey,bits256 otherpub);
160 bits256 curve25519_basepoint9();
161 bits256 curve25519(bits256 mysecret,bits256 basepoint);
162 void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len);
163 bits256 bits256_doublesha256(char *deprecated,uint8_t *data,int32_t datalen);
164
165 #endif
This page took 0.039593 seconds and 4 git commands to generate.