-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnode_info.go
143 lines (124 loc) · 3.95 KB
/
node_info.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
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
package main
import (
"context"
"encoding/hex"
"errors"
"fmt"
"math"
"strconv"
"time"
"github.com/breez/server/breez"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lightningnetwork/lnd/lnwire"
)
var (
ErrKeyNotSupported = errors.New("key is not supported")
ErrInvalidTimestamp = errors.New("invalid timestamp")
allowedKeys = map[string]struct{}{
"routing_hints": {},
}
maxRequesTimeDiff = time.Second * 10
)
func verifyMessage(msg, pubKey, signature []byte) (bool, error) {
if msg == nil {
return false, fmt.Errorf("a message to verify MUST be passed in")
}
if signature == nil {
return false, fmt.Errorf("a signature to verify MUST be passed in")
}
if pubKey == nil {
return false, fmt.Errorf("a pubkey to verify MUST be passed in")
}
pubkey, err := btcec.ParsePubKey(pubKey)
if err != nil {
return false, fmt.Errorf("unable to parse pubkey: %v", err)
}
// The signature must be fixed-size LN wire format encoded.
wireSig, err := lnwire.NewSigFromRawSignature(signature)
if err != nil {
return false, fmt.Errorf("failed to decode signature: %v", err)
}
sig, err := wireSig.ToSignature()
if err != nil {
return false, fmt.Errorf("failed to convert from wire format: %v", err)
}
// The signature is over the sha256 hash of the message.
digest := chainhash.HashB(msg)
valid := sig.Verify(digest, pubkey)
return valid, nil
}
// SetNodeInfo sets the meeting information by the meeting moderator. The moderator provides a proof by signing the value
func (s *server) SetNodeInfo(ctx context.Context, in *breez.SetNodeInfoRequest) (*breez.SetNodeInfoResponse, error) {
if _, ok := allowedKeys[in.Key]; !ok {
return nil, ErrKeyNotSupported
}
requestTimeDiff := time.Since(time.Unix(in.Timestamp, 0))
if math.Abs(float64(requestTimeDiff)) > float64(maxRequesTimeDiff) {
return nil, ErrInvalidTimestamp
}
// concatenate all request payload fields
msg := fmt.Sprintf("%v-%v-%v", in.Key, hex.EncodeToString(in.Value), in.Timestamp)
// Verify the message
valid, err := verifyMessage([]byte(msg), in.Pubkey, in.Signature)
if err != nil {
return nil, err
}
if !valid {
return nil, errors.New("failed to verify value")
}
// Update the value in redis and set expiration of 1 hour.
redisKey := fmt.Sprintf("%v-%v", hex.EncodeToString(in.Pubkey), in.Key)
if err := updateKeyFields(redisKey, map[string]string{
"value": hex.EncodeToString(in.Value),
"timestamp": strconv.FormatInt(in.Timestamp, 10),
"signature": hex.EncodeToString(in.Signature),
}); err != nil {
return nil, fmt.Errorf("failed to update value")
}
if err := setKeyExpiration(redisKey, 3600); err != nil {
return nil, err
}
return &breez.SetNodeInfoResponse{}, nil
}
// GetNodeInfo is used by other participants to get the meeting information. They should verify the message using th
// provided signature.
func (s *server) GetNodeInfo(ctx context.Context, in *breez.GetNodeInfoRequest) (*breez.GetNodeInfoResponse, error) {
if _, ok := allowedKeys[in.Key]; !ok {
return nil, ErrKeyNotSupported
}
redisKey := fmt.Sprintf("%v-%v", hex.EncodeToString(in.Pubkey), in.Key)
fields, err := getKeyFields(redisKey)
if err != nil {
return nil, err
}
value, ok := fields["value"]
if !ok {
return nil, fmt.Errorf("failed to get value")
}
valueBytes, err := hex.DecodeString(value)
if err != nil {
return nil, fmt.Errorf("failed to get value")
}
signature, ok := fields["signature"]
if !ok {
return nil, fmt.Errorf("failed to get value")
}
signatureBytes, err := hex.DecodeString(signature)
if err != nil {
return nil, fmt.Errorf("failed to get value")
}
timestampStr, ok := fields["timestamp"]
if !ok {
return nil, fmt.Errorf("failed to get value")
}
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to get value")
}
return &breez.GetNodeInfoResponse{
Value: valueBytes,
Timestamp: timestamp,
Signature: signatureBytes,
}, nil
}