Skip to content
Draft
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
4 changes: 2 additions & 2 deletions .github/workflows/android-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ jobs:

- name: Rename the rust lib for uniffi compatibility
working-directory: ./rust/target/${{ env.TARGET }}/release
run: mv ./libzingo.so ./libuniffi_zingo.so
run: mv ./libzingo.so ./libzingo.so

- name: Upload native rust
uses: actions/upload-artifact@v4
with:
name: native-android-uniffi-${{ env.ARCH }}-${{ env.CACHE-KEY }}
path: rust/target/${{ env.TARGET }}/release/libuniffi_zingo.so
path: rust/target/${{ env.TARGET }}/release/libzingo.so

cache-native-android-uniffi:
name: Cache native rust
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ jobs:
sudo mkdir -p ../../android/app/src/main/jniLibs/x86_64
sudo mkdir -p ../../android/app/build/generated/source/uniffi/debug/java/uniffi/zingo
sudo mkdir -p ../../android/app/build/generated/source/uniffi/release/java/uniffi/zingo
sudo cp /opt/jniLibs/x86_64/libuniffi_zingo.so ../../android/app/src/main/jniLibs/x86_64/libuniffi_zingo.so
sudo cp /opt/jniLibs/x86/libuniffi_zingo.so ../../android/app/src/main/jniLibs/x86/libuniffi_zingo.so
sudo cp /opt/jniLibs/armeabi-v7a/libuniffi_zingo.so ../../android/app/src/main/jniLibs/armeabi-v7a/libuniffi_zingo.so
sudo cp /opt/jniLibs/arm64-v8a/libuniffi_zingo.so ../../android/app/src/main/jniLibs/arm64-v8a/libuniffi_zingo.so
sudo cp /opt/jniLibs/x86_64/libzingo.so ../../android/app/src/main/jniLibs/x86_64/libzingo.so
sudo cp /opt/jniLibs/x86/libzingo.so ../../android/app/src/main/jniLibs/x86/libzingo.so
sudo cp /opt/jniLibs/armeabi-v7a/libzingo.so ../../android/app/src/main/jniLibs/armeabi-v7a/libzingo.so
sudo cp /opt/jniLibs/arm64-v8a/libzingo.so ../../android/app/src/main/jniLibs/arm64-v8a/libzingo.so
sudo cp /opt/jniLibs/kotlin/zingo.kt ../../android/app/build/generated/source/uniffi/debug/java/uniffi/zingo/zingo.kt
sudo cp /opt/jniLibs/kotlin/zingo.kt ../../android/app/build/generated/source/uniffi/release/java/uniffi/zingo/zingo.kt

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ios/vendor/
# Exclude compiled libs
android/app/src/main/jniLibs
android/app/release/
ios/libuniffi_zingo.a
ios/libzingo.a
ios/zingo.swift
ios/zingoFFI.h
ios/zingoFFI.modulemap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MainApplication : Application(), ReactApplication {
}

init {
System.loadLibrary("uniffi_zingo")
System.loadLibrary("zingo")
}
}
}
145 changes: 95 additions & 50 deletions android/app/src/main/java/org/ZingoLabs/ZingoDelegator/RPCModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,39 @@ class RPCModule internal constructor(private val reactContext: ReactApplicationC
}

