forked from tendermint/tendermint
/
upgrade.go
59 lines (51 loc) · 1.48 KB
/
upgrade.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
package types
import (
"encoding/json"
"io/ioutil"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common"
)
type PrivValidatorV1 struct {
Address cmn.HexBytes `json:"address"`
PubKey crypto.PubKey `json:"pub_key"`
LastHeight int64 `json:"last_height"`
LastRound int `json:"last_round"`
LastStep int8 `json:"last_step"`
LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures
LastSignBytes cmn.HexBytes `json:"last_signbytes,omitempty"` // so we dont lose signatures
PrivKey crypto.PrivKey `json:"priv_key"`
}
func UpgradePrivValidator(filePath string) (*PrivValidatorJSON, error) {
b, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
pv := new(PrivValidatorV1)
err = json.Unmarshal(b, pv)
if err != nil {
return nil, err
}
pvNew := &PrivValidatorJSON{
PrivValidatorUnencrypted: &PrivValidatorUnencrypted{
ID: types.ValidatorID{
Address: pv.Address,
PubKey: pv.PubKey,
},
PrivKey: PrivKey(pv.PrivKey),
LastSignedInfo: &LastSignedInfo{
Height: pv.LastHeight,
Round: pv.LastRound,
Step: pv.LastStep,
SignBytes: pv.LastSignBytes,
Signature: pv.LastSignature,
},
},
}
b, err = json.MarshalIndent(pvNew, "", " ")
if err != nil {
return nil, err
}
err = ioutil.WriteFile(filePath, b, 0600)
return pvNew, err
}