-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_public_key.go
52 lines (41 loc) · 1.15 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
package ocrkey
import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"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))
}
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
}