/
encoding.go
74 lines (64 loc) · 2.3 KB
/
encoding.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
package pubsubPlebbitValidator
import (
"errors"
codec "github.com/ugorji/go/codec"
cbor "github.com/fxamacker/cbor/v2"
"bytes"
)
func cborDecode(encoded []byte) (map[string]interface{}, error) {
cborHandle := &codec.CborHandle{}
cborHandle.Canonical = true
var decoder = codec.NewDecoderBytes(encoded, cborHandle)
var decoded map[string]interface{}
err := decoder.Decode(&decoded)
if (err != nil) {
return decoded, err
}
return decoded, err
}
func cborEncode(decoded map[string]interface{}) ([]byte) {
// Object properties are sorted according to the original RFC 7049 canonical representation recommended method: length-first and then bytewise.
options := cbor.CTAP2EncOptions()
encMode, _ := options.EncMode()
var encoded bytes.Buffer
encoder := encMode.NewEncoder(&encoded)
encoder.Encode(decoded)
return encoded.Bytes()
}
type Signature struct {
signature []byte
publicKey []byte
signedPropertyNames []string
}
// convert the cbor decoded message["signature"] to a usable Signature
func toSignature(_messageSignature interface{}) (Signature, error) {
messageSignature, ok := _messageSignature.(map[interface{}]interface{})
if !ok {
return Signature{}, errors.New("failed convert message.signature to map[interface{}]interface{}")
}
signature, ok := messageSignature["signature"].([]byte)
if !ok {
return Signature{}, errors.New("failed convert message.signature.signature to []byte")
}
publicKey, ok := messageSignature["publicKey"].([]byte)
if !ok {
return Signature{}, errors.New("failed convert message.signature.publicKey to []byte")
}
_signedPropertyNames, ok := messageSignature["signedPropertyNames"].([]interface{})
if !ok {
return Signature{}, errors.New("failed convert message.signature.signedPropertyNames to []string")
}
signedPropertyNames := make([]string, len(_signedPropertyNames))
for i, name := range _signedPropertyNames {
str, ok := name.(string)
if !ok {
return Signature{}, errors.New("failed convert message.signature.signedPropertyNames to []string")
}
signedPropertyNames[i] = str
}
return Signature{
signature,
publicKey,
signedPropertyNames,
}, nil
}