-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
config_public_key.go
72 lines (59 loc) · 1.74 KB
/
config_public_key.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
package ocrkey
import (
"database/sql/driver"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
"golang.org/x/crypto/curve25519"
)
const configPublicKeyPrefix = "ocrcfg_"
// ConfigPublicKey represents the public key for the config decryption keypair
type ConfigPublicKey [curve25519.PointSize]byte
func (cpk ConfigPublicKey) String() string {
return fmt.Sprintf("%s%s", configPublicKeyPrefix, cpk.Raw())
}
func (cpk ConfigPublicKey) Raw() string {
return hex.EncodeToString(cpk[:])
}
func (cpk ConfigPublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(cpk.String())
}
func (cpk *ConfigPublicKey) UnmarshalJSON(input []byte) error {
var hexString string
if err := json.Unmarshal(input, &hexString); err != nil {
return err
}
return cpk.UnmarshalText([]byte(hexString))
}
// Scan reads the database value and returns an instance.
func (cpk *ConfigPublicKey) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
return errors.Errorf("unable to convert %v of type %T to ConfigPublicKey", value, value)
}
if len(b) != curve25519.PointSize {
return errors.Errorf("unable to convert blob 0x%x of length %v to ConfigPublicKey", b, len(b))
}
copy(cpk[:], b)
return nil
}
// Value returns this instance serialized for database storage.
func (cpk ConfigPublicKey) Value() (driver.Value, error) {
return cpk[:], nil
}
func (cpk *ConfigPublicKey) UnmarshalText(bs []byte) error {
input := string(bs)
if strings.HasPrefix(input, configPublicKeyPrefix) {
input = string(bs[len(configPublicKeyPrefix):])
}
decodedString, err := hex.DecodeString(input)
if err != nil {
return err
}
var result [curve25519.PointSize]byte
copy(result[:], decodedString[:curve25519.PointSize])
*cpk = result
return nil
}