From 49c3ae6a5779f4495a88e31c6e45165d4a78108d Mon Sep 17 00:00:00 2001
From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com>
Date: Wed, 17 Apr 2024 10:21:30 +0000
Subject: [PATCH] fix: secp256r1 json missing quotes (backport #20060) (#20069)

Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
---
 crypto/keys/secp256r1/privkey.go | 4 ++--
 crypto/keys/secp256r1/pubkey.go  | 5 +++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/crypto/keys/secp256r1/privkey.go b/crypto/keys/secp256r1/privkey.go
index a0ae1b069bc0..3080dec21785 100644
--- a/crypto/keys/secp256r1/privkey.go
+++ b/crypto/keys/secp256r1/privkey.go
@@ -64,12 +64,12 @@ func (sk ecdsaSK) Marshal() ([]byte, error) {
 // MarshalJSON implements customProtobufType.
 func (sk ecdsaSK) MarshalJSON() ([]byte, error) {
 	b64 := base64.StdEncoding.EncodeToString(sk.PrivKey.Bytes())
-	return []byte(b64), nil
+	return []byte("\"" + b64 + "\""), nil
 }
 
 // UnmarshalJSON implements customProtobufType.
 func (sk *ecdsaSK) UnmarshalJSON(data []byte) error {
-	bz, err := base64.StdEncoding.DecodeString(string(data))
+	bz, err := base64.StdEncoding.DecodeString(string(data[1 : len(data)-1]))
 	if err != nil {
 		return err
 	}
diff --git a/crypto/keys/secp256r1/pubkey.go b/crypto/keys/secp256r1/pubkey.go
index c0d4cfc956b8..fea06f73f201 100644
--- a/crypto/keys/secp256r1/pubkey.go
+++ b/crypto/keys/secp256r1/pubkey.go
@@ -73,12 +73,13 @@ func (pk ecdsaPK) Marshal() ([]byte, error) {
 // MarshalJSON implements customProtobufType.
 func (pk ecdsaPK) MarshalJSON() ([]byte, error) {
 	b64 := base64.StdEncoding.EncodeToString(pk.PubKey.Bytes())
-	return []byte(b64), nil
+	return []byte("\"" + b64 + "\""), nil
 }
 
 // UnmarshalJSON implements customProtobufType.
 func (pk *ecdsaPK) UnmarshalJSON(data []byte) error {
-	bz, err := base64.StdEncoding.DecodeString(string(data))
+	// the string is quoted so we need to remove them
+	bz, err := base64.StdEncoding.DecodeString(string(data[1 : len(data)-1]))
 	if err != nil {
 		return err
 	}