name.go 3.17 KB
Newer Older
1 2 3 4 5 6
package options

import (
	"time"
)

7 8 9 10
const (
	DefaultNameValidTime = 24 * time.Hour
)

11 12 13 14 15 16
type NamePublishSettings struct {
	ValidTime time.Duration
	Key       string
}

type NameResolveSettings struct {
Łukasz Magiera's avatar
Łukasz Magiera committed
17 18 19 20 21 22
	Depth int
	Local bool
	Cache bool

	DhtRecordCount int
	DhtTimeout     time.Duration
23 24 25 26 27 28 29
}

type NamePublishOption func(*NamePublishSettings) error
type NameResolveOption func(*NameResolveSettings) error

func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error) {
	options := &NamePublishSettings{
30
		ValidTime: DefaultNameValidTime,
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
		Key:       "self",
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}

	return options, nil
}

func NameResolveOptions(opts ...NameResolveOption) (*NameResolveSettings, error) {
	options := &NameResolveSettings{
Łukasz Magiera's avatar
Łukasz Magiera committed
46 47 48 49 50 51
		Depth: 1,
		Local: false,
		Cache: true,

		DhtRecordCount: 16,
		DhtTimeout:     time.Minute,
52 53 54 55 56 57 58 59 60 61 62 63
	}

	for _, opt := range opts {
		err := opt(options)
		if err != nil {
			return nil, err
		}
	}

	return options, nil
}

64
type nameOpts struct{}
65

66 67 68 69
var Name nameOpts

// ValidTime is an option for Name.Publish which specifies for how long the
// entry will remain valid. Default value is 24h
70
func (nameOpts) ValidTime(validTime time.Duration) NamePublishOption {
71 72 73 74 75 76
	return func(settings *NamePublishSettings) error {
		settings.ValidTime = validTime
		return nil
	}
}

77 78 79 80 81
// Key is an option for Name.Publish which specifies the key to use for
// publishing. Default value is "self" which is the node's own PeerID.
// The key parameter must be either PeerID or keystore key alias.
//
// You can use KeyAPI to list and generate more names and their respective keys.
82
func (nameOpts) Key(key string) NamePublishOption {
83 84 85 86 87 88
	return func(settings *NamePublishSettings) error {
		settings.Key = key
		return nil
	}
}

Łukasz Magiera's avatar
Łukasz Magiera committed
89
// Depth is an option for Name.Resolve which specifies the maximum depth of a
90
// recursive lookup. Default value is false
Łukasz Magiera's avatar
Łukasz Magiera committed
91
func (nameOpts) Depth(depth int) NameResolveOption {
92
	return func(settings *NameResolveSettings) error {
Łukasz Magiera's avatar
Łukasz Magiera committed
93
		settings.Depth = depth
94 95 96 97
		return nil
	}
}

98 99
// Local is an option for Name.Resolve which specifies if the lookup should be
// offline. Default value is false
100
func (nameOpts) Local(local bool) NameResolveOption {
101 102 103 104 105 106
	return func(settings *NameResolveSettings) error {
		settings.Local = local
		return nil
	}
}

107 108
// Cache is an option for Name.Resolve which specifies if cache should be used.
// Default value is true
109
func (nameOpts) Cache(cache bool) NameResolveOption {
110
	return func(settings *NameResolveSettings) error {
111
		settings.Cache = cache
112 113 114
		return nil
	}
}
Łukasz Magiera's avatar
Łukasz Magiera committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

// DhtRecordCount is an option for Name.Resolve which specifies how many records
// we want to validate before selecting the best one (newest). Note that setting
// this value too low will have security implications
func (nameOpts) DhtRecordCount(rc int) NameResolveOption {
	return func(settings *NameResolveSettings) error {
		settings.DhtRecordCount = rc
		return nil
	}
}

// DhtTimeout is an option for Name.Resolve which specifies timeout for
// DHT lookup
func (nameOpts) DhtTimeout(timeout time.Duration) NameResolveOption {
	return func(settings *NameResolveSettings) error {
		settings.DhtTimeout = timeout
		return nil
	}
}