forked from BytomDAO/bytom
/
serialize.go
60 lines (49 loc) · 1.14 KB
/
serialize.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
package chainkd
import (
"encoding/hex"
"errors"
)
const (
extendedPublicKeySize = 64
extendedPrivateKeySize = 64
)
var (
ErrBadKeyLen = errors.New("bad key length")
ErrBadKeyStr = errors.New("bad key string")
)
func (xpub XPub) MarshalText() ([]byte, error) {
hexBytes := make([]byte, hex.EncodedLen(len(xpub.Bytes())))
hex.Encode(hexBytes, xpub.Bytes())
return hexBytes, nil
}
func (xpub XPub) Bytes() []byte {
return xpub[:]
}
func (xprv XPrv) MarshalText() ([]byte, error) {
hexBytes := make([]byte, hex.EncodedLen(len(xprv.Bytes())))
hex.Encode(hexBytes, xprv.Bytes())
return hexBytes, nil
}
func (xprv XPrv) Bytes() []byte {
return xprv[:]
}
func (xpub *XPub) UnmarshalText(inp []byte) error {
if len(inp) != 2*extendedPublicKeySize {
return ErrBadKeyStr
}
_, err := hex.Decode(xpub[:], inp)
return err
}
func (xpub XPub) String() string {
return hex.EncodeToString(xpub.Bytes())
}
func (xprv *XPrv) UnmarshalText(inp []byte) error {
if len(inp) != 2*extendedPrivateKeySize {
return ErrBadKeyStr
}
_, err := hex.Decode(xprv[:], inp)
return err
}
func (xprv XPrv) String() string {
return hex.EncodeToString(xprv.Bytes())
}