Bitcoin Core  0.19.99
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or
9 #include <hash.h>
10 #include <pubkey.h>
11 #include <script/interpreter.h>
12 #include <script/keyorigin.h>
13 #include <streams.h>
15 class CKey;
16 class CKeyID;
17 class CScript;
18 class CScriptID;
19 class CTransaction;
20 class SigningProvider;
22 struct CMutableTransaction;
26 public:
27  virtual ~BaseSignatureCreator() {}
28  virtual const BaseSignatureChecker& Checker() const =0;
31  virtual bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const =0;
32 };
37  unsigned int nIn;
38  int nHashType;
42 public:
43  MutableTransactionSignatureCreator(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn = SIGHASH_ALL);
44  const BaseSignatureChecker& Checker() const override { return checker; }
45  bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override;
46 };
53 typedef std::pair<CPubKey, std::vector<unsigned char>> SigPair;
55 // This struct contains information from a transaction input and also contains signatures for that input.
56 // The information contained here can be used to create a signature and is also filled by ProduceSignature
57 // in order to construct final scriptSigs and scriptWitnesses.
58 struct SignatureData {
59  bool complete = false;
60  bool witness = false;
65  std::map<CKeyID, SigPair> signatures;
66  std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> misc_pubkeys;
67  std::vector<CKeyID> missing_pubkeys;
68  std::vector<CKeyID> missing_sigs;
73  explicit SignatureData(const CScript& script) : scriptSig(script) {}
74  void MergeSignatureData(SignatureData sigdata);
75 };
77 // Takes a stream and multiple arguments and serializes them as if first serialized into a vector and then into the stream
78 // The resulting output into the stream has the total serialized length of all of the objects followed by all objects concatenated with each other.
79 template<typename Stream, typename... X>
80 void SerializeToVector(Stream& s, const X&... args)
81 {
82  WriteCompactSize(s, GetSerializeSizeMany(s.GetVersion(), args...));
83  SerializeMany(s, args...);
84 }
86 // Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments
87 template<typename Stream, typename... X>
88 void UnserializeFromVector(Stream& s, X&... args)
89 {
90  size_t expected_size = ReadCompactSize(s);
91  size_t remaining_before = s.size();
92  UnserializeMany(s, args...);
93  size_t remaining_after = s.size();
94  if (remaining_after + expected_size != remaining_before) {
95  throw std::ios_base::failure("Size of value was not the stated size");
96  }
97 }
99 // Deserialize HD keypaths into a map
100 template<typename Stream>
101 void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
102 {
103  // Make sure that the key is the size of pubkey + 1
104  if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
105  throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath");
106  }
107  // Read in the pubkey from key
108  CPubKey pubkey(key.begin() + 1, key.end());
109  if (!pubkey.IsFullyValid()) {
110  throw std::ios_base::failure("Invalid pubkey");
111  }
112  if (hd_keypaths.count(pubkey) > 0) {
113  throw std::ios_base::failure("Duplicate Key, pubkey derivation path already provided");
114  }
116  // Read in key path
117  uint64_t value_len = ReadCompactSize(s);
118  if (value_len % 4 || value_len == 0) {
119  throw std::ios_base::failure("Invalid length for HD key path");
120  }
122  KeyOriginInfo keypath;
123  s >> keypath.fingerprint;
124  for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
125  uint32_t index;
126  s >> index;
127  keypath.path.push_back(index);
128  }
130  // Add to map
131  hd_keypaths.emplace(pubkey, std::move(keypath));
132 }
134 // Serialize HD keypaths to a stream from a map
135 template<typename Stream>
136 void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, uint8_t type)
137 {
138  for (auto keypath_pair : hd_keypaths) {
139  if (!keypath_pair.first.IsValid()) {
140  throw std::ios_base::failure("Invalid CPubKey being serialized");
141  }
142  SerializeToVector(s, type, MakeSpan(keypath_pair.first));
143  WriteCompactSize(s, (keypath_pair.second.path.size() + 1) * sizeof(uint32_t));
144  s << keypath_pair.second.fingerprint;
145  for (const auto& path : keypath_pair.second.path) {
146  s << path;
147  }
148  }
149 }
152 bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata);
155 bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType);
156 bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType);
159 SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout);
160 void UpdateInput(CTxIn& input, const SignatureData& data);
162 /* Check whether we know how to sign for an output like this, assuming we
163  * have all private keys. While this function does not need private keys, the passed
164  * provider is used to look up public keys and redeemscripts by hash.
165  * Solvability is unrelated to whether we consider this output to be ours. */
166 bool IsSolvable(const SigningProvider& provider, const CScript& script);
169 bool IsSegWitOutput(const SigningProvider& provider, const CScript& script);
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
Definition: sign.cpp:453
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:434
unsigned char fingerprint[4]
First 32 bits of the Hash160 of the public key at the root of the path.
Definition: keyorigin.h:13
static constexpr unsigned int SIZE
Definition: pubkey.h:36
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
Definition: sign.h:63
void SerializeToVector(Stream &s, const X &... args)
Definition: sign.h:80
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:337
virtual bool CreateSig(const SigningProvider &provider, std::vector< unsigned char > &vchSig, const CKeyID &keyid, const CScript &scriptCode, SigVersion sigversion) const =0
Create a singular (non-script) signature.
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1100
const MutableTransactionSignatureChecker checker
Definition: sign.h:40
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Definition: sign.cpp:286
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format...
Definition: sign.h:61
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
Definition: sign.h:68
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:431
Interface for signature creators.
Definition: sign.h:25
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
Definition: sign.h:67
const BaseSignatureChecker & Checker() const override
Definition: sign.h:44
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
Definition: sign.h:66
A signature creator for transactions.
Definition: sign.h:35
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
Definition: span.h:55
uint256 missing_witness_script
SHA256 of the missing witnessScript (if any)
Definition: sign.h:70
void DeserializeHDKeypaths(Stream &s, const std::vector< unsigned char > &key, std::map< CPubKey, KeyOriginInfo > &hd_keypaths)
Definition: sign.h:101
void UnserializeMany(Stream &s)
Definition: serialize.h:1071
void UnserializeFromVector(Stream &s, X &... args)
Definition: sign.h:88
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &scriptPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
Definition: sign.cpp:196
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
SignatureData(const CScript &script)
Definition: sign.h:73
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:351
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
Definition: sign.h:69
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:37
An input of a transaction.
Definition: transaction.h:63
An encapsulated public key.
Definition: pubkey.h:30
std::pair< CPubKey, std::vector< unsigned char > > SigPair
Definition: sign.h:53
An output of a transaction.
Definition: transaction.h:133
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:432
void SerializeMany(Stream &s)
Definition: serialize.h:1059
CScriptWitness scriptWitness
The scriptWitness of an input. Contains complete signatures or the traditional partial signatures for...
Definition: sign.h:64
#define X(name)
Definition: net.cpp:500
256-bit opaque blob.
Definition: uint256.h:120
An interface to be implemented by keystores that support signing.
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType)
Produce a script signature for a transaction.
Definition: sign.cpp:373
void SerializeHDKeypaths(Stream &s, const std::map< CPubKey, KeyOriginInfo > &hd_keypaths, uint8_t type)
Definition: sign.h:136
virtual ~BaseSignatureCreator()
Definition: sign.h:27
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
size_t GetSerializeSizeMany(int nVersion, const T &... t)
Definition: serialize.h:1112
160-bit opaque blob.
Definition: uint256.h:109
Definition: sign.h:72
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:21
A mutable version of CTransaction.
Definition: transaction.h:366
An encapsulated private key.
Definition: key.h:27
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
std::vector< uint32_t > path
Definition: keyorigin.h:14
CScript redeem_script
The redeemScript (if any) for the input.
Definition: sign.h:62
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
Definition: sign.h:65
virtual const BaseSignatureChecker & Checker() const =0
const CMutableTransaction * txTo
Definition: sign.h:36
Definition: interpreter.h:130