-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
42 lines (38 loc) · 1 KB
/
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
package validator
import (
"encoding/json"
"fmt"
"github.com/ci123chain/ci123chain/pkg/app/types"
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto/ed25519"
)
type PubKey struct {
Type string `json:"type"`
Value string `json:"value"`
}
func NewValidatorKey() (validatorKey, pubKeyStr, address string, err error) {
var valKey ed25519.PrivKey
validator := ed25519.GenPrivKey()
cdc := amino.NewCodec()
keyByte, err := cdc.MarshalJSON(validator)
if err != nil {
return "","", "", err
}
validatorKey = string(keyByte[1:len(keyByte)-1])
privStr := fmt.Sprintf(`{"type":"%s","value":"%s"}`, ed25519.PrivKeyName, validatorKey)
cdc = types.GetCodec()
err = cdc.UnmarshalJSON([]byte(privStr), &valKey)
if err != nil {
return "","", "", err
}
valPubKey := valKey.PubKey()
address = valPubKey.Address().String()
var pubKey PubKey
pb, _ := cdc.MarshalJSON(valPubKey)
err = json.Unmarshal(pb, &pubKey)
if err != nil {
return "", "", "", err
}
pubKeyStr = pubKey.Value
return
}