Skip to content
This repository was archived by the owner on May 22, 2023. It is now read-only.

Commit 379fccc

Browse files
committed
Pass file mode to output data command
We can expect file mode to be provided to outputData function instead of the boolen value. We don't need to check if the file exists before writing to it as access permissions of the file will prevent us from overwriting it if configured properly.
1 parent 3fb44f0 commit 379fccc

2 files changed

Lines changed: 7 additions & 18 deletions

File tree

cmd/signing.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func DecryptKeyShare(c *cli.Context) error {
112112
)
113113
}
114114

115-
return outputData(c, signerBytes, false)
115+
return outputData(c, signerBytes, 0444) // store to read-only file
116116
}
117117

118118
// SignDigest signs a given digest using key shares from the provided directory.
@@ -262,23 +262,12 @@ func SignDigest(c *cli.Context) error {
262262
return nil
263263
}
264264

265-
func outputData(c *cli.Context, data []byte, overwrite bool) error {
265+
// If `output-file` flag is provided stores the output in a file.
266+
// `fileMode` determines the access permission for the output file. Sample values:
267+
// 0444 - read-only for all
268+
// 0644 - readable for all, but writeable only for the user (owner)
269+
func outputData(c *cli.Context, data []byte, fileMode os.FileMode) error {
266270
if outputFilePath := c.String("output-file"); len(outputFilePath) > 0 {
267-
var fileMode os.FileMode
268-
269-
if !overwrite {
270-
if _, err := os.Stat(outputFilePath); !os.IsNotExist(err) {
271-
return fmt.Errorf(
272-
"could not write output to a file; file [%s] already exists",
273-
outputFilePath,
274-
)
275-
}
276-
277-
fileMode = 0444 // read-only
278-
} else {
279-
fileMode = 0644 // user writable
280-
}
281-
282271
err := ioutil.WriteFile(outputFilePath, data, fileMode)
283272
if err != nil {
284273
return fmt.Errorf(

cmd/signing_ethereum.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func EthereumSign(c *cli.Context) error {
153153
return fmt.Errorf("failed to marshal ethereum signature: [%v]", err)
154154
}
155155

156-
return outputData(c, marshaledSignature, true)
156+
return outputData(c, marshaledSignature, 0644) // store to user writeable file
157157
}
158158

159159
// EthereumVerify verifies if a signature was calculated by a signer with the

0 commit comments

Comments
 (0)