Newer
Older
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
package keys
import (
"github.com/hyperledger/burrow/crypto"
)
type Key struct {
CurveType crypto.CurveType
Address crypto.Address
PublicKey crypto.PublicKey
PrivateKey crypto.PrivateKey
}
func NewKey(typ crypto.CurveType) (*Key, error) {
privKey, err := crypto.GeneratePrivateKey(nil, typ)
if err != nil {
return nil, err
}
pubKey := privKey.GetPublicKey()
return &Key{
CurveType: typ,
PublicKey: pubKey,
Address: pubKey.Address(),
PrivateKey: privKey,
}, nil
}
func (k *Key) Pubkey() []byte {
return k.PublicKey.RawBytes()
}
func NewKeyFromPub(curveType crypto.CurveType, PubKeyBytes []byte) (*Key, error) {
pubKey, err := crypto.PublicKeyFromBytes(PubKeyBytes, curveType)
if err != nil {
return nil, err
}
return &Key{
CurveType: curveType,
PublicKey: pubKey,
Address: pubKey.Address(),
}, nil
}
func NewKeyFromPriv(curveType crypto.CurveType, PrivKeyBytes []byte) (*Key, error) {
privKey, err := crypto.PrivateKeyFromRawBytes(PrivKeyBytes, curveType)
if err != nil {
return nil, err
}
pubKey := privKey.GetPublicKey()
return &Key{
CurveType: curveType,
Address: pubKey.Address(),
PublicKey: pubKey,
PrivateKey: privKey,
}, nil
}
func (k *Key) Sign(hash []byte) ([]byte, error) {
signature, err := k.PrivateKey.Sign(hash)
if err != nil {
return nil, err
}
return signature.RawBytes(), nil
}