From df5b6b25b8a2f1722bb67a85b66d09dd2ccc0436 Mon Sep 17 00:00:00 2001 From: Jonas Theis <mail@jonastheis.de> Date: Wed, 10 Jun 2020 09:16:51 +0200 Subject: [PATCH] Uniform usage of base58 over base64 and hex (#452) * Adjust autopeering/neighbors endpoint to display public key in default (base58) * Allow to specify node seed as base58 or base64 + add flag to show seed * Interpret entry node public key as base58 * Adjust integration tests to use base58 instead of base64 * Hex to base58 * Remove flag to print seed * Update hive.go --- config.default.json | 2 +- go.mod | 2 +- go.sum | 4 +- plugins/autopeering/autopeering.go | 4 +- plugins/autopeering/local/local.go | 18 ++++++-- plugins/autopeering/local/parameters.go | 2 +- plugins/remotelog/plugin.go | 3 +- plugins/webapi/autopeering/plugin.go | 44 ++++++++----------- plugins/webapi/info/plugin.go | 2 +- tools/docker-network/docker-compose.yml | 2 +- .../tester/framework/docker.go | 4 +- .../tester/framework/drngnetwork.go | 4 +- .../tester/framework/framework.go | 12 +++-- .../tester/framework/network.go | 16 +++---- tools/integration-tests/tester/go.mod | 2 +- tools/integration-tests/tester/go.sum | 4 +- 16 files changed, 63 insertions(+), 62 deletions(-) diff --git a/config.default.json b/config.default.json index 4c9da227..3161b48f 100644 --- a/config.default.json +++ b/config.default.json @@ -13,7 +13,7 @@ }, "autopeering": { "entryNodes": [ - "MRCJTGX9x+PPiT3um1DQSHXvALaUg/tCH/oQr6mliGo=@35.246.104.243:14626" + "4JXbv7u61TPfDkN63KK2UxDquuKsewZBMnBujTvXnmHK@35.246.104.243:14626" ], "port": 14626 }, diff --git a/go.mod b/go.mod index 84853763..b2a9251a 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/gobuffalo/packr/v2 v2.7.1 github.com/golang/protobuf v1.3.5 github.com/gorilla/websocket v1.4.1 - github.com/iotaledger/hive.go v0.0.0-20200525142347-543f24c486b8 + github.com/iotaledger/hive.go v0.0.0-20200608165853-97365812038c github.com/iotaledger/iota.go v1.0.0-beta.14 github.com/labstack/echo v3.3.10+incompatible github.com/labstack/gommon v0.3.0 diff --git a/go.sum b/go.sum index 77da261f..3a33c2d4 100644 --- a/go.sum +++ b/go.sum @@ -139,8 +139,8 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/iotaledger/hive.go v0.0.0-20200525142347-543f24c486b8 h1:QRVEyknRx6lCrHtk6BbBLcs16nQ8Ozbe4OSxV8XYAr8= -github.com/iotaledger/hive.go v0.0.0-20200525142347-543f24c486b8/go.mod h1:zwZhaE4ZeglpTrbmbwdnVPMI5XdRu2RmByi3Qn0ztmU= +github.com/iotaledger/hive.go v0.0.0-20200608165853-97365812038c h1:092HC7xRUSHpoXoAz2oTNWzfHDMMu3tEZsg2RF131hA= +github.com/iotaledger/hive.go v0.0.0-20200608165853-97365812038c/go.mod h1:zwZhaE4ZeglpTrbmbwdnVPMI5XdRu2RmByi3Qn0ztmU= github.com/iotaledger/iota.go v1.0.0-beta.9/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8= github.com/iotaledger/iota.go v1.0.0-beta.14 h1:Oeb28MfBuJEeXcGrLhTCJFtbsnc8y1u7xidsAmiOD5A= github.com/iotaledger/iota.go v1.0.0-beta.14/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8= diff --git a/plugins/autopeering/autopeering.go b/plugins/autopeering/autopeering.go index d9f60b6d..3fc0bea3 100644 --- a/plugins/autopeering/autopeering.go +++ b/plugins/autopeering/autopeering.go @@ -1,7 +1,6 @@ package autopeering import ( - "encoding/base64" "errors" "fmt" "hash/fnv" @@ -21,6 +20,7 @@ import ( "github.com/iotaledger/hive.go/crypto/ed25519" "github.com/iotaledger/hive.go/identity" "github.com/iotaledger/hive.go/logger" + "github.com/mr-tron/base58" ) // autopeering constants @@ -181,7 +181,7 @@ func parseEntryNodes() (result []*peer.Peer, err error) { if len(parts) != 2 { return nil, fmt.Errorf("%w: master node parts must be 2, is %d", ErrParsingMasterNode, len(parts)) } - pubKey, err := base64.StdEncoding.DecodeString(parts[0]) + pubKey, err := base58.Decode(parts[0]) if err != nil { return nil, fmt.Errorf("%w: invalid public key: %s", ErrParsingMasterNode, err) } diff --git a/plugins/autopeering/local/local.go b/plugins/autopeering/local/local.go index 040dcc0f..b2a211c8 100644 --- a/plugins/autopeering/local/local.go +++ b/plugins/autopeering/local/local.go @@ -1,8 +1,8 @@ package local import ( - "crypto/ed25519" "encoding/base64" + "fmt" "net" "strings" "sync" @@ -12,7 +12,9 @@ import ( "github.com/iotaledger/goshimmer/plugins/database" "github.com/iotaledger/hive.go/autopeering/peer" "github.com/iotaledger/hive.go/autopeering/peer/service" + "github.com/iotaledger/hive.go/crypto/ed25519" "github.com/iotaledger/hive.go/logger" + "github.com/mr-tron/base58" ) var ( @@ -49,7 +51,17 @@ func configureLocal() *peer.Local { // set the private key from the seed provided in the config var seed [][]byte if str := config.Node.GetString(CfgSeed); str != "" { - bytes, err := base64.StdEncoding.DecodeString(str) + var bytes []byte + var err error + + if strings.HasPrefix(str, "base58:") { + bytes, err = base58.Decode(str[7:]) + } else if strings.HasPrefix(str, "base64:") { + bytes, err = base64.StdEncoding.DecodeString(str[7:]) + } else { + err = fmt.Errorf("neither base58 nor base64 prefix provided") + } + if err != nil { log.Fatalf("Invalid %s: %s", CfgSeed, err) } @@ -65,7 +77,7 @@ func configureLocal() *peer.Local { // the private key seed of the current local can be returned the following way: // key, _ := peerDB.LocalPrivateKey() - // fmt.Println(base64.StdEncoding.EncodeToString(ed25519.PrivateKey(key).Seed())) + // fmt.Printf("Seed: base58:%s\n", key.Seed().String()) local, err := peer.NewLocal(peeringIP, services, peerDB, seed...) if err != nil { diff --git a/plugins/autopeering/local/parameters.go b/plugins/autopeering/local/parameters.go index b04ae156..6fa316d7 100644 --- a/plugins/autopeering/local/parameters.go +++ b/plugins/autopeering/local/parameters.go @@ -19,5 +19,5 @@ func init() { flag.String(CfgBind, "0.0.0.0", "bind address for global services such as autopeering and gossip") flag.String(CfgExternal, "auto", "external IP address under which the node is reachable; or 'auto' to determine it automatically") flag.Int(CfgPort, 14626, "UDP port for incoming peering requests") - flag.BytesBase64(CfgSeed, nil, "private key seed used to derive the node identity; optional Base64 encoded 256-bit string") + flag.String(CfgSeed, "", "private key seed used to derive the node identity; optional base58 or base64 encoded 256-bit string. Prefix with 'base58:' or 'base64', respectively") } diff --git a/plugins/remotelog/plugin.go b/plugins/remotelog/plugin.go index fadc136e..32a50778 100644 --- a/plugins/remotelog/plugin.go +++ b/plugins/remotelog/plugin.go @@ -5,7 +5,6 @@ package remotelog import ( - "encoding/hex" "encoding/json" "fmt" "net" @@ -78,7 +77,7 @@ func configure(plugin *node.Plugin) { conn = c if local.GetInstance() != nil { - myID = hex.EncodeToString(local.GetInstance().ID().Bytes()) + myID = local.GetInstance().ID().String() } getGitInfo() diff --git a/plugins/webapi/autopeering/plugin.go b/plugins/webapi/autopeering/plugin.go index d8647fbd..0267831b 100644 --- a/plugins/webapi/autopeering/plugin.go +++ b/plugins/webapi/autopeering/plugin.go @@ -1,7 +1,6 @@ package autopeering import ( - "encoding/base64" "net" "net/http" "strconv" @@ -27,41 +26,36 @@ func configure(plugin *node.Plugin) { // getNeighbors returns the chosen and accepted neighbors of the node func getNeighbors(c echo.Context) error { - chosen := []Neighbor{} - accepted := []Neighbor{} - knownPeers := []Neighbor{} + var chosen []Neighbor + var accepted []Neighbor + var knownPeers []Neighbor if c.QueryParam("known") == "1" { - for _, peer := range autopeering.Discovery().GetVerifiedPeers() { - n := Neighbor{ - ID: peer.ID().String(), - PublicKey: base64.StdEncoding.EncodeToString(peer.PublicKey().Bytes()), - } - n.Services = getServices(peer) - knownPeers = append(knownPeers, n) + for _, p := range autopeering.Discovery().GetVerifiedPeers() { + knownPeers = append(knownPeers, createNeighborFromPeer(p)) } } - for _, peer := range autopeering.Selection().GetOutgoingNeighbors() { - n := Neighbor{ - ID: peer.ID().String(), - PublicKey: base64.StdEncoding.EncodeToString(peer.PublicKey().Bytes()), - } - n.Services = getServices(peer) - chosen = append(chosen, n) + for _, p := range autopeering.Selection().GetOutgoingNeighbors() { + chosen = append(chosen, createNeighborFromPeer(p)) } - for _, peer := range autopeering.Selection().GetIncomingNeighbors() { - n := Neighbor{ - ID: peer.ID().String(), - PublicKey: base64.StdEncoding.EncodeToString(peer.PublicKey().Bytes()), - } - n.Services = getServices(peer) - accepted = append(accepted, n) + for _, p := range autopeering.Selection().GetIncomingNeighbors() { + accepted = append(accepted, createNeighborFromPeer(p)) } return c.JSON(http.StatusOK, Response{KnownPeers: knownPeers, Chosen: chosen, Accepted: accepted}) } +func createNeighborFromPeer(p *peer.Peer) Neighbor { + n := Neighbor{ + ID: p.ID().String(), + PublicKey: p.PublicKey().String(), + } + n.Services = getServices(p) + + return n +} + // Response contains information of the autopeering. type Response struct { KnownPeers []Neighbor `json:"known,omitempty"` diff --git a/plugins/webapi/info/plugin.go b/plugins/webapi/info/plugin.go index b6bf4eb3..5dfc6f15 100644 --- a/plugins/webapi/info/plugin.go +++ b/plugins/webapi/info/plugin.go @@ -86,7 +86,7 @@ type Response struct { Version string `json:"version,omitempty"` // whether the node is synchronized Synced bool `json:"synced"` - // identity ID of the node encoded in hex and truncated to its first 8 bytes + // identity ID of the node encoded in base58 and truncated to its first 8 bytes IdentityID string `json:"identityID,omitempty"` // public key of the node encoded in base58 PublicKey string `json:"publickey,omitempty"` diff --git a/tools/docker-network/docker-compose.yml b/tools/docker-network/docker-compose.yml index 510364e3..f275ec22 100644 --- a/tools/docker-network/docker-compose.yml +++ b/tools/docker-network/docker-compose.yml @@ -8,7 +8,7 @@ services: command: > --config-dir=/tmp --database.directory=/tmp/mainnetdb - --autopeering.seed=uuDCzsjyLNQ17/7fWKPNMYmr4IWuaVRf7qKqRL0v/6c= + --autopeering.seed=base58:8kPPCqaJFAt8BJtx6qw5PN8bKEM2XKXor6PxkmHf6bcr --autopeering.entryNodes= --analysis.server.bindAddress=0.0.0.0:1888 --analysis.dashboard.bindAddress=0.0.0.0:9000 diff --git a/tools/integration-tests/tester/framework/docker.go b/tools/integration-tests/tester/framework/docker.go index 37ab6594..a5c2cb24 100644 --- a/tools/integration-tests/tester/framework/docker.go +++ b/tools/integration-tests/tester/framework/docker.go @@ -63,7 +63,7 @@ func (d *DockerContainer) CreateGoShimmerEntryNode(name string, seed string) err "--logger.level=debug", fmt.Sprintf("--node.disablePlugins=%s", disabledPluginsEntryNode), "--autopeering.entryNodes=", - fmt.Sprintf("--autopeering.seed=%s", seed), + fmt.Sprintf("--autopeering.seed=base58:%s", seed), }, } @@ -90,7 +90,7 @@ func (d *DockerContainer) CreateGoShimmerPeer(config GoShimmerConfig) error { }()), fmt.Sprintf("--bootstrap.initialIssuance.timePeriodSec=%d", config.BootstrapInitialIssuanceTimePeriodSec), "--webapi.bindAddress=0.0.0.0:8080", - fmt.Sprintf("--autopeering.seed=%s", config.Seed), + fmt.Sprintf("--autopeering.seed=base58:%s", config.Seed), fmt.Sprintf("--autopeering.entryNodes=%s@%s:14626", config.EntryNodePublicKey, config.EntryNodeHost), fmt.Sprintf("--drng.instanceId=%d", config.DRNGInstance), fmt.Sprintf("--drng.threshold=%d", config.DRNGThreshold), diff --git a/tools/integration-tests/tester/framework/drngnetwork.go b/tools/integration-tests/tester/framework/drngnetwork.go index 4169c37e..9df42cdb 100644 --- a/tools/integration-tests/tester/framework/drngnetwork.go +++ b/tools/integration-tests/tester/framework/drngnetwork.go @@ -7,7 +7,7 @@ import ( "time" "github.com/docker/docker/client" - hive_ed25519 "github.com/iotaledger/hive.go/crypto/ed25519" + "github.com/iotaledger/hive.go/crypto/ed25519" "github.com/iotaledger/hive.go/identity" ) @@ -31,7 +31,7 @@ func newDRNGNetwork(dockerClient *client.Client, name string, tester *DockerCont } // CreatePeer creates a new peer/GoShimmer node in the network and returns it. -func (n *DRNGNetwork) CreatePeer(c GoShimmerConfig, publicKey hive_ed25519.PublicKey) (*Peer, error) { +func (n *DRNGNetwork) CreatePeer(c GoShimmerConfig, publicKey ed25519.PublicKey) (*Peer, error) { name := n.network.namePrefix(fmt.Sprintf("%s%d", containerNameReplica, len(n.network.peers))) config := c diff --git a/tools/integration-tests/tester/framework/framework.go b/tools/integration-tests/tester/framework/framework.go index 55f82286..449c2dc6 100644 --- a/tools/integration-tests/tester/framework/framework.go +++ b/tools/integration-tests/tester/framework/framework.go @@ -4,8 +4,6 @@ package framework import ( - "crypto/ed25519" - "encoding/base64" "encoding/hex" "fmt" "strings" @@ -14,7 +12,7 @@ import ( "github.com/docker/docker/api/types/strslice" "github.com/docker/docker/client" - hive_ed25519 "github.com/iotaledger/hive.go/crypto/ed25519" + "github.com/iotaledger/hive.go/crypto/ed25519" ) var ( @@ -210,12 +208,12 @@ func (f *Framework) CreateDRNGNetwork(name string, members, peers, minimumNeighb } // create GoShimmer identities - pubKeys := make([]hive_ed25519.PublicKey, peers) - privKeys := make([]hive_ed25519.PrivateKey, peers) + pubKeys := make([]ed25519.PublicKey, peers) + privKeys := make([]ed25519.PrivateKey, peers) var drngCommittee string for i := 0; i < peers; i++ { - pubKeys[i], privKeys[i], err = hive_ed25519.GenerateKey() + pubKeys[i], privKeys[i], err = ed25519.GenerateKey() if err != nil { return nil, err } @@ -238,7 +236,7 @@ func (f *Framework) CreateDRNGNetwork(name string, members, peers, minimumNeighb // create peers/GoShimmer nodes for i := 0; i < peers; i++ { config.Bootstrap = i == 0 - config.Seed = base64.StdEncoding.EncodeToString(ed25519.PrivateKey(privKeys[i].Bytes()).Seed()) + config.Seed = privKeys[i].Seed().String() if _, err = drng.CreatePeer(config, pubKeys[i]); err != nil { return nil, err } diff --git a/tools/integration-tests/tester/framework/network.go b/tools/integration-tests/tester/framework/network.go index f8efc979..c115fcb5 100644 --- a/tools/integration-tests/tester/framework/network.go +++ b/tools/integration-tests/tester/framework/network.go @@ -2,8 +2,6 @@ package framework import ( "context" - "crypto/ed25519" - "encoding/base64" "fmt" "log" "math/rand" @@ -11,7 +9,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/client" - hive_ed25519 "github.com/iotaledger/hive.go/crypto/ed25519" + "github.com/iotaledger/hive.go/crypto/ed25519" "github.com/iotaledger/hive.go/identity" ) @@ -57,13 +55,13 @@ func newNetwork(dockerClient *client.Client, name string, tester *DockerContaine // createEntryNode creates the network's entry node. func (n *Network) createEntryNode() error { // create identity - publicKey, privateKey, err := hive_ed25519.GenerateKey() + publicKey, privateKey, err := ed25519.GenerateKey() if err != nil { return err } n.entryNodeIdentity = identity.New(publicKey) - seed := base64.StdEncoding.EncodeToString(ed25519.PrivateKey(privateKey.Bytes()).Seed()) + seed := privateKey.Seed().String() // create entry node container n.entryNode = NewDockerContainer(n.dockerClient) @@ -89,11 +87,11 @@ func (n *Network) CreatePeer(c GoShimmerConfig) (*Peer, error) { name := n.namePrefix(fmt.Sprintf("%s%d", containerNameReplica, len(n.peers))) // create identity - publicKey, privateKey, err := hive_ed25519.GenerateKey() + publicKey, privateKey, err := ed25519.GenerateKey() if err != nil { return nil, err } - seed := base64.StdEncoding.EncodeToString(ed25519.PrivateKey(privateKey.Bytes()).Seed()) + seed := privateKey.Seed().String() config := c config.Name = name @@ -260,9 +258,9 @@ func (n *Network) namePrefix(suffix string) string { return fmt.Sprintf("%s-%s", n.name, suffix) } -// entryNodePublicKey returns the entry node's public key encoded as base64 +// entryNodePublicKey returns the entry node's public key encoded as base58 func (n *Network) entryNodePublicKey() string { - return base64.StdEncoding.EncodeToString(n.entryNodeIdentity.PublicKey().Bytes()) + return n.entryNodeIdentity.PublicKey().String() } // Peers returns all available peers in the network. diff --git a/tools/integration-tests/tester/go.mod b/tools/integration-tests/tester/go.mod index a9a1a0e3..f7331d45 100644 --- a/tools/integration-tests/tester/go.mod +++ b/tools/integration-tests/tester/go.mod @@ -10,7 +10,7 @@ require ( github.com/docker/go-units v0.4.0 // indirect github.com/drand/drand v0.8.1 github.com/iotaledger/goshimmer v0.1.3 - github.com/iotaledger/hive.go v0.0.0-20200525142347-543f24c486b8 + github.com/iotaledger/hive.go v0.0.0-20200608165853-97365812038c github.com/opencontainers/go-digest v1.0.0-rc1 // indirect github.com/stretchr/testify v1.5.1 ) diff --git a/tools/integration-tests/tester/go.sum b/tools/integration-tests/tester/go.sum index 22147549..aadb3c43 100644 --- a/tools/integration-tests/tester/go.sum +++ b/tools/integration-tests/tester/go.sum @@ -138,8 +138,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/iotaledger/hive.go v0.0.0-20200525142347-543f24c486b8 h1:QRVEyknRx6lCrHtk6BbBLcs16nQ8Ozbe4OSxV8XYAr8= -github.com/iotaledger/hive.go v0.0.0-20200525142347-543f24c486b8/go.mod h1:zwZhaE4ZeglpTrbmbwdnVPMI5XdRu2RmByi3Qn0ztmU= +github.com/iotaledger/hive.go v0.0.0-20200608165853-97365812038c h1:092HC7xRUSHpoXoAz2oTNWzfHDMMu3tEZsg2RF131hA= +github.com/iotaledger/hive.go v0.0.0-20200608165853-97365812038c/go.mod h1:zwZhaE4ZeglpTrbmbwdnVPMI5XdRu2RmByi3Qn0ztmU= github.com/iotaledger/iota.go v1.0.0-beta.9/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8= github.com/iotaledger/iota.go v1.0.0-beta.14/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= -- GitLab