Bugfix: avoid sub-cent change (lost in fees) whenever possible
[bitcoin.git] / script.h
blobda904ef489b2b45ab612828c81466917e2f7df30
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
5 class CTransaction;
7 enum
9 SIGHASH_ALL = 1,
10 SIGHASH_NONE = 2,
11 SIGHASH_SINGLE = 3,
12 SIGHASH_ANYONECANPAY = 0x80,
17 enum opcodetype
19 // push value
20 OP_0=0,
21 OP_FALSE=OP_0,
22 OP_PUSHDATA1=76,
23 OP_PUSHDATA2,
24 OP_PUSHDATA4,
25 OP_1NEGATE,
26 OP_RESERVED,
27 OP_1,
28 OP_TRUE=OP_1,
29 OP_2,
30 OP_3,
31 OP_4,
32 OP_5,
33 OP_6,
34 OP_7,
35 OP_8,
36 OP_9,
37 OP_10,
38 OP_11,
39 OP_12,
40 OP_13,
41 OP_14,
42 OP_15,
43 OP_16,
45 // control
46 OP_NOP,
47 OP_VER,
48 OP_IF,
49 OP_NOTIF,
50 OP_VERIF,
51 OP_VERNOTIF,
52 OP_ELSE,
53 OP_ENDIF,
54 OP_VERIFY,
55 OP_RETURN,
57 // stack ops
58 OP_TOALTSTACK,
59 OP_FROMALTSTACK,
60 OP_2DROP,
61 OP_2DUP,
62 OP_3DUP,
63 OP_2OVER,
64 OP_2ROT,
65 OP_2SWAP,
66 OP_IFDUP,
67 OP_DEPTH,
68 OP_DROP,
69 OP_DUP,
70 OP_NIP,
71 OP_OVER,
72 OP_PICK,
73 OP_ROLL,
74 OP_ROT,
75 OP_SWAP,
76 OP_TUCK,
78 // splice ops
79 OP_CAT,
80 OP_SUBSTR,
81 OP_LEFT,
82 OP_RIGHT,
83 OP_SIZE,
85 // bit logic
86 OP_INVERT,
87 OP_AND,
88 OP_OR,
89 OP_XOR,
90 OP_EQUAL,
91 OP_EQUALVERIFY,
92 OP_RESERVED1,
93 OP_RESERVED2,
95 // numeric
96 OP_1ADD,
97 OP_1SUB,
98 OP_2MUL,
99 OP_2DIV,
100 OP_NEGATE,
101 OP_ABS,
102 OP_NOT,
103 OP_0NOTEQUAL,
105 OP_ADD,
106 OP_SUB,
107 OP_MUL,
108 OP_DIV,
109 OP_MOD,
110 OP_LSHIFT,
111 OP_RSHIFT,
113 OP_BOOLAND,
114 OP_BOOLOR,
115 OP_NUMEQUAL,
116 OP_NUMEQUALVERIFY,
117 OP_NUMNOTEQUAL,
118 OP_LESSTHAN,
119 OP_GREATERTHAN,
120 OP_LESSTHANOREQUAL,
121 OP_GREATERTHANOREQUAL,
122 OP_MIN,
123 OP_MAX,
125 OP_WITHIN,
127 // crypto
128 OP_RIPEMD160,
129 OP_SHA1,
130 OP_SHA256,
131 OP_HASH160,
132 OP_HASH256,
133 OP_CODESEPARATOR,
134 OP_CHECKSIG,
135 OP_CHECKSIGVERIFY,
136 OP_CHECKMULTISIG,
137 OP_CHECKMULTISIGVERIFY,
139 // expansion
140 OP_NOP1,
141 OP_NOP2,
142 OP_NOP3,
143 OP_NOP4,
144 OP_NOP5,
145 OP_NOP6,
146 OP_NOP7,
147 OP_NOP8,
148 OP_NOP9,
149 OP_NOP10,
153 // template matching params
154 OP_PUBKEYHASH = 0xfd,
155 OP_PUBKEY = 0xfe,
157 OP_INVALIDOPCODE = 0xff,
167 inline const char* GetOpName(opcodetype opcode)
169 switch (opcode)
171 // push value
172 case OP_0 : return "0";
173 case OP_PUSHDATA1 : return "OP_PUSHDATA1";
174 case OP_PUSHDATA2 : return "OP_PUSHDATA2";
175 case OP_PUSHDATA4 : return "OP_PUSHDATA4";
176 case OP_1NEGATE : return "-1";
177 case OP_RESERVED : return "OP_RESERVED";
178 case OP_1 : return "1";
179 case OP_2 : return "2";
180 case OP_3 : return "3";
181 case OP_4 : return "4";
182 case OP_5 : return "5";
183 case OP_6 : return "6";
184 case OP_7 : return "7";
185 case OP_8 : return "8";
186 case OP_9 : return "9";
187 case OP_10 : return "10";
188 case OP_11 : return "11";
189 case OP_12 : return "12";
190 case OP_13 : return "13";
191 case OP_14 : return "14";
192 case OP_15 : return "15";
193 case OP_16 : return "16";
195 // control
196 case OP_NOP : return "OP_NOP";
197 case OP_VER : return "OP_VER";
198 case OP_IF : return "OP_IF";
199 case OP_NOTIF : return "OP_NOTIF";
200 case OP_VERIF : return "OP_VERIF";
201 case OP_VERNOTIF : return "OP_VERNOTIF";
202 case OP_ELSE : return "OP_ELSE";
203 case OP_ENDIF : return "OP_ENDIF";
204 case OP_VERIFY : return "OP_VERIFY";
205 case OP_RETURN : return "OP_RETURN";
207 // stack ops
208 case OP_TOALTSTACK : return "OP_TOALTSTACK";
209 case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
210 case OP_2DROP : return "OP_2DROP";
211 case OP_2DUP : return "OP_2DUP";
212 case OP_3DUP : return "OP_3DUP";
213 case OP_2OVER : return "OP_2OVER";
214 case OP_2ROT : return "OP_2ROT";
215 case OP_2SWAP : return "OP_2SWAP";
216 case OP_IFDUP : return "OP_IFDUP";
217 case OP_DEPTH : return "OP_DEPTH";
218 case OP_DROP : return "OP_DROP";
219 case OP_DUP : return "OP_DUP";
220 case OP_NIP : return "OP_NIP";
221 case OP_OVER : return "OP_OVER";
222 case OP_PICK : return "OP_PICK";
223 case OP_ROLL : return "OP_ROLL";
224 case OP_ROT : return "OP_ROT";
225 case OP_SWAP : return "OP_SWAP";
226 case OP_TUCK : return "OP_TUCK";
228 // splice ops
229 case OP_CAT : return "OP_CAT";
230 case OP_SUBSTR : return "OP_SUBSTR";
231 case OP_LEFT : return "OP_LEFT";
232 case OP_RIGHT : return "OP_RIGHT";
233 case OP_SIZE : return "OP_SIZE";
235 // bit logic
236 case OP_INVERT : return "OP_INVERT";
237 case OP_AND : return "OP_AND";
238 case OP_OR : return "OP_OR";
239 case OP_XOR : return "OP_XOR";
240 case OP_EQUAL : return "OP_EQUAL";
241 case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
242 case OP_RESERVED1 : return "OP_RESERVED1";
243 case OP_RESERVED2 : return "OP_RESERVED2";
245 // numeric
246 case OP_1ADD : return "OP_1ADD";
247 case OP_1SUB : return "OP_1SUB";
248 case OP_2MUL : return "OP_2MUL";
249 case OP_2DIV : return "OP_2DIV";
250 case OP_NEGATE : return "OP_NEGATE";
251 case OP_ABS : return "OP_ABS";
252 case OP_NOT : return "OP_NOT";
253 case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
254 case OP_ADD : return "OP_ADD";
255 case OP_SUB : return "OP_SUB";
256 case OP_MUL : return "OP_MUL";
257 case OP_DIV : return "OP_DIV";
258 case OP_MOD : return "OP_MOD";
259 case OP_LSHIFT : return "OP_LSHIFT";
260 case OP_RSHIFT : return "OP_RSHIFT";
261 case OP_BOOLAND : return "OP_BOOLAND";
262 case OP_BOOLOR : return "OP_BOOLOR";
263 case OP_NUMEQUAL : return "OP_NUMEQUAL";
264 case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
265 case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
266 case OP_LESSTHAN : return "OP_LESSTHAN";
267 case OP_GREATERTHAN : return "OP_GREATERTHAN";
268 case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
269 case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
270 case OP_MIN : return "OP_MIN";
271 case OP_MAX : return "OP_MAX";
272 case OP_WITHIN : return "OP_WITHIN";
274 // crypto
275 case OP_RIPEMD160 : return "OP_RIPEMD160";
276 case OP_SHA1 : return "OP_SHA1";
277 case OP_SHA256 : return "OP_SHA256";
278 case OP_HASH160 : return "OP_HASH160";
279 case OP_HASH256 : return "OP_HASH256";
280 case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
281 case OP_CHECKSIG : return "OP_CHECKSIG";
282 case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
283 case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
284 case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
286 // expanson
287 case OP_NOP1 : return "OP_NOP1";
288 case OP_NOP2 : return "OP_NOP2";
289 case OP_NOP3 : return "OP_NOP3";
290 case OP_NOP4 : return "OP_NOP4";
291 case OP_NOP5 : return "OP_NOP5";
292 case OP_NOP6 : return "OP_NOP6";
293 case OP_NOP7 : return "OP_NOP7";
294 case OP_NOP8 : return "OP_NOP8";
295 case OP_NOP9 : return "OP_NOP9";
296 case OP_NOP10 : return "OP_NOP10";
300 // template matching params
301 case OP_PUBKEYHASH : return "OP_PUBKEYHASH";
302 case OP_PUBKEY : return "OP_PUBKEY";
304 case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
305 default:
306 return "OP_UNKNOWN";
313 inline string ValueString(const vector<unsigned char>& vch)
315 if (vch.size() <= 4)
316 return strprintf("%d", CBigNum(vch).getint());
317 else
318 return HexStr(vch);
321 inline string StackString(const vector<vector<unsigned char> >& vStack)
323 string str;
324 foreach(const vector<unsigned char>& vch, vStack)
326 if (!str.empty())
327 str += " ";
328 str += ValueString(vch);
330 return str;
341 class CScript : public vector<unsigned char>
343 protected:
344 CScript& push_int64(int64 n)
346 if (n == -1 || (n >= 1 && n <= 16))
348 push_back(n + (OP_1 - 1));
350 else
352 CBigNum bn(n);
353 *this << bn.getvch();
355 return *this;
358 CScript& push_uint64(uint64 n)
360 if (n >= 1 && n <= 16)
362 push_back(n + (OP_1 - 1));
364 else
366 CBigNum bn(n);
367 *this << bn.getvch();
369 return *this;
372 public:
373 CScript() { }
374 CScript(const CScript& b) : vector<unsigned char>(b.begin(), b.end()) { }
375 CScript(const_iterator pbegin, const_iterator pend) : vector<unsigned char>(pbegin, pend) { }
376 #ifndef _MSC_VER
377 CScript(const unsigned char* pbegin, const unsigned char* pend) : vector<unsigned char>(pbegin, pend) { }
378 #endif
380 CScript& operator+=(const CScript& b)
382 insert(end(), b.begin(), b.end());
383 return *this;
386 friend CScript operator+(const CScript& a, const CScript& b)
388 CScript ret = a;
389 ret += b;
390 return ret;
394 explicit CScript(char b) { operator<<(b); }
395 explicit CScript(short b) { operator<<(b); }
396 explicit CScript(int b) { operator<<(b); }
397 explicit CScript(long b) { operator<<(b); }
398 explicit CScript(int64 b) { operator<<(b); }
399 explicit CScript(unsigned char b) { operator<<(b); }
400 explicit CScript(unsigned int b) { operator<<(b); }
401 explicit CScript(unsigned short b) { operator<<(b); }
402 explicit CScript(unsigned long b) { operator<<(b); }
403 explicit CScript(uint64 b) { operator<<(b); }
405 explicit CScript(opcodetype b) { operator<<(b); }
406 explicit CScript(const uint256& b) { operator<<(b); }
407 explicit CScript(const CBigNum& b) { operator<<(b); }
408 explicit CScript(const vector<unsigned char>& b) { operator<<(b); }
411 CScript& operator<<(char b) { return push_int64(b); }
412 CScript& operator<<(short b) { return push_int64(b); }
413 CScript& operator<<(int b) { return push_int64(b); }
414 CScript& operator<<(long b) { return push_int64(b); }
415 CScript& operator<<(int64 b) { return push_int64(b); }
416 CScript& operator<<(unsigned char b) { return push_uint64(b); }
417 CScript& operator<<(unsigned int b) { return push_uint64(b); }
418 CScript& operator<<(unsigned short b) { return push_uint64(b); }
419 CScript& operator<<(unsigned long b) { return push_uint64(b); }
420 CScript& operator<<(uint64 b) { return push_uint64(b); }
422 CScript& operator<<(opcodetype opcode)
424 if (opcode < 0 || opcode > 0xff)
425 throw runtime_error("CScript::operator<<() : invalid opcode");
426 insert(end(), (unsigned char)opcode);
427 return *this;
430 CScript& operator<<(const uint160& b)
432 insert(end(), sizeof(b));
433 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
434 return *this;
437 CScript& operator<<(const uint256& b)
439 insert(end(), sizeof(b));
440 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
441 return *this;
444 CScript& operator<<(const CBigNum& b)
446 *this << b.getvch();
447 return *this;
450 CScript& operator<<(const vector<unsigned char>& b)
452 if (b.size() < OP_PUSHDATA1)
454 insert(end(), (unsigned char)b.size());
456 else if (b.size() <= 0xff)
458 insert(end(), OP_PUSHDATA1);
459 insert(end(), (unsigned char)b.size());
461 else if (b.size() <= 0xffff)
463 insert(end(), OP_PUSHDATA2);
464 unsigned short nSize = b.size();
465 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
467 else
469 insert(end(), OP_PUSHDATA4);
470 unsigned int nSize = b.size();
471 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
473 insert(end(), b.begin(), b.end());
474 return *this;
477 CScript& operator<<(const CScript& b)
479 // I'm not sure if this should push the script or concatenate scripts.
480 // If there's ever a use for pushing a script onto a script, delete this member fn
481 assert(("warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate", false));
482 return *this;
486 bool GetOp(iterator& pc, opcodetype& opcodeRet, vector<unsigned char>& vchRet)
488 // Wrapper so it can be called with either iterator or const_iterator
489 const_iterator pc2 = pc;
490 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
491 pc = begin() + (pc2 - begin());
492 return fRet;
495 bool GetOp(iterator& pc, opcodetype& opcodeRet)
497 const_iterator pc2 = pc;
498 bool fRet = GetOp2(pc2, opcodeRet, NULL);
499 pc = begin() + (pc2 - begin());
500 return fRet;
503 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, vector<unsigned char>& vchRet) const
505 return GetOp2(pc, opcodeRet, &vchRet);
508 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
510 return GetOp2(pc, opcodeRet, NULL);
513 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, vector<unsigned char>* pvchRet) const
515 opcodeRet = OP_INVALIDOPCODE;
516 if (pvchRet)
517 pvchRet->clear();
518 if (pc >= end())
519 return false;
521 // Read instruction
522 if (end() - pc < 1)
523 return false;
524 unsigned int opcode = *pc++;
526 // Immediate operand
527 if (opcode <= OP_PUSHDATA4)
529 unsigned int nSize;
530 if (opcode < OP_PUSHDATA1)
532 nSize = opcode;
534 else if (opcode == OP_PUSHDATA1)
536 if (end() - pc < 1)
537 return false;
538 nSize = *pc++;
540 else if (opcode == OP_PUSHDATA2)
542 if (end() - pc < 2)
543 return false;
544 nSize = 0;
545 memcpy(&nSize, &pc[0], 2);
546 pc += 2;
548 else if (opcode == OP_PUSHDATA4)
550 if (end() - pc < 4)
551 return false;
552 memcpy(&nSize, &pc[0], 4);
553 pc += 4;
555 if (end() - pc < nSize)
556 return false;
557 if (pvchRet)
558 pvchRet->assign(pc, pc + nSize);
559 pc += nSize;
562 opcodeRet = (opcodetype)opcode;
563 return true;
567 void FindAndDelete(const CScript& b)
569 if (b.empty())
570 return;
571 iterator pc = begin();
572 opcodetype opcode;
575 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
576 erase(pc, pc + b.size());
578 while (GetOp(pc, opcode));
582 int GetSigOpCount() const
584 int n = 0;
585 const_iterator pc = begin();
586 while (pc < end())
588 opcodetype opcode;
589 if (!GetOp(pc, opcode))
590 break;
591 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
592 n++;
593 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
594 n += 20;
596 return n;
600 bool IsPushOnly() const
602 if (size() > 200)
603 return false;
604 const_iterator pc = begin();
605 while (pc < end())
607 opcodetype opcode;
608 if (!GetOp(pc, opcode))
609 return false;
610 if (opcode > OP_16)
611 return false;
613 return true;
617 uint160 GetBitcoinAddressHash160() const
619 opcodetype opcode;
620 vector<unsigned char> vch;
621 CScript::const_iterator pc = begin();
622 if (!GetOp(pc, opcode, vch) || opcode != OP_DUP) return 0;
623 if (!GetOp(pc, opcode, vch) || opcode != OP_HASH160) return 0;
624 if (!GetOp(pc, opcode, vch) || vch.size() != sizeof(uint160)) return 0;
625 uint160 hash160 = uint160(vch);
626 if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0;
627 if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0;
628 if (pc != end()) return 0;
629 return hash160;
632 string GetBitcoinAddress() const
634 uint160 hash160 = GetBitcoinAddressHash160();
635 if (hash160 == 0)
636 return "";
637 return Hash160ToAddress(hash160);
640 void SetBitcoinAddress(const uint160& hash160)
642 this->clear();
643 *this << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
646 void SetBitcoinAddress(const vector<unsigned char>& vchPubKey)
648 SetBitcoinAddress(Hash160(vchPubKey));
651 bool SetBitcoinAddress(const string& strAddress)
653 this->clear();
654 uint160 hash160;
655 if (!AddressToHash160(strAddress, hash160))
656 return false;
657 SetBitcoinAddress(hash160);
658 return true;
662 void PrintHex() const
664 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
667 string ToString() const
669 string str;
670 opcodetype opcode;
671 vector<unsigned char> vch;
672 const_iterator pc = begin();
673 while (pc < end())
675 if (!str.empty())
676 str += " ";
677 if (!GetOp(pc, opcode, vch))
679 str += "[error]";
680 return str;
682 if (0 <= opcode && opcode <= OP_PUSHDATA4)
683 str += ValueString(vch);
684 else
685 str += GetOpName(opcode);
687 return str;
690 void print() const
692 printf("%s\n", ToString().c_str());
703 uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
704 bool IsStandard(const CScript& scriptPubKey);
705 bool IsMine(const CScript& scriptPubKey);
706 bool ExtractPubKey(const CScript& scriptPubKey, bool fMineOnly, vector<unsigned char>& vchPubKeyRet);
707 bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret);
708 bool SignSignature(const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
709 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);