hnt-571 client integration for v2 (#1237)

Fixes the remaining issues in tests, client lib, and dendrite to interact with v2 smart contracts
This commit is contained in:
Tak Wai Wong 2023-01-17 16:04:56 -08:00 committed by GitHub
parent 34d457c8b0
commit 69609d885b

View file

@ -32,15 +32,17 @@ type ZionAuthorizationV2 struct {
store Store store Store
localhostSpaceFactory *localhost_space_factory.LocalhostSpaceFactory localhostSpaceFactory *localhost_space_factory.LocalhostSpaceFactory
localhostSpaces map[string]*localhost_space.LocalhostSpace localhostSpaces map[string]*localhost_space.LocalhostSpace
//goerliSpaceFactory *goerli_space_factory.LocalhostSpaceFactory //goerliSpaceFactory *goerli_space_factory.GoerliSpaceFactory
//goerliSpaces map[string]*goerli_space.LocalhostSpace //goerliSpaces map[string]*goerli_space.GoerliSpace
} }
func NewZionAuthorizationV2(chainId int, ethClient *ethclient.Client, store Store) (authorization.Authorization, error) { func NewZionAuthorizationV2(chainId int, ethClient *ethclient.Client, store Store) (authorization.Authorization, error) {
za := &ZionAuthorizationV2{ za := &ZionAuthorizationV2{
chainId: chainId, chainId: chainId,
ethClient: ethClient, ethClient: ethClient,
store: store, store: store,
localhostSpaces: make(map[string]*localhost_space.LocalhostSpace),
//goerliSpaces: make(map[string]*goerli_space.GoerliSpace),
} }
switch za.chainId { switch za.chainId {
case 1337, 31337: case 1337, 31337:
@ -94,8 +96,7 @@ func (za *ZionAuthorizationV2) IsAllowed(args authorization.AuthorizationArgs) (
} }
func (za *ZionAuthorizationV2) getSpaceLocalhost(networkId string) (*localhost_space.LocalhostSpace, error) { func (za *ZionAuthorizationV2) getSpaceLocalhost(networkId string) (*localhost_space.LocalhostSpace, error) {
space := za.localhostSpaces[networkId] if za.localhostSpaces[networkId] == nil {
if space == nil {
// convert the networkId to keccak256 spaceIdHash // convert the networkId to keccak256 spaceIdHash
spaceIdHash := NetworkIdToHash(networkId) spaceIdHash := NetworkIdToHash(networkId)
// then use the spaceFactory to fetch the space address // then use the spaceFactory to fetch the space address
@ -104,16 +105,16 @@ func (za *ZionAuthorizationV2) getSpaceLocalhost(networkId string) (*localhost_s
return nil, err return nil, err
} }
// cache the space for future use // cache the space for future use
space, err = localhost_space.NewLocalhostSpace(spaceAddress, za.ethClient) space, err := localhost_space.NewLocalhostSpace(spaceAddress, za.ethClient)
if err != nil { if err != nil {
return nil, err return nil, err
} }
za.localhostSpaces[networkId] = space za.localhostSpaces[networkId] = space
} }
return space, nil return za.localhostSpaces[networkId], nil
} }
func (za *ZionAuthorizationV2) isSpaceChannelDisabledLocalhost(roomInfo RoomInfo) (bool, error) { func (za *ZionAuthorizationV2) isSpaceOrChannelDisabledLocalhost(roomInfo RoomInfo) (bool, error) {
if za.localhostSpaceFactory == nil { if za.localhostSpaceFactory == nil {
return false, errors.New("error fetching localhost space factory contract") return false, errors.New("error fetching localhost space factory contract")
} }
@ -122,6 +123,10 @@ func (za *ZionAuthorizationV2) isSpaceChannelDisabledLocalhost(roomInfo RoomInfo
if err != nil { if err != nil {
return false, err return false, err
} }
if space == nil {
errMsg := fmt.Sprintf("error fetching localhost space contract for %s", roomInfo.SpaceNetworkId)
return false, errors.New(errMsg)
}
switch roomInfo.ChannelNetworkId { switch roomInfo.ChannelNetworkId {
case "": case "":
@ -130,6 +135,9 @@ func (za *ZionAuthorizationV2) isSpaceChannelDisabledLocalhost(roomInfo RoomInfo
default: default:
channelIdHash := NetworkIdToHash(roomInfo.ChannelNetworkId) channelIdHash := NetworkIdToHash(roomInfo.ChannelNetworkId)
channel, err := space.GetChannelByHash(nil, channelIdHash) channel, err := space.GetChannelByHash(nil, channelIdHash)
if err != nil {
return false, err
}
return channel.Disabled, err return channel.Disabled, err
} }
} }
@ -139,12 +147,12 @@ func (za *ZionAuthorizationV2) isAllowedLocalhost(
user common.Address, user common.Address,
permission authorization.Permission, permission authorization.Permission,
) (bool, error) { ) (bool, error) {
if za.localhostSpaceFactory != nil { if za.localhostSpaceFactory == nil {
return false, errors.New("localhost SpaceFactory is not initialised") return false, errors.New("localhost SpaceFactory is not initialised")
} }
// check if space / channel is disabled. // check if space / channel is disabled.
disabled, err := za.isSpaceChannelDisabledLocalhost(roomInfo) disabled, err := za.isSpaceOrChannelDisabledLocalhost(roomInfo)
if disabled { if disabled {
return false, ErrSpaceDisabled return false, ErrSpaceDisabled
} else if err != nil { } else if err != nil {