Android SDK API
Android SDK API
#Version
This documentation centers around using magic-android
SDK version 8.0.0
and above. You can view the open-source repository for this SDK on Github.
#Demo
Check out this repository for a demo application using of all of the following methods.
#Configuration & Setup
In order to use the Magic Android SDK, you must get a valid publishable API key from the Magic Dashboard. In the examples below, replace the value of YOUR_API_KEY
with the value you get from the dashboard. See the quickstart section for more details and installation instructions.
#Constructor
The Magic class is the entry-point to the Magic SDK. It must be instantiated with a Magic publishable key.
Magic
Public constructors | |
Magic(context: Context, apiKey: String) | Construct a Magic instance with publishable API Key retrieved from the Magic Dashboard |
Magic(context:Context, apiKey: String, network: Magic.EthNetwork) | Construct a Magic instance with publishable Key and Ethereum network |
Magic(context:Context, apiKey: String, customNode: CustomNodeConfiguration) | Construct a Magic instance with publishable Key and Custom Node configuration |
Example
01class MagicDemoApp: Application() {
02
03 lateinit var magic: Magic
04 override fun onCreate() {
05 magic = Magic(this, "YOUR_PUBLISHABLE_KEY")
06 super.onCreate()
07 }
08}
#Wallet Module Methods
All methods below are namespaced to the magic.wallet
module.
#connectWithUI()
Request the user to connect their wallet to the dapp. This is done by using the EVM RPC method getAccount
.
#Arguments
None.
#Returns
An string array (
<[String]>
) of user accounts that are connected, with the first element being the current public address of the user.
#Example
01val accounts = (magic as Magic).wallet.connectWithUI(this)
02
03accounts.whenComplete { response: ConnectWithUIResponse?, error: Throwable? ->
04 if (error != null) {
05 Log.d("error", error.localizedMessage)
06 }
07
08 if (response != null && !response.hasError()) {
09 response.result?.let { Log.d("Your Public Address is:", it.first()) }
10 startTabActivity()
11 } else {
12 Log.d("MC Login", "Magic Connect Not logged in")
13 }
14}
#getInfo()
Get information about the wallet the user is currently logged-in with. User must be signed in for this method to return or else it will throw an error.
#Arguments
None.
#Returns
- Returns a response with the wallet type as a
<String>
. Wallet type values aremagic
,metamask
,coinbase_wallet
, orwallet_connect
.
Will throw an error if no user is logged in.
#Example
01val completable = magic.wallet.getInfo(this.requireContext())
02
03completable.whenComplete { response: WalletInfoResponse?, error: Throwable? ->
04 if (error != null) {
05 Log.d("error", error.localizedMessage)
06 }
07
08 if (response != null) {
09 tabActivity.toastAsync("Wallet Type:" + response.result.walletType)
10 }
11}
12
#showUI()
Displays the wallet widget. This is only supported for users who login with email or Google and not third party wallets such as metamask. User must be signed in for this method to return or else it will throw an error.
#Arguments
None.
#Returns
Promise
which resolves when the user closes the wallet widget window.
#Example
01val completable = magic.wallet.showUI(this.requireContext())
02
03completable.whenComplete { response: ShowWalletResponse?, error: Throwable? ->
04 if (error != null) {
05 Log.d("error", error.localizedMessage)
06 }
07
08 if (response != null) {
09 tabActivity.toastAsync("show Wallet:" + response.result)
10 }
11}
12
#requestUserInfoWithUI()
Displays the wallet widget within an iframe that prompts the user to consent to sharing information with the requesting dApp with OpenID profile scopes. Currently, the only profile scope that can be requested is a verified email. Collecting a verified email address from third-party wallet users (MetaMask, Coinbase Wallet, etc.) is a premium feature but included in the free trial period (see pricing). User must be signed in for this method to return or else it will throw an error.
#Arguments
Parameter | Type | Definition |
configuration: | RequestUserInfoWithUIConfiguration? |
|
#Returns
Returns a promise which will resolve to an object containing the requested information or rejects if the user declines to share information.
#Example
01
02val config = RequestUserInfoWithUIConfiguration(scope = WalletUserInfoScope(email = WalletUserInfoEmailOptions.required))
03val completable = magic.wallet.requestUserInfoWithUI(this.requireContext(), config)
04
05completable.whenComplete { response: RequestUserInfoWithUIResponse?, error: Throwable? ->
06 if (error != null) {
07 Log.d("error", error.localizedMessage)
08 }
09
10 if (response != null) {
11 tabActivity.toastAsync("Request User Info" + response.result.email)
12 }
13}
14
#disconnect()
Disconnects the user from the dApp. This is similar to logging a user out in that it will clear the authenticated state for the current user, the iframe, and any third party wallet the user may have signed in with. User must be signed in for this method to return or else it will throw an error.
#Arguments
None.
#Return Value
A
promise
that return aboolean
: If the user's session was succesfully cleared or not.
#Example
01val completable = magic.wallet.disconnect(this.requireContext())
02
03completable.whenComplete { response: DisconnectResponse?, error: Throwable? ->
04 if (error != null) {
05 Log.d("error", error.localizedMessage)
06 }
07
08 if (response != null) {
09 tabActivity.toastAsync("Disconnect -" + response.result)
10 }
11}
12