This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix build issue with cmd/keys not being in, added to .gitignore.
- Loading branch information
Showing
2 changed files
with
122 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |