@@ -37,6 +37,7 @@ import (
3737 evmtypes "github.com/smartcontractkit/chainlink-evm/pkg/types"
3838
3939 "github.com/smartcontractkit/chainlink/v2/core/build"
40+ "github.com/smartcontractkit/chainlink/v2/core/config"
4041 "github.com/smartcontractkit/chainlink/v2/core/logger"
4142 "github.com/smartcontractkit/chainlink/v2/core/services/keystore"
4243 "github.com/smartcontractkit/chainlink/v2/core/services/keystore/chaintype"
@@ -317,6 +318,27 @@ func (s *Shell) RunNode(c *cli.Context) error {
317318 return nil
318319}
319320
321+ type ks [K any ] interface {
322+ Import (ctx context.Context , keyJSON []byte , password string ) (K , error )
323+ }
324+
325+ func importKeyIfProvided [K any ](ctx context.Context , lggr logger.Logger , keyType string , importableKey config.ImportableKey , k ks [K ]) error {
326+ if importableKey .JSON () == "" {
327+ return nil
328+ }
329+
330+ lggr .Debugf ("Importing %s %s" , keyType , importableKey .JSON ())
331+ _ , err := k .Import (ctx , []byte (importableKey .JSON ()), importableKey .Password ())
332+ if errors .Is (err , keystore .ErrKeyExists ) {
333+ lggr .Debugf ("%s key already exists %s" , keyType , importableKey .JSON ())
334+ return nil
335+ } else if err != nil {
336+ return fmt .Errorf ("error importing %s key: %w" , keyType , err )
337+ }
338+
339+ return nil
340+ }
341+
320342func (s * Shell ) runNode (c * cli.Context ) error {
321343 ctx := s .ctx ()
322344 lggr := logger .Sugared (s .Logger .Named ("RunNode" ))
@@ -423,6 +445,11 @@ func (s *Shell) runNode(c *cli.Context) error {
423445 }
424446 }
425447 if s .Config .OCR2 ().Enabled () {
448+ err2 := importKeyIfProvided (rootCtx , lggr , "OCR2Key" , s .Config .ImportedOCR2Key (), app .GetKeyStore ().OCR2 ())
449+ if err2 != nil {
450+ return s .errorOut (err2 )
451+ }
452+
426453 var enabledChains []chaintype.ChainType
427454 if s .Config .EVMEnabled () {
428455 enabledChains = append (enabledChains , chaintype .EVM )
@@ -448,23 +475,19 @@ func (s *Shell) runNode(c *cli.Context) error {
448475 if s .Config .SuiEnabled () {
449476 enabledChains = append (enabledChains , chaintype .Sui )
450477 }
451- err2 : = app .GetKeyStore ().OCR2 ().EnsureKeys (rootCtx , enabledChains ... )
478+ err2 = app .GetKeyStore ().OCR2 ().EnsureKeys (rootCtx , enabledChains ... )
452479 if err2 != nil {
453480 return fmt .Errorf ("failed to ensure ocr key: %w" , err2 )
454481 }
455482 }
456483
457484 if s .Config .P2P ().Enabled () {
458- if s .Config .ImportedP2PKey ().JSON () != "" {
459- lggr .Debugf ("Importing p2p key %s" , s .Config .ImportedP2PKey ().JSON ())
460- _ , err2 := app .GetKeyStore ().P2P ().Import (rootCtx , []byte (s .Config .ImportedP2PKey ().JSON ()), s .Config .ImportedP2PKey ().Password ())
461- if errors .Is (err2 , keystore .ErrKeyExists ) {
462- lggr .Debugf ("P2P key already exists %s" , s .Config .ImportedP2PKey ().JSON ())
463- } else if err2 != nil {
464- return s .errorOut (fmt .Errorf ("error importing p2p key: %w" , err2 ))
465- }
485+ err2 := importKeyIfProvided (rootCtx , lggr , "P2P" , s .Config .ImportedP2PKey (), app .GetKeyStore ().P2P ())
486+ if err2 != nil {
487+ return s .errorOut (err2 )
466488 }
467- err2 := app .GetKeyStore ().P2P ().EnsureKey (rootCtx )
489+
490+ err2 = app .GetKeyStore ().P2P ().EnsureKey (rootCtx )
468491 if err2 != nil {
469492 return fmt .Errorf ("failed to ensure p2p key: %w" , err2 )
470493 }
@@ -525,17 +548,12 @@ func (s *Shell) runNode(c *cli.Context) error {
525548 }
526549 }
527550 if s .Config .CRE ().EnableDKGRecipient () {
528- if s .Config .ImportedDKGRecipientKey ().JSON () != "" {
529- lggr .Debugf ("Importing DKG recipient key %s" , s .Config .ImportedDKGRecipientKey ().JSON ())
530- _ , err2 := app .GetKeyStore ().DKGRecipient ().Import (rootCtx , []byte (s .Config .ImportedDKGRecipientKey ().JSON ()), s .Config .ImportedDKGRecipientKey ().Password ())
531- if errors .Is (err2 , keystore .ErrKeyExists ) {
532- lggr .Debugf ("DKG recipient key already exists %s" , s .Config .ImportedDKGRecipientKey ().JSON ())
533- } else if err2 != nil {
534- return s .errorOut (fmt .Errorf ("error importing dkg recipient key: %w" , err2 ))
535- }
551+ err2 := importKeyIfProvided (rootCtx , lggr , "DKG Recipient" , s .Config .ImportedDKGRecipientKey (), app .GetKeyStore ().DKGRecipient ())
552+ if err2 != nil {
553+ return s .errorOut (err2 )
536554 }
537555
538- err2 : = app .GetKeyStore ().DKGRecipient ().EnsureKey (rootCtx )
556+ err2 = app .GetKeyStore ().DKGRecipient ().EnsureKey (rootCtx )
539557 if err2 != nil {
540558 return fmt .Errorf ("failed to ensure dkg recipient key: %w" , err2 )
541559 }
@@ -546,6 +564,11 @@ func (s *Shell) runNode(c *cli.Context) error {
546564 return fmt .Errorf ("failed to ensure workflow key: %w" , err2 )
547565 }
548566
567+ err2 = importKeyIfProvided (rootCtx , lggr , "CSA" , s .Config .ImportedCSAKey (), app .GetKeyStore ().CSA ())
568+ if err2 != nil {
569+ return s .errorOut (err2 )
570+ }
571+
549572 err2 = app .GetKeyStore ().CSA ().EnsureKey (rootCtx )
550573 if err2 != nil {
551574 return fmt .Errorf ("failed to ensure CSA key: %w" , err2 )
0 commit comments