-
Notifications
You must be signed in to change notification settings - Fork 1
/
host.go
63 lines (55 loc) · 1.73 KB
/
host.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
package rtns
import (
"context"
"time"
"github.com/ipfs/go-datastore"
keystore "github.com/ipfs/go-ipfs-keystore"
"github.com/ipfs/go-ipfs/namesys"
"github.com/ipfs/go-path"
ci "github.com/libp2p/go-libp2p-core/crypto"
routing "github.com/libp2p/go-libp2p-routing"
)
// RTNS manages all the needed components
// to interact with public and private IPNS
// networks.
type RTNS struct {
ns namesys.NameSystem
ds datastore.Datastore
keys keystore.Keystore
cache *cache
ctx context.Context
}
// NewRTNS instantiates our RTNS service, and starts the republisher
func NewRTNS(ctx context.Context, rvs routing.ValueStore, ds datastore.Datastore, keys keystore.Keystore, size int) *RTNS {
r := &RTNS{
ns: namesys.NewNameSystem(rvs, ds, size),
ds: ds,
keys: keys,
cache: newCache(),
ctx: ctx,
}
go r.startRepublisher()
return r
}
// Publish enables publishing of an IPNS record with a default lifetime of 24 hours
func (r *RTNS) Publish(ctx context.Context, pk ci.PrivKey, cache bool, keyID, content string) error {
if cache {
r.cache.Set(keyID)
}
return r.ns.Publish(ctx, pk, path.FromString(content))
}
// PublishWithEOL allows specifying a lifetime for this record overriding the default lifetime of 24 hours
func (r *RTNS) PublishWithEOL(ctx context.Context, pk ci.PrivKey, eol time.Time, cache bool, keyID, content string) error {
if cache {
r.cache.Set(keyID)
}
return r.ns.PublishWithEOL(ctx, pk, path.FromString(content), eol)
}
// GetKey returns a key from the underlying krab keystore
func (r *RTNS) GetKey(name string) (ci.PrivKey, error) {
return r.keys.Get(name)
}
// HasKey returns whether or not the key is in our keystore
func (r *RTNS) HasKey(name string) (bool, error) {
return r.keys.Has(name)
}