From bbbf9f821d838f2d80fb996467bbc1ccfef8efbb Mon Sep 17 00:00:00 2001 From: xmariachi Date: Fri, 23 Feb 2024 17:07:08 +0100 Subject: [PATCH] Fixing cmd keys failure (#80) Fix build issue with cmd/keys not being in, added to .gitignore. --- .gitignore | 4 +- cmd/keys/main.go | 121 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 cmd/keys/main.go diff --git a/.gitignore b/.gitignore index 4d7f42c..f9fac83 100644 --- a/.gitignore +++ b/.gitignore @@ -9,10 +9,8 @@ worker-fdb workspace runtime.tar.gz cmd/node/node -cmd/keys/keys data -keys data-head data-worker main.py -.DS_Store \ No newline at end of file +.DS_Store diff --git a/cmd/keys/main.go b/cmd/keys/main.go new file mode 100644 index 0000000..ee0cb61 --- /dev/null +++ b/cmd/keys/main.go @@ -0,0 +1,121 @@ +package main + +import ( + "encoding/base64" + "log" + "os" + "path/filepath" + + "github.com/libp2p/go-libp2p/core/crypto" + "github.com/libp2p/go-libp2p/core/peer" + "github.com/spf13/pflag" +) + +const ( + privKeyName = "priv.bin" + pubKeyName = "pub.bin" + privKeyTxtName = "priv.txt" + pubKeyTxtName = "pubkey.txt" + identityName = "identity" + privKeyPermissions = 0600 + pubKeyPermissions = 0644 +) + +func main() { + var ( + flagOutputDir string + ) + + pflag.StringVarP(&flagOutputDir, "output", "o", ".", "directory where keys should be stored") + + pflag.Parse() + + // Initialize output directory + err := os.MkdirAll(flagOutputDir, os.ModePerm) + if err != nil { + log.Fatalf("Could not create output directory: %s", err) + } + + privKeyFile := filepath.Join(flagOutputDir, privKeyName) + + _, _, err = LoadOrCreateKeys(privKeyFile, flagOutputDir) + if err != nil { + log.Fatalf("Error loading or creating keys: %s", err) + } +} + +// LoadOrCreateKeys loads existing keys or creates new ones if not present +func LoadOrCreateKeys(privKeyFile string, outputDir string) (crypto.PrivKey, crypto.PubKey, error) { + var priv crypto.PrivKey + var pub crypto.PubKey + var err error + + if _, err := os.Stat(privKeyFile); os.IsNotExist(err) { + priv, pub, err = crypto.GenerateKeyPair(crypto.Ed25519, 0) + if err != nil { + return nil, nil, err + } + } else { + privBytes, err := os.ReadFile(privKeyFile) + if err != nil { + return nil, nil, err + } + + priv, err = crypto.UnmarshalPrivateKey(privBytes) + if err != nil { + return nil, nil, err + } + pub = priv.GetPublic() + } + + 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("Could not generate identity: %s", err) + } + + pubKeyFile := filepath.Join(outputDir, pubKeyName) + err = os.WriteFile(pubKeyFile, pubPayload, pubKeyPermissions) + if err != nil { + log.Fatalf("Could not write public key to file: %s", err) + } + + pubKeyTextFile := filepath.Join(outputDir, pubKeyTxtName) + pubKeyBase64 := base64.StdEncoding.EncodeToString(pubPayload) + err = os.WriteFile(pubKeyTextFile, []byte(pubKeyBase64), pubKeyPermissions) + if err != nil { + log.Fatalf("Could not write public key text to file: %s", err) + } + + // Write peer ID to file + identityFile := filepath.Join(outputDir, identityName) + err = os.WriteFile(identityFile, []byte(identity.String()), pubKeyPermissions) + if err != nil { + log.Fatalf("Could not write identity to file: %s", err) + } + + // Private key binary + privKeyFile = filepath.Join(outputDir, privKeyName) + err = os.WriteFile(privKeyFile, privPayload, privKeyPermissions) + if err != nil { + log.Fatalf("Could not write private key to file: %s", err) + } + // Private ke txt + privKeyTextFile := filepath.Join(outputDir, privKeyTxtName) + privKeyBase64 := base64.StdEncoding.EncodeToString(privPayload) + err = os.WriteFile(privKeyTextFile, []byte(privKeyBase64), privKeyPermissions) + if err != nil { + log.Fatalf("Could not write private key text to file: %s", err) + } + + return priv, pub, nil +}