From ad6203a26f7ab98374e5e94078e9abb85869a65f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDiga=20Kokelj?= Date: Tue, 13 Aug 2024 13:02:38 +0200 Subject: [PATCH 1/2] add network config path --- tools/walletextension/common/constants.go | 1 + tools/walletextension/httpapi/routes.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/tools/walletextension/common/constants.go b/tools/walletextension/common/constants.go index 3fdb4d63a2..5b72b8ee35 100644 --- a/tools/walletextension/common/constants.go +++ b/tools/walletextension/common/constants.go @@ -24,6 +24,7 @@ const ( PathRevoke = "/revoke/" PathHealth = "/health/" PathNetworkHealth = "/network-health/" + PathNetworkConfig = "/network-config/" WSProtocol = "ws://" HTTPProtocol = "http://" EncryptedTokenQueryParameter = "token" diff --git a/tools/walletextension/httpapi/routes.go b/tools/walletextension/httpapi/routes.go index abb8f9573a..91d7f688e2 100644 --- a/tools/walletextension/httpapi/routes.go +++ b/tools/walletextension/httpapi/routes.go @@ -58,6 +58,10 @@ func NewHTTPRoutes(walletExt *rpcapi.Services) []node.Route { Name: common.APIVersion1 + common.PathVersion, Func: httpHandler(walletExt, versionRequestHandler), }, + { + Name: common.APIVersion1 + common.PathNetworkConfig, + Func: httpHandler(walletExt, networkHealthRequestHandler), + }, } } From 203a2255b3db3f5fc5d98e582f86a0cfd2aa6035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDiga=20Kokelj?= Date: Tue, 13 Aug 2024 16:33:18 +0200 Subject: [PATCH 2/2] return network config --- tools/walletextension/httpapi/routes.go | 53 ++++++++++++++++++- .../rpcapi/wallet_extension.go | 8 +++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/tools/walletextension/httpapi/routes.go b/tools/walletextension/httpapi/routes.go index 91d7f688e2..cbdb5baf8e 100644 --- a/tools/walletextension/httpapi/routes.go +++ b/tools/walletextension/httpapi/routes.go @@ -60,7 +60,7 @@ func NewHTTPRoutes(walletExt *rpcapi.Services) []node.Route { }, { Name: common.APIVersion1 + common.PathNetworkConfig, - Func: httpHandler(walletExt, networkHealthRequestHandler), + Func: httpHandler(walletExt, networkConfigRequestHandler), }, } } @@ -321,6 +321,57 @@ func networkHealthRequestHandler(walletExt *rpcapi.Services, userConn UserConn) } } +func networkConfigRequestHandler(walletExt *rpcapi.Services, userConn UserConn) { + // read the request + _, err := userConn.ReadRequest() + if err != nil { + walletExt.Logger().Error("error reading request", log.ErrKey, err) + return + } + + // Call the RPC method to get the network configuration + networkConfig, err := walletExt.GetTenNetworkConfig() + if err != nil { + walletExt.Logger().Error("error fetching network config", log.ErrKey, err) + } + + // Define a struct to represent the response + type NetworkConfigResponse struct { + ManagementContractAddress string `json:"ManagementContractAddress"` + L1StartHash string `json:"L1StartHash"` + MessageBusAddress string `json:"MessageBusAddress"` + L2MessageBusAddress string `json:"L2MessageBusAddress"` + ImportantContracts map[string]string `json:"ImportantContracts"` + } + + // Convert the TenNetworkInfo fields to strings + importantContracts := make(map[string]string) + for name, address := range networkConfig.ImportantContracts { + importantContracts[name] = address.Hex() + } + + networkConfigResponse := NetworkConfigResponse{ + ManagementContractAddress: networkConfig.ManagementContractAddress.Hex(), + L1StartHash: networkConfig.L1StartHash.Hex(), + MessageBusAddress: networkConfig.MessageBusAddress.Hex(), + L2MessageBusAddress: networkConfig.L2MessageBusAddress.Hex(), + ImportantContracts: importantContracts, + } + + // Marshal the response into JSON format + data, err := json.Marshal(networkConfigResponse) + if err != nil { + walletExt.Logger().Error("error marshaling response", log.ErrKey, err) + return + } + + // Write the response back to the user + err = userConn.WriteResponse(data) + if err != nil { + walletExt.Logger().Error("error writing success response", log.ErrKey, err) + } +} + // Handles request to /version endpoint. func versionRequestHandler(walletExt *rpcapi.Services, userConn UserConn) { // read the request diff --git a/tools/walletextension/rpcapi/wallet_extension.go b/tools/walletextension/rpcapi/wallet_extension.go index 518dd3c29b..a4c1f092a8 100644 --- a/tools/walletextension/rpcapi/wallet_extension.go +++ b/tools/walletextension/rpcapi/wallet_extension.go @@ -283,6 +283,14 @@ func (w *Services) GetTenNodeHealthStatus() (bool, error) { return *res, err } +func (w *Services) GetTenNetworkConfig() (tencommon.TenNetworkInfo, error) { + res, err := withPlainRPCConnection[tencommon.TenNetworkInfo](context.Background(), w, func(client *gethrpc.Client) (*tencommon.TenNetworkInfo, error) { + res, err := obsclient.NewObsClient(client).GetConfig() + return res, err + }) + return *res, err +} + func (w *Services) GenerateUserMessageToSign(encryptionToken []byte, formatsSlice []string) (string, error) { // Check if the formats are valid for _, format := range formatsSlice {