-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
fix: delegated signatures: check every field of txs and roundtrip eth <-> FIL #10007
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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
This file contains 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 |
---|---|---|
|
@@ -8,10 +8,10 @@ import ( | |
|
||
cbg "github.com/whyrusleeping/cbor-gen" | ||
"golang.org/x/crypto/sha3" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/filecoin-project/go-address" | ||
gocrypto "github.com/filecoin-project/go-crypto" | ||
"github.com/filecoin-project/go-state-types/abi" | ||
"github.com/filecoin-project/go-state-types/big" | ||
builtintypes "github.com/filecoin-project/go-state-types/builtin" | ||
"github.com/filecoin-project/go-state-types/builtin/v10/eam" | ||
|
@@ -58,27 +58,29 @@ type EthTxArgs struct { | |
S big.Int `json:"s"` | ||
} | ||
|
||
func NewEthTxArgsFromMessage(msg *types.Message) (EthTxArgs, error) { | ||
func EthTxArgsFromMessage(msg *types.Message) (EthTxArgs, error) { | ||
var ( | ||
to *EthAddress | ||
decodedParams []byte | ||
paramsReader = bytes.NewReader(msg.Params) | ||
to *EthAddress | ||
params []byte | ||
paramsReader = bytes.NewReader(msg.Params) | ||
) | ||
|
||
if msg.Version != 0 { | ||
return EthTxArgs{}, xerrors.Errorf("unsupported msg version: %d", msg.Version) | ||
} | ||
|
||
if msg.To == builtintypes.EthereumAddressManagerActorAddr { | ||
switch msg.Method { | ||
// TODO: Uncomment | ||
//case builtintypes.MethodsEAM.CreateExternal: | ||
case builtintypes.MethodsEAM.Create: | ||
// TODO: Uncomment | ||
// var create eam.CreateExternalParams | ||
var create eam.CreateParams | ||
if err := create.UnmarshalCBOR(paramsReader); err != nil { | ||
return EthTxArgs{}, err | ||
} | ||
decodedParams = create.Initcode | ||
case builtintypes.MethodsEAM.Create2: | ||
var create2 eam.Create2Params | ||
if err := create2.UnmarshalCBOR(paramsReader); err != nil { | ||
return EthTxArgs{}, err | ||
} | ||
decodedParams = create2.Initcode | ||
params = create.Initcode | ||
default: | ||
return EthTxArgs{}, fmt.Errorf("unsupported EAM method") | ||
} | ||
|
@@ -89,12 +91,30 @@ func NewEthTxArgsFromMessage(msg *types.Message) (EthTxArgs, error) { | |
} | ||
to = &addr | ||
|
||
if len(msg.Params) > 0 { | ||
params, err := cbg.ReadByteArray(paramsReader, uint64(len(msg.Params))) | ||
if len(msg.Params) == 0 { | ||
if msg.Method != builtintypes.MethodSend { | ||
return EthTxArgs{}, xerrors.Errorf("cannot invoke method %d on non-EAM actor without params", msg.Method) | ||
} | ||
} else { | ||
if msg.Method != builtintypes.MethodsEVM.InvokeContract { | ||
return EthTxArgs{}, | ||
xerrors.Errorf("invalid methodnum %d: only allowed non-send method is InvokeContract(%d)", | ||
msg.Method, | ||
builtintypes.MethodsEVM.InvokeContract) | ||
} | ||
|
||
params, err = cbg.ReadByteArray(paramsReader, uint64(len(msg.Params))) | ||
if err != nil { | ||
return EthTxArgs{}, err | ||
return EthTxArgs{}, xerrors.Errorf("failed to read params byte array: %w", err) | ||
} | ||
|
||
if paramsReader.Len() != 0 { | ||
return EthTxArgs{}, xerrors.Errorf("extra data found in params") | ||
} | ||
if len(params) == 0 { | ||
// Otherwise, we don't get a guaranteed round-trip. | ||
return EthTxArgs{}, xerrors.Errorf("cannot invoke contracts with empty parameters from an eth-account") | ||
} | ||
decodedParams = params | ||
} | ||
} | ||
|
||
|
@@ -103,84 +123,95 @@ func NewEthTxArgsFromMessage(msg *types.Message) (EthTxArgs, error) { | |
Nonce: int(msg.Nonce), | ||
To: to, | ||
Value: msg.Value, | ||
Input: decodedParams, | ||
Input: params, | ||
MaxFeePerGas: msg.GasFeeCap, | ||
MaxPriorityFeePerGas: msg.GasPremium, | ||
GasLimit: int(msg.GasLimit), | ||
}, nil | ||
} | ||
|
||
func (tx *EthTxArgs) ToSignedMessage() (*types.SignedMessage, error) { | ||
from, err := tx.Sender() | ||
if err != nil { | ||
return nil, err | ||
func (tx *EthTxArgs) ToUnsignedMessage(from address.Address) (*types.Message, error) { | ||
if tx.ChainID != build.Eip155ChainId { | ||
return nil, xerrors.Errorf("unsupported chain id: %d", tx.ChainID) | ||
} | ||
|
||
var to address.Address | ||
var err error | ||
method := builtintypes.MethodSend | ||
var params []byte | ||
|
||
if len(tx.To) == 0 && len(tx.Input) == 0 { | ||
return nil, fmt.Errorf("to and input cannot both be empty") | ||
} | ||
|
||
var method abi.MethodNum | ||
var to address.Address | ||
// nil indicates the EAM, only CreateExternal is allowed | ||
if tx.To == nil { | ||
// TODO unify with applyEvmMsg | ||
|
||
// this is a contract creation | ||
to = builtintypes.EthereumAddressManagerActorAddr | ||
|
||
params2, err := actors.SerializeParams(&eam.CreateParams{ | ||
// TODO: Uncomment | ||
//method = builtintypes.MethodsEAM.CreateExternal | ||
method = builtintypes.MethodsEAM.Create | ||
if len(tx.Input) == 0 { | ||
return nil, xerrors.New("cannot call CreateExternal without params") | ||
} | ||
// TODO: CreateExternalParams, it doesn't have a nonce | ||
params, err = actors.SerializeParams(&eam.CreateParams{ | ||
Initcode: tx.Input, | ||
Nonce: uint64(tx.Nonce), | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to serialize Create params: %w", err) | ||
} | ||
params = params2 | ||
method = builtintypes.MethodsEAM.Create | ||
|
||
} else { | ||
addr, err := tx.To.ToFilecoinAddress() | ||
to, err = tx.To.ToFilecoinAddress() | ||
if err != nil { | ||
return nil, err | ||
return nil, xerrors.Errorf("failed to convert To into filecoin addr: %w", err) | ||
} | ||
to = addr | ||
|
||
if len(tx.Input) > 0 { | ||
var buf bytes.Buffer | ||
if err := cbg.WriteByteArray(&buf, tx.Input); err != nil { | ||
return nil, fmt.Errorf("failed to encode tx input into a cbor byte-string") | ||
if len(tx.Input) == 0 { | ||
// Yes, this is redundant, but let's be sure what we're doing | ||
method = builtintypes.MethodSend | ||
params = make([]byte, 0) | ||
} else { | ||
// must be InvokeContract | ||
method = builtintypes.MethodsEVM.InvokeContract | ||
buf := new(bytes.Buffer) | ||
if err = cbg.WriteByteArray(buf, tx.Input); err != nil { | ||
return nil, xerrors.Errorf("failed to write input args: %w", err) | ||
} | ||
|
||
params = buf.Bytes() | ||
method = builtintypes.MethodsEVM.InvokeContract | ||
} else { | ||
method = builtintypes.MethodSend | ||
} | ||
} | ||
|
||
msg := &types.Message{ | ||
Nonce: uint64(tx.Nonce), | ||
From: from, | ||
return &types.Message{ | ||
Version: 0, | ||
To: to, | ||
From: from, | ||
Nonce: uint64(tx.Nonce), | ||
Value: tx.Value, | ||
Method: method, | ||
Params: params, | ||
GasLimit: int64(tx.GasLimit), | ||
GasFeeCap: tx.MaxFeePerGas, | ||
GasPremium: tx.MaxPriorityFeePerGas, | ||
Method: method, | ||
Params: params, | ||
}, nil | ||
} | ||
|
||
func (tx *EthTxArgs) ToSignedMessage() (*types.SignedMessage, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I didn't notice the method right after this one does the same thing. We'll need to either consolidate these methods or drop the new one before merging. |
||
from, err := tx.Sender() | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to calculate sender: %w", err) | ||
} | ||
|
||
sig, err := tx.Signature() | ||
unsignedMsg, err := tx.ToUnsignedMessage(from) | ||
if err != nil { | ||
return nil, err | ||
return nil, xerrors.Errorf("failed to convert to unsigned msg: %w", err) | ||
} | ||
|
||
signedMsg := types.SignedMessage{ | ||
Message: *msg, | ||
Signature: *sig, | ||
siggy, err := tx.Signature() | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to calculate signature: %w", err) | ||
} | ||
return &signedMsg, nil | ||
|
||
return &types.SignedMessage{ | ||
Message: *unsignedMsg, | ||
Signature: *siggy, | ||
}, nil | ||
} | ||
|
||
func (tx *EthTxArgs) HashedOriginalRlpMsg() ([]byte, error) { | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to assert that
params
is non-empty here. Otherwise, this should have been a method send and someone pulled a fast one.