dendrite/zion/zion_authorization.go
Tak Wai Wong 982ce71aad fix dendrite to interact with v1 / v2 contracts (#1155)
dendrite uses commandline flag v1 or v2 to choose the smart contracts for the isEntitled check
2022-12-29 15:37:03 -08:00

63 lines
1.5 KiB
Go

package zion
import (
_ "embed"
"errors"
"fmt"
"github.com/matrix-org/dendrite/authorization"
roomserver "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/setup/config"
log "github.com/sirupsen/logrus"
)
type ContractVersion uint8
const (
v1 ContractVersion = 1
v2 ContractVersion = 2
)
var ErrSpaceDisabled = errors.New("space disabled")
var ErrChannelDisabled = errors.New("channel disabled")
func NewZionAuthorization(cfg *config.ClientAPI, roomQueryAPI roomserver.QueryEventsAPI) (authorization.Authorization, error) {
// create the authorization states
store := NewStore(roomQueryAPI)
chainId := cfg.PublicKeyAuthentication.Ethereum.GetChainID()
contractVersion := v1 // default
if cfg.PublicKeyAuthentication.Ethereum.ContractVersion == "v2" {
contractVersion = v2
}
// initialise the eth client.
if cfg.PublicKeyAuthentication.Ethereum.NetworkUrl == "" {
log.Errorf("No blockchain network url specified in config\n")
return nil, nil
}
ethClient, err := GetEthClient(cfg.PublicKeyAuthentication.Ethereum.NetworkUrl)
if err != nil {
log.Errorf("Cannot connect to eth client %v\n", cfg.PublicKeyAuthentication.Ethereum.NetworkUrl)
return nil, err
}
switch contractVersion {
case v1:
return NewZionAuthorizationV1(
chainId,
ethClient,
store,
)
case v2:
return NewZionAuthorizationV2(
chainId,
ethClient,
store,
)
default:
errMsg := fmt.Sprintf("Unsupported contract version: %d", contractVersion)
return nil, errors.New(errMsg)
}
}