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

MPC Core Kit Swift docs #699

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
183 changes: 183 additions & 0 deletions docs/sdk/core-kit/mpc-core-kit-ios/authentication.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
---
title: "Authentication in MPC Core Kit iOS SDK"
sidebar_label: "Authentication"
displayed_sidebar: sdk
description: "Web3Auth MPC Core Kit iOS SDK - Authentication | Documentation - Web3Auth"
---

import ServiceWorkerCode from "@site/src/common/sdk/core-kit/tkey/_sw-js.mdx";
import TabItem from "@theme/TabItem";
import Tabs from "@theme/Tabs";

There are two ways to login your users, depending on the type of authentication method you've
chosen. If you are looking for an Authentication Flow in your application (like Auth0 SPA Flow), you
can use the `loginWithOAuth()` function. If you are looking to pass a JWT-based IdToken to the SDK
from your application (like Auth0 RWA Flow or even BYOA JWT provider), you can use the
`loginWithJWT()` function.

As a prerequisite, before triggering the login function, you need to create a verifier for your
login method on the [Web3Auth Dashboard](https://dashboard.web3auth.io).

## Creating a Verifier

Since this is a Core Kit SDK, it does not provide any default authentication methods. You need to
create a custom verifier to use this SDK. This means that you need to authenticate users with your
own custom authentication service. For example, while authenticating with Google, you have to use
your own Google Client ID and Dashboard to authenticate users directly or use aggregate services
like Auth0, Firebase, AWS Cognito etc. Additionally, you can make your own JWT token authentication
system and pass over the ID Token to Web3Auth to generate a private key for them.

For enabling this, you need [Create a Verifier](/auth-provider-setup/verifiers) from the **Custom
Auth** section of the [Web3Auth Developer Dashboard](https://dashboard.web3auth.io) with your
desired configuration.

:::tip

If you want to know more about setting up a verifier and how to use it, please refer to the
[Custom Authentication Documentation](/features/custom-authentication).

:::

:::warning

Core Kit SDK only supports Sapphire Mainnet and Devnet networks. The other networks don't support
MPC functionalities.

:::

## Log In with OAuth

If you wish to have SPA flow, similar to Auth0, you can use `loginWithOAuth` function.

### Usage

```swift
let result = try await mpcCoreKit.loginWithOAuth(
loginProvider: .google,
clientId: "Your Google Client id",
verifier: "Your Custom Verifier name"
);
```

### Arguements

| Variable | Description |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `loginProvider` | Defines the login provider to be used. Supported values are `LoginProviders.google`, `LoginProviders.facebook`, `LoginProviders.twitch`, `LoginProviders.reddit`, `LoginProviders.discord`, `LoginProviders.apple`, `LoginProviders.github`, `LoginProviders.linkedin`, `LoginProviders.kakao`, `LoginProviders.twitter`, `LoginProviders.weibo`, `LoginProviders.line`, `LoginProviders.wechat`, `LoginProviders.email_password`, `LoginProviders.jwt` |
| `clientId` | Client id for respective `loginProvider`. For instance google client id, auth0 client id, and etc. |
| `verifier` | Verifier name from the Web3Auth Dashboard. |
| `jwtParams` | JWT parameters. It takes `[Stirng: Stirng]` as an input. The default value is `[:]`. |
| `redirectURL` | |
| `browserRedirectURL` | |

### Result

The `loginWith0Auth`returns `MpcKeyDetails` upon success, which has infomration regarding
thresholds, required factors, and etc.

<Tabs
defaultValue="table"
values={[
{ label: "Table", value: "table" },
{ label: "Class", value: "class" },
]}
>

<TabItem value="table">

| Parameter | Description |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `tssPubKey` | It holds the value for the TSS PubKey in compressed form. The type of `tssPubKey` is `String`. |
| `metadataPubKey` | It holds the value for metadata PubKey which is used for interacting with metadata layer. |
| `requiredFactors` | It defines the total factors still required for SDK to be in write mode. If required factors are greater than 0, the SDK will be in read mode. You can use `inputFactor` function to input factor and refresh the instance. |
| `threshold` | Defines the threshold set by the SDK. Ideally it would be 2. |
| `total_shares` | Defines the total number of shares for user. Initially if the hashed cloud factor is set, it would be 2. |

</TabItem>

<TabItem value = "class">
```swift
public struct MpcKeyDetails : Codable {
public let tssPubKey : String
public let metadataPubKey: String
public let requiredFactors: Int32
public let threshold: UInt32
public let shareDescriptions : String
public let total_shares: UInt32
}
```
</TabItem>
</Tabs>

## Log In with JWT (BYOA)

If you are looking to pass a JWT-based IdToken to the SDK from your application, you can use the
`loginWithJWT()` function.

### Usage

```swift
let result = try await mpcCoreKit.loginWithJwt(
verifier: "Your verifier name from Web3Auth Dashboard",
verifierId: "Your verifierd Id value",
idToken: "Your JWT id Token"
)
```

### Arguements

| Argument | Description |
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
| `verifier` | Your verifier name from Web3Auth dashboard. |
| `verifierId` | Your verifier id value, i.e. email, sub or custom. Make sure you have selected correct verifier id while creating the custom verifier. |
| `idToken` | Your JWT id token for verification. |

### Result

The `loginWithJWT`returns `MpcKeyDetails` upon success, which has infomration regarding thresholds,
required factors, and etc.

<Tabs
defaultValue="table"
values={[
{ label: "Table", value: "table" },
{ label: "Class", value: "class" },
]}
>

<TabItem value="table">

| Parameter | Description |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `tssPubKey` | It holds the value for the TSS PubKey in compressed form. The type of `tssPubKey` is `String`. |
| `metadataPubKey` | It holds the value for metadata PubKey which is used for interacting with metadata layer. |
| `requiredFactors` | It defines the total factors still required for SDK to be in write mode. If required factors are greater than 0, the SDK will be in read mode. You can use `inputFactor` function to input factor and refresh the instance. |
| `threshold` | Defines the threshold set by the SDK. Ideally it would be 2. |
| `total_shares` | Defines the total number of shares for user. Initially if the hashed cloud factor is set, it would be 2. |

</TabItem>

<TabItem value = "class">
```swift
public struct MpcKeyDetails : Codable {
public let tssPubKey : String
public let metadataPubKey: String
public let requiredFactors: Int32
public let threshold: UInt32
public let shareDescriptions : String
public let total_shares: UInt32
}
```
</TabItem>
</Tabs>

## Logging out the User

To disconnect user's connected wallet/ provider and log them out of the Web3Auth MPC Core Kit SDK
you can use `logout` function.

### Usage

```swift
try await mpcCoreKit.logout();
```
105 changes: 105 additions & 0 deletions docs/sdk/core-kit/mpc-core-kit-ios/initialize.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
title: "Initializing MPC Core Kit iOS SDK"
sidebar_label: "Initialize"
displayed_sidebar: sdk
description: "Web3Auth MPC Core Kit iOS SDK - Initialize | Documentation - Web3Auth"
---

import ChainConfig from "@site/src/common/sdk/pnp/web/_chain-config.mdx";
import Web3AuthOptions from "@site/src/common/sdk/pnp/web/_web3authcore-options.mdx";
import TabItem from "@theme/TabItem";
import Tabs from "@theme/Tabs";

After Installation, the next step to use Web3Auth MPC Core Kit iOS is to initialize the SDK.

## Instantiating MpcCoreKit

### Creating a class to conform ILocalStorage

MpcCoreKit takes `ILocalStorage` as an input parameter. The package uses it to store and retrieve
the device factor. `ILocalStorage` being a `protocol` gives us the freedom to choose the local
storage of our choice.

Here we are using `UserDefaults`, but you can choose the storage of your choice.

```swift
import Foundation
import mpc_core_kit_swift

class UserStorage : ILocalStorage {
func get(key: String) async throws -> Data {
guard let data = UserDefaults().value(forKey: key) as? Data else {
return Data()
}
return data
}

func set(key: String, payload: Data) async throws {
UserDefaults().setValue(payload, forKey: key)
}
}

let storage = UserStorage()
```

### Creating MpcCoreKit instance

Once, we have created a class to conform `ILocalStorage` we are good to create an instance of
`MpcCoreKit`. If you want to disable the hashed cloud factor, you can set `disableHashFactor` to
true during the initialization.

```swift
let mpcCoreKit = MpcCoreKit(
web3AuthClientId: "Your Web3Auth Client ID" // Get it from Web3Auth Dashboard,
web3AuthNetwork: .SAPPHIRE_MAINNET,
localStorage: UserStorage()
)
```

#### Arguments

<Tabs
defaultValue="table"
values={[
{ label: "Table", value: "table" },
{ label: "Interface", value: "interface" },
]}
>

<TabItem value="table">

| Parameter | Description |
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `web3AuthClientId` | Your Web3Auth Client Id from [Web3Auth Dashboard](https://dashboard.web3auth.io/). |
| `web3AuthNetwork` | The Web3auth network to be used for MPC Wallet Management. Supported values are `Web3AuthNetwork.SAPPHIRE_MAINNET`, `Web3AuthNetwork.SAPPHIRE_DEVNET`, `Web3AuthNetwork.MAINNET`, `Web3AuthNetwork.TESTNET`, `Web3AuthNetwork.CYAN`, `Web3AuthNetwork.AQUA` |
| `disableHashFactor` | Defines whether to use hashed cloud factor or not. Default value is `false`. |
| `localStorage` | A class which conforms to `ILocalStorage`. Used by the SDK internally to store and retrieve device share. |

</TabItem>

<TabItem value="interface">

```swift
public struct MpcCoreKit {
public init(web3AuthClientId : String , web3AuthNetwork: Web3AuthNetwork, disableHashFactor : Bool = false, localStorage: ILocalStorage ) {
self.option = .init(disableHashFactor: disableHashFactor , Web3AuthClientId: web3AuthClientId, network: web3AuthNetwork)
self.appState = CoreKitAppState.init()

self.network = web3AuthNetwork

self.torusUtils = TorusUtils(
enableOneKey: true,
network: self.network.toTorusNetwork(),
clientId: web3AuthClientId
)

self.nodeDetailsManager = NodeDetailManager(network: self.network.toTorusNetwork())

self.coreKitStorage = .init(storeKey: self.storeKey, storage: localStorage)

}
}
```

</TabItem>
</Tabs>
47 changes: 47 additions & 0 deletions docs/sdk/core-kit/mpc-core-kit-ios/install.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: "Installing MPC Core Kit iOS SDK"
sidebar_label: "Install"
displayed_sidebar: sdk
description: "Web3Auth MPC Core Kit iOS SDK - Install | Documentation - Web3Auth"
---

## MPC Core Kit iOS

This package helps you implement the Web3Auth MPC Features while giving you the flexibility to
customize the UI and UX of the authentication process.

#### Swift Package Manager

1. In Xcode, with your app project open, navigate to **File > Add Packages**.

2. When prompted, add the tKey iOS SDK repository:

```sh
https://github.com/Web3Auth/mpc-core-kit-swift
```

From the `Dependency Rule` dropdown, select `Exact Version` and enter `1.0.0` as the version.

3. When finished, Xcode will automatically begin resolving and downloading your dependencies in the
background.

## Web3 Swift MPC Provider

This package gives access to tss signing capabilities to be used with mpc-core-kit-swift. This comes
in handy by providing you with a standard way of retriving user's address and interacting with
blockchain. As of now, it only supports Ethereum.

#### Swift Package Manager

1. In Xcode, with your app project open, navigate to **File > Add Packages**.

2. When prompted, add the tKey iOS SDK repository:

```sh
https://github.com/tkey/web3-swift-mpc-provider
```

From the `Dependency Rule` dropdown, select `Exact Version` and enter `1.0.0` as the version.

3. When finished, Xcode will automatically begin resolving and downloading your dependencies in the
background.
Loading
Loading