2022-08-22 10:10:31 +00:00
|
|
|
package communities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-06-06 18:33:09 +00:00
|
|
|
"strings"
|
2022-08-22 10:10:31 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-06-06 18:33:09 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2022-08-22 10:10:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func CalculateRequestID(publicKey string, communityID types.HexBytes) types.HexBytes {
|
|
|
|
idString := fmt.Sprintf("%s-%s", publicKey, communityID)
|
|
|
|
return crypto.Keccak256([]byte(idString))
|
|
|
|
}
|
2023-06-06 18:33:09 +00:00
|
|
|
|
2023-08-17 17:14:23 +00:00
|
|
|
func ExtractTokenCriteria(permissions []*CommunityTokenPermission) (erc20TokenCriteria map[uint64]map[string]*protobuf.TokenCriteria, erc721TokenCriteria map[uint64]map[string]*protobuf.TokenCriteria, ensTokenCriteria []string) {
|
2023-06-06 18:33:09 +00:00
|
|
|
erc20TokenCriteria = make(map[uint64]map[string]*protobuf.TokenCriteria)
|
|
|
|
erc721TokenCriteria = make(map[uint64]map[string]*protobuf.TokenCriteria)
|
|
|
|
ensTokenCriteria = make([]string, 0)
|
|
|
|
|
|
|
|
for _, tokenPermission := range permissions {
|
|
|
|
for _, tokenRequirement := range tokenPermission.TokenCriteria {
|
|
|
|
|
|
|
|
isERC721 := tokenRequirement.Type == protobuf.CommunityTokenType_ERC721
|
|
|
|
isERC20 := tokenRequirement.Type == protobuf.CommunityTokenType_ERC20
|
|
|
|
isENS := tokenRequirement.Type == protobuf.CommunityTokenType_ENS
|
|
|
|
|
|
|
|
for chainID, contractAddress := range tokenRequirement.ContractAddresses {
|
|
|
|
|
|
|
|
_, existsERC721 := erc721TokenCriteria[chainID]
|
|
|
|
|
|
|
|
if isERC721 && !existsERC721 {
|
|
|
|
erc721TokenCriteria[chainID] = make(map[string]*protobuf.TokenCriteria)
|
|
|
|
}
|
|
|
|
_, existsERC20 := erc20TokenCriteria[chainID]
|
|
|
|
|
|
|
|
if isERC20 && !existsERC20 {
|
|
|
|
erc20TokenCriteria[chainID] = make(map[string]*protobuf.TokenCriteria)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, existsERC721 = erc721TokenCriteria[chainID][contractAddress]
|
|
|
|
if isERC721 && !existsERC721 {
|
|
|
|
erc721TokenCriteria[chainID][strings.ToLower(contractAddress)] = tokenRequirement
|
|
|
|
}
|
|
|
|
|
|
|
|
_, existsERC20 = erc20TokenCriteria[chainID][contractAddress]
|
|
|
|
if isERC20 && !existsERC20 {
|
|
|
|
erc20TokenCriteria[chainID][strings.ToLower(contractAddress)] = tokenRequirement
|
|
|
|
}
|
|
|
|
|
|
|
|
if isENS {
|
|
|
|
ensTokenCriteria = append(ensTokenCriteria, tokenRequirement.EnsPattern)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|