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.

181 lines
4.0 KiB

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