Skip to content

Commit

Permalink
Merge branch 'tilen/fix_key_padding' into 'main'
Browse files Browse the repository at this point in the history
Fix padding of keys to be always 64 hex values.

See merge request flarenetwork/fast-updates!31
  • Loading branch information
tilenflare committed Jun 12, 2024
2 parents 13a4b6e + ccd1eec commit 8a77341
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions go-client/keygen/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 8a77341

Please sign in to comment.