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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
// Package addressbook manages contacts and their public keys
package addressbook
import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"sync"
"time"
)
// Contact represents a contact in the address book
type Contact struct {
Name string `json:"name"`
PublicKey string `json:"public_key"` // hex-encoded Ed25519 public key
OnionAddress string `json:"onion_address"` // Tor hidden service address
Fingerprint string `json:"fingerprint"` // Short fingerprint for verification
AddedAt time.Time `json:"added_at"`
LastSeen time.Time `json:"last_seen"`
Verified bool `json:"verified"` // Manual verification flag
Notes string `json:"notes"`
Metadata map[string]string `json:"metadata,omitempty"`
}
// AddressBook manages a collection of contacts
type AddressBook struct {
contacts map[string]*Contact // key = public key hex
mu sync.RWMutex
path string
}
// NewAddressBook creates a new empty address book
func NewAddressBook() *AddressBook {
return &AddressBook{
contacts: make(map[string]*Contact),
}
}
// LoadAddressBook loads an address book from disk
func LoadAddressBook(path string) (*AddressBook, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to read address book: %w", err)
}
var contacts []*Contact
if err := json.Unmarshal(data, &contacts); err != nil {
return nil, fmt.Errorf("failed to parse address book: %w", err)
}
ab := &AddressBook{
contacts: make(map[string]*Contact),
path: path,
}
for _, contact := range contacts {
ab.contacts[contact.PublicKey] = contact
}
return ab, nil
}
// Save saves the address book to disk
func (ab *AddressBook) Save(path string) error {
ab.mu.RLock()
defer ab.mu.RUnlock()
// Convert map to slice
contacts := make([]*Contact, 0, len(ab.contacts))
for _, contact := range ab.contacts {
contacts = append(contacts, contact)
}
// Marshal to JSON
data, err := json.MarshalIndent(contacts, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal address book: %w", err)
}
// Write to file
if err := os.WriteFile(path, data, 0600); err != nil {
return fmt.Errorf("failed to write address book: %w", err)
}
ab.path = path
return nil
}
// AddContact adds a new contact to the address book
func (ab *AddressBook) AddContact(contact *Contact) error {
ab.mu.Lock()
defer ab.mu.Unlock()
// Validate public key
pubKeyBytes, err := hex.DecodeString(contact.PublicKey)
if err != nil {
return fmt.Errorf("invalid public key hex: %w", err)
}
if len(pubKeyBytes) != ed25519.PublicKeySize {
return fmt.Errorf("invalid public key size: expected %d, got %d", ed25519.PublicKeySize, len(pubKeyBytes))
}
// Generate fingerprint (first 8 bytes of public key)
contact.Fingerprint = hex.EncodeToString(pubKeyBytes[:8])
contact.AddedAt = time.Now()
contact.LastSeen = time.Now()
ab.contacts[contact.PublicKey] = contact
return nil
}
// GetContact retrieves a contact by public key
func (ab *AddressBook) GetContact(publicKey string) (*Contact, bool) {
ab.mu.RLock()
defer ab.mu.RUnlock()
contact, exists := ab.contacts[publicKey]
return contact, exists
}
// GetContactByName retrieves a contact by name (first match)
func (ab *AddressBook) GetContactByName(name string) (*Contact, bool) {
ab.mu.RLock()
defer ab.mu.RUnlock()
for _, contact := range ab.contacts {
if contact.Name == name {
return contact, true
}
}
return nil, false
}
// RemoveContact removes a contact from the address book
func (ab *AddressBook) RemoveContact(publicKey string) bool {
ab.mu.Lock()
defer ab.mu.Unlock()
if _, exists := ab.contacts[publicKey]; exists {
delete(ab.contacts, publicKey)
return true
}
return false
}
// UpdateLastSeen updates the last seen timestamp for a contact
func (ab *AddressBook) UpdateLastSeen(publicKey string) {
ab.mu.Lock()
defer ab.mu.Unlock()
if contact, exists := ab.contacts[publicKey]; exists {
contact.LastSeen = time.Now()
}
}
// MarkVerified marks a contact as manually verified
func (ab *AddressBook) MarkVerified(publicKey string, verified bool) error {
ab.mu.Lock()
defer ab.mu.Unlock()
contact, exists := ab.contacts[publicKey]
if !exists {
return fmt.Errorf("contact not found")
}
contact.Verified = verified
return nil
}
// ListContacts returns all contacts
func (ab *AddressBook) ListContacts() []*Contact {
ab.mu.RLock()
defer ab.mu.RUnlock()
contacts := make([]*Contact, 0, len(ab.contacts))
for _, contact := range ab.contacts {
contacts = append(contacts, contact)
}
return contacts
}
// Count returns the number of contacts
func (ab *AddressBook) Count() int {
ab.mu.RLock()
defer ab.mu.RUnlock()
return len(ab.contacts)
}
// Clear removes all contacts
func (ab *AddressBook) Clear() {
ab.mu.Lock()
defer ab.mu.Unlock()
ab.contacts = make(map[string]*Contact)
}
// VerifyFingerprint checks if a fingerprint matches a public key
func VerifyFingerprint(publicKeyHex, fingerprint string) bool {
pubKeyBytes, err := hex.DecodeString(publicKeyHex)
if err != nil {
return false
}
if len(pubKeyBytes) < 8 {
return false
}
expectedFingerprint := hex.EncodeToString(pubKeyBytes[:8])
return expectedFingerprint == fingerprint
}
// String returns a string representation
func (ab *AddressBook) String() string {
ab.mu.RLock()
defer ab.mu.RUnlock()
return fmt.Sprintf("AddressBook(contacts=%d)", len(ab.contacts))
}
|