2019-09-09 14:31:19 -07:00
|
|
|
// Used for converting keys.yaml files from eth2.0-pm for interop testing.
|
|
|
|
// See: https://github.com/ethereum/eth2.0-pm/tree/master/interop/mocked_start
|
|
|
|
//
|
|
|
|
// This code can be discarded after interop testing.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2024-02-14 21:46:47 -08:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/tools/unencrypted-keys-gen/keygen"
|
2022-08-05 05:52:02 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-09-10 19:54:14 +05:30
|
|
|
"gopkg.in/yaml.v2"
|
2019-09-09 14:31:19 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// KeyPair with hex encoded data.
|
|
|
|
type KeyPair struct {
|
|
|
|
Priv string `yaml:"privkey"`
|
|
|
|
Pub string `yaml:"pubkey"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyPairs represent the data format in the upstream yaml.
|
|
|
|
type KeyPairs []KeyPair
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) < 3 {
|
|
|
|
fmt.Println("Usage: convert-keys path/to/keys.yaml path/to/output.json")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
inFile := os.Args[1]
|
|
|
|
|
2022-04-18 22:42:07 +02:00
|
|
|
in, err := os.ReadFile(inFile) // #nosec G304
|
2019-09-09 14:31:19 -07:00
|
|
|
if err != nil {
|
2022-08-05 05:52:02 -05:00
|
|
|
log.WithError(err).Fatalf("Failed to read file %s", inFile)
|
2019-09-09 14:31:19 -07:00
|
|
|
}
|
|
|
|
data := make(KeyPairs, 0)
|
2021-07-31 20:26:24 -07:00
|
|
|
if err := yaml.UnmarshalStrict(in, &data); err != nil {
|
2022-08-05 05:52:02 -05:00
|
|
|
log.WithError(err).Fatal("Failed to unmarshal yaml")
|
2019-09-09 14:31:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
out := &keygen.UnencryptedKeysContainer{}
|
|
|
|
for _, key := range data {
|
|
|
|
pk, err := hex.DecodeString(key.Priv[2:])
|
|
|
|
if err != nil {
|
2022-08-05 05:52:02 -05:00
|
|
|
log.WithError(err).Fatalf("Failed to decode hex string %s", key.Priv)
|
2019-09-09 14:31:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
out.Keys = append(out.Keys, &keygen.UnencryptedKeys{
|
2019-09-10 19:54:14 +05:30
|
|
|
ValidatorKey: pk,
|
2019-09-09 14:31:19 -07:00
|
|
|
WithdrawalKey: pk,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-16 05:41:46 +08:00
|
|
|
outFile, err := os.OpenFile(os.Args[2], os.O_CREATE|os.O_EXCL, params.BeaconIoConfig().ReadWritePermissions)
|
2019-09-09 14:31:19 -07:00
|
|
|
if err != nil {
|
2022-08-05 05:52:02 -05:00
|
|
|
log.WithError(err).Fatalf("Failed to create file at %s", os.Args[2])
|
2019-09-09 14:31:19 -07:00
|
|
|
}
|
2020-10-04 17:03:10 +02:00
|
|
|
cleanup := func() {
|
2020-04-12 21:11:09 -07:00
|
|
|
if err := outFile.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-10-04 17:03:10 +02:00
|
|
|
}
|
|
|
|
defer cleanup()
|
2019-09-09 14:31:19 -07:00
|
|
|
if err := keygen.SaveUnencryptedKeysToFile(outFile, out); err != nil {
|
2020-10-04 17:03:10 +02:00
|
|
|
// log.Fatalf will prevent defer from being called
|
|
|
|
cleanup()
|
2022-08-05 05:52:02 -05:00
|
|
|
log.WithError(err).Fatal("Failed to save")
|
2019-09-09 14:31:19 -07:00
|
|
|
}
|
|
|
|
log.Printf("Wrote %s\n", os.Args[2])
|
|
|
|
}
|