-
Notifications
You must be signed in to change notification settings - Fork 12
/
secp256k1.go
113 lines (88 loc) · 2.39 KB
/
secp256k1.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
package keypair
import (
"github.com/tendermint/tendermint/crypto/secp256k1"
. "github.com/yu-org/yu/common"
)
// ------ Public Key ------
type SecpPubkey struct {
pubkey secp256k1.PubKey
}
func SecpPubkeyFromBytes(data []byte) *SecpPubkey {
return &SecpPubkey{pubkey: data}
}
func (spb *SecpPubkey) Address() Address {
addressByt := spb.pubkey.Address().Bytes()
return BytesToAddress(addressByt)
}
func (spb *SecpPubkey) VerifySignature(msg, sig []byte) bool {
return spb.pubkey.VerifySignature(msg, sig)
}
func (spb *SecpPubkey) Type() string {
return spb.pubkey.Type()
}
func (spb *SecpPubkey) Equals(key Key) bool {
srKey, ok := key.(*SecpPubkey)
if !ok {
return false
}
return spb.pubkey.Equals(srKey.pubkey)
}
func (spb *SecpPubkey) Bytes() []byte {
return spb.pubkey.Bytes()
}
func (spb *SecpPubkey) String() string {
return ToHex(spb.Bytes())
}
func (spb *SecpPubkey) BytesWithType() []byte {
return append([]byte(Sr25519Idx), spb.pubkey.Bytes()...)
}
func (spb *SecpPubkey) StringWithType() string {
return ToHex(spb.BytesWithType())
}
// ----- Private Key ------
type SecpPrivkey struct {
privkey secp256k1.PrivKey
}
func SecpPrivkeyFromBytes(data []byte) *SecpPrivkey {
return &SecpPrivkey{privkey: data}
}
func (spr *SecpPrivkey) SignData(data []byte) ([]byte, error) {
return spr.privkey.Sign(data)
}
func (spr *SecpPrivkey) Type() string {
return spr.privkey.Type()
}
func (spr *SecpPrivkey) Equals(key Key) bool {
srKey, ok := key.(*SecpPrivkey)
if !ok {
return false
}
return spr.privkey.Equals(srKey.privkey)
}
func (spr *SecpPrivkey) Bytes() []byte {
return spr.privkey.Bytes()
}
func (spr *SecpPrivkey) String() string {
return ToHex(spr.Bytes())
}
func (spr *SecpPrivkey) BytesWithType() []byte {
return append([]byte(Sr25519Idx), spr.privkey.Bytes()...)
}
func (spr *SecpPrivkey) StringWithType() string {
return ToHex(spr.BytesWithType())
}
func (spr *SecpPrivkey) GenPubkey() []byte {
return spr.privkey.PubKey().Bytes()
}
func GenSecpKeyWithSecret(secret []byte) (PubKey, PrivKey) {
secpPrivkey := secp256k1.GenPrivKeySecp256k1(secret)
privkey := &SecpPrivkey{secpPrivkey}
pubkey := &SecpPubkey{secpPrivkey.PubKey().(secp256k1.PubKey)}
return pubkey, privkey
}
func GenSecpKey() (PubKey, PrivKey) {
secpPrivkey := secp256k1.GenPrivKey()
privkey := &SecpPrivkey{secpPrivkey}
pubkey := &SecpPubkey{secpPrivkey.PubKey().(secp256k1.PubKey)}
return pubkey, privkey
}