mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
swarm, p2p/simulations, cmd/swarm: Use nodekey in binary record sign
This commit is contained in:
parent
dfce98d01b
commit
70061520cb
10 changed files with 108 additions and 43 deletions
|
|
@ -130,7 +130,7 @@ func initSwarmNode(config *bzzapi.Config, stack *node.Node, ctx *cli.Context) er
|
||||||
//set the resolved config path (geth --datadir)
|
//set the resolved config path (geth --datadir)
|
||||||
config.Path = expandPath(stack.InstanceDir())
|
config.Path = expandPath(stack.InstanceDir())
|
||||||
//finally, initialize the configuration
|
//finally, initialize the configuration
|
||||||
err := config.Init(prvkey)
|
err := config.Init(prvkey, stack.Server().PrivateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,7 @@ func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error) {
|
||||||
return nil, fmt.Errorf("error creating node directory: %s", err)
|
return nil, fmt.Errorf("error creating node directory: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := config.initDefaultEnode()
|
err := config.initDummyEnode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -411,11 +411,11 @@ func startExecNodeStack() (*node.Node, error) {
|
||||||
if err := json.Unmarshal([]byte(confEnv), &conf); err != nil {
|
if err := json.Unmarshal([]byte(confEnv), &conf); err != nil {
|
||||||
return nil, fmt.Errorf("error decoding %s: %v", envNodeConfig, err)
|
return nil, fmt.Errorf("error decoding %s: %v", envNodeConfig, err)
|
||||||
}
|
}
|
||||||
// TODO verify that ListenAddr will contain the corrent tcp addr
|
// TODO verify that ListenAddr will contain the correct tcp addr
|
||||||
// if we should start using exec adapters with other host than local
|
// if we should start using exec adapters with other host than local
|
||||||
nodeTcpConn, err := net.ResolveTCPAddr("tcp", conf.Stack.P2P.ListenAddr)
|
nodeTcpConn, err := net.ResolveTCPAddr("tcp", conf.Stack.P2P.ListenAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conf.Node.initDefaultEnode()
|
conf.Node.initDummyEnode()
|
||||||
} else {
|
} else {
|
||||||
conf.Node.initEnode(nodeTcpConn.IP, nodeTcpConn.Port, nodeTcpConn.Port)
|
conf.Node.initEnode(nodeTcpConn.IP, nodeTcpConn.Port, nodeTcpConn.Port)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,7 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := config.initDefaultEnode()
|
err := config.initDummyEnode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -290,6 +290,6 @@ func (n *NodeConfig) initEnode(ip net.IP, tcpport int, udpport int) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NodeConfig) initDefaultEnode() error {
|
func (n *NodeConfig) initDummyEnode() error {
|
||||||
return n.initEnode(net.IPv4(127, 0, 0, 1), 0, 0)
|
return n.initEnode(net.IPv4(127, 0, 0, 1), 0, 0)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,6 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
|
||||||
"github.com/ethereum/go-ethereum/swarm/network"
|
"github.com/ethereum/go-ethereum/swarm/network"
|
||||||
"github.com/ethereum/go-ethereum/swarm/pss"
|
"github.com/ethereum/go-ethereum/swarm/pss"
|
||||||
"github.com/ethereum/go-ethereum/swarm/services/swap"
|
"github.com/ethereum/go-ethereum/swarm/services/swap"
|
||||||
|
|
@ -104,43 +103,35 @@ func NewConfig() (c *Config) {
|
||||||
|
|
||||||
//some config params need to be initialized after the complete
|
//some config params need to be initialized after the complete
|
||||||
//config building phase is completed (e.g. due to overriding flags)
|
//config building phase is completed (e.g. due to overriding flags)
|
||||||
func (c *Config) Init(prvKey *ecdsa.PrivateKey) error {
|
func (c *Config) Init(prvKey *ecdsa.PrivateKey, nodeKey *ecdsa.PrivateKey) error {
|
||||||
|
|
||||||
address := crypto.PubkeyToAddress(prvKey.PublicKey)
|
// create swarm dir and record key
|
||||||
c.Path = filepath.Join(c.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
|
err := c.createAndSetPath(c.Path, prvKey)
|
||||||
err := os.MkdirAll(c.Path, os.ModePerm)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error creating root swarm data directory: %v", err)
|
return fmt.Errorf("Error creating root swarm data directory: %v", err)
|
||||||
}
|
}
|
||||||
|
c.setKey(prvKey)
|
||||||
|
|
||||||
pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
|
// create the new enode record
|
||||||
pubkeyhex := common.ToHex(pubkey)
|
// signed with the ephemeral node key
|
||||||
bzzkeybytes := network.PrivateKeyToBzzKey(prvKey)
|
enodeParams := &network.EnodeParams{
|
||||||
keyhex := hexutil.Encode(bzzkeybytes)
|
PrivateKey: prvKey,
|
||||||
|
EnodeKey: nodeKey,
|
||||||
c.PublicKey = pubkeyhex
|
Lightnode: c.LightNodeEnabled,
|
||||||
c.BzzKey = keyhex
|
Bootnode: c.BootnodeMode,
|
||||||
|
|
||||||
var record enr.Record
|
|
||||||
record.Set(network.NewENRAddrEntry(bzzkeybytes))
|
|
||||||
record.Set(network.ENRLightNodeEntry(c.LightNodeEnabled))
|
|
||||||
record.Set(network.ENRBootNodeEntry(c.BootnodeMode))
|
|
||||||
err = enode.SignV4(&record, prvKey)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("ENR create fail: %v", err)
|
|
||||||
}
|
}
|
||||||
c.Enode, err = enode.New(enode.V4ID{}, &record)
|
c.Enode, err = network.NewEnode(enodeParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Enode create fail: %v", err)
|
return fmt.Errorf("Error creating enode: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// initialize components that depend on the swarm instance's private key
|
||||||
if c.SwapEnabled {
|
if c.SwapEnabled {
|
||||||
c.Swap.Init(c.Contract, prvKey)
|
c.Swap.Init(c.Contract, prvKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.privateKey = prvKey
|
|
||||||
c.LocalStoreParams.Init(c.Path)
|
c.LocalStoreParams.Init(c.Path)
|
||||||
c.LocalStoreParams.BaseKey = common.FromHex(keyhex)
|
c.LocalStoreParams.BaseKey = common.FromHex(c.BzzKey)
|
||||||
|
|
||||||
c.Pss = c.Pss.WithPrivateKey(c.privateKey)
|
c.Pss = c.Pss.WithPrivateKey(c.privateKey)
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -153,3 +144,25 @@ func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
|
||||||
}
|
}
|
||||||
return privKey
|
return privKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) setKey(prvKey *ecdsa.PrivateKey) {
|
||||||
|
bzzkeybytes := network.PrivateKeyToBzzKey(prvKey)
|
||||||
|
pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
|
||||||
|
pubkeyhex := hexutil.Encode(pubkey)
|
||||||
|
keyhex := hexutil.Encode(bzzkeybytes)
|
||||||
|
|
||||||
|
c.privateKey = prvKey
|
||||||
|
c.PublicKey = pubkeyhex
|
||||||
|
c.BzzKey = keyhex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) createAndSetPath(datadirPath string, prvKey *ecdsa.PrivateKey) error {
|
||||||
|
address := crypto.PubkeyToAddress(prvKey.PublicKey)
|
||||||
|
bzzdirPath := filepath.Join(datadirPath, "bzz-"+common.Bytes2Hex(address.Bytes()))
|
||||||
|
err := os.MkdirAll(bzzdirPath, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.Path = bzzdirPath
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,11 +27,16 @@ import (
|
||||||
func TestConfig(t *testing.T) {
|
func TestConfig(t *testing.T) {
|
||||||
|
|
||||||
var hexprvkey = "65138b2aa745041b372153550584587da326ab440576b2a1191dd95cee30039c"
|
var hexprvkey = "65138b2aa745041b372153550584587da326ab440576b2a1191dd95cee30039c"
|
||||||
|
var hexnodekey = "75138b2aa745041b372153550584587da326ab440576b2a1191dd95cee30039c"
|
||||||
|
|
||||||
prvkey, err := crypto.HexToECDSA(hexprvkey)
|
prvkey, err := crypto.HexToECDSA(hexprvkey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to load private key: %v", err)
|
t.Fatalf("failed to load private key: %v", err)
|
||||||
}
|
}
|
||||||
|
nodekey, err := crypto.HexToECDSA(hexnodekey)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to load private key: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
one := NewConfig()
|
one := NewConfig()
|
||||||
two := NewConfig()
|
two := NewConfig()
|
||||||
|
|
@ -41,7 +46,7 @@ func TestConfig(t *testing.T) {
|
||||||
t.Fatal("Two default configs are not equal")
|
t.Fatal("Two default configs are not equal")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := one.Init(prvkey)
|
err = one.Init(prvkey, nodekey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BzzAddr implements the PeerAddr interface
|
// BzzAddr implements the PeerAddr interface
|
||||||
|
|
@ -68,3 +69,37 @@ func PrivateKeyToBzzKey(prvKey *ecdsa.PrivateKey) []byte {
|
||||||
pubkeyBytes := crypto.FromECDSAPub(&prvKey.PublicKey)
|
pubkeyBytes := crypto.FromECDSAPub(&prvKey.PublicKey)
|
||||||
return crypto.Keccak256Hash(pubkeyBytes).Bytes()
|
return crypto.Keccak256Hash(pubkeyBytes).Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EnodeParams struct {
|
||||||
|
PrivateKey *ecdsa.PrivateKey
|
||||||
|
EnodeKey *ecdsa.PrivateKey
|
||||||
|
Lightnode bool
|
||||||
|
Bootnode bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEnodeRecord(params *EnodeParams) (*enr.Record, error) {
|
||||||
|
|
||||||
|
if params.PrivateKey == nil {
|
||||||
|
return nil, fmt.Errorf("all param private keys must be defined")
|
||||||
|
}
|
||||||
|
|
||||||
|
bzzkeybytes := PrivateKeyToBzzKey(params.PrivateKey)
|
||||||
|
|
||||||
|
var record enr.Record
|
||||||
|
record.Set(NewENRAddrEntry(bzzkeybytes))
|
||||||
|
record.Set(ENRLightNodeEntry(params.Lightnode))
|
||||||
|
record.Set(ENRBootNodeEntry(params.Bootnode))
|
||||||
|
return &record, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEnode(params *EnodeParams) (*enode.Node, error) {
|
||||||
|
record, err := NewEnodeRecord(params)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = enode.SignV4(record, params.EnodeKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("ENR create fail: %v", err)
|
||||||
|
}
|
||||||
|
return enode.New(enode.V4ID{}, record)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -102,16 +102,16 @@ func (s *Simulation) AddNode(opts ...AddNodeOption) (id enode.ID, err error) {
|
||||||
// most importantly the bzz overlay address
|
// most importantly the bzz overlay address
|
||||||
//
|
//
|
||||||
// for now we have no way of setting bootnodes or lightnodes in sims
|
// for now we have no way of setting bootnodes or lightnodes in sims
|
||||||
// so we just set them as false
|
// so we just let them be set to false
|
||||||
// they should perhaps be possible to override them with AddNodeOption
|
// they should perhaps be possible to override them with AddNodeOption
|
||||||
bzzKey := network.PrivateKeyToBzzKey(conf.PrivateKey)
|
enodeParams := &network.EnodeParams{
|
||||||
bzzAddr := network.NewENRAddrEntry(bzzKey)
|
PrivateKey: conf.PrivateKey,
|
||||||
|
}
|
||||||
var lightnode network.ENRLightNodeEntry
|
record, err := network.NewEnodeRecord(enodeParams)
|
||||||
var bootnode network.ENRBootNodeEntry
|
if err != nil {
|
||||||
conf.Record.Set(bzzAddr)
|
return enode.ID{}, err
|
||||||
conf.Record.Set(&lightnode)
|
}
|
||||||
conf.Record.Set(&bootnode)
|
conf.Record = *record
|
||||||
|
|
||||||
// Add the bzz address to the node config
|
// Add the bzz address to the node config
|
||||||
node, err := s.Net.NewNodeWithConfig(conf)
|
node, err := s.Net.NewNodeWithConfig(conf)
|
||||||
|
|
|
||||||
|
|
@ -311,8 +311,12 @@ func testSwarmNetwork(t *testing.T, o *testSwarmNetworkOptions, steps ...testSwa
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, cleanup, err
|
return nil, cleanup, err
|
||||||
}
|
}
|
||||||
|
nodekey, err := crypto.GenerateKey()
|
||||||
|
if err != nil {
|
||||||
|
return nil, cleanup, err
|
||||||
|
}
|
||||||
|
|
||||||
config.Init(privkey)
|
config.Init(privkey, nodekey)
|
||||||
config.DeliverySkipCheck = o.SkipCheck
|
config.DeliverySkipCheck = o.SkipCheck
|
||||||
config.Port = ""
|
config.Port = ""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -170,8 +170,12 @@ func TestNewSwarm(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
nodekey, err := crypto.GenerateKey()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
config.Init(privkey)
|
config.Init(privkey, nodekey)
|
||||||
|
|
||||||
if tc.configure != nil {
|
if tc.configure != nil {
|
||||||
tc.configure(config)
|
tc.configure(config)
|
||||||
|
|
@ -307,8 +311,12 @@ func TestLocalStoreAndRetrieve(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
nodekey, err := crypto.GenerateKey()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
config.Init(privkey)
|
config.Init(privkey, nodekey)
|
||||||
|
|
||||||
swarm, err := NewSwarm(config, nil)
|
swarm, err := NewSwarm(config, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue