-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
off_chain_public_key.go
67 lines (54 loc) · 1.41 KB
/
off_chain_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
package ocrkey
import (
"crypto/ed25519"
"database/sql/driver"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/ethereum/go-ethereum/common"
)
const offChainPublicKeyPrefix = "ocroff_"
type OffChainPublicKey ed25519.PublicKey
func (ocpk OffChainPublicKey) String() string {
return fmt.Sprintf("%s%s", offChainPublicKeyPrefix, ocpk.Raw())
}
func (ocpk OffChainPublicKey) Raw() string {
return hex.EncodeToString(ocpk)
}
func (ocpk OffChainPublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(ocpk.String())
}
func (ocpk *OffChainPublicKey) UnmarshalJSON(input []byte) error {
var hexString string
if err := json.Unmarshal(input, &hexString); err != nil {
return err
}
return ocpk.UnmarshalText([]byte(hexString))
}
func (ocpk *OffChainPublicKey) UnmarshalText(bs []byte) error {
input := string(bs)
if strings.HasPrefix(input, offChainPublicKeyPrefix) {
input = string(bs[len(offChainPublicKeyPrefix):])
}
result, err := hex.DecodeString(input)
if err != nil {
return err
}
copy(result[:], result[:common.AddressLength])
*ocpk = result
return nil
}
func (ocpk *OffChainPublicKey) Scan(value interface{}) error {
switch v := value.(type) {
case []byte:
*ocpk = v
return nil
default:
return errors.Errorf("invalid public key bytes got %T wanted []byte", v)
}
}
func (ocpk OffChainPublicKey) Value() (driver.Value, error) {
return []byte(ocpk), nil
}