splitting the modules
This commit is contained in:
parent
a53a500fd7
commit
8705f87504
12 changed files with 402 additions and 396 deletions
97
yggdrasil/keys.go
Normal file
97
yggdrasil/keys.go
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
package yggdrasil
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gologme/log"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
yggConfig "github.com/yggdrasil-network/yggdrasil-go/src/config"
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
||||
)
|
||||
|
||||
func GeneratePrivateKey() yggConfig.KeyBytes {
|
||||
return yggConfig.GenerateConfig().PrivateKey
|
||||
}
|
||||
|
||||
func GetPublicKey(keyPath string) (ed25519.PublicKey, error) {
|
||||
data, err := os.ReadFile(keyPath)
|
||||
if err != nil {
|
||||
return ed25519.PublicKey{}, err
|
||||
}
|
||||
|
||||
decoded, err := hex.DecodeString(strings.TrimSpace(string(data)))
|
||||
if err != nil {
|
||||
return ed25519.PublicKey{}, err
|
||||
}
|
||||
|
||||
if len(decoded) != ed25519.PrivateKeySize {
|
||||
return ed25519.PublicKey{}, fmt.Errorf("invalid private key size: %d", len(decoded))
|
||||
}
|
||||
|
||||
privateKey := ed25519.PrivateKey(decoded)
|
||||
return privateKey.Public().(ed25519.PublicKey), nil
|
||||
}
|
||||
|
||||
func GetYggdrasilAddress(config *viper.Viper) string {
|
||||
//var remoteTcp types.TCPRemoteMappings
|
||||
ygg := config.Sub("yggdrasil")
|
||||
if ygg == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
//laddr := config.Sub("p2p").GetString("laddr")
|
||||
//remoteTcp.Set(laddr)
|
||||
|
||||
cfg := yggConfig.GenerateConfig()
|
||||
|
||||
cfg.AdminListen = ygg.GetString("admin_listen")
|
||||
cfg.Listen = ygg.GetStringSlice("listen")
|
||||
cfg.Peers = ygg.GetStringSlice("peers")
|
||||
cfg.AllowedPublicKeys = ygg.GetStringSlice("allowed-public-keys")
|
||||
cfg.PrivateKeyPath = ygg.GetString("private-key-file")
|
||||
|
||||
logger := log.Default()
|
||||
|
||||
n := &node{}
|
||||
|
||||
// Setup the Yggdrasil node itself.
|
||||
{
|
||||
options := []core.SetupOption{
|
||||
core.NodeInfo(cfg.NodeInfo),
|
||||
core.NodeInfoPrivacy(cfg.NodeInfoPrivacy),
|
||||
}
|
||||
for _, addr := range cfg.Listen {
|
||||
options = append(options, core.ListenAddress(addr))
|
||||
}
|
||||
for _, peer := range cfg.Peers {
|
||||
options = append(options, core.Peer{URI: peer})
|
||||
}
|
||||
for intf, peers := range cfg.InterfacePeers {
|
||||
for _, peer := range peers {
|
||||
options = append(options, core.Peer{URI: peer, SourceInterface: intf})
|
||||
}
|
||||
}
|
||||
for _, allowed := range cfg.AllowedPublicKeys {
|
||||
k, err := hex.DecodeString(allowed)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
options = append(options, core.AllowedPublicKey(k[:]))
|
||||
}
|
||||
|
||||
var err error
|
||||
if n.core, err = core.New(cfg.Certificate, logger, options...); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
address := n.core.Address()
|
||||
n.core.Stop()
|
||||
return address.String()
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue