Bitcoin ABC 0.34.2
P2P Digital Currency
sign.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_SCRIPT_SIGN_H
7#define BITCOIN_SCRIPT_SIGN_H
8
9#include <attributes.h>
10#include <coins.h>
11#include <hash.h>
12#include <pubkey.h>
13#include <script/interpreter.h>
14#include <script/keyorigin.h>
15#include <script/sighashtype.h>
16#include <streams.h>
17
18class CKey;
19class CKeyID;
21class CScript;
22class CScriptID;
23class CTransaction;
24class SigningProvider;
25
28public:
29 virtual ~BaseSignatureCreator() = default;
30 virtual const BaseSignatureChecker &Checker() const = 0;
31
33 virtual bool CreateSig(const SigningProvider &provider,
34 std::vector<uint8_t> &vchSig, const CKeyID &keyid,
35 const CScript &scriptCode) const = 0;
36};
37
41 unsigned int nIn;
45
46public:
48 const CMutableTransaction &tx LIFETIMEBOUND, unsigned int input_idx,
49 const Amount &amount, SigHashType sig_hash_type = SigHashType());
50 const BaseSignatureChecker &Checker() const override { return checker; }
51 bool CreateSig(const SigningProvider &provider,
52 std::vector<uint8_t> &vchSig, const CKeyID &keyid,
53 const CScript &scriptCode) const override;
54};
55
60
61typedef std::pair<CPubKey, std::vector<uint8_t>> SigPair;
62
63// This struct contains information from a transaction input and also contains
64// signatures for that input. The information contained here can be used to
65// create a signature and is also filled by ProduceSignature in order to
66// construct final scriptSigs.
69 bool complete = false;
77 std::map<CKeyID, SigPair> signatures;
78 std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> misc_pubkeys;
80 std::vector<CKeyID> missing_pubkeys;
82 std::vector<CKeyID> missing_sigs;
85
86 SignatureData() = default;
89};
90
91// Takes a stream and multiple arguments and serializes them as if first
92// serialized into a vector and then into the stream. The resulting output into
93// the stream has the total serialized length of all of the objects followed by
94// all objects concatenated with each other.
95template <typename Stream, typename... X>
96void SerializeToVector(Stream &s, const X &...args) {
97 SizeComputer sizecomp;
98 SerializeMany(sizecomp, args...);
99 WriteCompactSize(s, sizecomp.size());
100 SerializeMany(s, args...);
101}
102
103// Takes a stream and multiple arguments and unserializes them first as a vector
104// then each object individually in the order provided in the arguments.
105template <typename Stream, typename... X>
106void UnserializeFromVector(Stream &s, X &...args) {
107 size_t expected_size = ReadCompactSize(s);
108 size_t remaining_before = s.size();
109 UnserializeMany(s, args...);
110 size_t remaining_after = s.size();
111 if (remaining_after + expected_size != remaining_before) {
112 throw std::ios_base::failure("Size of value was not the stated size");
113 }
114}
115
116// Deserialize HD keypaths into a map
117template <typename Stream>
118void DeserializeHDKeypaths(Stream &s, const std::vector<uint8_t> &key,
119 std::map<CPubKey, KeyOriginInfo> &hd_keypaths) {
120 // Make sure that the key is the size of pubkey + 1
121 if (key.size() != CPubKey::SIZE + 1 &&
122 key.size() != CPubKey::COMPRESSED_SIZE + 1) {
123 throw std::ios_base::failure(
124 "Size of key was not the expected size for the type BIP32 keypath");
125 }
126 // Read in the pubkey from key
127 CPubKey pubkey(key.begin() + 1, key.end());
128 if (!pubkey.IsFullyValid()) {
129 throw std::ios_base::failure("Invalid pubkey");
130 }
131 if (hd_keypaths.count(pubkey) > 0) {
132 throw std::ios_base::failure(
133 "Duplicate Key, pubkey derivation path already provided");
134 }
135
136 // Read in key path
137 uint64_t value_len = ReadCompactSize(s);
138 if (value_len % 4 || value_len == 0) {
139 throw std::ios_base::failure("Invalid length for HD key path");
140 }
141
142 KeyOriginInfo keypath;
143 s >> keypath.fingerprint;
144 for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
145 uint32_t index;
146 s >> index;
147 keypath.path.push_back(index);
148 }
149
150 // Add to map
151 hd_keypaths.emplace(pubkey, std::move(keypath));
152}
153
154// Serialize HD keypaths to a stream from a map
155template <typename Stream>
156void SerializeHDKeypaths(Stream &s,
157 const std::map<CPubKey, KeyOriginInfo> &hd_keypaths,
158 uint8_t type) {
159 for (const auto &keypath_pair : hd_keypaths) {
160 if (!keypath_pair.first.IsValid()) {
161 throw std::ios_base::failure("Invalid CPubKey being serialized");
162 }
163 SerializeToVector(s, type, Span{keypath_pair.first});
164 WriteCompactSize(s, (keypath_pair.second.path.size() + 1) *
165 sizeof(uint32_t));
166 s << keypath_pair.second.fingerprint;
167 for (const auto &path : keypath_pair.second.path) {
168 s << path;
169 }
170 }
171}
172
174bool ProduceSignature(const SigningProvider &provider,
175 const BaseSignatureCreator &creator,
176 const CScript &scriptPubKey, SignatureData &sigdata);
177
179bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey,
180 CMutableTransaction &txTo, unsigned int nIn,
181 const Amount amount, SigHashType sigHashType);
182bool SignSignature(const SigningProvider &provider, const CTransaction &txFrom,
183 CMutableTransaction &txTo, unsigned int nIn,
184 SigHashType sigHashType);
185
188 unsigned int nIn, const CTxOut &txout);
189void UpdateInput(CTxIn &input, const SignatureData &data);
190
197bool IsSolvable(const SigningProvider &provider, const CScript &script);
198
200bool SignTransaction(CMutableTransaction &mtx, const SigningProvider *provider,
201 const std::map<COutPoint, Coin> &coins,
202 SigHashType sigHashType,
203 std::map<int, std::string> &input_errors);
204
205#endif // BITCOIN_SCRIPT_SIGN_H
#define LIFETIMEBOUND
Definition: attributes.h:16
Interface for signature creators.
Definition: sign.h:27
virtual bool CreateSig(const SigningProvider &provider, std::vector< uint8_t > &vchSig, const CKeyID &keyid, const CScript &scriptCode) const =0
Create a singular (non-script) signature.
virtual const BaseSignatureChecker & Checker() const =0
virtual ~BaseSignatureCreator()=default
An encapsulated secp256k1 private key.
Definition: key.h:28
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
A mutable version of CTransaction.
Definition: transaction.h:274
An encapsulated public key.
Definition: pubkey.h:31
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:37
static constexpr unsigned int SIZE
secp256k1:
Definition: pubkey.h:36
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
Definition: pubkey.cpp:261
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:434
A reference to a CScript: the Hash160 of its serialization.
Definition: script.h:564
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
An input of a transaction.
Definition: transaction.h:59
An output of a transaction.
Definition: transaction.h:128
A signature creator for transactions.
Definition: sign.h:39
MutableTransactionSignatureCreator(const CMutableTransaction &tx LIFETIMEBOUND, unsigned int input_idx, const Amount &amount, SigHashType sig_hash_type=SigHashType())
Definition: sign.cpp:19
bool CreateSig(const SigningProvider &provider, std::vector< uint8_t > &vchSig, const CKeyID &keyid, const CScript &scriptCode) const override
Create a singular (non-script) signature.
Definition: sign.cpp:25
const BaseSignatureChecker & Checker() const override
Definition: sign.h:50
const CMutableTransaction & m_txto
Definition: sign.h:40
const MutableTransactionSignatureChecker checker
Definition: sign.h:44
Signature hash type wrapper class.
Definition: sighashtype.h:37
An interface to be implemented by keystores that support signing.
GetSerializeSize implementations
Definition: serialize.h:1196
size_t size() const
Definition: serialize.h:1213
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:94
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:228
160-bit opaque blob.
Definition: uint256.h:184
void UnserializeMany(Stream &s, Args &&...args)
Definition: serialize.h:1222
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Definition: serialize.h:1260
void SerializeMany(Stream &s, const Args &...args)
Definition: serialize.h:1217
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:469
void UnserializeFromVector(Stream &s, X &...args)
Definition: sign.h:106
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:343
void SerializeToVector(Stream &s, const X &...args)
Definition: sign.h:96
bool SignTransaction(CMutableTransaction &mtx, const SigningProvider *provider, const std::map< COutPoint, Coin > &coins, SigHashType sigHashType, std::map< int, std::string > &input_errors)
Sign the CMutableTransaction.
Definition: sign.cpp:453
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Check whether we know how to sign for an output like this, assuming we have all private keys.
Definition: sign.cpp:436
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:433
void SerializeHDKeypaths(Stream &s, const std::map< CPubKey, KeyOriginInfo > &hd_keypaths, uint8_t type)
Definition: sign.h:156
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Definition: sign.cpp:287
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:431
void DeserializeHDKeypaths(Stream &s, const std::vector< uint8_t > &key, std::map< CPubKey, KeyOriginInfo > &hd_keypaths)
Definition: sign.h:118
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:210
std::pair< CPubKey, std::vector< uint8_t > > SigPair
Definition: sign.h:61
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const Amount amount, SigHashType sigHashType)
Produce a script signature for a transaction.
Definition: sign.cpp:362
Definition: amount.h:23
std::vector< uint32_t > path
Definition: keyorigin.h:14
uint8_t fingerprint[4]
First 32 bits of the Hash160 of the public key at the root of the path.
Definition: keyorigin.h:13
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
Definition: sign.h:84
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
Definition: sign.h:82
void MergeSignatureData(SignatureData sigdata)
Definition: sign.cpp:347
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input.
Definition: sign.h:77
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
Definition: sign.h:78
SignatureData()=default
CScript scriptSig
The scriptSig of an input.
Definition: sign.h:72
CScript redeem_script
The redeemScript (if any) for the input.
Definition: sign.h:74
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
Definition: sign.h:80
SignatureData(const CScript &script)
Definition: sign.h:87
bool complete
Stores whether the scriptSig are complete.
Definition: sign.h:69