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

Native Module Refactor #473

Merged
merged 13 commits into from
Jun 2, 2023
Merged

Native Module Refactor #473

merged 13 commits into from
Jun 2, 2023

Conversation

Funkatronics
Copy link
Contributor

@Funkatronics Funkatronics commented May 24, 2023

@Funkatronics Funkatronics changed the title Native Module Refactor [WIP] Native Module Refactor May 25, 2023
@Funkatronics
Copy link
Contributor Author

marked as wip, need to update the PR with the new resolve API

@Funkatronics Funkatronics changed the title [WIP] Native Module Refactor Native Module Refactor Jun 1, 2023
this@SolanaMobileWalletAdapterWalletLibModule.request =
MobileWalletAdapterRemoteRequest.SignMessages(request)
val request = MobileWalletAdapterRemoteRequest.SignMessages(request)
pendingRequests.put(request.id, request)
Copy link
Contributor

Choose a reason for hiding this comment

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

where does the .id field come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its a default constructor parameter on the request. It defaults to a randomly generated UUID if no id is provided.

see here:

Comment on lines +190 to +197
SolanaMobileWalletAdapterWalletLib.completeSignPayloadsWithDecline(request.sessionId, request.requestId)
else if ((response as TooManyPayloadsResponse).failReason === MWARequestFailReason.TooManyPayloads)
SolanaMobileWalletAdapterWalletLib.completeSignPayloadsWithTooManyPayloads(request.sessionId, request.requestId)
else if ((response as AuthorizationNotValidResponse).failReason === MWARequestFailReason.AuthorizationNotValid)
SolanaMobileWalletAdapterWalletLib.completeSignPayloadsWithAuthorizationNotValid(request.sessionId, request.requestId)
else if ((response as InvalidSignaturesResponse).failReason === MWARequestFailReason.InvalidSignatures)
SolanaMobileWalletAdapterWalletLib.completeWithInvalidPayloads(request.sessionId, request.requestId,
(response as InvalidSignaturesResponse).valid)
Copy link
Contributor

Choose a reason for hiding this comment

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

Future Consideration:
Is it possible to combine the "failure response" handling into a single helper function then check for the failure response case at the top of each case?

Something like:

case MWARequestType.SignTransactionsRequest:
            if (response.failReason) {
                  handleFailResponse(request, response)
                  break;
            }
            ....

Guessing would be hard because you lose type signal on which exact completion to call. Not a blocker at all.

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 I have plans to improve all of this. I am working on a more robust serialization approach that will make it easier to pass objects back and forth across the bridge. With that, these switch statements will improve and possibly even go away.

@@ -36,6 +36,13 @@ export enum MobileWalletAdapterServiceRequestEventType {
// Requests that come from the dApp for Authorization, Signing, Sending services.
export abstract class MobileWalletAdapterServiceRequest {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can probably delete all these deprecated files, especially if the full example app UI is working now.

val uri = Uri.parse(uriStr)

// TODO: this is dirty, need some stateful object/data to know what state we are in.
Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely a TODO for now. Maybe this is just something we can follow up once we get feedback from first wallets that implement.

authRequest.completeWithAuthorize(
publicKey.toByteArray(),
accountLabel,
null, // walletUriBase,
Copy link
Contributor

Choose a reason for hiding this comment

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

TODO: Fill in these null fields.

walletUriBase is the custom "scheme" wallets can return to dapps to use (not sure if anyone actually uses this lol)
authorizationScope is the auth key that we implement during ClientTrustUseCase. Maybe we can just add a dummy String/bytearray?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed, tho lets tackle in a separate PR

) = launch {
checkSessionId(sessionId) {
Log.d(TAG, "completeWithAuthorize: authorized public key = $publicKey")
(pendingRequests.remove(requestId) as? MobileWalletAdapterRemoteRequest.AuthorizeDapp)?.request?.let { authRequest ->
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: What happens if the type check here fails for some reason?

We've already executed `pendingRequests.remove(requestId). Does that get lost forever in the fail case? Is that even an issue?

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 so you are correct that the remove will still occur no matter what. Probably would be safer to only remove if we actually complete the request. I will update.

private fun checkSessionId(sessionId: String, doIfValid: (() -> Unit)) =
if (sessionId == scenarioId) doIfValid()
else sendSessionEventToReact(MobileWalletAdapterSessionEvent.ScenarioError(
"Invalid session ($sessionId). This session does not exist/is no longer active."
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@Funkatronics Funkatronics merged commit a927b4e into main Jun 2, 2023
@Funkatronics Funkatronics deleted the martini-native-module-refactor branch June 2, 2023 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants