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.

83 lines
1.5 KiB

5 years ago
  1. package cmd
  2. import (
  3. "crypto/ecdsa"
  4. "encoding/hex"
  5. "fmt"
  6. "io/ioutil"
  7. "github.com/arnaucube/slowlorisdb/config"
  8. "github.com/arnaucube/slowlorisdb/core"
  9. "github.com/arnaucube/slowlorisdb/db"
  10. "github.com/arnaucube/slowlorisdb/node"
  11. log "github.com/sirupsen/logrus"
  12. "github.com/urfave/cli"
  13. )
  14. var Commands = []cli.Command{
  15. {
  16. Name: "create",
  17. Aliases: []string{},
  18. Usage: "create the node",
  19. Action: cmdCreate,
  20. },
  21. {
  22. Name: "start",
  23. Aliases: []string{},
  24. Usage: "start the node",
  25. Action: cmdStart,
  26. },
  27. }
  28. // creates the node, this needs to be executed for first time
  29. func cmdCreate(c *cli.Context) error {
  30. log.Info("creating new keys of the node")
  31. privK, err := core.NewKey()
  32. if err != nil {
  33. return err
  34. }
  35. fmt.Println(privK)
  36. return nil
  37. }
  38. func cmdStart(c *cli.Context) error {
  39. conf, err := config.MustRead(c)
  40. if err != nil {
  41. return err
  42. }
  43. dir, err := ioutil.TempDir("", conf.DbPath)
  44. if err != nil {
  45. return err
  46. }
  47. db, err := db.New(dir)
  48. if err != nil {
  49. return err
  50. }
  51. // parse AuthNodes from the config file
  52. var authNodes []*ecdsa.PublicKey
  53. for _, authNode := range conf.AuthNodes {
  54. packedPubK, err := hex.DecodeString(authNode)
  55. if err != nil {
  56. return err
  57. }
  58. pubK := core.UnpackPubK(packedPubK)
  59. authNodes = append(authNodes, pubK)
  60. }
  61. bc := core.NewPoABlockchain(db, authNodes)
  62. // TODO parse privK from path in the config file
  63. privK, err := core.NewKey()
  64. node, err := node.NewNode(privK, bc, true)
  65. if err != nil {
  66. return err
  67. }
  68. err = node.Start()
  69. if err != nil {
  70. return err
  71. }
  72. return nil
  73. }