|
| 1 | +// Copyright (c) 2009-2010 Satoshi Nakamoto |
| 2 | +// Copyright (c) 2009-2013 The Bitcoin developers |
| 3 | +// Distributed under the MIT/X11 software license, see the accompanying |
| 4 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +#include "script.h" |
| 7 | + |
| 8 | +#include <boost/foreach.hpp> |
| 9 | + |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +const char* GetOpName(opcodetype opcode) |
| 13 | +{ |
| 14 | + switch (opcode) |
| 15 | + { |
| 16 | + // push value |
| 17 | + case OP_0 : return "0"; |
| 18 | + case OP_PUSHDATA1 : return "OP_PUSHDATA1"; |
| 19 | + case OP_PUSHDATA2 : return "OP_PUSHDATA2"; |
| 20 | + case OP_PUSHDATA4 : return "OP_PUSHDATA4"; |
| 21 | + case OP_1NEGATE : return "-1"; |
| 22 | + case OP_RESERVED : return "OP_RESERVED"; |
| 23 | + case OP_1 : return "1"; |
| 24 | + case OP_2 : return "2"; |
| 25 | + case OP_3 : return "3"; |
| 26 | + case OP_4 : return "4"; |
| 27 | + case OP_5 : return "5"; |
| 28 | + case OP_6 : return "6"; |
| 29 | + case OP_7 : return "7"; |
| 30 | + case OP_8 : return "8"; |
| 31 | + case OP_9 : return "9"; |
| 32 | + case OP_10 : return "10"; |
| 33 | + case OP_11 : return "11"; |
| 34 | + case OP_12 : return "12"; |
| 35 | + case OP_13 : return "13"; |
| 36 | + case OP_14 : return "14"; |
| 37 | + case OP_15 : return "15"; |
| 38 | + case OP_16 : return "16"; |
| 39 | + |
| 40 | + // control |
| 41 | + case OP_NOP : return "OP_NOP"; |
| 42 | + case OP_VER : return "OP_VER"; |
| 43 | + case OP_IF : return "OP_IF"; |
| 44 | + case OP_NOTIF : return "OP_NOTIF"; |
| 45 | + case OP_VERIF : return "OP_VERIF"; |
| 46 | + case OP_VERNOTIF : return "OP_VERNOTIF"; |
| 47 | + case OP_ELSE : return "OP_ELSE"; |
| 48 | + case OP_ENDIF : return "OP_ENDIF"; |
| 49 | + case OP_VERIFY : return "OP_VERIFY"; |
| 50 | + case OP_RETURN : return "OP_RETURN"; |
| 51 | + |
| 52 | + // stack ops |
| 53 | + case OP_TOALTSTACK : return "OP_TOALTSTACK"; |
| 54 | + case OP_FROMALTSTACK : return "OP_FROMALTSTACK"; |
| 55 | + case OP_2DROP : return "OP_2DROP"; |
| 56 | + case OP_2DUP : return "OP_2DUP"; |
| 57 | + case OP_3DUP : return "OP_3DUP"; |
| 58 | + case OP_2OVER : return "OP_2OVER"; |
| 59 | + case OP_2ROT : return "OP_2ROT"; |
| 60 | + case OP_2SWAP : return "OP_2SWAP"; |
| 61 | + case OP_IFDUP : return "OP_IFDUP"; |
| 62 | + case OP_DEPTH : return "OP_DEPTH"; |
| 63 | + case OP_DROP : return "OP_DROP"; |
| 64 | + case OP_DUP : return "OP_DUP"; |
| 65 | + case OP_NIP : return "OP_NIP"; |
| 66 | + case OP_OVER : return "OP_OVER"; |
| 67 | + case OP_PICK : return "OP_PICK"; |
| 68 | + case OP_ROLL : return "OP_ROLL"; |
| 69 | + case OP_ROT : return "OP_ROT"; |
| 70 | + case OP_SWAP : return "OP_SWAP"; |
| 71 | + case OP_TUCK : return "OP_TUCK"; |
| 72 | + |
| 73 | + // splice ops |
| 74 | + case OP_CAT : return "OP_CAT"; |
| 75 | + case OP_SUBSTR : return "OP_SUBSTR"; |
| 76 | + case OP_LEFT : return "OP_LEFT"; |
| 77 | + case OP_RIGHT : return "OP_RIGHT"; |
| 78 | + case OP_SIZE : return "OP_SIZE"; |
| 79 | + |
| 80 | + // bit logic |
| 81 | + case OP_INVERT : return "OP_INVERT"; |
| 82 | + case OP_AND : return "OP_AND"; |
| 83 | + case OP_OR : return "OP_OR"; |
| 84 | + case OP_XOR : return "OP_XOR"; |
| 85 | + case OP_EQUAL : return "OP_EQUAL"; |
| 86 | + case OP_EQUALVERIFY : return "OP_EQUALVERIFY"; |
| 87 | + case OP_RESERVED1 : return "OP_RESERVED1"; |
| 88 | + case OP_RESERVED2 : return "OP_RESERVED2"; |
| 89 | + |
| 90 | + // numeric |
| 91 | + case OP_1ADD : return "OP_1ADD"; |
| 92 | + case OP_1SUB : return "OP_1SUB"; |
| 93 | + case OP_2MUL : return "OP_2MUL"; |
| 94 | + case OP_2DIV : return "OP_2DIV"; |
| 95 | + case OP_NEGATE : return "OP_NEGATE"; |
| 96 | + case OP_ABS : return "OP_ABS"; |
| 97 | + case OP_NOT : return "OP_NOT"; |
| 98 | + case OP_0NOTEQUAL : return "OP_0NOTEQUAL"; |
| 99 | + case OP_ADD : return "OP_ADD"; |
| 100 | + case OP_SUB : return "OP_SUB"; |
| 101 | + case OP_MUL : return "OP_MUL"; |
| 102 | + case OP_DIV : return "OP_DIV"; |
| 103 | + case OP_MOD : return "OP_MOD"; |
| 104 | + case OP_LSHIFT : return "OP_LSHIFT"; |
| 105 | + case OP_RSHIFT : return "OP_RSHIFT"; |
| 106 | + case OP_BOOLAND : return "OP_BOOLAND"; |
| 107 | + case OP_BOOLOR : return "OP_BOOLOR"; |
| 108 | + case OP_NUMEQUAL : return "OP_NUMEQUAL"; |
| 109 | + case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY"; |
| 110 | + case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL"; |
| 111 | + case OP_LESSTHAN : return "OP_LESSTHAN"; |
| 112 | + case OP_GREATERTHAN : return "OP_GREATERTHAN"; |
| 113 | + case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL"; |
| 114 | + case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL"; |
| 115 | + case OP_MIN : return "OP_MIN"; |
| 116 | + case OP_MAX : return "OP_MAX"; |
| 117 | + case OP_WITHIN : return "OP_WITHIN"; |
| 118 | + |
| 119 | + // crypto |
| 120 | + case OP_RIPEMD160 : return "OP_RIPEMD160"; |
| 121 | + case OP_SHA1 : return "OP_SHA1"; |
| 122 | + case OP_SHA256 : return "OP_SHA256"; |
| 123 | + case OP_HASH160 : return "OP_HASH160"; |
| 124 | + case OP_HASH256 : return "OP_HASH256"; |
| 125 | + case OP_CODESEPARATOR : return "OP_CODESEPARATOR"; |
| 126 | + case OP_CHECKSIG : return "OP_CHECKSIG"; |
| 127 | + case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY"; |
| 128 | + case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG"; |
| 129 | + case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY"; |
| 130 | + |
| 131 | + // expanson |
| 132 | + case OP_NOP1 : return "OP_NOP1"; |
| 133 | + case OP_NOP2 : return "OP_NOP2"; |
| 134 | + case OP_NOP3 : return "OP_NOP3"; |
| 135 | + case OP_NOP4 : return "OP_NOP4"; |
| 136 | + case OP_NOP5 : return "OP_NOP5"; |
| 137 | + case OP_NOP6 : return "OP_NOP6"; |
| 138 | + case OP_NOP7 : return "OP_NOP7"; |
| 139 | + case OP_NOP8 : return "OP_NOP8"; |
| 140 | + case OP_NOP9 : return "OP_NOP9"; |
| 141 | + case OP_NOP10 : return "OP_NOP10"; |
| 142 | + |
| 143 | + case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE"; |
| 144 | + |
| 145 | + // Note: |
| 146 | + // The template matching params OP_SMALLDATA/etc are defined in opcodetype enum |
| 147 | + // as kind of implementation hack, they are *NOT* real opcodes. If found in real |
| 148 | + // Script, just let the default: case deal with them. |
| 149 | + |
| 150 | + default: |
| 151 | + return "OP_UNKNOWN"; |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +unsigned int CScript::GetSigOpCount(bool fAccurate) const |
| 156 | +{ |
| 157 | + unsigned int n = 0; |
| 158 | + const_iterator pc = begin(); |
| 159 | + opcodetype lastOpcode = OP_INVALIDOPCODE; |
| 160 | + while (pc < end()) |
| 161 | + { |
| 162 | + opcodetype opcode; |
| 163 | + if (!GetOp(pc, opcode)) |
| 164 | + break; |
| 165 | + if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY) |
| 166 | + n++; |
| 167 | + else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY) |
| 168 | + { |
| 169 | + if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16) |
| 170 | + n += DecodeOP_N(lastOpcode); |
| 171 | + else |
| 172 | + n += 20; |
| 173 | + } |
| 174 | + lastOpcode = opcode; |
| 175 | + } |
| 176 | + return n; |
| 177 | +} |
| 178 | + |
| 179 | +unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const |
| 180 | +{ |
| 181 | + if (!IsPayToScriptHash()) |
| 182 | + return GetSigOpCount(true); |
| 183 | + |
| 184 | + // This is a pay-to-script-hash scriptPubKey; |
| 185 | + // get the last item that the scriptSig |
| 186 | + // pushes onto the stack: |
| 187 | + const_iterator pc = scriptSig.begin(); |
| 188 | + vector<unsigned char> data; |
| 189 | + while (pc < scriptSig.end()) |
| 190 | + { |
| 191 | + opcodetype opcode; |
| 192 | + if (!scriptSig.GetOp(pc, opcode, data)) |
| 193 | + return 0; |
| 194 | + if (opcode > OP_16) |
| 195 | + return 0; |
| 196 | + } |
| 197 | + |
| 198 | + /// ... and return its opcount: |
| 199 | + CScript subscript(data.begin(), data.end()); |
| 200 | + return subscript.GetSigOpCount(true); |
| 201 | +} |
| 202 | + |
| 203 | +bool CScript::IsPayToScriptHash() const |
| 204 | +{ |
| 205 | + // Extra-fast test for pay-to-script-hash CScripts: |
| 206 | + return (this->size() == 23 && |
| 207 | + this->at(0) == OP_HASH160 && |
| 208 | + this->at(1) == 0x14 && |
| 209 | + this->at(22) == OP_EQUAL); |
| 210 | +} |
| 211 | + |
| 212 | +bool CScript::IsPushOnly() const |
| 213 | +{ |
| 214 | + const_iterator pc = begin(); |
| 215 | + while (pc < end()) |
| 216 | + { |
| 217 | + opcodetype opcode; |
| 218 | + if (!GetOp(pc, opcode)) |
| 219 | + return false; |
| 220 | + // Note that IsPushOnly() *does* consider OP_RESERVED to be a |
| 221 | + // push-type opcode, however execution of OP_RESERVED fails, so |
| 222 | + // it's not relevant to P2SH as the scriptSig would fail prior to |
| 223 | + // the P2SH special validation code being executed. |
| 224 | + if (opcode > OP_16) |
| 225 | + return false; |
| 226 | + } |
| 227 | + return true; |
| 228 | +} |
| 229 | + |
| 230 | +bool CScript::HasCanonicalPushes() const |
| 231 | +{ |
| 232 | + const_iterator pc = begin(); |
| 233 | + while (pc < end()) |
| 234 | + { |
| 235 | + opcodetype opcode; |
| 236 | + std::vector<unsigned char> data; |
| 237 | + if (!GetOp(pc, opcode, data)) |
| 238 | + return false; |
| 239 | + if (opcode > OP_16) |
| 240 | + continue; |
| 241 | + if (opcode < OP_PUSHDATA1 && opcode > OP_0 && (data.size() == 1 && data[0] <= 16)) |
| 242 | + // Could have used an OP_n code, rather than a 1-byte push. |
| 243 | + return false; |
| 244 | + if (opcode == OP_PUSHDATA1 && data.size() < OP_PUSHDATA1) |
| 245 | + // Could have used a normal n-byte push, rather than OP_PUSHDATA1. |
| 246 | + return false; |
| 247 | + if (opcode == OP_PUSHDATA2 && data.size() <= 0xFF) |
| 248 | + // Could have used an OP_PUSHDATA1. |
| 249 | + return false; |
| 250 | + if (opcode == OP_PUSHDATA4 && data.size() <= 0xFFFF) |
| 251 | + // Could have used an OP_PUSHDATA2. |
| 252 | + return false; |
| 253 | + } |
| 254 | + return true; |
| 255 | +} |
| 256 | + |
| 257 | +class CScriptVisitor : public boost::static_visitor<bool> |
| 258 | +{ |
| 259 | +private: |
| 260 | + CScript *script; |
| 261 | +public: |
| 262 | + CScriptVisitor(CScript *scriptin) { script = scriptin; } |
| 263 | + |
| 264 | + bool operator()(const CNoDestination &dest) const { |
| 265 | + script->clear(); |
| 266 | + return false; |
| 267 | + } |
| 268 | + |
| 269 | + bool operator()(const CKeyID &keyID) const { |
| 270 | + script->clear(); |
| 271 | + *script << OP_DUP << OP_HASH160 << keyID << OP_EQUALVERIFY << OP_CHECKSIG; |
| 272 | + return true; |
| 273 | + } |
| 274 | + |
| 275 | + bool operator()(const CScriptID &scriptID) const { |
| 276 | + script->clear(); |
| 277 | + *script << OP_HASH160 << scriptID << OP_EQUAL; |
| 278 | + return true; |
| 279 | + } |
| 280 | +}; |
| 281 | + |
| 282 | +void CScript::SetDestination(const CTxDestination& dest) |
| 283 | +{ |
| 284 | + boost::apply_visitor(CScriptVisitor(this), dest); |
| 285 | +} |
| 286 | + |
| 287 | +void CScript::SetMultisig(int nRequired, const std::vector<CPubKey>& keys) |
| 288 | +{ |
| 289 | + this->clear(); |
| 290 | + |
| 291 | + *this << EncodeOP_N(nRequired); |
| 292 | + BOOST_FOREACH(const CPubKey& key, keys) |
| 293 | + *this << key; |
| 294 | + *this << EncodeOP_N(keys.size()) << OP_CHECKMULTISIG; |
| 295 | +} |
0 commit comments