forked from iotaledger/hive.go
/
local.go
118 lines (97 loc) · 2.95 KB
/
local.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package peer
import (
"net"
"sync"
"github.com/izuc/zipp.foundation/core/autopeering/peer/service"
"github.com/izuc/zipp.foundation/core/autopeering/salt"
"github.com/izuc/zipp.foundation/core/crypto/ed25519"
"github.com/izuc/zipp.foundation/core/identity"
)
// Local defines the struct of a local peer.
type Local struct {
*Peer
identity *identity.LocalIdentity
db *DB
// everything below is protected by a lock
mu sync.RWMutex
serviceRecord *service.Record
publicSalt *salt.Salt
privateSalt *salt.Salt
}
// newLocal creates a new local peer.
func newLocal(key ed25519.PrivateKey, ip net.IP, serviceRecord *service.Record, db *DB) *Local {
id := identity.New(key.Public())
return &Local{
Peer: NewPeer(id, ip, serviceRecord),
identity: identity.NewLocalIdentityWithIdentity(id, key),
db: db,
serviceRecord: serviceRecord,
}
}
// NewLocal creates a new local peer linked to the provided store.
// If an optional seed is provided, the seed is used to generate the private key. Without a seed,
// the provided key is loaded from the provided database and generated if not stored there.
func NewLocal(ip net.IP, serviceRecord *service.Record, db *DB, seed ...[]byte) (*Local, error) {
var key ed25519.PrivateKey
if len(seed) > 0 {
key = ed25519.PrivateKeyFromSeed(seed[0])
if db != nil {
if err := db.UpdateLocalPrivateKey(key); err != nil {
return nil, err
}
}
} else {
var err error
key, err = db.LocalPrivateKey()
if err != nil {
return nil, err
}
}
return newLocal(key, ip, serviceRecord, db), nil
}
// Database returns the node database associated with the local peer.
func (l *Local) Database() *DB {
return l.db
}
// UpdateService updates the endpoint address of the given local service.
func (l *Local) UpdateService(key service.Key, network string, port int) error {
l.mu.Lock()
defer l.mu.Unlock()
// update the service in the read protected map
l.serviceRecord.Update(key, network, port)
// create a new peer with the corresponding services
l.Peer = NewPeer(l.identity.Identity, l.IP(), l.serviceRecord)
return nil
}
// GetPublicSalt returns the public salt.
func (l *Local) GetPublicSalt() *salt.Salt {
l.mu.RLock()
defer l.mu.RUnlock()
return l.publicSalt
}
// SetPublicSalt sets the public salt.
func (l *Local) SetPublicSalt(salt *salt.Salt) {
l.mu.Lock()
defer l.mu.Unlock()
l.publicSalt = salt
}
// GetPrivateSalt returns the private salt.
func (l *Local) GetPrivateSalt() *salt.Salt {
l.mu.RLock()
defer l.mu.RUnlock()
return l.privateSalt
}
// SetPrivateSalt sets the private salt.
func (l *Local) SetPrivateSalt(salt *salt.Salt) {
l.mu.Lock()
defer l.mu.Unlock()
l.privateSalt = salt
}
// Sign signs a message using the node's LocalIdentity.
func (l *Local) Sign(message []byte) ed25519.Signature {
return l.identity.Sign(message)
}
// LocalIdentity returns the local identity.
func (l *Local) LocalIdentity() *identity.LocalIdentity {
return l.identity
}