mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
whisper: refactored input/output functions
This commit is contained in:
parent
9d5cdd28a0
commit
ac273604ed
1 changed files with 21 additions and 42 deletions
|
|
@ -20,7 +20,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
|
@ -29,10 +28,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/console"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/logger"
|
"github.com/ethereum/go-ethereum/logger"
|
||||||
"github.com/ethereum/go-ethereum/logger/glog"
|
"github.com/ethereum/go-ethereum/logger/glog"
|
||||||
|
|
@ -41,10 +41,8 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/p2p/nat"
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
||||||
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
||||||
"golang.org/x/crypto/pbkdf2"
|
"golang.org/x/crypto/pbkdf2"
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var input *bufio.Reader
|
|
||||||
var done chan struct{}
|
var done chan struct{}
|
||||||
var server p2p.Server
|
var server p2p.Server
|
||||||
var shh *whisper.Whisper
|
var shh *whisper.Whisper
|
||||||
|
|
@ -81,7 +79,7 @@ var (
|
||||||
argNamePub = "-pub"
|
argNamePub = "-pub"
|
||||||
argNameEnode = "-enode"
|
argNameEnode = "-enode"
|
||||||
argNameIdSrc = "-idfile"
|
argNameIdSrc = "-idfile"
|
||||||
quitCommand = "~q"
|
quitCommand = "~Q"
|
||||||
)
|
)
|
||||||
|
|
||||||
func padRight(str string) string {
|
func padRight(str string) string {
|
||||||
|
|
@ -150,8 +148,7 @@ func parseArgs() {
|
||||||
if len(NodeIdFile) > 0 {
|
if len(NodeIdFile) > 0 {
|
||||||
nodeid, err = crypto.LoadECDSA(NodeIdFile)
|
nodeid, err = crypto.LoadECDSA(NodeIdFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to load file [%s]: %s.\n", NodeIdFile, err)
|
utils.Fatalf("Failed to load file [%s]: %s.", NodeIdFile, err)
|
||||||
os.Exit(-1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -170,8 +167,7 @@ func parseArgs() {
|
||||||
var x []byte
|
var x []byte
|
||||||
x, err := hex.DecodeString(topicStr)
|
x, err := hex.DecodeString(topicStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to parse the topic: %s \n", err)
|
utils.Fatalf("Failed to parse the topic: %s", err)
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
topic = whisper.BytesToTopic(x)
|
topic = whisper.BytesToTopic(x)
|
||||||
}
|
}
|
||||||
|
|
@ -179,8 +175,7 @@ func parseArgs() {
|
||||||
if isAsymmetric && len(pubStr) > 0 {
|
if isAsymmetric && len(pubStr) > 0 {
|
||||||
pub = crypto.ToECDSAPub(common.FromHex(pubStr))
|
pub = crypto.ToECDSAPub(common.FromHex(pubStr))
|
||||||
if !isKeyValid(pub) {
|
if !isKeyValid(pub) {
|
||||||
fmt.Println("Error: invalid public key")
|
utils.Fatalf("invalid public key")
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -209,8 +204,7 @@ func checkIntArg(pattern string, dst *uint32) {
|
||||||
s := arg[sz:]
|
s := arg[sz:]
|
||||||
i, err := strconv.ParseUint(s, 10, 0)
|
i, err := strconv.ParseUint(s, 10, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to parse argument %s: %s \n", pattern, err)
|
utils.Fatalf("Failed to parse argument %s: %s", pattern, err)
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
if err == nil && i > 0 {
|
if err == nil && i > 0 {
|
||||||
*dst = uint32(i)
|
*dst = uint32(i)
|
||||||
|
|
@ -253,7 +247,6 @@ func initialize() {
|
||||||
glog.SetToStderr(true)
|
glog.SetToStderr(true)
|
||||||
|
|
||||||
done = make(chan struct{})
|
done = make(chan struct{})
|
||||||
input = bufio.NewReader(os.Stdin)
|
|
||||||
var peers []*discover.Node
|
var peers []*discover.Node
|
||||||
|
|
||||||
if testMode {
|
if testMode {
|
||||||
|
|
@ -305,8 +298,7 @@ func initialize() {
|
||||||
func startServer() {
|
func startServer() {
|
||||||
err := server.Start()
|
err := server.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to start Whsiper peer: %s.\n", err)
|
utils.Fatalf("Failed to start Whsiper peer: %s.", err)
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("my public key: %s \n", common.ToHex(crypto.FromECDSAPub(&asymKey.PublicKey)))
|
fmt.Printf("my public key: %s \n", common.ToHex(crypto.FromECDSAPub(&asymKey.PublicKey)))
|
||||||
|
|
@ -338,33 +330,27 @@ func configureChat() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if isAsymmetric && len(pubStr) == 0 {
|
if isAsymmetric && len(pubStr) == 0 {
|
||||||
fmt.Printf("Please enter the peer's public key: ")
|
pubStr = scanLine("Please enter the peer's public key: ")
|
||||||
pubStr = scanLine()
|
|
||||||
pub = crypto.ToECDSAPub(common.FromHex(pubStr))
|
pub = crypto.ToECDSAPub(common.FromHex(pubStr))
|
||||||
if !isKeyValid(pub) {
|
if !isKeyValid(pub) {
|
||||||
fmt.Println("Error: invalid public key")
|
utils.Fatalf("Error: invalid public key")
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isAsymmetric && !testMode {
|
if !isAsymmetric && !testMode {
|
||||||
fmt.Printf("Please enter the password: ")
|
pass, err := console.Stdin.PromptPassword("Please enter the password: ")
|
||||||
pass, err := terminal.ReadPassword(int(syscall.Stdin))
|
|
||||||
fmt.Println()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: %s \n", err)
|
utils.Fatalf("Failed to read passphrase: %v", err)
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(salt) == 0 {
|
if len(salt) == 0 {
|
||||||
fmt.Printf("Please enter the salt: ")
|
salt = scanLine("Please enter the salt: ")
|
||||||
salt = scanLine()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
symKey = pbkdf2.Key(pass, []byte(salt), 65356, 32, sha256.New)
|
symKey = pbkdf2.Key([]byte(pass), []byte(salt), 65356, 32, sha256.New)
|
||||||
|
|
||||||
if len(topicStr) == 0 {
|
if len(topicStr) == 0 {
|
||||||
generateTopic(pass, []byte(salt))
|
generateTopic([]byte(pass), []byte(salt))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -393,8 +379,7 @@ func waitForConnection(timeout bool) {
|
||||||
if timeout {
|
if timeout {
|
||||||
cnt++
|
cnt++
|
||||||
if cnt > 1000 {
|
if cnt > 1000 {
|
||||||
fmt.Println("Timeout expired, failed to connect")
|
utils.Fatalf("Timeout expired, failed to connect")
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -413,7 +398,7 @@ func run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
s := scanLine()
|
s := scanLine("")
|
||||||
if s == quitCommand {
|
if s == quitCommand {
|
||||||
fmt.Println("Quit command received")
|
fmt.Println("Quit command received")
|
||||||
close(done)
|
close(done)
|
||||||
|
|
@ -431,15 +416,10 @@ func run() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanLine() string {
|
func scanLine(prompt string) string {
|
||||||
txt, err := input.ReadString('\n')
|
txt, err := console.Stdin.PromptInput(prompt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("input error: %s \n", err)
|
utils.Fatalf("input error: %s", err)
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
last := len(txt) - 1
|
|
||||||
if txt[last] == '\n' {
|
|
||||||
return txt[:last] // without the trailing newline
|
|
||||||
}
|
}
|
||||||
return txt
|
return txt
|
||||||
}
|
}
|
||||||
|
|
@ -472,8 +452,7 @@ func sendMsg(payload []byte) {
|
||||||
func messageLoop() {
|
func messageLoop() {
|
||||||
f := shh.GetFilter(filterID)
|
f := shh.GetFilter(filterID)
|
||||||
if f == nil {
|
if f == nil {
|
||||||
fmt.Println("error: filter is not installed!")
|
utils.Fatalf("filter is not installed")
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Millisecond * 50)
|
ticker := time.NewTicker(time.Millisecond * 50)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue