-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexPubkeyConverter.go
75 lines (60 loc) · 2.25 KB
/
hexPubkeyConverter.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
75
package pubkeyConverter
import (
"encoding/hex"
"fmt"
"github.com/subrahamanyam341/andes-core-16/core"
)
// hexPubkeyConverter encodes or decodes provided public key as/from hex
type hexPubkeyConverter struct {
len int
}
// NewHexPubkeyConverter returns a hexPubkeyConverter instance
func NewHexPubkeyConverter(addressLen int) (*hexPubkeyConverter, error) {
if addressLen < 1 {
return nil, fmt.Errorf("%w when creating hex address converter, addressLen should have been greater than 0",
ErrInvalidAddressLength)
}
if addressLen%2 == 1 {
return nil, fmt.Errorf("%w when creating hex address converter, addressLen should have been an even number",
ErrInvalidAddressLength)
}
return &hexPubkeyConverter{
len: addressLen,
}, nil
}
// Decode converts the provided public key string as hex decoded bytes
func (ppc *hexPubkeyConverter) Decode(humanReadable string) ([]byte, error) {
buff, err := hex.DecodeString(humanReadable)
if err != nil {
return nil, err
}
if len(buff) != ppc.len {
return nil, fmt.Errorf("%w when converting to address, expected length %d, received %d",
ErrWrongSize, ppc.len, len(buff))
}
return buff, nil
}
// Encode converts the provided bytes in a form that this converter can decode. In this case it will encode to hex
func (ppc *hexPubkeyConverter) Encode(pkBytes []byte) (string, error) {
return hex.EncodeToString(pkBytes), nil
}
// EncodeSlice converts the provided bytes slice in a form that this converter can decode. In this case it will encode to hex
func (ppc *hexPubkeyConverter) EncodeSlice(pkBytesSlice [][]byte) ([]string, error) {
encodedSlice := make([]string, 0, len(pkBytesSlice))
for _, item := range pkBytesSlice {
encodedSlice = append(encodedSlice, hex.EncodeToString(item))
}
return encodedSlice, nil
}
// SilentEncode converts the provided bytes in a form that this converter can decode. In this case it will encode to hex
func (ppc *hexPubkeyConverter) SilentEncode(pkBytes []byte, log core.Logger) string {
return hex.EncodeToString(pkBytes)
}
// Len returns the decoded address length
func (ppc *hexPubkeyConverter) Len() int {
return ppc.len
}
// IsInterfaceNil returns true if there is no value under the interface
func (ppc *hexPubkeyConverter) IsInterfaceNil() bool {
return ppc == nil
}