From ecbf1e4e3b01bbf5d55b3077c63843e5a5dee751 Mon Sep 17 00:00:00 2001 From: Marc Font <36164126+Marketen@users.noreply.github.com> Date: Wed, 15 May 2024 11:32:17 +0200 Subject: [PATCH] Init bls only once before serving api (#37) * init bls only once before serving api * change bls init to main --- listener/cmd/listener/main.go | 10 ++++++++++ listener/internal/api/validation/VerifySignature.go | 11 ----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/listener/cmd/listener/main.go b/listener/cmd/listener/main.go index 0fd4b7d..0a19e04 100644 --- a/listener/cmd/listener/main.go +++ b/listener/cmd/listener/main.go @@ -4,6 +4,7 @@ import ( "github.com/dappnode/validator-monitoring/listener/internal/api" "github.com/dappnode/validator-monitoring/listener/internal/config" "github.com/dappnode/validator-monitoring/listener/internal/logger" + "github.com/herumi/bls-eth-go-binary/bls" ) func main() { @@ -15,6 +16,15 @@ func main() { } logger.SetLogLevelFromString(config.LogLevel) + // This is a configuration of the BLS library at the process level. Notice how bls.Init() does not return an initialized BLS object. + // Any call to bls functions within the process will use this configuration. We initialize bls before starting the api. + if err := bls.Init(bls.BLS12_381); err != nil { + logger.Fatal("Failed to initialize BLS: " + err.Error()) + } + if err := bls.SetETHmode(bls.EthModeDraft07); err != nil { + logger.Fatal("Failed to set BLS ETH mode: " + err.Error()) + } + s := api.NewApi( config.Port, config.MongoDBURI, diff --git a/listener/internal/api/validation/VerifySignature.go b/listener/internal/api/validation/VerifySignature.go index 2b5ae1b..39a742e 100644 --- a/listener/internal/api/validation/VerifySignature.go +++ b/listener/internal/api/validation/VerifySignature.go @@ -11,17 +11,6 @@ import ( ) func VerifySignature(req types.SignatureRequestDecoded) (bool, error) { - // Initialize the BLS system - if err := bls.Init(bls.BLS12_381); err != nil { - logger.Error("Failed to initialize BLS system: " + err.Error()) - return false, err - } - // Set the BLS mode to ETH draft 07 - if err := bls.SetETHmode(bls.EthModeDraft07); err != nil { - logger.Error("Failed to set BLS mode to ETH draft 07: " + err.Error()) - return false, err - } - // Decode the public key from hex, remove the 0x prefix ONLY if exists from req.Pubkey req.Pubkey = strings.TrimPrefix(req.Pubkey, "0x") req.Pubkey = strings.TrimSpace(req.Pubkey)