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.

standard.cpp 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels 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 "script/standard.h"
  6. #include "pubkey.h"
  7. #include "script/script.h"
  8. #include "util.h"
  9. #include "utilstrencodings.h"
  10. typedef std::vector<unsigned char> valtype;
  11. bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
  12. unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
  13. CScriptID::CScriptID(const CScript& in) : uint160(Hash160(in.begin(), in.end())) {}
  14. const char* GetTxnOutputType(txnouttype t)
  15. {
  16. switch (t)
  17. {
  18. case TX_NONSTANDARD: return "nonstandard";
  19. case TX_PUBKEY: return "pubkey";
  20. case TX_PUBKEYHASH: return "pubkeyhash";
  21. case TX_SCRIPTHASH: return "scripthash";
  22. case TX_MULTISIG: return "multisig";
  23. case TX_NULL_DATA: return "nulldata";
  24. case TX_WITNESS_V0_KEYHASH: return "witness_v0_keyhash";
  25. case TX_WITNESS_V0_SCRIPTHASH: return "witness_v0_scripthash";
  26. }
  27. return nullptr;
  28. }
  29. /**
  30. * Return public keys or hashes from scriptPubKey, for 'standard' transaction types.
  31. */
  32. bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet)
  33. {
  34. // Templates
  35. static std::multimap<txnouttype, CScript> mTemplates;
  36. if (mTemplates.empty())
  37. {
  38. // Standard tx, sender provides pubkey, receiver adds signature
  39. mTemplates.insert(std::make_pair(TX_PUBKEY, CScript() << OP_PUBKEY << OP_CHECKSIG));
  40. // Starwels address tx, sender provides hash of pubkey, receiver provides signature and pubkey
  41. mTemplates.insert(std::make_pair(TX_PUBKEYHASH, CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG));
  42. // Sender provides N pubkeys, receivers provides M signatures
  43. mTemplates.insert(std::make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
  44. }
  45. vSolutionsRet.clear();
  46. // Shortcut for pay-to-script-hash, which are more constrained than the other types:
  47. // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
  48. if (scriptPubKey.IsPayToScriptHash())
  49. {
  50. typeRet = TX_SCRIPTHASH;
  51. std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
  52. vSolutionsRet.push_back(hashBytes);
  53. return true;
  54. }
  55. int witnessversion;
  56. std::vector<unsigned char> witnessprogram;
  57. if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
  58. if (witnessversion == 0 && witnessprogram.size() == 20) {
  59. typeRet = TX_WITNESS_V0_KEYHASH;
  60. vSolutionsRet.push_back(witnessprogram);
  61. return true;
  62. }
  63. if (witnessversion == 0 && witnessprogram.size() == 32) {
  64. typeRet = TX_WITNESS_V0_SCRIPTHASH;
  65. vSolutionsRet.push_back(witnessprogram);
  66. return true;
  67. }
  68. return false;
  69. }
  70. // Provably prunable, data-carrying output
  71. //
  72. // So long as script passes the IsUnspendable() test and all but the first
  73. // byte passes the IsPushOnly() test we don't care what exactly is in the
  74. // script.
  75. if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN && scriptPubKey.IsPushOnly(scriptPubKey.begin()+1)) {
  76. typeRet = TX_NULL_DATA;
  77. return true;
  78. }
  79. // Scan templates
  80. const CScript& script1 = scriptPubKey;
  81. for (const std::pair<txnouttype, CScript>& tplate : mTemplates)
  82. {
  83. const CScript& script2 = tplate.second;
  84. vSolutionsRet.clear();
  85. opcodetype opcode1, opcode2;
  86. std::vector<unsigned char> vch1, vch2;
  87. // Compare
  88. CScript::const_iterator pc1 = script1.begin();
  89. CScript::const_iterator pc2 = script2.begin();
  90. while (true)
  91. {
  92. if (pc1 == script1.end() && pc2 == script2.end())
  93. {
  94. // Found a match
  95. typeRet = tplate.first;
  96. if (typeRet == TX_MULTISIG)
  97. {
  98. // Additional checks for TX_MULTISIG:
  99. unsigned char m = vSolutionsRet.front()[0];
  100. unsigned char n = vSolutionsRet.back()[0];
  101. if (m < 1 || n < 1 || m > n || vSolutionsRet.size()-2 != n)
  102. return false;
  103. }
  104. return true;
  105. }
  106. if (!script1.GetOp(pc1, opcode1, vch1))
  107. break;
  108. if (!script2.GetOp(pc2, opcode2, vch2))
  109. break;
  110. // Template matching opcodes:
  111. if (opcode2 == OP_PUBKEYS)
  112. {
  113. while (vch1.size() >= 33 && vch1.size() <= 65)
  114. {
  115. vSolutionsRet.push_back(vch1);
  116. if (!script1.GetOp(pc1, opcode1, vch1))
  117. break;
  118. }
  119. if (!script2.GetOp(pc2, opcode2, vch2))
  120. break;
  121. // Normal situation is to fall through
  122. // to other if/else statements
  123. }
  124. if (opcode2 == OP_PUBKEY)
  125. {
  126. if (vch1.size() < 33 || vch1.size() > 65)
  127. break;
  128. vSolutionsRet.push_back(vch1);
  129. }
  130. else if (opcode2 == OP_PUBKEYHASH)
  131. {
  132. if (vch1.size() != sizeof(uint160))
  133. break;
  134. vSolutionsRet.push_back(vch1);
  135. }
  136. else if (opcode2 == OP_SMALLINTEGER)
  137. { // Single-byte small integer pushed onto vSolutions
  138. if (opcode1 == OP_0 ||
  139. (opcode1 >= OP_1 && opcode1 <= OP_16))
  140. {
  141. char n = (char)CScript::DecodeOP_N(opcode1);
  142. vSolutionsRet.push_back(valtype(1, n));
  143. }
  144. else
  145. break;
  146. }
  147. else if (opcode1 != opcode2 || vch1 != vch2)
  148. {
  149. // Others must match exactly
  150. break;
  151. }
  152. }
  153. }
  154. vSolutionsRet.clear();
  155. typeRet = TX_NONSTANDARD;
  156. return false;
  157. }
  158. bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
  159. {
  160. std::vector<valtype> vSolutions;
  161. txnouttype whichType;
  162. if (!Solver(scriptPubKey, whichType, vSolutions))
  163. return false;
  164. if (whichType == TX_PUBKEY)
  165. {
  166. CPubKey pubKey(vSolutions[0]);
  167. if (!pubKey.IsValid())
  168. return false;
  169. addressRet = pubKey.GetID();
  170. return true;
  171. }
  172. else if (whichType == TX_PUBKEYHASH)
  173. {
  174. addressRet = CKeyID(uint160(vSolutions[0]));
  175. return true;
  176. }
  177. else if (whichType == TX_SCRIPTHASH)
  178. {
  179. addressRet = CScriptID(uint160(vSolutions[0]));
  180. return true;
  181. }
  182. // Multisig txns have more than one address...
  183. return false;
  184. }
  185. bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
  186. {
  187. addressRet.clear();
  188. typeRet = TX_NONSTANDARD;
  189. std::vector<valtype> vSolutions;
  190. if (!Solver(scriptPubKey, typeRet, vSolutions))
  191. return false;
  192. if (typeRet == TX_NULL_DATA){
  193. // This is data, not addresses
  194. return false;
  195. }
  196. if (typeRet == TX_MULTISIG)
  197. {
  198. nRequiredRet = vSolutions.front()[0];
  199. for (unsigned int i = 1; i < vSolutions.size()-1; i++)
  200. {
  201. CPubKey pubKey(vSolutions[i]);
  202. if (!pubKey.IsValid())
  203. continue;
  204. CTxDestination address = pubKey.GetID();
  205. addressRet.push_back(address);
  206. }
  207. if (addressRet.empty())
  208. return false;
  209. }
  210. else
  211. {
  212. nRequiredRet = 1;
  213. CTxDestination address;
  214. if (!ExtractDestination(scriptPubKey, address))
  215. return false;
  216. addressRet.push_back(address);
  217. }
  218. return true;
  219. }
  220. namespace
  221. {
  222. class CScriptVisitor : public boost::static_visitor<bool>
  223. {
  224. private:
  225. CScript *script;
  226. public:
  227. CScriptVisitor(CScript *scriptin) { script = scriptin; }
  228. bool operator()(const CNoDestination &dest) const {
  229. script->clear();
  230. return false;
  231. }
  232. bool operator()(const CKeyID &keyID) const {
  233. script->clear();
  234. *script << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
  235. return true;
  236. }
  237. bool operator()(const CScriptID &scriptID) const {
  238. script->clear();
  239. *script << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
  240. return true;
  241. }
  242. };
  243. } // namespace
  244. CScript GetScriptForDestination(const CTxDestination& dest)
  245. {
  246. CScript script;
  247. boost::apply_visitor(CScriptVisitor(&script), dest);
  248. return script;
  249. }
  250. CScript GetScriptForRawPubKey(const CPubKey& pubKey)
  251. {
  252. return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
  253. }
  254. CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
  255. {
  256. CScript script;
  257. script << CScript::EncodeOP_N(nRequired);
  258. for (const CPubKey& key : keys)
  259. script << ToByteVector(key);
  260. script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
  261. return script;
  262. }
  263. CScript GetScriptForWitness(const CScript& redeemscript)
  264. {
  265. CScript ret;
  266. txnouttype typ;
  267. std::vector<std::vector<unsigned char> > vSolutions;
  268. if (Solver(redeemscript, typ, vSolutions)) {
  269. if (typ == TX_PUBKEY) {
  270. unsigned char h160[20];
  271. CHash160().Write(&vSolutions[0][0], vSolutions[0].size()).Finalize(h160);
  272. ret << OP_0 << std::vector<unsigned char>(&h160[0], &h160[20]);
  273. return ret;
  274. } else if (typ == TX_PUBKEYHASH) {
  275. ret << OP_0 << vSolutions[0];
  276. return ret;
  277. }
  278. }
  279. uint256 hash;
  280. CSHA256().Write(&redeemscript[0], redeemscript.size()).Finalize(hash.begin());
  281. ret << OP_0 << ToByteVector(hash);
  282. return ret;
  283. }