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
32func cmdCreate(c *cli.Context) error {
33	log.Info("creating new keys of the node")
34	privK, err := core.NewKey()
35	if err != nil {
36		return err
37	}
38	fmt.Println(privK)
39	return nil
40}
41
42func cmdStart(c *cli.Context) error {
43	conf, err := config.MustRead(c)
44	if err != nil {
45		return err
46	}
47
48	dir, err := ioutil.TempDir("", conf.DbPath)
49	if err != nil {
50		return err
51	}
52	db, err := db.New(dir)
53	if err != nil {
54		return err
55	}
56
57	// parse AuthNodes from the config file
58	var authNodes []*ecdsa.PublicKey
59	for _, authNode := range conf.AuthNodes {
60		packedPubK, err := hex.DecodeString(authNode)
61		if err != nil {
62			return err
63		}
64		pubK := core.UnpackPubK(packedPubK)
65		authNodes = append(authNodes, pubK)
66	}
67
68	bc := core.NewPoABlockchain(db, authNodes)
69
70	// TODO parse privK from path in the config file
71	privK, err := core.NewKey()
72	node, err := node.NewNode(privK, bc, true)
73	if err != nil {
74		return err
75	}
76	err = node.Start()
77	if err != nil {
78		return err
79	}
80
81	return nil
82}