Updated main.go

Fixed:
libp2p/go-libp2p-core@v0.20.1/peer/peer.go:69:14: undefined: peer.Encode
This commit is contained in:
Diyath Sahan Rajapakshe 2023-11-13 17:28:21 +05:30 committed by GitHub
parent 6a8cc33630
commit 6b2debd120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

51
main.go
View File

@ -1,20 +1,21 @@
package main
import (
// Update the import path for crypto
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
crp "github.com/libp2p/go-libp2p-core/crypto"
peer "github.com/libp2p/go-libp2p-core/peer"
crp "github.com/libp2p/go-libp2p/core/crypto"
peer "github.com/libp2p/go-libp2p/core/peer"
)
func main() {
size := flag.Int("bitsize", 2048, "select the bitsize of the key to generate")
typ := flag.String("type", "", "select type of key to generate (RSA, Ed25519, Secp256k1 or ECDSA)")
key := flag.String("key", "", "specify the location of the key to decode it's peerID")
typ := flag.String("type", "", "select the type of key to generate (RSA, Ed25519, Secp256k1, or ECDSA)")
key := flag.String("key", "", "specify the location of the key to decode its peer ID")
flag.Parse()
@ -42,69 +43,67 @@ func readKey(keyLoc *string, typ *string) error {
fmt.Fprintf(os.Stderr, "Reading key at: %s\n", *keyLoc)
var unmarshalPrivateKeyFucn func(data []byte) (crp.PrivKey, error)
// rsa and ed25519 unmarshalPrivateKeyFucn are for backward compatibility
// for keys saved with raw(), to read such keys, specify the key type
var unmarshalPrivateKeyFunc func(data []byte) (crp.PrivKey, error)
switch strings.ToLower(*typ) {
case "rsa":
unmarshalPrivateKeyFucn = crp.UnmarshalRsaPrivateKey
unmarshalPrivateKeyFunc = crp.UnmarshalRsaPrivateKey
case "ed25519":
unmarshalPrivateKeyFucn = crp.UnmarshalEd25519PrivateKey
unmarshalPrivateKeyFunc = crp.UnmarshalEd25519PrivateKey
default:
unmarshalPrivateKeyFucn = crp.UnmarshalPrivateKey
unmarshalPrivateKeyFunc = crp.UnmarshalPrivateKey
}
prvk, err := unmarshalPrivateKeyFucn(data)
prvKey, err := unmarshalPrivateKeyFunc(data)
if err != nil {
return err
}
id, err := peer.IDFromPrivateKey(prvk)
id, err := peer.IDFromPrivateKey(prvKey)
if err != nil {
return err
}
_, err = fmt.Fprintf(os.Stderr, "Success!\nID for %s key: %s\n", prvk.Type().String(), id.Pretty())
return err
fmt.Fprintf(os.Stderr, "Success!\nID for %s key: %s\n", prvKey.Type().String(), id)
return nil
}
func genKey(typ *string, size *int) error {
var atyp int
var keyType int
switch strings.ToLower(*typ) {
case "rsa":
atyp = crp.RSA
keyType = crp.RSA
case "ed25519":
atyp = crp.Ed25519
keyType = crp.Ed25519
case "secp256k1":
atyp = crp.Secp256k1
keyType = crp.Secp256k1
case "ecdsa":
atyp = crp.ECDSA
keyType = crp.ECDSA
default:
return fmt.Errorf("unrecognized key type: %s", *typ)
}
fmt.Fprintf(os.Stderr, "Generating a %d bit %s key...\n", *size, *typ)
fmt.Fprintf(os.Stderr, "Generating a %d-bit %s key...\n", *size, *typ)
priv, pub, err := crp.GenerateKeyPair(atyp, *size)
prvKey, pubKey, err := crp.GenerateKeyPair(keyType, *size)
if err != nil {
return err
}
pid, err := peer.IDFromPublicKey(pub)
id, err := peer.IDFromPublicKey(pubKey)
if err != nil {
return err
}
data, err := crp.MarshalPrivateKey(priv)
data, err := crp.MarshalPrivateKey(prvKey)
if err != nil {
return err
}
_, err = os.Stdout.Write(data)
if err != nil {
return nil
return err
}
_, err = fmt.Fprintf(os.Stderr, "Success!\nID for generated key: %s\n", pid.Pretty())
return err
fmt.Fprintf(os.Stderr, "Success!\nID for the generated key: %s\n", id)
return nil
}