-
Notifications
You must be signed in to change notification settings - Fork 36
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 libp2p/feat/identity
Persistent Daemon Identity
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
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,25 @@ | ||
package p2pd | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
crypto "github.com/libp2p/go-libp2p-crypto" | ||
) | ||
|
||
func ReadIdentity(path string) (crypto.PrivKey, error) { | ||
bytes, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return crypto.UnmarshalPrivateKey(bytes) | ||
} | ||
|
||
func WriteIdentity(k crypto.PrivKey, path string) error { | ||
bytes, err := crypto.MarshalPrivateKey(k) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ioutil.WriteFile(path, bytes, 0400) | ||
} |
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,48 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
|
||
p2pd "github.com/libp2p/go-libp2p-daemon" | ||
|
||
crypto "github.com/libp2p/go-libp2p-crypto" | ||
peer "github.com/libp2p/go-libp2p-peer" | ||
) | ||
|
||
func main() { | ||
file := flag.String("f", "identity", "output key file") | ||
ktype := flag.String("t", "rsa", "key type; rsa or ed25519") | ||
bits := flag.Int("b", 2048, "key size in bits (for rsa)") | ||
flag.Parse() | ||
|
||
var typ int | ||
|
||
switch *ktype { | ||
case "rsa": | ||
typ = crypto.RSA | ||
|
||
case "ed25519": | ||
typ = crypto.Ed25519 | ||
|
||
default: | ||
log.Fatalf("Unknown key type %s; must be rsa or ed25519", *ktype) | ||
} | ||
|
||
priv, pub, err := crypto.GenerateKeyPair(typ, *bits) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
id, err := peer.IDFromPublicKey(pub) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("Peer ID: %s\n", id.Pretty()) | ||
|
||
err = p2pd.WriteIdentity(priv, *file) | ||
if err != nil { | ||
log.Fatal(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