-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddr.go
101 lines (84 loc) · 2.82 KB
/
addr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package walletutils
import (
"context"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/glifio/go-pools/util"
)
func GetGenericAddr(addr string) (interface{}, KeyType, error) {
// check if the key is an eth address
if strings.HasPrefix(addr, "0x") {
return common.HexToAddress(addr), KeyTypeEth, nil
}
// check if the key is a filecoin address
filAddr, err := address.NewFromString(addr)
if err != nil {
return "", KeyTypeUnknown, err
}
return filAddr, KeyTypeFil, nil
}
func GetFilAddr(ctx context.Context, addrStr string, lapi api.FullNode) (address.Address, error) {
addr, keyType, err := GetGenericAddr(addrStr)
if err != nil {
return address.Undef, err
}
if keyType == KeyTypeFil {
// make sure the fil key type is not an ID address of an EVM actor type
if err := util.CheckIDNotEVMActorType(ctx, addr.(address.Address), lapi); err != nil {
return address.Undef, err
}
return addr.(address.Address), nil
} else if keyType == KeyTypeEth {
// convert to filecoin address
ethAddr, err := ethtypes.ParseEthAddress(addr.(common.Address).String())
if err != nil {
return address.Undef, err
}
return ethAddr.ToFilecoinAddress()
}
return address.Undef, ErrUnsupportedKeyType
}
func GetEthAddr(ctx context.Context, addrStr string, lapi api.FullNode) (common.Address, error) {
addr, keyType, err := GetGenericAddr(addrStr)
if err != nil {
return common.Address{}, err
}
if keyType == KeyTypeFil {
filAddr := addr.(address.Address)
// make sure the fil key type is not an ID address of an EVM actor type
if err := util.CheckIDNotEVMActorType(ctx, filAddr, lapi); err != nil {
return common.Address{}, err
}
// if the address is f1, f2, f3, go fetch the ID addr and return the masked hex id
if filAddr.Protocol() != address.ID && filAddr.Protocol() != address.Delegated {
// convert to filecoin f0 address
filAddr, err = lapi.StateLookupID(ctx, filAddr, types.EmptyTSK)
if err != nil {
return common.Address{}, err
}
}
ethAddr, err := ethtypes.EthAddressFromFilecoinAddress(filAddr)
if err != nil {
return common.Address{}, err
}
return common.HexToAddress(ethAddr.String()), nil
} else if keyType == KeyTypeEth {
return addr.(common.Address), nil
}
return common.Address{}, ErrUnsupportedKeyType
}
func GetEthFilAddr(ctx context.Context, addr string, lapi api.FullNode) (common.Address, address.Address, error) {
filAddr, err := GetFilAddr(ctx, addr, lapi)
if err != nil {
return common.Address{}, address.Undef, err
}
ethAddr, err := GetEthAddr(ctx, addr, lapi)
if err != nil {
return common.Address{}, address.Undef, err
}
return ethAddr, filAddr, nil
}