-
Notifications
You must be signed in to change notification settings - Fork 3.9k
op-chain-ops: implement withdrawal hashing #3469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package crossdomain | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "fmt" | ||
| "math/big" | ||
|
|
||
| "github.com/ethereum-optimism/optimism/op-bindings/predeploys" | ||
| "github.com/ethereum/go-ethereum/accounts/abi" | ||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/crypto" | ||
| ) | ||
|
|
||
| // LegacyWithdrawal represents a pre bedrock upgrade withdrawal. | ||
| type LegacyWithdrawal struct { | ||
| Target *common.Address | ||
| Sender *common.Address | ||
| Data []byte | ||
| Nonce *big.Int | ||
| } | ||
|
|
||
| var _ WithdrawalMessage = (*LegacyWithdrawal)(nil) | ||
|
|
||
| // NewLegacyWithdrawal will construct a LegacyWithdrawal | ||
| func NewLegacyWithdrawal(target, sender *common.Address, data []byte, nonce *big.Int) *LegacyWithdrawal { | ||
| return &LegacyWithdrawal{ | ||
| Target: target, | ||
| Sender: sender, | ||
| Data: data, | ||
| Nonce: nonce, | ||
| } | ||
| } | ||
|
|
||
| // Encode will serialze the Withdrawal in the legacy format so that it | ||
| // is suitable for hashing. This assumes that the message is being withdrawn | ||
| // through the standard optimism cross domain messaging system by hashing in | ||
| // the L2CrossDomainMessenger address. | ||
| func (w *LegacyWithdrawal) Encode() ([]byte, error) { | ||
| enc, err := EncodeCrossDomainMessageV0(w.Target, w.Sender, w.Data, w.Nonce) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| out := make([]byte, len(enc)+len(predeploys.L2CrossDomainMessengerAddr.Bytes())) | ||
| copy(out, enc) | ||
| copy(out[len(enc):], predeploys.L2CrossDomainMessengerAddr.Bytes()) | ||
| return out, nil | ||
| } | ||
|
|
||
| // Decode will decode a serialized LegacyWithdrawal | ||
| func (w *LegacyWithdrawal) Decode(data []byte) error { | ||
| if len(data) < len(predeploys.L2CrossDomainMessengerAddr)+4 { | ||
| return fmt.Errorf("withdrawal data too short: %d", len(data)) | ||
| } | ||
|
|
||
| selector := crypto.Keccak256([]byte("relayMessage(address,address,bytes,uint256)"))[0:4] | ||
| if !bytes.Equal(data[0:4], selector) { | ||
| return fmt.Errorf("invalid selector: 0x%x", data[0:4]) | ||
| } | ||
|
|
||
| msgSender := data[len(data)-len(predeploys.L2CrossDomainMessengerAddr):] | ||
| if !bytes.Equal(msgSender, predeploys.L2CrossDomainMessengerAddr.Bytes()) { | ||
| return errors.New("invalid msg.sender") | ||
| } | ||
|
|
||
| raw := data[4 : len(data)-len(predeploys.L2CrossDomainMessengerAddr)] | ||
|
|
||
| args := abi.Arguments{ | ||
| {Name: "target", Type: AddressType}, | ||
| {Name: "sender", Type: AddressType}, | ||
| {Name: "data", Type: BytesType}, | ||
| {Name: "nonce", Type: Uint256Type}, | ||
| } | ||
|
|
||
| decoded, err := args.Unpack(raw) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| target, ok := decoded[0].(common.Address) | ||
| if !ok { | ||
| return errors.New("cannot abi decode target") | ||
| } | ||
| sender, ok := decoded[1].(common.Address) | ||
| if !ok { | ||
| return errors.New("cannot abi decode sender") | ||
| } | ||
| msgData, ok := decoded[2].([]byte) | ||
| if !ok { | ||
| return errors.New("cannot abi decode data") | ||
| } | ||
| nonce, ok := decoded[3].(*big.Int) | ||
| if !ok { | ||
| return errors.New("cannot abi decode nonce") | ||
| } | ||
|
|
||
| w.Target = &target | ||
| w.Sender = &sender | ||
| w.Data = msgData | ||
| w.Nonce = nonce | ||
| return nil | ||
| } | ||
|
|
||
| // Hash will compute the legacy style hash that is computed in the | ||
| // OVM_L2ToL1MessagePasser. | ||
| func (w *LegacyWithdrawal) Hash() (common.Hash, error) { | ||
| encoded, err := w.Encode() | ||
| if err != nil { | ||
| return common.Hash{}, nil | ||
| } | ||
| hash := crypto.Keccak256(encoded) | ||
| return common.BytesToHash(hash), nil | ||
| } | ||
|
|
||
| // StorageSlot will compute the storage slot that is set | ||
| // to true in the legacy L2ToL1MessagePasser. | ||
| func (w *LegacyWithdrawal) StorageSlot() (common.Hash, error) { | ||
| hash, err := w.Hash() | ||
| if err != nil { | ||
| return common.Hash{}, err | ||
| } | ||
| preimage := make([]byte, 64) | ||
| copy(preimage, hash.Bytes()) | ||
|
|
||
| slot := crypto.Keccak256(preimage) | ||
| return common.BytesToHash(slot), nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.