address comments

This commit is contained in:
Shrenuj Bansal 2022-12-19 13:05:11 -05:00
parent c2629b0a96
commit f14eda4468

View File

@ -24,62 +24,23 @@ var EthCmd = &cli.Command{
}, },
} }
func ethAddrFromFilecoinAddress(ctx context.Context, addr address.Address, fnapi v0api.FullNode) (ethtypes.EthAddress, address.Address, error) {
var faddr address.Address
var err error
switch addr.Protocol() {
case address.BLS, address.SECP256K1:
faddr, err = fnapi.StateLookupID(ctx, addr, types.EmptyTSK)
if err != nil {
return ethtypes.EthAddress{}, addr, err
}
case address.Actor, address.ID:
faddr, err = fnapi.StateLookupID(ctx, addr, types.EmptyTSK)
if err != nil {
return ethtypes.EthAddress{}, addr, err
}
fAct, err := fnapi.StateGetActor(ctx, faddr, types.EmptyTSK)
if err != nil {
return ethtypes.EthAddress{}, addr, err
}
if fAct.Address != nil && (*fAct.Address).Protocol() == address.Delegated {
faddr = *fAct.Address
}
case address.Delegated:
faddr = addr
default:
return ethtypes.EthAddress{}, addr, xerrors.Errorf("Filecoin address doesn't match known protocols")
}
ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(faddr)
if err != nil {
return ethtypes.EthAddress{}, addr, err
}
return ethAddr, faddr, nil
}
var EthGetAddressCmd = &cli.Command{ var EthGetAddressCmd = &cli.Command{
Name: "stat", Name: "stat",
Usage: "Print eth/filecoin addrs and code cid", Usage: "Print eth/filecoin addrs and code cid",
ArgsUsage: "",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "filaddr", Name: "filAddr",
Value: "",
Usage: "Filecoin address", Usage: "Filecoin address",
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "ethaddr", Name: "ethAddr",
Value: "",
Usage: "Ethereum address", Usage: "Ethereum address",
}, },
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
filaddr := cctx.String("filaddr") filAddr := cctx.String("filAddr")
ethaddr := cctx.String("ethaddr") ethAddr := cctx.String("ethAddr")
var faddr address.Address var faddr address.Address
var eaddr ethtypes.EthAddress var eaddr ethtypes.EthAddress
@ -91,8 +52,8 @@ var EthGetAddressCmd = &cli.Command{
defer closer() defer closer()
ctx := ReqContext(cctx) ctx := ReqContext(cctx)
if filaddr != "" { if filAddr != "" {
addr, err := address.NewFromString(filaddr) addr, err := address.NewFromString(filAddr)
if err != nil { if err != nil {
return err return err
} }
@ -100,8 +61,11 @@ var EthGetAddressCmd = &cli.Command{
if err != nil { if err != nil {
return err return err
} }
} else if ethaddr != "" { } else if ethAddr != "" {
addr, err := hex.DecodeString(ethaddr) if ethAddr[:2] == "0x" {
ethAddr = ethAddr[2:]
}
addr, err := hex.DecodeString(ethAddr)
if err != nil { if err != nil {
return err return err
} }
@ -111,7 +75,7 @@ var EthGetAddressCmd = &cli.Command{
return err return err
} }
} else { } else {
return xerrors.Errorf("Neither filaddr or ethaddr specified") return xerrors.Errorf("Neither filAddr nor ethAddr specified")
} }
actor, err := api.StateGetActor(ctx, faddr, types.EmptyTSK) actor, err := api.StateGetActor(ctx, faddr, types.EmptyTSK)
@ -176,3 +140,39 @@ var EthCallSimulateCmd = &cli.Command{
}, },
} }
func ethAddrFromFilecoinAddress(ctx context.Context, addr address.Address, fnapi v0api.FullNode) (ethtypes.EthAddress, address.Address, error) {
var faddr address.Address
var err error
switch addr.Protocol() {
case address.BLS, address.SECP256K1:
faddr, err = fnapi.StateLookupID(ctx, addr, types.EmptyTSK)
if err != nil {
return ethtypes.EthAddress{}, addr, err
}
case address.Actor, address.ID:
faddr, err = fnapi.StateLookupID(ctx, addr, types.EmptyTSK)
if err != nil {
return ethtypes.EthAddress{}, addr, err
}
fAct, err := fnapi.StateGetActor(ctx, faddr, types.EmptyTSK)
if err != nil {
return ethtypes.EthAddress{}, addr, err
}
if fAct.Address != nil && (*fAct.Address).Protocol() == address.Delegated {
faddr = *fAct.Address
}
case address.Delegated:
faddr = addr
default:
return ethtypes.EthAddress{}, addr, xerrors.Errorf("Filecoin address doesn't match known protocols")
}
ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(faddr)
if err != nil {
return ethtypes.EthAddress{}, addr, err
}
return ethAddr, faddr, nil
}