From 39660763aea2f209d442110e4e02fd89b3e6d9e1 Mon Sep 17 00:00:00 2001 From: Pouya Date: Mon, 25 Mar 2024 20:03:39 +0100 Subject: [PATCH] Replace base58 with hex --- src/crypto/bls/store.go | 45 +++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/crypto/bls/store.go b/src/crypto/bls/store.go index a6f17e49..4f1d3ae7 100644 --- a/src/crypto/bls/store.go +++ b/src/crypto/bls/store.go @@ -1,6 +1,7 @@ package bls import ( + "encoding/hex" "math/big" "github.com/KenshiTech/unchained/address" @@ -17,38 +18,52 @@ var ClientPublicKey *bls12381.G2Affine var ClientShortPublicKey *bls12381.G1Affine var ClientSigner Signer +func saveConfig() { + pkBytes := ClientPublicKey.Bytes() + + config.Secrets.Set("secretKey", hex.EncodeToString(ClientSecretKey.Bytes())) + config.Secrets.Set("publicKey", hex.EncodeToString(pkBytes[:])) + + err := config.Secrets.WriteConfig() + + if err != nil { + panic(err) + } +} + func InitClientIdentity() { var err error - var pkBytes [96]byte if config.Secrets.IsSet("secretKey") { - decoded := base58.Decode(config.Secrets.GetString("secretKey")) + secretKeyFromConfig := config.Secrets.GetString("secretKey") + decoded, err := hex.DecodeString(secretKeyFromConfig) + + if err != nil { + // TODO: Backwards compatibility with base58 encoded secret keys + // Remove this after a few releases + decoded = base58.Decode(secretKeyFromConfig) + } ClientSecretKey = new(big.Int) ClientSecretKey.SetBytes(decoded) - ClientPublicKey = GetPublicKey(ClientSecretKey) - pkBytes = ClientPublicKey.Bytes() - } else { - ClientSecretKey, ClientPublicKey, err = GenerateKeyPair() if err != nil { - panic(err) + saveConfig() } - - pkBytes = ClientPublicKey.Bytes() - - config.Secrets.Set("secretKey", base58.Encode(ClientSecretKey.Bytes())) - config.Secrets.Set("publicKey", base58.Encode(pkBytes[:])) - - err = config.Secrets.WriteConfig() + } else { + ClientSecretKey, ClientPublicKey, err = GenerateKeyPair() if err != nil { panic(err) } + + saveConfig() } ClientShortPublicKey = GetShortPublicKey(ClientSecretKey) + + pkBytes := ClientPublicKey.Bytes() addrStr := address.Calculate(pkBytes[:]) ClientSigner = Signer{ @@ -63,7 +78,7 @@ func InitClientIdentity() { Info("Unchained") // TODO: Avoid recalculating this - config.Secrets.Set("publicKey", base58.Encode(pkBytes[:])) + config.Secrets.Set("publicKey", hex.EncodeToString(pkBytes[:])) if !config.Secrets.IsSet("address") { config.Secrets.Set("address", addrStr)