Skip to content

Commit e3faec3

Browse files
authored
Merge pull request #421 from bane-labs/ensure-decrypted-hash
2 parents ec31e5d + b37daa5 commit e3faec3

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

antimev/envelope.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
minEncryptedDataSize = EncryptedDataPrefixLen + EncryptedDataRoundLen + EncryptedDataHashLen + tpke.CipherTextSize + 105 + (aes.BlockSize - 105%aes.BlockSize)
3838
)
3939

40-
// isEnvelope checks whether a transaction is an Envelope transaction. The criteria
40+
// IsEnvelope checks whether a transaction is an Envelope transaction. The criteria
4141
// include receiver's address, data prefix and data length check.
4242
func IsEnvelope(tx *types.Transaction) bool {
4343
if tx.To() == nil || *(tx.To()) != systemcontracts.GovernanceRewardProxyHash {
@@ -51,3 +51,10 @@ func IsEnvelope(tx *types.Transaction) bool {
5151

5252
return true
5353
}
54+
55+
// GetEncryptedHash returns the hash of inner encrypted transaction specified in an
56+
// unencrypted part of Envelope data. Passing non-Envelope as an argument is a no-op.
57+
func GetEncryptedHash(envelope *types.Transaction) common.Hash {
58+
hashOffset := EncryptedDataPrefixLen + EncryptedDataRoundLen
59+
return common.Hash(envelope.Data()[hashOffset : hashOffset+EncryptedDataHashLen])
60+
}

consensus/dbft/dbft.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,11 +1237,14 @@ func (c *DBFT) validateDecryptedTx(head *types.Header, decryptedTx *types.Transa
12371237
if decryptedTx.Nonce() != envelope.Nonce() {
12381238
return fmt.Errorf("decryptedTx nonce mismatch: decryptedNonce %v, envelopeNonce %v", decryptedTx.Nonce(), envelope.Nonce())
12391239
}
1240+
12401241
// Ensure the gasprice is high enough to replace the envelope transaction
12411242
baseFee := head.BaseFee
12421243
if decryptedTx.EffectiveGasTipCmp(envelope, baseFee) < 0 {
12431244
return fmt.Errorf("decryptedTx underpriced: EffectiveGasTip needed %v, EffectiveGasTip permitted %v", envelope.EffectiveGasTipValue(baseFee), decryptedTx.EffectiveGasTipValue(baseFee))
12441245
}
1246+
1247+
// Ensure envelope sender matches decrypted sender.
12451248
envelopeFrom, err := types.Sender(c.signerConfig, envelope)
12461249
if err != nil {
12471250
return fmt.Errorf("%w: failed to retrieve envelope sender: %w", txpool.ErrInvalidSender, err)
@@ -1253,6 +1256,13 @@ func (c *DBFT) validateDecryptedTx(head *types.Header, decryptedTx *types.Transa
12531256
if envelopeFrom != decryptedFrom {
12541257
return fmt.Errorf("decryptedTx from mismatch: decryptedFrom %v, envelopeFrom %v", decryptedFrom, envelopeFrom)
12551258
}
1259+
1260+
// Ensure decrypted hash matches the one specified in an unencrypted part of Envelope data.
1261+
expectedH := antimev.GetEncryptedHash(envelope)
1262+
if decryptedTx.Hash().Cmp(expectedH) != 0 {
1263+
return fmt.Errorf("decryptedTx hash mismatch: expected %s, got %s", expectedH, decryptedTx.Hash())
1264+
}
1265+
12561266
return nil
12571267
}
12581268

internal/ethapi/api.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,8 +1893,7 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c
18931893

18941894
// If the transaction is an antimev envelope, then return the declared transaction hash instead of its own.
18951895
if antimev.IsEnvelope(tx) {
1896-
hashOffSet := antimev.EncryptedDataPrefixLen + antimev.EncryptedDataRoundLen
1897-
return common.Hash(tx.Data()[hashOffSet : hashOffSet+antimev.EncryptedDataHashLen]), nil
1896+
return antimev.GetEncryptedHash(tx), nil
18981897
} else {
18991898
return tx.Hash(), nil
19001899
}

0 commit comments

Comments
 (0)