/
on_chain_signing_address.go
68 lines (56 loc) · 1.76 KB
/
on_chain_signing_address.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
package ocrkey
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting/types"
)
const onChainSigningAddressPrefix = "ocrsad_"
type OnChainSigningAddress ocrtypes.OnChainSigningAddress
func (ocsa OnChainSigningAddress) String() string {
return fmt.Sprintf("%s%s", onChainSigningAddressPrefix, hexutil.Encode(ocsa[:]))
}
func (ocsa OnChainSigningAddress) MarshalJSON() ([]byte, error) {
return json.Marshal(ocsa.String())
}
func (ocsa *OnChainSigningAddress) UnmarshalJSON(input []byte) error {
var hexString string
if err := json.Unmarshal(input, &hexString); err != nil {
return err
}
return ocsa.UnmarshalText([]byte(hexString))
}
func (ocsa *OnChainSigningAddress) UnmarshalText(bs []byte) error {
input := string(bs)
if strings.HasPrefix(input, onChainSigningAddressPrefix) {
input = string(bs[len(onChainSigningAddressPrefix):])
}
result, err := hexutil.Decode(input)
if err != nil {
return err
}
var onChainSigningAddress common.Address
copy(onChainSigningAddress[:], result[:common.AddressLength])
*ocsa = OnChainSigningAddress(onChainSigningAddress)
return nil
}
func (ocsa OnChainSigningAddress) Value() (driver.Value, error) {
byteArray := [common.AddressLength]byte(ocsa)
return byteArray[:], nil
}
func (ocsa *OnChainSigningAddress) Scan(value interface{}) error {
switch typed := value.(type) {
case []byte:
if len(typed) != common.AddressLength {
return errors.New("wrong number of bytes to scan into address")
}
copy(ocsa[:], typed)
return nil
default:
return errors.Errorf(`unable to convert %v of %T to OnChainSigningAddress`, value, value)
}
}