Skip to content

Commit

Permalink
Merge pull request #91 from KenshiTech/replace-base58-87
Browse files Browse the repository at this point in the history
Replace base58 with hex
  • Loading branch information
pouya-eghbali authored Mar 25, 2024
2 parents 8bde094 + 3966076 commit 9b68054
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/crypto/bls/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bls

import (
"encoding/hex"
"math/big"

"github.com/KenshiTech/unchained/address"
Expand All @@ -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{
Expand All @@ -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)
Expand Down

0 comments on commit 9b68054

Please sign in to comment.