Skip to content

Commit

Permalink
Merge #13976: [0.17] Backport #13960 & #13917
Browse files Browse the repository at this point in the history
0333914 More tests of signer checks (Andrew Chow)
8935869 Test that a non-witness script as witness utxo is not signed (Andrew Chow)
dbaadc9 Only wipe wrong UTXO type data if overwritten by wallet (Pieter Wuille)
ad6d845 Additional sanity checks in SignPSBTInput (Pieter Wuille)
517010e Serialize non-witness utxo as a non-witness tx but always deserialize as witness (Andrew Chow)
8c4cd2b Fix PSBT deserialization of 0-input transactions (Andrew Chow)

Pull request description:

  Backports #13917 and #13960 to the 0.17 branch.

Tree-SHA512: b3853aff2a13a53aa0a390b6b4b0c539f0ef0d42f2c517e956efd0b135c74c4ddce6a1d00700849a58c696824fa95951d8cac6ca58b426e8dfcb8bb62f680b7c
  • Loading branch information
laanwj committed Aug 15, 2018
2 parents ff41e47 + 0333914 commit 4a2960f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
16 changes: 16 additions & 0 deletions src/script/sign.cpp
Expand Up @@ -244,17 +244,33 @@ bool SignPSBTInput(const SigningProvider& provider, const CMutableTransaction& t
input.FillSignatureData(sigdata);

// Get UTXO
bool require_witness_sig = false;
CTxOut utxo;
if (input.non_witness_utxo) {
// If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
if (input.non_witness_utxo->GetHash() != tx.vin[index].prevout.hash) return false;
// If both witness and non-witness UTXO are provided, verify that they match. This check shouldn't
// matter, as the PSBT deserializer enforces only one of both is provided, and the only way both
// can be present is when they're added simultaneously by FillPSBT (in which case they always match).
// Still, check in order to not rely on callers to enforce this.
if (!input.witness_utxo.IsNull() && input.non_witness_utxo->vout[tx.vin[index].prevout.n] != input.witness_utxo) return false;
utxo = input.non_witness_utxo->vout[tx.vin[index].prevout.n];
} else if (!input.witness_utxo.IsNull()) {
utxo = input.witness_utxo;
// When we're taking our information from a witness UTXO, we can't verify it is actually data from
// the output being spent. This is safe in case a witness signature is produced (which includes this
// information directly in the hash), but not for non-witness signatures. Remember that we require
// a witness signature in this situation.
require_witness_sig = true;
} else {
return false;
}

MutableTransactionSignatureCreator creator(&tx, index, utxo.nValue, sighash);
sigdata.witness = false;
bool sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata);
// Verify that a witness signature was produced in case one was required.
if (require_witness_sig && !sigdata.witness) return false;
input.FromSignatureData(sigdata);
return sig_complete;
}
Expand Down
18 changes: 13 additions & 5 deletions src/script/sign.h
Expand Up @@ -223,7 +223,8 @@ struct PSBTInput
// If there is a non-witness utxo, then don't add the witness one.
if (non_witness_utxo) {
SerializeToVector(s, PSBT_IN_NON_WITNESS_UTXO);
SerializeToVector(s, non_witness_utxo);
OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
SerializeToVector(os, non_witness_utxo);
} else if (!witness_utxo.IsNull()) {
SerializeToVector(s, PSBT_IN_WITNESS_UTXO);
SerializeToVector(s, witness_utxo);
Expand Down Expand Up @@ -297,13 +298,17 @@ struct PSBTInput
// Do stuff based on type
switch(type) {
case PSBT_IN_NON_WITNESS_UTXO:
{
if (non_witness_utxo) {
throw std::ios_base::failure("Duplicate Key, input non-witness utxo already provided");
} else if (key.size() != 1) {
throw std::ios_base::failure("Non-witness utxo key is more than one byte type");
}
UnserializeFromVector(s, non_witness_utxo);
// Set the stream to unserialize with witness since this is always a valid network transaction
OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() & ~SERIALIZE_TRANSACTION_NO_WITNESS);
UnserializeFromVector(os, non_witness_utxo);
break;
}
case PSBT_IN_WITNESS_UTXO:
if (!witness_utxo.IsNull()) {
throw std::ios_base::failure("Duplicate Key, input witness utxo already provided");
Expand Down Expand Up @@ -547,7 +552,8 @@ struct PartiallySignedTransaction
SerializeToVector(s, PSBT_GLOBAL_UNSIGNED_TX);

// Write serialized tx to a stream
SerializeToVector(s, *tx);
OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
SerializeToVector(os, *tx);

// Write the unknown things
for (auto& entry : unknown) {
Expand Down Expand Up @@ -601,7 +607,9 @@ struct PartiallySignedTransaction
throw std::ios_base::failure("Global unsigned tx key is more than one byte type");
}
CMutableTransaction mtx;
UnserializeFromVector(s, mtx);
// Set the stream to serialize with non-witness since this should always be non-witness
OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
UnserializeFromVector(os, mtx);
tx = std::move(mtx);
// Make sure that all scriptSigs and scriptWitnesses are empty
for (const CTxIn& txin : tx->vin) {
Expand Down Expand Up @@ -678,7 +686,7 @@ bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreato
bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType);
bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType);

/** Signs a PSBTInput */
/** Signs a PSBTInput, verifying that all provided data matches what is being signed. */
bool SignPSBTInput(const SigningProvider& provider, const CMutableTransaction& tx, PSBTInput& input, SignatureData& sigdata, int index, int sighash = 1);

/** Extract signature data from a transaction input, and insert it. */
Expand Down
1 change: 1 addition & 0 deletions src/streams.h
Expand Up @@ -61,6 +61,7 @@ class OverrideStream

int GetVersion() const { return nVersion; }
int GetType() const { return nType; }
size_t size() const { return stream->size(); }
};

template<typename S>
Expand Down
15 changes: 9 additions & 6 deletions src/wallet/rpcwallet.cpp
Expand Up @@ -4504,10 +4504,11 @@ bool FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, const C

// If we don't know about this input, skip it and let someone else deal with it
const uint256& txhash = txin.prevout.hash;
const auto& it = pwallet->mapWallet.find(txhash);
const auto it = pwallet->mapWallet.find(txhash);
if (it != pwallet->mapWallet.end()) {
const CWalletTx& wtx = it->second;
CTxOut utxo = wtx.tx->vout[txin.prevout.n];
// Update both UTXOs from the wallet.
input.non_witness_utxo = wtx.tx;
input.witness_utxo = utxo;
}
Expand All @@ -4524,11 +4525,13 @@ bool FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& psbtx, const C
complete &= SignPSBTInput(PublicOnlySigningProvider(pwallet), *psbtx.tx, input, sigdata, i, sighash_type);
}

// Drop the unnecessary UTXO
if (sigdata.witness) {
input.non_witness_utxo = nullptr;
} else {
input.witness_utxo.SetNull();
if (it != pwallet->mapWallet.end()) {
// Drop the unnecessary UTXO if we added both from the wallet.
if (sigdata.witness) {
input.non_witness_utxo = nullptr;
} else {
input.witness_utxo.SetNull();
}
}

// Get public key paths
Expand Down

0 comments on commit 4a2960f

Please sign in to comment.