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

[SFA Android] Update docs #917

Merged
merged 7 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions docs/migration-guides/sfa-android-v0.4.0-to-v1.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: SFA Android SDK - v0.4.0 to v1
description: "SFA Android SDK - v0.4.0 to v1 | Documentation - Web3Auth"
sidebar_label: v0.4.0 to v1
---

This migration guide provides steps for upgrading from version v0.4.0 to v1 of the SFA Android SDK.
The guide outlines significant changes and enhancements, including the support of Web3Auth's
Sapphire network support.

## Breaking Changes

### Changes in SingleFactorAuth Constructor

In v8, we try to improve the developer experience by renaming the parameter and
`SingleFactorAuthArgs` class, and introducing a new parameter for session management.

- `singleFactorAuthArgs` used to initialize the SingleFactorAuth SDK has been renamed to
`sfaParams`. It has been renamed to align the naming conventions across Web3Auth SDKs.
- `context` was added to specific the application's context.

### Changes in SingleFactorAuthArgs Changes

- `SingleFactorAuthArgs` is now renamed to `SFAParams`. It has been renamed to align with naming
conventions across Web3Auth SDKs.
- `sessionTime` was added to specify session duration in seconds. By default, the value is set to
86400 seconds (1 day), with a maximum session duration of up to 7 days.
- `serverTimeOffset` was added to specify a custom server time offset. The default value is 0

```kotlin
// remove-next-line
val params = SingleFactorAuthArgs(
// add-next-line
val params = SFAParams(
Web3AuthNetwork.SAPPHIRE_MAINNET,
"YOUR_WEB3AUTH_CLIENT_ID"
)

val singleFactorAuth = SingleFactorAuth(
singleFactorAuthArgs,
// add-next-line
this,
)


```

### TorusKey is renamed to SFAKey

In v1, the `TorusKey` is now removed. Developers can now use `SFAKey` to access the private key and
address for the account. It was renamed to maintain consistency across SDKs.

### Deprecation of authentication methods

In v1, the `getKey` method is deprecated. Developers can now use `connect`, the common method for
single verifier and aggregate verifier login. This change was done to improve the developer
experience and align with other Web3Auth SFA SDKs.

```kotlin
val loginParams = LoginParams(
"YOUR_VERIFIER_NAME",
"YOUR_VERIFIER_ID_VALUE",
"YOUR_ID_TOKEN"
);

// remove-next-line
val torusKey = singleFactorAuth.getKey(loginParams).get()
// add-next-line
val sfaKey = singleFactorAuth.connect(loginParams, this.applicationContext)
```

## New Addition: Sapphire Support

We are happy to announce that with v1, SFA Android SDK now supports the Web3Auth Sapphire networks.
Developers can now use the verifiers deployed on sapphire networks. Please check the supported
networks by the SDK.

```kotlin
public enum Web3AuthNetwork {
MAINNET("mainnet"),
TESTNET("testnet"),
CYAN("cyan"),
AQUA("aqua"),
SAPPHIRE_DEVNET("sapphire_devnet"),
SAPPHIRE_MAINNET("sapphire_mainnet");
}
```
67 changes: 12 additions & 55 deletions docs/sdk/core-kit/sfa-android/initialize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,22 @@ Once you have installed the Web3Auth SDK, the next crucial step is to initialize
requires passing various parameters that align with your project preferences. It's important to note
that the initialization process is critical to the successful use of Web3Auth.

## Create Web3Auth Instance
### Parameters

In your activity, create a `SingleFactorAuth` instance with `SingleFactorAuthArgs`.
In your activity, create a `SingleFactorAuth` instance with `SFAParams`. You can define the Web3Auth
network, client id, and other parameters during initialization.

### Arguments
| Parameter | Description |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `network` | The Web3auth network is to be used by the SDK. Supported values are `Web3AuthNetwork.SAPPHIRE_MAINNET`, `Web3AuthNetwork.SAPPHIRE_DEVNET` ,`Web3AuthNetwork.MAINNET`, `Web3AuthNetwork.TESTNET`, `Web3AuthNetwork.CYAN`, and `Web3AuthNetwork.AQUA` |
| `clientId` | The clientId for your Web3Auth project. You can get it from [Web3Auth dashboard](https://dashboard.web3auth.io/). |
| `sessionTime` | Specifies the session duration in seconds. By default, the value is set to 86400 seconds (1 day), with a maximum session duration of up to 7 days. |
| `serverTimeOffset` | Specify a custom server time offset. The default value is 0. |

#### `SingleFactorAuthArgs`

The SingleFactorAuth Constructor takes `SingleFactorAuthArgs` as input.

| Parameter | Description |
| ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `network` | The Web3auth network is to be used by the SDK. Supported values are `Web3AuthNetwork.MAINNET`, `Web3AuthNetwork.TESTNET`, `Web3AuthNetwork.CYAN`, and `Web3AuthNetwork.AQUA` |
| `clientId` | The clientId for your Web3Auth project. You can get it from [Web3Auth dashboard](https://dashboard.web3auth.io). |

### Instance
### Initialize SingleFactorAuth

```kotlin
// You can get the client id for your Web3Auth project from Web3Auth dashboard.
val singleFactorAuthArgs = SingleFactorAuthArgs(Web3AuthNetwork.MAINNET, "YOUR_WEB3AUTH_CLIENT_ID")
val singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)
```

## Example

```kotlin
class MainActivity : AppCompatActivity() {
// ...
private lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var singleFactorAuthArgs: SingleFactorAuthArgs
private lateinit var loginParams: LoginParams
private var torusKey: TorusKey? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

singleFactorAuthArgs = SingleFactorAuthArgs(Web3AuthNetwork.MAINNET, "YOUR_WEB3AUTH_CLIENT_ID")
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)

// Setup UI and event handlers
val signInButton = findViewById<Button>(R.id.signIn)
signInButton.setOnClickListener { signIn() }
}

private fun signIn() {
loginParams = LoginParams("web3auth-firebase-examples", "sub_value", "id_token")

try {
torusKey = singleFactorAuth.getKey(loginParams).get()
} catch (e: ExecutionException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}

println("""Private Key: ${torusKey.privateKey?.toString(16)}""".trimIndent())
println("""Public Address: ${torusKey.publicAddress}""".trimIndent())
}
//...
}
val sfaParams = SFAParams(Web3AuthNetwork.SAPPHIRE_MAINNET, "YOUR_WEB3AUTH_CLIENT_ID")
val singleFactorAuth = SingleFactorAuth(sfaParams)
```
8 changes: 4 additions & 4 deletions docs/sdk/core-kit/sfa-android/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Then, in your app-level `build.gradle` dependencies section, add the following:
dependencies {
// ...
// focus-next-line
implementation 'com.github.web3auth:single-factor-auth-android:0.4.0'
implementation 'com.github.web3auth:single-factor-auth-android:1.0.0'
}
```

Expand All @@ -40,11 +40,11 @@ and update accordingly.

## Permissions

Open your app's `AndroidManifest.xml` file and add the following permission:
Open your app's `AndroidManifest.xml` file and add the INTERNET permission.

Please note, the `<uses-permission>` element must be a direct child of the `<manifest>` root element

```xml
// focus-next-line
<uses-permission android:name="android.permission.INTERNET" />
```

> Note: The `<uses-permission>` element must be a direct child of the `<manifest>` root element
152 changes: 60 additions & 92 deletions docs/sdk/core-kit/sfa-android/usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@ description: "Web3Auth Core Kit Single Factor Auth Android SDK - Usage | Documen
import TabItem from "@theme/TabItem";
import Tabs from "@theme/Tabs";

After successfully installing and initializing `SingleFactorAuth`, you can use it to authenticate
your users and obtain their private and public keys.
After successfully installing and initializing SingleFactorAuth, you can use it to authenticate your
users and obtain their private and public keys.

The `SingleFactorAuth` instance natively provides the following functions:
:::tip NOTE

## `getKey` method
Web3Auth SFA iOS SDK only works for users who have **not enabled MFA**. For MFA enabled users,
you'll see an Error message.

:::

The `getKey` method returns the torus key using the `verifier`, `verifierId` & `idToken`. On a
successful call the function returns a `TorusKey` instance.
The SingleFactorAuth instance natively provides the following methods:

### Arguments
- [connect](#login-user) - Use to login user and retrive private key pair.
- [initialize](#session-management) - This method helps to achieve session management. It
authenticates user if the session is present, avoiding re-logging.

| Variable | Description |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| `loginParams` | Parameters for the login. It takes `LoginParams` as the value. |
| `context?` | Android activity context |
| `sessionTime` | The `sessionTime` parameter is used for session management. The default value is 1 day ~ 86400, and max value is 7 days. It takes `Long` as a value. |
## Login User

#### `LoginParams`
To obtain a user's private key using the Web3Auth SFA Android SDK, you can call the `connect`
method. The method accepts `LoginParams`, and returns `SFAKey`.

### Parameters

<Tabs
defaultValue="table"
Expand All @@ -50,29 +53,34 @@ successful call the function returns a `TorusKey` instance.

```kotlin
class LoginParams {
val verifier: String
val verifierId: String
val idToken: String
var subVerifierInfoArray: Array<TorusSubVerifierInfo>? = null

constructor(verifier: String, verifierId: String, idToken: String) {
this.verifier = verifier
this.verifierId = verifierId
this.idToken = idToken
}
val verifier: String
val verifierId: String
val idToken: String
var subVerifierInfoArray: Array<TorusSubVerifierInfo>? = null

constructor(verifier: String, verifierId: String, idToken: String) {
this.verifier = verifier
this.verifierId = verifierId
this.idToken = idToken
}

constructor(
verifier: String,
verifierId: String,
idToken: String,
subVerifierInfoArray: Array<TorusSubVerifierInfo>
) {
this.verifier = verifier
this.verifierId = verifierId
this.idToken = idToken
this.subVerifierInfoArray = subVerifierInfoArray
constructor(
verifier: String,
verifierId: String,
idToken: String,
subVerifierInfoArray: Array<TorusSubVerifierInfo>
) {
this.verifier = verifier
this.verifierId = verifierId
this.idToken = idToken
this.subVerifierInfoArray = subVerifierInfoArray
}
}

class TorusSubVerifierInfo(
var verifier: String,
var idToken: String
)
```

</TabItem>
Expand All @@ -81,70 +89,30 @@ class LoginParams {

### Usage

```kotlin title="Usage"
val loginParams = LoginParams("YOUR_VERIFIER_NAME", "YOUR_VERIFIER_ID_VALUE", "YOUR_ID_TOKEN");
val torusKey = singleFactorAuth.getKey(loginParams).get();
```kotlin
val loginParams = LoginParams(
"YOUR_VERIFIER_NAME",
"YOUR_VERIFIER_ID_VALUE",
"YOUR_ID_TOKEN"
);
// focus-next-line
val sfaKey = singleFactorAuth.connect(loginParams);
```

:::tip Note
## Session Management

Web3Auth SFA Android SDK only works for users who have **not enabled MFA**. For MFA enabled users,
you'll see an Error message.
We have included Session Management in this SDK, so calling the initialize function to get the
SFAKey value without re-logging in the user if a user has an active session will return the SFAKey,
otherwise, it will throw an error.

:::
### Parameter

### Example
| Parameter | Description |
| --------- | ---------------------------------- |
| `ctx` | Takes in android activity context. |

```kotlin
import android.widget.Button
import android.os.Bundle
import com.github.web3auth.singlefactorauth.SingleFactorAuth
import com.github.web3auth.singlefactorauth.types.SingleFactorAuthArgs
import com.github.web3auth.singlefactorauth.types.LoginParams
import com.github.web3auth.singlefactorauth.types.TorusKey
import org.torusresearch.fetchnodedetails.types.TorusNetwork
import com.auth0.android.jwt.JWT

class MainActivity : AppCompatActivity() {
// ...
private lateinit var singleFactorAuth: SingleFactorAuth
private lateinit var singleFactorAuthArgs: SingleFactorAuthArgs
private lateinit var loginParams: LoginParams
private var torusKey: TorusKey? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

singleFactorAuthArgs = SingleFactorAuthArgs(Web3AuthNetwork.MAINNET)
singleFactorAuth = SingleFactorAuth(singleFactorAuthArgs)

// Setup UI and event handlers
val signInButton = findViewById<Button>(R.id.signIn)
signInButton.setOnClickListener { signIn() }
}
### Usage

private fun signIn() {
val jwt = JWT(idToken) // idToken is the JWT token obtained from auth provider or your custom JWT provider
val sub = jwt.getClaim("sub").asString() // get sub claims
loginParams = LoginParams("w3a-firebase-demo", "$sub", "$idToken")
try {
torusKey = singleFactorAuth.getKey(loginParams).get()
} catch (e: ExecutionException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}
println("""Private Key: ${torusKey!!.privateKey.toString(16)}""".trimIndent())
println("""Public Address: ${torusKey!!.publicAddress}""".trimIndent())
}
//...
}
```kotlin
val sfakey = singleFactorAuth.initialize(this.applicationContext)
```

:::info Example

You can have a look at the **[this example](/examples?product=Core+Kit&sdk=SFA+Android)** made on
top of this SDK and try it out yourself.

:::
5 changes: 4 additions & 1 deletion sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,10 @@ const sidebars: SidebarsConfig = {
{
type: "category",
label: "Migration Guides",
items: ["migration-guides/sfa-android-v0.1.0-to-v0.3.0"],
items: [
"migration-guides/sfa-android-v0.4.0-to-v1",
"migration-guides/sfa-android-v0.1.0-to-v0.3.0",
],
},
...sdkQuickLinks,
],
Expand Down
2 changes: 1 addition & 1 deletion src/common/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const pnpUnityVersion = `5.x.x`;
export const pnpUnrealVersion = `4.1.x`;

export const sfaWebVersion = `8.0.x`;
export const sfaAndroidVersion = `0.0.6`;
export const sfaAndroidVersion = `1.1.0`;
export const sfaIOSVersion = `8.0.0`;
export const sfaRNVersion = `2.0.x`;
export const sfaFlutterVersion = `5.1.0`;
Expand Down
Loading