mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-09 22:47:24 +00:00
Generate account from seed
This commit is contained in:
@@ -4,17 +4,39 @@ import (
|
||||
"fmt"
|
||||
|
||||
acm "github.com/tendermint/tendermint/account"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
"github.com/tendermint/tendermint/wire"
|
||||
)
|
||||
|
||||
func gen_account() {
|
||||
privAccount := acm.GenPrivAccount()
|
||||
privAccountJSONBytes := wire.JSONBytes(privAccount)
|
||||
fmt.Printf(`Generated a new account!
|
||||
|
||||
seed, err := Prompt(`Enter your desired seed, or just hit <Enter> to generate a random account.
|
||||
IMPORTANT: If you don't know what a dictionary attack is, just hit Enter
|
||||
> `, "")
|
||||
if err != nil {
|
||||
Exit(Fmt("Not sure what happened: %v", err))
|
||||
}
|
||||
|
||||
if seed == "" {
|
||||
privAccount := acm.GenPrivAccount()
|
||||
privAccountJSONBytes := wire.JSONBytes(privAccount)
|
||||
fmt.Printf(`Generated a new random account!
|
||||
|
||||
%v
|
||||
|
||||
`,
|
||||
string(privAccountJSONBytes),
|
||||
)
|
||||
string(privAccountJSONBytes),
|
||||
)
|
||||
} else {
|
||||
privAccount := acm.GenPrivAccountFromSecret([]byte(seed))
|
||||
privAccountJSONBytes := wire.JSONBytes(privAccount)
|
||||
fmt.Printf(`Generated a new account from seed: [%v]!
|
||||
|
||||
%v
|
||||
|
||||
`,
|
||||
seed,
|
||||
string(privAccountJSONBytes),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
17
common/os.go
17
common/os.go
@@ -1,10 +1,12 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -206,3 +208,18 @@ func Tempfile(prefix string) (*os.File, string) {
|
||||
}
|
||||
return file, file.Name()
|
||||
}
|
||||
|
||||
func Prompt(prompt string, defaultValue string) (string, error) {
|
||||
fmt.Print(prompt)
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
return defaultValue, err
|
||||
} else {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
return defaultValue, nil
|
||||
}
|
||||
return line, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,10 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Prompt(prompt string, defaultValue string) string {
|
||||
fmt.Print(prompt)
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
log.Warn("Error reading stdin", "err", err)
|
||||
return defaultValue
|
||||
} else {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
return defaultValue
|
||||
}
|
||||
return line
|
||||
}
|
||||
}
|
||||
|
||||
type Config interface {
|
||||
Get(key string) interface{}
|
||||
GetBool(key string) bool
|
||||
|
||||
@@ -30,8 +30,8 @@ func initTMRoot(rootDir string) {
|
||||
// Write default config file if missing.
|
||||
if !FileExists(configFilePath) {
|
||||
// Ask user for moniker
|
||||
moniker := cfg.Prompt("Type hostname: ", "anonymous")
|
||||
MustWriteFile(configFilePath, []byte(defaultConfig(moniker)))
|
||||
// moniker := cfg.Prompt("Type hostname: ", "anonymous")
|
||||
MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")))
|
||||
}
|
||||
if !FileExists(genesisFilePath) {
|
||||
MustWriteFile(genesisFilePath, []byte(defaultGenesis))
|
||||
|
||||
@@ -35,8 +35,8 @@ func initTMRoot(rootDir string) {
|
||||
// Write default config file if missing.
|
||||
if !FileExists(configFilePath) {
|
||||
// Ask user for moniker
|
||||
moniker := cfg.Prompt("Type hostname: ", "anonymous")
|
||||
MustWriteFile(configFilePath, []byte(defaultConfig(moniker)))
|
||||
// moniker := cfg.Prompt("Type hostname: ", "anonymous")
|
||||
MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")))
|
||||
}
|
||||
if !FileExists(genesisFilePath) {
|
||||
MustWriteFile(genesisFilePath, []byte(defaultGenesis))
|
||||
|
||||
Reference in New Issue
Block a user