Skip to content

Commit

Permalink
Mandatoy to field for 7702 transactions (hyperledger#7600)
Browse files Browse the repository at this point in the history
* fix 7702 tx validation in tx pool

Signed-off-by: Daniel Lehrner <daniel.lehrner@consensys.net>

* check that 7702 transactions always have a mandatory to field

Signed-off-by: Daniel Lehrner <daniel.lehrner@consensys.net>

---------

Signed-off-by: Daniel Lehrner <daniel.lehrner@consensys.net>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
  • Loading branch information
daniellehrner and macfarla authored Sep 10, 2024
1 parent 07adb41 commit 3d71254
Showing 1 changed file with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,42 +132,59 @@ public ValidationResult<TransactionInvalidReason> validate(
}

if (transactionType == TransactionType.DELEGATE_CODE) {
if (isDelegateCodeEmpty(transaction)) {
return ValidationResult.invalid(
TransactionInvalidReason.EMPTY_CODE_DELEGATION,
"transaction code delegation transactions must have a non-empty code delegation list");
ValidationResult<TransactionInvalidReason> codeDelegationValidation =
validateCodeDelegation(transaction);
if (!codeDelegationValidation.isValid()) {
return codeDelegationValidation;
}
}

return validateCostAndFee(transaction, baseFee, blobFee, transactionValidationParams);
}

final BigInteger halfCurveOrder = SignatureAlgorithmFactory.getInstance().getHalfCurveOrder();
final Optional<ValidationResult<TransactionInvalidReason>> validationResult =
transaction
.getCodeDelegationList()
.map(
codeDelegations -> {
for (CodeDelegation codeDelegation : codeDelegations) {
if (codeDelegation.signature().getS().compareTo(halfCurveOrder) > 0) {
return ValidationResult.invalid(
TransactionInvalidReason.INVALID_SIGNATURE,
"Invalid signature for code delegation. S value must be less or equal than the half curve order.");
}

if (codeDelegation.signature().getRecId() != 0
&& codeDelegation.signature().getRecId() != 1) {
return ValidationResult.invalid(
TransactionInvalidReason.INVALID_SIGNATURE,
"Invalid signature for code delegation. RecId value must be 0 or 1.");
}
private static ValidationResult<TransactionInvalidReason> validateCodeDelegation(
final Transaction transaction) {
if (isDelegateCodeEmpty(transaction)) {
return ValidationResult.invalid(
TransactionInvalidReason.EMPTY_CODE_DELEGATION,
"transaction code delegation transactions must have a non-empty code delegation list");
}

if (transaction.getTo().isEmpty()) {
return ValidationResult.invalid(
TransactionInvalidReason.INVALID_TRANSACTION_FORMAT,
"transaction code delegation transactions must have a to address");
}

final BigInteger halfCurveOrder = SignatureAlgorithmFactory.getInstance().getHalfCurveOrder();
final Optional<ValidationResult<TransactionInvalidReason>> validationResult =
transaction
.getCodeDelegationList()
.map(
codeDelegations -> {
for (CodeDelegation codeDelegation : codeDelegations) {
if (codeDelegation.signature().getS().compareTo(halfCurveOrder) > 0) {
return ValidationResult.invalid(
TransactionInvalidReason.INVALID_SIGNATURE,
"Invalid signature for code delegation. S value must be less or equal than the half curve order.");
}

return ValidationResult.valid();
});
if (codeDelegation.signature().getRecId() != 0
&& codeDelegation.signature().getRecId() != 1) {
return ValidationResult.invalid(
TransactionInvalidReason.INVALID_SIGNATURE,
"Invalid signature for code delegation. RecId value must be 0 or 1.");
}
}

if (validationResult.isPresent() && !validationResult.get().isValid()) {
return validationResult.get();
}
return ValidationResult.valid();
});

if (validationResult.isPresent() && !validationResult.get().isValid()) {
return validationResult.get();
}

return validateCostAndFee(transaction, baseFee, blobFee, transactionValidationParams);
return ValidationResult.valid();
}

private static boolean isDelegateCodeEmpty(final Transaction transaction) {
Expand Down

0 comments on commit 3d71254

Please sign in to comment.