|
|
@ -2,9 +2,12 @@ package cmd |
|
|
|
|
|
|
|
import ( |
|
|
|
"crypto/ecdsa" |
|
|
|
"crypto/x509" |
|
|
|
"encoding/hex" |
|
|
|
"encoding/pem" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"os" |
|
|
|
|
|
|
|
"github.com/arnaucube/slowlorisdb/config" |
|
|
|
"github.com/arnaucube/slowlorisdb/core" |
|
|
@ -29,14 +32,52 @@ var Commands = []cli.Command{ |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
func writePrivKToFile(privK *ecdsa.PrivateKey, path string) error { |
|
|
|
x509Encoded, err := x509.MarshalECPrivateKey(privK) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
pemEncoded := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509Encoded}) |
|
|
|
// write privK to file
|
|
|
|
err = ioutil.WriteFile(path, pemEncoded, 0777) |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
func readPrivKFromFile(path string) (*ecdsa.PrivateKey, error) { |
|
|
|
pemEncoded, err := ioutil.ReadFile(path) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
block, _ := pem.Decode([]byte(pemEncoded)) |
|
|
|
x509Encoded := block.Bytes |
|
|
|
privK, err := x509.ParseECPrivateKey(x509Encoded) |
|
|
|
return privK, err |
|
|
|
} |
|
|
|
|
|
|
|
// creates the node, this needs to be executed for first time
|
|
|
|
func cmdCreate(c *cli.Context) error { |
|
|
|
conf, err := config.MustRead(c) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
log.Info("creating new keys of the node") |
|
|
|
privK, err := core.NewKey() |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
fmt.Println(privK) |
|
|
|
err = os.MkdirAll(conf.StoragePath, 0777) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
err = writePrivKToFile(privK, conf.StoragePath+"/privK.pem") |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
fmt.Println("pubK", hex.EncodeToString(core.PackPubK(&privK.PublicKey))) |
|
|
|
fmt.Println("addr", core.AddressFromPubK(&privK.PublicKey).String()) |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
@ -46,11 +87,7 @@ func cmdStart(c *cli.Context) error { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", conf.DbPath) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
db, err := db.New(dir) |
|
|
|
db, err := db.New(conf.StoragePath + "/db") |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
@ -68,8 +105,14 @@ func cmdStart(c *cli.Context) error { |
|
|
|
|
|
|
|
bc := core.NewPoABlockchain(db, authNodes) |
|
|
|
|
|
|
|
// TODO parse privK from path in the config file
|
|
|
|
privK, err := core.NewKey() |
|
|
|
// parse privK from path in the config file
|
|
|
|
privK, err := readPrivKFromFile(conf.StoragePath + "/privK.pem") |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
fmt.Println("pubK", hex.EncodeToString(core.PackPubK(&privK.PublicKey))) |
|
|
|
fmt.Println("addr", core.AddressFromPubK(&privK.PublicKey).String()) |
|
|
|
|
|
|
|
node, err := node.NewNode(privK, bc, true) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|