github.com/status-im/status-go@v1.1.0/protocol/requests/request_to_join_community.go (about)

     1  package requests
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/status-im/status-go/eth-node/crypto"
     7  	"github.com/status-im/status-go/eth-node/types"
     8  )
     9  
    10  var ErrRequestToJoinCommunityInvalidCommunityID = errors.New("request-to-join-community: invalid community id")
    11  var ErrRequestToJoinCommunityNoAddressesToReveal = errors.New("request-to-join-community: no addresses to reveal")
    12  var ErrRequestToJoinCommunityMissingPassword = errors.New("request-to-join-community: password is necessary when sending a list of addresses")
    13  var ErrRequestToJoinNoAirdropAddress = errors.New("request-to-join-community: airdropAddress is necessary when sending a list of addresses")
    14  var ErrRequestToJoinNoAirdropAddressAmongAddressesToReveal = errors.New("request-to-join-community: airdropAddress must be in the set of addresses to reveal")
    15  var ErrRequestToJoinCommunityInvalidSignature = errors.New("request-to-join-community: invalid signature")
    16  
    17  type RequestToJoinCommunity struct {
    18  	CommunityID          types.HexBytes   `json:"communityId"`
    19  	ENSName              string           `json:"ensName"`
    20  	AddressesToReveal    []string         `json:"addressesToReveal"`
    21  	Signatures           []types.HexBytes `json:"signatures"` // the order of signatures should match the order of addresses
    22  	AirdropAddress       string           `json:"airdropAddress"`
    23  	ShareFutureAddresses bool             `json:"shareFutureAddresses"`
    24  }
    25  
    26  func (j *RequestToJoinCommunity) Validate() error {
    27  	if len(j.CommunityID) == 0 {
    28  		return ErrRequestToJoinCommunityInvalidCommunityID
    29  	}
    30  
    31  	if len(j.AddressesToReveal) == 0 {
    32  		return ErrRequestToJoinCommunityNoAddressesToReveal
    33  	}
    34  
    35  	if j.AirdropAddress == "" {
    36  		return ErrRequestToJoinNoAirdropAddress
    37  	}
    38  
    39  	found := false
    40  	for _, address := range j.AddressesToReveal {
    41  		if address == j.AirdropAddress {
    42  			found = true
    43  			break
    44  		}
    45  	}
    46  
    47  	if !found {
    48  		return ErrRequestToJoinNoAirdropAddressAmongAddressesToReveal
    49  	}
    50  
    51  	for _, signature := range j.Signatures {
    52  		if len(signature) != crypto.SignatureLength {
    53  			return ErrRequestToJoinCommunityInvalidSignature
    54  		}
    55  	}
    56  
    57  	return nil
    58  }