This repository was archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathipns.go
80 lines (67 loc) · 2.08 KB
/
ipns.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
package ipfs
import (
"context"
"fmt"
"time"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
iface "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/options"
nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
path "github.com/ipfs/interface-go-ipfs-core/path"
peer "github.com/libp2p/go-libp2p-core/peer"
record "github.com/libp2p/go-libp2p-record"
)
// PublishIPNS publishes a content id to ipns
func PublishIPNS(node *core.IpfsNode, id string, key string, timeout time.Duration) (iface.IpnsEntry, error) {
api, err := coreapi.NewCoreAPI(node)
if err != nil {
return nil, err
}
if key == "" {
key = "self" // default value in ipns module
}
opts := []options.NamePublishOption{
options.Name.Key(key),
}
ctx, cancel := context.WithTimeout(node.Context(), timeout)
defer cancel()
return api.Name().Publish(ctx, path.New(id), opts...)
}
// ResolveIPNS resolves an ipns path to an ipfs path
func ResolveIPNS(node *core.IpfsNode, name peer.ID, timeout time.Duration) (path.Path, error) {
api, err := coreapi.NewCoreAPI(node)
if err != nil {
return nil, err
}
key := fmt.Sprintf("/ipns/%s", name.Pretty())
opts := []options.NameResolveOption{
options.Name.ResolveOption(nsopts.Depth(1)),
options.Name.ResolveOption(nsopts.DhtRecordCount(4)),
options.Name.ResolveOption(nsopts.DhtTimeout(timeout)),
}
ctx, cancel := context.WithTimeout(node.Context(), timeout)
defer cancel()
return api.Name().Resolve(ctx, key, opts...)
}
// IpnsSubs shows current name subscriptions
func IpnsSubs(node *core.IpfsNode) ([]string, error) {
if node.PSRouter == nil {
return nil, fmt.Errorf("IPNS pubsub subsystem is not enabled")
}
var paths []string
for _, key := range node.PSRouter.GetSubscriptions() {
ns, k, err := record.SplitKey(key)
if err != nil || ns != "ipns" {
// not necessarily an error.
continue
}
pid, err := peer.IDFromString(k)
if err != nil {
log.Errorf("ipns key not a valid peer ID: %s", err)
continue
}
paths = append(paths, "/ipns/"+peer.IDB58Encode(pid))
}
return paths, nil
}