feat(wallet): add optional Infura auth token to network controller options - #9927
Open
basgys wants to merge 2 commits into
Open
feat(wallet): add optional Infura auth token to network controller options#9927basgys wants to merge 2 commits into
basgys wants to merge 2 commits into
Conversation
…tions Adds `getInfuraAuthToken` to the `networkController` instance options. When supplied, the token it returns is presented as a bearer credential on requests to Infura RPC endpoints. Endpoints are matched by hostname rather than by network client type, since a network client configured as Infura can still direct traffic elsewhere through its failover URLs, and only the endpoints served by Infura should carry the token. Requests that already carry an `Authorization` header keep it. The token is retrieved per request, so one that is refreshed during the lifetime of a network client is picked up on the next request. A failure to retrieve it leaves the request unauthenticated rather than failing it. Omitting the option leaves request behavior unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation
NetworkControlleralready acceptsgetRpcServiceOptions, which customizes theRpcServicebuilt for each RPC endpoint, including thefetchit uses.@metamask/walletforwards nothing to it, so a client has no way to influence RPC requests.This adds
getInfuraAuthTokento thenetworkControllerinstance options:When supplied, the token it returns is presented as
Authorization: Beareron requests to Infura RPC endpoints. Omitting it leaves request behavior unchanged.Endpoints are matched by hostname. A client configured as
NetworkClientType.Infuracan still direct traffic elsewhere:getAvailableEndpointsputs failover URLs in the same chain, and underrpcFailoverMode: 'forced'it bypasses Infura entirely. Matching on the client type would hand the credential to a failover provider.create-network-client.tsalready checks the hostname for this reason, noting that some networks have been added with invalid configuration types.The token is read per request.
getRpcServiceOptionsruns once when theRpcServiceis constructed, so a value captured there would pin whatever the token was at that moment. Wrappingfetchpicks up a refreshed token on the next request.A token that cannot be retrieved leaves the request unauthenticated. A request that already carries an
Authorizationheader keeps it, sinceRpcServicesets one for endpoints whose URL embeds credentials.@metamask/walletnow occupiesgetRpcServiceOptionsunconditionally, so a future instance option wanting to customize RPC services has to compose with this function. If the token belongs on the Infura path proper, a first-class option onNetworkControllerthreaded to the Infura branch increate-network-client.tsis the more honest home. That is a two-package change, which is why this starts smaller.References
None.
Checklist