@@ -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,28 @@ 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+ keyJSON := importableKey .JSON ()
327+ if keyJSON == "" {
328+ return nil
329+ }
330+
331+ lggr .Debugf ("Importing %s %s" , keyType , keyJSON )
332+ _ , err := k .Import (ctx , []byte (keyJSON ), importableKey .Password ())
333+ if errors .Is (err , keystore .ErrKeyExists ) {
334+ lggr .Debugf ("%s key already exists %s" , keyType , keyJSON )
335+ return nil
336+ } else if err != nil {
337+ return fmt .Errorf ("error importing %s key: %w" , keyType , err )
338+ }
339+
340+ return nil
341+ }
342+
320343func (s * Shell ) runNode (c * cli.Context ) error {
321344 ctx := s .ctx ()
322345 lggr := logger .Sugared (s .Logger .Named ("RunNode" ))
@@ -423,6 +446,11 @@ func (s *Shell) runNode(c *cli.Context) error {
423446 }
424447 }
425448 if s .Config .OCR2 ().Enabled () {
449+ err2 := importKeyIfProvided (rootCtx , lggr , "OCR2Key" , s .Config .ImportedOCR2Key (), app .GetKeyStore ().OCR2 ())
450+ if err2 != nil {
451+ return s .errorOut (err2 )
452+ }
453+
426454 var enabledChains []chaintype.ChainType
427455 if s .Config .EVMEnabled () {
428456 enabledChains = append (enabledChains , chaintype .EVM )
@@ -448,23 +476,19 @@ func (s *Shell) runNode(c *cli.Context) error {
448476 if s .Config .SuiEnabled () {
449477 enabledChains = append (enabledChains , chaintype .Sui )
450478 }
451- err2 : = app .GetKeyStore ().OCR2 ().EnsureKeys (rootCtx , enabledChains ... )
479+ err2 = app .GetKeyStore ().OCR2 ().EnsureKeys (rootCtx , enabledChains ... )
452480 if err2 != nil {
453481 return fmt .Errorf ("failed to ensure ocr key: %w" , err2 )
454482 }
455483 }
456484
457485 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- }
486+ err2 := importKeyIfProvided (rootCtx , lggr , "P2P" , s .Config .ImportedP2PKey (), app .GetKeyStore ().P2P ())
487+ if err2 != nil {
488+ return s .errorOut (err2 )
466489 }
467- err2 := app .GetKeyStore ().P2P ().EnsureKey (rootCtx )
490+
491+ err2 = app .GetKeyStore ().P2P ().EnsureKey (rootCtx )
468492 if err2 != nil {
469493 return fmt .Errorf ("failed to ensure p2p key: %w" , err2 )
470494 }
@@ -525,17 +549,12 @@ func (s *Shell) runNode(c *cli.Context) error {
525549 }
526550 }
527551 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- }
552+ err2 := importKeyIfProvided (rootCtx , lggr , "DKG Recipient" , s .Config .ImportedDKGRecipientKey (), app .GetKeyStore ().DKGRecipient ())
553+ if err2 != nil {
554+ return s .errorOut (err2 )
536555 }
537556
538- err2 : = app .GetKeyStore ().DKGRecipient ().EnsureKey (rootCtx )
557+ err2 = app .GetKeyStore ().DKGRecipient ().EnsureKey (rootCtx )
539558 if err2 != nil {
540559 return fmt .Errorf ("failed to ensure dkg recipient key: %w" , err2 )
541560 }
@@ -546,6 +565,11 @@ func (s *Shell) runNode(c *cli.Context) error {
546565 return fmt .Errorf ("failed to ensure workflow key: %w" , err2 )
547566 }
548567
568+ err2 = importKeyIfProvided (rootCtx , lggr , "CSA" , s .Config .ImportedCSAKey (), app .GetKeyStore ().CSA ())
569+ if err2 != nil {
570+ return s .errorOut (err2 )
571+ }
572+
549573 err2 = app .GetKeyStore ().CSA ().EnsureKey (rootCtx )
550574 if err2 != nil {
551575 return fmt .Errorf ("failed to ensure CSA key: %w" , err2 )
0 commit comments