Rename bdk-kotlin companion project, fix gradle warnings
This commit is contained in:
6
bdk-kotlin/.gitignore
vendored
Normal file
6
bdk-kotlin/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/target
|
||||
.idea
|
||||
.gradle
|
||||
local.properties
|
||||
build
|
||||
*.so
|
||||
73
bdk-kotlin/aar/build.gradle
Normal file
73
bdk-kotlin/aar/build.gradle
Normal file
@@ -0,0 +1,73 @@
|
||||
apply plugin: 'com.android.library'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
android {
|
||||
compileSdkVersion 30
|
||||
buildToolsVersion "29.0.3"
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 30
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles 'consumer-rules.pro'
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//task buildRust(type: Exec) {
|
||||
// workingDir '../'
|
||||
// commandLine './build.sh'
|
||||
//}
|
||||
|
||||
afterEvaluate {
|
||||
// android.libraryVariants.all { variant ->
|
||||
// variant.javaCompileProvider.get().dependsOn(buildRust)
|
||||
// }
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
// Creates a Maven publication called "release".
|
||||
release(MavenPublication) {
|
||||
// Applies the component for the release build variant.
|
||||
from components.release
|
||||
|
||||
// You can then customize attributes of the publication as shown below.
|
||||
groupId = 'org.bitcoindevkit.bdkffi'
|
||||
artifactId = 'bdk'
|
||||
version = '0.0.1-dev'
|
||||
}
|
||||
// Creates a Maven publication called “debug”.
|
||||
debug(MavenPublication) {
|
||||
// Applies the component for the debug build variant.
|
||||
from components.debug
|
||||
|
||||
groupId = 'org.bitcoindevkit.bdkffi'
|
||||
artifactId = 'bdk-debug'
|
||||
version = '0.0.1-dev'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation project(':jar')
|
||||
api 'net.java.dev.jna:jna:5.8.0@aar'
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
implementation 'androidx.appcompat:appcompat:1.3.0'
|
||||
implementation 'androidx.core:core-ktx:1.5.0'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
|
||||
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1'
|
||||
}
|
||||
0
bdk-kotlin/aar/consumer-rules.pro
Normal file
0
bdk-kotlin/aar/consumer-rules.pro
Normal file
26
bdk-kotlin/aar/proguard-rules.pro
vendored
Normal file
26
bdk-kotlin/aar/proguard-rules.pro
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
|
||||
# for JNA
|
||||
-dontwarn java.awt.*
|
||||
-keep class com.sun.jna.* { *; }
|
||||
-keepclassmembers class * extends com.sun.jna.* { public *; }
|
||||
@@ -0,0 +1,147 @@
|
||||
package org.bitcoindevkit.bdk
|
||||
|
||||
import android.util.Log
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.After
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Before
|
||||
|
||||
//import org.bitcoindevkit.bdkjni.Types.Network
|
||||
//import org.bitcoindevkit.bdkjni.Types.WalletConstructor
|
||||
//import org.bitcoindevkit.bdkjni.Types.WalletPtr
|
||||
import org.junit.Ignore
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ExampleInstrumentedTest {
|
||||
|
||||
companion object {
|
||||
init {
|
||||
System.loadLibrary("bdk_jni")
|
||||
}
|
||||
}
|
||||
|
||||
private lateinit var wallet: WalletPtr
|
||||
|
||||
@Test
|
||||
fun useAppContext() {
|
||||
// Context of the app under test.
|
||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
||||
assertEquals("org.bitcoindevkit.bdkjni.test", appContext.packageName)
|
||||
}
|
||||
|
||||
@Before
|
||||
fun constructor() {
|
||||
val dir = createTempDir()
|
||||
val descriptor = "wpkh(tprv8ZgxMBicQKsPexGYyaFwnAsCXCjmz2FaTm6LtesyyihjbQE3gRMfXqQBXKM43DvC1UgRVv1qom1qFxNMSqVAs88qx9PhgFnfGVUdiiDf6j4/0/*)"
|
||||
val electrum = "tcp://electrum.blockstream.info:60001"
|
||||
wallet = Lib().constructor(WalletConstructor("testnet", Network.regtest, dir.toString(), descriptor, null, electrum, null))
|
||||
Lib().sync(wallet)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun newAddress() {
|
||||
val address = Lib().get_new_address(wallet)
|
||||
assertFalse(address.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sync() {
|
||||
Lib().sync(wallet, 100)
|
||||
val balance = Lib().get_balance(wallet)
|
||||
assertFalse(balance == 0L)
|
||||
}
|
||||
|
||||
// TODO need to figure out why this passes when testing with a localhost node but fails when using blockstream.info
|
||||
@Ignore
|
||||
@Test
|
||||
fun multiThreadBalance() {
|
||||
runBlocking {
|
||||
val flow1 = newBalanceFlow(1).flowOn(Dispatchers.IO)
|
||||
val flow2 = newBalanceFlow(2).flowOn(Dispatchers.IO)
|
||||
flow1.flatMapMerge(concurrency = 2) { flow2 }.collect()
|
||||
//flow1.collect()
|
||||
}
|
||||
}
|
||||
|
||||
private fun newBalanceFlow(id: Int): Flow<Pair<Int, Long>> {
|
||||
return (1..10).asFlow()
|
||||
//.onStart { Log.d("BAL_FLOW", "start flow $id") }
|
||||
//.onCompletion { Log.d("BAL_FLOW", "complete flow $id") }
|
||||
//.onEach { Log.d("BAL_FLOW", "flow $id, iteration $it") }
|
||||
.map {
|
||||
val balance = Lib().get_balance(wallet)
|
||||
Pair(it, balance)
|
||||
}
|
||||
.catch { e ->
|
||||
Log.e("BAL_FLOW", "failed flow $id with exception: $e")
|
||||
fail()
|
||||
}
|
||||
.onEach {
|
||||
//Log.d("BAL_FLOW", "verifying flow $id, iteration ${it.first}")
|
||||
assertFalse(it.second == 0L)
|
||||
//Log.d("BAL_FLOW", "finished flow $id iteration ${it.first}")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun balance() {
|
||||
val balance = Lib().get_balance(wallet)
|
||||
assertFalse(balance == 0L)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun unspent() {
|
||||
val unspent = Lib().list_unspent(wallet)
|
||||
assertFalse(unspent.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun transactions() {
|
||||
val transactions = Lib().list_transactions(wallet)
|
||||
assertFalse(transactions.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun generate_key() {
|
||||
val keys = Lib().generate_extended_key(Network.testnet, 24, "test123")
|
||||
assertNotNull(keys)
|
||||
assertEquals(24, keys.mnemonic.split(' ').size)
|
||||
assertEquals("tprv", keys.xprv.substring(0,4))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun restore_key() {
|
||||
val mnemonic = "shell bid diary primary focus average truly secret lonely circle radar fall tank action place body wedding sponsor embody glue swing gauge shop penalty"
|
||||
val keys = Lib().restore_extended_key(Network.testnet, mnemonic, null)
|
||||
assertNotNull(keys)
|
||||
assertEquals(mnemonic, keys.mnemonic)
|
||||
assertEquals("tprv8ZgxMBicQKsPeh5nd4nCDLGh9dLfhqGfUoiQsbThkttjX9oroRY2j5vpEGwkiKiKtzdU7u4eqH2yFicGvz19rMVVXfY8XB9fdoeXWJ7SgVE", keys.xprv)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun restore_key_password() {
|
||||
val mnemonic = "shell bid diary primary focus average truly secret lonely circle radar fall tank action place body wedding sponsor embody glue swing gauge shop penalty"
|
||||
val keys = Lib().restore_extended_key(Network.testnet, mnemonic, "test123")
|
||||
assertNotNull(keys)
|
||||
assertEquals(mnemonic, keys.mnemonic)
|
||||
assertEquals("tprv8ZgxMBicQKsPebcVXyErMuuv2rgE34m2SLMBhy4hURbSEAWQ1VsWVVmMnD7FKiAuRrxzAETFnUaSvFNQ5SAS5tYEwsM1KHDpUhLLQgd6yG1", keys.xprv)
|
||||
}
|
||||
|
||||
@After
|
||||
fun destructor() {
|
||||
Lib().destructor(wallet)
|
||||
}
|
||||
}
|
||||
6
bdk-kotlin/aar/src/main/AndroidManifest.xml
Normal file
6
bdk-kotlin/aar/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.bitcoindevkit.bdk" >
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
</manifest>
|
||||
22
bdk-kotlin/build.gradle
Normal file
22
bdk-kotlin/build.gradle
Normal file
@@ -0,0 +1,22 @@
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.5.10'
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:4.2.1'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
|
||||
task clean(type: Delete) {
|
||||
delete rootProject.buildDir
|
||||
}
|
||||
21
bdk-kotlin/gradle.properties
Normal file
21
bdk-kotlin/gradle.properties
Normal file
@@ -0,0 +1,21 @@
|
||||
# Project-wide Gradle settings.
|
||||
# IDE (e.g. Android Studio) users:
|
||||
# Gradle settings configured through the IDE *will override*
|
||||
# any settings specified in this file.
|
||||
# For more details on how to configure your build environment visit
|
||||
# http://www.gradle.org/docs/current/userguide/build_environment.html
|
||||
# Specifies the JVM arguments used for the daemon process.
|
||||
# The setting is particularly useful for tweaking memory settings.
|
||||
org.gradle.jvmargs=-Xmx1536m
|
||||
# When configured, Gradle will run in incubating parallel mode.
|
||||
# This option should only be used with decoupled projects. More details, visit
|
||||
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
|
||||
# org.gradle.parallel=true
|
||||
# AndroidX package structure to make it clearer which packages are bundled with the
|
||||
# Android operating system, and which are packaged with your app's APK
|
||||
# https://developer.android.com/topic/libraries/support-library/androidx-rn
|
||||
android.useAndroidX=true
|
||||
# Automatically convert third-party libraries to use AndroidX
|
||||
android.enableJetifier=true
|
||||
# Kotlin code style for this project: "official" or "obsolete":
|
||||
kotlin.code.style=official
|
||||
BIN
bdk-kotlin/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
bdk-kotlin/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
6
bdk-kotlin/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
6
bdk-kotlin/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
#Thu Jun 10 21:43:37 PDT 2021
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
|
||||
distributionPath=wrapper/dists
|
||||
zipStorePath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
42
bdk-kotlin/jar/build.gradle
Normal file
42
bdk-kotlin/jar/build.gradle
Normal file
@@ -0,0 +1,42 @@
|
||||
plugins {
|
||||
id 'org.jetbrains.kotlin.jvm'
|
||||
id 'java-library'
|
||||
id 'maven-publish'
|
||||
}
|
||||
|
||||
test {
|
||||
environment "LD_LIBRARY_PATH", file("${projectDir}/../../target/debug").absolutePath
|
||||
// testLogging {
|
||||
// events "PASSED", "SKIPPED", "FAILED", "STANDARD_OUT", "STANDARD_ERROR"
|
||||
// }
|
||||
}
|
||||
|
||||
task buildRust(type: Exec) {
|
||||
workingDir '../'
|
||||
commandLine './build.sh'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation platform('org.jetbrains.kotlin:kotlin-bom')
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
implementation "net.java.dev.jna:jna:5.8.0"
|
||||
|
||||
testImplementation 'org.jetbrains.kotlin:kotlin-test'
|
||||
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
maven(MavenPublication) {
|
||||
groupId = 'org.bitcoindevkit'
|
||||
artifactId = 'bdk-debug'
|
||||
version = '0.0.1-dev'
|
||||
|
||||
from components.java
|
||||
}
|
||||
}
|
||||
}
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
35
bdk-kotlin/jar/src/main/java/org/bitcoindevkit/bdk/Lib.kt
Normal file
35
bdk-kotlin/jar/src/main/java/org/bitcoindevkit/bdk/Lib.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
package org.bitcoindevkit.bdk
|
||||
|
||||
import com.sun.jna.*
|
||||
|
||||
interface Lib : Library {
|
||||
|
||||
// typedef struct WalletPtr WalletPtr_t;
|
||||
class WalletPtr_t : PointerType {
|
||||
constructor(): super()
|
||||
constructor(pointer: Pointer): super(pointer)
|
||||
}
|
||||
|
||||
// void free_string (
|
||||
// char * string);
|
||||
fun free_string(string: String)
|
||||
|
||||
// WalletPtr_t * new_wallet (
|
||||
// char const * name,
|
||||
// char const * descriptor,
|
||||
// char const * change_descriptor);
|
||||
fun new_wallet(name: String, descriptor: String, changeDescriptor: String?): WalletPtr_t
|
||||
|
||||
// void sync_wallet (
|
||||
// WalletPtr_t * const * wallet);
|
||||
//fun sync_wallet(wallet: WalletPtr_t)
|
||||
fun sync_wallet(wallet: WalletPtr_t)
|
||||
|
||||
// char * new_address (
|
||||
// WalletPtr_t * const * wallet);
|
||||
fun new_address(wallet: WalletPtr_t): String
|
||||
|
||||
// void free_wallet (
|
||||
// WalletPtr_t * wallet);
|
||||
fun free_wallet(wallet: WalletPtr_t)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.bitcoindevkit.bdk
|
||||
|
||||
import com.sun.jna.Native
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
/**
|
||||
* Library test, which will execute on linux host.
|
||||
*
|
||||
*/
|
||||
class LibTest {
|
||||
|
||||
private val bdkFfi: Lib = Native.load("bdk_ffi", Lib::class.java)
|
||||
|
||||
@Test
|
||||
fun new_sync_free_wallet() {
|
||||
val name = "test_wallet"
|
||||
val desc = "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)"
|
||||
val change = "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/1/*)"
|
||||
|
||||
val wallet = bdkFfi.new_wallet(name, desc, change)
|
||||
bdkFfi.sync_wallet(wallet)
|
||||
bdkFfi.free_wallet(wallet)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun new_newaddress_wallet() {
|
||||
val name = "test_wallet"
|
||||
val desc = "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)"
|
||||
val change = "wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/1/*)"
|
||||
|
||||
val wallet = bdkFfi.new_wallet(name, desc, change)
|
||||
val address = bdkFfi.new_address(wallet)
|
||||
//println("address created from kotlin: $address")
|
||||
assertEquals(address, "tb1qzg4mckdh50nwdm9hkzq06528rsu73hjxxzem3e")
|
||||
bdkFfi.free_string(address)
|
||||
bdkFfi.free_wallet(wallet)
|
||||
}
|
||||
}
|
||||
3
bdk-kotlin/settings.gradle
Normal file
3
bdk-kotlin/settings.gradle
Normal file
@@ -0,0 +1,3 @@
|
||||
rootProject.name = 'bdk-kotlin'
|
||||
|
||||
include 'jar'
|
||||
Reference in New Issue
Block a user