Skip to content
Closed
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
121 changes: 121 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,57 @@ You do not need to request camera permission in your app code before calling `st

Workflows can include a **Bank** step (the person links a bank account) and a **Location** step (the device position is read once, with the person's consent). Both screens are drawn by the native SDKs, so this package needs no new API: they run inside `startVerification()` / `startVerificationWithWorkflow()`, and the outcome comes back as the same `VerificationResult`.

### Bank Link

Bank Link is the standalone bank product: it opens the connection flow on its own, outside a verification session, and keeps reading balances and transactions for as long as the person allows (or reads once, for a one-time link). Every screen, state and string is native; this package only carries the result and the events, under the same names, events and payload keys as native and the web SDK.

Create the link on your backend with `POST /v3/bank/links/` and pass its `client_token` (`blt_...`) to the app. **The API key stays on the server: it never reaches your app.**

```tsx
import {
presentBankLink,
onBankLinkEvent,
type BankLinkResult,
} from '@didit-protocol/sdk-react-native';

const stop = onBankLinkEvent(({ name, payload }) => {
console.log(name, payload.link_id);
});

try {
const result: BankLinkResult = await presentBankLink(clientToken);
if (result.type === 'success') {
console.log('Linked', result.connectionId);
} else {
console.log('Ended', result.reason, result.step);
}
} finally {
stop();
}
```

Resume after relaunch, when the person was at their bank while the app was closed:

```tsx
import { resumeBankLink } from '@didit-protocol/sdk-react-native';

const resumed = await resumeBankLink(); // null when nothing is pending
if (resumed?.type === 'success') {
console.log('Linked', resumed.connectionId, 'resumed:', resumed.resumed);
}
```

If your bank return is an app link or universal link, hand every address the app is opened with to the SDK; it decides whether the address is a link return, and the server decides the screen:

```tsx
import { handleBankLinkReturn } from '@didit-protocol/sdk-react-native';

