mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
swarm/network: Store swarm private key in simulation bucket
This commit is contained in:
parent
547ecdbe47
commit
d5f3393a7b
3 changed files with 23 additions and 6 deletions
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
|
@ -34,6 +35,10 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/swarm/network"
|
"github.com/ethereum/go-ethereum/swarm/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
BucketKeyBzzPrivateKey BucketKey = "bzzprivkey"
|
||||||
|
)
|
||||||
|
|
||||||
// NodeIDs returns NodeIDs for all nodes in the network.
|
// NodeIDs returns NodeIDs for all nodes in the network.
|
||||||
func (s *Simulation) NodeIDs() (ids []enode.ID) {
|
func (s *Simulation) NodeIDs() (ids []enode.ID) {
|
||||||
nodes := s.Net.GetNodes()
|
nodes := s.Net.GetNodes()
|
||||||
|
|
@ -111,6 +116,8 @@ func (s *Simulation) AddNode(opts ...AddNodeOption) (id enode.ID, err error) {
|
||||||
PrivateKey: conf.PrivateKey,
|
PrivateKey: conf.PrivateKey,
|
||||||
}
|
}
|
||||||
record, err := network.NewEnodeRecord(enodeParams)
|
record, err := network.NewEnodeRecord(enodeParams)
|
||||||
|
|
||||||
|
bzzPrivateKey, bzzKey, err := BzzKeyFromConfig(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return enode.ID{}, err
|
return enode.ID{}, err
|
||||||
}
|
}
|
||||||
|
|
@ -121,6 +128,8 @@ func (s *Simulation) AddNode(opts ...AddNodeOption) (id enode.ID, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return id, err
|
return id, err
|
||||||
}
|
}
|
||||||
|
s.buckets[node.ID()] = new(sync.Map)
|
||||||
|
s.SetNodeItem(node.ID(), BucketKeyBzzPrivateKey, bzzPrivateKey)
|
||||||
|
|
||||||
return node.ID(), s.Net.Start(node.ID())
|
return node.ID(), s.Net.Start(node.ID())
|
||||||
}
|
}
|
||||||
|
|
@ -331,13 +340,14 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// derive a private key for swarm for the node key
|
// derive a private key for swarm for the node key
|
||||||
func BzzKeyFromConfig(conf *adapters.NodeConfig) ([]byte, error) {
|
// returns the private key used to generate the bzz key AND the generated bzz key
|
||||||
|
func BzzKeyFromConfig(conf *adapters.NodeConfig) (*ecdsa.PrivateKey, []byte, error) {
|
||||||
// ecdsa.GenerateKey takes 40 bytes entropy
|
// ecdsa.GenerateKey takes 40 bytes entropy
|
||||||
privKeyBuf := append(crypto.FromECDSA(conf.PrivateKey), []byte{0x62, 0x7a, 0x7a, 0x62, 0x7a, 0x7a, 0x62, 0x7a}...)
|
privKeyBuf := append(crypto.FromECDSA(conf.PrivateKey), []byte{0x62, 0x7a, 0x7a, 0x62, 0x7a, 0x7a, 0x62, 0x7a}...)
|
||||||
bzzPrivateKey, err := ecdsa.GenerateKey(crypto.S256(), bytes.NewReader(privKeyBuf))
|
bzzPrivateKey, err := ecdsa.GenerateKey(crypto.S256(), bytes.NewReader(privKeyBuf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
bzzKey := network.PrivateKeyToBzzKey(bzzPrivateKey)
|
bzzKey := network.PrivateKeyToBzzKey(bzzPrivateKey)
|
||||||
return bzzKey, nil
|
return bzzPrivateKey, bzzKey, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,11 @@ func New(services map[string]ServiceFunc) (s *Simulation) {
|
||||||
name, serviceFunc := name, serviceFunc
|
name, serviceFunc := name, serviceFunc
|
||||||
s.serviceNames = append(s.serviceNames, name)
|
s.serviceNames = append(s.serviceNames, name)
|
||||||
adapterServices[name] = func(ctx *adapters.ServiceContext) (node.Service, error) {
|
adapterServices[name] = func(ctx *adapters.ServiceContext) (node.Service, error) {
|
||||||
b := new(sync.Map)
|
var b *sync.Map
|
||||||
|
var ok bool
|
||||||
|
if b, ok = s.buckets[ctx.Config.ID]; !ok {
|
||||||
|
b = new(sync.Map)
|
||||||
|
}
|
||||||
service, cleanup, err := serviceFunc(ctx, b)
|
service, cleanup, err := serviceFunc(ctx, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package pss
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/ecdsa"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
@ -408,15 +409,17 @@ func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[T
|
||||||
return map[string]simulation.ServiceFunc{
|
return map[string]simulation.ServiceFunc{
|
||||||
"bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
|
"bzz": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
|
||||||
var err error
|
var err error
|
||||||
|
var bzzPrivateKey *ecdsa.PrivateKey
|
||||||
// normally translation of enode id to swarm address is concealed by the network package
|
// normally translation of enode id to swarm address is concealed by the network package
|
||||||
// however, we need to keep track of it in the test driver as well.
|
// however, we need to keep track of it in the test driver as well.
|
||||||
// if the translation in the network package changes, that can cause these tests to unpredictably fail
|
// if the translation in the network package changes, that can cause these tests to unpredictably fail
|
||||||
// therefore we keep a local copy of the translation here
|
// therefore we keep a local copy of the translation here
|
||||||
addr := network.NewAddr(ctx.Config.Node())
|
addr := network.NewAddr(ctx.Config.Node())
|
||||||
addr.OAddr, err = simulation.BzzKeyFromConfig(ctx.Config)
|
bzzPrivateKey, addr.OAddr, err = simulation.BzzKeyFromConfig(ctx.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
b.Store(simulation.BucketKeyBzzPrivateKey, bzzPrivateKey)
|
||||||
hp := network.NewHiveParams()
|
hp := network.NewHiveParams()
|
||||||
hp.Discovery = false
|
hp.Discovery = false
|
||||||
config := &network.BzzConfig{
|
config := &network.BzzConfig{
|
||||||
|
|
@ -437,7 +440,7 @@ func newProxServices(tstdata *testData, allowRaw bool, handlerContextFuncs map[T
|
||||||
privkey, err := w.GetPrivateKey(keys)
|
privkey, err := w.GetPrivateKey(keys)
|
||||||
pssp := NewPssParams().WithPrivateKey(privkey)
|
pssp := NewPssParams().WithPrivateKey(privkey)
|
||||||
pssp.AllowRaw = allowRaw
|
pssp.AllowRaw = allowRaw
|
||||||
bzzKey, err := simulation.BzzKeyFromConfig(ctx.Config)
|
_, bzzKey, err := simulation.BzzKeyFromConfig(ctx.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue