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

Add React Native package #99

Merged
merged 1 commit into from
Apr 25, 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
484 changes: 441 additions & 43 deletions lib/Cargo.lock

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions lib/ls-sdk-react-native/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
node_modules
**/.DS_Store
# OSX
#
.DS_Store

# XDE
.expo/

# VSCode
.vscode/
jsconfig.json

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IJ
#
.classpath
.cxx
.gradle
.idea
.project
.settings
local.properties
android.iml

# Cocoapods
#
breez_sdk.podspec.*
BreezSDK.podspec
example/ios/Pods

# node.js
#
node_modules/
npm-debug.log
yarn-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
android/app/libs
android/keystores/debug.keystore

# Expo
.expo/*

# generated by bob
lib/

# bindings (used for local development only)
android/src/main/java/com/lssdk/ls_sdk.kt
android/src/main/jniLibs
ios/include/*
ios/libs/*
ios/bindings-swift/**
110 changes: 110 additions & 0 deletions lib/ls-sdk-react-native/DEVELOPING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Setting up a development environment

The Liquid Swap SDK React Native plugin consumes the underlying Liquid Swap SDK from the following sources:

- For iOS: The Liquid Swap SDK Swift bindings are integrated via CocoaPods.
- For Android: The Liquid Swap SDK Android bindings are integrated via Jitpack.

When developing, it can be useful to work with a locally built version of the Liquid Swap SDK instead of relying on what is published already on CocoaPods / Jitpack.
To do this, you first need to build the Liquid Swap SDK bindings locally and then point the plugin to make use of the locally built Liquid Swap SDK bindings.

All the following commands can be run in the `lib/ls-sdk-react-native` directory.

## Prerequisites

Set the ANDROID_NDK_HOME env variable to your SDK home directory:
```
export ANDROID_NDK_HOME=<your android ndk directory>
```

To lint the result of the code generation ktlint, swiftformat and tslint need to be installed:
```bash
brew install kotlin ktlint swiftformat
yarn global add tslint typescript
```

On first usage you will need to run:
```bash
make init
```

## Building the bindings

Then to build and copy the Kotlin, Swift and React Native bindings into the React Native package run:
```bash
make all
```

This will generate the following artifacts:

- iOS
- `ios/LiquidSwapSDKMapper.swift`
- `ios/LiquidSwapSDK.m`
- `ios/LiquidSwapSDK.swift`
- `ios/bindings-swift/ls_sdkFFI.xcframework`
- `ios/bindings-swift/Sources/LiquidSwapSDK/LiquidSwapSDK.swift`
- Android
- `android/src/main/java/com/lssdk/ls_sdk.kt`
- `android/src/main/java/com/lssdk/LiquidSwapSDKMapper.kt`
- `android/src/main/java/com/lssdk/LiquidSwapSDKModule.kt`
- `android/src/main/jniLibs/arm64-v8a/libls_sdk_bindings.so`
- `android/src/main/jniLibs/armeabi-v7a/libls_sdk_bindings.so`
- `android/src/main/jniLibs/x86/libls_sdk_bindings.so`
- `android/src/main/jniLibs/x86_64/libls_sdk_bindings.so`
- Typescript
- `src/index.ts`

### Building for one platform only

You can also build for Android or iOS only, in that case run:
```bash
make android react-native
```
or
```bash
make ios react-native
```

## Using the locally built bindings

To use the locally built bindings instead of integrating them remotely, make the following changes:

- For iOS:
- Rename the podspec files in `lib/ls-sdk-react-native/`:
- Rename `ls_sdk.podspec` to `ls_sdk.podspec.prod`
- Rename `LiquidSwapSDK.podspec.dev` to `LiquidSwapSDK.podspec`
- For Android:
- Comment out the following line from the dependencies section in `lib/ls-sdk-react-native/android/build.gradle`:
- `implementation("com.github.breez:breez-sdk-liquid:${getVersionFromNpmPackage()}") { exclude group:"net.java.dev.jna" }`

Reinstall the dependencies in the example project and run it.
It will now use the locally built bindings.

## Testing with the example app

To test locally built bindings in the example app, the npm dependencies need to be updated to use the local package.
In `lib/ls-sdk-react-native/example/package.json` replace the current version with `file:../`:
```json
"@breeztech/react-native-liquid-swap-sdk": "file:../",
```

Run the npm/yarn install to download dependences for both the react-native-liquid-swap-sdk package and the example app:
```bash
yarn bootstrap
```

Finally in the `lib/ls-sdk-react-native/example/` directory start either the iOS or Android app:
```bash
yarn android
```
or for iOS:
```bash
yarn ios
```

## Troubleshooting

In case you get an error like:
> java.lang.RuntimeException: Unable to load script. Make sure you're either running Metro (run 'npx react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release.

Then manually run `npx react-native start` in the example directory and reload the app.
21 changes: 21 additions & 0 deletions lib/ls-sdk-react-native/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Breez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions lib/ls-sdk-react-native/LiquidSwapSDK.podspec.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "json"

package = JSON.parse(File.read(File.join(__dir__, "package.json")))

Pod::Spec.new do |s|
s.name = "LiquidSwapSDK"
s.version = package["version"]
s.summary = package["description"]
s.homepage = package["homepage"]
s.license = package["license"]
s.authors = package["author"]

s.platforms = { :ios => "11.0" }
s.source = { :git => "https://github.com/breez/breez-sdk-liquid.git", :tag => "#{s.version}" }

s.source_files = "ios/**/*.{h,m,mm,swift}"