// onOpenURL / onNewIntent
handleBankLinkReturn(url);
```

`presentBankLink` presents from the current view controller or activity, and `resumeBankLink` takes the same single-link slot, so a second call of either while a link is open rejects instead of replacing the first one's result.

### Bank

No setup. The native SDKs open the bank's page (or a page Didit hosts) in the system browser, so there is no permission, URL scheme or deep link to register.
Expand Down Expand Up @@ -826,6 +877,62 @@ Fetch the current state of a transaction previously submitted with the same toke

Returns: `Promise<DiditTransactionResult>`

### `presentBankLink(token, options?)`

Open a Bank Link. Every screen, state and string is native.

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `token` | `string` | Yes | The link's `client_token` (`blt_...`) from `POST /v3/bank/links/` |
| `options` | `BankLinkOptions` | No | `config` (`DiditConfig`) for language, font and logging |

Returns: `Promise<BankLinkResult>`, resolved once, as `{ type: 'success', linkId, connectionId?, checkId?, resumed? }` or `{ type: 'exit', reason, step?, resumed? }`. `reason` is one of `closed`, `cancelled`, `bank_unavailable`, `expired`, `failed`.

Throws: when a link is already open, or when the native result cannot be read.

### `resumeBankLink()`

Pick up a link whose bank hop outlived the app, for example after the app was killed in the background. Call it once at start-up.

Returns: `Promise<BankLinkResult | null>` - the result with `resumed: true`, or `null` when nothing is pending.

On Android it waits up to 30 seconds for the host activity when the call arrives before it is attached, then rejects with `no_activity`; it also rejects while a link is already open, so it never replaces the callbacks of a presentation in flight.

### `onBankLinkEvent(listener)`

Listen to the events of a Bank Link. Subscribe before `presentBankLink` to see `opened` and every step after it.

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `listener` | `(event: BankLinkEvent) => void` | Yes | Called once per event |

Returns: `() => void` - the unsubscribe function.

| Event | Payload keys |
|-------|--------------|
| `opened` | `link_id`, `mode`, `access`, `presentation` |
| `consent_shown` | `link_id`, `consent_version` |
| `consent_accepted` | `link_id`, `consent_version` |
| `institution_selected` | `link_id`, `institution_id`, `country` |
| `bank_redirect` | `link_id`, `via` |
| `bank_returned` | `link_id`, `result` |
| `accounts_selected` | `link_id`, `count` |
| `success` | `link_id`, `connection_id?`, `check_id?` |
| `exit` | `link_id`, `reason`, `step` |
| `error` | `link_id`, `code` |

Every payload includes `link_id`, which is `null` until the ID is known. Replayed terminal events may also carry `resumed`. Payloads carry ids only: never a name, an account number, a token or an email. `success` and `exit` fire at most once each, and never both for one link.

### `handleBankLinkReturn(url)`

Hand an address the app was opened with to the SDK, for hosts whose bank return is an app link or universal link. Call it from `onOpenURL`/`onNewIntent` with every address: native decides whether it is a link return.

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `url` | `string` | Yes | The address the app was opened with |

Returns: `void`.

## Exported Types

```tsx
Expand All @@ -835,6 +942,10 @@ import {
startVerificationWithWorkflow,
submitTransaction,
getTransaction,
presentBankLink,
resumeBankLink,
onBankLinkEvent,
handleBankLinkReturn,

// Enum
VerificationStatus,
Expand Down Expand Up @@ -863,6 +974,16 @@ import {
type DiditTransactionResult,
type DiditTransactionOptions,
type DiditTransactionErrorCode,
type BankLinkOptions,
type BankLinkResult,
type BankLinkSuccess,
type BankLinkExit,
type BankLinkExitReason,
type BankLinkStep,
type BankLinkEvent,
type BankLinkEventName,
type BankLinkEventPayload,
type BankLinkEventListener,
} from '@didit-protocol/sdk-react-native';
```

Expand Down
13 changes: 13 additions & 0 deletions SdkReactNative.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ if package.dig("diditNativeSdkVersions", "source")
raise "The pinned native source requires CocoaPods linkage." if didit_sdk_ios_linkage == "spm"
end

# Bank Link is only in the native source pin until a released SDK version
# carries it; `diditNativeSdkVersions.bankLink` marks that release. Without
# either, the bridge compiles a stand-in that rejects the calls, because the
# released SDK it links against has no Bank Link API.
didit_sdk_bank_link_enabled = (
!package.dig("diditNativeSdkVersions", "source").nil? ||
package.dig("diditNativeSdkVersions", "bankLink") == true
)

unless ["spm", "cocoapods"].include?(didit_sdk_ios_linkage)
raise "Invalid DiditSdk iOS linkage '#{didit_sdk_ios_linkage}'. Set $DiditSdkIosLinkage or DIDIT_SDK_IOS_LINKAGE to one of: spm, cocoapods."
end
Expand Down Expand Up @@ -116,6 +125,10 @@ Pod::Spec.new do |s|
].join(" ")
}

if didit_sdk_bank_link_enabled
s.pod_target_xcconfig["SWIFT_ACTIVE_COMPILATION_CONDITIONS"] = "$(inherited) DIDIT_BANK_LINK"
end

if didit_sdk_ios_linkage == "spm"
spm_dependency(s,
url: "https://github.com/didit-protocol/sdk-ios.git",
Expand Down
12 changes: 12 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ if (nativePins.source && !gradle.ext.has("diditNativeSourceRepository")) {
throw new GradleException("Apply the wrapper android/native-source.gradle in settings.gradle to use the pinned native source.")
}

// Bank Link is only in the native source pin until a released SDK version
// carries it; `diditNativeSdkVersions.bankLink` marks that release. Without
// either, the module builds against a released SDK that has no Bank Link API,
// so compile the released-only stand-in instead of the native calls.
def bankLinkEnabled = nativePins.source != null || nativePins.bankLink == true

def diditSdkAndroidVariantProperty = (
rootProject.findProperty("diditSdkAndroidVariant")
?: findProperty("diditSdkAndroidVariant")
Expand Down Expand Up @@ -111,6 +117,12 @@ android {
jvmTarget = "17"
}

sourceSets {
main {
java.srcDirs += bankLinkEnabled ? "src/bankLink/java" : "src/released/java"
}
}

if (diditSdkAndroidUsesNfc) {
configurations.configureEach {
exclude group: 'org.bouncycastle', module: 'bcprov-jdk15to18'
Expand Down
85 changes: 85 additions & 0 deletions android/src/bankLink/java/com/sdkreactnative/BankLinkJson.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.sdkreactnative

import me.didit.sdk.features.bank.link.BankLinkEvent
import me.didit.sdk.features.bank.link.BankLinkResult
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
* The Bank Link values the TurboModule hands JavaScript.
*
* An event travels as `{ name, payload }`, with the wire name of the link
* contract (`opened`, `consent_shown`, ..., `error`) and its ids-only payload;
* a presentation travels as one result object. JavaScript maps both, so the
* keys here are the ones `src/bankLink.ts` reads and nothing else.
*
* This file, [BankLinkSupport] and the module's three Bank Link overrides are
* the only places that touch the native Bank Link API; they are reconciled
* with that API when the native version that carries it is pinned.
*/
internal object BankLinkJson {

fun event(event: BankLinkEvent): String = try {
JSONObject()
.put("name", event.name.wire)
.put("payload", payloadJson(event.payload))
.toString()
} catch (e: JSONException) {
// A payload the JSON writer cannot carry must not throw inside the
// native event callback: the event still arrives, with what is safe.
JSONObject()
.put("name", event.name.wire)
.put("payload", JSONObject().put("link_id", JSONObject.NULL))
.toString()
}

/**
* An event payload as JSON. Mirrors the iOS `jsonSafe` guard: strings,
* numbers, booleans, arrays and maps travel; anything else becomes `null`
* instead of throwing inside the callback.
*/
private fun payloadJson(payload: Map<String, Any?>): JSONObject {
val json = JSONObject()
for ((key, value) in payload) {
json.put(key, jsonSafe(value))
}
return json
}

private fun jsonSafe(value: Any?): Any = when (value) {
null -> JSONObject.NULL
is String, is Boolean -> value
is Double -> if (value.isFinite()) value else JSONObject.NULL
is Float -> if (value.isFinite()) value else JSONObject.NULL
is Number -> value
is Map<*, *> -> JSONObject().also { json ->
for ((key, nested) in value) {
if (key is String) json.put(key, jsonSafe(nested))
}
}
is Iterable<*> -> JSONArray().also { array ->
for (element in value) array.put(jsonSafe(element))
}
else -> JSONObject.NULL
}

fun result(result: BankLinkResult): String = when (result) {
is BankLinkResult.Success -> JSONObject()
.put("type", "success")
.put("linkId", result.success.linkId)
.put("connectionId", result.success.connectionId ?: JSONObject.NULL)
.put("checkId", result.success.checkId ?: JSONObject.NULL)
.put("resumed", result.success.resumed)
.toString()

is BankLinkResult.Exit -> exit(result.exit.reason.wire, result.exit.step?.wire, result.exit.resumed)
}

fun exit(reason: String, step: String?, resumed: Boolean): String = JSONObject()
.put("type", "exit")
.put("reason", reason)
.put("step", step ?: JSONObject.NULL)
.put("resumed", resumed)
.toString()
}
52 changes: 52 additions & 0 deletions android/src/bankLink/java/com/sdkreactnative/BankLinkSupport.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.sdkreactnative

import android.app.Activity
import android.net.Uri
import me.didit.sdk.Configuration
import me.didit.sdk.DiditSdk

/**
* The native Bank Link calls, compiled only when the pinned native SDK carries
* Bank Link (a source pin, or `diditNativeSdkVersions.bankLink` once released).
*
* The released-only build compiles the counterpart in `src/released` instead,
* so the module keeps building against a native SDK that predates Bank Link.
*/
internal object BankLinkSupport {

const val available = true

/** The JSON `null` a resume answers when nothing is pending. */
const val NONE = "null"

fun present(
activity: Activity,
token: String,
configuration: Configuration?,
onEvent: (String) -> Unit,
onResult: (String) -> Unit
) {
val presented = DiditSdk.presentBankLink(
activity = activity,
token = token,
configuration = configuration,
onEvent = { event -> onEvent(BankLinkJson.event(event)) },
onResult = { result -> onResult(BankLinkJson.result(result)) }
)
check(presented) { "Bank Link presentation was declined." }
}

fun resume(
activity: Activity,
onEvent: (String) -> Unit,
onResult: (String) -> Unit
): Boolean = DiditSdk.resumeBankLink(
activity = activity,
onEvent = { event -> onEvent(BankLinkJson.event(event)) },
onResult = { result -> onResult(BankLinkJson.result(result)) }
)

fun handle(uri: Uri) {
DiditSdk.handleBankLinkReturn(uri)
}
}
Loading
Loading