-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathutil.go
50 lines (43 loc) · 1.23 KB
/
util.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
package cmd
import (
"context"
"encoding/json"
"fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
)
func GetAddrInfo(ctx context.Context, api api.Gateway, maddr address.Address) (*peer.AddrInfo, error) {
minfo, err := api.StateMinerInfo(ctx, maddr, types.EmptyTSK)
if err != nil {
return nil, err
}
if minfo.PeerId == nil {
return nil, fmt.Errorf("storage provider %s has no peer ID set on-chain", maddr)
}
var maddrs []multiaddr.Multiaddr
for _, mma := range minfo.Multiaddrs {
ma, err := multiaddr.NewMultiaddrBytes(mma)
if err != nil {
return nil, fmt.Errorf("storage provider %s had invalid multiaddrs in their info: %w", maddr, err)
}
maddrs = append(maddrs, ma)
}
if len(maddrs) == 0 {
return nil, fmt.Errorf("storage provider %s has no multiaddrs set on-chain", maddr)
}
return &peer.AddrInfo{
ID: *minfo.PeerId,
Addrs: maddrs,
}, nil
}
func PrintJson(obj interface{}) error {
resJson, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return fmt.Errorf("marshalling json: %w", err)
}
fmt.Println(string(resJson))
return nil
}