2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2019-01-07 18:02:22 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2019-01-08 14:05:14 +00:00
|
|
|
|
2019-01-07 18:02:22 +00:00
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/process"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
keyGenerateCmd = &cobra.Command{
|
2019-01-24 15:41:16 +00:00
|
|
|
Use: "batch-generate",
|
2019-01-18 10:36:58 +00:00
|
|
|
Short: "generate lots of keys",
|
|
|
|
RunE: cmdKeyGenerate,
|
|
|
|
Annotations: map[string]string{"type": "setup"},
|
2019-01-07 18:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyCfg struct {
|
2019-01-23 11:36:19 +00:00
|
|
|
MinDifficulty int `help:"minimum difficulty to output" default:"30"`
|
2019-01-07 18:02:22 +00:00
|
|
|
Concurrency int `help:"worker concurrency" default:"4"`
|
|
|
|
OutputDir string `help:"output directory to place keys" default:"."`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-01-24 15:41:16 +00:00
|
|
|
rootCmd.AddCommand(keyGenerateCmd)
|
2019-01-07 18:02:22 +00:00
|
|
|
cfgstruct.Bind(keyGenerateCmd.Flags(), &keyCfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdKeyGenerate(cmd *cobra.Command, args []string) (err error) {
|
|
|
|
ctx := process.Ctx(cmd)
|
|
|
|
err = os.MkdirAll(keyCfg.OutputDir, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
counter := new(uint32)
|
|
|
|
return identity.GenerateKeys(ctx, uint16(keyCfg.MinDifficulty), keyCfg.Concurrency,
|
|
|
|
func(k *ecdsa.PrivateKey, id storj.NodeID) (done bool, err error) {
|
|
|
|
data, err := x509.MarshalECPrivateKey(k)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
difficulty, err := id.Difficulty()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
filename := fmt.Sprintf("gen-%02d-%d.key", difficulty, atomic.AddUint32(counter, 1))
|
|
|
|
fmt.Println("writing", filename)
|
|
|
|
err = ioutil.WriteFile(filepath.Join(keyCfg.OutputDir, filename), data, 0600)
|
|
|
|
return false, err
|
|
|
|
})
|
|
|
|
}
|