public void correctlySpends(Transaction txContainingThis, int scriptSigIndex, @Nullable TransactionWitness witness, @Nullable Coin value,
            Script scriptPubKey, Set<VerifyFlag> verifyFlags) throws ScriptException {
        if (ScriptPattern.isP2WPKH(scriptPubKey)) {
            // For SegWit, full validation isn't implemented. So we simply check the signature. P2SH_P2WPKH is handled
            // by the P2SH code for now.
            if (witness.getPushCount() < 2)
                throw new ScriptException(ScriptError.SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY, witness.toString());
            TransactionSignature signature;
            try {
                signature = TransactionSignature.decodeFromBitcoin(witness.getPush(0), true, true);
            } catch (SignatureDecodeException x) {
                throw new ScriptException(ScriptError.SCRIPT_ERR_SIG_DER, "Cannot decode", x);
            }
            ECKey pubkey = ECKey.fromPublicOnly(witness.getPush(1));
            Script scriptCode = new ScriptBuilder().data(ScriptBuilder.createP2PKHOutputScript(pubkey).getProgram())
                    .build();
            Sha256Hash sigHash = txContainingThis.hashForWitnessSignature(scriptSigIndex, scriptCode, value,
                    signature.sigHashMode(), false);
            boolean validSig = pubkey.verify(sigHash, signature);
            if (!validSig)
                throw new ScriptException(ScriptError.SCRIPT_ERR_CHECKSIGVERIFY, "Invalid signature");
        } else {
            correctlySpends(txContainingThis, scriptSigIndex, scriptPubKey, verifyFlags);
        }
    }
public void correctlySpends ( Transaction txContainingThis , int scriptSigIndex , @ Nullable TransactionWitness witness , @ Nullable Coin value , Script scriptPubKey , Set < VerifyFlag > verifyFlags ) throws ScriptException { if ( ScriptPattern . isP2WPKH ( scriptPubKey ) ) { if ( witness . getPushCount ( ) < 2 ) throw new ScriptException ( ScriptError . SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY , witness . toString ( ) ) ; TransactionSignature signature ; try { signature = TransactionSignature . decodeFromBitcoin ( witness . getPush ( 0 ) , true , true ) ; } catch ( SignatureDecodeException x ) { throw new ScriptException ( ScriptError . SCRIPT_ERR_SIG_DER , "Cannot decode" , x ) ; } ECKey pubkey = ECKey . fromPublicOnly ( witness . getPush ( 1 ) ) ; Script scriptCode = new ScriptBuilder ( ) . data ( ScriptBuilder . createP2PKHOutputScript ( pubkey ) . getProgram ( ) ) . build ( ) ; Sha256Hash sigHash = txContainingThis . hashForWitnessSignature ( scriptSigIndex , scriptCode , value , signature . sigHashMode ( ) , false ) ; boolean validSig = pubkey . verify ( sigHash , signature ) ; if ( ! validSig ) throw new ScriptException ( ScriptError . SCRIPT_ERR_CHECKSIGVERIFY , "Invalid signature" ) ; } else { correctlySpends ( txContainingThis , scriptSigIndex , scriptPubKey , verifyFlags ) ; } }
Verifies that this script (interpreted as a scriptSig) correctly spends the given scriptPubKey.
@param txContainingThis The transaction in which this input scriptSig resides.
Accessing txContainingThis from another thread while this method runs results in undefined behavior.
@param scriptSigIndex The index in txContainingThis of the scriptSig (note: NOT the index of the scriptPubKey).
@param scriptPubKey The connected scriptPubKey containing the conditions needed to claim the value.
@param witness Transaction witness belonging to the transaction input containing this script. Needed for SegWit.
@param value Value of the output. Needed for SegWit scripts.
@param verifyFlags Each flag enables one validation rule.