Commit c3ec18a0 authored by Janoš Guljaš's avatar Janoš Guljaš Committed by GitHub

construct overlay address with network id (#190)

parent 3f97ad93
...@@ -99,7 +99,7 @@ func (c *command) initStartCmd() (err error) { ...@@ -99,7 +99,7 @@ func (c *command) initStartCmd() (err error) {
Addr: c.config.GetString(optionNameP2PAddr), Addr: c.config.GetString(optionNameP2PAddr),
DisableWS: c.config.GetBool(optionNameP2PDisableWS), DisableWS: c.config.GetBool(optionNameP2PDisableWS),
DisableQUIC: c.config.GetBool(optionNameP2PDisableQUIC), DisableQUIC: c.config.GetBool(optionNameP2PDisableQUIC),
NetworkID: c.config.GetInt32(optionNameNetworkID), NetworkID: c.config.GetUint64(optionNameNetworkID),
Bootnodes: c.config.GetStringSlice(optionNameBootnodes), Bootnodes: c.config.GetStringSlice(optionNameBootnodes),
TracingEnabled: c.config.GetBool(optionNameTracingEnabled), TracingEnabled: c.config.GetBool(optionNameTracingEnabled),
TracingEndpoint: c.config.GetString(optionNameTracingEndpoint), TracingEndpoint: c.config.GetString(optionNameTracingEndpoint),
...@@ -159,7 +159,7 @@ func (c *command) initStartCmd() (err error) { ...@@ -159,7 +159,7 @@ func (c *command) initStartCmd() (err error) {
cmd.Flags().StringSlice(optionNameBootnodes, nil, "initial nodes to connect to") cmd.Flags().StringSlice(optionNameBootnodes, nil, "initial nodes to connect to")
cmd.Flags().Bool(optionNameEnableDebugAPI, false, "enable debug HTTP API") cmd.Flags().Bool(optionNameEnableDebugAPI, false, "enable debug HTTP API")
cmd.Flags().String(optionNameDebugAPIAddr, ":6060", "debug HTTP API listen address") cmd.Flags().String(optionNameDebugAPIAddr, ":6060", "debug HTTP API listen address")
cmd.Flags().Int32(optionNameNetworkID, 1, "ID of the Swarm network") cmd.Flags().Uint64(optionNameNetworkID, 1, "ID of the Swarm network")
cmd.Flags().Bool(optionNameTracingEnabled, false, "enable tracing") cmd.Flags().Bool(optionNameTracingEnabled, false, "enable tracing")
cmd.Flags().String(optionNameTracingEndpoint, "127.0.0.1:6831", "endpoint to send tracing data") cmd.Flags().String(optionNameTracingEndpoint, "127.0.0.1:6831", "endpoint to send tracing data")
cmd.Flags().String(optionNameTracingServiceName, "bee", "service name identifier for tracing") cmd.Flags().String(optionNameTracingServiceName, "bee", "service name identifier for tracing")
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
"encoding/binary"
"fmt" "fmt"
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
...@@ -15,9 +16,12 @@ import ( ...@@ -15,9 +16,12 @@ import (
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )
// NewAddress constructs a Swarm Address from ECDSA private key. // NewOverlayAddress constructs a Swarm Address from ECDSA private key.
func NewAddress(p ecdsa.PublicKey) swarm.Address { func NewOverlayAddress(p ecdsa.PublicKey, networkID uint64) swarm.Address {
h := sha3.Sum256(elliptic.Marshal(btcec.S256(), p.X, p.Y)) data := make([]byte, 28)
copy(data, elliptic.Marshal(btcec.S256(), p.X, p.Y)[12:32])
binary.LittleEndian.PutUint64(data[20:28], networkID)
h := sha3.Sum256(data)
return swarm.NewAddress(h[:]) return swarm.NewAddress(h[:])
} }
......
...@@ -37,7 +37,7 @@ func TestNewAddress(t *testing.T) { ...@@ -37,7 +37,7 @@ func TestNewAddress(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
a := crypto.NewAddress(k.PublicKey) a := crypto.NewOverlayAddress(k.PublicKey, 1)
if l := len(a.Bytes()); l != 32 { if l := len(a.Bytes()); l != 32 {
t.Errorf("got address length %v, want %v", l, 32) t.Errorf("got address length %v, want %v", l, 32)
} }
......
...@@ -17,7 +17,6 @@ import ( ...@@ -17,7 +17,6 @@ import (
"github.com/ethersphere/bee/pkg/crypto" "github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/keystore" "github.com/ethersphere/bee/pkg/keystore"
"github.com/ethersphere/bee/pkg/swarm"
"golang.org/x/crypto/scrypt" "golang.org/x/crypto/scrypt"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
) )
...@@ -35,9 +34,8 @@ const ( ...@@ -35,9 +34,8 @@ const (
) )
type encryptedKey struct { type encryptedKey struct {
Address swarm.Address `json:"address"` Crypto keyCripto `json:"crypto"`
Crypto keyCripto `json:"crypto"` Version int `json:"version"`
Version int `json:"version"`
} }
type keyCripto struct { type keyCripto struct {
...@@ -68,7 +66,6 @@ func encryptKey(k *ecdsa.PrivateKey, password string) ([]byte, error) { ...@@ -68,7 +66,6 @@ func encryptKey(k *ecdsa.PrivateKey, password string) ([]byte, error) {
return nil, err return nil, err
} }
return json.Marshal(encryptedKey{ return json.Marshal(encryptedKey{
Address: crypto.NewAddress(k.PublicKey),
Crypto: *kc, Crypto: *kc,
Version: keyVersion, Version: keyVersion,
}) })
......
...@@ -65,7 +65,7 @@ type Options struct { ...@@ -65,7 +65,7 @@ type Options struct {
Addr string Addr string
DisableWS bool DisableWS bool
DisableQUIC bool DisableQUIC bool
NetworkID int32 NetworkID uint64
Bootnodes []string Bootnodes []string
Logger logging.Logger Logger logging.Logger
TracingEnabled bool TracingEnabled bool
...@@ -105,7 +105,7 @@ func NewBee(o Options) (*Bee, error) { ...@@ -105,7 +105,7 @@ func NewBee(o Options) (*Bee, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("swarm key: %w", err) return nil, fmt.Errorf("swarm key: %w", err)
} }
address := crypto.NewAddress(swarmPrivateKey.PublicKey) address := crypto.NewOverlayAddress(swarmPrivateKey.PublicKey, o.NetworkID)
if created { if created {
logger.Info("new swarm key created") logger.Info("new swarm key created")
} }
......
...@@ -42,7 +42,7 @@ type PeerFinder interface { ...@@ -42,7 +42,7 @@ type PeerFinder interface {
type Service struct { type Service struct {
overlay swarm.Address overlay swarm.Address
networkID int32 networkID uint64
receivedHandshakes map[libp2ppeer.ID]struct{} receivedHandshakes map[libp2ppeer.ID]struct{}
receivedHandshakesMu sync.Mutex receivedHandshakesMu sync.Mutex
logger logging.Logger logger logging.Logger
...@@ -50,7 +50,7 @@ type Service struct { ...@@ -50,7 +50,7 @@ type Service struct {
network.Notifiee // handhsake service can be the receiver for network.Notify network.Notifiee // handhsake service can be the receiver for network.Notify
} }
func New(overlay swarm.Address, networkID int32, logger logging.Logger) *Service { func New(overlay swarm.Address, networkID uint64, logger logging.Logger) *Service {
return &Service{ return &Service{
overlay: overlay, overlay: overlay,
networkID: networkID, networkID: networkID,
...@@ -146,6 +146,6 @@ func (s *Service) Disconnected(_ network.Network, c network.Conn) { ...@@ -146,6 +146,6 @@ func (s *Service) Disconnected(_ network.Network, c network.Conn) {
type Info struct { type Info struct {
Address swarm.Address Address swarm.Address
NetworkID int32 NetworkID uint64
Light bool Light bool
} }
...@@ -24,7 +24,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package ...@@ -24,7 +24,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
type Syn struct { type Syn struct {
Address []byte `protobuf:"bytes,1,opt,name=Address,proto3" json:"Address,omitempty"` Address []byte `protobuf:"bytes,1,opt,name=Address,proto3" json:"Address,omitempty"`
NetworkID int32 `protobuf:"varint,2,opt,name=NetworkID,proto3" json:"NetworkID,omitempty"` NetworkID uint64 `protobuf:"varint,2,opt,name=NetworkID,proto3" json:"NetworkID,omitempty"`
Light bool `protobuf:"varint,3,opt,name=Light,proto3" json:"Light,omitempty"` Light bool `protobuf:"varint,3,opt,name=Light,proto3" json:"Light,omitempty"`
} }
...@@ -68,7 +68,7 @@ func (m *Syn) GetAddress() []byte { ...@@ -68,7 +68,7 @@ func (m *Syn) GetAddress() []byte {
return nil return nil
} }
func (m *Syn) GetNetworkID() int32 { func (m *Syn) GetNetworkID() uint64 {
if m != nil { if m != nil {
return m.NetworkID return m.NetworkID
} }
...@@ -192,15 +192,15 @@ var fileDescriptor_a77305914d5d202f = []byte{ ...@@ -192,15 +192,15 @@ var fileDescriptor_a77305914d5d202f = []byte{
0x29, 0xce, 0x48, 0xcc, 0x4e, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x84, 0x0b, 0x28, 0x29, 0xce, 0x48, 0xcc, 0x4e, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x84, 0x0b, 0x28,
0x05, 0x73, 0x31, 0x07, 0x57, 0xe6, 0x09, 0x49, 0x70, 0xb1, 0x3b, 0xa6, 0xa4, 0x14, 0xa5, 0x16, 0x05, 0x73, 0x31, 0x07, 0x57, 0xe6, 0x09, 0x49, 0x70, 0xb1, 0x3b, 0xa6, 0xa4, 0x14, 0xa5, 0x16,
0x17, 0x4b, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x04, 0xc1, 0xb8, 0x42, 0x32, 0x5c, 0x9c, 0x7e, 0xa9, 0x17, 0x4b, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x04, 0xc1, 0xb8, 0x42, 0x32, 0x5c, 0x9c, 0x7e, 0xa9,
0x25, 0xe5, 0xf9, 0x45, 0xd9, 0x9e, 0x2e, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0xac, 0x41, 0x08, 0x01, 0x25, 0xe5, 0xf9, 0x45, 0xd9, 0x9e, 0x2e, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0x2c, 0x41, 0x08, 0x01,
0x21, 0x11, 0x2e, 0x56, 0x9f, 0xcc, 0xf4, 0x8c, 0x12, 0x09, 0x66, 0x05, 0x46, 0x0d, 0x8e, 0x20, 0x21, 0x11, 0x2e, 0x56, 0x9f, 0xcc, 0xf4, 0x8c, 0x12, 0x09, 0x66, 0x05, 0x46, 0x0d, 0x8e, 0x20,
0x08, 0x47, 0xc9, 0x87, 0x8b, 0x2d, 0xb8, 0x32, 0xcf, 0x31, 0x39, 0x5b, 0x48, 0x01, 0x6c, 0x3c, 0x08, 0x47, 0xc9, 0x87, 0x8b, 0x2d, 0xb8, 0x32, 0xcf, 0x31, 0x39, 0x5b, 0x48, 0x01, 0x6c, 0x3c,
0xd8, 0x4c, 0x6e, 0x23, 0x3e, 0x3d, 0x84, 0x43, 0x82, 0x2b, 0xf3, 0x82, 0xc0, 0x36, 0x2b, 0x70, 0xd8, 0x4c, 0x6e, 0x23, 0x3e, 0x3d, 0x84, 0x43, 0x82, 0x2b, 0xf3, 0x82, 0xc0, 0x36, 0x2b, 0x70,
0x31, 0x3b, 0x26, 0x67, 0x83, 0x4d, 0x46, 0x55, 0xe1, 0x98, 0x9c, 0x1d, 0x04, 0x92, 0x52, 0x92, 0x31, 0x3b, 0x26, 0x67, 0x83, 0x4d, 0x46, 0x55, 0xe1, 0x98, 0x9c, 0x1d, 0x04, 0x92, 0x52, 0x92,
0x07, 0xab, 0xc0, 0xed, 0x44, 0x27, 0x99, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0x07, 0xab, 0xc0, 0xed, 0x44, 0x27, 0x99, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c,
0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63,
0x88, 0x62, 0x2a, 0x48, 0x4a, 0x62, 0x03, 0xfb, 0xd9, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0x53, 0x88, 0x62, 0x2a, 0x48, 0x4a, 0x62, 0x03, 0xfb, 0xd9, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0x04,
0xfe, 0x4c, 0x62, 0x06, 0x01, 0x00, 0x00, 0x2c, 0x04, 0x89, 0x06, 0x01, 0x00, 0x00,
} }
func (m *Syn) Marshal() (dAtA []byte, err error) { func (m *Syn) Marshal() (dAtA []byte, err error) {
...@@ -468,7 +468,7 @@ func (m *Syn) Unmarshal(dAtA []byte) error { ...@@ -468,7 +468,7 @@ func (m *Syn) Unmarshal(dAtA []byte) error {
} }
b := dAtA[iNdEx] b := dAtA[iNdEx]
iNdEx++ iNdEx++
m.NetworkID |= int32(b&0x7F) << shift m.NetworkID |= uint64(b&0x7F) << shift
if b < 0x80 { if b < 0x80 {
break break
} }
......
...@@ -10,7 +10,7 @@ option go_package = "pb"; ...@@ -10,7 +10,7 @@ option go_package = "pb";
message Syn { message Syn {
bytes Address = 1; bytes Address = 1;
int32 NetworkID = 2; uint64 NetworkID = 2;
bool Light = 3; bool Light = 3;
} }
......
...@@ -44,7 +44,7 @@ type Service struct { ...@@ -44,7 +44,7 @@ type Service struct {
host host.Host host host.Host
libp2pPeerstore peerstore.Peerstore libp2pPeerstore peerstore.Peerstore
metrics metrics metrics metrics
networkID int32 networkID uint64
handshakeService *handshake.Service handshakeService *handshake.Service
addrssbook addressbook.Putter addrssbook addressbook.Putter
peers *peerRegistry peers *peerRegistry
...@@ -60,7 +60,7 @@ type Options struct { ...@@ -60,7 +60,7 @@ type Options struct {
Addr string Addr string
DisableWS bool DisableWS bool
DisableQUIC bool DisableQUIC bool
NetworkID int32 NetworkID uint64
Addressbook addressbook.Putter Addressbook addressbook.Putter
Logger logging.Logger Logger logging.Logger
Tracer *tracing.Tracer Tracer *tracing.Tracer
......
...@@ -40,7 +40,7 @@ func newService(t *testing.T, o libp2p.Options) (s *libp2p.Service, overlay swar ...@@ -40,7 +40,7 @@ func newService(t *testing.T, o libp2p.Options) (s *libp2p.Service, overlay swar
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
o.Overlay = crypto.NewAddress(swarmPK.PublicKey) o.Overlay = crypto.NewOverlayAddress(swarmPK.PublicKey, o.NetworkID)
} }
if o.Logger == nil { if o.Logger == nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment