github.com/ethersphere/bee/v2@v2.2.0/pkg/util/nbhdutil/miner.go (about) 1 // Copyright 2023 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package nbhdutil 6 7 import ( 8 "context" 9 "crypto/ecdsa" 10 "encoding/binary" 11 "fmt" 12 13 "github.com/ethersphere/bee/v2/pkg/crypto" 14 "github.com/ethersphere/bee/v2/pkg/swarm" 15 ) 16 17 func MineOverlay(ctx context.Context, p ecdsa.PublicKey, networkID uint64, targetNeighborhood string) (swarm.Address, []byte, error) { 18 19 nonce := make([]byte, 32) 20 21 neighborhood, err := swarm.ParseBitStrAddress(targetNeighborhood) 22 if err != nil { 23 return swarm.ZeroAddress, nil, err 24 } 25 prox := len(targetNeighborhood) 26 27 i := uint64(0) 28 for { 29 30 select { 31 case <-ctx.Done(): 32 return swarm.ZeroAddress, nil, ctx.Err() 33 default: 34 } 35 36 binary.LittleEndian.PutUint64(nonce, i) 37 38 swarmAddress, err := crypto.NewOverlayAddress(p, networkID, nonce) 39 if err != nil { 40 return swarm.ZeroAddress, nil, fmt.Errorf("compute overlay address: %w", err) 41 } 42 43 if swarm.Proximity(swarmAddress.Bytes(), neighborhood.Bytes()) >= uint8(prox) { 44 return swarmAddress, nonce, nil 45 } 46 47 i++ 48 } 49 }