@ -10,6 +10,7 @@
# include "keystore.h"
# include "keystore.h"
# include "main.h"
# include "main.h"
# include "script/script.h"
# include "script/script.h"
# include "script/script_error.h"
# include "script/sign.h"
# include "script/sign.h"
# include "util.h"
# include "util.h"
@ -92,7 +93,9 @@ CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CMu
void DoTest ( const CScript & scriptPubKey , const CScript & scriptSig , int flags , bool expect , const std : : string & message )
void DoTest ( const CScript & scriptPubKey , const CScript & scriptSig , int flags , bool expect , const std : : string & message )
{
{
BOOST_CHECK_MESSAGE ( VerifyScript ( scriptSig , scriptPubKey , flags , SignatureChecker ( BuildSpendingTransaction ( scriptSig , BuildCreditingTransaction ( scriptPubKey ) ) , 0 ) ) = = expect , message ) ;
ScriptError err ;
BOOST_CHECK_MESSAGE ( VerifyScript ( scriptSig , scriptPubKey , flags , SignatureChecker ( BuildSpendingTransaction ( scriptSig , BuildCreditingTransaction ( scriptPubKey ) ) , 0 ) , & err ) = = expect , message ) ;
BOOST_CHECK_MESSAGE ( expect = = ( err = = SCRIPT_ERR_OK ) , std : : string ( ScriptErrorString ( err ) ) + " : " + message ) ;
}
}
void static NegateSignatureS ( std : : vector < unsigned char > & vchSig ) {
void static NegateSignatureS ( std : : vector < unsigned char > & vchSig ) {
@ -590,20 +593,25 @@ BOOST_AUTO_TEST_CASE(script_PushData)
static const unsigned char pushdata2 [ ] = { OP_PUSHDATA2 , 1 , 0 , 0x5a } ;
static const unsigned char pushdata2 [ ] = { OP_PUSHDATA2 , 1 , 0 , 0x5a } ;
static const unsigned char pushdata4 [ ] = { OP_PUSHDATA4 , 1 , 0 , 0 , 0 , 0x5a } ;
static const unsigned char pushdata4 [ ] = { OP_PUSHDATA4 , 1 , 0 , 0 , 0 , 0x5a } ;
ScriptError err ;
vector < vector < unsigned char > > directStack ;
vector < vector < unsigned char > > directStack ;
BOOST_CHECK ( EvalScript ( directStack , CScript ( & direct [ 0 ] , & direct [ sizeof ( direct ) ] ) , true , BaseSignatureChecker ( ) ) ) ;
BOOST_CHECK ( EvalScript ( directStack , CScript ( & direct [ 0 ] , & direct [ sizeof ( direct ) ] ) , true , BaseSignatureChecker ( ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
vector < vector < unsigned char > > pushdata1Stack ;
vector < vector < unsigned char > > pushdata1Stack ;
BOOST_CHECK ( EvalScript ( pushdata1Stack , CScript ( & pushdata1 [ 0 ] , & pushdata1 [ sizeof ( pushdata1 ) ] ) , true , BaseSignatureChecker ( ) ) ) ;
BOOST_CHECK ( EvalScript ( pushdata1Stack , CScript ( & pushdata1 [ 0 ] , & pushdata1 [ sizeof ( pushdata1 ) ] ) , true , BaseSignatureChecker ( ) , & err ) ) ;
BOOST_CHECK ( pushdata1Stack = = directStack ) ;
BOOST_CHECK ( pushdata1Stack = = directStack ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
vector < vector < unsigned char > > pushdata2Stack ;
vector < vector < unsigned char > > pushdata2Stack ;
BOOST_CHECK ( EvalScript ( pushdata2Stack , CScript ( & pushdata2 [ 0 ] , & pushdata2 [ sizeof ( pushdata2 ) ] ) , true , BaseSignatureChecker ( ) ) ) ;
BOOST_CHECK ( EvalScript ( pushdata2Stack , CScript ( & pushdata2 [ 0 ] , & pushdata2 [ sizeof ( pushdata2 ) ] ) , true , BaseSignatureChecker ( ) , & err ) ) ;
BOOST_CHECK ( pushdata2Stack = = directStack ) ;
BOOST_CHECK ( pushdata2Stack = = directStack ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
vector < vector < unsigned char > > pushdata4Stack ;
vector < vector < unsigned char > > pushdata4Stack ;
BOOST_CHECK ( EvalScript ( pushdata4Stack , CScript ( & pushdata4 [ 0 ] , & pushdata4 [ sizeof ( pushdata4 ) ] ) , true , BaseSignatureChecker ( ) ) ) ;
BOOST_CHECK ( EvalScript ( pushdata4Stack , CScript ( & pushdata4 [ 0 ] , & pushdata4 [ sizeof ( pushdata4 ) ] ) , true , BaseSignatureChecker ( ) , & err ) ) ;
BOOST_CHECK ( pushdata4Stack = = directStack ) ;
BOOST_CHECK ( pushdata4Stack = = directStack ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
}
}
CScript
CScript
@ -640,6 +648,7 @@ sign_multisig(CScript scriptPubKey, const CKey &key, CTransaction transaction)
BOOST_AUTO_TEST_CASE ( script_CHECKMULTISIG12 )
BOOST_AUTO_TEST_CASE ( script_CHECKMULTISIG12 )
{
{
ScriptError err ;
CKey key1 , key2 , key3 ;
CKey key1 , key2 , key3 ;
key1 . MakeNewKey ( true ) ;
key1 . MakeNewKey ( true ) ;
key2 . MakeNewKey ( false ) ;
key2 . MakeNewKey ( false ) ;
@ -652,19 +661,24 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
CMutableTransaction txTo12 = BuildSpendingTransaction ( CScript ( ) , txFrom12 ) ;
CMutableTransaction txTo12 = BuildSpendingTransaction ( CScript ( ) , txFrom12 ) ;
CScript goodsig1 = sign_multisig ( scriptPubKey12 , key1 , txTo12 ) ;
CScript goodsig1 = sign_multisig ( scriptPubKey12 , key1 , txTo12 ) ;
BOOST_CHECK ( VerifyScript ( goodsig1 , scriptPubKey12 , flags , SignatureChecker ( txTo12 , 0 ) ) ) ;
BOOST_CHECK ( VerifyScript ( goodsig1 , scriptPubKey12 , flags , SignatureChecker ( txTo12 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
txTo12 . vout [ 0 ] . nValue = 2 ;
txTo12 . vout [ 0 ] . nValue = 2 ;
BOOST_CHECK ( ! VerifyScript ( goodsig1 , scriptPubKey12 , flags , SignatureChecker ( txTo12 , 0 ) ) ) ;
BOOST_CHECK ( ! VerifyScript ( goodsig1 , scriptPubKey12 , flags , SignatureChecker ( txTo12 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_EVAL_FALSE , ScriptErrorString ( err ) ) ;
CScript goodsig2 = sign_multisig ( scriptPubKey12 , key2 , txTo12 ) ;
CScript goodsig2 = sign_multisig ( scriptPubKey12 , key2 , txTo12 ) ;
BOOST_CHECK ( VerifyScript ( goodsig2 , scriptPubKey12 , flags , SignatureChecker ( txTo12 , 0 ) ) ) ;
BOOST_CHECK ( VerifyScript ( goodsig2 , scriptPubKey12 , flags , SignatureChecker ( txTo12 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
CScript badsig1 = sign_multisig ( scriptPubKey12 , key3 , txTo12 ) ;
CScript badsig1 = sign_multisig ( scriptPubKey12 , key3 , txTo12 ) ;
BOOST_CHECK ( ! VerifyScript ( badsig1 , scriptPubKey12 , flags , SignatureChecker ( txTo12 , 0 ) ) ) ;
BOOST_CHECK ( ! VerifyScript ( badsig1 , scriptPubKey12 , flags , SignatureChecker ( txTo12 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_EVAL_FALSE , ScriptErrorString ( err ) ) ;
}
}
BOOST_AUTO_TEST_CASE ( script_CHECKMULTISIG23 )
BOOST_AUTO_TEST_CASE ( script_CHECKMULTISIG23 )
{
{
ScriptError err ;
CKey key1 , key2 , key3 , key4 ;
CKey key1 , key2 , key3 , key4 ;
key1 . MakeNewKey ( true ) ;
key1 . MakeNewKey ( true ) ;
key2 . MakeNewKey ( false ) ;
key2 . MakeNewKey ( false ) ;
@ -680,46 +694,55 @@ BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
std : : vector < CKey > keys ;
std : : vector < CKey > keys ;
keys . push_back ( key1 ) ; keys . push_back ( key2 ) ;
keys . push_back ( key1 ) ; keys . push_back ( key2 ) ;
CScript goodsig1 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
CScript goodsig1 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
BOOST_CHECK ( VerifyScript ( goodsig1 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) ) ) ;
BOOST_CHECK ( VerifyScript ( goodsig1 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
keys . clear ( ) ;
keys . clear ( ) ;
keys . push_back ( key1 ) ; keys . push_back ( key3 ) ;
keys . push_back ( key1 ) ; keys . push_back ( key3 ) ;
CScript goodsig2 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
CScript goodsig2 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
BOOST_CHECK ( VerifyScript ( goodsig2 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) ) ) ;
BOOST_CHECK ( VerifyScript ( goodsig2 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
keys . clear ( ) ;
keys . clear ( ) ;
keys . push_back ( key2 ) ; keys . push_back ( key3 ) ;
keys . push_back ( key2 ) ; keys . push_back ( key3 ) ;
CScript goodsig3 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
CScript goodsig3 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
BOOST_CHECK ( VerifyScript ( goodsig3 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) ) ) ;
BOOST_CHECK ( VerifyScript ( goodsig3 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
keys . clear ( ) ;
keys . clear ( ) ;
keys . push_back ( key2 ) ; keys . push_back ( key2 ) ; // Can't re-use sig
keys . push_back ( key2 ) ; keys . push_back ( key2 ) ; // Can't re-use sig
CScript badsig1 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
CScript badsig1 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
BOOST_CHECK ( ! VerifyScript ( badsig1 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) ) ) ;
BOOST_CHECK ( ! VerifyScript ( badsig1 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_EVAL_FALSE , ScriptErrorString ( err ) ) ;
keys . clear ( ) ;
keys . clear ( ) ;
keys . push_back ( key2 ) ; keys . push_back ( key1 ) ; // sigs must be in correct order
keys . push_back ( key2 ) ; keys . push_back ( key1 ) ; // sigs must be in correct order
CScript badsig2 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
CScript badsig2 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
BOOST_CHECK ( ! VerifyScript ( badsig2 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) ) ) ;
BOOST_CHECK ( ! VerifyScript ( badsig2 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_EVAL_FALSE , ScriptErrorString ( err ) ) ;
keys . clear ( ) ;
keys . clear ( ) ;
keys . push_back ( key3 ) ; keys . push_back ( key2 ) ; // sigs must be in correct order
keys . push_back ( key3 ) ; keys . push_back ( key2 ) ; // sigs must be in correct order
CScript badsig3 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
CScript badsig3 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
BOOST_CHECK ( ! VerifyScript ( badsig3 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) ) ) ;
BOOST_CHECK ( ! VerifyScript ( badsig3 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_EVAL_FALSE , ScriptErrorString ( err ) ) ;
keys . clear ( ) ;
keys . clear ( ) ;
keys . push_back ( key4 ) ; keys . push_back ( key2 ) ; // sigs must match pubkeys
keys . push_back ( key4 ) ; keys . push_back ( key2 ) ; // sigs must match pubkeys
CScript badsig4 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
CScript badsig4 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
BOOST_CHECK ( ! VerifyScript ( badsig4 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) ) ) ;
BOOST_CHECK ( ! VerifyScript ( badsig4 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_EVAL_FALSE , ScriptErrorString ( err ) ) ;
keys . clear ( ) ;
keys . clear ( ) ;
keys . push_back ( key1 ) ; keys . push_back ( key4 ) ; // sigs must match pubkeys
keys . push_back ( key1 ) ; keys . push_back ( key4 ) ; // sigs must match pubkeys
CScript badsig5 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
CScript badsig5 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
BOOST_CHECK ( ! VerifyScript ( badsig5 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) ) ) ;
BOOST_CHECK ( ! VerifyScript ( badsig5 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_EVAL_FALSE , ScriptErrorString ( err ) ) ;
keys . clear ( ) ; // Must have signatures
keys . clear ( ) ; // Must have signatures
CScript badsig6 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
CScript badsig6 = sign_multisig ( scriptPubKey23 , keys , txTo23 ) ;
BOOST_CHECK ( ! VerifyScript ( badsig6 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) ) ) ;
BOOST_CHECK ( ! VerifyScript ( badsig6 , scriptPubKey23 , flags , SignatureChecker ( txTo23 , 0 ) , & err ) ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_INVALID_STACK_OPERATION , ScriptErrorString ( err ) ) ;
}
}
BOOST_AUTO_TEST_CASE ( script_combineSigs )
BOOST_AUTO_TEST_CASE ( script_combineSigs )
@ -833,11 +856,13 @@ BOOST_AUTO_TEST_CASE(script_combineSigs)
BOOST_AUTO_TEST_CASE ( script_standard_push )
BOOST_AUTO_TEST_CASE ( script_standard_push )
{
{
ScriptError err ;
for ( int i = 0 ; i < 67000 ; i + + ) {
for ( int i = 0 ; i < 67000 ; i + + ) {
CScript script ;
CScript script ;
script < < i ;
script < < i ;
BOOST_CHECK_MESSAGE ( script . IsPushOnly ( ) , " Number " < < i < < " is not pure push. " ) ;
BOOST_CHECK_MESSAGE ( script . IsPushOnly ( ) , " Number " < < i < < " is not pure push. " ) ;
BOOST_CHECK_MESSAGE ( VerifyScript ( script , CScript ( ) < < OP_1 , SCRIPT_VERIFY_MINIMALDATA , BaseSignatureChecker ( ) ) , " Number " < < i < < " push is not minimal data. " ) ;
BOOST_CHECK_MESSAGE ( VerifyScript ( script , CScript ( ) < < OP_1 , SCRIPT_VERIFY_MINIMALDATA , BaseSignatureChecker ( ) , & err ) , " Number " < < i < < " push is not minimal data. " ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
}
}
for ( unsigned int i = 0 ; i < = MAX_SCRIPT_ELEMENT_SIZE ; i + + ) {
for ( unsigned int i = 0 ; i < = MAX_SCRIPT_ELEMENT_SIZE ; i + + ) {
@ -845,7 +870,8 @@ BOOST_AUTO_TEST_CASE(script_standard_push)
CScript script ;
CScript script ;
script < < data ;
script < < data ;
BOOST_CHECK_MESSAGE ( script . IsPushOnly ( ) , " Length " < < i < < " is not pure push. " ) ;
BOOST_CHECK_MESSAGE ( script . IsPushOnly ( ) , " Length " < < i < < " is not pure push. " ) ;
BOOST_CHECK_MESSAGE ( VerifyScript ( script , CScript ( ) < < OP_1 , SCRIPT_VERIFY_MINIMALDATA , BaseSignatureChecker ( ) ) , " Length " < < i < < " push is not minimal data. " ) ;
BOOST_CHECK_MESSAGE ( VerifyScript ( script , CScript ( ) < < OP_1 , SCRIPT_VERIFY_MINIMALDATA , BaseSignatureChecker ( ) , & err ) , " Length " < < i < < " push is not minimal data. " ) ;
BOOST_CHECK_MESSAGE ( err = = SCRIPT_ERR_OK , ScriptErrorString ( err ) ) ;
}
}
}
}