github.com/ipni/storetheindex@v0.8.30/assigner/scripts/peer_id_from_priv_key.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/libp2p/go-libp2p/core/crypto"
     9  	"github.com/libp2p/go-libp2p/core/peer"
    10  )
    11  
    12  // Reads marshalled crypto.PrivKey from os.stdin and prints the peer.ID that corresponds to the key.
    13  // See: crypto.UnmarshalPrivateKey, peer.IDFromPrivateKey.
    14  //
    15  // For example, you can directly get the peer.ID from a sops-encrypted identity file as long as
    16  // your terminal session is authenticated to the storetheindex AWS account and has access
    17  // to the relevant KMS keys like this:
    18  //
    19  //	sops -d indexer-0-identity.encrypted | go run <path>/<to>/peer_id_from_priv_key.go
    20  func main() {
    21  	all, err := io.ReadAll(os.Stdin)
    22  	if err != nil {
    23  		panic(err)
    24  	}
    25  	key, err := crypto.UnmarshalPrivateKey(all)
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  	pid, err := peer.IDFromPrivateKey(key)
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	fmt.Println(pid.String())
    34  }