@@ -15,7 +15,7 @@ import (
1515 "github.com/initia-labs/weave/common"
1616 weavecontext "github.com/initia-labs/weave/context"
1717 "github.com/initia-labs/weave/cosmosutils"
18- "github.com/initia-labs/weave/io"
18+ weaveio "github.com/initia-labs/weave/io"
1919 "github.com/initia-labs/weave/models/opinit_bots"
2020 "github.com/initia-labs/weave/service"
2121)
@@ -136,27 +136,6 @@ func OPInitBotsKeysSetupCommand() *cobra.Command {
136136 return setupCmd
137137}
138138
139- func generateKeyFile (keyPath string , botName string ) (opinit_bots.KeyFile , error ) {
140- keyFile , err := opinit_bots .GenerateMnemonicKeyfile (botName )
141- if err != nil {
142- return keyFile , err
143- }
144-
145- // Marshal KeyFile to JSON
146- data , err := json .MarshalIndent (keyFile , "" , " " )
147- if err != nil {
148- return keyFile , fmt .Errorf ("error marshaling KeyFile to JSON: %w" , err )
149- }
150-
151- // Write JSON data to a file
152- err = os .WriteFile (keyPath , data , 0644 )
153- if err != nil {
154- return keyFile , fmt .Errorf ("error writing to file: %w" , err )
155- }
156-
157- return keyFile , nil
158- }
159-
160139func validateConfigFlags (args []string , configPath , keyFilePath string , isGenerateKeyFile bool ) error {
161140 if configPath != "" {
162141 if len (args ) == 0 {
@@ -168,7 +147,7 @@ func validateConfigFlags(args []string, configPath, keyFilePath string, isGenera
168147 if keyFilePath == "" && ! isGenerateKeyFile {
169148 return fmt .Errorf ("invalid configuration: if --with-config is set, either --generate-key-file or --key-file must be provided" )
170149 }
171- if ! io .FileOrFolderExists (configPath ) {
150+ if ! weaveio .FileOrFolderExists (configPath ) {
172151 return fmt .Errorf ("the provided --with-config does not exist: %s" , configPath )
173152 }
174153 } else {
@@ -192,16 +171,20 @@ func handleWithConfig(cmd *cobra.Command, userHome, opInitHome, configPath, keyF
192171 return err
193172 }
194173
195- var keyFile opinit_bots .KeyFile
174+ var keyFile * weaveio .KeyFile
196175 if isGenerateKeyFile {
197176 keyPath := filepath .Join (userHome , common .WeaveDataDirectory , fmt .Sprintf ("%s.%s.keyfile" , common .OpinitGeneratedKeyFilename , botName ))
198- keyFile , err = generateKeyFile ( keyPath , botName )
177+ keyFile , err = opinit_bots . GenerateMnemonicKeyfile ( botName )
199178 if err != nil {
200- return err
179+ return fmt .Errorf ("error generating keyfile: %v" , err )
180+ }
181+ err = keyFile .Write (keyPath )
182+ if err != nil {
183+ return fmt .Errorf ("error writing to file: %w" , err )
201184 }
202185 fmt .Printf ("Key file successfully generated. You can find it at: %s\n " , keyPath )
203186 } else {
204- if ! io .FileOrFolderExists (keyFilePath ) {
187+ if ! weaveio .FileOrFolderExists (keyFilePath ) {
205188 return fmt .Errorf ("key file is missing at path: %s" , keyFilePath )
206189 }
207190
@@ -225,25 +208,25 @@ func handleWithConfig(cmd *cobra.Command, userHome, opInitHome, configPath, keyF
225208}
226209
227210// readAndUnmarshalKeyFile read and unmarshal the key file into the KeyFile struct
228- func readAndUnmarshalKeyFile (keyFilePath string ) (opinit_bots .KeyFile , error ) {
211+ func readAndUnmarshalKeyFile (keyFilePath string ) (* weaveio .KeyFile , error ) {
229212 fileData , err := os .ReadFile (keyFilePath )
230213 if err != nil {
231- return opinit_bots. KeyFile {} , err
214+ return nil , err
232215 }
233216
234- var keyFile opinit_bots .KeyFile
235- err = json .Unmarshal (fileData , & keyFile )
217+ keyFile := & weaveio .KeyFile {}
218+ err = json .Unmarshal (fileData , keyFile )
236219 return keyFile , err
237220}
238221
239222// handleExistingOpInitHome handle the case where the opInitHome directory exists
240223func handleExistingOpInitHome (opInitHome string , botName string , force bool ) error {
241- if io .FileOrFolderExists (opInitHome ) {
224+ if weaveio .FileOrFolderExists (opInitHome ) {
242225 if force {
243226 // delete db
244227 dbPath := filepath .Join (opInitHome , fmt .Sprintf ("%s.db" , botName ))
245- if io .FileOrFolderExists (dbPath ) {
246- err := io .DeleteDirectory (dbPath )
228+ if weaveio .FileOrFolderExists (dbPath ) {
229+ err := weaveio .DeleteDirectory (dbPath )
247230 if err != nil {
248231 return fmt .Errorf ("failed to delete %s" , dbPath )
249232 }
@@ -256,7 +239,7 @@ func handleExistingOpInitHome(opInitHome string, botName string, force bool) err
256239}
257240
258241// initializeBotWithConfig initialize a bot based on the provided config
259- func initializeBotWithConfig (cmd * cobra.Command , fileData []byte , keyFile opinit_bots .KeyFile , opInitHome , userHome , botName string ) error {
242+ func initializeBotWithConfig (cmd * cobra.Command , fileData []byte , keyFile * weaveio .KeyFile , opInitHome , userHome , botName string ) error {
260243 var err error
261244
262245 switch botName {
@@ -266,14 +249,14 @@ func initializeBotWithConfig(cmd *cobra.Command, fileData []byte, keyFile opinit
266249 if err != nil {
267250 return err
268251 }
269- err = opinit_bots .InitializeExecutorWithConfig (config , & keyFile , opInitHome , userHome )
252+ err = opinit_bots .InitializeExecutorWithConfig (config , keyFile , opInitHome , userHome )
270253 case "challenger" :
271254 var config opinit_bots.ChallengerConfig
272255 err = json .Unmarshal (fileData , & config )
273256 if err != nil {
274257 return err
275258 }
276- err = opinit_bots .InitializeChallengerWithConfig (config , & keyFile , opInitHome , userHome )
259+ err = opinit_bots .InitializeChallengerWithConfig (config , keyFile , opInitHome , userHome )
277260 }
278261 if err != nil {
279262 return err
0 commit comments