-
Notifications
You must be signed in to change notification settings - Fork 2
/
pk_pub.go
138 lines (110 loc) · 2.95 KB
/
pk_pub.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package types
import (
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
"strings"
"crypto/ecdsa"
"crypto/sha256"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/hint"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pkg/errors"
)
// MEPublickey is the optional public key of mitum, it is based on Ether Privatekey.
type MEPublickey struct {
k *ecdsa.PublicKey
s string
b []byte
hint.BaseHinter
}
func NewMEPublickey(k *ecdsa.PublicKey) MEPublickey {
pub := MEPublickey{
BaseHinter: hint.NewBaseHinter(MEPublickeyHint),
k: k,
}
return pub.ensure()
}
func ParseMEPublickey(s string) (MEPublickey, error) {
t := MEPublickeyHint.Type().String()
switch {
case !strings.HasSuffix(s, t):
return MEPublickey{}, util.ErrInvalid.Errorf("Unknown public key string")
case len(s) <= len(t):
return MEPublickey{}, util.ErrInvalid.Errorf("Invalid public key string; too short")
}
return LoadMEPublicKey(s[:len(s)-len(t)])
}
func LoadMEPublicKey(s string) (MEPublickey, error) {
h, err := hex.DecodeString(s)
if err != nil {
return MEPublickey{}, util.ErrInvalid.WithMessage(err, "load public key")
}
pk, err := crypto.DecompressPubkey(h)
if err != nil {
return MEPublickey{}, util.ErrInvalid.WithMessage(err, "unmarshal public key")
}
return NewMEPublickey(pk), nil
}
func (k MEPublickey) String() string {
return k.s
}
func (k MEPublickey) Bytes() []byte {
return k.b
}
func (k MEPublickey) IsValid([]byte) error {
if err := k.BaseHinter.IsValid(MEPublickeyHint.Type().Bytes()); err != nil {
return util.ErrInvalid.WithMessage(err, "wrong hint in public key")
}
switch {
case k.k == nil:
return util.ErrInvalid.Errorf("Empty btc public key in MEPublickey")
case len(k.s) < 1:
return util.ErrInvalid.Errorf("Empty public key string")
case len(k.b) < 1:
return util.ErrInvalid.Errorf("Empty public key []byte")
}
return nil
}
func (k MEPublickey) Equal(b base.PKKey) bool {
switch {
case b == nil:
return false
default:
return k.s == b.String()
}
}
func (k MEPublickey) Verify(input []byte, sig base.Signature) error {
if len(sig) < 4 {
return base.ErrSignatureVerification.WithStack()
}
rlength := int(binary.LittleEndian.Uint32(sig[:4]))
r := big.NewInt(0).SetBytes(sig[4 : 4+rlength])
s := big.NewInt(0).SetBytes(sig[4+rlength:])
h := sha256.Sum256(input)
if !ecdsa.Verify(k.k, h[:], r, s) {
return base.ErrSignatureVerification.WithStack()
}
return nil
}
func (k MEPublickey) MarshalText() ([]byte, error) {
return []byte(k.s), nil
}
func (k *MEPublickey) UnmarshalText(b []byte) error {
u, err := LoadMEPublicKey(string(b))
if err != nil {
return errors.Wrap(err, "UnmarshalText for public key")
}
*k = u.ensure()
return nil
}
func (k *MEPublickey) ensure() MEPublickey {
if k.k == nil {
return *k
}
k.s = fmt.Sprintf("%s%s", hex.EncodeToString(crypto.CompressPubkey(k.k)), k.Hint().Type().String())
k.b = []byte(k.s)
return *k
}