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.

sign.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef STARWELS_SCRIPT_SIGN_H
  6. #define STARWELS_SCRIPT_SIGN_H
  7. #include "script/interpreter.h"
  8. class CKeyID;
  9. class CKeyStore;
  10. class CScript;
  11. class CTransaction;
  12. struct CMutableTransaction;
  13. /** Virtual base class for signature creators. */
  14. class BaseSignatureCreator {
  15. protected:
  16. const CKeyStore* keystore;
  17. public:
  18. BaseSignatureCreator(const CKeyStore* keystoreIn) : keystore(keystoreIn) {}
  19. const CKeyStore& KeyStore() const { return *keystore; };
  20. virtual ~BaseSignatureCreator() {}
  21. virtual const BaseSignatureChecker& Checker() const =0;
  22. /** Create a singular (non-script) signature. */
  23. virtual bool CreateSig(std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const =0;
  24. };
  25. /** A signature creator for transactions. */
  26. class TransactionSignatureCreator : public BaseSignatureCreator {
  27. const CTransaction* txTo;
  28. unsigned int nIn;
  29. int nHashType;
  30. CAmount amount;
  31. const TransactionSignatureChecker checker;
  32. public:
  33. TransactionSignatureCreator(const CKeyStore* keystoreIn, const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn=SIGHASH_ALL);
  34. const BaseSignatureChecker& Checker() const override { return checker; }
  35. bool CreateSig(std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override;
  36. };
  37. class MutableTransactionSignatureCreator : public TransactionSignatureCreator {
  38. CTransaction tx;
  39. public:
  40. MutableTransactionSignatureCreator(const CKeyStore* keystoreIn, const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn) : TransactionSignatureCreator(keystoreIn, &tx, nInIn, amountIn, nHashTypeIn), tx(*txToIn) {}
  41. };
  42. /** A signature creator that just produces 72-byte empty signatures. */
  43. class DummySignatureCreator : public BaseSignatureCreator {
  44. public:
  45. DummySignatureCreator(const CKeyStore* keystoreIn) : BaseSignatureCreator(keystoreIn) {}
  46. const BaseSignatureChecker& Checker() const override;
  47. bool CreateSig(std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override;
  48. };
  49. struct SignatureData {
  50. CScript scriptSig;
  51. CScriptWitness scriptWitness;
  52. SignatureData() {}
  53. explicit SignatureData(const CScript& script) : scriptSig(script) {}
  54. };
  55. /** Produce a script signature using a generic signature creator. */
  56. bool ProduceSignature(const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata);
  57. /** Produce a script signature for a transaction. */
  58. bool SignSignature(const CKeyStore &keystore, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType);
  59. bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType);
  60. /** Combine two script signatures using a generic signature checker, intelligently, possibly with OP_0 placeholders. */
  61. SignatureData CombineSignatures(const CScript& scriptPubKey, const BaseSignatureChecker& checker, const SignatureData& scriptSig1, const SignatureData& scriptSig2);
  62. /** Extract signature data from a transaction, and insert it. */
  63. SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn);
  64. void UpdateTransaction(CMutableTransaction& tx, unsigned int nIn, const SignatureData& data);
  65. #endif // STARWELS_SCRIPT_SIGN_H