Skip to content

Commit

Permalink
Add node type + refactor models + refactor executables (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maelkum authored Feb 21, 2023
1 parent 5af1b5f commit ebfa571
Show file tree
Hide file tree
Showing 44 changed files with 1,941 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
cmd/node/node
cmd/keygen/keygen

dist/
runtime/
*.env
Expand Down
89 changes: 89 additions & 0 deletions cmd/keygen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/spf13/pflag"

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
)

const (
// Names used for created files.
privKeyName = "priv.bin"
pubKeyName = "pub.bin"
identityName = "identity"
)

const (
// Permissions used for created files.
privKeyPermissions = 0600
pubKeyPermissions = 0644
)

func main() {

var (
flagOutputDir string
)

pflag.StringVarP(&flagOutputDir, "output", "o", ".", "directory where keys should be stored")

pflag.Parse()

// Create output directory, if it doesn't exist.
err := os.MkdirAll(flagOutputDir, os.ModePerm)
if err != nil {
log.Fatalf("could not create output directory: %s", err)
}

// Generate key pair.
priv, pub, err := crypto.GenerateKeyPair(crypto.Ed25519, 0)
if err != nil {
log.Fatalf("could not generate key pair: %s", err)
}

// Encode keys and extract peer ID from key.
privPayload, err := crypto.MarshalPrivateKey(priv)
if err != nil {
log.Fatalf("could not marshal private key: %s", err)
}

pubPayload, err := crypto.MarshalPublicKey(pub)
if err != nil {
log.Fatalf("could not marshal public key: %s", err)
}

identity, err := peer.IDFromPublicKey(pub)
if err != nil {
log.Fatalf("failed to get peer identity from public key: %s", err)
}

// Write keys and identity to files.

pubKeyFile := filepath.Join(flagOutputDir, pubKeyName)
err = os.WriteFile(pubKeyFile, pubPayload, pubKeyPermissions)
if err != nil {
log.Fatalf("could not write private key to file: %s", err)
}

idFile := filepath.Join(flagOutputDir, identityName)
err = os.WriteFile(idFile, []byte(identity), pubKeyPermissions)
if err != nil {
log.Fatalf("could not write private key to file: %s", err)
}

privKeyFile := filepath.Join(flagOutputDir, privKeyName)
err = os.WriteFile(privKeyFile, privPayload, privKeyPermissions)
if err != nil {
log.Fatalf("could not write private key to file: %s", err)
}

fmt.Printf("generated private key: %s\n", privKeyFile)
fmt.Printf("generated public key: %s\n", pubKeyFile)
fmt.Printf("generated identity file: %s\n", idFile)
}
44 changes: 44 additions & 0 deletions cmd/node/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"fmt"

"github.com/multiformats/go-multiaddr"

"github.com/blocklessnetworking/b7s/models/blockless"
)

// getPeerAddresses returns the list of the multiaddreses for the peer list.
func getPeerAddresses(peers []blockless.Peer) ([]multiaddr.Multiaddr, error) {

var addrs []multiaddr.Multiaddr

for _, peer := range peers {

addr, err := multiaddr.NewMultiaddr(peer.MultiAddr)
if err != nil {
return nil, fmt.Errorf("could not parse multiaddress (addr: %s): %w", peer.MultiAddr, err)
}

addrs = append(addrs, addr)
}

return addrs, nil
}

// parse list of strings with multiaddresses
func getBootNodeAddresses(addrs []string) ([]multiaddr.Multiaddr, error) {

var out []multiaddr.Multiaddr
for _, addr := range addrs {

addr, err := multiaddr.NewMultiaddr(addr)
if err != nil {
return nil, fmt.Errorf("could not parse multiaddress (addr: %s): %w", addr, err)
}

out = append(out, addr)
}

return out, nil
}
39 changes: 39 additions & 0 deletions cmd/node/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"github.com/spf13/pflag"

"github.com/blocklessnetworking/b7s/config"
)

// Default values.
const (
defaultPort = 0
defaultAddress = "0.0.0.0"
defaultDB = "db"

defaultRole = "worker"
)

func parseFlags() *config.Config {

var cfg config.Config

pflag.StringVarP(&cfg.Log.Level, "log-level", "l", "info", "log level to use")
pflag.StringVarP(&cfg.DatabasePath, "db", "d", defaultDB, "path to the database used for persisting node data")

// Node configuration.
pflag.StringVarP(&cfg.Role, "role", "r", defaultRole, "role this note will have in the Blockless protocol (head or worker)")
pflag.StringVarP(&cfg.Host.Address, "address", "a", defaultAddress, "address that the libp2p host will use")
pflag.UintVarP(&cfg.Host.Port, "port", "p", defaultPort, "port that the libp2p host will use")
pflag.StringVar(&cfg.Host.PrivateKey, "private-key", "", "private key that the libp2p host will use")
pflag.StringVar(&cfg.API, "rest-api", "", "address where the head node REST API will listen on")
pflag.StringSliceVar(&cfg.BootNodes, "boot-nodes", nil, "list of addresses that this node will connect to on startup, in multiaddr format")

pflag.StringVar(&cfg.Workspace, "workspace", "./workspace", "directory that the node can use for file storage")
pflag.StringVar(&cfg.Runtime, "runtime", "", "runtime address (used by the worker node)")

pflag.Parse()

return &cfg
}
Loading

0 comments on commit ebfa571

Please sign in to comment.