genesis 1
genesis 2
genesis 3
genesis 4
genesis 5 #include "headers.h"
genesis 6
genesis 7 using namespace std;
genesis 8 using namespace boost;
genesis 9
genesis 10 bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
genesis 11
genesis 12
genesis 13
genesis 14 typedef vector<unsigned char> valtype;
genesis 15 static const valtype vchFalse(0);
genesis 16 static const valtype vchZero(0);
genesis 17 static const valtype vchTrue(1, 1);
genesis 18 static const CBigNum bnZero(0);
genesis 19 static const CBigNum bnOne(1);
genesis 20 static const CBigNum bnFalse(0);
genesis 21 static const CBigNum bnTrue(1);
genesis 22 static const size_t nMaxNumSize = 4;
genesis 23
genesis 24
genesis 25 CBigNum CastToBigNum(const valtype& vch)
genesis 26 {
genesis 27 if (vch.size() > nMaxNumSize)
genesis 28 throw runtime_error("CastToBigNum() : overflow");
genesis 29
genesis 30 return CBigNum(CBigNum(vch).getvch());
genesis 31 }
genesis 32
genesis 33 bool CastToBool(const valtype& vch)
genesis 34 {
genesis 35 for (int i = 0; i < vch.size(); i++)
genesis 36 {
genesis 37 if (vch[i] != 0)
genesis 38 {
genesis 39
genesis 40 if (i == vch.size()-1 && vch[i] == 0x80)
genesis 41 return false;
genesis 42 return true;
genesis 43 }
genesis 44 }
genesis 45 return false;
genesis 46 }
genesis 47
genesis 48 void MakeSameSize(valtype& vch1, valtype& vch2)
genesis 49 {
genesis 50
genesis 51 if (vch1.size() < vch2.size())
genesis 52 vch1.resize(vch2.size(), 0);
genesis 53 if (vch2.size() < vch1.size())
genesis 54 vch2.resize(vch1.size(), 0);
genesis 55 }
genesis 56
genesis 57
genesis 58
genesis 59
genesis 60
genesis 61
genesis 62
genesis 63 #define stacktop(i) (stack.at(stack.size()+(i)))
genesis 64 #define altstacktop(i) (altstack.at(altstack.size()+(i)))
genesis 65 static inline void popstack(vector<valtype>& stack)
genesis 66 {
genesis 67 if (stack.empty())
genesis 68 throw runtime_error("popstack() : stack empty");
genesis 69 stack.pop_back();
genesis 70 }
genesis 71
genesis 72
genesis 73 bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType)
genesis 74 {
genesis 75 CAutoBN_CTX pctx;
genesis 76 CScript::const_iterator pc = script.begin();
genesis 77 CScript::const_iterator pend = script.end();
genesis 78 CScript::const_iterator pbegincodehash = script.begin();
genesis 79 opcodetype opcode;
genesis 80 valtype vchPushValue;
genesis 81 vector<bool> vfExec;
genesis 82 vector<valtype> altstack;
genesis 83 if (script.size() > 10000)
genesis 84 return false;
genesis 85 int nOpCount = 0;
genesis 86
genesis 87
genesis 88 try
genesis 89 {
genesis 90 while (pc < pend)
genesis 91 {
genesis 92 bool fExec = !count(vfExec.begin(), vfExec.end(), false);
genesis 93
genesis 94
genesis 95
genesis 96
genesis 97 if (!script.GetOp(pc, opcode, vchPushValue))
genesis 98 return false;
genesis 99 if (vchPushValue.size() > 520)
genesis 100 return false;
genesis 101 if (opcode > OP_16 && ++nOpCount > 201)
genesis 102 return false;
genesis 103
genesis 104 if (opcode == OP_CAT ||
genesis 105 opcode == OP_SUBSTR ||
genesis 106 opcode == OP_LEFT ||
genesis 107 opcode == OP_RIGHT ||
genesis 108 opcode == OP_INVERT ||
genesis 109 opcode == OP_AND ||
genesis 110 opcode == OP_OR ||
genesis 111 opcode == OP_XOR ||
genesis 112 opcode == OP_2MUL ||
genesis 113 opcode == OP_2DIV ||
genesis 114 opcode == OP_MUL ||
genesis 115 opcode == OP_DIV ||
genesis 116 opcode == OP_MOD ||
genesis 117 opcode == OP_LSHIFT ||
genesis 118 opcode == OP_RSHIFT)
genesis 119 return false;
genesis 120
genesis 121 if (fExec && 0 <= opcode && opcode <= OP_PUSHDATA4)
genesis 122 stack.push_back(vchPushValue);
genesis 123 else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF))
genesis 124 switch (opcode)
genesis 125 {
genesis 126
genesis 127
genesis 128
genesis 129 case OP_1NEGATE:
genesis 130 case OP_1:
genesis 131 case OP_2:
genesis 132 case OP_3:
genesis 133 case OP_4:
genesis 134 case OP_5:
genesis 135 case OP_6:
genesis 136 case OP_7:
genesis 137 case OP_8:
genesis 138 case OP_9:
genesis 139 case OP_10:
genesis 140 case OP_11:
genesis 141 case OP_12:
genesis 142 case OP_13:
genesis 143 case OP_14:
genesis 144 case OP_15:
genesis 145 case OP_16:
genesis 146 {
genesis 147
genesis 148 CBigNum bn((int)opcode - (int)(OP_1 - 1));
genesis 149 stack.push_back(bn.getvch());
genesis 150 }
genesis 151 break;
genesis 152
genesis 153
genesis 154
genesis 155
genesis 156
genesis 157 case OP_NOP:
genesis 158 case OP_NOP1: case OP_NOP2: case OP_NOP3: case OP_NOP4: case OP_NOP5:
genesis 159 case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10:
genesis 160 break;
genesis 161
genesis 162 case OP_IF:
genesis 163 case OP_NOTIF:
genesis 164 {
genesis 165
genesis 166 bool fValue = false;
genesis 167 if (fExec)
genesis 168 {
genesis 169 if (stack.size() < 1)
genesis 170 return false;
genesis 171 valtype& vch = stacktop(-1);
genesis 172 fValue = CastToBool(vch);
genesis 173 if (opcode == OP_NOTIF)
genesis 174 fValue = !fValue;
genesis 175 popstack(stack);
genesis 176 }
genesis 177 vfExec.push_back(fValue);
genesis 178 }
genesis 179 break;
genesis 180
genesis 181 case OP_ELSE:
genesis 182 {
genesis 183 if (vfExec.empty())
genesis 184 return false;
genesis 185 vfExec.back() = !vfExec.back();
genesis 186 }
genesis 187 break;
genesis 188
genesis 189 case OP_ENDIF:
genesis 190 {
genesis 191 if (vfExec.empty())
genesis 192 return false;
genesis 193 vfExec.pop_back();
genesis 194 }
genesis 195 break;
genesis 196
genesis 197 case OP_VERIFY:
genesis 198 {
genesis 199
genesis 200
genesis 201 if (stack.size() < 1)
genesis 202 return false;
genesis 203 bool fValue = CastToBool(stacktop(-1));
genesis 204 if (fValue)
genesis 205 popstack(stack);
genesis 206 else
genesis 207 return false;
genesis 208 }
genesis 209 break;
genesis 210
genesis 211 case OP_RETURN:
genesis 212 {
genesis 213 return false;
genesis 214 }
genesis 215 break;
genesis 216
genesis 217
genesis 218
genesis 219
genesis 220
genesis 221 case OP_TOALTSTACK:
genesis 222 {
genesis 223 if (stack.size() < 1)
genesis 224 return false;
genesis 225 altstack.push_back(stacktop(-1));
genesis 226 popstack(stack);
genesis 227 }
genesis 228 break;
genesis 229
genesis 230 case OP_FROMALTSTACK:
genesis 231 {
genesis 232 if (altstack.size() < 1)
genesis 233 return false;
genesis 234 stack.push_back(altstacktop(-1));
genesis 235 popstack(altstack);
genesis 236 }
genesis 237 break;
genesis 238
genesis 239 case OP_2DROP:
genesis 240 {
genesis 241
genesis 242 if (stack.size() < 2)
genesis 243 return false;
genesis 244 popstack(stack);
genesis 245 popstack(stack);
genesis 246 }
genesis 247 break;
genesis 248
genesis 249 case OP_2DUP:
genesis 250 {
genesis 251
genesis 252 if (stack.size() < 2)
genesis 253 return false;
genesis 254 valtype vch1 = stacktop(-2);
genesis 255 valtype vch2 = stacktop(-1);
genesis 256 stack.push_back(vch1);
genesis 257 stack.push_back(vch2);
genesis 258 }
genesis 259 break;
genesis 260
genesis 261 case OP_3DUP:
genesis 262 {
genesis 263
genesis 264 if (stack.size() < 3)
genesis 265 return false;
genesis 266 valtype vch1 = stacktop(-3);
genesis 267 valtype vch2 = stacktop(-2);
genesis 268 valtype vch3 = stacktop(-1);
genesis 269 stack.push_back(vch1);
genesis 270 stack.push_back(vch2);
genesis 271 stack.push_back(vch3);
genesis 272 }
genesis 273 break;
genesis 274
genesis 275 case OP_2OVER:
genesis 276 {
genesis 277
genesis 278 if (stack.size() < 4)
genesis 279 return false;
genesis 280 valtype vch1 = stacktop(-4);
genesis 281 valtype vch2 = stacktop(-3);
genesis 282 stack.push_back(vch1);
genesis 283 stack.push_back(vch2);
genesis 284 }
genesis 285 break;
genesis 286
genesis 287 case OP_2ROT:
genesis 288 {
genesis 289
genesis 290 if (stack.size() < 6)
genesis 291 return false;
genesis 292 valtype vch1 = stacktop(-6);
genesis 293 valtype vch2 = stacktop(-5);
genesis 294 stack.erase(stack.end()-6, stack.end()-4);
genesis 295 stack.push_back(vch1);
genesis 296 stack.push_back(vch2);
genesis 297 }
genesis 298 break;
genesis 299
genesis 300 case OP_2SWAP:
genesis 301 {
genesis 302
genesis 303 if (stack.size() < 4)
genesis 304 return false;
genesis 305 swap(stacktop(-4), stacktop(-2));
genesis 306 swap(stacktop(-3), stacktop(-1));
genesis 307 }
genesis 308 break;
genesis 309
genesis 310 case OP_IFDUP:
genesis 311 {
genesis 312
genesis 313 if (stack.size() < 1)
genesis 314 return false;
genesis 315 valtype vch = stacktop(-1);
genesis 316 if (CastToBool(vch))
genesis 317 stack.push_back(vch);
genesis 318 }
genesis 319 break;
genesis 320
genesis 321 case OP_DEPTH:
genesis 322 {
genesis 323
genesis 324 CBigNum bn(stack.size());
genesis 325 stack.push_back(bn.getvch());
genesis 326 }
genesis 327 break;
genesis 328
genesis 329 case OP_DROP:
genesis 330 {
genesis 331
genesis 332 if (stack.size() < 1)
genesis 333 return false;
genesis 334 popstack(stack);
genesis 335 }
genesis 336 break;
genesis 337
genesis 338 case OP_DUP:
genesis 339 {
genesis 340
genesis 341 if (stack.size() < 1)
genesis 342 return false;
genesis 343 valtype vch = stacktop(-1);
genesis 344 stack.push_back(vch);
genesis 345 }
genesis 346 break;
genesis 347
genesis 348 case OP_NIP:
genesis 349 {
genesis 350
genesis 351 if (stack.size() < 2)
genesis 352 return false;
genesis 353 stack.erase(stack.end() - 2);
genesis 354 }
genesis 355 break;
genesis 356
genesis 357 case OP_OVER:
genesis 358 {
genesis 359
genesis 360 if (stack.size() < 2)
genesis 361 return false;
genesis 362 valtype vch = stacktop(-2);
genesis 363 stack.push_back(vch);
genesis 364 }
genesis 365 break;
genesis 366
genesis 367 case OP_PICK:
genesis 368 case OP_ROLL:
genesis 369 {
genesis 370
genesis 371
genesis 372 if (stack.size() < 2)
genesis 373 return false;
genesis 374 int n = CastToBigNum(stacktop(-1)).getint();
genesis 375 popstack(stack);
genesis 376 if (n < 0 || n >= stack.size())
genesis 377 return false;
genesis 378 valtype vch = stacktop(-n-1);
genesis 379 if (opcode == OP_ROLL)
genesis 380 stack.erase(stack.end()-n-1);
genesis 381 stack.push_back(vch);
genesis 382 }
genesis 383 break;
genesis 384
genesis 385 case OP_ROT:
genesis 386 {
genesis 387
genesis 388
genesis 389
genesis 390 if (stack.size() < 3)
genesis 391 return false;
genesis 392 swap(stacktop(-3), stacktop(-2));
genesis 393 swap(stacktop(-2), stacktop(-1));
genesis 394 }
genesis 395 break;
genesis 396
genesis 397 case OP_SWAP:
genesis 398 {
genesis 399
genesis 400 if (stack.size() < 2)
genesis 401 return false;
genesis 402 swap(stacktop(-2), stacktop(-1));
genesis 403 }
genesis 404 break;
genesis 405
genesis 406 case OP_TUCK:
genesis 407 {
genesis 408
genesis 409 if (stack.size() < 2)
genesis 410 return false;
genesis 411 valtype vch = stacktop(-1);
genesis 412 stack.insert(stack.end()-2, vch);
genesis 413 }
genesis 414 break;
genesis 415
genesis 416
genesis 417
genesis 418
genesis 419
genesis 420 case OP_CAT:
genesis 421 {
genesis 422
genesis 423 if (stack.size() < 2)
genesis 424 return false;
genesis 425 valtype& vch1 = stacktop(-2);
genesis 426 valtype& vch2 = stacktop(-1);
genesis 427 vch1.insert(vch1.end(), vch2.begin(), vch2.end());
genesis 428 popstack(stack);
genesis 429 if (stacktop(-1).size() > 520)
genesis 430 return false;
genesis 431 }
genesis 432 break;
genesis 433
genesis 434 case OP_SUBSTR:
genesis 435 {
genesis 436
genesis 437 if (stack.size() < 3)
genesis 438 return false;
genesis 439 valtype& vch = stacktop(-3);
genesis 440 int nBegin = CastToBigNum(stacktop(-2)).getint();
genesis 441 int nEnd = nBegin + CastToBigNum(stacktop(-1)).getint();
genesis 442 if (nBegin < 0 || nEnd < nBegin)
genesis 443 return false;
genesis 444 if (nBegin > vch.size())
genesis 445 nBegin = vch.size();
genesis 446 if (nEnd > vch.size())
genesis 447 nEnd = vch.size();
genesis 448 vch.erase(vch.begin() + nEnd, vch.end());
genesis 449 vch.erase(vch.begin(), vch.begin() + nBegin);
genesis 450 popstack(stack);
genesis 451 popstack(stack);
genesis 452 }
genesis 453 break;
genesis 454
genesis 455 case OP_LEFT:
genesis 456 case OP_RIGHT:
genesis 457 {
genesis 458
genesis 459 if (stack.size() < 2)
genesis 460 return false;
genesis 461 valtype& vch = stacktop(-2);
genesis 462 int nSize = CastToBigNum(stacktop(-1)).getint();
genesis 463 if (nSize < 0)
genesis 464 return false;
genesis 465 if (nSize > vch.size())
genesis 466 nSize = vch.size();
genesis 467 if (opcode == OP_LEFT)
genesis 468 vch.erase(vch.begin() + nSize, vch.end());
genesis 469 else
genesis 470 vch.erase(vch.begin(), vch.end() - nSize);
genesis 471 popstack(stack);
genesis 472 }
genesis 473 break;
genesis 474
genesis 475 case OP_SIZE:
genesis 476 {
genesis 477
genesis 478 if (stack.size() < 1)
genesis 479 return false;
genesis 480 CBigNum bn(stacktop(-1).size());
genesis 481 stack.push_back(bn.getvch());
genesis 482 }
genesis 483 break;
genesis 484
genesis 485
genesis 486
genesis 487
genesis 488
genesis 489 case OP_INVERT:
genesis 490 {
genesis 491
genesis 492 if (stack.size() < 1)
genesis 493 return false;
genesis 494 valtype& vch = stacktop(-1);
genesis 495 for (int i = 0; i < vch.size(); i++)
genesis 496 vch[i] = ~vch[i];
genesis 497 }
genesis 498 break;
genesis 499
genesis 500 case OP_AND:
genesis 501 case OP_OR:
genesis 502 case OP_XOR:
genesis 503 {
genesis 504
genesis 505 if (stack.size() < 2)
genesis 506 return false;
genesis 507 valtype& vch1 = stacktop(-2);
genesis 508 valtype& vch2 = stacktop(-1);
genesis 509 MakeSameSize(vch1, vch2);
genesis 510 if (opcode == OP_AND)
genesis 511 {
genesis 512 for (int i = 0; i < vch1.size(); i++)
genesis 513 vch1[i] &= vch2[i];
genesis 514 }
genesis 515 else if (opcode == OP_OR)
genesis 516 {
genesis 517 for (int i = 0; i < vch1.size(); i++)
genesis 518 vch1[i] |= vch2[i];
genesis 519 }
genesis 520 else if (opcode == OP_XOR)
genesis 521 {
genesis 522 for (int i = 0; i < vch1.size(); i++)
genesis 523 vch1[i] ^= vch2[i];
genesis 524 }
genesis 525 popstack(stack);
genesis 526 }
genesis 527 break;
genesis 528
genesis 529 case OP_EQUAL:
genesis 530 case OP_EQUALVERIFY:
genesis 531
genesis 532 {
genesis 533
genesis 534 if (stack.size() < 2)
genesis 535 return false;
genesis 536 valtype& vch1 = stacktop(-2);
genesis 537 valtype& vch2 = stacktop(-1);
genesis 538 bool fEqual = (vch1 == vch2);
genesis 539
genesis 540
genesis 541
genesis 542
genesis 543
genesis 544 popstack(stack);
genesis 545 popstack(stack);
genesis 546 stack.push_back(fEqual ? vchTrue : vchFalse);
genesis 547 if (opcode == OP_EQUALVERIFY)
genesis 548 {
genesis 549 if (fEqual)
genesis 550 popstack(stack);
genesis 551 else
genesis 552 return false;
genesis 553 }
genesis 554 }
genesis 555 break;
genesis 556
genesis 557
genesis 558
genesis 559
genesis 560
genesis 561 case OP_1ADD:
genesis 562 case OP_1SUB:
genesis 563 case OP_2MUL:
genesis 564 case OP_2DIV:
genesis 565 case OP_NEGATE:
genesis 566 case OP_ABS:
genesis 567 case OP_NOT:
genesis 568 case OP_0NOTEQUAL:
genesis 569 {
genesis 570
genesis 571 if (stack.size() < 1)
genesis 572 return false;
genesis 573 CBigNum bn = CastToBigNum(stacktop(-1));
genesis 574 switch (opcode)
genesis 575 {
genesis 576 case OP_1ADD: bn += bnOne; break;
genesis 577 case OP_1SUB: bn -= bnOne; break;
genesis 578 case OP_2MUL: bn <<= 1; break;
genesis 579 case OP_2DIV: bn >>= 1; break;
genesis 580 case OP_NEGATE: bn = -bn; break;
genesis 581 case OP_ABS: if (bn < bnZero) bn = -bn; break;
genesis 582 case OP_NOT: bn = (bn == bnZero); break;
genesis 583 case OP_0NOTEQUAL: bn = (bn != bnZero); break;
genesis 584 default: assert(!"invalid opcode"); break;
genesis 585 }
genesis 586 popstack(stack);
genesis 587 stack.push_back(bn.getvch());
genesis 588 }
genesis 589 break;
genesis 590
genesis 591 case OP_ADD:
genesis 592 case OP_SUB:
genesis 593 case OP_MUL:
genesis 594 case OP_DIV:
genesis 595 case OP_MOD:
genesis 596 case OP_LSHIFT:
genesis 597 case OP_RSHIFT:
genesis 598 case OP_BOOLAND:
genesis 599 case OP_BOOLOR:
genesis 600 case OP_NUMEQUAL:
genesis 601 case OP_NUMEQUALVERIFY:
genesis 602 case OP_NUMNOTEQUAL:
genesis 603 case OP_LESSTHAN:
genesis 604 case OP_GREATERTHAN:
genesis 605 case OP_LESSTHANOREQUAL:
genesis 606 case OP_GREATERTHANOREQUAL:
genesis 607 case OP_MIN:
genesis 608 case OP_MAX:
genesis 609 {
genesis 610
genesis 611 if (stack.size() < 2)
genesis 612 return false;
genesis 613 CBigNum bn1 = CastToBigNum(stacktop(-2));
genesis 614 CBigNum bn2 = CastToBigNum(stacktop(-1));
genesis 615 CBigNum bn;
genesis 616 switch (opcode)
genesis 617 {
genesis 618 case OP_ADD:
genesis 619 bn = bn1 + bn2;
genesis 620 break;
genesis 621
genesis 622 case OP_SUB:
genesis 623 bn = bn1 - bn2;
genesis 624 break;
genesis 625
genesis 626 case OP_MUL:
genesis 627 if (!BN_mul(&bn, &bn1, &bn2, pctx))
genesis 628 return false;
genesis 629 break;
genesis 630
genesis 631 case OP_DIV:
genesis 632 if (!BN_div(&bn, NULL, &bn1, &bn2, pctx))
genesis 633 return false;
genesis 634 break;
genesis 635
genesis 636 case OP_MOD:
genesis 637 if (!BN_mod(&bn, &bn1, &bn2, pctx))
genesis 638 return false;
genesis 639 break;
genesis 640
genesis 641 case OP_LSHIFT:
genesis 642 if (bn2 < bnZero || bn2 > CBigNum(2048))
genesis 643 return false;
genesis 644 bn = bn1 << bn2.getulong();
genesis 645 break;
genesis 646
genesis 647 case OP_RSHIFT:
genesis 648 if (bn2 < bnZero || bn2 > CBigNum(2048))
genesis 649 return false;
genesis 650 bn = bn1 >> bn2.getulong();
genesis 651 break;
genesis 652
genesis 653 case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break;
genesis 654 case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break;
genesis 655 case OP_NUMEQUAL: bn = (bn1 == bn2); break;
genesis 656 case OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break;
genesis 657 case OP_NUMNOTEQUAL: bn = (bn1 != bn2); break;
genesis 658 case OP_LESSTHAN: bn = (bn1 < bn2); break;
genesis 659 case OP_GREATERTHAN: bn = (bn1 > bn2); break;
genesis 660 case OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break;
genesis 661 case OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break;
genesis 662 case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2); break;
genesis 663 case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2); break;
genesis 664 default: assert(!"invalid opcode"); break;
genesis 665 }
genesis 666 popstack(stack);
genesis 667 popstack(stack);
genesis 668 stack.push_back(bn.getvch());
genesis 669
genesis 670 if (opcode == OP_NUMEQUALVERIFY)
genesis 671 {
genesis 672 if (CastToBool(stacktop(-1)))
genesis 673 popstack(stack);
genesis 674 else
genesis 675 return false;
genesis 676 }
genesis 677 }
genesis 678 break;
genesis 679
genesis 680 case OP_WITHIN:
genesis 681 {
genesis 682
genesis 683 if (stack.size() < 3)
genesis 684 return false;
genesis 685 CBigNum bn1 = CastToBigNum(stacktop(-3));
genesis 686 CBigNum bn2 = CastToBigNum(stacktop(-2));
genesis 687 CBigNum bn3 = CastToBigNum(stacktop(-1));
genesis 688 bool fValue = (bn2 <= bn1 && bn1 < bn3);
genesis 689 popstack(stack);
genesis 690 popstack(stack);
genesis 691 popstack(stack);
genesis 692 stack.push_back(fValue ? vchTrue : vchFalse);
genesis 693 }
genesis 694 break;
genesis 695
genesis 696
genesis 697
genesis 698
genesis 699
genesis 700 case OP_RIPEMD160:
genesis 701 case OP_SHA1:
genesis 702 case OP_SHA256:
genesis 703 case OP_HASH160:
genesis 704 case OP_HASH256:
genesis 705 {
genesis 706
genesis 707 if (stack.size() < 1)
genesis 708 return false;
genesis 709 valtype& vch = stacktop(-1);
genesis 710 valtype vchHash((opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160) ? 20 : 32);
genesis 711 if (opcode == OP_RIPEMD160)
genesis 712 RIPEMD160(&vch[0], vch.size(), &vchHash[0]);
genesis 713 else if (opcode == OP_SHA1)
genesis 714 SHA1(&vch[0], vch.size(), &vchHash[0]);
genesis 715 else if (opcode == OP_SHA256)
genesis 716 SHA256(&vch[0], vch.size(), &vchHash[0]);
genesis 717 else if (opcode == OP_HASH160)
genesis 718 {
genesis 719 uint160 hash160 = Hash160(vch);
genesis 720 memcpy(&vchHash[0], &hash160, sizeof(hash160));
genesis 721 }
genesis 722 else if (opcode == OP_HASH256)
genesis 723 {
genesis 724 uint256 hash = Hash(vch.begin(), vch.end());
genesis 725 memcpy(&vchHash[0], &hash, sizeof(hash));
genesis 726 }
genesis 727 popstack(stack);
genesis 728 stack.push_back(vchHash);
genesis 729 }
genesis 730 break;
genesis 731
genesis 732 case OP_CODESEPARATOR:
genesis 733 {
genesis 734
genesis 735 pbegincodehash = pc;
genesis 736 }
genesis 737 break;
genesis 738
genesis 739 case OP_CHECKSIG:
genesis 740 case OP_CHECKSIGVERIFY:
genesis 741 {
genesis 742
genesis 743 if (stack.size() < 2)
genesis 744 return false;
genesis 745
genesis 746 valtype& vchSig = stacktop(-2);
genesis 747 valtype& vchPubKey = stacktop(-1);
genesis 748
genesis 749
genesis 750
genesis 751
genesis 752
genesis 753
genesis 754 CScript scriptCode(pbegincodehash, pend);
genesis 755
genesis 756
genesis 757 scriptCode.FindAndDelete(CScript(vchSig));
genesis 758
genesis 759 bool fSuccess = CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType);
genesis 760
genesis 761 popstack(stack);
genesis 762 popstack(stack);
genesis 763 stack.push_back(fSuccess ? vchTrue : vchFalse);
genesis 764 if (opcode == OP_CHECKSIGVERIFY)
genesis 765 {
genesis 766 if (fSuccess)
genesis 767 popstack(stack);
genesis 768 else
genesis 769 return false;
genesis 770 }
genesis 771 }
genesis 772 break;
genesis 773
genesis 774 case OP_CHECKMULTISIG:
genesis 775 case OP_CHECKMULTISIGVERIFY:
genesis 776 {
genesis 777
genesis 778
genesis 779 int i = 1;
genesis 780 if (stack.size() < i)
genesis 781 return false;
genesis 782
genesis 783 int nKeysCount = CastToBigNum(stacktop(-i)).getint();
genesis 784 if (nKeysCount < 0 || nKeysCount > 20)
genesis 785 return false;
genesis 786 nOpCount += nKeysCount;
genesis 787 if (nOpCount > 201)
genesis 788 return false;
genesis 789 int ikey = ++i;
genesis 790 i += nKeysCount;
genesis 791 if (stack.size() < i)
genesis 792 return false;
genesis 793
genesis 794 int nSigsCount = CastToBigNum(stacktop(-i)).getint();
genesis 795 if (nSigsCount < 0 || nSigsCount > nKeysCount)
genesis 796 return false;
genesis 797 int isig = ++i;
genesis 798 i += nSigsCount;
genesis 799 if (stack.size() < i)
genesis 800 return false;
genesis 801
genesis 802
genesis 803 CScript scriptCode(pbegincodehash, pend);
genesis 804
genesis 805
genesis 806 for (int k = 0; k < nSigsCount; k++)
genesis 807 {
genesis 808 valtype& vchSig = stacktop(-isig-k);
genesis 809 scriptCode.FindAndDelete(CScript(vchSig));
genesis 810 }
genesis 811
genesis 812 bool fSuccess = true;
genesis 813 while (fSuccess && nSigsCount > 0)
genesis 814 {
genesis 815 valtype& vchSig = stacktop(-isig);
genesis 816 valtype& vchPubKey = stacktop(-ikey);
genesis 817
genesis 818
genesis 819 if (CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType))
genesis 820 {
genesis 821 isig++;
genesis 822 nSigsCount--;
genesis 823 }
genesis 824 ikey++;
genesis 825 nKeysCount--;
genesis 826
genesis 827
genesis 828
genesis 829 if (nSigsCount > nKeysCount)
genesis 830 fSuccess = false;
genesis 831 }
genesis 832
genesis 833 while (i-- > 0)
genesis 834 popstack(stack);
genesis 835 stack.push_back(fSuccess ? vchTrue : vchFalse);
genesis 836
genesis 837 if (opcode == OP_CHECKMULTISIGVERIFY)
genesis 838 {
genesis 839 if (fSuccess)
genesis 840 popstack(stack);
genesis 841 else
genesis 842 return false;
genesis 843 }
genesis 844 }
genesis 845 break;
genesis 846
genesis 847 default:
genesis 848 return false;
genesis 849 }
genesis 850
genesis 851
genesis 852 if (stack.size() + altstack.size() > 1000)
genesis 853 return false;
genesis 854 }
genesis 855 }
genesis 856 catch (...)
genesis 857 {
genesis 858 return false;
genesis 859 }
genesis 860
genesis 861
genesis 862 if (!vfExec.empty())
genesis 863 return false;
genesis 864
genesis 865 return true;
genesis 866 }
genesis 867
genesis 868
genesis 869
genesis 870
genesis 871
genesis 872
genesis 873
genesis 874
genesis 875
genesis 876 uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
genesis 877 {
genesis 878 if (nIn >= txTo.vin.size())
genesis 879 {
polarbeard_better... 880 printf(SERR SMEM "signature hash for input number %d out of range\n", nIn);
genesis 881 return 1;
genesis 882 }
genesis 883 CTransaction txTmp(txTo);
genesis 884
genesis 885
genesis 886
genesis 887 scriptCode.FindAndDelete(CScript(OP_CODESEPARATOR));
genesis 888
genesis 889
genesis 890 for (int i = 0; i < txTmp.vin.size(); i++)
genesis 891 txTmp.vin[i].scriptSig = CScript();
genesis 892 txTmp.vin[nIn].scriptSig = scriptCode;
genesis 893
genesis 894
genesis 895 if ((nHashType & 0x1f) == SIGHASH_NONE)
genesis 896 {
genesis 897
genesis 898 txTmp.vout.clear();
genesis 899
genesis 900
genesis 901 for (int i = 0; i < txTmp.vin.size(); i++)
genesis 902 if (i != nIn)
genesis 903 txTmp.vin[i].nSequence = 0;
genesis 904 }
genesis 905 else if ((nHashType & 0x1f) == SIGHASH_SINGLE)
genesis 906 {
genesis 907
genesis 908 unsigned int nOut = nIn;
genesis 909 if (nOut >= txTmp.vout.size())
genesis 910 {
polarbeard_better... 911 printf(SERR SMEM "signature hash for output number %d out of range\n", nOut);
genesis 912 return 1;
genesis 913 }
genesis 914 txTmp.vout.resize(nOut+1);
genesis 915 for (int i = 0; i < nOut; i++)
genesis 916 txTmp.vout[i].SetNull();
genesis 917
genesis 918
genesis 919 for (int i = 0; i < txTmp.vin.size(); i++)
genesis 920 if (i != nIn)
genesis 921 txTmp.vin[i].nSequence = 0;
genesis 922 }
genesis 923
genesis 924
genesis 925 if (nHashType & SIGHASH_ANYONECANPAY)
genesis 926 {
genesis 927 txTmp.vin[0] = txTmp.vin[nIn];
genesis 928 txTmp.vin.resize(1);
genesis 929 }
genesis 930
genesis 931
genesis 932 CDataStream ss(SER_GETHASH);
genesis 933 ss.reserve(10000);
genesis 934 ss << txTmp << nHashType;
genesis 935 return Hash(ss.begin(), ss.end());
genesis 936 }
genesis 937
genesis 938
genesis 939 bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode,
genesis 940 const CTransaction& txTo, unsigned int nIn, int nHashType)
genesis 941 {
genesis 942 CKey key;
genesis 943 if (!key.SetPubKey(vchPubKey))
genesis 944 return false;
genesis 945
genesis 946
genesis 947 if (vchSig.empty())
genesis 948 return false;
genesis 949 if (nHashType == 0)
genesis 950 nHashType = vchSig.back();
genesis 951 else if (nHashType != vchSig.back())
genesis 952 return false;
genesis 953 vchSig.pop_back();
genesis 954
genesis 955 return key.Verify(SignatureHash(scriptCode, txTo, nIn, nHashType), vchSig);
genesis 956 }
genesis 957
genesis 958
genesis 959
genesis 960
genesis 961
genesis 962
genesis 963
genesis 964
genesis 965
genesis 966
genesis 967 bool Solver(const CScript& scriptPubKey, vector<pair<opcodetype, valtype> >& vSolutionRet)
genesis 968 {
genesis 969
genesis 970 static vector<CScript> vTemplates;
genesis 971 if (vTemplates.empty())
genesis 972 {
genesis 973
genesis 974 vTemplates.push_back(CScript() << OP_PUBKEY << OP_CHECKSIG);
genesis 975
genesis 976
genesis 977 vTemplates.push_back(CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG);
genesis 978 }
genesis 979
genesis 980
genesis 981 const CScript& script1 = scriptPubKey;
genesis 982 BOOST_FOREACH(const CScript& script2, vTemplates)
genesis 983 {
genesis 984 vSolutionRet.clear();
genesis 985 opcodetype opcode1, opcode2;
genesis 986 vector<unsigned char> vch1, vch2;
genesis 987
genesis 988
genesis 989 CScript::const_iterator pc1 = script1.begin();
genesis 990 CScript::const_iterator pc2 = script2.begin();
genesis 991 loop
genesis 992 {
genesis 993 if (pc1 == script1.end() && pc2 == script2.end())
genesis 994 {
genesis 995
genesis 996 reverse(vSolutionRet.begin(), vSolutionRet.end());
genesis 997 return true;
genesis 998 }
genesis 999 if (!script1.GetOp(pc1, opcode1, vch1))
genesis 1000 break;
genesis 1001 if (!script2.GetOp(pc2, opcode2, vch2))
genesis 1002 break;
genesis 1003 if (opcode2 == OP_PUBKEY)
genesis 1004 {
genesis 1005 if (vch1.size() < 33 || vch1.size() > 120)
genesis 1006 break;
genesis 1007 vSolutionRet.push_back(make_pair(opcode2, vch1));
genesis 1008 }
genesis 1009 else if (opcode2 == OP_PUBKEYHASH)
genesis 1010 {
genesis 1011 if (vch1.size() != sizeof(uint160))
genesis 1012 break;
genesis 1013 vSolutionRet.push_back(make_pair(opcode2, vch1));
genesis 1014 }
genesis 1015 else if (opcode1 != opcode2 || vch1 != vch2)
genesis 1016 {
genesis 1017 break;
genesis 1018 }
genesis 1019 }
genesis 1020 }
genesis 1021
genesis 1022 vSolutionRet.clear();
genesis 1023 return false;
genesis 1024 }
genesis 1025
genesis 1026
genesis 1027 bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash, int nHashType, CScript& scriptSigRet)
genesis 1028 {
genesis 1029 scriptSigRet.clear();
genesis 1030
genesis 1031 vector<pair<opcodetype, valtype> > vSolution;
genesis 1032 if (!Solver(scriptPubKey, vSolution))
genesis 1033 return false;
genesis 1034
genesis 1035
genesis 1036 BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution)
genesis 1037 {
genesis 1038 if (item.first == OP_PUBKEY)
genesis 1039 {
genesis 1040
genesis 1041 const valtype& vchPubKey = item.second;
genesis 1042 CKey key;
genesis 1043 if (!keystore.GetKey(Hash160(vchPubKey), key))
genesis 1044 return false;
genesis 1045 if (key.GetPubKey() != vchPubKey)
genesis 1046 return false;
genesis 1047 if (hash != 0)
genesis 1048 {
genesis 1049 vector<unsigned char> vchSig;
genesis 1050 if (!key.Sign(hash, vchSig))
genesis 1051 return false;
genesis 1052 vchSig.push_back((unsigned char)nHashType);
genesis 1053 scriptSigRet << vchSig;
genesis 1054 }
genesis 1055 }
genesis 1056 else if (item.first == OP_PUBKEYHASH)
genesis 1057 {
genesis 1058
genesis 1059 CKey key;
genesis 1060 if (!keystore.GetKey(uint160(item.second), key))
genesis 1061 return false;
genesis 1062 if (hash != 0)
genesis 1063 {
genesis 1064 vector<unsigned char> vchSig;
genesis 1065 if (!key.Sign(hash, vchSig))
genesis 1066 return false;
genesis 1067 vchSig.push_back((unsigned char)nHashType);
genesis 1068 scriptSigRet << vchSig << key.GetPubKey();
genesis 1069 }
genesis 1070 }
genesis 1071 else
genesis 1072 {
genesis 1073 return false;
genesis 1074 }
genesis 1075 }
genesis 1076
genesis 1077 return true;
genesis 1078 }
genesis 1079
genesis 1080
genesis 1081 bool IsStandard(const CScript& scriptPubKey)
genesis 1082 {
genesis 1083 vector<pair<opcodetype, valtype> > vSolution;
genesis 1084 return Solver(scriptPubKey, vSolution);
genesis 1085 }
genesis 1086
genesis 1087
genesis 1088 bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
genesis 1089 {
genesis 1090 vector<pair<opcodetype, valtype> > vSolution;
genesis 1091 if (!Solver(scriptPubKey, vSolution))
genesis 1092 return false;
genesis 1093
genesis 1094
genesis 1095 BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution)
genesis 1096 {
genesis 1097 if (item.first == OP_PUBKEY)
genesis 1098 {
genesis 1099 const valtype& vchPubKey = item.second;
genesis 1100 vector<unsigned char> vchPubKeyFound;
genesis 1101 if (!keystore.GetPubKey(Hash160(vchPubKey), vchPubKeyFound))
genesis 1102 return false;
genesis 1103 if (vchPubKeyFound != vchPubKey)
genesis 1104 return false;
genesis 1105 }
genesis 1106 else if (item.first == OP_PUBKEYHASH)
genesis 1107 {
genesis 1108 if (!keystore.HaveKey(uint160(item.second)))
genesis 1109 return false;
genesis 1110 }
genesis 1111 else
genesis 1112 {
genesis 1113 return false;
genesis 1114 }
genesis 1115 }
genesis 1116
genesis 1117 return true;
genesis 1118 }
genesis 1119
genesis 1120 bool static ExtractAddressInner(const CScript& scriptPubKey, const CKeyStore* keystore, CBitcoinAddress& addressRet)
genesis 1121 {
genesis 1122 vector<pair<opcodetype, valtype> > vSolution;
genesis 1123 if (!Solver(scriptPubKey, vSolution))
genesis 1124 return false;
genesis 1125
genesis 1126 BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution)
genesis 1127 {
genesis 1128 if (item.first == OP_PUBKEY)
genesis 1129 addressRet.SetPubKey(item.second);
genesis 1130 else if (item.first == OP_PUBKEYHASH)
genesis 1131 addressRet.SetHash160((uint160)item.second);
genesis 1132 if (keystore == NULL || keystore->HaveKey(addressRet))
genesis 1133 return true;
genesis 1134 }
genesis 1135
genesis 1136 return false;
genesis 1137 }
genesis 1138
genesis 1139
genesis 1140 bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBitcoinAddress& addressRet)
genesis 1141 {
genesis 1142 if (keystore)
genesis 1143 return ExtractAddressInner(scriptPubKey, keystore, addressRet);
genesis 1144 else
genesis 1145 return ExtractAddressInner(scriptPubKey, NULL, addressRet);
genesis 1146 return false;
genesis 1147 }
genesis 1148
genesis 1149
genesis 1150 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, int nHashType)
genesis 1151 {
genesis 1152 vector<vector<unsigned char> > stack;
genesis 1153 if (!EvalScript(stack, scriptSig, txTo, nIn, nHashType))
genesis 1154 return false;
genesis 1155 if (!EvalScript(stack, scriptPubKey, txTo, nIn, nHashType))
genesis 1156 return false;
genesis 1157 if (stack.empty())
genesis 1158 return false;
genesis 1159 return CastToBool(stack.back());
genesis 1160 }
genesis 1161
genesis 1162
genesis 1163 bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType, CScript scriptPrereq)
genesis 1164 {
genesis 1165 assert(nIn < txTo.vin.size());
genesis 1166 CTxIn& txin = txTo.vin[nIn];
genesis 1167 assert(txin.prevout.n < txFrom.vout.size());
genesis 1168 const CTxOut& txout = txFrom.vout[txin.prevout.n];
genesis 1169
genesis 1170
genesis 1171
genesis 1172 uint256 hash = SignatureHash(scriptPrereq + txout.scriptPubKey, txTo, nIn, nHashType);
genesis 1173
genesis 1174 if (!Solver(keystore, txout.scriptPubKey, hash, nHashType, txin.scriptSig))
genesis 1175 return false;
genesis 1176
genesis 1177 txin.scriptSig = scriptPrereq + txin.scriptSig;
genesis 1178
genesis 1179
genesis 1180 if (scriptPrereq.empty())
genesis 1181 if (!VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, 0))
genesis 1182 return false;
genesis 1183
genesis 1184 return true;
genesis 1185 }
genesis 1186
genesis 1187
genesis 1188 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType)
genesis 1189 {
genesis 1190 assert(nIn < txTo.vin.size());
genesis 1191 const CTxIn& txin = txTo.vin[nIn];
genesis 1192 if (txin.prevout.n >= txFrom.vout.size())
genesis 1193 return false;
genesis 1194 const CTxOut& txout = txFrom.vout[txin.prevout.n];
genesis 1195
genesis 1196 if (txin.prevout.hash != txFrom.GetHash())
genesis 1197 return false;
genesis 1198
genesis 1199 if (!VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, nHashType))
genesis 1200 return false;
genesis 1201
genesis 1202 return true;
genesis 1203 }