Skip to content

Commit

Permalink
Merge pull request #5661 from filecoin-project/fix/delegated-addr
Browse files Browse the repository at this point in the history
 fix: use ToFilecoinAddress() to get f4 addr and validate v sig + cleanups
  • Loading branch information
diwufeiwen authored Jan 16, 2023
2 parents 7b1bdf8 + c691fd0 commit 0b510c5
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 139 deletions.
33 changes: 15 additions & 18 deletions app/submodule/eth/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ func (a *ethAPI) EthSendRawTransaction(ctx context.Context, rawTx types.EthBytes
if err != nil {
return types.EmptyEthHash, err
}
return types.NewEthHashFromCid(cid)
return types.EthHashFromCid(cid)
}

func (a *ethAPI) ethCallToFilecoinMessage(ctx context.Context, tx types.EthCall) (*types.Message, error) {
Expand Down Expand Up @@ -706,7 +706,7 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
if err != nil {
return types.EthBlock{}, err
}
parentBlkHash, err := types.NewEthHashFromCid(parentKeyCid)
parentBlkHash, err := types.EthHashFromCid(parentKeyCid)
if err != nil {
return types.EthBlock{}, err
}
Expand All @@ -715,7 +715,7 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
if err != nil {
return types.EthBlock{}, err
}
blkHash, err := types.NewEthHashFromCid(blkCid)
blkHash, err := types.EthHashFromCid(blkCid)
if err != nil {
return types.EthBlock{}, err
}
Expand Down Expand Up @@ -743,7 +743,7 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
}
block.Transactions = append(block.Transactions, tx)
} else {
hash, err := types.NewEthHashFromCid(msg.Cid())
hash, err := types.EthHashFromCid(msg.Cid())
if err != nil {
return types.EthBlock{}, err
}
Expand All @@ -769,30 +769,27 @@ func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTx
// use that ID to form the masked ID address.
// 4. Otherwise, we fetch the actor's ID from the state tree and form the masked ID with it.
func lookupEthAddress(ctx context.Context, addr address.Address, ca v1.IChain) (types.EthAddress, error) {
// Attempt to convert directly.
if ethAddr, ok, err := types.TryEthAddressFromFilecoinAddress(addr, false); err != nil {
return types.EthAddress{}, err
} else if ok {
// BLOCK A: We are trying to get an actual Ethereum address from an f410 address.
// Attempt to convert directly, if it's an f4 address.
ethAddr, err := types.EthAddressFromFilecoinAddress(addr)
if err == nil && !ethAddr.IsMaskedID() {
return ethAddr, nil
}

// Lookup on the target actor.
// Lookup on the target actor and try to get an f410 address.
actor, err := ca.StateGetActor(ctx, addr, types.EmptyTSK)
if err != nil {
return types.EthAddress{}, err
}
if actor.Address != nil {
if ethAddr, ok, err := types.TryEthAddressFromFilecoinAddress(*actor.Address, false); err != nil {
return types.EthAddress{}, err
} else if ok {
if ethAddr, err := types.EthAddressFromFilecoinAddress(*actor.Address); err == nil && !ethAddr.IsMaskedID() {
return ethAddr, nil
}
}

// BLOCK B: We gave up on getting an actual Ethereum address and are falling back to a Masked ID address.
// Check if we already have an ID addr, and use it if possible.
if ethAddr, ok, err := types.TryEthAddressFromFilecoinAddress(addr, true); err != nil {
return types.EthAddress{}, err
} else if ok {
if err == nil && ethAddr.IsMaskedID() {
return ethAddr, nil
}

Expand Down Expand Up @@ -850,7 +847,7 @@ func newEthTxFromFilecoinMessage(ctx context.Context, smsg *types.SignedMessage,
r, s, v = types.EthBigIntZero, types.EthBigIntZero, types.EthBigIntZero
}

hash, err := types.NewEthHashFromCid(smsg.Cid())
hash, err := types.EthHashFromCid(smsg.Cid())
if err != nil {
return types.EthTx{}, err
}
Expand Down Expand Up @@ -882,7 +879,7 @@ func newEthTxFromFilecoinMessageLookup(ctx context.Context, msgLookup *types.Msg
return types.EthTx{}, fmt.Errorf("msg does not exist")
}
cid := msgLookup.Message
txHash, err := types.NewEthHashFromCid(cid)
txHash, err := types.EthHashFromCid(cid)
if err != nil {
return types.EthTx{}, err
}
Expand Down Expand Up @@ -920,7 +917,7 @@ func newEthTxFromFilecoinMessageLookup(ctx context.Context, msgLookup *types.Msg
}
}

blkHash, err := types.NewEthHashFromCid(parentTSCid)
blkHash, err := types.EthHashFromCid(parentTSCid)
if err != nil {
return types.EthTx{}, err
}
Expand Down
8 changes: 4 additions & 4 deletions app/submodule/eth/eth_event_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ func ethFilterResultFromEvents(evs []*filter.CollectedEvent) (*types.EthFilterRe
return nil, err
}

log.TransactionHash, err = types.NewEthHashFromCid(ev.MsgCid)
log.TransactionHash, err = types.EthHashFromCid(ev.MsgCid)
if err != nil {
return nil, err
}
Expand All @@ -554,7 +554,7 @@ func ethFilterResultFromEvents(evs []*filter.CollectedEvent) (*types.EthFilterRe
if err != nil {
return nil, err
}
log.BlockHash, err = types.NewEthHashFromCid(c)
log.BlockHash, err = types.EthHashFromCid(c)
if err != nil {
return nil, err
}
Expand All @@ -573,7 +573,7 @@ func ethFilterResultFromTipSets(tsks []types.TipSetKey) (*types.EthFilterResult,
if err != nil {
return nil, err
}
hash, err := types.NewEthHashFromCid(c)
hash, err := types.EthHashFromCid(c)
if err != nil {
return nil, err
}
Expand All @@ -588,7 +588,7 @@ func ethFilterResultFromMessages(cs []cid.Cid) (*types.EthFilterResult, error) {
res := &types.EthFilterResult{}

for _, c := range cs {
hash, err := types.NewEthHashFromCid(c)
hash, err := types.EthHashFromCid(c)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var evmGetInfoCmd = &cmds.Command{
return err
}
} else if ethAddr != "" {
eaddr, err := types.EthAddressFromHex(ethAddr)
eaddr, err := types.ParseEthAddress(ethAddr)
if err != nil {
return err
}
Expand Down Expand Up @@ -110,12 +110,12 @@ var evmCallSimulateCmd = &cmds.Command{
return fmt.Errorf("incorrect number of arguments, got %d", len(req.Arguments))
}

fromEthAddr, err := types.EthAddressFromHex(req.Arguments[0])
fromEthAddr, err := types.ParseEthAddress(req.Arguments[0])
if err != nil {
return err
}

toEthAddr, err := types.EthAddressFromHex(req.Arguments[1])
toEthAddr, err := types.ParseEthAddress(req.Arguments[1])
if err != nil {
return err
}
Expand Down Expand Up @@ -155,7 +155,7 @@ var evmGetContractAddressCmd = &cmds.Command{
return fmt.Errorf("incorrect number of arguments, got %d", len(req.Arguments))
}

sender, err := types.EthAddressFromHex(req.Arguments[0])
sender, err := types.ParseEthAddress(req.Arguments[0])
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/crypto/delegated/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func (delegatedSigner) Verify(sig []byte, a address.Address, msg []byte) error {
hasher.Write(pubk)
addrHash := hasher.Sum(nil)

// The address hash will not start with [12]byte{0xff}, so we don't have to use
// EthAddr.ToFilecoinAddress() to handle the case with an id address
// Also, importing ethtypes here will cause circulating import
maybeaddr, err := address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, addrHash[12:])
if err != nil {
return err
Expand Down
10 changes: 0 additions & 10 deletions pkg/statemanger/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,6 @@ func (s *Stmgr) CallWithGas(ctx context.Context, msg *types.Message, priorMsgs [
Data: make([]byte, 65),
},
}
default:
// XXX: Hack to make sending from f099 (and others) "just work".
// REMOVE ME.
msgApply = &types.SignedMessage{
Message: *msg,
Signature: crypto.Signature{
Type: crypto.SigTypeSecp256k1,
Data: make([]byte, 65),
},
}
}

// If the fee cap is set to zero, make gas free.
Expand Down
21 changes: 10 additions & 11 deletions pkg/wallet/key/keyinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/awnumar/memguard"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/crypto"
acrypto "github.com/filecoin-project/venus/pkg/crypto"
_ "github.com/filecoin-project/venus/pkg/crypto/bls"
Expand All @@ -17,7 +16,6 @@ import (
"github.com/filecoin-project/venus/venus-shared/types"
logging "github.com/ipfs/go-log/v2"
"github.com/pkg/errors"
"golang.org/x/crypto/sha3"
)

var log = logging.Logger("keyinfo")
Expand Down Expand Up @@ -156,17 +154,18 @@ func (ki *KeyInfo) Address() (address.Address, error) {
return address.NewSecp256k1Address(pubKey)
}
if ki.SigType == types.SigTypeDelegated {
// Assume eth for now
hasher := sha3.NewLegacyKeccak256()
// if we get an uncompressed public key (that's what we get from the library,
// but putting this check here for defensiveness), strip the prefix
if pubKey[0] == 0x04 {
pubKey = pubKey[1:]
// Transitory Delegated signature verification as per FIP-0055
ethAddr, err := types.EthAddressFromPubKey(pubKey)
if err != nil {
return address.Undef, fmt.Errorf("failed to calculate Eth address from public key: %w", err)
}
ea, err := types.CastEthAddress(ethAddr)
if err != nil {
return address.Undef, fmt.Errorf("failed to create ethereum address from bytes: %w", err)
}

hasher.Write(pubKey)

return address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, hasher.Sum(nil)[12:])
// return address.NewDelegatedAddress(builtin.EthereumAddressManagerActorID, hasher.Sum(nil)[12:])
return ea.ToFilecoinAddress()
}

return address.Undef, errors.Errorf("can not generate address for unknown crypto system: %d", ki.SigType)
Expand Down
4 changes: 2 additions & 2 deletions venus-devtool/api-gen/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ func init() {
ethint := types.EthUint64(5)
addExample(ethint)
addExample(&ethint)
ethaddr, _ := types.EthAddressFromHex("0x5CbEeCF99d3fDB3f25E309Cc264f240bb0664031")
ethaddr, _ := types.ParseEthAddress("0x5CbEeCF99d3fDB3f25E309Cc264f240bb0664031")
addExample(&ethaddr)
ethhash, _ := types.NewEthHashFromCid(c)
ethhash, _ := types.EthHashFromCid(c)
addExample(&ethhash)
ethFeeHistoryReward := [][]types.EthBigInt{}
addExample(&ethFeeHistoryReward)
Expand Down
Loading

0 comments on commit 0b510c5

Please sign in to comment.