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