add privk parser, add node start read config multiple nodes
arnaucube arnaucube@gmail.com
Sat, 04 May 2019 18:02:15 +0200
7 files changed,
99 insertions(+),
16 deletions(-)
M
README.md
→
README.md
@@ -1,4 +1,4 @@
-# slowlorisdb +# slowlorisdb [](https://goreportcard.com/report/github.com/arnaucube/slowlorisdb) [](https://godoc.org/github.com/arnaucube/slowlorisdb) Slow, decentralized and cryptographically consistent database@@ -7,3 +7,25 @@
Watch the blockchain in action: http://www.youtubemultiplier.com/5ca9c1a540b31-slowlorisdb-visual-representation.php  + + +## Run +The repo is under construction + +- create node +```sh +# node0 +go run main.go --config config0.yaml create + +# node1 +go run main.go --config config1.yaml create +``` + +- run node +```sh +# node0 +go run main.go --config config0.yaml start + +# node1 +go run main.go --config config1.yaml start +```
M
cmd/cmd.go
→
cmd/cmd.go
@@ -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 @@ Action: cmdStart,
}, } +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 @@ if err != nil {
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 @@ }
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
M
config/config.go
→
config/config.go
@@ -8,10 +8,10 @@ "github.com/urfave/cli"
) type Config struct { - DbPath string - Port string - Dest string - AuthNodes []string // PubKs in hex format of the AuthNodes for the blockchain + StoragePath string + Port string + Dest string + AuthNodes []string // PubKs in hex format of the AuthNodes for the blockchain } func MustRead(c *cli.Context) (*Config, error) {
A
config0.yaml
@@ -0,0 +1,6 @@
+storagepath: "tmp/node0" +port: 3000 +dest: 3001 +authnodes: + "16be582c80c9b9be29b60f2bcd032654c21256b6bf74535743842bef33432132" + "96758cfdc821abad27c6301439af9f6d0873e237affc60fbc4cc5b33b41b8726"
A
config1.yaml
@@ -0,0 +1,6 @@
+storagepath: "tmp/node1" +port: 4000 +dest: 4001 +authnodes: + "16be582c80c9b9be29b60f2bcd032654c21256b6bf74535743842bef33432132" + "96758cfdc821abad27c6301439af9f6d0873e237affc60fbc4cc5b33b41b8726"
M
core/keys.go
→
core/keys.go
@@ -19,7 +19,7 @@
func NewKey() (*ecdsa.PrivateKey, error) { curve := elliptic.P256() - privatekey := new(ecdsa.PrivateKey) + // privatekey := new(ecdsa.PrivateKey) privatekey, err := ecdsa.GenerateKey(curve, rand.Reader) if err != nil { return nil, err@@ -42,6 +42,11 @@ }
func AddressFromPrivK(privK *ecdsa.PrivateKey) Address { h := HashBytes(PackPubK(&privK.PublicKey)) + return Address(h) +} + +func AddressFromPubK(pubK *ecdsa.PublicKey) Address { + h := HashBytes(PackPubK(pubK)) return Address(h) }@@ -74,8 +79,8 @@ S *big.Int
} func Sign(privK *ecdsa.PrivateKey, m []byte) (*Signature, error) { - r := big.NewInt(0) - s := big.NewInt(0) + // r := big.NewInt(0) + // s := big.NewInt(0) hashMsg := HashBytes(m)