2019-05-23 21:37:17 -07:00
|
|
|
package cash.z.wallet.sdk.jni
|
|
|
|
|
2019-09-26 09:58:37 -07:00
|
|
|
import android.content.Context
|
2019-11-01 13:25:28 -07:00
|
|
|
import cash.z.wallet.sdk.exception.BirthdayException
|
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk
|
2019-09-26 09:58:37 -07:00
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk.OUTPUT_PARAM_FILE_NAME
|
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk.SPEND_PARAM_FILE_NAME
|
2019-11-01 13:25:28 -07:00
|
|
|
import cash.z.wallet.sdk.ext.twig
|
2019-10-21 03:26:02 -07:00
|
|
|
import java.io.File
|
2019-05-23 21:37:17 -07:00
|
|
|
|
|
|
|
/**
|
2019-09-26 09:58:37 -07:00
|
|
|
* Serves as the JNI boundary between the Kotlin and Rust layers. Functions in this class should
|
|
|
|
* not be called directly by code outside of the SDK. Instead, one of the higher-level components
|
|
|
|
* should be used such as Wallet.kt or CompactBlockProcessor.kt.
|
2019-05-23 21:37:17 -07:00
|
|
|
*/
|
2019-11-01 13:25:28 -07:00
|
|
|
class RustBackend : RustBackendWelding {
|
|
|
|
|
|
|
|
internal lateinit var dbDataPath: String
|
|
|
|
internal lateinit var dbCachePath: String
|
|
|
|
internal lateinit var dbNamePrefix: String
|
|
|
|
internal lateinit var paramDestinationDir: String
|
|
|
|
internal var birthdayHeight: Int = -1
|
|
|
|
get() = if (field != -1) field else throw BirthdayException.UninitializedBirthdayException
|
|
|
|
|
|
|
|
fun init(appContext: Context, dbNamePrefix: String = ZcashSdk.DEFAULT_DB_NAME_PREFIX) =
|
|
|
|
init(
|
|
|
|
appContext.getDatabasePath("unused.db").parentFile.absolutePath,
|
|
|
|
"${appContext.cacheDir.absolutePath}/params",
|
|
|
|
dbNamePrefix
|
|
|
|
)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
2019-10-21 03:26:02 -07:00
|
|
|
/**
|
|
|
|
* Loads the library and initializes path variables. Although it is best to only call this
|
|
|
|
* function once, it is idempotent.
|
|
|
|
*/
|
2019-11-01 13:25:28 -07:00
|
|
|
fun init(
|
|
|
|
dbPath: String,
|
|
|
|
paramsPath: String,
|
|
|
|
dbNamePrefix: String = ZcashSdk.DEFAULT_DB_NAME_PREFIX
|
|
|
|
): RustBackend {
|
|
|
|
this.dbNamePrefix = dbNamePrefix
|
2019-09-26 09:58:37 -07:00
|
|
|
twig("Creating RustBackend") {
|
2019-11-01 13:25:28 -07:00
|
|
|
load()
|
|
|
|
dbCachePath = File(dbPath, "${dbNamePrefix}${ZcashSdk.DB_CACHE_NAME}").absolutePath
|
|
|
|
dbDataPath = File(dbPath, "${dbNamePrefix}${ZcashSdk.DB_DATA_NAME}").absolutePath
|
|
|
|
paramDestinationDir = paramsPath
|
2019-09-26 09:58:37 -07:00
|
|
|
}
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2019-10-21 03:26:02 -07:00
|
|
|
fun clear() {
|
|
|
|
twig("Deleting databases")
|
|
|
|
File(dbCachePath).delete()
|
|
|
|
File(dbDataPath).delete()
|
|
|
|
}
|
|
|
|
|
2019-09-26 09:58:37 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// Wrapper Functions
|
|
|
|
//
|
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun initDataDb() = initDataDb(dbDataPath)
|
|
|
|
|
|
|
|
// override fun initAccountsTable(extfvks: Array<String>) =
|
|
|
|
// initAccountsTableWithKeys(dbDataPath, extfvks)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun initAccountsTable(
|
|
|
|
seed: ByteArray,
|
|
|
|
numberOfAccounts: Int
|
|
|
|
) = initAccountsTable(dbDataPath, seed, numberOfAccounts)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
|
|
|
override fun initBlocksTable(
|
|
|
|
height: Int,
|
|
|
|
hash: String,
|
|
|
|
time: Long,
|
|
|
|
saplingTree: String
|
2019-11-01 13:25:28 -07:00
|
|
|
): Boolean {
|
|
|
|
birthdayHeight = height
|
|
|
|
return initBlocksTable(dbDataPath, height, hash, time, saplingTree)
|
|
|
|
}
|
2019-09-26 09:58:37 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun getAddress(account: Int) = getAddress(dbDataPath, account)
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun getBalance(account: Int) = getBalance(dbDataPath, account)
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun getVerifiedBalance(account: Int) = getVerifiedBalance(dbDataPath, account)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun getReceivedMemoAsUtf8(idNote: Long) =
|
2019-09-26 09:58:37 -07:00
|
|
|
getReceivedMemoAsUtf8(dbDataPath, idNote)
|
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun getSentMemoAsUtf8(idNote: Long) = getSentMemoAsUtf8(dbDataPath, idNote)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun validateCombinedChain() = validateCombinedChain(dbCachePath, dbDataPath)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun rewindToHeight(height: Int) = rewindToHeight(dbDataPath, height)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun scanBlocks() = scanBlocks(dbCachePath, dbDataPath)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
|
|
|
override fun createToAddress(
|
|
|
|
account: Int,
|
|
|
|
extsk: String,
|
|
|
|
to: String,
|
|
|
|
value: Long,
|
2019-11-12 08:58:15 -08:00
|
|
|
memo: ByteArray?
|
2019-09-26 09:58:37 -07:00
|
|
|
): Long = createToAddress(
|
|
|
|
dbDataPath,
|
|
|
|
account,
|
|
|
|
extsk,
|
|
|
|
to,
|
|
|
|
value,
|
2019-11-12 08:58:15 -08:00
|
|
|
memo ?: ByteArray(0),
|
2019-09-26 09:58:37 -07:00
|
|
|
"${paramDestinationDir}/$SPEND_PARAM_FILE_NAME",
|
|
|
|
"${paramDestinationDir}/$OUTPUT_PARAM_FILE_NAME"
|
|
|
|
)
|
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun deriveSpendingKeys(seed: ByteArray, numberOfAccounts: Int) =
|
|
|
|
deriveExtendedSpendingKeys(seed, numberOfAccounts)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun deriveViewingKeys(seed: ByteArray, numberOfAccounts: Int) =
|
|
|
|
deriveExtendedFullViewingKeys(seed, numberOfAccounts)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun deriveViewingKey(spendingKey: String) = deriveExtendedFullViewingKey(spendingKey)
|
2019-09-26 09:58:37 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun isValidShieldedAddr(addr: String) =
|
|
|
|
isValidShieldedAddress(addr)
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
override fun isValidTransparentAddr(addr: String) =
|
|
|
|
isValidTransparentAddress(addr)
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
/**
|
|
|
|
* Exposes all of the librustzcash functions along with helpers for loading the static library.
|
|
|
|
*/
|
|
|
|
companion object {
|
|
|
|
private var loaded = false
|
|
|
|
|
|
|
|
fun load() {
|
|
|
|
// It is safe to call these things twice but not efficient. So we add a loose check and
|
|
|
|
// ignore the fact that it's not thread-safe.
|
|
|
|
if (!loaded) {
|
|
|
|
twig("Loading RustBackend") {
|
|
|
|
loadRustLibrary()
|
|
|
|
initLogs()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The first call made to this object in order to load the Rust backend library. All other
|
|
|
|
* external function calls will fail if the libraries have not been loaded.
|
|
|
|
*/
|
|
|
|
private fun loadRustLibrary() {
|
|
|
|
try {
|
|
|
|
System.loadLibrary("zcashwalletsdk")
|
|
|
|
loaded = true
|
|
|
|
} catch (e: Throwable) {
|
|
|
|
twig("Error while loading native library: ${e.message}")
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-06-04 07:15:19 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
//
|
|
|
|
// External Functions
|
|
|
|
//
|
2019-06-04 07:15:19 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
@JvmStatic private external fun initDataDb(dbDataPath: String): Boolean
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
@JvmStatic private external fun initAccountsTable(
|
|
|
|
dbDataPath: String,
|
|
|
|
seed: ByteArray,
|
|
|
|
accounts: Int
|
|
|
|
): Array<String>
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
// @JvmStatic private external fun initAccountsTableWithKeys(
|
|
|
|
// dbDataPath: String,
|
|
|
|
// extfvk: Array<String>
|
|
|
|
// )
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
@JvmStatic private external fun initBlocksTable(
|
|
|
|
dbDataPath: String,
|
|
|
|
height: Int,
|
|
|
|
hash: String,
|
|
|
|
time: Long,
|
|
|
|
saplingTree: String
|
|
|
|
): Boolean
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
@JvmStatic private external fun getAddress(dbDataPath: String, account: Int): String
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
@JvmStatic private external fun isValidShieldedAddress(addr: String): Boolean
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
@JvmStatic private external fun isValidTransparentAddress(addr: String): Boolean
|
|
|
|
|
|
|
|
@JvmStatic private external fun getBalance(dbDataPath: String, account: Int): Long
|
|
|
|
|
|
|
|
@JvmStatic private external fun getVerifiedBalance(dbDataPath: String, account: Int): Long
|
|
|
|
|
|
|
|
@JvmStatic private external fun getReceivedMemoAsUtf8(dbDataPath: String, idNote: Long): String
|
|
|
|
|
|
|
|
@JvmStatic private external fun getSentMemoAsUtf8(dbDataPath: String, idNote: Long): String
|
|
|
|
|
|
|
|
@JvmStatic private external fun validateCombinedChain(dbCachePath: String, dbDataPath: String): Int
|
|
|
|
|
|
|
|
@JvmStatic private external fun rewindToHeight(dbDataPath: String, height: Int): Boolean
|
|
|
|
|
|
|
|
@JvmStatic private external fun scanBlocks(dbCachePath: String, dbDataPath: String): Boolean
|
|
|
|
|
|
|
|
@JvmStatic private external fun createToAddress(
|
|
|
|
dbDataPath: String,
|
|
|
|
account: Int,
|
|
|
|
extsk: String,
|
|
|
|
to: String,
|
|
|
|
value: Long,
|
|
|
|
memo: ByteArray,
|
|
|
|
spendParamsPath: String,
|
|
|
|
outputParamsPath: String
|
|
|
|
): Long
|
|
|
|
|
|
|
|
@JvmStatic private external fun initLogs()
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
@JvmStatic private external fun deriveExtendedSpendingKeys(seed: ByteArray, numberOfAccounts: Int): Array<String>
|
2019-05-23 21:37:17 -07:00
|
|
|
|
2019-11-01 13:25:28 -07:00
|
|
|
@JvmStatic private external fun deriveExtendedFullViewingKeys(seed: ByteArray, numberOfAccounts: Int): Array<String>
|
|
|
|
|
|
|
|
@JvmStatic private external fun deriveExtendedFullViewingKey(spendingKey: String): String
|
|
|
|
}
|
2019-05-23 21:37:17 -07:00
|
|
|
}
|