diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9a5aec --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tmp diff --git a/README.md b/README.md index cd84ff5..de5f144 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# slowlorisdb +# slowlorisdb [![Go Report Card](https://goreportcard.com/badge/github.com/arnaucube/slowlorisdb)](https://goreportcard.com/report/github.com/arnaucube/slowlorisdb) [![GoDoc](https://godoc.org/github.com/arnaucube/slowlorisdb?status.svg)](https://godoc.org/github.com/arnaucube/slowlorisdb) Slow, decentralized and cryptographically consistent database @@ -7,3 +7,25 @@ Basically this repo is a blockchain written from scratch, that allows to launch Watch the blockchain in action: http://www.youtubemultiplier.com/5ca9c1a540b31-slowlorisdb-visual-representation.php ![slowloris](https://04019a5a-a-62cb3a1a-s-sites.googlegroups.com/site/jchristensensdigitalportfolio/slow-loris/IO-moth-eating-frozen-apple-sauce.jpg "slowloris") + + +## 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 +``` diff --git a/cmd/cmd.go b/cmd/cmd.go index b46f201..f854cda 100644 --- a/cmd/cmd.go +++ b/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 @@ 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 diff --git a/config/config.go b/config/config.go index 8d05f57..c515581 100644 --- a/config/config.go +++ b/config/config.go @@ -8,10 +8,10 @@ import ( ) 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) { diff --git a/config0.yaml b/config0.yaml new file mode 100644 index 0000000..c7e8c50 --- /dev/null +++ b/config0.yaml @@ -0,0 +1,6 @@ +storagepath: "tmp/node0" +port: 3000 +dest: 3001 +authnodes: + "16be582c80c9b9be29b60f2bcd032654c21256b6bf74535743842bef33432132" + "96758cfdc821abad27c6301439af9f6d0873e237affc60fbc4cc5b33b41b8726" diff --git a/config1.yaml b/config1.yaml new file mode 100644 index 0000000..9a01de8 --- /dev/null +++ b/config1.yaml @@ -0,0 +1,6 @@ +storagepath: "tmp/node1" +port: 4000 +dest: 4001 +authnodes: + "16be582c80c9b9be29b60f2bcd032654c21256b6bf74535743842bef33432132" + "96758cfdc821abad27c6301439af9f6d0873e237affc60fbc4cc5b33b41b8726" diff --git a/core/keys.go b/core/keys.go index 90709e5..f4386fc 100644 --- a/core/keys.go +++ b/core/keys.go @@ -19,7 +19,7 @@ func (addr Address) String() string { 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 @@ -45,6 +45,11 @@ func AddressFromPrivK(privK *ecdsa.PrivateKey) Address { return Address(h) } +func AddressFromPubK(pubK *ecdsa.PublicKey) Address { + h := HashBytes(PackPubK(pubK)) + return Address(h) +} + func (sig *Signature) Bytes() []byte { b := sig.R.Bytes() b = append(b, sig.S.Bytes()...) @@ -74,8 +79,8 @@ type Signature struct { } 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)