mirror of
https://github.com/arnaucube/slowlorisdb.git
synced 2026-02-28 05:46:48 +01:00
add privk parser, add node start read config multiple nodes
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
tmp
|
||||
24
README.md
24
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 @@ 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
|
||||
|
||||

|
||||
|
||||
|
||||
## 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
|
||||
```
|
||||
|
||||
59
cmd/cmd.go
59
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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
6
config0.yaml
Normal file
6
config0.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
storagepath: "tmp/node0"
|
||||
port: 3000
|
||||
dest: 3001
|
||||
authnodes:
|
||||
"16be582c80c9b9be29b60f2bcd032654c21256b6bf74535743842bef33432132"
|
||||
"96758cfdc821abad27c6301439af9f6d0873e237affc60fbc4cc5b33b41b8726"
|
||||
6
config1.yaml
Normal file
6
config1.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
storagepath: "tmp/node1"
|
||||
port: 4000
|
||||
dest: 4001
|
||||
authnodes:
|
||||
"16be582c80c9b9be29b60f2bcd032654c21256b6bf74535743842bef33432132"
|
||||
"96758cfdc821abad27c6301439af9f6d0873e237affc60fbc4cc5b33b41b8726"
|
||||
11
core/keys.go
11
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user