2014-08-27 17:22:33 +02:00
// Copyright (c) 2009-2010 Satoshi Nakamoto
2018-07-26 18:36:45 -04:00
// Copyright (c) 2009-2018 The Bitcoin Core developers
2014-09-09 10:00:42 +02:00
// Distributed under the MIT software license, see the accompanying
2014-08-27 17:22:33 +02:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2017-11-10 13:57:53 +13:00
# include <script/sign.h>
2014-08-27 17:22:33 +02:00
2017-11-10 13:57:53 +13:00
# include <key.h>
# include <policy/policy.h>
# include <primitives/transaction.h>
# include <script/standard.h>
# include <uint256.h>
2014-08-27 17:22:33 +02:00
2015-10-29 07:11:24 +01:00
typedef std : : vector < unsigned char > valtype ;
2014-08-27 17:22:33 +02:00
2018-05-20 22:47:14 +02:00
MutableTransactionSignatureCreator : : MutableTransactionSignatureCreator ( const CMutableTransaction * txToIn , unsigned int nInIn , const CAmount & amountIn , int nHashTypeIn ) : txTo ( txToIn ) , nIn ( nInIn ) , nHashType ( nHashTypeIn ) , amount ( amountIn ) , checker ( txTo , nIn , amountIn ) { }
2014-11-04 10:06:20 -08:00
2018-05-20 22:47:14 +02:00
bool MutableTransactionSignatureCreator : : CreateSig ( const SigningProvider & provider , std : : vector < unsigned char > & vchSig , const CKeyID & address , const CScript & scriptCode , SigVersion sigversion ) const
2014-08-27 17:22:33 +02:00
{
CKey key ;
2018-03-27 13:15:10 -07:00
if ( ! provider . GetKey ( address , key ) )
2014-08-27 17:22:33 +02:00
return false ;
2016-10-02 01:19:33 +08:00
// Signing with uncompressed keys is disabled in witness scripts
2018-03-09 15:03:40 +01:00
if ( sigversion = = SigVersion : : WITNESS_V0 & & ! key . IsCompressed ( ) )
2016-10-02 01:19:33 +08:00
return false ;
2016-03-31 14:54:58 +02:00
uint256 hash = SignatureHash ( scriptCode , * txTo , nIn , nHashType , amount , sigversion ) ;
2014-08-27 17:22:33 +02:00
if ( ! key . Sign ( hash , vchSig ) )
return false ;
vchSig . push_back ( ( unsigned char ) nHashType ) ;
2014-11-04 10:06:20 -08:00
return true ;
}
2014-08-27 17:22:33 +02:00
2018-06-07 21:12:25 -07:00
static bool GetCScript ( const SigningProvider & provider , const SignatureData & sigdata , const CScriptID & scriptid , CScript & script )
{
if ( provider . GetCScript ( scriptid , script ) ) {
return true ;
}
// Look for scripts in SignatureData
if ( CScriptID ( sigdata . redeem_script ) = = scriptid ) {
script = sigdata . redeem_script ;
return true ;
} else if ( CScriptID ( sigdata . witness_script ) = = scriptid ) {
script = sigdata . witness_script ;
return true ;
}
return false ;
}
2018-11-13 10:42:10 -05:00
static bool GetPubKey ( const SigningProvider & provider , const SignatureData & sigdata , const CKeyID & address , CPubKey & pubkey )
2018-06-07 21:12:25 -07:00
{
// Look for pubkey in all partial sigs
const auto it = sigdata . signatures . find ( address ) ;
if ( it ! = sigdata . signatures . end ( ) ) {
pubkey = it - > second . first ;
return true ;
}
2018-06-27 16:56:30 -07:00
// Look for pubkey in pubkey list
const auto & pk_it = sigdata . misc_pubkeys . find ( address ) ;
if ( pk_it ! = sigdata . misc_pubkeys . end ( ) ) {
2018-07-19 18:47:24 -07:00
pubkey = pk_it - > second . first ;
return true ;
}
// Query the underlying provider
2018-11-07 10:50:32 -05:00
return provider . GetPubKey ( address , pubkey ) ;
2018-06-07 21:12:25 -07:00
}
2018-10-06 03:33:24 -07:00
static bool CreateSig ( const BaseSignatureCreator & creator , SignatureData & sigdata , const SigningProvider & provider , std : : vector < unsigned char > & sig_out , const CPubKey & pubkey , const CScript & scriptcode , SigVersion sigversion )
2018-06-07 21:12:25 -07:00
{
2018-10-06 03:33:24 -07:00
CKeyID keyid = pubkey . GetID ( ) ;
2018-06-07 21:12:25 -07:00
const auto it = sigdata . signatures . find ( keyid ) ;
if ( it ! = sigdata . signatures . end ( ) ) {
sig_out = it - > second . second ;
return true ;
}
2018-10-06 03:33:24 -07:00
KeyOriginInfo info ;
if ( provider . GetKeyOrigin ( keyid , info ) ) {
sigdata . misc_pubkeys . emplace ( keyid , std : : make_pair ( pubkey , std : : move ( info ) ) ) ;
}
2018-06-07 21:12:25 -07:00
if ( creator . CreateSig ( provider , sig_out , keyid , scriptcode , sigversion ) ) {
auto i = sigdata . signatures . emplace ( keyid , SigPair ( pubkey , sig_out ) ) ;
assert ( i . second ) ;
return true ;
}
return false ;
}
2014-11-10 14:40:01 +08:00
/**
2014-11-04 10:06:20 -08:00
* Sign scriptPubKey using signature made with creator .
2014-11-10 14:40:01 +08:00
* Signatures are returned in scriptSigRet ( or returns false if scriptPubKey can ' t be signed ) ,
* unless whichTypeRet is TX_SCRIPTHASH , in which case scriptSigRet is the redemption script .
* Returns false if scriptPubKey could not be completely satisfied .
*/
2018-03-27 13:15:10 -07:00
static bool SignStep ( const SigningProvider & provider , const BaseSignatureCreator & creator , const CScript & scriptPubKey ,
2018-06-07 21:12:25 -07:00
std : : vector < valtype > & ret , txnouttype & whichTypeRet , SigVersion sigversion , SignatureData & sigdata )
2014-08-27 17:22:33 +02:00
{
2016-03-31 14:54:58 +02:00
CScript scriptRet ;
uint160 h160 ;
ret . clear ( ) ;
2018-06-09 12:06:25 -07:00
std : : vector < unsigned char > sig ;
2014-08-27 17:22:33 +02:00
2017-01-04 14:42:14 +09:00
std : : vector < valtype > vSolutions ;
2018-06-10 22:19:07 -07:00
whichTypeRet = Solver ( scriptPubKey , vSolutions ) ;
2014-08-27 17:22:33 +02:00
switch ( whichTypeRet )
{
case TX_NONSTANDARD :
case TX_NULL_DATA :
2017-08-25 19:55:52 -07:00
case TX_WITNESS_UNKNOWN :
2014-08-27 17:22:33 +02:00
return false ;
case TX_PUBKEY :
2018-10-06 03:33:24 -07:00
if ( ! CreateSig ( creator , sigdata , provider , sig , CPubKey ( vSolutions [ 0 ] ) , scriptPubKey , sigversion ) ) return false ;
2018-06-09 12:06:25 -07:00
ret . push_back ( std : : move ( sig ) ) ;
2014-08-27 17:22:33 +02:00
return true ;
2018-06-09 12:06:25 -07:00
case TX_PUBKEYHASH : {
CKeyID keyID = CKeyID ( uint160 ( vSolutions [ 0 ] ) ) ;
CPubKey pubkey ;
2018-11-08 10:08:46 -05:00
if ( ! GetPubKey ( provider , sigdata , keyID , pubkey ) ) return false ;
2018-10-06 03:33:24 -07:00
if ( ! CreateSig ( creator , sigdata , provider , sig , pubkey , scriptPubKey , sigversion ) ) return false ;
ret . push_back ( std : : move ( sig ) ) ;
2018-06-09 12:06:25 -07:00
ret . push_back ( ToByteVector ( pubkey ) ) ;
return true ;
}
2014-08-27 17:22:33 +02:00
case TX_SCRIPTHASH :
2018-06-07 21:12:25 -07:00
if ( GetCScript ( provider , sigdata , uint160 ( vSolutions [ 0 ] ) , scriptRet ) ) {
2016-03-31 14:54:58 +02:00
ret . push_back ( std : : vector < unsigned char > ( scriptRet . begin ( ) , scriptRet . end ( ) ) ) ;
return true ;
}
return false ;
2014-08-27 17:22:33 +02:00
2018-06-09 12:06:25 -07:00
case TX_MULTISIG : {
size_t required = vSolutions . front ( ) [ 0 ] ;
2016-03-31 14:54:58 +02:00
ret . push_back ( valtype ( ) ) ; // workaround CHECKMULTISIG bug
2018-06-09 12:06:25 -07:00
for ( size_t i = 1 ; i < vSolutions . size ( ) - 1 ; + + i ) {
CPubKey pubkey = CPubKey ( vSolutions [ i ] ) ;
2018-10-06 03:33:24 -07:00
if ( ret . size ( ) < required + 1 & & CreateSig ( creator , sigdata , provider , sig , pubkey , scriptPubKey , sigversion ) ) {
2018-06-09 12:06:25 -07:00
ret . push_back ( std : : move ( sig ) ) ;
}
}
bool ok = ret . size ( ) = = required + 1 ;
for ( size_t i = 0 ; i + ret . size ( ) < required + 1 ; + + i ) {
ret . push_back ( valtype ( ) ) ;
}
return ok ;
}
2016-03-31 14:54:58 +02:00
case TX_WITNESS_V0_KEYHASH :
ret . push_back ( vSolutions [ 0 ] ) ;
return true ;
case TX_WITNESS_V0_SCRIPTHASH :
CRIPEMD160 ( ) . Write ( & vSolutions [ 0 ] [ 0 ] , vSolutions [ 0 ] . size ( ) ) . Finalize ( h160 . begin ( ) ) ;
2018-06-07 21:12:25 -07:00
if ( GetCScript ( provider , sigdata , h160 , scriptRet ) ) {
2016-03-31 14:54:58 +02:00
ret . push_back ( std : : vector < unsigned char > ( scriptRet . begin ( ) , scriptRet . end ( ) ) ) ;
return true ;
}
return false ;
default :
return false ;
2014-08-27 17:22:33 +02:00
}
}
2017-01-04 14:42:14 +09:00
static CScript PushAll ( const std : : vector < valtype > & values )
2014-08-27 17:22:33 +02:00
{
2016-03-31 14:54:58 +02:00
CScript result ;
2017-06-02 03:18:57 +02:00
for ( const valtype & v : values ) {
2016-03-31 14:54:58 +02:00
if ( v . size ( ) = = 0 ) {
result < < OP_0 ;
} else if ( v . size ( ) = = 1 & & v [ 0 ] > = 1 & & v [ 0 ] < = 16 ) {
result < < CScript : : EncodeOP_N ( v [ 0 ] ) ;
} else {
result < < v ;
}
}
return result ;
}
2018-03-27 13:15:10 -07:00
bool ProduceSignature ( const SigningProvider & provider , const BaseSignatureCreator & creator , const CScript & fromPubKey , SignatureData & sigdata )
2016-03-31 14:54:58 +02:00
{
2018-07-03 17:18:52 -07:00
if ( sigdata . complete ) return true ;
2016-03-31 14:54:58 +02:00
std : : vector < valtype > result ;
2014-08-27 17:22:33 +02:00
txnouttype whichType ;
2018-06-07 21:12:25 -07:00
bool solved = SignStep ( provider , creator , fromPubKey , result , whichType , SigVersion : : BASE , sigdata ) ;
2016-03-31 14:54:58 +02:00
bool P2SH = false ;
CScript subscript ;
sigdata . scriptWitness . stack . clear ( ) ;
2014-08-27 17:22:33 +02:00
2016-03-31 14:54:58 +02:00
if ( solved & & whichType = = TX_SCRIPTHASH )
2014-08-27 17:22:33 +02:00
{
2016-03-31 14:54:58 +02:00
// Solver returns the subscript that needs to be evaluated;
2014-08-27 17:22:33 +02:00
// the final scriptSig is the signatures from that
// and then the serialized subscript:
2017-12-13 15:16:23 -05:00
subscript = CScript ( result [ 0 ] . begin ( ) , result [ 0 ] . end ( ) ) ;
2018-06-07 21:12:25 -07:00
sigdata . redeem_script = subscript ;
solved = solved & & SignStep ( provider , creator , subscript , result , whichType , SigVersion : : BASE , sigdata ) & & whichType ! = TX_SCRIPTHASH ;
2016-03-31 14:54:58 +02:00
P2SH = true ;
}
2014-08-27 17:22:33 +02:00
2016-03-31 14:54:58 +02:00
if ( solved & & whichType = = TX_WITNESS_V0_KEYHASH )
{
CScript witnessscript ;
witnessscript < < OP_DUP < < OP_HASH160 < < ToByteVector ( result [ 0 ] ) < < OP_EQUALVERIFY < < OP_CHECKSIG ;
2014-08-27 17:22:33 +02:00
txnouttype subType ;
2018-06-07 21:12:25 -07:00
solved = solved & & SignStep ( provider , creator , witnessscript , result , subType , SigVersion : : WITNESS_V0 , sigdata ) ;
2016-03-31 14:54:58 +02:00
sigdata . scriptWitness . stack = result ;
2018-06-27 16:56:30 -07:00
sigdata . witness = true ;
2016-03-31 14:54:58 +02:00
result . clear ( ) ;
}
else if ( solved & & whichType = = TX_WITNESS_V0_SCRIPTHASH )
{
CScript witnessscript ( result [ 0 ] . begin ( ) , result [ 0 ] . end ( ) ) ;
2018-06-07 21:12:25 -07:00
sigdata . witness_script = witnessscript ;
2016-03-31 14:54:58 +02:00
txnouttype subType ;
2018-06-07 21:12:25 -07:00
solved = solved & & SignStep ( provider , creator , witnessscript , result , subType , SigVersion : : WITNESS_V0 , sigdata ) & & subType ! = TX_SCRIPTHASH & & subType ! = TX_WITNESS_V0_SCRIPTHASH & & subType ! = TX_WITNESS_V0_KEYHASH ;
2016-03-31 14:54:58 +02:00
result . push_back ( std : : vector < unsigned char > ( witnessscript . begin ( ) , witnessscript . end ( ) ) ) ;
sigdata . scriptWitness . stack = result ;
2018-06-27 16:56:30 -07:00
sigdata . witness = true ;
2016-03-31 14:54:58 +02:00
result . clear ( ) ;
2018-06-27 16:56:30 -07:00
} else if ( solved & & whichType = = TX_WITNESS_UNKNOWN ) {
sigdata . witness = true ;
2014-08-27 17:22:33 +02:00
}
2016-03-31 14:54:58 +02:00
if ( P2SH ) {
result . push_back ( std : : vector < unsigned char > ( subscript . begin ( ) , subscript . end ( ) ) ) ;
}
sigdata . scriptSig = PushAll ( result ) ;
2014-08-27 17:22:33 +02:00
// Test solution
2018-07-03 17:18:52 -07:00
sigdata . complete = solved & & VerifyScript ( sigdata . scriptSig , fromPubKey , & sigdata . scriptWitness , STANDARD_SCRIPT_VERIFY_FLAGS , creator . Checker ( ) ) ;
return sigdata . complete ;
}
2018-10-26 15:28:48 -07:00
bool PSBTInputSigned ( PSBTInput & input )
{
return ! input . final_script_sig . empty ( ) | | ! input . final_script_witness . IsNull ( ) ;
}
2018-10-26 15:30:50 -07:00
bool SignPSBTInput ( const SigningProvider & provider , PartiallySignedTransaction & psbt , int index , int sighash )
2018-06-28 18:56:34 -07:00
{
2018-10-26 15:30:50 -07:00
PSBTInput & input = psbt . inputs . at ( index ) ;
const CMutableTransaction & tx = * psbt . tx ;
2018-10-26 15:28:48 -07:00
if ( PSBTInputSigned ( input ) ) {
2018-06-28 18:56:34 -07:00
return true ;
}
// Fill SignatureData with input info
2018-07-20 00:23:32 -07:00
SignatureData sigdata ;
2018-06-28 18:56:34 -07:00
input . FillSignatureData ( sigdata ) ;
// Get UTXO
2018-08-08 14:18:55 -07:00
bool require_witness_sig = false ;
2018-06-28 18:56:34 -07:00
CTxOut utxo ;
Refactor PSBTInput signing to enforce invariant
Refactor the process of PSBTInput signing to enforce the invariant that
a PSBTInput always has _either_ a witness_utxo or a non_witness_utxo,
never both.
This simplifies the logic of SignPSBTInput slightly, since it no longer
has to deal with the "both" case. When calling it, we now give it, in
order of preference: (1) whichever of the utxo fields was already
present in the PSBT we received, or (2) if neither, the
non_witness_utxo field, which is just a copy of the input transaction,
which we get from the wallet.
SignPSBTInput no longer has to remove one of the two fields; instead, it
will check if we have a witness signature, and if so, it will replace
the non_witness_utxo with the witness_utxo (which is smaller, as it is
just a copy of the output being spent.)
Add PSBTInput::IsSane checks in two more places, which checks for
both utxo fields being present; we will now give an RPC error early on
if we are supplied such a malformed PSBT to fill in.
Also add a check to FillPSBT, to avoid touching any input that is
already signed. (This is now redundant, since we should no longer
potentially harm an already-signed input, but it's harmless.)
fixes #14473
2018-10-26 15:31:41 -07:00
// Verify input sanity, which checks that at most one of witness or non-witness utxos is provided.
if ( ! input . IsSane ( ) ) {
return false ;
}
2018-06-28 18:56:34 -07:00
if ( input . non_witness_utxo ) {
2018-08-08 14:18:55 -07:00
// If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
Refactor PSBTInput signing to enforce invariant
Refactor the process of PSBTInput signing to enforce the invariant that
a PSBTInput always has _either_ a witness_utxo or a non_witness_utxo,
never both.
This simplifies the logic of SignPSBTInput slightly, since it no longer
has to deal with the "both" case. When calling it, we now give it, in
order of preference: (1) whichever of the utxo fields was already
present in the PSBT we received, or (2) if neither, the
non_witness_utxo field, which is just a copy of the input transaction,
which we get from the wallet.
SignPSBTInput no longer has to remove one of the two fields; instead, it
will check if we have a witness signature, and if so, it will replace
the non_witness_utxo with the witness_utxo (which is smaller, as it is
just a copy of the output being spent.)
Add PSBTInput::IsSane checks in two more places, which checks for
both utxo fields being present; we will now give an RPC error early on
if we are supplied such a malformed PSBT to fill in.
Also add a check to FillPSBT, to avoid touching any input that is
already signed. (This is now redundant, since we should no longer
potentially harm an already-signed input, but it's harmless.)
fixes #14473
2018-10-26 15:31:41 -07:00
COutPoint prevout = tx . vin [ index ] . prevout ;
if ( input . non_witness_utxo - > GetHash ( ) ! = prevout . hash ) {
return false ;
}
utxo = input . non_witness_utxo - > vout [ prevout . n ] ;
2018-06-28 18:56:34 -07:00
} else if ( ! input . witness_utxo . IsNull ( ) ) {
utxo = input . witness_utxo ;
2018-08-08 14:18:55 -07:00
// 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 ;
2018-06-28 18:56:34 -07:00
} else {
return false ;
}
MutableTransactionSignatureCreator creator ( & tx , index , utxo . nValue , sighash ) ;
2018-08-08 14:18:55 -07:00
sigdata . witness = false ;
2018-06-28 18:56:34 -07:00
bool sig_complete = ProduceSignature ( provider , creator , utxo . scriptPubKey , sigdata ) ;
2018-08-08 14:18:55 -07:00
// Verify that a witness signature was produced in case one was required.
if ( require_witness_sig & & ! sigdata . witness ) return false ;
2018-06-28 18:56:34 -07:00
input . FromSignatureData ( sigdata ) ;
2018-07-20 00:23:32 -07:00
Refactor PSBTInput signing to enforce invariant
Refactor the process of PSBTInput signing to enforce the invariant that
a PSBTInput always has _either_ a witness_utxo or a non_witness_utxo,
never both.
This simplifies the logic of SignPSBTInput slightly, since it no longer
has to deal with the "both" case. When calling it, we now give it, in
order of preference: (1) whichever of the utxo fields was already
present in the PSBT we received, or (2) if neither, the
non_witness_utxo field, which is just a copy of the input transaction,
which we get from the wallet.
SignPSBTInput no longer has to remove one of the two fields; instead, it
will check if we have a witness signature, and if so, it will replace
the non_witness_utxo with the witness_utxo (which is smaller, as it is
just a copy of the output being spent.)
Add PSBTInput::IsSane checks in two more places, which checks for
both utxo fields being present; we will now give an RPC error early on
if we are supplied such a malformed PSBT to fill in.
Also add a check to FillPSBT, to avoid touching any input that is
already signed. (This is now redundant, since we should no longer
potentially harm an already-signed input, but it's harmless.)
fixes #14473
2018-10-26 15:31:41 -07:00
// If we have a witness signature, use the smaller witness UTXO.
2018-09-11 00:13:19 -04:00
if ( sigdata . witness ) {
input . witness_utxo = utxo ;
Refactor PSBTInput signing to enforce invariant
Refactor the process of PSBTInput signing to enforce the invariant that
a PSBTInput always has _either_ a witness_utxo or a non_witness_utxo,
never both.
This simplifies the logic of SignPSBTInput slightly, since it no longer
has to deal with the "both" case. When calling it, we now give it, in
order of preference: (1) whichever of the utxo fields was already
present in the PSBT we received, or (2) if neither, the
non_witness_utxo field, which is just a copy of the input transaction,
which we get from the wallet.
SignPSBTInput no longer has to remove one of the two fields; instead, it
will check if we have a witness signature, and if so, it will replace
the non_witness_utxo with the witness_utxo (which is smaller, as it is
just a copy of the output being spent.)
Add PSBTInput::IsSane checks in two more places, which checks for
both utxo fields being present; we will now give an RPC error early on
if we are supplied such a malformed PSBT to fill in.
Also add a check to FillPSBT, to avoid touching any input that is
already signed. (This is now redundant, since we should no longer
potentially harm an already-signed input, but it's harmless.)
fixes #14473
2018-10-26 15:31:41 -07:00
input . non_witness_utxo = nullptr ;
2018-07-20 00:23:32 -07:00
}
2018-06-28 18:56:34 -07:00
return sig_complete ;
}
2018-07-03 17:18:52 -07:00
class SignatureExtractorChecker final : public BaseSignatureChecker
{
private :
SignatureData & sigdata ;
BaseSignatureChecker & checker ;
public :
SignatureExtractorChecker ( SignatureData & sigdata , BaseSignatureChecker & checker ) : sigdata ( sigdata ) , checker ( checker ) { }
bool CheckSig ( const std : : vector < unsigned char > & scriptSig , const std : : vector < unsigned char > & vchPubKey , const CScript & scriptCode , SigVersion sigversion ) const override ;
} ;
bool SignatureExtractorChecker : : CheckSig ( const std : : vector < unsigned char > & scriptSig , const std : : vector < unsigned char > & vchPubKey , const CScript & scriptCode , SigVersion sigversion ) const
{
if ( checker . CheckSig ( scriptSig , vchPubKey , scriptCode , sigversion ) ) {
CPubKey pubkey ( vchPubKey ) ;
sigdata . signatures . emplace ( pubkey . GetID ( ) , SigPair ( pubkey , scriptSig ) ) ;
return true ;
}
return false ;
}
namespace
{
struct Stacks
{
std : : vector < valtype > script ;
std : : vector < valtype > witness ;
2018-07-05 23:43:54 -04:00
Stacks ( ) = delete ;
Stacks ( const Stacks & ) = delete ;
2018-07-03 17:18:52 -07:00
explicit Stacks ( const SignatureData & data ) : witness ( data . scriptWitness . stack ) {
EvalScript ( script , data . scriptSig , SCRIPT_VERIFY_STRICTENC , BaseSignatureChecker ( ) , SigVersion : : BASE ) ;
}
} ;
2016-03-31 14:54:58 +02:00
}
2018-07-03 17:18:52 -07:00
// Extracts signatures and scripts from incomplete scriptSigs. Please do not extend this, use PSBT instead
SignatureData DataFromTransaction ( const CMutableTransaction & tx , unsigned int nIn , const CTxOut & txout )
2016-03-31 14:54:58 +02:00
{
SignatureData data ;
assert ( tx . vin . size ( ) > nIn ) ;
data . scriptSig = tx . vin [ nIn ] . scriptSig ;
2016-08-04 02:49:16 +02:00
data . scriptWitness = tx . vin [ nIn ] . scriptWitness ;
2018-07-03 17:18:52 -07:00
Stacks stack ( data ) ;
// Get signatures
MutableTransactionSignatureChecker tx_checker ( & tx , nIn , txout . nValue ) ;
SignatureExtractorChecker extractor_checker ( data , tx_checker ) ;
if ( VerifyScript ( data . scriptSig , txout . scriptPubKey , & data . scriptWitness , STANDARD_SCRIPT_VERIFY_FLAGS , extractor_checker ) ) {
data . complete = true ;
return data ;
}
// Get scripts
std : : vector < std : : vector < unsigned char > > solutions ;
2018-06-10 22:19:07 -07:00
txnouttype script_type = Solver ( txout . scriptPubKey , solutions ) ;
2018-07-03 17:18:52 -07:00
SigVersion sigversion = SigVersion : : BASE ;
CScript next_script = txout . scriptPubKey ;
if ( script_type = = TX_SCRIPTHASH & & ! stack . script . empty ( ) & & ! stack . script . back ( ) . empty ( ) ) {
// Get the redeemScript
CScript redeem_script ( stack . script . back ( ) . begin ( ) , stack . script . back ( ) . end ( ) ) ;
data . redeem_script = redeem_script ;
next_script = std : : move ( redeem_script ) ;
// Get redeemScript type
2018-06-10 22:19:07 -07:00
script_type = Solver ( next_script , solutions ) ;
2018-07-03 17:18:52 -07:00
stack . script . pop_back ( ) ;
}
if ( script_type = = TX_WITNESS_V0_SCRIPTHASH & & ! stack . witness . empty ( ) & & ! stack . witness . back ( ) . empty ( ) ) {
// Get the witnessScript
CScript witness_script ( stack . witness . back ( ) . begin ( ) , stack . witness . back ( ) . end ( ) ) ;
data . witness_script = witness_script ;
next_script = std : : move ( witness_script ) ;
// Get witnessScript type
2018-06-10 22:19:07 -07:00
script_type = Solver ( next_script , solutions ) ;
2018-07-03 17:18:52 -07:00
stack . witness . pop_back ( ) ;
stack . script = std : : move ( stack . witness ) ;
stack . witness . clear ( ) ;
sigversion = SigVersion : : WITNESS_V0 ;
}
if ( script_type = = TX_MULTISIG & & ! stack . script . empty ( ) ) {
// Build a map of pubkey -> signature by matching sigs to pubkeys:
assert ( solutions . size ( ) > 1 ) ;
unsigned int num_pubkeys = solutions . size ( ) - 2 ;
unsigned int last_success_key = 0 ;
for ( const valtype & sig : stack . script ) {
for ( unsigned int i = last_success_key ; i < num_pubkeys ; + + i ) {
const valtype & pubkey = solutions [ i + 1 ] ;
// We either have a signature for this pubkey, or we have found a signature and it is valid
if ( data . signatures . count ( CPubKey ( pubkey ) . GetID ( ) ) | | extractor_checker . CheckSig ( sig , pubkey , next_script , sigversion ) ) {
last_success_key = i + 1 ;
break ;
}
}
}
}
2016-03-31 14:54:58 +02:00
return data ;
}
2018-03-05 16:37:24 -05:00
void UpdateInput ( CTxIn & input , const SignatureData & data )
{
input . scriptSig = data . scriptSig ;
input . scriptWitness = data . scriptWitness ;
}
2018-06-07 21:12:25 -07:00
void SignatureData : : MergeSignatureData ( SignatureData sigdata )
{
if ( complete ) return ;
if ( sigdata . complete ) {
* this = std : : move ( sigdata ) ;
return ;
}
if ( redeem_script . empty ( ) & & ! sigdata . redeem_script . empty ( ) ) {
redeem_script = sigdata . redeem_script ;
}
if ( witness_script . empty ( ) & & ! sigdata . witness_script . empty ( ) ) {
witness_script = sigdata . witness_script ;
}
signatures . insert ( std : : make_move_iterator ( sigdata . signatures . begin ( ) ) , std : : make_move_iterator ( sigdata . signatures . end ( ) ) ) ;
}
2018-03-17 19:19:09 -07:00
bool SignSignature ( const SigningProvider & provider , const CScript & fromPubKey , CMutableTransaction & txTo , unsigned int nIn , const CAmount & amount , int nHashType )
2014-11-04 10:06:20 -08:00
{
assert ( nIn < txTo . vin . size ( ) ) ;
2018-05-20 22:47:14 +02:00
MutableTransactionSignatureCreator creator ( & txTo , nIn , amount , nHashType ) ;
2014-11-04 10:06:20 -08:00
2016-03-31 14:54:58 +02:00
SignatureData sigdata ;
2018-03-27 13:15:10 -07:00
bool ret = ProduceSignature ( provider , creator , fromPubKey , sigdata ) ;
2018-05-17 17:54:18 -07:00
UpdateInput ( txTo . vin . at ( nIn ) , sigdata ) ;
2016-03-31 14:54:58 +02:00
return ret ;
2014-08-27 17:22:33 +02:00
}
2018-03-17 19:19:09 -07:00
bool SignSignature ( const SigningProvider & provider , const CTransaction & txFrom , CMutableTransaction & txTo , unsigned int nIn , int nHashType )
2014-08-27 17:22:33 +02:00
{
assert ( nIn < txTo . vin . size ( ) ) ;
CTxIn & txin = txTo . vin [ nIn ] ;
assert ( txin . prevout . n < txFrom . vout . size ( ) ) ;
const CTxOut & txout = txFrom . vout [ txin . prevout . n ] ;
2018-03-17 19:19:09 -07:00
return SignSignature ( provider , txout . scriptPubKey , txTo , nIn , txout . nValue , nHashType ) ;
2014-08-27 17:22:33 +02:00
}
2015-05-01 15:21:06 +02:00
namespace {
/** Dummy signature checker which accepts all signatures. */
2018-03-27 13:34:39 -07:00
class DummySignatureChecker final : public BaseSignatureChecker
2015-05-01 15:21:06 +02:00
{
public :
DummySignatureChecker ( ) { }
2018-03-27 13:34:39 -07:00
bool CheckSig ( const std : : vector < unsigned char > & scriptSig , const std : : vector < unsigned char > & vchPubKey , const CScript & scriptCode , SigVersion sigversion ) const override { return true ; }
} ;
const DummySignatureChecker DUMMY_CHECKER ;
2015-05-01 15:21:06 +02:00
2018-03-27 13:34:39 -07:00
class DummySignatureCreator final : public BaseSignatureCreator {
2018-08-07 16:59:53 -07:00
private :
char m_r_len = 32 ;
char m_s_len = 32 ;
2018-03-27 13:34:39 -07:00
public :
2018-08-07 16:59:53 -07:00
DummySignatureCreator ( char r_len , char s_len ) : m_r_len ( r_len ) , m_s_len ( s_len ) { }
2018-03-27 13:34:39 -07:00
const BaseSignatureChecker & Checker ( ) const override { return DUMMY_CHECKER ; }
bool CreateSig ( const SigningProvider & provider , std : : vector < unsigned char > & vchSig , const CKeyID & keyid , const CScript & scriptCode , SigVersion sigversion ) const override
2015-05-01 15:21:06 +02:00
{
2018-03-27 13:34:39 -07:00
// Create a dummy signature that is a valid DER-encoding
2018-08-07 16:59:53 -07:00
vchSig . assign ( m_r_len + m_s_len + 7 , ' \000 ' ) ;
2018-03-27 13:34:39 -07:00
vchSig [ 0 ] = 0x30 ;
2018-08-07 16:59:53 -07:00
vchSig [ 1 ] = m_r_len + m_s_len + 4 ;
2018-03-27 13:34:39 -07:00
vchSig [ 2 ] = 0x02 ;
2018-08-07 16:59:53 -07:00
vchSig [ 3 ] = m_r_len ;
2018-03-27 13:34:39 -07:00
vchSig [ 4 ] = 0x01 ;
2018-08-07 16:59:53 -07:00
vchSig [ 4 + m_r_len ] = 0x02 ;
vchSig [ 5 + m_r_len ] = m_s_len ;
vchSig [ 6 + m_r_len ] = 0x01 ;
vchSig [ 6 + m_r_len + m_s_len ] = SIGHASH_ALL ;
2015-05-01 15:21:06 +02:00
return true ;
}
} ;
2018-07-12 15:03:55 -07:00
template < typename M , typename K , typename V >
bool LookupHelper ( const M & map , const K & key , V & value )
{
auto it = map . find ( key ) ;
if ( it ! = map . end ( ) ) {
value = it - > second ;
return true ;
}
return false ;
}
2015-05-01 15:21:06 +02:00
}
2018-08-07 16:59:53 -07:00
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR = DummySignatureCreator ( 32 , 32 ) ;
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR = DummySignatureCreator ( 33 , 32 ) ;
2018-06-07 21:12:25 -07:00
const SigningProvider & DUMMY_SIGNING_PROVIDER = SigningProvider ( ) ;
2017-11-30 16:48:31 -08:00
2018-03-17 19:19:09 -07:00
bool IsSolvable ( const SigningProvider & provider , const CScript & script )
2017-11-30 16:48:31 -08:00
{
// This check is to make sure that the script we created can actually be solved for and signed by us
// if we were to have the private keys. This is just to make sure that the script is valid and that,
// if found in a transaction, we would still accept and relay that transaction. In particular,
// it will reject witness outputs that require signing with an uncompressed public key.
SignatureData sigs ;
// Make sure that STANDARD_SCRIPT_VERIFY_FLAGS includes SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, the most
// important property this function is designed to test for.
static_assert ( STANDARD_SCRIPT_VERIFY_FLAGS & SCRIPT_VERIFY_WITNESS_PUBKEYTYPE , " IsSolvable requires standard script flags to include WITNESS_PUBKEYTYPE " ) ;
2018-03-27 13:34:39 -07:00
if ( ProduceSignature ( provider , DUMMY_SIGNATURE_CREATOR , script , sigs ) ) {
2017-11-30 16:48:31 -08:00
// VerifyScript check is just defensive, and should never fail.
2018-06-26 17:19:31 +02:00
bool verified = VerifyScript ( sigs . scriptSig , script , & sigs . scriptWitness , STANDARD_SCRIPT_VERIFY_FLAGS , DUMMY_CHECKER ) ;
assert ( verified ) ;
2017-11-30 16:48:31 -08:00
return true ;
}
return false ;
}
2018-06-27 16:53:48 -07:00
2018-12-09 22:03:07 -08:00
PartiallySignedTransaction : : PartiallySignedTransaction ( const CMutableTransaction & tx ) : tx ( tx )
2018-10-26 15:26:16 -07:00
{
inputs . resize ( tx . vin . size ( ) ) ;
outputs . resize ( tx . vout . size ( ) ) ;
}
2018-06-27 16:53:48 -07:00
bool PartiallySignedTransaction : : IsNull ( ) const
{
return ! tx & & inputs . empty ( ) & & outputs . empty ( ) & & unknown . empty ( ) ;
}
2018-06-27 16:58:01 -07:00
void PartiallySignedTransaction : : Merge ( const PartiallySignedTransaction & psbt )
{
for ( unsigned int i = 0 ; i < inputs . size ( ) ; + + i ) {
inputs [ i ] . Merge ( psbt . inputs [ i ] ) ;
}
for ( unsigned int i = 0 ; i < outputs . size ( ) ; + + i ) {
outputs [ i ] . Merge ( psbt . outputs [ i ] ) ;
}
2018-07-19 18:49:51 -07:00
unknown . insert ( psbt . unknown . begin ( ) , psbt . unknown . end ( ) ) ;
2018-06-27 16:58:01 -07:00
}
bool PartiallySignedTransaction : : IsSane ( ) const
{
for ( PSBTInput input : inputs ) {
if ( ! input . IsSane ( ) ) return false ;
}
return true ;
}
2018-06-27 16:53:48 -07:00
bool PSBTInput : : IsNull ( ) const
{
return ! non_witness_utxo & & witness_utxo . IsNull ( ) & & partial_sigs . empty ( ) & & unknown . empty ( ) & & hd_keypaths . empty ( ) & & redeem_script . empty ( ) & & witness_script . empty ( ) ;
}
2018-06-27 16:58:01 -07:00
void PSBTInput : : FillSignatureData ( SignatureData & sigdata ) const
{
if ( ! final_script_sig . empty ( ) ) {
sigdata . scriptSig = final_script_sig ;
sigdata . complete = true ;
}
if ( ! final_script_witness . IsNull ( ) ) {
sigdata . scriptWitness = final_script_witness ;
sigdata . complete = true ;
}
if ( sigdata . complete ) {
return ;
}
sigdata . signatures . insert ( partial_sigs . begin ( ) , partial_sigs . end ( ) ) ;
if ( ! redeem_script . empty ( ) ) {
sigdata . redeem_script = redeem_script ;
}
if ( ! witness_script . empty ( ) ) {
sigdata . witness_script = witness_script ;
}
for ( const auto & key_pair : hd_keypaths ) {
2018-07-19 18:47:24 -07:00
sigdata . misc_pubkeys . emplace ( key_pair . first . GetID ( ) , key_pair ) ;
2018-06-27 16:58:01 -07:00
}
}
void PSBTInput : : FromSignatureData ( const SignatureData & sigdata )
{
if ( sigdata . complete ) {
partial_sigs . clear ( ) ;
hd_keypaths . clear ( ) ;
redeem_script . clear ( ) ;
witness_script . clear ( ) ;
if ( ! sigdata . scriptSig . empty ( ) ) {
final_script_sig = sigdata . scriptSig ;
}
if ( ! sigdata . scriptWitness . IsNull ( ) ) {
final_script_witness = sigdata . scriptWitness ;
}
return ;
}
partial_sigs . insert ( sigdata . signatures . begin ( ) , sigdata . signatures . end ( ) ) ;
if ( redeem_script . empty ( ) & & ! sigdata . redeem_script . empty ( ) ) {
redeem_script = sigdata . redeem_script ;
}
if ( witness_script . empty ( ) & & ! sigdata . witness_script . empty ( ) ) {
witness_script = sigdata . witness_script ;
}
2018-07-19 18:47:24 -07:00
for ( const auto & entry : sigdata . misc_pubkeys ) {
hd_keypaths . emplace ( entry . second ) ;
}
2018-06-27 16:58:01 -07:00
}
void PSBTInput : : Merge ( const PSBTInput & input )
{
if ( ! non_witness_utxo & & input . non_witness_utxo ) non_witness_utxo = input . non_witness_utxo ;
if ( witness_utxo . IsNull ( ) & & ! input . witness_utxo . IsNull ( ) ) {
witness_utxo = input . witness_utxo ;
non_witness_utxo = nullptr ; // Clear out any non-witness utxo when we set a witness one.
}
partial_sigs . insert ( input . partial_sigs . begin ( ) , input . partial_sigs . end ( ) ) ;
hd_keypaths . insert ( input . hd_keypaths . begin ( ) , input . hd_keypaths . end ( ) ) ;
unknown . insert ( input . unknown . begin ( ) , input . unknown . end ( ) ) ;
if ( redeem_script . empty ( ) & & ! input . redeem_script . empty ( ) ) redeem_script = input . redeem_script ;
if ( witness_script . empty ( ) & & ! input . witness_script . empty ( ) ) witness_script = input . witness_script ;
if ( final_script_sig . empty ( ) & & ! input . final_script_sig . empty ( ) ) final_script_sig = input . final_script_sig ;
if ( final_script_witness . IsNull ( ) & & ! input . final_script_witness . IsNull ( ) ) final_script_witness = input . final_script_witness ;
}
bool PSBTInput : : IsSane ( ) const
{
// Cannot have both witness and non-witness utxos
if ( ! witness_utxo . IsNull ( ) & & non_witness_utxo ) return false ;
// If we have a witness_script or a scriptWitness, we must also have a witness utxo
if ( ! witness_script . empty ( ) & & witness_utxo . IsNull ( ) ) return false ;
if ( ! final_script_witness . IsNull ( ) & & witness_utxo . IsNull ( ) ) return false ;
return true ;
}
void PSBTOutput : : FillSignatureData ( SignatureData & sigdata ) const
{
if ( ! redeem_script . empty ( ) ) {
sigdata . redeem_script = redeem_script ;
}
if ( ! witness_script . empty ( ) ) {
sigdata . witness_script = witness_script ;
}
for ( const auto & key_pair : hd_keypaths ) {
2018-07-19 18:47:24 -07:00
sigdata . misc_pubkeys . emplace ( key_pair . first . GetID ( ) , key_pair ) ;
2018-06-27 16:58:01 -07:00
}
}
void PSBTOutput : : FromSignatureData ( const SignatureData & sigdata )
{
if ( redeem_script . empty ( ) & & ! sigdata . redeem_script . empty ( ) ) {
redeem_script = sigdata . redeem_script ;
}
if ( witness_script . empty ( ) & & ! sigdata . witness_script . empty ( ) ) {
witness_script = sigdata . witness_script ;
}
2018-07-19 18:47:24 -07:00
for ( const auto & entry : sigdata . misc_pubkeys ) {
hd_keypaths . emplace ( entry . second ) ;
}
2018-06-27 16:58:01 -07:00
}
2018-06-27 16:53:48 -07:00
bool PSBTOutput : : IsNull ( ) const
{
return redeem_script . empty ( ) & & witness_script . empty ( ) & & hd_keypaths . empty ( ) & & unknown . empty ( ) ;
}
2018-06-27 16:58:01 -07:00
void PSBTOutput : : Merge ( const PSBTOutput & output )
{
hd_keypaths . insert ( output . hd_keypaths . begin ( ) , output . hd_keypaths . end ( ) ) ;
unknown . insert ( output . unknown . begin ( ) , output . unknown . end ( ) ) ;
if ( redeem_script . empty ( ) & & ! output . redeem_script . empty ( ) ) redeem_script = output . redeem_script ;
if ( witness_script . empty ( ) & & ! output . witness_script . empty ( ) ) witness_script = output . witness_script ;
}
2018-06-28 19:05:05 -07:00
2018-07-20 00:04:02 -07:00
bool HidingSigningProvider : : GetCScript ( const CScriptID & scriptid , CScript & script ) const
2018-06-28 19:05:05 -07:00
{
return m_provider - > GetCScript ( scriptid , script ) ;
}
2018-07-20 00:04:02 -07:00
bool HidingSigningProvider : : GetPubKey ( const CKeyID & keyid , CPubKey & pubkey ) const
2018-06-28 19:05:05 -07:00
{
2018-07-19 17:25:21 -07:00
return m_provider - > GetPubKey ( keyid , pubkey ) ;
}
2018-07-20 00:04:02 -07:00
bool HidingSigningProvider : : GetKey ( const CKeyID & keyid , CKey & key ) const
2018-07-19 17:25:21 -07:00
{
2018-07-20 00:04:02 -07:00
if ( m_hide_secret ) return false ;
return m_provider - > GetKey ( keyid , key ) ;
}
bool HidingSigningProvider : : GetKeyOrigin ( const CKeyID & keyid , KeyOriginInfo & info ) const
2018-06-28 19:05:05 -07:00
{
2018-07-20 00:04:02 -07:00
if ( m_hide_origin ) return false ;
2018-07-19 17:25:21 -07:00
return m_provider - > GetKeyOrigin ( keyid , info ) ;
2018-06-28 19:05:05 -07:00
}
2018-07-12 15:03:55 -07:00
bool FlatSigningProvider : : GetCScript ( const CScriptID & scriptid , CScript & script ) const { return LookupHelper ( scripts , scriptid , script ) ; }
bool FlatSigningProvider : : GetPubKey ( const CKeyID & keyid , CPubKey & pubkey ) const { return LookupHelper ( pubkeys , keyid , pubkey ) ; }
2018-07-18 12:24:36 -07:00
bool FlatSigningProvider : : GetKeyOrigin ( const CKeyID & keyid , KeyOriginInfo & info ) const { return LookupHelper ( origins , keyid , info ) ; }
2018-07-12 15:03:55 -07:00
bool FlatSigningProvider : : GetKey ( const CKeyID & keyid , CKey & key ) const { return LookupHelper ( keys , keyid , key ) ; }
FlatSigningProvider Merge ( const FlatSigningProvider & a , const FlatSigningProvider & b )
{
FlatSigningProvider ret ;
ret . scripts = a . scripts ;
ret . scripts . insert ( b . scripts . begin ( ) , b . scripts . end ( ) ) ;
ret . pubkeys = a . pubkeys ;
ret . pubkeys . insert ( b . pubkeys . begin ( ) , b . pubkeys . end ( ) ) ;
ret . keys = a . keys ;
ret . keys . insert ( b . keys . begin ( ) , b . keys . end ( ) ) ;
2018-11-02 18:00:35 -07:00
ret . origins = a . origins ;
ret . origins . insert ( b . origins . begin ( ) , b . origins . end ( ) ) ;
2018-07-12 15:03:55 -07:00
return ret ;
}