@@ -15,6 +15,7 @@ import (
1515 _ "github.com/lib/pq"
1616 "github.com/spf13/cobra"
1717
18+ "github.com/smartcontractkit/chainlink-common/keystore"
1819 ks "github.com/smartcontractkit/chainlink-common/keystore"
1920 "github.com/smartcontractkit/chainlink-common/keystore/kms"
2021 "github.com/smartcontractkit/chainlink-common/keystore/pgstore"
@@ -114,7 +115,7 @@ func NewGetCmd() *cobra.Command {
114115 cmd := cobra.Command {
115116 Use : "get" , Short : "Get keys" ,
116117 RunE : func (cmd * cobra.Command , args []string ) error {
117- return runKeystoreCommandWithSigner [ks.GetKeysRequest , ks.GetKeysResponse ](cmd , args , func (ctx context.Context , k ks.KeystoreSignerReader , req ks.GetKeysRequest ) (ks.GetKeysResponse , error ) {
118+ return runKeystoreCommand [ks.KeystoreSignerReader , ks. GetKeysRequest , ks.GetKeysResponse ](cmd , args , loadKeystoreSignerReader , func (ctx context.Context , k ks.KeystoreSignerReader , req ks.GetKeysRequest ) (ks.GetKeysResponse , error ) {
118119 return k .GetKeys (ctx , req )
119120 })
120121 },
@@ -128,7 +129,7 @@ func NewCreateCmd() *cobra.Command {
128129 cmd := cobra.Command {
129130 Use : "create" , Short : "Create a key" ,
130131 RunE : func (cmd * cobra.Command , args []string ) error {
131- return runKeystoreCommand [ks.CreateKeysRequest , ks.CreateKeysResponse ](cmd , args , func (ctx context.Context , k ks .Keystore , req ks.CreateKeysRequest ) (ks.CreateKeysResponse , error ) {
132+ return runKeystoreCommand [ks.Keystore , ks. CreateKeysRequest , ks.CreateKeysResponse ](cmd , args , loadKeystore , func (ctx context.Context , k keystore .Keystore , req ks.CreateKeysRequest ) (ks.CreateKeysResponse , error ) {
132133 return k .CreateKeys (ctx , req )
133134 })
134135 },
@@ -188,7 +189,7 @@ func NewExportCmd() *cobra.Command {
188189 cmd := cobra.Command {
189190 Use : "export" , Short : "Export a key to an encrypted JSON file" ,
190191 RunE : func (cmd * cobra.Command , args []string ) error {
191- return runKeystoreCommand [ks.ExportKeysRequest , ks.ExportKeysResponse ](cmd , args , func (ctx context.Context , k ks.Keystore , req ks.ExportKeysRequest ) (ks.ExportKeysResponse , error ) {
192+ return runKeystoreCommand [ks.Keystore , ks. ExportKeysRequest , ks.ExportKeysResponse ](cmd , args , loadKeystore , func (ctx context.Context , k ks.Keystore , req ks.ExportKeysRequest ) (ks.ExportKeysResponse , error ) {
192193 return k .ExportKeys (ctx , req )
193194 })
194195 },
@@ -202,7 +203,7 @@ func NewImportCmd() *cobra.Command {
202203 cmd := cobra.Command {
203204 Use : "import" , Short : "Import an encrypted key JSON file" ,
204205 RunE : func (cmd * cobra.Command , args []string ) error {
205- return runKeystoreCommand [ks.ImportKeysRequest , ks.ImportKeysResponse ](cmd , args , func (ctx context.Context , k ks.Keystore , req ks.ImportKeysRequest ) (ks.ImportKeysResponse , error ) {
206+ return runKeystoreCommand [ks.Keystore , ks. ImportKeysRequest , ks.ImportKeysResponse ](cmd , args , loadKeystore , func (ctx context.Context , k ks.Keystore , req ks.ImportKeysRequest ) (ks.ImportKeysResponse , error ) {
206207 return k .ImportKeys (ctx , req )
207208 })
208209 },
@@ -216,7 +217,7 @@ func NewSetMetadataCmd() *cobra.Command {
216217 cmd := cobra.Command {
217218 Use : "set-metadata" , Short : "Set metadata for keys" ,
218219 RunE : func (cmd * cobra.Command , args []string ) error {
219- return runKeystoreCommand [ks.SetMetadataRequest , ks.SetMetadataResponse ](cmd , args , func (ctx context.Context , k ks.Keystore , req ks.SetMetadataRequest ) (ks.SetMetadataResponse , error ) {
220+ return runKeystoreCommand [ks.Keystore , ks. SetMetadataRequest , ks.SetMetadataResponse ](cmd , args , loadKeystore , func (ctx context.Context , k ks.Keystore , req ks.SetMetadataRequest ) (ks.SetMetadataResponse , error ) {
220221 return k .SetMetadata (ctx , req )
221222 })
222223 },
@@ -226,7 +227,13 @@ func NewSetMetadataCmd() *cobra.Command {
226227 return & cmd
227228}
228229
229- func runKeystoreCommandWithSigner [Req any , Resp any ](cmd * cobra.Command , args []string , fn func (ctx context.Context , k ks.KeystoreSignerReader , req Req ) (Resp , error )) error {
230+ // runKeystoreCommandGeneric is a generic helper that runs a keystore command with a custom loader function.
231+ func runKeystoreCommand [K any , Req any , Resp any ](
232+ cmd * cobra.Command ,
233+ args []string ,
234+ loader func (ctx context.Context , cmd * cobra.Command ) (K , error ),
235+ fn func (ctx context.Context , k K , req Req ) (Resp , error ),
236+ ) error {
230237 jsonBytes , err := readJSONInput (cmd )
231238 if err != nil {
232239 return err
@@ -238,38 +245,7 @@ func runKeystoreCommandWithSigner[Req any, Resp any](cmd *cobra.Command, args []
238245 }
239246 ctx , cancel := context .WithTimeout (cmd .Context (), KeystoreLoadTimeout )
240247 defer cancel ()
241- k , err := loadKeystoreSignerReader (ctx , cmd )
242- if err != nil {
243- return err
244- }
245- resp , err := fn (ctx , k , req )
246- if err != nil {
247- return err
248- }
249- jsonBytesOut , err := json .Marshal (resp )
250- if err != nil {
251- return err
252- }
253- _ , err = cmd .OutOrStdout ().Write (jsonBytesOut )
254- if err != nil {
255- return err
256- }
257- return nil
258- }
259-
260- func runKeystoreCommand [Req any , Resp any ](cmd * cobra.Command , args []string , fn func (ctx context.Context , k ks.Keystore , req Req ) (Resp , error )) error {
261- jsonBytes , err := readJSONInput (cmd )
262- if err != nil {
263- return err
264- }
265- var req Req
266- err = json .Unmarshal (jsonBytes , & req )
267- if err != nil {
268- return err
269- }
270- ctx , cancel := context .WithTimeout (cmd .Context (), KeystoreLoadTimeout )
271- defer cancel ()
272- k , err := loadKeystore (ctx , cmd )
248+ k , err := loader (ctx , cmd )
273249 if err != nil {
274250 return err
275251 }
@@ -292,7 +268,7 @@ func NewSignCmd() *cobra.Command {
292268 cmd := cobra.Command {
293269 Use : "sign" , Short : "Sign data with a key" ,
294270 RunE : func (cmd * cobra.Command , args []string ) error {
295- return runKeystoreCommandWithSigner [ks.SignRequest , ks.SignResponse ](cmd , args , func (ctx context.Context , k ks.KeystoreSignerReader , req ks.SignRequest ) (ks.SignResponse , error ) {
271+ return runKeystoreCommand [ks.KeystoreSignerReader , ks. SignRequest , ks.SignResponse ](cmd , args , loadKeystoreSignerReader , func (ctx context.Context , k ks.KeystoreSignerReader , req ks.SignRequest ) (ks.SignResponse , error ) {
296272 return k .Sign (ctx , req )
297273 })
298274 },
@@ -331,7 +307,7 @@ func NewVerifyCmd() *cobra.Command {
331307 cmd := cobra.Command {
332308 Use : "verify" , Short : "Verify a signature" ,
333309 RunE : func (cmd * cobra.Command , args []string ) error {
334- return runKeystoreCommandWithSigner [ks.VerifyRequest , ks.VerifyResponse ](cmd , args , func (ctx context.Context , k ks.KeystoreSignerReader , req ks.VerifyRequest ) (ks.VerifyResponse , error ) {
310+ return runKeystoreCommand [ks.KeystoreSignerReader , ks. VerifyRequest , ks.VerifyResponse ](cmd , args , loadKeystoreSignerReader , func (ctx context.Context , k ks.KeystoreSignerReader , req ks.VerifyRequest ) (ks.VerifyResponse , error ) {
335311 return k .Verify (ctx , req )
336312 })
337313 },
@@ -345,7 +321,7 @@ func NewEncryptCmd() *cobra.Command {
345321 cmd := cobra.Command {
346322 Use : "encrypt" , Short : "Encrypt data to a remote public key" ,
347323 RunE : func (cmd * cobra.Command , args []string ) error {
348- return runKeystoreCommand [ks.EncryptRequest , ks.EncryptResponse ](cmd , args , func (ctx context.Context , k ks.Keystore , req ks.EncryptRequest ) (ks.EncryptResponse , error ) {
324+ return runKeystoreCommand [ks.Keystore , ks. EncryptRequest , ks.EncryptResponse ](cmd , args , loadKeystore , func (ctx context.Context , k ks.Keystore , req ks.EncryptRequest ) (ks.EncryptResponse , error ) {
349325 return k .Encrypt (ctx , req )
350326 })
351327 },
@@ -384,7 +360,7 @@ func NewDecryptCmd() *cobra.Command {
384360 cmd := cobra.Command {
385361 Use : "decrypt" , Short : "Decrypt data with a key" ,
386362 RunE : func (cmd * cobra.Command , args []string ) error {
387- return runKeystoreCommand [ks.DecryptRequest , ks.DecryptResponse ](cmd , args , func (ctx context.Context , k ks.Keystore , req ks.DecryptRequest ) (ks.DecryptResponse , error ) {
363+ return runKeystoreCommand [ks.Keystore , ks. DecryptRequest , ks.DecryptResponse ](cmd , args , loadKeystore , func (ctx context.Context , k ks.Keystore , req ks.DecryptRequest ) (ks.DecryptResponse , error ) {
388364 return k .Decrypt (ctx , req )
389365 })
390366 },
@@ -398,23 +374,19 @@ func loadKeystoreSignerReader(ctx context.Context, cmd *cobra.Command) (ks.Keyst
398374 // Check if KMS mode is enabled
399375 kmsProfile := os .Getenv ("KEYSTORE_KMS_PROFILE" )
400376 if kmsProfile != "" {
401- return loadKMSKeystore (ctx )
377+ kmsProfile := os .Getenv ("KEYSTORE_KMS_PROFILE" )
378+ if kmsProfile == "" {
379+ return nil , errors .New ("KEYSTORE_KMS_PROFILE is required for KMS keystore" )
380+ }
381+ client , err := kms .NewClient (kmsProfile )
382+ if err != nil {
383+ return nil , fmt .Errorf ("create KMS client: %w" , err )
384+ }
385+ return kms .NewKeystore (client )
402386 }
403387 return loadKeystore (ctx , cmd )
404388}
405389
406- func loadKMSKeystore (ctx context.Context ) (ks.KeystoreSignerReader , error ) {
407- kmsProfile := os .Getenv ("KEYSTORE_KMS_PROFILE" )
408- if kmsProfile == "" {
409- return nil , errors .New ("KEYSTORE_KMS_PROFILE is required for KMS keystore" )
410- }
411- client , err := kms .NewClient (kmsProfile )
412- if err != nil {
413- return nil , fmt .Errorf ("create KMS client: %w" , err )
414- }
415- return kms .NewKeystore (client )
416- }
417-
418390func loadKeystore (ctx context.Context , cmd * cobra.Command ) (ks.Keystore , error ) {
419391 // Read from environment variables only
420392 filePath := os .Getenv ("KEYSTORE_FILE_PATH" )
0 commit comments