From 69609d885b225c319ced52ec18a91a9a2a6309d2 Mon Sep 17 00:00:00 2001 From: Tak Wai Wong <64229756+tak-hntlabs@users.noreply.github.com> Date: Tue, 17 Jan 2023 16:04:56 -0800 Subject: [PATCH] hnt-571 client integration for v2 (#1237) Fixes the remaining issues in tests, client lib, and dendrite to interact with v2 smart contracts --- zion/zion_authorization_v2.go | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/zion/zion_authorization_v2.go b/zion/zion_authorization_v2.go index ae9acc1cf..79a7a6725 100644 --- a/zion/zion_authorization_v2.go +++ b/zion/zion_authorization_v2.go @@ -32,15 +32,17 @@ type ZionAuthorizationV2 struct { store Store localhostSpaceFactory *localhost_space_factory.LocalhostSpaceFactory localhostSpaces map[string]*localhost_space.LocalhostSpace - //goerliSpaceFactory *goerli_space_factory.LocalhostSpaceFactory - //goerliSpaces map[string]*goerli_space.LocalhostSpace + //goerliSpaceFactory *goerli_space_factory.GoerliSpaceFactory + //goerliSpaces map[string]*goerli_space.GoerliSpace } func NewZionAuthorizationV2(chainId int, ethClient *ethclient.Client, store Store) (authorization.Authorization, error) { za := &ZionAuthorizationV2{ - chainId: chainId, - ethClient: ethClient, - store: store, + chainId: chainId, + ethClient: ethClient, + store: store, + localhostSpaces: make(map[string]*localhost_space.LocalhostSpace), + //goerliSpaces: make(map[string]*goerli_space.GoerliSpace), } switch za.chainId { case 1337, 31337: @@ -94,8 +96,7 @@ func (za *ZionAuthorizationV2) IsAllowed(args authorization.AuthorizationArgs) ( } func (za *ZionAuthorizationV2) getSpaceLocalhost(networkId string) (*localhost_space.LocalhostSpace, error) { - space := za.localhostSpaces[networkId] - if space == nil { + if za.localhostSpaces[networkId] == nil { // convert the networkId to keccak256 spaceIdHash spaceIdHash := NetworkIdToHash(networkId) // then use the spaceFactory to fetch the space address @@ -104,16 +105,16 @@ func (za *ZionAuthorizationV2) getSpaceLocalhost(networkId string) (*localhost_s return nil, err } // 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 { return nil, err } 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 { return false, errors.New("error fetching localhost space factory contract") } @@ -122,6 +123,10 @@ func (za *ZionAuthorizationV2) isSpaceChannelDisabledLocalhost(roomInfo RoomInfo if err != nil { 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 { case "": @@ -130,6 +135,9 @@ func (za *ZionAuthorizationV2) isSpaceChannelDisabledLocalhost(roomInfo RoomInfo default: channelIdHash := NetworkIdToHash(roomInfo.ChannelNetworkId) channel, err := space.GetChannelByHash(nil, channelIdHash) + if err != nil { + return false, err + } return channel.Disabled, err } } @@ -139,12 +147,12 @@ func (za *ZionAuthorizationV2) isAllowedLocalhost( user common.Address, permission authorization.Permission, ) (bool, error) { - if za.localhostSpaceFactory != nil { + if za.localhostSpaceFactory == nil { return false, errors.New("localhost SpaceFactory is not initialised") } // check if space / channel is disabled. - disabled, err := za.isSpaceChannelDisabledLocalhost(roomInfo) + disabled, err := za.isSpaceOrChannelDisabledLocalhost(roomInfo) if disabled { return false, ErrSpaceDisabled } else if err != nil {