-
Notifications
You must be signed in to change notification settings - Fork 4
/
serverops.go
91 lines (77 loc) · 2.41 KB
/
serverops.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
// Copyright (c) 2015 Monetas.
// Copyright 2016 Daniel Krawisz.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"time"
"github.com/DanielKrawisz/bmagent/idmgr/keys"
"github.com/DanielKrawisz/bmagent/user"
"github.com/DanielKrawisz/bmagent/user/email"
"github.com/DanielKrawisz/bmutil/identity"
"github.com/DanielKrawisz/bmutil/wire"
)
// serverOps implements the email.ServerOps interface.
type serverOps struct {
pubIDs map[string]identity.Public // a cache
id uint32
user *User
server *server
}
// GetOrRequestPublic attempts to retreive a public identity for the given
// address. If the function returns nil with no error, that means that a pubkey
// request was successfully queued for proof-of-work.
func (s *serverOps) GetOrRequestPublicID(emailAddress string) (identity.Public, error) {
addr, err := email.ToBm(emailAddress)
if err != nil {
return nil, err
}
if addr == email.Broadcast {
return user.Broadcast, nil
}
serverLog.Debug("GetOrRequestPublicID for ", addr)
// Check the map of cached identities.
identity, ok := s.pubIDs[addr]
if ok {
serverLog.Debug("GetOrRequestPublicID: identity found ", addr)
return identity, nil
}
// Check the private identities, just in case.
private := s.GetPrivateID(addr)
if private != nil {
return private.Public(), nil
}
pubID, err := s.server.getOrRequestPublicIdentity(s.id, addr)
if err != nil { // Some error occured.
return nil, err
}
if pubID == nil && err == nil { // Getpubkey request sent.
return nil, email.ErrGetPubKeySent
}
s.pubIDs[addr] = pubID
return pubID, nil
}
// GetPrivateID queries the key manager for the right private key for the given
// address.
func (s *serverOps) GetPrivateID(addr string) *keys.PrivateID {
return s.user.Keys.Get(addr)
}
// ObjectExpiration returns the time duration after which an object of the
// given type will expire on the network. It's used for POW calculations.
func ObjectExpiration(objType wire.ObjectType) time.Duration {
switch objType {
case wire.ObjectTypeMsg:
return cfg.MsgExpiry
case wire.ObjectTypeBroadcast:
return cfg.BroadcastExpiry
case wire.ObjectTypeGetPubKey:
return defaultGetpubkeyExpiry
case wire.ObjectTypePubKey:
return defaultPubkeyExpiry
default:
return defaultUnknownObjExpiry
}
}
func (s *serverOps) Send(obj []byte) { // Send the object out on the network.
s.server.Send(obj)
}