Skip to content

Commit

Permalink
return network config
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Aug 13, 2024
1 parent ad6203a commit 203a225
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
53 changes: 52 additions & 1 deletion tools/walletextension/httpapi/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewHTTPRoutes(walletExt *rpcapi.Services) []node.Route {
},
{
Name: common.APIVersion1 + common.PathNetworkConfig,
Func: httpHandler(walletExt, networkHealthRequestHandler),
Func: httpHandler(walletExt, networkConfigRequestHandler),
},
}
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tools/walletextension/rpcapi/wallet_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 203a225

Please sign in to comment.