Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

don't overwrite existing keys #449

Merged
merged 1 commit into from
Apr 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ test-wallet: clean fmt REBUILD
# have been hit during testing and how many times each line has been hit.
coverpackages = api crypto encoding modules/consensus modules/gateway \
modules/host modules/hostdb modules/miner modules/renter \
modules/transactionpool modules/wallet siakg types
modules/transactionpool modules/wallet siag types
cover: clean REBUILD
@mkdir -p cover/modules
@for package in $(coverpackages); do \
Expand Down
17 changes: 15 additions & 2 deletions siag/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
var (
ErrCorruptedKey = errors.New("A corrupted key has been presented")
ErrInsecureAddress = errors.New("An address needs at least one required key to be secure")
ErrOverwrite = errors.New("Keys already exist with that keyname.")
ErrUnspendableAddress = errors.New("An address is unspendable if the number of required keys is greater than the total number of keys")
)

Expand Down Expand Up @@ -88,7 +89,15 @@ func generateKeys(requiredKeys int, totalKeys int, folder string, keyname string
}
}
for i, key := range keys {
err := encoding.WriteFile(filepath.Join(folder, keyname+"_Key"+strconv.Itoa(i)+FileExtension), key)
keyFilename := filepath.Join(folder, keyname+"_Key"+strconv.Itoa(i)+FileExtension)
_, err := os.Stat(keyFilename)
if !os.IsNotExist(err) {
if err != nil {
return types.UnlockConditions{}, err
}
return types.UnlockConditions{}, ErrOverwrite
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be sufficient:

// return an error if the file already exists
if _, err := os.Stat(keyFilename); err == nil {
    return types.UnlockConditions{}, ErrOverwrite
}

err = encoding.WriteFile(keyFilename, key)
if err != nil {
return types.UnlockConditions{}, err
}
Expand Down Expand Up @@ -198,7 +207,11 @@ func printKeyInfo(filename string) error {
// printKeyInfo. This structure makes testing easier.
func keyInfo(c *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Usage: siag keyinfo [filename]")
err := c.Usage()
if err != nil {
fmt.Println("Unusual error:", err)
return
}
return
}
err := printKeyInfo(args[0])
Expand Down
6 changes: 6 additions & 0 deletions siag/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func TestGenerateKeys(t *testing.T) {
if err != nil {
t.Error(err)
}

// Try to overwrite the file that was created.
_, err = generateKeys(1, 1, testDir, "genuine")
if err != ErrOverwrite {
t.Error("Expecting ErrOverwrite:", err)
}
}

// TestVerifyKeys proves the verifyKeys function.
Expand Down