Skip to content

Commit

Permalink
Add network config endpoint (#2016)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj authored Aug 13, 2024
1 parent f692c26 commit 86ee410
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/walletextension/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
PathRevoke = "/revoke/"
PathHealth = "/health/"
PathNetworkHealth = "/network-health/"
PathNetworkConfig = "/network-config/"
WSProtocol = "ws://"
HTTPProtocol = "http://"
EncryptedTokenQueryParameter = "token"
Expand Down
55 changes: 55 additions & 0 deletions tools/walletextension/httpapi/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, networkConfigRequestHandler),
},
}
}

Expand Down Expand Up @@ -317,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 @@ -280,6 +280,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 86ee410

Please sign in to comment.