fun saveWalletFile(): Boolean {
try {
return try {
uniffi.zingo.initLogging()

// Get the encoded wallet file
val b64encoded: String = uniffi.zingo.saveToB64()
if (b64encoded.lowercase().startsWith(ErrorPrefix.value)) {
// with error don't save the file. Obviously.
Log.e("MAIN", "Error: [Native] Couldn't save the wallet. $b64encoded")
return false
val b64encoded = uniffi.zingo.saveToB64()

// No data to save
if (b64encoded.isNullOrEmpty()) {
Log.i("MAIN", "[Native] No need to save the wallet.")
return true
}
// Log.i("MAIN", b64encoded)

val correct = uniffi.zingo.checkB64(b64encoded)
if (correct == "false") {
Log.e("MAIN", "Error: [Native] Couldn't save the wallet. The Encoded content is incorrect: $b64encoded")
val correct: Boolean = uniffi.zingo.checkB64(b64encoded)
if (!correct) {
Log.e(
"MAIN",
"Error: [Native] Couldn't save the wallet. The encoded content is incorrect."
)
return false
}

// check if the content is correct. Stored Decoded.
val fileBytes = Base64.decode(b64encoded, Base64.NO_WRAP)
Log.i("MAIN", "[Native] file size: ${fileBytes.size} bytes")

if (fileBytes.size > 0) {
if (fileBytes.isNotEmpty()) {
writeFile(WalletFileName.value, fileBytes)
return true
} else {
Log.e("MAIN", "[Native] No need to save the wallet.")
return true
Log.i("MAIN", "[Native] No need to save the wallet (empty file).")
}

true
} catch (e: Exception) {
Log.e("MAIN", "Error: [Native] Unexpected error. Couldn't save the wallet. $e")
return false
Log.e("MAIN", "Error: [Native] Unexpected error. Couldn't save the wallet.", e)
false
}
}

Expand Down Expand Up @@ -140,19 +141,26 @@ class RPCModule internal constructor(private val reactContext: ReactApplicationC
}

@ReactMethod
fun createNewWallet(serveruri: String, chainhint: String, performancelevel: String, minconfirmations: String, promise: Promise) {
fun createNewWallet(
serveruri: String,
chainhint: String,
performancelevel: String,
minconfirmations: String,
promise: Promise,
) {
try {
uniffi.zingo.initLogging()

// Create a seed
val resp = uniffi.zingo.initNew(serveruri, chainhint, performancelevel, minconfirmations.toUInt())
// Log.i("MAIN-Seed", resp)
val result = uniffi.zingo.initNew(
serveruri,
chainhint,
performancelevel,
minconfirmations.toUInt()
)

if (!resp.lowercase().startsWith(ErrorPrefix.value)) {
saveWalletFile()
}
val value = result.value

promise.resolve(resp)
promise.resolve(value)
} catch (e: Exception) {
val errorMessage = "Error: [Native] create new wallet: ${e.localizedMessage}"
Log.e("MAIN", errorMessage, e)
Expand All @@ -161,18 +169,29 @@ class RPCModule internal constructor(private val reactContext: ReactApplicationC
}

@ReactMethod
fun restoreWalletFromSeed(seed: String, birthday: String, serveruri: String, chainhint: String, performancelevel: String, minconfirmations: String, promise: Promise) {
fun restoreWalletFromSeed(
seed: String,
birthday: String,
serveruri: String,
chainhint: String,
performancelevel: String,
minconfirmations: String,
promise: Promise,
) {
try {
uniffi.zingo.initLogging()

val resp = uniffi.zingo.initFromSeed(seed, birthday.toUInt(), serveruri, chainhint, performancelevel, minconfirmations.toUInt())
// Log.i("MAIN", resp)

if (!resp.lowercase().startsWith(ErrorPrefix.value)) {
saveWalletFile()
}

promise.resolve(resp)
val result = uniffi.zingo.initFromSeed(
seed,
birthday.toUInt(),
serveruri,
chainhint,
performancelevel,
minconfirmations.toUInt()
)
val value = result.value

promise.resolve(value)
} catch (e: Exception) {
val errorMessage = "Error: [Native] restore wallet from seed: ${e.localizedMessage}"
Log.e("MAIN", errorMessage, e)
Expand All @@ -181,31 +200,48 @@ class RPCModule internal constructor(private val reactContext: ReactApplicationC
}

@ReactMethod
fun restoreWalletFromUfvk(ufvk: String, birthday: String, serveruri: String, chainhint: String, performancelevel: String, minconfirmations: String, promise: Promise) {
fun restoreWalletFromUfvk(
ufvk: String,
birthday: String,
serveruri: String,
chainhint: String,
performancelevel: String,
minconfirmations: String,
promise: Promise,
) {
try {
uniffi.zingo.initLogging()

val resp = uniffi.zingo.initFromUfvk(ufvk, birthday.toUInt(), serveruri, chainhint, performancelevel, minconfirmations.toUInt())
// Log.i("MAIN", resp)

if (!resp.lowercase().startsWith(ErrorPrefix.value)) {
saveWalletFile()
}

promise.resolve(resp)
val result = uniffi.zingo.initFromUfvk(
ufvk,
birthday.toUInt(),
serveruri,
chainhint,
performancelevel,
minconfirmations.toUInt()
)
val value = result.value

promise.resolve(value)
} catch (e: Exception) {
val errorMessage = "Error: [Native] restore wallet from ufvk: ${e.localizedMessage}"
Log.e("MAIN", errorMessage, e)
promise.resolve(errorMessage)
}
}
}

@ReactMethod
fun loadExistingWallet(serveruri: String, chainhint: String, performancelevel: String, minconfirmations: String, promise: Promise) {
promise.resolve(loadExistingWalletNative(serveruri, chainhint, performancelevel, minconfirmations))
}

fun loadExistingWalletNative(serveruri: String, chainhint: String, performancelevel: String, minconfirmations: String): String {
// TODO: https://github.com/zingolabs/crosslink-mobile-client/issues/8
fun loadExistingWalletNative(
serveruri: String,
chainhint: String,
performancelevel: String,
minconfirmations: String
): String {
try {
// Read the file
val fileBytes = readFile(WalletFileName.value)
Expand Down Expand Up @@ -367,16 +403,25 @@ class RPCModule internal constructor(private val reactContext: ReactApplicationC

Log.i("MAIN", "file size: $middle8w")

val resp = uniffi.zingo.initFromB64(fileb64.toString(), serveruri, chainhint, performancelevel, minconfirmations.toUInt())

return resp
val result = uniffi.zingo.initFromB64(
fileb64.toString(),
serveruri,
chainhint,
performancelevel,
minconfirmations.toUInt()
)

// UniFFI now returns InitResult; JS expects a String here,
// so we keep returning the underlying value.
return result.value
} catch (e: Exception) {
val errorMessage = "Error: [Native] load existing wallet: ${e.localizedMessage}"
Log.e("MAIN", errorMessage, e)
return errorMessage
}
}


@ReactMethod
fun restoreExistingWalletBackup(promise: Promise) {
val fileBytesBackup: ByteArray
Expand Down Expand Up @@ -504,7 +549,7 @@ class RPCModule internal constructor(private val reactContext: ReactApplicationC
CoroutineScope(Dispatchers.IO).launch {
try {
uniffi.zingo.initLogging()
val resp = uniffi.zingo.getLatestBlockServer(serveruri)
val resp = uniffi.zingo.getLatestBlockHeightServer(serveruri)

withContext(Dispatchers.Main) {
promise.resolve(resp)
Expand Down
Loading
Loading