11package cmd
22
33import (
4+ "errors"
45 "fmt"
56 "os"
67 "path/filepath"
8+ "strings"
79
810 rollconf "github.com/evstack/ev-node/pkg/config"
911 "github.com/evstack/ev-node/pkg/hash"
1012 "github.com/evstack/ev-node/pkg/p2p/key"
13+ "github.com/evstack/ev-node/pkg/signer"
1114 "github.com/evstack/ev-node/pkg/signer/file"
1215)
1316
@@ -18,18 +21,54 @@ func CreateSigner(config *rollconf.Config, homePath string, passphrase string) (
1821 return nil , fmt .Errorf ("passphrase is required when using local file signer" )
1922 }
2023
21- signerDir := filepath .Join (homePath , "config" )
22- if err := os .MkdirAll (signerDir , 0o750 ); err != nil {
24+ signerPath := config .Signer .SignerPath
25+ if signerPath == "" {
26+ signerPath = filepath .Join ("config" )
27+ }
28+ if ! filepath .IsAbs (signerPath ) {
29+ signerPath = filepath .Join (homePath , signerPath )
30+ }
31+ if strings .HasSuffix (strings .ToLower (signerPath ), "signer.json" ) {
32+ signerPath = filepath .Dir (signerPath )
33+ }
34+
35+ if info , err := os .Stat (signerPath ); err == nil {
36+ if ! info .IsDir () {
37+ return nil , fmt .Errorf ("signer path %s must be a directory" , signerPath )
38+ }
39+ } else if err != nil && ! errors .Is (err , os .ErrNotExist ) {
40+ return nil , fmt .Errorf ("failed to inspect signer path: %w" , err )
41+ }
42+
43+ if err := os .MkdirAll (signerPath , 0o750 ); err != nil {
2344 return nil , fmt .Errorf ("failed to create signer directory: %w" , err )
2445 }
2546
26- config . Signer . SignerPath = signerDir
47+ keyFile := filepath . Join ( signerPath , "signer.json" )
2748
28- signer , err := file .CreateFileSystemSigner (config .Signer .SignerPath , []byte (passphrase ))
29- if err != nil {
30- return nil , fmt .Errorf ("failed to initialize signer: %w" , err )
49+ var (
50+ signer signer.Signer
51+ err error
52+ )
53+
54+ if _ , err = os .Stat (keyFile ); err != nil {
55+ if ! errors .Is (err , os .ErrNotExist ) {
56+ return nil , fmt .Errorf ("failed to check signer file: %w" , err )
57+ }
58+
59+ signer , err = file .CreateFileSystemSigner (signerPath , []byte (passphrase ))
60+ if err != nil {
61+ return nil , fmt .Errorf ("failed to initialize signer: %w" , err )
62+ }
63+ } else {
64+ signer , err = file .LoadFileSystemSigner (signerPath , []byte (passphrase ))
65+ if err != nil {
66+ return nil , fmt .Errorf ("failed to load signer: %w" , err )
67+ }
3168 }
3269
70+ config .Signer .SignerPath = signerPath
71+
3372 pubKey , err := signer .GetPublic ()
3473 if err != nil {
3574 return nil , fmt .Errorf ("failed to get public key: %w" , err )
0 commit comments