-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from TheDiscordian/main
Improvements to the Go daemon
- Loading branch information
Showing
5 changed files
with
118 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
identity.key |
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,49 @@ | ||
package main | ||
// Borrowed from https://github.com/libp2p/go-libp2p-relay-daemon/blob/master/identity.go | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/libp2p/go-libp2p/core/crypto" | ||
) | ||
|
||
// LoadIdentity reads a private key from the given path and, if it does not | ||
// exist, generates a new one. | ||
func LoadIdentity(idPath string) (crypto.PrivKey, error) { | ||
if _, err := os.Stat(idPath); err == nil { | ||
return ReadIdentity(idPath) | ||
} else if os.IsNotExist(err) { | ||
fmt.Printf("Generating peer identity in %s\n", idPath) | ||
return GenerateIdentity(idPath) | ||
} else { | ||
return nil, err | ||
} | ||
} | ||
|
||
// ReadIdentity reads a private key from the given path. | ||
func ReadIdentity(path string) (crypto.PrivKey, error) { | ||
bytes, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return crypto.UnmarshalPrivateKey(bytes) | ||
} | ||
|
||
// GenerateIdentity writes a new random private key to the given path. | ||
func GenerateIdentity(path string) (crypto.PrivKey, error) { | ||
privk, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bytes, err := crypto.MarshalPrivateKey(privk) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = os.WriteFile(path, bytes, 0400) | ||
|
||
return privk, err | ||
} |
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
776ba27
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
universal-connectivity – ./
universal-connectivity-libp2p.vercel.app
universal-connectivity.vercel.app
universal-connectivity-git-main-libp2p.vercel.app