github.com/koko1123/flow-go-1@v0.29.6/module/id/fixed_provider.go (about)

     1  package id
     2  
     3  import (
     4  	"github.com/libp2p/go-libp2p/core/peer"
     5  
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  	"github.com/koko1123/flow-go-1/module"
     8  	"github.com/koko1123/flow-go-1/network/p2p/keyutils"
     9  )
    10  
    11  // FixedIdentifierProvider implements an IdentifierProvider which provides a fixed list
    12  // of identifiers.
    13  type FixedIdentifierProvider struct {
    14  	identifiers flow.IdentifierList
    15  }
    16  
    17  func NewFixedIdentifierProvider(identifiers flow.IdentifierList) *FixedIdentifierProvider {
    18  	return &FixedIdentifierProvider{identifiers}
    19  }
    20  
    21  func (p *FixedIdentifierProvider) Identifiers() flow.IdentifierList {
    22  	return p.identifiers
    23  }
    24  
    25  // FixedIdentityProvider implements an IdentityProvider which provides a fixed list
    26  // of identities.
    27  type FixedIdentityProvider struct {
    28  	identities flow.IdentityList
    29  }
    30  
    31  var _ module.IdentityProvider = (*FixedIdentityProvider)(nil)
    32  
    33  func NewFixedIdentityProvider(identities flow.IdentityList) *FixedIdentityProvider {
    34  	return &FixedIdentityProvider{identities}
    35  }
    36  
    37  func (p *FixedIdentityProvider) Identities(filter flow.IdentityFilter) flow.IdentityList {
    38  	return p.identities.Filter(filter)
    39  }
    40  
    41  func (p *FixedIdentityProvider) ByNodeID(flowID flow.Identifier) (*flow.Identity, bool) {
    42  	for _, v := range p.identities {
    43  		if v.ID() == flowID {
    44  			return v, true
    45  		}
    46  	}
    47  	return nil, false
    48  }
    49  
    50  func (p *FixedIdentityProvider) ByPeerID(peerID peer.ID) (*flow.Identity, bool) {
    51  	for _, v := range p.identities {
    52  		if id, err := keyutils.PeerIDFromFlowPublicKey(v.NetworkPubKey); err == nil {
    53  			if id == peerID {
    54  				return v, true
    55  			}
    56  		}
    57  
    58  	}
    59  	return nil, false
    60  
    61  }