github.com/prysmaticlabs/prysm@v1.4.4/tools/interop/convert-keys/main.go (about)

     1  // Used for converting keys.yaml files from eth2.0-pm for interop testing.
     2  // See: https://github.com/ethereum/eth2.0-pm/tree/master/interop/mocked_start
     3  //
     4  // This code can be discarded after interop testing.
     5  package main
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"log"
    12  	"os"
    13  
    14  	"github.com/prysmaticlabs/prysm/tools/unencrypted-keys-gen/keygen"
    15  	"gopkg.in/yaml.v2"
    16  )
    17  
    18  // KeyPair with hex encoded data.
    19  type KeyPair struct {
    20  	Priv string `yaml:"privkey"`
    21  	Pub  string `yaml:"pubkey"`
    22  }
    23  
    24  // KeyPairs represent the data format in the upstream yaml.
    25  type KeyPairs []KeyPair
    26  
    27  func main() {
    28  	if len(os.Args) < 3 {
    29  		fmt.Println("Usage: convert-keys path/to/keys.yaml path/to/output.json")
    30  		return
    31  	}
    32  	inFile := os.Args[1]
    33  
    34  	in, err := ioutil.ReadFile(inFile)
    35  	if err != nil {
    36  		log.Fatalf("Failed to read file %s: %v", inFile, err)
    37  	}
    38  	data := make(KeyPairs, 0)
    39  	if err := yaml.Unmarshal(in, &data); err != nil {
    40  		log.Fatalf("Failed to unmarshal yaml: %v", err)
    41  	}
    42  
    43  	out := &keygen.UnencryptedKeysContainer{}
    44  	for _, key := range data {
    45  		pk, err := hex.DecodeString(key.Priv[2:])
    46  		if err != nil {
    47  			log.Fatalf("Failed to decode hex string %s: %v", key.Priv, err)
    48  		}
    49  
    50  		out.Keys = append(out.Keys, &keygen.UnencryptedKeys{
    51  			ValidatorKey:  pk,
    52  			WithdrawalKey: pk,
    53  		})
    54  	}
    55  
    56  	outFile, err := os.Create(os.Args[2])
    57  	if err != nil {
    58  		log.Fatalf("Failed to create file at %s: %v", os.Args[2], err)
    59  	}
    60  	cleanup := func() {
    61  		if err := outFile.Close(); err != nil {
    62  			panic(err)
    63  		}
    64  	}
    65  	defer cleanup()
    66  	if err := keygen.SaveUnencryptedKeysToFile(outFile, out); err != nil {
    67  		// log.Fatalf will prevent defer from being called
    68  		cleanup()
    69  		log.Fatalf("Failed to save %v", err)
    70  	}
    71  	log.Printf("Wrote %s\n", os.Args[2])
    72  }