1
- package cli
1
+ package state
2
2
3
3
import (
4
4
"encoding/hex"
@@ -10,28 +10,33 @@ import (
10
10
"github.com/randa-mu/ssv-dkg/shared/api"
11
11
)
12
12
13
+ type StoredState struct {
14
+ OwnerConfig api.OwnerConfig
15
+ SigningOutput api.SigningOutput
16
+ }
17
+
13
18
func CreateFilename (stateDirectory string , output api.SigningOutput ) string {
14
19
return path .Join (stateDirectory , fmt .Sprintf ("%s.json" , hex .EncodeToString (output .SessionID )))
15
20
}
16
21
17
- // StoreState stores the JSON encoded `api.SigningOutput ` in a flat file.
22
+ // StoreState stores the JSON encoded `StoredState ` in a flat file.
18
23
// it will overwrite any file that is presently there
19
24
// it returns the json bytes on file write failure, so they can be printed to console
20
25
// so users don't just lose their DKG state completely if e.g. they write somewhere without perms
21
- func StoreState (filepath string , output api. SigningOutput ) ([]byte , error ) {
22
- return storeWithFlags (filepath , output , os .O_WRONLY )
26
+ func StoreState (filepath string , state StoredState ) ([]byte , error ) {
27
+ return storeWithFlags (filepath , state , os .O_WRONLY )
23
28
}
24
29
25
- // StoreStateIfNotExists stores the JSON encoded `api.SigningOutput ` in a flat file.
30
+ // StoreStateIfNotExists stores the JSON encoded `StoredState ` in a flat file.
26
31
// it will fail if a file with the given name already exists
27
32
// it returns the json bytes on file write failure, so they can be printed to console
28
33
// so users don't just lose their DKG state completely if e.g. they write somewhere without perms
29
- func StoreStateIfNotExists (filepath string , output api. SigningOutput ) ([]byte , error ) {
30
- return storeWithFlags (filepath , output , os .O_WRONLY | os .O_CREATE | os .O_EXCL )
34
+ func StoreStateIfNotExists (filepath string , state StoredState ) ([]byte , error ) {
35
+ return storeWithFlags (filepath , state , os .O_WRONLY | os .O_CREATE | os .O_EXCL )
31
36
}
32
37
33
- func storeWithFlags (filepath string , output api. SigningOutput , flag int ) ([]byte , error ) {
34
- bytes , err := json .Marshal (output )
38
+ func storeWithFlags (filepath string , state StoredState , flag int ) ([]byte , error ) {
39
+ bytes , err := json .Marshal (state )
35
40
if err != nil {
36
41
return nil , err
37
42
}
@@ -45,14 +50,14 @@ func storeWithFlags(filepath string, output api.SigningOutput, flag int) ([]byte
45
50
return bytes , err
46
51
}
47
52
48
- // LoadState loads and unmarshalls the JSON encoded `api.SigningOutput ` from a flat file.
49
- func LoadState (filepath string ) (api. SigningOutput , error ) {
53
+ // LoadState loads and unmarshals the JSON encoded `StoredState ` from a flat file.
54
+ func LoadState (filepath string ) (StoredState , error ) {
50
55
bytes , err := os .ReadFile (filepath )
51
56
if err != nil {
52
- return api. SigningOutput {}, err
57
+ return StoredState {}, err
53
58
}
54
59
55
- var s api. SigningOutput
60
+ var s StoredState
56
61
err = json .Unmarshal (bytes , & s )
57
62
return s , err
58
63
}
0 commit comments