Merge pull request #4381 from chimp1984/add-input-verification

Add input validation
This commit is contained in:
sqrrm 2020-07-28 14:58:06 +02:00 committed by GitHub
commit 901af075c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -29,6 +29,7 @@ import org.bitcoinj.core.Coin;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionInput;
import org.bitcoinj.core.TransactionOutPoint;
import org.bitcoinj.core.TransactionOutput;
import java.util.List;
@ -70,6 +71,12 @@ public class DelayedPayoutTxValidation {
}
}
public static class InvalidInputException extends Exception {
InvalidInputException(String msg) {
super(msg);
}
}
public static void validatePayoutTx(Trade trade,
Transaction delayedPayoutTx,
DaoFacade daoFacade,
@ -184,4 +191,19 @@ public class DelayedPayoutTxValidation {
throw new DonationAddressException(errorMsg);
}
}
public static void validatePayoutTxInput(Transaction depositTx,
Transaction delayedPayoutTx)
throws InvalidInputException {
TransactionInput input = delayedPayoutTx.getInput(0);
checkNotNull(input, "delayedPayoutTx.getInput(0) must not be null");
// input.getConnectedOutput() is null as the tx is not committed at that point
TransactionOutPoint outpoint = input.getOutpoint();
if (!outpoint.getHash().toString().equals(depositTx.getHashAsString()) || outpoint.getIndex() != 0) {
throw new InvalidInputException("Input of delayed payout transaction does not point to output of deposit tx.\n" +
"Delayed payout tx=" + delayedPayoutTx + "\n" +
"Deposit tx=" + depositTx);
}
}
}

View File

@ -27,6 +27,8 @@ import org.bitcoinj.core.Transaction;
import lombok.extern.slf4j.Slf4j;
import static com.google.common.base.Preconditions.checkNotNull;
@Slf4j
public class BuyerVerifiesFinalDelayedPayoutTx extends TradeTask {
@SuppressWarnings({"unused"})
@ -40,18 +42,25 @@ public class BuyerVerifiesFinalDelayedPayoutTx extends TradeTask {
runInterceptHook();
Transaction delayedPayoutTx = trade.getDelayedPayoutTx();
checkNotNull(delayedPayoutTx, "trade.getDelayedPayoutTx() must not be null");
// Check again tx
DelayedPayoutTxValidation.validatePayoutTx(trade,
delayedPayoutTx,
processModel.getDaoFacade(),
processModel.getBtcWalletService());
// Now as we know the deposit tx we can also verify the input
Transaction depositTx = trade.getDepositTx();
checkNotNull(depositTx, "trade.getDepositTx() must not be null");
DelayedPayoutTxValidation.validatePayoutTxInput(depositTx, delayedPayoutTx);
complete();
} catch (DelayedPayoutTxValidation.DonationAddressException |
DelayedPayoutTxValidation.MissingDelayedPayoutTxException |
DelayedPayoutTxValidation.InvalidTxException |
DelayedPayoutTxValidation.InvalidLockTimeException |
DelayedPayoutTxValidation.AmountMismatchException e) {
DelayedPayoutTxValidation.AmountMismatchException |
DelayedPayoutTxValidation.InvalidInputException e) {
failed(e.getMessage());
} catch (Throwable t) {
failed(t);