arnaucube
arnaucube/slowlorisdbPublic Self-hosted repository
slowlorisdb/cmd/cmd.go
cmd/cmd.goRaw
  1package cmd
  2
  3import (
  4	"crypto/ecdsa"
  5	"crypto/x509"
  6	"encoding/hex"
  7	"encoding/pem"
  8	"fmt"
  9	"io/ioutil"
 10	"os"
 11
 12	"github.com/arnaucube/slowlorisdb/config"
 13	"github.com/arnaucube/slowlorisdb/core"
 14	"github.com/arnaucube/slowlorisdb/db"
 15	"github.com/arnaucube/slowlorisdb/node"
 16	"github.com/arnaucube/slowlorisdb/peer"
 17	log "github.com/sirupsen/logrus"
 18	"github.com/urfave/cli"
 19)
 20
 21var Commands = []cli.Command{
 22	{
 23		Name:    "create",
 24		Aliases: []string{},
 25		Usage:   "create the node",
 26		Action:  cmdCreate,
 27	},
 28	{
 29		Name:    "start",
 30		Aliases: []string{},
 31		Usage:   "start the node",
 32		Action:  cmdStart,
 33	},
 34}
 35
 36func writePrivKToFile(privK *ecdsa.PrivateKey, path string) error {
 37	x509Encoded, err := x509.MarshalECPrivateKey(privK)
 38	if err != nil {
 39		return err
 40	}
 41	pemEncoded := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509Encoded})
 42	// write privK to file
 43	err = ioutil.WriteFile(path, pemEncoded, 0777)
 44	return err
 45}
 46
 47func readPrivKFromFile(path string) (*ecdsa.PrivateKey, error) {
 48	pemEncoded, err := ioutil.ReadFile(path)
 49	if err != nil {
 50		return nil, err
 51	}
 52	block, _ := pem.Decode([]byte(pemEncoded))
 53	x509Encoded := block.Bytes
 54	privK, err := x509.ParseECPrivateKey(x509Encoded)
 55	return privK, err
 56}
 57
 58// creates the node, this needs to be executed for first time
 59func cmdCreate(c *cli.Context) error {
 60	conf, err := config.MustRead(c)
 61	if err != nil {
 62		return err
 63	}
 64
 65	log.Info("creating new keys of the node")
 66	privK, err := core.NewKey()
 67	if err != nil {
 68		return err
 69	}
 70	err = os.MkdirAll(conf.StoragePath, 0777)
 71	if err != nil {
 72		return err
 73	}
 74	err = writePrivKToFile(privK, conf.StoragePath+"/privK.pem")
 75	if err != nil {
 76		return err
 77	}
 78
 79	fmt.Println("pubK", hex.EncodeToString(core.PackPubK(&privK.PublicKey)))
 80	fmt.Println("addr", core.AddressFromPubK(&privK.PublicKey).String())
 81
 82	return nil
 83}
 84
 85func cmdStart(c *cli.Context) error {
 86	conf, err := config.MustRead(c)
 87	if err != nil {
 88		return err
 89	}
 90
 91	db, err := db.New(conf.StoragePath + "/db")
 92	if err != nil {
 93		return err
 94	}
 95
 96	// parse AuthNodes from the config file
 97	var authNodes []*ecdsa.PublicKey
 98	for _, authNode := range conf.AuthNodes {
 99		packedPubK, err := hex.DecodeString(authNode)
100		if err != nil {
101			return err
102		}
103		pubK := core.UnpackPubK(packedPubK)
104		authNodes = append(authNodes, pubK)
105	}
106
107	bc := core.NewPoABlockchain(db, authNodes)
108
109	// parse privK from path in the config file
110	privK, err := readPrivKFromFile(conf.StoragePath + "/privK.pem")
111	if err != nil {
112		return err
113	}
114	fmt.Println("pubK", hex.EncodeToString(core.PackPubK(&privK.PublicKey)))
115	fmt.Println("addr", core.AddressFromPubK(&privK.PublicKey).String())
116
117	node, err := node.NewNode(privK, bc, true)
118	if err != nil {
119		return err
120	}
121
122	p := peer.NewPeer(node, conf)
123	p.Start()
124
125	return nil
126}