]>
Commit | Line | Data |
---|---|---|
c4408a6c | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
2d79bba3 | 3 | // Distributed under the MIT software license, see the accompanying |
bc909a7a | 4 | // file COPYING or https://www.opensource.org/licenses/mit-license.php . |
c4408a6c | 5 | |
84738627 PJ |
6 | #ifndef BITCOIN_SCRIPT_STANDARD_H |
7 | #define BITCOIN_SCRIPT_STANDARD_H | |
c4408a6c | 8 | |
b4347f60 | 9 | #include "uint256.h" |
b7c685b8 | 10 | #include "interpreter.h" |
0d7fed99 | 11 | #include "key.h" |
c4408a6c | 12 | |
85c579e3 CF |
13 | #include <boost/variant.hpp> |
14 | ||
c4408a6c | 15 | #include <stdint.h> |
16 | ||
eaa05ccf | 17 | static const unsigned int MAX_OP_RETURN_RELAY = MAX_SCRIPT_SIZE; //! bytes |
2aa63292 | 18 | extern unsigned nMaxDatacarrierBytes; |
c4408a6c | 19 | |
b9a36b15 MF |
20 | /** |
21 | * Mandatory script verification flags that all new blocks must comply with for | |
22 | * them to be valid. (but old blocks may not comply with) Currently just P2SH, | |
de609b8c DH |
23 | * but in the future other flags may be added. |
24 | * | |
b9a36b15 MF |
25 | * Failing one of these tests may trigger a DoS ban - see CheckInputs() for |
26 | * details. | |
27 | */ | |
c4408a6c | 28 | static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH; |
29 | ||
b9a36b15 MF |
30 | /** |
31 | * Standard script verification flags that standard transactions will comply | |
32 | * with. However scripts violating these flags may still be present in valid | |
33 | * blocks and we must accept those blocks. | |
34 | */ | |
c4408a6c | 35 | static const unsigned int STANDARD_SCRIPT_VERIFY_FLAGS = MANDATORY_SCRIPT_VERIFY_FLAGS | |
de609b8c | 36 | // SCRIPT_VERIFY_DERSIG is always enforced |
c4408a6c | 37 | SCRIPT_VERIFY_STRICTENC | |
698c6abb | 38 | SCRIPT_VERIFY_MINIMALDATA | |
03914234 | 39 | SCRIPT_VERIFY_NULLDUMMY | |
da918ac0 | 40 | SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS | |
71cc9d9f | 41 | SCRIPT_VERIFY_CLEANSTACK | |
6ea5ca4b | 42 | SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY | |
71cc9d9f | 43 | SCRIPT_VERIFY_LOW_S; |
c4408a6c | 44 | |
b9a36b15 | 45 | /** For convenience, standard but not mandatory verify flags. */ |
c4408a6c | 46 | static const unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS = STANDARD_SCRIPT_VERIFY_FLAGS & ~MANDATORY_SCRIPT_VERIFY_FLAGS; |
47 | ||
48 | enum txnouttype | |
49 | { | |
50 | TX_NONSTANDARD, | |
51 | // 'standard' transaction types: | |
52 | TX_PUBKEY, | |
53 | TX_PUBKEYHASH, | |
54 | TX_SCRIPTHASH, | |
55 | TX_MULTISIG, | |
a99ca25a | 56 | TX_CRYPTOCONDITION, |
c4408a6c | 57 | TX_NULL_DATA, |
58 | }; | |
59 | ||
185b2d4f | 60 | class CStakeParams |
61 | { | |
62 | public: | |
56fe75cb | 63 | static const uint32_t STAKE_MINPARAMS = 2; |
185b2d4f | 64 | static const uint32_t STAKE_MAXPARAMS = 5; |
56fe75cb | 65 | |
66 | enum | |
67 | { | |
68 | VERSION_INVALID = 0, | |
69 | VERSION_FIRST = 1, | |
70 | VERSION_ORIGINAL = 1, | |
71 | VERSION_CURRENT = 2, | |
72 | VERSION_EXTENDED_STAKE = 2, | |
73 | VERSION_LAST = 2 | |
74 | }; | |
185b2d4f | 75 | |
56fe75cb | 76 | uint32_t version; // used to determine the format as it evolves |
185b2d4f | 77 | uint32_t srcHeight; |
78 | uint32_t blkHeight; | |
79 | uint256 prevHash; | |
56fe75cb | 80 | CPubKey pk; // this was from an older version, and is only saved and restored during custom serialization |
81 | CTxDestination delegate; // this identifies an alternate valid recipient of the stake reward | |
185b2d4f | 82 | |
83 | CStakeParams() : srcHeight(0), blkHeight(0), prevHash(), pk() {} | |
84 | ||
85 | CStakeParams(const std::vector<std::vector<unsigned char>> &vData); | |
86 | ||
87 | CStakeParams(uint32_t _srcHeight, uint32_t _blkHeight, const uint256 &_prevHash, const CPubKey &_pk) : | |
56fe75cb | 88 | version(VERSION_ORIGINAL), srcHeight(_srcHeight), blkHeight(_blkHeight), prevHash(_prevHash), pk(_pk) {} |
89 | ||
90 | CStakeParams(uint32_t _srcHeight, uint32_t _blkHeight, const uint256 &_prevHash, const CTxDestination &_delegate) : | |
91 | version(VERSION_CURRENT), srcHeight(_srcHeight), blkHeight(_blkHeight), prevHash(_prevHash), delegate(_delegate) {} | |
92 | ||
93 | ADD_SERIALIZE_METHODS; | |
94 | ||
95 | template <typename Stream, typename Operation> | |
96 | inline void SerializationOp(Stream& s, Operation ser_action) { | |
97 | READWRITE(version); | |
98 | READWRITE(srcHeight); | |
99 | READWRITE(blkHeight); | |
100 | READWRITE(prevHash); | |
101 | CTransferDestination serDelegate; | |
102 | if (ser_action.ForRead()) | |
103 | { | |
104 | READWRITE(serDelegate); | |
105 | delegate = TransferDestinationToDestination(serDelegate); | |
106 | } | |
107 | else | |
108 | { | |
109 | serDelegate = DestinationToTransferDestination(delegate); | |
110 | READWRITE(serDelegate); | |
111 | } | |
112 | } | |
185b2d4f | 113 | |
114 | std::vector<unsigned char> AsVector() | |
115 | { | |
116 | std::vector<unsigned char> ret; | |
117 | CScript scr = CScript(); | |
56fe75cb | 118 | if (version >= VERSION_EXTENDED_STAKE) |
185b2d4f | 119 | { |
56fe75cb | 120 | scr << OPRETTYPE_STAKEPARAMS2; |
121 | scr << ::AsVector(*this); | |
31609f35 | 122 | ret = std::vector<unsigned char>(scr.begin(), scr.end()); |
56fe75cb | 123 | } |
124 | else | |
125 | { | |
126 | scr << OPRETTYPE_STAKEPARAMS; | |
127 | scr << srcHeight; | |
128 | scr << blkHeight; | |
129 | scr << std::vector<unsigned char>(prevHash.begin(), prevHash.end()); | |
130 | ||
131 | if (pk.IsValid()) | |
132 | { | |
133 | scr << std::vector<unsigned char>(pk.begin(), pk.end()); | |
134 | } | |
135 | ret = std::vector<unsigned char>(scr.begin(), scr.end()); | |
185b2d4f | 136 | } |
185b2d4f | 137 | return ret; |
138 | } | |
139 | ||
56fe75cb | 140 | bool IsValid() const { return version >= VERSION_FIRST && version <= VERSION_LAST && srcHeight != 0; } |
141 | ||
142 | uint32_t Version() const { return version; } | |
185b2d4f | 143 | }; |
144 | ||
07444da1 PW |
145 | /** Check whether a CTxDestination is a CNoDestination. */ |
146 | bool IsValidDestination(const CTxDestination& dest); | |
989b1de1 | 147 | bool IsTransparentAddress(const CTxDestination& dest); |
07444da1 | 148 | |
c4408a6c | 149 | const char* GetTxnOutputType(txnouttype t); |
150 | ||
151 | bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet); | |
152 | int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions); | |
153 | bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType); | |
b8deecdc | 154 | bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet, bool returnPubKey=false); |
0d7fed99 | 155 | bool ExtractDestinations(const CScript& scriptPubKey, |
156 | txnouttype& typeRet, | |
157 | std::vector<CTxDestination>& addressRet, | |
158 | int &nRequiredRet, | |
159 | const CKeyStore *pKeyStore=nullptr, | |
160 | bool *canSign=nullptr, | |
5bc89dab | 161 | bool *canSpend=nullptr, |
162 | uint32_t lastIdHeight=INT_MAX, | |
0d7fed99 | 163 | std::map<uint160, CKey> *pPrivKeys=nullptr); |
c4408a6c | 164 | |
0be990ba PW |
165 | CScript GetScriptForDestination(const CTxDestination& dest); |
166 | CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys); | |
167 | ||
b2a98c42 MT |
168 | bool IsPayToCryptoCondition(const CScript &scr, COptCCParams &ccParams); |
169 | ||
170 | template <typename T> | |
171 | bool IsPayToCryptoCondition(const CScript &scr, COptCCParams &ccParams, T &extraObject) | |
172 | { | |
173 | CScript subScript; | |
174 | std::vector<std::vector<unsigned char>> vParams; | |
175 | COptCCParams p; | |
176 | ||
177 | if (scr.IsPayToCryptoCondition(&subScript, vParams)) | |
178 | { | |
179 | if (!vParams.empty()) | |
180 | { | |
181 | ccParams = COptCCParams(vParams[0]); | |
182 | if (ccParams.IsValid() && ccParams.vData.size() > 0) | |
183 | { | |
184 | try | |
185 | { | |
186 | extraObject = T(ccParams.vData[0]); | |
187 | } | |
188 | catch(const std::exception& e) | |
189 | { | |
190 | std::cerr << e.what() << '\n'; | |
191 | } | |
192 | } | |
193 | } | |
194 | return true; | |
195 | } | |
196 | return false; | |
197 | } | |
198 | ||
68e174e2 | 199 | CTxDestination DestFromAddressHash(int scriptType, uint160& addressHash); |
3d682fc8 | 200 | CScript::ScriptType AddressTypeFromDest(const CTxDestination &dest); |
f381d4e0 | 201 | |
84738627 | 202 | #endif // BITCOIN_SCRIPT_STANDARD_H |