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

cmd/swarm: make bzzaccount flag optional and add bzzkeyhex flag #1531

Merged
merged 12 commits into from
Jul 4, 2019
Merged
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ FROM alpine:3.9
RUN apk --no-cache add ca-certificates && update-ca-certificates
COPY --from=builder /swarm/build/bin/swarm /usr/local/bin/
COPY --from=geth /usr/local/bin/geth /usr/local/bin/
COPY docker/run.sh /run.sh
ENTRYPOINT ["/run.sh"]
ENTRYPOINT ["/usr/local/bin/swarm"]
3 changes: 1 addition & 2 deletions Dockerfile.alltools
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ FROM alpine:3.9
RUN apk --no-cache add ca-certificates
COPY --from=builder /swarm/build/bin/* /usr/local/bin/
COPY --from=geth /usr/local/bin/geth /usr/local/bin/
COPY docker/run.sh /run.sh
COPY docker/run-smoke.sh /run-smoke.sh
ENTRYPOINT ["/run.sh"]
ENTRYPOINT ["/usr/local/bin/swarm"]
2 changes: 1 addition & 1 deletion cmd/swarm/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func accessNewPass(ctx *cli.Context) {
accessKey []byte
err error
ref = args[0]
password = getPassPhrase("", 0, makePasswordList(ctx))
password = getPassPhrase("", false, 0, makePasswordList(ctx))
dryRun = ctx.Bool(SwarmDryRunFlag.Name)
)
accessKey, ae, err = api.DoPassword(ctx, password, salt)
Expand Down
4 changes: 3 additions & 1 deletion cmd/swarm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var (
const (
SwarmEnvChequebookAddr = "SWARM_CHEQUEBOOK_ADDR"
SwarmEnvAccount = "SWARM_ACCOUNT"
SwarmEnvBzzKeyHex = "SWARM_BZZ_KEY_HEX"
SwarmEnvListenAddr = "SWARM_LISTEN_ADDR"
SwarmEnvPort = "SWARM_PORT"
SwarmEnvNetworkID = "SWARM_NETWORK_ID"
Expand Down Expand Up @@ -126,7 +127,8 @@ func buildConfig(ctx *cli.Context) (config *bzzapi.Config, err error) {
func initSwarmNode(config *bzzapi.Config, stack *node.Node, ctx *cli.Context, nodeconfig *node.Config) error {
//at this point, all vars should be set in the Config
//get the account for the provided swarm account
prvkey := getAccount(config.BzzAccount, ctx, stack)
bzzaccount, prvkey := getAccount(config.BzzAccount, ctx, stack)
config.BzzAccount = bzzaccount
skylenet marked this conversation as resolved.
Show resolved Hide resolved
//set the resolved config path (geth --datadir)
config.Path = expandPath(stack.InstanceDir())
//finally, initialize the configuration
Expand Down
162 changes: 157 additions & 5 deletions cmd/swarm/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
Expand All @@ -28,6 +29,8 @@ import (

"github.com/docker/docker/pkg/reexec"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethersphere/swarm"
"github.com/ethersphere/swarm/api"
Expand All @@ -48,6 +51,7 @@ func TestConfigFailsSwapEnabledNoSwapApi(t *testing.T) {
flags := []string{
fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545",
fmt.Sprintf("--%s", utils.ListenPortFlag.Name), "0",
fmt.Sprintf("--%s", SwarmSwapEnabledFlag.Name),
}

Expand All @@ -56,15 +60,162 @@ func TestConfigFailsSwapEnabledNoSwapApi(t *testing.T) {
swarm.ExpectExit()
}

func TestConfigFailsNoBzzAccount(t *testing.T) {
func TestBzzKeyFlag(t *testing.T) {
key, err := crypto.GenerateKey()
if err != nil {
t.Fatal("unable to generate key")
}
hexKey := hex.EncodeToString(crypto.FromECDSA(key))
bzzaccount := crypto.PubkeyToAddress(key.PublicKey).Hex()

dir, err := ioutil.TempDir("", "bzztest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

conf := &node.Config{
DataDir: dir,
IPCPath: fmt.Sprintf("bzzd-%s.ipc", bzzaccount),
NoUSB: true,
}

node := &testNode{Dir: dir}

flags := []string{
fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545",
fmt.Sprintf("--%s", SwarmPortFlag.Name), "0",
fmt.Sprintf("--%s", utils.ListenPortFlag.Name), "0",
fmt.Sprintf("--%s", utils.DataDirFlag.Name), dir,
fmt.Sprintf("--%s", utils.IPCPathFlag.Name), conf.IPCPath,
fmt.Sprintf("--%s", SwarmBzzKeyHexFlag.Name), hexKey,
}

swarm := runSwarm(t, flags...)
swarm.Expect("Fatal: " + SwarmErrNoBZZAccount + "\n")
swarm.ExpectExit()
node.Cmd = runSwarm(t, flags...)
defer func() {
node.Shutdown()
}()

node.Cmd.InputLine(testPassphrase)

// wait for the node to start
for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
node.Client, err = rpc.Dial(conf.IPCEndpoint())
if err == nil {
break
}
}
if node.Client == nil {
t.Fatal(err)
}

// load info
var info swarm.Info
if err := node.Client.Call(&info, "bzz_info"); err != nil {
t.Fatal(err)
}

if info.BzzAccount != bzzaccount {
t.Fatalf("Expected account to be %s, got %s", bzzaccount, info.BzzAccount)
}

skylenet marked this conversation as resolved.
Show resolved Hide resolved
}
func TestEmptyBzzAccountFlagMultipleAccounts(t *testing.T) {
dir, err := ioutil.TempDir("", "bzztest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

// Create two accounts on disk
getTestAccount(t, dir)
getTestAccount(t, dir)

node := &testNode{Dir: dir}

flags := []string{
fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
fmt.Sprintf("--%s", SwarmPortFlag.Name), "0",
fmt.Sprintf("--%s", utils.ListenPortFlag.Name), "0",
fmt.Sprintf("--%s", utils.DataDirFlag.Name), dir,
}

node.Cmd = runSwarm(t, flags...)

node.Cmd.ExpectRegexp(fmt.Sprintf("Please choose one of the accounts by running swarm with the --%s flag.", SwarmAccountFlag.Name))
}

func TestEmptyBzzAccountFlagSingleAccount(t *testing.T) {
dir, err := ioutil.TempDir("", "bzztest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

conf, account := getTestAccount(t, dir)
node := &testNode{Dir: dir}

flags := []string{
fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
fmt.Sprintf("--%s", SwarmPortFlag.Name), "0",
fmt.Sprintf("--%s", utils.ListenPortFlag.Name), "0",
fmt.Sprintf("--%s", utils.DataDirFlag.Name), dir,
fmt.Sprintf("--%s", utils.IPCPathFlag.Name), conf.IPCPath,
}

node.Cmd = runSwarm(t, flags...)
defer func() {
node.Shutdown()
}()

node.Cmd.InputLine(testPassphrase)

// wait for the node to start
for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
node.Client, err = rpc.Dial(conf.IPCEndpoint())
if err == nil {
break
}
}
if node.Client == nil {
t.Fatal(err)
}

// load info
var info swarm.Info
if err := node.Client.Call(&info, "bzz_info"); err != nil {
t.Fatal(err)
}

if info.BzzAccount != account.Address.Hex() {
t.Fatalf("Expected account to be %s, got %s", account.Address.Hex(), info.BzzAccount)
}
}

func TestEmptyBzzAccountFlagNoAccountWrongPassword(t *testing.T) {
dir, err := ioutil.TempDir("", "bzztest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

node := &testNode{Dir: dir}

flags := []string{
fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
fmt.Sprintf("--%s", SwarmPortFlag.Name), "0",
fmt.Sprintf("--%s", utils.ListenPortFlag.Name), "0",
fmt.Sprintf("--%s", utils.DataDirFlag.Name), dir,
}

node.Cmd = runSwarm(t, flags...)

// Set password
node.Cmd.InputLine("goodpassword")
// Confirm password
node.Cmd.InputLine("wrongpassword")

node.Cmd.ExpectRegexp("Passphrases do not match")
}

func TestConfigCmdLineOverrides(t *testing.T) {
Expand All @@ -86,6 +237,7 @@ func TestConfigCmdLineOverrides(t *testing.T) {
flags := []string{
fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
fmt.Sprintf("--%s", SwarmPortFlag.Name), httpPort,
fmt.Sprintf("--%s", utils.ListenPortFlag.Name), "0",
fmt.Sprintf("--%s", SwarmSyncDisabledFlag.Name),
fmt.Sprintf("--%s", CorsStringFlag.Name), "*",
fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
Expand Down
2 changes: 1 addition & 1 deletion cmd/swarm/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func download(ctx *cli.Context) {
return nil
}
if passwords := makePasswordList(ctx); passwords != nil {
password := getPassPhrase(fmt.Sprintf("Downloading %s is restricted", uri), 0, passwords)
password := getPassPhrase(fmt.Sprintf("Downloading %s is restricted", uri), false, 0, passwords)
err = dl(password)
} else {
err = dl("")
Expand Down
5 changes: 5 additions & 0 deletions cmd/swarm/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ var (
Usage: "Swarm account key file",
EnvVar: SwarmEnvAccount,
}
SwarmBzzKeyHexFlag = cli.StringFlag{
Name: "bzzkeyhex",
Usage: "BzzAccount key in hex (for testing)",
EnvVar: SwarmEnvBzzKeyHex,
}
SwarmListenAddrFlag = cli.StringFlag{
Name: "httpaddr",
Usage: "Swarm HTTP API listening interface",
Expand Down
Loading