-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
55 lines (42 loc) · 1.18 KB
/
utils.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
package lnsocket
import (
"crypto/rand"
lnwire "github.com/lightningnetwork/lnd/lnwire"
)
// ParseMsgType parses the type of message
func ParseMsgType(bytes []byte) uint16 {
return uint16(bytes[0])<<8 | uint16(bytes[1])
}
// GetMandatoryFeatures gets mandatory features
func GetMandatoryFeatures(init *lnwire.Init) map[lnwire.FeatureBit]struct{} {
result := make(map[lnwire.FeatureBit]struct{})
vector := lnwire.NewFeatureVector(init.Features, nil)
for one := range vector.Features() {
bit := uint16(one)
if bit&1 == 0 {
result[one] = struct{}{}
}
}
global := lnwire.NewFeatureVector(init.GlobalFeatures, nil)
for one := range global.Features() {
bit := uint16(one)
if bit&1 == 0 {
result[one] = struct{}{}
}
}
return result
}
// ToRawFeatureVector gets a raw feature vector
func ToRawFeatureVector(features map[lnwire.FeatureBit]struct{}) *lnwire.RawFeatureVector {
all := make([]lnwire.FeatureBit, 0)
for feature := range features {
all = append(all, feature)
}
return lnwire.NewRawFeatureVector(all...)
}
// GenRandomBytes gets random bytes
func GenRandomBytes(size int) (blk []byte, err error) {
blk = make([]byte, size)
_, err = rand.Read(blk)
return
}