Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add network config endpoint #2016

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit of a shame that we can't use ObscuroNetworkInfo like the ObsClient but I guess it's for json compatibility? Maybe better to have separate control over it anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is because of that + we need to convert data fields to strings. I was also asking ChatGPT if there is a good way, but unfortunately nothing simpler than that :/

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this do any caching by default? Maybe safer not to cache for now I guess, there are some edge cases when network starts up before L2 contract deployment maybe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't do any caching. I was thinking about it, but it's not an expensive call to the node and we can add it later if needed.

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
Loading