Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Fixing cmd keys failure (#80)
Browse files Browse the repository at this point in the history
Fix build issue with cmd/keys not being in, added to .gitignore.
  • Loading branch information
xmariachi authored Feb 23, 2024
1 parent 32acd5d commit bbbf9f8
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
.DS_Store
121 changes: 121 additions & 0 deletions cmd/keys/main.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit bbbf9f8

Please sign in to comment.