s.dependency "React-Core"
s.vendored_frameworks = "ios/bindings-swift/ls_sdkFFI.xcframework"
end
42 changes: 42 additions & 0 deletions lib/ls-sdk-react-native/PUBLISHING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## Build

### Prerequisites
* set the ANDROID_NDK_HOME env variable to your sdk home folder
```
export ANDROID_NDK_HOME=<your android ndk directory>
```

### Building the plugin
On first usage you will need to run:
```
make init
```
Then to generate the React Native code:
```
make react-native
```

### Generated artifacts
* Android
>* android/src/main/java/com/lssdk/LiquidSwapSDKMapper.kt
>* android/src/main/java/com/lssdk/LiquidSwapSDKModule.kt
* iOS
>* ios/LiquidSwapSDKMapper.swift
>* ios/LiquidSwapSDK.m
>* ios/LiquidSwapSDK.swift
* Typescript
>* src/index.ts

### Publish
When publishing, make sure the following are updated:
- Update the version number in `package.json`.
- Set the published version of `@breeztech/react-native-liquid-swap-sdk` in `example/package.json`.

Then login to npm:
```
npm login --@scope=@breeztech
```
Then publish:
```
npm publish --access public
```
78 changes: 78 additions & 0 deletions lib/ls-sdk-react-native/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import groovy.json.JsonSlurper

def getVersionFromNpmPackage() {
def inputFile = new File("$rootDir/../node_modules/@breeztech/react-native-liquid-swap-sdk/package.json")
def packageJson = new JsonSlurper().parseText(inputFile.text)

return packageJson["version"]
}

buildscript {
repositories {
google()
mavenCentral()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21"
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 34
defaultConfig {
minSdkVersion 24
targetSdkVersion 34
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
}
}
lintOptions {
disable 'GradleCompatible'
}
}

repositories {
mavenLocal()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../node_modules/react-native/android")
}
google()
mavenCentral {
// We don't want to fetch react-native from Maven Central as there are
// older versions over there.
content {
excludeGroup "com.facebook.react"
}
}
jcenter()
}

allprojects {
repositories {
google()
}
}

dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.21"
// Due to an issue with Jitpack (https://github.com/jitpack/jitpack.io/issues/5752)
// the Android platform versions of JNA (specifically libjnadispatch.so) are missing when downloading the Liquid Swap SDK from Jitpack.
// Therefore we ignore the version of JNA that comes with the Liquid Swap SDK from Jitpack
// and manually add one that does include the necessary Android platform binaries.
implementation("com.github.breez:breez-sdk-liquid:${getVersionFromNpmPackage()}") { exclude group:"net.java.dev.jna" } // remove for using locally built bindings during development
implementation("net.java.dev.jna:jna:5.14.0@aar")
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading