Before you can add Auth Service to your Android app, you need to create a Particle project to connect to your Android app. Visit Particle Dashboard to learn more about Particle projects and apps.
Declare them in your module (app-level) Gradle file (usually app/build.gradle).
repositories {google()mavenCentral()maven { setUrl("https://jitpack.io") }//...}dependencies {// Particle Auth Serviceimplementation("network.particle:auth-service:${latest_version}") //find the latest version of the sdk://https://search.maven.org/search?q=g:network.particle//...}
Replace pn_project_id, pn_project_client_key, pn_app_id with the new values created in Particle Dashboard.
API Reference
Initialize the SDK
Initialize the SDK by calling the ParticleNetwork.init()method, passing the method a context. Do this as soon as your app starts, like in the onCreate()method of your Application.
// Async switch chain name, it will check if user has logged in this chain name.// For example, if a user logged in with ethereum, then switch to bsc,// it will switch to bsc directly, beacuse both bsc and ethereum are evm,// but if switch to solana, beacuse user didn't log in solana before, it will // present a web browser for additional information automatically.ParticleNetwork.switchChain( ChainInfo.Ethereum,object : ResultCallback {overridefunsuccess() {val currChain = ParticleNetwork.chainInfo.name }overridefunfailure() { } })// Sync switch chain name. it will wont check if user has logged in.ParticleNetwork.setChainInfo(ChainInfo.Polygon)
Login
To auth login with Particle, call ParticleNetwork.login(...)with activity and callback. You can log in with your email or phone number by changing the loginType parameter. Your wallet is created when you log in successfully for the first time.
// - Parameters:// - loginType: Login type, support jwt,email, phone, google, apple and facebook// - account: When login type is email or phone, you could pass email address or phone number,Optional. When login type is jwt, you must pass json web token.// - supportAuthType: Controls whether third-party login buttons are displayed. default will show none third-party login buttons.
// - loginFormMode: Controls whether show light UI in web, default is false.// - prompt: Social login prompt. default is nullParticleNetwork.login( loginType = LoginType.PHONE, account ="", supportAuthTypeValues = SupportAuthType.FACEBOOK.value or SupportAuthType.GOOGLE.value or SupportAuthType.APPLE.value,
prompt =null, loginCallback =object : WebServiceCallback<UserInfo> {overridefunsuccess(output: UserInfo) { }overridefunfailure(errMsg: ErrorInfo) { } })// - without account parameter you can use this method overwriteParticleNetwork.login( loginType = LoginType.PHONE, supportAuthTypeValues = SupportAuthType.FACEBOOK.value or SupportAuthType.GOOGLE.value or SupportAuthType.APPLE.value,
loginCallback =object : WebServiceCallback<UserInfo> {overridefunsuccess(output: UserInfo) { }overridefunfailure(errMsg: ErrorInfo) { } })// Login with JWTval account ="json web token"ParticleNetwork.login( loginType = LoginType.JWT, account = account, supportAuthTypeValues = SupportAuthType.ALL.value, loginCallback =object : WebServiceCallback<UserInfo>{overridefunsuccess(output: UserInfo) { }overridefunfailure(errMsg: ErrorInfo) { } })
After log-in success, you can obtain user info by calling ParticleNetwork.getUserInfo()
Logout
The SDK will delete users' account information in cache.
You can create transaction with TxData.There's an easy way to do this with Wallet Service
Set Display Wallet
// set display wallet when call sign and send transaction. default is falseParticleNetwork.setDisplayWallet(true)
Open Web Wallet
// if user is login, it will open web wallet.//https://docs.particle.network/developers/wallet-service/sdks/webval customStyleJson =""" { "supportAddToken": false, "supportChains": [{ "id": 1, "name": "Ethereum" }, { "id": 5, "name": "Ethereum" } ] }"""ParticleNetwork.openWebWallet(customStyleJson)
Open account and security
ParticleNetwork.openAccountAndSecurity()
Set security account config
// set security account config, // promptSettingWhenSign default value is 1.// promptMasterPasswordSettingWhenLogin default value is 0.// 0 no prompt// 1 first time show prompt// 2 every time show prompt ParticleNetwork.setSecurityAccountConfig(SecurityAccountConfig( promptSettingWhenSign =1, promptMasterPasswordSettingWhenLogin =2 ))
Custom interface style
//this is the default settingParticleNetwork.setAppearence(ThemeEnum.AUTO)//dark ParticleNetwork.setAppearence(ThemeEnum.DARK)//lightParticleNetwork.setAppearence(ThemeEnum.LIGHT)
Language setting
//Set the language of the SDK//support LanguageEnum.EN、LanguageEnum.JA、LanguageEnum.KO、LanguageEnum.ZH_CN、LanguageEnum.ZH_TWParticleNetwork.setAppliedLanguage(LanguageEnum.EN)//Get Applied Languageval languageEnum = ParticleNetwork.getAppliedLanguage()
Control setpassword dialog
If the user does not set a password, a dialog box will pop up when the user sends a transaction,
If you don't want to prompt the user, you can call the following code
// Note: If you want to add listening events, you need to put them before initParticleWalletConnect.onXXListeners = {...}// Initialize Particle Wallet Connect SDK, a wallet meta dataval dAppMetadata =DAppMetadata("walletConnectProjectId","Particle Connect","https://connect.particle.network/icons/512.png","https://particle.network", description = "Particle Connect is a decentralized wallet connection protocol that makes it easy for users to connect their wallets to your DApp.",
)ParticleWalletConnect.init(this, dAppData = dAppMetadata)
Connect
// Connect to a wallet connect codeval uri = "wc:DDCBBAA8-B1F1-4B98-8F74-84939E0B1533@1?bridge=https%3A%2F%2Fbridge%2Ewalletconnect%2Eorg%2F&key=3da9dbb33b560beeb1750203a8d0e3487b4fe3fdd7b7953d79fbccadae8aab48"
ParticleWalletConnect.connect(uri)
Set Provider And Listeners
//require: This method needs to be called before ParticleWalletConnect.init//AuthProvider Implemented the following methods://personal_sign、eth_signTypedData_v1、eth_signTypedData_v3、eth_signTypedData_v4、eth_sendTransaction、eth_chainId、eth_accounts、eth_requestAccounts
//Here you can replace your own Provider ParticleWalletDelegate.wcEventModels.onEach { wcEvent ->when (wcEvent) {is Sign.Model.SessionProposal -> { }is Sign.Model.SessionRequest -> { }else-> { } }}.launchIn(lifecycleScope)//approve or rejectParticleWalletConnect.onSessionRequest = { id, peerMeta ->//approve ParticleWalletConnect.approveSession(context,"PublicAddress",chainId)//or reject ParticleWalletConnect.rejectSession()}//disconnect sessionParticleWalletConnect.onDisconnect = { code, topic, reason ->//disconnect ParticleWalletConnect.delWCSessionStoreItem(topic)}//receive change network requestParticleWalletConnect.onWalletChangeNetwork = { id, topic, chainId ->//show dialog to switch network}
Session Operation
//when ParticleWalletConnect.approveSession() is call,a new Session can be saved//get All Session InfoParticleWalletConnect.getAllWCSessionStoreItem()//get Session by topicParticleWalletConnect.getWCSessionStoreItem(topic)//delete Session by topicParticleWalletConnect.delWCSessionStoreItem(topic)//delete all SessionsParticleWalletConnect.clearWCSessionStore()