/
handler.go
80 lines (63 loc) · 1.71 KB
/
handler.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
package comm
import (
"context"
"time"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/pkg/errors"
"github.com/tcfw/didem/internal/utils/logging"
"github.com/tcfw/didem/pkg/comm"
"github.com/tcfw/didem/pkg/node"
)
const (
ProtocolID = protocol.ID("/did/comm/0.0.1")
helloLimit = 1 << 10
helloTimeWindow = 5 * time.Second
)
type Handler struct {
n node.Node
receiveTimeout time.Duration
store comm.Store
}
func NewHandler(n node.Node) *Handler {
return &Handler{n: n}
}
func (h *Handler) Handle(stream network.Stream) {
srv := NewServerHandler(stream, h.store, h.n.ID())
ctx, cancel := context.WithTimeout(context.Background(), h.receiveTimeout)
defer cancel()
if err := srv.handle(ctx); err != nil {
logging.WithError(err).Error("handling comm stream")
}
}
func (h *Handler) Send(ctx context.Context, email *comm.Message, opts ...comm.SendOption) error {
cfg := comm.NewDefaultSendConfig()
for _, opt := range opts {
if err := opt(cfg); err != nil {
return errors.Wrap(err, "building config")
}
}
return h.send(ctx, email, cfg)
}
func (h *Handler) send(ctx context.Context, email *comm.Message, cfg *comm.SendConfig) error {
if cfg.Id == nil {
return errors.New("sender identity missing")
}
if email.To == nil {
return errors.New("recipient identity missing")
}
if cfg.PublicResolver == nil {
return errors.New("no resolver configure")
}
rDID, err := cfg.PublicResolver.ResolvePI(email.To[0].ID)
if err != nil {
return errors.Wrap(err, "resolving recipient")
}
client := &ClientHandler{
n: h.n,
email: email,
recipient: rDID,
resolver: h.n.Resolver(),
}
return client.handle(ctx)
}