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