Browse Source

add privk parser, add node start read config multiple nodes

master
arnaucube 5 years ago
parent
commit
3d1ac395a5
7 changed files with 99 additions and 16 deletions
  1. +1
    -0
      .gitignore
  2. +23
    -1
      README.md
  3. +51
    -8
      cmd/cmd.go
  4. +4
    -4
      config/config.go
  5. +6
    -0
      config0.yaml
  6. +6
    -0
      config1.yaml
  7. +8
    -3
      core/keys.go

+ 1
- 0
.gitignore

@ -0,0 +1 @@
tmp

+ 23
- 1
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 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 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") ![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
```

+ 51
- 8
cmd/cmd.go

@ -2,9 +2,12 @@ package cmd
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/x509"
"encoding/hex" "encoding/hex"
"encoding/pem"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"github.com/arnaucube/slowlorisdb/config" "github.com/arnaucube/slowlorisdb/config"
"github.com/arnaucube/slowlorisdb/core" "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 // creates the node, this needs to be executed for first time
func cmdCreate(c *cli.Context) error { func cmdCreate(c *cli.Context) error {
conf, err := config.MustRead(c)
if err != nil {
return err
}
log.Info("creating new keys of the node") log.Info("creating new keys of the node")
privK, err := core.NewKey() privK, err := core.NewKey()
if err != nil { if err != nil {
return err 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 return nil
} }
@ -46,11 +87,7 @@ func cmdStart(c *cli.Context) error {
return err 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 { if err != nil {
return err return err
} }
@ -68,8 +105,14 @@ func cmdStart(c *cli.Context) error {
bc := core.NewPoABlockchain(db, authNodes) 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) node, err := node.NewNode(privK, bc, true)
if err != nil { if err != nil {
return err return err

+ 4
- 4
config/config.go

@ -8,10 +8,10 @@ import (
) )
type Config struct { 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) { func MustRead(c *cli.Context) (*Config, error) {

+ 6
- 0
config0.yaml

@ -0,0 +1,6 @@
storagepath: "tmp/node0"
port: 3000
dest: 3001
authnodes:
"16be582c80c9b9be29b60f2bcd032654c21256b6bf74535743842bef33432132"
"96758cfdc821abad27c6301439af9f6d0873e237affc60fbc4cc5b33b41b8726"

+ 6
- 0
config1.yaml

@ -0,0 +1,6 @@
storagepath: "tmp/node1"
port: 4000
dest: 4001
authnodes:
"16be582c80c9b9be29b60f2bcd032654c21256b6bf74535743842bef33432132"
"96758cfdc821abad27c6301439af9f6d0873e237affc60fbc4cc5b33b41b8726"

+ 8
- 3
core/keys.go

@ -19,7 +19,7 @@ func (addr Address) String() string {
func NewKey() (*ecdsa.PrivateKey, error) { func NewKey() (*ecdsa.PrivateKey, error) {
curve := elliptic.P256() curve := elliptic.P256()
privatekey := new(ecdsa.PrivateKey)
// privatekey := new(ecdsa.PrivateKey)
privatekey, err := ecdsa.GenerateKey(curve, rand.Reader) privatekey, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil { if err != nil {
return nil, err return nil, err
@ -45,6 +45,11 @@ func AddressFromPrivK(privK *ecdsa.PrivateKey) Address {
return Address(h) return Address(h)
} }
func AddressFromPubK(pubK *ecdsa.PublicKey) Address {
h := HashBytes(PackPubK(pubK))
return Address(h)
}
func (sig *Signature) Bytes() []byte { func (sig *Signature) Bytes() []byte {
b := sig.R.Bytes() b := sig.R.Bytes()
b = append(b, sig.S.Bytes()...) b = append(b, sig.S.Bytes()...)
@ -74,8 +79,8 @@ type Signature struct {
} }
func Sign(privK *ecdsa.PrivateKey, m []byte) (*Signature, error) { 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) hashMsg := HashBytes(m)

Loading…
Cancel
Save