From ccd1eec3e166b78f6a6b1e98d3c0da8c7f21bcfa Mon Sep 17 00:00:00 2001 From: Tilen Marc Date: Wed, 12 Jun 2024 08:28:34 +0000 Subject: [PATCH] Fix padding of keys to be always 64 hex values. --- go-client/README.md | 2 +- go-client/keygen/keygen.go | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/go-client/README.md b/go-client/README.md index 40a388f..1976175 100644 --- a/go-client/README.md +++ b/go-client/README.md @@ -151,7 +151,7 @@ go run keygen/keygen.go --key 0x1512de600a10a0aac01580dbfc080965b89ed2329a7b2bf5 ``` where the key value needs to be replaced by the generated private key and the address value needs -to be replaced by the actual address that will be used to sign the updates. +to be replaced by the actual _entity_ address. In case you forgot your public key, but saved the private one, you can run the following for outputting the key: ```bash diff --git a/go-client/keygen/keygen.go b/go-client/keygen/keygen.go index f798db0..5dd0f91 100644 --- a/go-client/keygen/keygen.go +++ b/go-client/keygen/keygen.go @@ -15,6 +15,7 @@ import ( "log" "math/big" "os" + "strings" "github.com/consensys/gnark-crypto/ecc/bn254" "github.com/consensys/gnark-crypto/ecc/bn254/fp" @@ -63,7 +64,7 @@ func main() { } if *KeyOutFlag == "" { - keysString := keyStrings{PrivateKey: "0x" + keys.Sk.Text(16), PublicKeyX: "0x" + keys.Pk.X.Text(16), PublicKeyY: "0x" + keys.Pk.Y.Text(16)} + keysString := keyStrings{PrivateKey: PadKey(keys.Sk.Text(16)), PublicKeyX: PadKey(keys.Pk.X.Text(16)), PublicKeyY: PadKey(keys.Pk.Y.Text(16))} keyBytes, err := json.Marshal(keysString) if err != nil { log.Fatal(err) @@ -79,7 +80,7 @@ func main() { log.Fatal(err) } - keyEncrypted := keyEncrypted{EncryptedPrivateKey: encryptedPrivateKey, PublicKeyX: "0x" + keys.Pk.X.Text(16), PublicKeyY: "0x" + keys.Pk.Y.Text(16)} + keyEncrypted := keyEncrypted{EncryptedPrivateKey: encryptedPrivateKey, PublicKeyX: PadKey(keys.Pk.X.Text(16)), PublicKeyY: PadKey(keys.Pk.Y.Text(16))} keyBytes, err := json.Marshal(keyEncrypted) if err != nil { log.Fatal(err) @@ -182,6 +183,9 @@ func main() { if err != nil { log.Fatal(err) } + if len(sigBytes) != 96 { + log.Fatal("signature not correct length") + } if *SigOutFlag == "" { logger.Info("Signature generated: 0x" + hex.EncodeToString(sigBytes)) @@ -209,7 +213,7 @@ func main() { } else { // in the case that a key was provided but not used for the signature, just print it out if *InFileFlag != "" || *InFlag != "" { - keysString := keyStrings{PrivateKey: "0x" + keys.Sk.Text(16), PublicKeyX: "0x" + keys.Pk.X.Text(16), PublicKeyY: "0x" + keys.Pk.Y.Text(16)} + keysString := keyStrings{PrivateKey: PadKey(keys.Sk.Text(16)), PublicKeyX: PadKey(keys.Pk.X.Text(16)), PublicKeyY: PadKey(keys.Pk.Y.Text(16))} keyBytes, err := json.Marshal(keysString) if err != nil { log.Fatal(err) @@ -219,6 +223,13 @@ func main() { } } +func PadKey(hexKey string) string { + if len(hexKey) > 64 { + logger.Fatal("key too long") + } + return "0x" + strings.Repeat("0", 64-len(hexKey)) + hexKey +} + func Encrypt(password, data []byte) ([]byte, error) { key, salt, err := DeriveKey(password, nil) if err != nil {