-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprotocol_host.go
More file actions
60 lines (48 loc) · 1.14 KB
/
Copy pathprotocol_host.go
File metadata and controls
60 lines (48 loc) · 1.14 KB
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
package main
import (
"context"
"crypto/rand"
"io"
"time"
)
type protocolClock interface {
Now() (time.Time, error)
}
type protocolEntropy interface {
Read([]byte) error
}
type protocolHostDeps struct {
Clock protocolClock
Entropy protocolEntropy
}
type wallProtocolClock struct{}
func (wallProtocolClock) Now() (time.Time, error) {
return time.Now(), nil
}
type cryptoProtocolEntropy struct{}
func (cryptoProtocolEntropy) Read(dst []byte) error {
_, err := io.ReadFull(rand.Reader, dst)
return err
}
func productionProtocolHostDeps() protocolHostDeps {
return protocolHostDeps{
Clock: wallProtocolClock{},
Entropy: cryptoProtocolEntropy{},
}
}
type protocolHostDepsContextKey struct{}
func withProtocolHostDeps(ctx context.Context, deps protocolHostDeps) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, protocolHostDepsContextKey{}, deps)
}
func protocolHostDepsFor(ctx context.Context, fallback protocolHostDeps) protocolHostDeps {
if ctx == nil {
return fallback
}
if deps, ok := ctx.Value(protocolHostDepsContextKey{}).(protocolHostDeps); ok {
return deps
}
return fallback
}