-
Notifications
You must be signed in to change notification settings - Fork 0
/
public_key.go
86 lines (74 loc) · 1.74 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package mint
import (
"encoding/json"
"fmt"
)
// PublicKeySize is public key length in bytes
const PublicKeySize = 32
// PublicKey bytes
type PublicKey [PublicKeySize]byte
// Bytes of the instance
func (p PublicKey) Bytes() []byte {
b := make([]byte, len(p[:]))
copy(b, p[:])
return b
}
// String packs the instance into a Base58 string
func (p PublicKey) String() string {
return Pack58(p[:])
}
// StringMask packs the instance into 6+4 a masked Base58 string
func (p PublicKey) StringMask() string {
return MaskString6P4(p.String())
}
// MarshalJSON impl
func (p PublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(p.String())
}
// UnmarshalJSON impl
func (p *PublicKey) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
x, err := ParsePublicKey(s)
if err != nil {
return err
}
copy(p[:], x[:])
return nil
}
// ParsePublicKey parses an instance from Base58 string
func ParsePublicKey(s string) (PublicKey, error) {
var v PublicKey
b, err := Unpack58(s)
if err != nil {
return v, err
}
return BytesToPublicKey(b)
}
// MustParsePublicKey parses an instance from Base58 string or panics
func MustParsePublicKey(s string) PublicKey {
v, err := ParsePublicKey(s)
if err != nil {
panic(err)
}
return v
}
// BytesToPublicKey creates an instance from a bytes slice
func BytesToPublicKey(b []byte) (PublicKey, error) {
var v PublicKey
if len(b) != PublicKeySize {
return v, fmt.Errorf("invalid length, got %v expected %v", len(b), PublicKeySize)
}
copy(v[:], b)
return v, nil
}
// MustBytesToPublicKey creates an instance from bytes slice or panics
func MustBytesToPublicKey(b []byte) PublicKey {
v, err := BytesToPublicKey(b)
if err != nil {
panic(err)
}
return v
}