The Self SDK provides powerful tools for digital identity management and secure communication.
This guide will walk you through the process of setting up your Self account and creating an inbox, which are essential steps for using the SDK effectively.
cfg:=&account.Config{StorageKey:make([]byte,32),// Replace with a securely generated keyStoragePath:"./storage",// Local storage path for account stateEnvironment:account.TargetSandbox,// Choose between Develop and SandboxLogLevel:account.LogWarn,// Set log level (Error, Warn, Info, Debug, Trace)Callbacks:account.Callbacks{},}
SelfSDK.initialize(androidContext,applicationAddress="<document address from admin app>",// required for non-sandbox environment log={Log.d("Self",it)})valstoragePath=File(androidContext.filesDir.absolutePath+"/account1")if(!storagePath.exists())storagePath.mkdirs()valaccount=Account.Builder().setContext(androidContext).setEnvironment(Environment).setSandbox(true).setStoragePath(storagePath.absolutePath).build()
// Account is already initialized in the configuration stepaccount.setOnStatusListener{statusinprint("init account status:\(status) -> \(self.account.generateAddress())")}
valinboxAddress=runBlocking{suspendCoroutine{continuation->account.inboxOpen{status:SelfStatus,address:PublicKey->println("inbox open status:${status.code()} - address:${address.encodeHex()}")if(status.success()){continuation.resumeWith(Result.success(address))}else{continuation.resumeWith(Result.success(null))}}}}
You don’t need to open the inbox on iOS.
It’s managed internally by the Self iOS SDK.
Note: Creating an inbox is only necessary for the current version of the SDK. This requirement will be removed in future updates to streamline the setup process.
By following these steps, you'll establish a secure foundation for your Self-enabled application. Let's dive into each step in detail.
To use any functionality provided by the Android or iOS SDK, users are required to register an account beforehand.
The SDK includes built-in Kotlin Compose for Android and SwiftUI for iOS UI components to facilitate this process, making it easy to integrate account registration into your app's user interface.
importcom.joinself.sdk.ui.addLivenessCheckRouteimportcom.joinself.ui.theme.SelfModifierimportcom.joinself.sdk.SelfSDKimportcom.joinself.sdk.models.AccountclassMainActivity:ComponentActivity(){overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)enableEdgeToEdge()SelfSDK.initialize(applicationContext,log={Log.d("Self",it)})valstoragePath=File(applicationContext.filesDir.absolutePath+"/account1")if(!storagePath.exists())storagePath.mkdirs()valaccount=Account.Builder().setContext(applicationContext).setEnvironment(Environment).setSandbox(true).setStoragePath(storagePath.absolutePath).build()account.setOnStatusListener{status->println("onStatus $status")}setContent{valcoroutineScope=rememberCoroutineScope()valnavController=rememberNavController()valselfModifier=SelfModifier.sdk()varisRegisteredbyremember{mutableStateOf(account.registered())}NavHost(navController=navController,startDestination="main",modifier=Modifier.systemBarsPadding(),enterTransition={EnterTransition.None},exitTransition={ExitTransition.None}){composable("main"){Column(verticalArrangement=Arrangement.spacedBy(10.dp),horizontalAlignment=Alignment.CenterHorizontally,modifier=Modifier.padding(start=8.dp,end=8.dp).fillMaxWidth()){Text(modifier=Modifier.padding(top=40.dp),text="Registered:${isRegistered}")Button(modifier=Modifier.padding(top=20.dp),onClick={navController.navigate("livenessRoute")},enabled=!isRegistered){Text(text="Create Account")}}}addLivenessCheckRoute(navController,route="livenessRoute",selfModifier=selfModifier,account={account},withCredential=true,onFinish={selfie,credentials->if(!account.registered()){coroutineScope.launch(Dispatchers.IO){try{// use the credentials to register an accountif(selfie.isNotEmpty()&&credentials.isNotEmpty()){valsuccess=account.register(selfieImage=selfie,credentials=credentials)if(success){isRegistered=truewithContext(Dispatchers.Main){Toast.makeText(applicationContext,"Register account successfully",Toast.LENGTH_LONG).show()}}}}catch(_:InvalidCredentialException){}}}coroutineScope.launch(Dispatchers.Main){navController.popBackStack("livenessRoute",true)}})}}}}
We setup an onboarding viewmodel to hold the account and do the liveness check and then register an account. The following codes to do that.