You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.0 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "time"
  9. "github.com/tendermint/go-amino"
  10. "github.com/tendermint/tendermint/crypto/ed25519"
  11. cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
  12. cmn "github.com/tendermint/tendermint/libs/common"
  13. "github.com/tendermint/tendermint/p2p"
  14. "github.com/tendermint/tendermint/privval"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. type GenesisValidator struct {
  18. PubKey Data `json:"pub_key"`
  19. Power int64 `json:"power"`
  20. Name string `json:"name"`
  21. }
  22. type Genesis struct {
  23. GenesisTime time.Time `json:"genesis_time"`
  24. ChainID string `json:"chain_id"`
  25. ConsensusParams *types.ConsensusParams `json:"consensus_params,omitempty"`
  26. Validators []GenesisValidator `json:"validators"`
  27. AppHash cmn.HexBytes `json:"app_hash"`
  28. AppState json.RawMessage `json:"app_state,omitempty"`
  29. AppOptions json.RawMessage `json:"app_options,omitempty"` // DEPRECATED
  30. }
  31. type NodeKey struct {
  32. PrivKey Data `json:"priv_key"`
  33. }
  34. type PrivVal struct {
  35. Address cmn.HexBytes `json:"address"`
  36. LastHeight int64 `json:"last_height"`
  37. LastRound int `json:"last_round"`
  38. LastStep int8 `json:"last_step"`
  39. PubKey Data `json:"pub_key"`
  40. PrivKey Data `json:"priv_key"`
  41. }
  42. type Data struct {
  43. Type string `json:"type"`
  44. Data cmn.HexBytes `json:"data"`
  45. }
  46. func convertNodeKey(cdc *amino.Codec, jsonBytes []byte) ([]byte, error) {
  47. var nodeKey NodeKey
  48. err := json.Unmarshal(jsonBytes, &nodeKey)
  49. if err != nil {
  50. return nil, err
  51. }
  52. var privKey ed25519.PrivKeyEd25519
  53. copy(privKey[:], nodeKey.PrivKey.Data)
  54. nodeKeyNew := p2p.NodeKey{privKey}
  55. bz, err := cdc.MarshalJSON(nodeKeyNew)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return bz, nil
  60. }
  61. func convertPrivVal(cdc *amino.Codec, jsonBytes []byte) ([]byte, error) {
  62. var privVal PrivVal
  63. err := json.Unmarshal(jsonBytes, &privVal)
  64. if err != nil {
  65. return nil, err
  66. }
  67. var privKey ed25519.PrivKeyEd25519
  68. copy(privKey[:], privVal.PrivKey.Data)
  69. var pubKey ed25519.PubKeyEd25519
  70. copy(pubKey[:], privVal.PubKey.Data)
  71. privValNew := privval.FilePV{
  72. Address: pubKey.Address(),
  73. PubKey: pubKey,
  74. LastHeight: privVal.LastHeight,
  75. LastRound: privVal.LastRound,
  76. LastStep: privVal.LastStep,
  77. PrivKey: privKey,
  78. }
  79. bz, err := cdc.MarshalJSON(privValNew)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return bz, nil
  84. }
  85. func convertGenesis(cdc *amino.Codec, jsonBytes []byte) ([]byte, error) {
  86. var genesis Genesis
  87. err := json.Unmarshal(jsonBytes, &genesis)
  88. if err != nil {
  89. return nil, err
  90. }
  91. genesisNew := types.GenesisDoc{
  92. GenesisTime: genesis.GenesisTime,
  93. ChainID: genesis.ChainID,
  94. ConsensusParams: genesis.ConsensusParams,
  95. // Validators
  96. AppHash: genesis.AppHash,
  97. AppState: genesis.AppState,
  98. }
  99. if genesis.AppOptions != nil {
  100. genesisNew.AppState = genesis.AppOptions
  101. }
  102. for _, v := range genesis.Validators {
  103. var pubKey ed25519.PubKeyEd25519
  104. copy(pubKey[:], v.PubKey.Data)
  105. genesisNew.Validators = append(
  106. genesisNew.Validators,
  107. types.GenesisValidator{
  108. PubKey: pubKey,
  109. Power: v.Power,
  110. Name: v.Name,
  111. },
  112. )
  113. }
  114. bz, err := cdc.MarshalJSON(genesisNew)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return bz, nil
  119. }
  120. func main() {
  121. cdc := amino.NewCodec()
  122. cryptoAmino.RegisterAmino(cdc)
  123. args := os.Args[1:]
  124. if len(args) != 1 {
  125. fmt.Println("Please specify a file to convert")
  126. os.Exit(1)
  127. }
  128. filePath := args[0]
  129. fileName := filepath.Base(filePath)
  130. fileBytes, err := ioutil.ReadFile(filePath)
  131. if err != nil {
  132. panic(err)
  133. }
  134. var bz []byte
  135. switch fileName {
  136. case "node_key.json":
  137. bz, err = convertNodeKey(cdc, fileBytes)
  138. case "priv_validator.json":
  139. bz, err = convertPrivVal(cdc, fileBytes)
  140. case "genesis.json":
  141. bz, err = convertGenesis(cdc, fileBytes)
  142. default:
  143. fmt.Println("Expected file name to be in (node_key.json, priv_validator.json, genesis.json)")
  144. os.Exit(1)
  145. }
  146. if err != nil {
  147. panic(err)
  148. }
  149. fmt.Println(string(bz))
  150. }