You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compressor.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "compressor.h"
  6. #include "key.h"
  7. #include "script/standard.h"
  8. bool CScriptCompressor::IsToKeyID(CKeyID &hash) const
  9. {
  10. if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
  11. && script[2] == 20 && script[23] == OP_EQUALVERIFY
  12. && script[24] == OP_CHECKSIG) {
  13. memcpy(&hash, &script[3], 20);
  14. return true;
  15. }
  16. return false;
  17. }
  18. bool CScriptCompressor::IsToScriptID(CScriptID &hash) const
  19. {
  20. if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
  21. && script[22] == OP_EQUAL) {
  22. memcpy(&hash, &script[2], 20);
  23. return true;
  24. }
  25. return false;
  26. }
  27. bool CScriptCompressor::IsToPubKey(CPubKey &pubkey) const
  28. {
  29. if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
  30. && (script[1] == 0x02 || script[1] == 0x03)) {
  31. pubkey.Set(&script[1], &script[34]);
  32. return true;
  33. }
  34. if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG
  35. && script[1] == 0x04) {
  36. pubkey.Set(&script[1], &script[66]);
  37. return pubkey.IsFullyValid(); // if not fully valid, a case that would not be compressible
  38. }
  39. return false;
  40. }
  41. bool CScriptCompressor::Compress(std::vector<unsigned char> &out) const
  42. {
  43. CKeyID keyID;
  44. if (IsToKeyID(keyID)) {
  45. out.resize(21);
  46. out[0] = 0x00;
  47. memcpy(&out[1], &keyID, 20);
  48. return true;
  49. }
  50. CScriptID scriptID;
  51. if (IsToScriptID(scriptID)) {
  52. out.resize(21);
  53. out[0] = 0x01;
  54. memcpy(&out[1], &scriptID, 20);
  55. return true;
  56. }
  57. CPubKey pubkey;
  58. if (IsToPubKey(pubkey)) {
  59. out.resize(33);
  60. memcpy(&out[1], &pubkey[1], 32);
  61. if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
  62. out[0] = pubkey[0];
  63. return true;
  64. } else if (pubkey[0] == 0x04) {
  65. out[0] = 0x04 | (pubkey[64] & 0x01);
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. unsigned int CScriptCompressor::GetSpecialSize(unsigned int nSize) const
  72. {
  73. if (nSize == 0 || nSize == 1)
  74. return 20;
  75. if (nSize == 2 || nSize == 3 || nSize == 4 || nSize == 5)
  76. return 32;
  77. return 0;
  78. }
  79. bool CScriptCompressor::Decompress(unsigned int nSize, const std::vector<unsigned char> &in)
  80. {
  81. switch(nSize) {
  82. case 0x00:
  83. script.resize(25);
  84. script[0] = OP_DUP;
  85. script[1] = OP_HASH160;
  86. script[2] = 20;
  87. memcpy(&script[3], &in[0], 20);
  88. script[23] = OP_EQUALVERIFY;
  89. script[24] = OP_CHECKSIG;
  90. return true;
  91. case 0x01:
  92. script.resize(23);
  93. script[0] = OP_HASH160;
  94. script[1] = 20;
  95. memcpy(&script[2], &in[0], 20);
  96. script[22] = OP_EQUAL;
  97. return true;
  98. case 0x02:
  99. case 0x03:
  100. script.resize(35);
  101. script[0] = 33;
  102. script[1] = nSize;
  103. memcpy(&script[2], &in[0], 32);
  104. script[34] = OP_CHECKSIG;
  105. return true;
  106. case 0x04:
  107. case 0x05:
  108. unsigned char vch[33] = {};
  109. vch[0] = nSize - 2;
  110. memcpy(&vch[1], &in[0], 32);
  111. CPubKey pubkey(&vch[0], &vch[33]);
  112. if (!pubkey.Decompress())
  113. return false;
  114. assert(pubkey.size() == 65);
  115. script.resize(67);
  116. script[0] = 65;
  117. memcpy(&script[1], pubkey.begin(), 65);
  118. script[66] = OP_CHECKSIG;
  119. return true;
  120. }
  121. return false;
  122. }