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

Commit

Permalink
Merge pull request #449 from NebulousLabs/siag-usability
Browse files Browse the repository at this point in the history
don't overwrite existing keys
  • Loading branch information
lukechampine committed Apr 20, 2015
2 parents bc05495 + 02d89fa commit af67f02
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
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
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
}
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
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

0 comments on commit af67f02

Please sign in to comment.