Add dependencies
Themobile-wallet-adapter-clientlib-ktx library is Solana Mobile’s implementation of the Mobile Wallet Adapter protocol.
It provides a convenient API to connect, issue signing requests to a locally installed wallet app, and receive responses.
Instantiate MobileWalletAdapter client
The MobileWalletAdapter object provides methods to connect to wallets and issue MWA requests.
Define the ConnectionIdentity of your dApp so that the wallet app can properly display your dApp info to the user.
Parameters:
identityName: The name of your app.identityUri: The web URL associated with your app.iconUri: A path to your app icon relative to the app uri above.
Managing the authToken
The MobileWalletAdapter object exposes an authToken property that it manages throughout its lifetime.
If present, the authToken is automatically used by the MWA client when issuing MWA requests (like connect, signMessages, etc). And if valid,
the user is able to skip the connection approval dialog for subsequent requests.
The authToken is stored by the MobileWalletAdapter client whenever you connect to a wallet, but it can also be
provided manually:
Establishing an MWA session
To establish a session, or ‘connect’, with an MWA wallet, use thetransact method provided by the MobileWalletAdapter object.
Calling transact dispatches an assocication intent to a locally installed MWA wallet app and prompts the
user to approve or reject the connection request.
Once connected, the user can begin issuing MWA requests and receiving responses from the wallet app. The MobileWalletAdapter
object also stores, in memory, the authToken from successful connections to be used automatically subsequent sessions.
transact returns a TransactionResult that can be unwrapped and conditioned upon to handle success and error cases.
Connecting to a wallet
If you only need to connect to a wallet and do not need to send any additional MWA requests, use theconnect method from the MobileWalletAdapter client.
TransactionResult will contain an AuthorizationResult that contains the user’s wallet address, authToken, etc.
What’s the difference with transact and connect?
Under the hood, the connect method just calls the transact function with an empty callback, immediately returning the authResult.
Sign in with Solana
To connect to a wallet and simultaneously verify the user’s ownership of the wallet, use the Sign in with Solana feature. SIWS combines theauthorize and signMessage step and returns a SignInResult that can be verified by the dApp.
To initiate SIWS, use the signIn method and pass in a SignInPayload parameter. If provided, the wallet
will display a dedicated SIWS UI and prompt the user to sign in by signing the statement message.
Verifying the sign-in result
If successful, the wallet will respond with anauthResult that includes a SignInResult object, which can be used
for verifying the sign-in process. The SignInResult object will contain the fields outlined in the SIWS spec.
To verify the Sign-In output, use an Ed25519 library to verify that the message was correctly signed by the user’s wallet. See fakedapp for an example of message verification in Kotlin or an example with javascript on server-side.
Transact after signing in
Similarly toconnect, the signIn method just wraps an empty transact call and includes the provided signInPayload.
If you want to sign in to the wallet and and continue issuing additional MWA requests, then you can use
the include the optional signInPayload parameter when using the transact method.
Disconnecting from a wallet
A dApp can revoke authorization or disconnect from a wallet by sending a disconnect request. The wallet will invalidate theauthToken stored by the MobileWalletAdapter. This will require the user to approve the connection request once again, when connecting to that wallet.
deauthorize request to the wallet and provide a specific authToken to invalidate.
Signing and sending transactions
To request a wallet to sign and then send a Solana transaction, use thesignAndSendTransactions method. With this method,
the wallet will handle both signing the transactions then submitting them to the Solana network.
For an example of building a transaction, see the ‘Building transactions’ guide.
TransactionResult will contain a successPayload with an array (signatures), where each item is a transaction
signature serialized as ByteArray, in corresponding order to the input.
Signing messages
To request a wallet to sign a message, use thesignMessagesDetached method. In this case, a message is any payload of bytes.
TransactionResult will contain a successPayload with an array (messages), where each item is a signed message
payload serialized as a ByteArray, in corresponding order to the input.
Signing transactions (deprecated)
To request a wallet to sign a Solana transaction, use thesignTransactions method. For an example
of building a transaction, see the ‘Building transactions’ guide.
signTransactions method accepts an array of serialized transactions and, on success, returns signedPayloads containing the corresponding
signed payloads serialized as ByteArray.
Next Steps
- Browse or clone the MintyFresh repo to reference best practices for a live, published Kotlin Solana dApp.
