From 04349769889fc552622ae0655ba6f9403f56d53d Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Sat, 9 Mar 2019 13:39:04 -0500 Subject: [PATCH 01/12] wip: Add core compilation in BRCrypto --- Java/Crypto/CMakeLists.txt | 260 ++++++++++++++++++++++++++++++++++ Java/Crypto/build.gradle | 41 +++++- Java/Crypto/src/main/cpp/core | 1 + 3 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 Java/Crypto/CMakeLists.txt create mode 120000 Java/Crypto/src/main/cpp/core diff --git a/Java/Crypto/CMakeLists.txt b/Java/Crypto/CMakeLists.txt new file mode 100644 index 000000000..5f072611b --- /dev/null +++ b/Java/Crypto/CMakeLists.txt @@ -0,0 +1,260 @@ +# For more information about using CMake with Android Studio, read the +# documentation: https://d.android.com/studio/projects/add-native-code.html + +# Sets the minimum version of CMake required to build the native library. + +cmake_minimum_required(VERSION 3.4.1) + +# now build app's shared lib +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -DANDROID_STL=gnustl_static -DANDROID_TOOLCHAIN=clang") + +# -Wimplicit-function-declaration +# -Wno-missing-prototypes -Werror=return-type -Wdocumentation -Wunreachable-code-aggressive -Wno-missing-braces +# -Wparentheses -Wswitch -Wno-unused-function -Wunused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body +# -Wconditional-uninitialized -Wno-unknown-pragmas -pedantic -Wshadow -Wfour-char-constants -Wno-conversion -Wconstant-conversion +# -Wint-conversion -Wbool-conversion -Wenum-conversion -Wassign-enum -Wno-shorten-64-to-32 -Wpointer-sign -Wnewline-eof +# -Wdeprecated-declarations -Wno-sign-conversion + + +add_library( # Sets the name of the library. + crypto + + # Sets the library as a shared library. + SHARED) + +# Core +target_sources (crypto + PUBLIC + + # Core files + src/main/cpp/core/BRAddress.c + src/main/cpp/core/BRAddress.h + src/main/cpp/core/BRArray.h + src/main/cpp/core/BRBase58.c + src/main/cpp/core/BRBase58.h + src/main/cpp/core/BRBech32.c + src/main/cpp/core/BRBech32.h + src/main/cpp/core/BRBIP32Sequence.c + src/main/cpp/core/BRBIP32Sequence.h + src/main/cpp/core/BRBIP38Key.c + src/main/cpp/core/BRBIP38Key.h + src/main/cpp/core/BRBIP39Mnemonic.c + src/main/cpp/core/BRBIP39Mnemonic.h + src/main/cpp/core/BRBIP39WordsEn.h + src/main/cpp/core/BRBloomFilter.c + src/main/cpp/core/BRBloomFilter.h + src/main/cpp/core/BRCrypto.c + src/main/cpp/core/BRCrypto.h + src/main/cpp/core/BRInt.h + src/main/cpp/core/BRKey.c + src/main/cpp/core/BRKey.h + src/main/cpp/core/BRKeyECIES.c + src/main/cpp/core/BRKeyECIES.h + src/main/cpp/core/BRMerkleBlock.c + src/main/cpp/core/BRMerkleBlock.h + src/main/cpp/core/BRPaymentProtocol.c + src/main/cpp/core/BRPaymentProtocol.h + src/main/cpp/core/BRPeer.c + src/main/cpp/core/BRPeer.h + src/main/cpp/core/BRPeerManager.c + src/main/cpp/core/BRPeerManager.h + src/main/cpp/core/BRSet.c + src/main/cpp/core/BRSet.h + src/main/cpp/core/BRTransaction.c + src/main/cpp/core/BRTransaction.h + src/main/cpp/core/BRWallet.c + src/main/cpp/core/BRWallet.h + + src/main/cpp/core/support/BRFileService.c + src/main/cpp/core/support/BRFileService.h + src/main/cpp/core/support/BRAssert.c + src/main/cpp/core/support/BRAssert.h + + # BCash files + src/main/cpp/core/bcash/BRBCashAddr.c + src/main/cpp/core/bcash/BRBCashAddr.h + src/main/cpp/core/bcash/BRBCashParams.h + + # Bech32 files + src/main/cpp/core/BRBech32.c + src/main/cpp/core/BRBech32.h) + +# Core Ethereum +target_sources (crypto + PUBLIC + + # + src/main/cpp/core/ethereum/BREthereum.h + + # Util + src/main/cpp/core/ethereum/util/BRKeccak.c + src/main/cpp/core/ethereum/util/BRKeccak.h + src/main/cpp/core/ethereum/util/BRUtil.h + src/main/cpp/core/ethereum/util/BRUtilHex.c + src/main/cpp/core/ethereum/util/BRUtilHex.h + src/main/cpp/core/ethereum/util/BRUtilMath.c + src/main/cpp/core/ethereum/util/BRUtilMath.h + src/main/cpp/core/ethereum/util/BRUtilMathParse.c +# src/main/cpp/core/ethereum/util/testUtil.c + + # RLP + src/main/cpp/core/ethereum/rlp/BRRlp.h + src/main/cpp/core/ethereum/rlp/BRRlpCoder.c + src/main/cpp/core/ethereum/rlp/BRRlpCoder.h +# src/main/cpp/core/ethereum/rlp/testRlp.c + + # Event + src/main/cpp/core/ethereum/event/BREvent.c + src/main/cpp/core/ethereum/event/BREvent.h + src/main/cpp/core/ethereum/event/BREventAlarm.c + src/main/cpp/core/ethereum/event/BREventAlarm.h + src/main/cpp/core/ethereum/event/BREventQueue.c + src/main/cpp/core/ethereum/event/BREventQueue.h +# src/main/cpp/core/ethereum/event/testEvent.c + + # Base + src/main/cpp/core/ethereum/base/BREthereumAddress.c + src/main/cpp/core/ethereum/base/BREthereumAddress.h + src/main/cpp/core/ethereum/base/BREthereumBase.h + src/main/cpp/core/ethereum/base/BREthereumEther.c + src/main/cpp/core/ethereum/base/BREthereumEther.h + src/main/cpp/core/ethereum/base/BREthereumGas.c + src/main/cpp/core/ethereum/base/BREthereumGas.h + src/main/cpp/core/ethereum/base/BREthereumHash.c + src/main/cpp/core/ethereum/base/BREthereumHash.h + src/main/cpp/core/ethereum/base/BREthereumData.c + src/main/cpp/core/ethereum/base/BREthereumData.h + src/main/cpp/core/ethereum/base/BREthereumLogic.h + src/main/cpp/core/ethereum/base/BREthereumSignature.c + src/main/cpp/core/ethereum/base/BREthereumSignature.h +# src/main/cpp/core/ethereum/base/testBase.c + + # Block Chain + src/main/cpp/core/ethereum/blockchain/BREthereumAccountState.c + src/main/cpp/core/ethereum/blockchain/BREthereumAccountState.h + src/main/cpp/core/ethereum/blockchain/BREthereumBlock.c + src/main/cpp/core/ethereum/blockchain/BREthereumBlock.h + src/main/cpp/core/ethereum/blockchain/BREthereumBlockChain.h + src/main/cpp/core/ethereum/blockchain/BREthereumBloomFilter.c + src/main/cpp/core/ethereum/blockchain/BREthereumBloomFilter.h + src/main/cpp/core/ethereum/blockchain/BREthereumLog.c + src/main/cpp/core/ethereum/blockchain/BREthereumLog.h + src/main/cpp/core/ethereum/blockchain/BREthereumNetwork.c + src/main/cpp/core/ethereum/blockchain/BREthereumNetwork.h + src/main/cpp/core/ethereum/blockchain/BREthereumTransaction.c + src/main/cpp/core/ethereum/blockchain/BREthereumTransaction.h + src/main/cpp/core/ethereum/blockchain/BREthereumTransactionReceipt.c + src/main/cpp/core/ethereum/blockchain/BREthereumTransactionReceipt.h + src/main/cpp/core/ethereum/blockchain/BREthereumTransactionStatus.c + src/main/cpp/core/ethereum/blockchain/BREthereumTransactionStatus.h + src/main/cpp/core/ethereum/blockchain/BREthereumProofOfWork.c +# src/main/cpp/core/ethereum/blockchain/testBc.c + + # Contract + src/main/cpp/core/ethereum/contract/BREthereumContract.c + src/main/cpp/core/ethereum/contract/BREthereumContract.h + src/main/cpp/core/ethereum/contract/BREthereumToken.c + src/main/cpp/core/ethereum/contract/BREthereumToken.h +# src/main/cpp/core/ethereum/contract/testContract.c + + # MPT + src/main/cpp/core/ethereum/mpt/BREthereumMPT.c + src/main/cpp/core/ethereum/mpt/BREthereumMPT.h + + # LES Msg + src/main/cpp/core/ethereum/les/msg/BREthereumMessageDIS.c + src/main/cpp/core/ethereum/les/msg/BREthereumMessageDIS.h + src/main/cpp/core/ethereum/les/msg/BREthereumMessageETH.h + src/main/cpp/core/ethereum/les/msg/BREthereumMessageLES.c + src/main/cpp/core/ethereum/les/msg/BREthereumMessageLES.h + src/main/cpp/core/ethereum/les/msg/BREthereumMessageP2P.c + src/main/cpp/core/ethereum/les/msg/BREthereumMessageP2P.h + src/main/cpp/core/ethereum/les/msg/BREthereumMessagePIP.c + src/main/cpp/core/ethereum/les/msg/BREthereumMessagePIP.h + + # LES + src/main/cpp/core/ethereum/les/BREthereumLES.c + src/main/cpp/core/ethereum/les/BREthereumLES.h + src/main/cpp/core/ethereum/les/BREthereumLESBase.h + src/main/cpp/core/ethereum/les/BREthereumLESFrameCoder.c + src/main/cpp/core/ethereum/les/BREthereumLESFrameCoder.h + src/main/cpp/core/ethereum/les/BREthereumLESRandom.c + src/main/cpp/core/ethereum/les/BREthereumLESRandom.h + src/main/cpp/core/ethereum/les/BREthereumMessage.c + src/main/cpp/core/ethereum/les/BREthereumMessage.h + src/main/cpp/core/ethereum/les/BREthereumNode.c + src/main/cpp/core/ethereum/les/BREthereumNode.h + src/main/cpp/core/ethereum/les/BREthereumNodeEndpoint.c + src/main/cpp/core/ethereum/les/BREthereumNodeEndpoint.h + src/main/cpp/core/ethereum/les/BREthereumProvision.c + src/main/cpp/core/ethereum/les/BREthereumProvision.h + src/main/cpp/core/ethereum/les/msg/BREthereumMessageP2P.h + src/main/cpp/core/ethereum/les/msg/BREthereumMessageP2P.c + src/main/cpp/core/ethereum/les/msg/BREthereumMessageDIS.h + src/main/cpp/core/ethereum/les/msg/BREthereumMessageDIS.c + src/main/cpp/core/ethereum/les/msg/BREthereumMessageETH.h + src/main/cpp/core/ethereum/les/msg/BREthereumMessageLES.h + src/main/cpp/core/ethereum/les/msg/BREthereumMessageLES.c + src/main/cpp/core/ethereum/les/msg/BREthereumMessagePIP.h + src/main/cpp/core/ethereum/les/msg/BREthereumMessagePIP.c +# src/main/cpp/core/ethereum/les/test-les.c +# src/main/cpp/core/ethereum/les/test-les.h + + # BCS + src/main/cpp/core/ethereum/bcs/BREthereumBCS.c + src/main/cpp/core/ethereum/bcs/BREthereumBCS.h + src/main/cpp/core/ethereum/bcs/BREthereumBCSEvent.c + src/main/cpp/core/ethereum/bcs/BREthereumBCSPrivate.h + src/main/cpp/core/ethereum/bcs/BREthereumBCSSync.c + src/main/cpp/core/ethereum/bcs/BREthereumBlockChainSlice.h + + #EWM + src/main/cpp/core/ethereum/ewm/BREthereumBase.h + src/main/cpp/core/ethereum/ewm/BREthereumAccount.c + src/main/cpp/core/ethereum/ewm/BREthereumAccount.h + src/main/cpp/core/ethereum/ewm/BREthereumAmount.c + src/main/cpp/core/ethereum/ewm/BREthereumAmount.h + src/main/cpp/core/ethereum/ewm/BREthereumTransfer.c + src/main/cpp/core/ethereum/ewm/BREthereumTransfer.h + src/main/cpp/core/ethereum/ewm/BREthereumWallet.c + src/main/cpp/core/ethereum/ewm/BREthereumWallet.h + src/main/cpp/core/ethereum/ewm/BREthereumClient.h + src/main/cpp/core/ethereum/ewm/BREthereumEWM.c + src/main/cpp/core/ethereum/ewm/BREthereumEWM.h + src/main/cpp/core/ethereum/ewm/BREthereumEWMClient.c + src/main/cpp/core/ethereum/ewm/BREthereumEWMEvent.c + src/main/cpp/core/ethereum/ewm/BREthereumEWMPrivate.h +# src/main/cpp/core/ethereum/ewm/testEwm.c + ) + +# Crypto JNI +target_sources (crypto + PUBLIC) + +include_directories(src/main/cpp/core/) +include_directories(src/main/cpp/core/ethereum/) +include_directories(src/main/cpp/core/secp256k1/) + +# Searches for a specified prebuilt library and stores the path as a +# variable. Because CMake includes system libraries in the search path by +# default, you only need to specify the name of the public NDK library +# you want to add. CMake verifies that the library exists before +# completing its build. + +find_library( # Sets the name of the path variable. + log-lib + + # Specifies the name of the NDK library that + # you want CMake to locate. + log ) + +# Specifies libraries CMake should link to your target library. You +# can link multiple libraries, such as libraries you define in this +# build script, prebuilt third-party libraries, or system libraries. + +target_link_libraries( # Specifies the target library. + crypto + + # Links the target library to the log library + # included in the NDK. + ${log-lib} ) \ No newline at end of file diff --git a/Java/Crypto/build.gradle b/Java/Crypto/build.gradle index e185bedf5..03dca2a9d 100644 --- a/Java/Crypto/build.gradle +++ b/Java/Crypto/build.gradle @@ -7,6 +7,13 @@ android { targetCompatibility JavaVersion.VERSION_1_8 } + // Link Gradle to the CMake script + externalNativeBuild { + cmake { + path "CMakeLists.txt" //path can only be set outside (in android block) + } + } + defaultConfig { minSdkVersion 23 targetSdkVersion 28 @@ -24,11 +31,44 @@ android { missingDimensionStrategy 'network', 'onMainnet' } + flavorDimensions "network" + productFlavors { + + onMainnet { + dimension "network" + } + + onTestnet { + dimension "network" + externalNativeBuild { + cmake { + cFlags "-DBITCOIN_TESTNET" + } + } + } + } + + // http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.BaseExtension.html#com.android.build.gradle.BaseExtension:buildTypes(org.gradle.api.Action) + // "Encapsulates all build type configurations for this project" buildTypes { + + // Default: required release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } + + // Default: created on demand with 'debuggable true' + debug { + debuggable true + jniDebuggable true + minifyEnabled false + externalNativeBuild { + cmake { + cFlags "-DDEBUG", "-DBITCOIN_DEBUG" + } + } + } } } @@ -38,5 +78,4 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' - implementation project(':Core') } diff --git a/Java/Crypto/src/main/cpp/core b/Java/Crypto/src/main/cpp/core new file mode 120000 index 000000000..3ed539a75 --- /dev/null +++ b/Java/Crypto/src/main/cpp/core @@ -0,0 +1 @@ +../../../../../. \ No newline at end of file From 53fab1a0bcc3ca8df450ed58e607d0075c1c20c1 Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Sat, 9 Mar 2019 22:05:23 -0500 Subject: [PATCH 02/12] wip: Add java implementation --- .../{ => api}/ethereum/TransferAIT.java | 4 +- .../java/com/breadwallet/crypto/Account.java | 4 - .../java/com/breadwallet/crypto/Address.java | 4 - .../java/com/breadwallet/crypto/Network.java | 61 ---------- .../com/breadwallet/crypto/WalletManager.java | 70 ----------- .../com/breadwallet/crypto/api/Account.java | 7 ++ .../com/breadwallet/crypto/api/Address.java | 4 + .../breadwallet/crypto/{ => api}/Amount.java | 2 +- .../crypto/{ => api}/Currency.java | 2 +- .../com/breadwallet/crypto/api/Network.java | 91 ++++++++++++++ .../crypto/{ => api}/Transfer.java | 2 +- .../breadwallet/crypto/{ => api}/Unit.java | 2 +- .../breadwallet/crypto/{ => api}/Wallet.java | 2 +- .../breadwallet/crypto/api/WalletManager.java | 21 ++++ .../api/WalletManagerBackendClient.java | 6 + .../crypto/api/WalletManagerListener.java | 11 ++ .../api/WalletManagerPersistenceClient.java | 12 ++ .../api/bitcoin/BitcoinBackendClient.java | 6 + .../api/bitcoin/BitcoinChainParams.java | 4 + .../api/bitcoin/BitcoinMasterPubKey.java | 4 + .../api/bitcoin/BitcoinPersistenceClient.java | 21 ++++ .../bitcoin/BitcoinWalletManagerListener.java | 6 + .../crypto/{ => api}/ethereum/Ethereum.java | 8 +- .../crypto/{ => api}/ethereum/Transfer.java | 10 +- .../crypto/{ => api}/ethereum/Wallet.java | 10 +- .../crypto/api/event/TransferEvent.java | 4 + .../crypto/api/event/WalletEvent.java | 4 + .../crypto/api/event/WalletManagerEvent.java | 4 + .../com/breadwallet/crypto/core/Account.java | 23 ++++ .../core/bitcoin/BitcoinChainParams.java | 14 +++ .../bitcoin/BitcoinChainParamsAdapter.java | 12 ++ .../core/bitcoin/BitcoinMasterPubKey.java | 16 +++ .../bitcoin/BitcoinMasterPubKeyAdapter.java | 12 ++ .../crypto/core/bitcoin/BitcoinNetworks.java | 8 ++ .../core/bitcoin/BitcoinWalletManager.java | 107 ++++++++++++++++ .../bitcoin/jni/CoreBitcoinChainParams.java | 15 +++ .../bitcoin/jni/CoreBitcoinMasterPubKey.java | 18 +++ .../bitcoin/jni/CoreBitcoinPeerManager.java | 11 ++ .../core/bitcoin/jni/CoreBitcoinWallet.java | 9 ++ .../bitcoin/jni/CoreBitcoinWalletManager.java | 58 +++++++++ .../jni/CoreBitcoinWalletManagerClient.java | 10 ++ .../core/common/CommonWalletManager.java | 114 ++++++++++++++++++ .../crypto/core/common/jni/JniReference.java | 50 ++++++++ .../breadwallet/crypto/core/jni/BIP39.java | 5 + 44 files changed, 707 insertions(+), 161 deletions(-) rename Java/Crypto/src/androidTest/java/com/breadwallet/crypto/{ => api}/ethereum/TransferAIT.java (70%) delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/Account.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/Address.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/Network.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/WalletManager.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/Address.java rename Java/Crypto/src/main/java/com/breadwallet/crypto/{ => api}/Amount.java (92%) rename Java/Crypto/src/main/java/com/breadwallet/crypto/{ => api}/Currency.java (94%) create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java rename Java/Crypto/src/main/java/com/breadwallet/crypto/{ => api}/Transfer.java (96%) rename Java/Crypto/src/main/java/com/breadwallet/crypto/{ => api}/Unit.java (95%) rename Java/Crypto/src/main/java/com/breadwallet/crypto/{ => api}/Wallet.java (97%) create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerBackendClient.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinBackendClient.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinWalletManagerListener.java rename Java/Crypto/src/main/java/com/breadwallet/crypto/{ => api}/ethereum/Ethereum.java (76%) rename Java/Crypto/src/main/java/com/breadwallet/crypto/{ => api}/ethereum/Transfer.java (58%) rename Java/Crypto/src/main/java/com/breadwallet/crypto/{ => api}/ethereum/Wallet.java (79%) create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java diff --git a/Java/Crypto/src/androidTest/java/com/breadwallet/crypto/ethereum/TransferAIT.java b/Java/Crypto/src/androidTest/java/com/breadwallet/crypto/api/ethereum/TransferAIT.java similarity index 70% rename from Java/Crypto/src/androidTest/java/com/breadwallet/crypto/ethereum/TransferAIT.java rename to Java/Crypto/src/androidTest/java/com/breadwallet/crypto/api/ethereum/TransferAIT.java index 845cd1d7b..4e0bf06ff 100644 --- a/Java/Crypto/src/androidTest/java/com/breadwallet/crypto/ethereum/TransferAIT.java +++ b/Java/Crypto/src/androidTest/java/com/breadwallet/crypto/api/ethereum/TransferAIT.java @@ -1,11 +1,9 @@ -package com.breadwallet.crypto.ethereum; +package com.breadwallet.crypto.api.ethereum; import com.breadwallet.crypto.BaseAIT; import org.junit.Test; -import static org.junit.Assert.*; - public class TransferAIT extends BaseAIT { @Test public void ethTransferOne() { diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/Account.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/Account.java deleted file mode 100644 index 6eef2f8bf..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/Account.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto; - -public class Account { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/Address.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/Address.java deleted file mode 100644 index 22f627cf6..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/Address.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto; - -public class Address { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/Network.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/Network.java deleted file mode 100644 index d0d1bc65b..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/Network.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.breadwallet.crypto; -import com.breadwallet.crypto.ethereum.Ethereum; - -public class Network { - enum Type { Bitcoin, Bitcash, Ethereum } - - public static final class Bitcoin { - final String name; - final int forkId; - // chainParams - - public Bitcoin(String name, int forkId) { - this.name = name; - this.forkId = forkId; - } - } - - public static final class Bitcash { - // as above - - } - - public static final class Ethereum { - final String name; - final int chainId; - // BREthereumNetwork - - public Ethereum(String name, int chainId) { - this.name = name; - this.chainId = chainId; - } - } - - public final Currency currency; - - private final Type type; - private final Bitcoin bitcoin; - private final Bitcash bitcash; - private final Ethereum ethereum; - - public Network(Type type, Bitcoin bitcoin, Bitcash bitcash, Ethereum ethereum, Currency currency) { - this.type = type; - this.bitcoin = bitcoin; - this.bitcash = bitcash; - this.ethereum = ethereum; - this.currency = currency; - } - - public Network (Bitcoin bitcoin) { - this (Type.Bitcoin, bitcoin, null, null, null); - } - - public Network (Bitcash bitcash) { - this (Type.Bitcash, null, bitcash, null, null); - } - - public Network (Ethereum ethereum) { - this (Type.Ethereum, null, null, ethereum, - com.breadwallet.crypto.ethereum.Ethereum.currency); - } -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/WalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/WalletManager.java deleted file mode 100644 index 029fe5335..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/WalletManager.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.breadwallet.crypto; - -public abstract class WalletManager { - public interface Listener { - - } - - public interface PersistenceClient { - - } - - public interface BackendClient { - - } - - public final Account account; - - public final Network network; - - public final Listener listener; - -/* - /// The listener receives Wallet, Transfer and perhaps other asynchronous events. - var listener: WalletManagerListener { get } - - /// The account - var account: Account { get } - - /// The network - var network: Network { get } - - /// The primaryWallet - var primaryWallet: Wallet { get } - - /// The managed wallets - often will just be [primaryWallet] - var wallets: [Wallet] { get } - - // The mode determines how the manager manages the account and wallets on network - var mode: WalletManagerMode { get } - - // The file-system path to use for persistent storage. - var path: String { get } // persistent storage - - var state: WalletManagerState { get } - - #if false - /// The default WalletFactory for creating wallets. - var walletFactory: WalletFactory { get set } - #endif - - /// Connect to network and begin managing wallets for account - func connect () - - /// Disconnect from the network. - func disconnect () - - /// isConnected - /// sync(...) - /// isSyncing - - /// sign(transfer) - /// submit(transfer) -*/ - - protected WalletManager (Listener listener, Account account, Network network) { - this.listener = listener; - this.account = account; - this.network = network; - } -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java new file mode 100644 index 000000000..d44635e27 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java @@ -0,0 +1,7 @@ +package com.breadwallet.crypto.api; + +import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; + +public interface Account { + BitcoinMasterPubKey masterPublicKey(); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Address.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Address.java new file mode 100644 index 000000000..82ada1389 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Address.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api; + +public class Address { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/Amount.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java similarity index 92% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/Amount.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java index 2b948a372..e04b030a6 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/Amount.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java @@ -1,4 +1,4 @@ -package com.breadwallet.crypto; +package com.breadwallet.crypto.api; import java.math.BigInteger; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/Currency.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java similarity index 94% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/Currency.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java index 2311f4701..516cb2cf9 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/Currency.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java @@ -1,4 +1,4 @@ -package com.breadwallet.crypto; +package com.breadwallet.crypto.api; public class Currency { public final String code; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java new file mode 100644 index 000000000..f0a794774 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java @@ -0,0 +1,91 @@ +package com.breadwallet.crypto.api; + +import com.breadwallet.crypto.api.bitcoin.BitcoinChainParams; + +// TODO: Refactor this class +public class Network { + enum Type { BITCOIN, BITCASH, ETHEREUM} + + public static final class Bitcoin { + final String name; + final int forkId; + final BitcoinChainParams chainParams; + + public Bitcoin(String name, int forkId, BitcoinChainParams chainParams) { + this.name = name; + this.forkId = forkId; + this.chainParams = chainParams; + } + } + + public static final class Bitcash { + final String name; + final int forkId; + final BitcoinChainParams chainParams; + + public Bitcash(String name, int forkId, BitcoinChainParams chainParams) { + this.name = name; + this.forkId = forkId; + this.chainParams = chainParams; + } + } + + public static final class Ethereum { + final String name; + final int chainId; + + public Ethereum(String name, int chainId) { + this.name = name; + this.chainId = chainId; + } + } + + private final Type type; + private final Bitcoin bitcoin; + private final Bitcash bitcash; + private final Ethereum ethereum; + + private Network(Type type, Bitcoin bitcoin, Bitcash bitcash, Ethereum ethereum) { + this.type = type; + this.bitcoin = bitcoin; + this.bitcash = bitcash; + this.ethereum = ethereum; + } + + public Network (Bitcoin bitcoin) { + this (Type.BITCOIN, bitcoin, null, null); + } + + public Network (Bitcash bitcash) { + this (Type.BITCASH, null, bitcash, null); + } + + public Network (Ethereum ethereum) { + this (Type.ETHEREUM, null, null, ethereum); + } + + public String name() { + switch (type) { + case BITCOIN: return bitcoin.name; + case BITCASH: return bitcash.name; + case ETHEREUM: return ethereum.name; + default: throw new IllegalStateException(); + } + } + + public int forkId() { + switch (type) { + case BITCOIN: return bitcoin.forkId; + case BITCASH: return bitcash.forkId; + default: throw new IllegalStateException(); + } + } + + public BitcoinChainParams chainParams() { + switch (type) { + case BITCOIN: return bitcoin.chainParams; + case BITCASH: return bitcash.chainParams; + default: throw new IllegalStateException(); + } + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/Transfer.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Transfer.java similarity index 96% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/Transfer.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/Transfer.java index f35d4d5d1..920aad880 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/Transfer.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Transfer.java @@ -1,4 +1,4 @@ -package com.breadwallet.crypto; +package com.breadwallet.crypto.api; public abstract class Transfer { public Wallet wallet; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/Unit.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java similarity index 95% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/Unit.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java index f5383b167..a0e206be2 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/Unit.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java @@ -1,4 +1,4 @@ -package com.breadwallet.crypto; +package com.breadwallet.crypto.api; public class Unit { public final Currency currency; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/Wallet.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java similarity index 97% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/Wallet.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java index 2f247081a..0fba6a09e 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/Wallet.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java @@ -1,4 +1,4 @@ -package com.breadwallet.crypto; +package com.breadwallet.crypto.api; public abstract class Wallet { public final WalletManager manager; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java new file mode 100644 index 000000000..6bdaaf524 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java @@ -0,0 +1,21 @@ +package com.breadwallet.crypto.api; + +public interface WalletManager { + + enum Mode { + API_ONLY, + API_WITH_P2P_SUBMIT, + P2P_WITH_API_SYNC, + P2P_ONLY, + } + + enum State { + CREATED, + DISCONNECTED, + CONNECTED, + SYNCHING, + DELETED + } + + void connect(); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerBackendClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerBackendClient.java new file mode 100644 index 000000000..d220a0e5a --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerBackendClient.java @@ -0,0 +1,6 @@ +package com.breadwallet.crypto.api; + +public interface WalletManagerBackendClient { + + boolean isReachable(); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java new file mode 100644 index 000000000..6f47f1c30 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java @@ -0,0 +1,11 @@ +package com.breadwallet.crypto.api; + +import com.breadwallet.crypto.api.event.TransferEvent; +import com.breadwallet.crypto.api.event.WalletEvent; +import com.breadwallet.crypto.api.event.WalletManagerEvent; + +public interface WalletManagerListener { + void handleManagerEvent(WalletManager manager, WalletManagerEvent event); + void handleTransferEvent(WalletManager manager, TransferEvent event); + void handleWalletEvent(WalletManager manager, WalletEvent event); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java new file mode 100644 index 000000000..fa3a01c46 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java @@ -0,0 +1,12 @@ +package com.breadwallet.crypto.api; + +import java.util.Map; + +public interface WalletManagerPersistenceClient { + + void savePeers(WalletManager walletManager, Map data); + + void saveBlocks(WalletManager walletManager, Map data); + + // TODO: Add persistence change type +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinBackendClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinBackendClient.java new file mode 100644 index 000000000..426493446 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinBackendClient.java @@ -0,0 +1,6 @@ +package com.breadwallet.crypto.api.bitcoin; + +import com.breadwallet.crypto.api.WalletManagerBackendClient; + +public interface BitcoinBackendClient extends WalletManagerBackendClient { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java new file mode 100644 index 000000000..0f9b7133a --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.bitcoin; + +public interface BitcoinChainParams { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java new file mode 100644 index 000000000..d617026aa --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.bitcoin; + +public interface BitcoinMasterPubKey { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java new file mode 100644 index 000000000..49554769f --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java @@ -0,0 +1,21 @@ +package com.breadwallet.crypto.api.bitcoin; + +import com.breadwallet.crypto.api.WalletManagerPersistenceClient; +import com.breadwallet.crypto.api.WalletManager; + +import java.util.Map; + +public interface BitcoinPersistenceClient extends WalletManagerPersistenceClient { + + @Override + default void savePeers(WalletManager walletManager, Map data) { + System.out.println("TST: BTC: savePeers"); + } + + @Override + default void saveBlocks(WalletManager walletManager, Map data) { + System.out.println("TST: BTC: saveBlocks"); + } + + // TODO: Add persistence change type +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinWalletManagerListener.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinWalletManagerListener.java new file mode 100644 index 000000000..a6c93c0d0 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinWalletManagerListener.java @@ -0,0 +1,6 @@ +package com.breadwallet.crypto.api.bitcoin; + +import com.breadwallet.crypto.api.WalletManagerListener; + +public interface BitcoinWalletManagerListener extends WalletManagerListener { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/ethereum/Ethereum.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Ethereum.java similarity index 76% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/ethereum/Ethereum.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Ethereum.java index 9d1926288..79db012f1 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/ethereum/Ethereum.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Ethereum.java @@ -1,8 +1,8 @@ -package com.breadwallet.crypto.ethereum; +package com.breadwallet.crypto.api.ethereum; -import com.breadwallet.crypto.Currency; -import com.breadwallet.crypto.Network; -import com.breadwallet.crypto.Unit; +import com.breadwallet.crypto.api.Currency; +import com.breadwallet.crypto.api.Network; +import com.breadwallet.crypto.api.Unit; public interface Ethereum { Currency currency = new Currency ("ETH", "Ξ", "Ethereum", 18, "WEI", "wei"); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/ethereum/Transfer.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Transfer.java similarity index 58% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/ethereum/Transfer.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Transfer.java index ba8a5ca4d..a93940fa5 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/ethereum/Transfer.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Transfer.java @@ -1,10 +1,10 @@ -package com.breadwallet.crypto.ethereum; +package com.breadwallet.crypto.api.ethereum; -import com.breadwallet.crypto.Address; -import com.breadwallet.crypto.Amount; -import com.breadwallet.crypto.Wallet; +import com.breadwallet.crypto.api.Address; +import com.breadwallet.crypto.api.Amount; +import com.breadwallet.crypto.api.Wallet; -class Transfer extends com.breadwallet.crypto.Transfer { +class Transfer extends com.breadwallet.crypto.api.Transfer { long core; private Transfer(long core, Wallet wallet, Address source, Address target, Amount amount, Amount fee) { diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/ethereum/Wallet.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Wallet.java similarity index 79% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/ethereum/Wallet.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Wallet.java index fccc8a8c7..e06a465fc 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/ethereum/Wallet.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Wallet.java @@ -1,10 +1,10 @@ -package com.breadwallet.crypto.ethereum; +package com.breadwallet.crypto.api.ethereum; -import com.breadwallet.crypto.Amount; -import com.breadwallet.crypto.Currency; -import com.breadwallet.crypto.WalletManager; +import com.breadwallet.crypto.api.Amount; +import com.breadwallet.crypto.api.Currency; +import com.breadwallet.crypto.api.WalletManager; -public class Wallet extends com.breadwallet.crypto.Wallet { +public class Wallet extends com.breadwallet.crypto.api.Wallet { long core; /// Balance diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java new file mode 100644 index 000000000..97b1c780a --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.event; + +public interface TransferEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java new file mode 100644 index 000000000..8af13168a --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.event; + +public interface WalletEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java new file mode 100644 index 000000000..c345c506e --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.event; + +public interface WalletManagerEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java new file mode 100644 index 000000000..cadd45416 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java @@ -0,0 +1,23 @@ +package com.breadwallet.crypto.core; + +import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinMasterPubKey; +import com.breadwallet.crypto.core.jni.BIP39; + +public class Account implements com.breadwallet.crypto.api.Account { + + private final BitcoinMasterPubKey masterPublicKey; + + public Account(String phrase) { + this(BIP39.deriveKey(phrase)); + } + + public Account(byte[] seed) { + this.masterPublicKey = new CoreBitcoinMasterPubKey(seed); + } + + @Override + public BitcoinMasterPubKey masterPublicKey() { + return masterPublicKey; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java new file mode 100644 index 000000000..94fb15259 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java @@ -0,0 +1,14 @@ +package com.breadwallet.crypto.core.bitcoin; + +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinChainParams; + +public class BitcoinChainParams implements com.breadwallet.crypto.api.bitcoin.BitcoinChainParams { + + public static BitcoinChainParams TESTNET = new BitcoinChainParams(CoreBitcoinChainParams.TESTNET); + + /* package */ final CoreBitcoinChainParams chainParams; + + private BitcoinChainParams(CoreBitcoinChainParams chainParams) { + this.chainParams = chainParams; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java new file mode 100644 index 000000000..aa99cc566 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java @@ -0,0 +1,12 @@ +package com.breadwallet.crypto.core.bitcoin; + +import com.breadwallet.crypto.api.bitcoin.BitcoinChainParams; + +/* package */ class BitcoinChainParamsAdapter { + public static com.breadwallet.crypto.core.bitcoin.BitcoinChainParams from(BitcoinChainParams bcp) { + if (bcp instanceof com.breadwallet.crypto.core.bitcoin.BitcoinChainParams) { + return (com.breadwallet.crypto.core.bitcoin.BitcoinChainParams) bcp; + } + throw new IllegalArgumentException("Unsupported BitcoinChainParams implementation"); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java new file mode 100644 index 000000000..3dc080c76 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java @@ -0,0 +1,16 @@ +package com.breadwallet.crypto.core.bitcoin; + +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinMasterPubKey; + +public class BitcoinMasterPubKey implements com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey { + + /* package */ final CoreBitcoinMasterPubKey masterPubKey; + + public BitcoinMasterPubKey(byte[] seed) { + this(new CoreBitcoinMasterPubKey(seed)); + } + + public BitcoinMasterPubKey(CoreBitcoinMasterPubKey masterPubKey) { + this.masterPubKey = masterPubKey; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java new file mode 100644 index 000000000..6fe34c83f --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java @@ -0,0 +1,12 @@ +package com.breadwallet.crypto.core.bitcoin; + +import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; + +/* package */ class BitcoinMasterPubKeyAdapter { + public static com.breadwallet.crypto.core.bitcoin.BitcoinMasterPubKey from(BitcoinMasterPubKey mbk) { + if (mbk instanceof com.breadwallet.crypto.core.bitcoin.BitcoinMasterPubKey) { + return (com.breadwallet.crypto.core.bitcoin.BitcoinMasterPubKey) mbk; + } + throw new IllegalArgumentException("Unsupported BitcoinMasterPubKey implementation"); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java new file mode 100644 index 000000000..404a257a1 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java @@ -0,0 +1,8 @@ +package com.breadwallet.crypto.core.bitcoin; + +import com.breadwallet.crypto.api.Network; + +public class BitcoinNetworks { + public static Network TESTNET = new Network( + new Network.Bitcoin("BTC Testnet", 0x40, BitcoinChainParams.TESTNET)); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java new file mode 100644 index 000000000..43ede7ab6 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java @@ -0,0 +1,107 @@ +package com.breadwallet.crypto.core.bitcoin; + +import com.breadwallet.crypto.api.Account; +import com.breadwallet.crypto.api.Network; +import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinChainParams; +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinMasterPubKey; +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinPeerManager; +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWallet; +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWalletManager; +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWalletManagerClient; +import com.breadwallet.crypto.core.common.CommonWalletManager; + +import java.lang.ref.WeakReference; +import java.util.concurrent.Executor; + +// TODO: It would be nice to have this exposed via a factory since the ctors are all the same +public final class BitcoinWalletManager + extends CommonWalletManager implements CoreBitcoinWalletManagerClient { + + private final WeakReference listener; + private final CoreBitcoinWalletManager coreWalletManager; + private final CoreBitcoinPeerManager corePeerManager; + private final CoreBitcoinWallet coreWallet; + + private final BitcoinPersistenceClient persistenceClient; + private final BitcoinBackendClient backendClient; + + public BitcoinWalletManager(BitcoinWalletManagerListener listener, + Account account, + Network network, + Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient) { + this(DEFAULT_EXECUTOR, listener, account, network, mode, earliestKeyTime, storagePath, persistenceClient, backendClient); + } + + public BitcoinWalletManager(Executor listenerExector, + BitcoinWalletManagerListener listener, + Account account, + Network network, + Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient) { + super(listenerExector, account, network, mode, earliestKeyTime, storagePath); + + this.persistenceClient = persistenceClient; + this.backendClient = backendClient; + + CoreBitcoinChainParams chainParams = BitcoinChainParamsAdapter.from(network.chainParams()).chainParams; + CoreBitcoinMasterPubKey masterPubKey = BitcoinMasterPubKeyAdapter.from(account.masterPublicKey()).masterPubKey; + + this.listener = new WeakReference<>(listener); + this.coreWalletManager = new CoreBitcoinWalletManager(this, masterPubKey, chainParams, earliestKeyTime, storagePath); + this.corePeerManager = coreWalletManager.getPeerManager(); + this.coreWallet = coreWalletManager.getWallet(); + } + + // WalletManager + + @Override + public void connect() { + coreWalletManager.connect(); + corePeerManager.connect(); + } + + // CoreWalletManagerClient + + @Override + public void handleTransactionEvent() { + // TODO: implement me + listenerExecutor.execute(() -> { + BitcoinWalletManagerListener l = listener.get(); + if (l != null) { + l.handleTransferEvent(this, null); + } + }); + } + + @Override + public void handleWalletEvent() { + // TODO: implement me + listenerExecutor.execute(() -> { + BitcoinWalletManagerListener l = listener.get(); + if (l != null) { + l.handleWalletEvent(this, null); + } + }); + } + + @Override + public void handleWalletManagerEvent() { + // TODO: implement me + listenerExecutor.execute(() -> { + BitcoinWalletManagerListener l = listener.get(); + if (l != null) { + l.handleManagerEvent(this, null); + } + }); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java new file mode 100644 index 000000000..fbee5c9d7 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java @@ -0,0 +1,15 @@ +package com.breadwallet.crypto.core.bitcoin.jni; + +import com.breadwallet.crypto.core.common.jni.JniReference; + +public class CoreBitcoinChainParams + extends JniReference { + + public static CoreBitcoinChainParams TESTNET = new CoreBitcoinChainParams(getTestnetParams()); + + private static native long getTestnetParams(); + + protected CoreBitcoinChainParams(long jniReferenceAddress) { + super(jniReferenceAddress); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java new file mode 100644 index 000000000..56f7e5532 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java @@ -0,0 +1,18 @@ +package com.breadwallet.crypto.core.bitcoin.jni; + +import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; +import com.breadwallet.crypto.core.common.jni.JniReference; + +public class CoreBitcoinMasterPubKey + extends JniReference implements BitcoinMasterPubKey { + + private static native long createBitcoinMasterPubKey (byte[] seed); + + public CoreBitcoinMasterPubKey(byte[] seed) { + this(createBitcoinMasterPubKey(seed)); + } + + protected CoreBitcoinMasterPubKey(long jniReferenceAddress) { + super(jniReferenceAddress); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java new file mode 100644 index 000000000..bcefd9d4e --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java @@ -0,0 +1,11 @@ +package com.breadwallet.crypto.core.bitcoin.jni; + +import com.breadwallet.crypto.core.common.jni.JniReference; + +public class CoreBitcoinPeerManager extends JniReference { + protected CoreBitcoinPeerManager(long jniReferenceAddress) { + super(jniReferenceAddress); + } + + public native void connect(); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java new file mode 100644 index 000000000..7d082ffae --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java @@ -0,0 +1,9 @@ +package com.breadwallet.crypto.core.bitcoin.jni; + +import com.breadwallet.crypto.core.common.jni.JniReference; + +public class CoreBitcoinWallet extends JniReference { + protected CoreBitcoinWallet(long jniReferenceAddress) { + super(jniReferenceAddress); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java new file mode 100644 index 000000000..4f7d20f32 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java @@ -0,0 +1,58 @@ +package com.breadwallet.crypto.core.bitcoin.jni; + +import com.breadwallet.crypto.core.common.jni.JniReference; + +import java.lang.ref.WeakReference; + +public class CoreBitcoinWalletManager + extends JniReference implements CoreBitcoinWalletManagerClient { + + private static native long createBitcoinWalletManager(CoreBitcoinMasterPubKey mpk, + CoreBitcoinChainParams params, + int earliestKeyTime, + String storagePath); + + private final WeakReference client; + + public CoreBitcoinWalletManager(CoreBitcoinWalletManagerClient client, + CoreBitcoinMasterPubKey mpk, + CoreBitcoinChainParams params, + int earliestKeyTime, + String storagePath) { + // client needs to be in place before BRWalletManagerNew is invoked, as it calls back + this.client = new WeakReference<>(client); + this.jniReferenceAddress = createBitcoinWalletManager(mpk, params, earliestKeyTime, storagePath); + } + + public native CoreBitcoinPeerManager getPeerManager(); + + public native CoreBitcoinWallet getWallet(); + + public native void connect(); + + // CoreBitcoinWalletManagerClient + + @Override + public void handleTransactionEvent() { + CoreBitcoinWalletManagerClient c = client.get(); + if (null != c) { + c.handleTransactionEvent(); + } + } + + @Override + public void handleWalletEvent() { + CoreBitcoinWalletManagerClient c = client.get(); + if (null != c) { + c.handleWalletEvent(); + } + } + + @Override + public void handleWalletManagerEvent() { + CoreBitcoinWalletManagerClient c = client.get(); + if (null != c) { + c.handleWalletManagerEvent(); + } + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java new file mode 100644 index 000000000..7bda8c010 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java @@ -0,0 +1,10 @@ +package com.breadwallet.crypto.core.bitcoin.jni; + +public interface CoreBitcoinWalletManagerClient { + + void handleTransactionEvent(); + + void handleWalletEvent(); + + void handleWalletManagerEvent(); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java new file mode 100644 index 000000000..99e8021d4 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java @@ -0,0 +1,114 @@ +package com.breadwallet.crypto.core.common; + +import com.breadwallet.crypto.api.Account; +import com.breadwallet.crypto.api.Network; +import com.breadwallet.crypto.api.WalletManager; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +public abstract class CommonWalletManager implements WalletManager { + + protected static Executor DEFAULT_EXECUTOR = Executors.newSingleThreadExecutor(); + + protected final Account account; + protected final Network network; + protected final Mode mode; + protected final long earliestKeyTime; + protected final String storagePath; + protected final Executor listenerExecutor; + + protected State state; + + public CommonWalletManager(Account account, + Network network, + Mode mode, + long earliestKeyTime, + String storagePath) { + this(DEFAULT_EXECUTOR, account, network, mode, earliestKeyTime, storagePath); + } + + public CommonWalletManager(Executor listenerExecutor, + Account account, + Network network, + Mode mode, + long earliestKeyTime, + String storagePath) { + // TODO: Check arguments for validity (i.e. null, correctness, etc.) + this.listenerExecutor = listenerExecutor; + this.account = account; + this.network = network; + this.mode = mode; + this.earliestKeyTime = earliestKeyTime; + this.storagePath = storagePath; + this.state = State.CREATED; + } + + +// public interface Listener { +// +// } +// +// public interface WalletManagerPersistenceClient { +// +// } +// +// public interface BackendClient { +// +// } +// +// public final Account account; +// +// public final Network network; +// +// public final com.breadwallet.crypto.WalletManager.Listener listener; + +/* + /// The listener receives Wallet, Transfer and perhaps other asynchronous events. + var listener: WalletManagerListener { get } + + /// The account + var account: Account { get } + + /// The network + var network: Network { get } + + /// The primaryWallet + var primaryWallet: Wallet { get } + + /// The managed wallets - often will just be [primaryWallet] + var wallets: [Wallet] { get } + + // The mode determines how the manager manages the account and wallets on network + var mode: WalletManagerMode { get } + + // The file-system path to use for persistent storage. + var path: String { get } // persistent storage + + var state: WalletManagerState { get } + + #if false + /// The default WalletFactory for creating wallets. + var walletFactory: WalletFactory { get set } + #endif + + /// Connect to network and begin managing wallets for account + func connect () + + /// Disconnect from the network. + func disconnect () + + /// isConnected + /// sync(...) + /// isSyncing + + /// sign(transfer) + /// submit(transfer) +*/ +// +// protected WalletManager (com.breadwallet.crypto.WalletManager.Listener listener, Account account, Network network) { +// this.listener = listener; +// this.account = account; +// this.network = network; +// } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java new file mode 100644 index 000000000..314596412 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java @@ -0,0 +1,50 @@ +package com.breadwallet.crypto.core.common.jni; + +public class JniReference { + static { + try { System.loadLibrary("core"); } + catch (UnsatisfiedLinkError e) { + e.printStackTrace(); + System.err.println ("Native code library failed to load.\\n\" + " + e); + } + } + + protected static boolean SHOW_FINALIZE = false; + /** + * C Pointer (as a Java long) to the underlying Breadwallet Core entity allocated from the + * C heap memory. The referenced Core entity is used to implement native functions that + * call Core functions (and thus expect a Core entity). + * + * The address must be determined in a subclass specific way and thus must be provided in the + * subclasses constructor. + */ + protected long jniReferenceAddress; + + protected JniReference (long jniReferenceAddress) + { + this.jniReferenceAddress = jniReferenceAddress; + } + + protected JniReference () + { + this.jniReferenceAddress = 0; + } + + // + // + // + protected void finalize() throws Throwable { + if (SHOW_FINALIZE) System.err.println("Finalize: " + toString()); + dispose(); + } + + public void dispose() { + disposeNative(); + } + + public native void disposeNative(); + + public String toString() { + return getClass().getName() + "@" + Integer.toHexString(hashCode()) + " JNI=" + Long.toHexString(jniReferenceAddress); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java new file mode 100644 index 000000000..795a18a2d --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java @@ -0,0 +1,5 @@ +package com.breadwallet.crypto.core.jni; + +public class BIP39 { + public static native byte[] deriveKey(String phrase); +} From 4adcf076a34b07e7134f22ba6010b1806e10998a Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Sun, 10 Mar 2019 17:34:39 -0400 Subject: [PATCH 03/12] wip: Flesh out implementation --- Java/{Makefile => Core.Makefile} | 0 Java/CoreDemo/build.gradle | 1 + .../coredemo/CoreDemoBitcoinClient.java | 80 ++++++++++ .../coredemo/CoreDemoEthereumClient.java | 3 + .../coredemo/WalletNavigationActivity.java | 61 ++++--- Java/Crypto.Makefile | 115 ++++++++++++++ Java/Crypto/CMakeLists.txt | 22 ++- Java/Crypto/src/main/cpp/jni/BRCryptoJni.c | 62 ++++++++ Java/Crypto/src/main/cpp/jni/BRCryptoJni.h | 51 ++++++ ..._core_bitcoin_jni_CoreBitcoinChainParams.c | 36 +++++ ..._core_bitcoin_jni_CoreBitcoinChainParams.h | 21 +++ ...core_bitcoin_jni_CoreBitcoinMasterPubKey.c | 46 ++++++ ...core_bitcoin_jni_CoreBitcoinMasterPubKey.h | 21 +++ ..._core_bitcoin_jni_CoreBitcoinPeerManager.c | 51 ++++++ ..._core_bitcoin_jni_CoreBitcoinPeerManager.h | 29 ++++ ...rypto_core_bitcoin_jni_CoreBitcoinWallet.c | 40 +++++ ...rypto_core_bitcoin_jni_CoreBitcoinWallet.h | 21 +++ ...ore_bitcoin_jni_CoreBitcoinWalletManager.c | 149 ++++++++++++++++++ ...ore_bitcoin_jni_CoreBitcoinWalletManager.h | 61 +++++++ ...tcoin_jni_CoreBitcoinWalletManagerClient.c | 0 ...tcoin_jni_CoreBitcoinWalletManagerClient.h | 13 ++ ...llet_crypto_core_common_jni_JniReference.c | 70 ++++++++ ...llet_crypto_core_common_jni_JniReference.h | 21 +++ .../com_breadwallet_crypto_core_jni_Bip39.c | 47 ++++++ .../com_breadwallet_crypto_core_jni_Bip39.h | 21 +++ .../com/breadwallet/crypto/api/Account.java | 1 + .../com/breadwallet/crypto/api/Network.java | 2 +- .../breadwallet/crypto/api/WalletManager.java | 15 +- .../crypto/api/WalletManagerListener.java | 3 + .../api/WalletManagerPersistenceClient.java | 4 +- .../api/bitcoin/BitcoinChainParams.java | 1 + .../api/bitcoin/BitcoinMasterPubKey.java | 1 + .../api/bitcoin/BitcoinPersistenceClient.java | 12 -- .../crypto/api/event/TransferEvent.java | 1 + .../crypto/api/event/WalletEvent.java | 1 + .../crypto/api/event/WalletManagerEvent.java | 1 + .../com/breadwallet/crypto/core/Account.java | 10 +- .../breadwallet/crypto/core/CoreCrypto.java | 9 ++ .../core/bitcoin/BitcoinChainParams.java | 1 + .../bitcoin/BitcoinChainParamsAdapter.java | 2 + .../core/bitcoin/BitcoinMasterPubKey.java | 4 +- .../bitcoin/BitcoinMasterPubKeyAdapter.java | 2 + .../crypto/core/bitcoin/BitcoinNetworks.java | 2 + .../core/bitcoin/BitcoinWalletManager.java | 22 +-- .../bitcoin/jni/CoreBitcoinChainParams.java | 5 +- .../bitcoin/jni/CoreBitcoinMasterPubKey.java | 1 + .../bitcoin/jni/CoreBitcoinPeerManager.java | 4 + .../core/bitcoin/jni/CoreBitcoinWallet.java | 4 + .../bitcoin/jni/CoreBitcoinWalletManager.java | 36 ++--- .../jni/CoreBitcoinWalletManagerClient.java | 1 + .../core/common/CommonWalletManager.java | 10 +- .../crypto/core/common/jni/JniReference.java | 17 +- .../breadwallet/crypto/core/jni/BIP39.java | 4 +- 53 files changed, 1104 insertions(+), 114 deletions(-) rename Java/{Makefile => Core.Makefile} (100%) create mode 100644 Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java create mode 100644 Java/Crypto.Makefile create mode 100644 Java/Crypto/src/main/cpp/jni/BRCryptoJni.c create mode 100644 Java/Crypto/src/main/cpp/jni/BRCryptoJni.h create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.h create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.h create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.c create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.c create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.c create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.h create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c create mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.h create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCrypto.java diff --git a/Java/Makefile b/Java/Core.Makefile similarity index 100% rename from Java/Makefile rename to Java/Core.Makefile diff --git a/Java/CoreDemo/build.gradle b/Java/CoreDemo/build.gradle index 2c7961cc9..53e804f59 100644 --- a/Java/CoreDemo/build.gradle +++ b/Java/CoreDemo/build.gradle @@ -38,4 +38,5 @@ dependencies { androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation project(':Core') + implementation project(':Crypto') } diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java new file mode 100644 index 000000000..443e2999c --- /dev/null +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java @@ -0,0 +1,80 @@ +package com.breadwallet.coredemo; + +import com.breadwallet.crypto.api.Network; +import com.breadwallet.crypto.api.WalletManager; +import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; +import com.breadwallet.crypto.api.event.TransferEvent; +import com.breadwallet.crypto.api.event.WalletEvent; +import com.breadwallet.crypto.api.event.WalletManagerEvent; +import com.breadwallet.crypto.core.Account; +import com.breadwallet.crypto.core.CoreCrypto; +import com.breadwallet.crypto.core.bitcoin.BitcoinWalletManager; + +import java.util.Map; + +public class CoreDemoBitcoinClient + implements BitcoinWalletManagerListener, BitcoinBackendClient, BitcoinPersistenceClient { + + static { CoreCrypto.init(); } + + private final WalletManager walletManager; + + public CoreDemoBitcoinClient(Network network, String storagePath, String paperKey) { + this.walletManager = new BitcoinWalletManager( + this, + new Account(paperKey), + network, + WalletManager.Mode.API_WITH_P2P_SUBMIT, + 1543190400, + storagePath, + this, + this); + } + + public void connect() { + walletManager.connect(); + } + + // BitcoinWalletManagerListener + + @Override + public void handleManagerEvent(WalletManager manager, WalletManagerEvent event) { + + } + + @Override + public void handleTransferEvent(WalletManager manager, TransferEvent event) { + + } + + @Override + public void handleWalletEvent(WalletManager manager, WalletEvent event) { + + } + + // BitcoinBackendClient + + @Override + public boolean isReachable() { + return false; + } + + // BitcoinPersistenceClient + + @Override + public void savePeers(WalletManager walletManager, Map data) { + + } + + @Override + public void saveBlocks(WalletManager walletManager, Map data) { + + } + + @Override + public void changeTransaction(WalletManager walletManager, ChangeType type, String hash, String data) { + + } +} diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoEthereumClient.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoEthereumClient.java index ba53833e7..b0cb01464 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoEthereumClient.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoEthereumClient.java @@ -13,6 +13,9 @@ import java.util.Map; public class CoreDemoEthereumClient implements BREthereumEWM.Client { + + static { System.loadLibrary("core"); } + interface WalletListener { void announceWalletEvent (BREthereumEWM ewm, BREthereumWallet wallet, diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java index 6ac10c2b3..db4806034 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java @@ -4,13 +4,14 @@ import android.os.Bundle; import com.breadwallet.core.ethereum.BREthereumNetwork; +import com.breadwallet.crypto.core.bitcoin.BitcoinNetworks; import java.io.File; public class WalletNavigationActivity extends AppCompatActivity { - static { System.loadLibrary("core"); } - static CoreDemoEthereumClient client = null; + CoreDemoBitcoinClient btcClient = null; + CoreDemoEthereumClient ethClient = null; private void deleteRecursively (File file) { if (file.isDirectory()) @@ -24,23 +25,45 @@ private void deleteRecursively (File file) { protected void onCreate(Bundle savedInstanceState) { System.out.println ("Starting"); - File storageFile = new File (getFilesDir(), "core"); - if (storageFile.exists()) deleteRecursively(storageFile); - storageFile.mkdirs(); - - client = new CoreDemoEthereumClient(BREthereumNetwork.mainnet, - storageFile.getAbsolutePath(), - "boring head harsh green empty clip fatal typical found crane dinner timber"); - - client.ewm.announceToken("0x722dd3f80bac40c951b51bdd28dd19d435762180", - "BRD", - "BRD Token", - "", - 18, - "92000", - "1000000000", - 0); - client.ewm.connect(); + // Bitcoin + + { + File storageFile = new File(getFilesDir(), "core"); + if (storageFile.exists()) deleteRecursively(storageFile); + storageFile.mkdirs(); + + btcClient = new CoreDemoBitcoinClient(BitcoinNetworks.TESTNET, + storageFile.getAbsolutePath(), + "boring head harsh green empty clip fatal typical found crane dinner timber"); + + btcClient.connect(); + + System.gc(); + } + + // Ethereum + + { + File storageFile = new File(getFilesDir(), "core"); + if (storageFile.exists()) deleteRecursively(storageFile); + storageFile.mkdirs(); + + ethClient = new CoreDemoEthereumClient(BREthereumNetwork.mainnet, + storageFile.getAbsolutePath(), + "boring head harsh green empty clip fatal typical found crane dinner timber"); + + ethClient.ewm.announceToken("0x722dd3f80bac40c951b51bdd28dd19d435762180", + "BRD", + "BRD Token", + "", + 18, + "92000", + "1000000000", + 0); + ethClient.ewm.connect(); + + System.gc(); + } super.onCreate(savedInstanceState); setContentView(R.layout.activity_wallet_navigation); diff --git a/Java/Crypto.Makefile b/Java/Crypto.Makefile new file mode 100644 index 000000000..b37ed0e16 --- /dev/null +++ b/Java/Crypto.Makefile @@ -0,0 +1,115 @@ +DIR=$(shell pwd) + +# +# Java +# + +JAVA_DIR=${JAVA_HOME} + +JAVA_LIB= + +JNI_LIB=libCrypto.jnilib + +JNI_SDIR=Crypto/src/main/cpp/jni + +JNI_SRCS= + +JNI_OBJS=$(JNI_SRCS:.c=.o) + +JNI_HDRS=$(JNI_SRCS:.c=.h) + +JAVA_SDIR=Crypto/src/main/java/com/breadwallet/crypto + +JAVA_SRCS=$(JAVA_SDIR)/api/bitcoin/BitcoinBackendClient.java \ + $(JAVA_SDIR)/api/bitcoin/BitcoinChainParams.java \ + $(JAVA_SDIR)/api/bitcoin/BitcoinMasterPubKey.java \ + $(JAVA_SDIR)/api/bitcoin/BitcoinPersistenceClient.java \ + $(JAVA_SDIR)/api/bitcoin/BitcoinWalletManagerListener.java \ + $(JAVA_SDIR)/api/event/TransferEvent.java \ + $(JAVA_SDIR)/api/event/WalletEvent.java \ + $(JAVA_SDIR)/api/event/WalletManagerEvent.java \ + $(JAVA_SDIR)/api/Account.java \ + $(JAVA_SDIR)/api/Network.java \ + $(JAVA_SDIR)/api/WalletManager.java \ + $(JAVA_SDIR)/api/WalletManagerBackendClient.java \ + $(JAVA_SDIR)/api/WalletManagerListener.java \ + $(JAVA_SDIR)/api/WalletManagerPersistenceClient.java \ + $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinChainParams.java \ + $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinMasterPubKey.java \ + $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinPeerManager.java \ + $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinWallet.java \ + $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinWalletManager.java \ + $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java \ + $(JAVA_SDIR)/core/bitcoin/BitcoinChainParams.java \ + $(JAVA_SDIR)/core/bitcoin/BitcoinChainParamsAdapter.java \ + $(JAVA_SDIR)/core/bitcoin/BitcoinMasterPubKey.java \ + $(JAVA_SDIR)/core/bitcoin/BitcoinMasterPubKeyAdapter.java \ + $(JAVA_SDIR)/core/bitcoin/BitcoinNetworks.java \ + $(JAVA_SDIR)/core/bitcoin/BitcoinWalletManager.java \ + $(JAVA_SDIR)/core/common/jni/JniReference.java \ + $(JAVA_SDIR)/core/common/CommonWalletManager.java \ + $(JAVA_SDIR)/core/jni/Bip39.java \ + $(JAVA_SDIR)/core/Account.java \ + $(JAVA_SDIR)/core/CoreCrypto.java + +JAVA_OBJS=$(JAVA_SRCS:.java=.class) + +# +# Core +# + +C_SDIR=Crypto/src/main/cpp/core + +CORE_SRCS= + +CORE_OBJS=$(CORE_SRCS:.c=.o) + +ETH_SRCS= + +ETH_OBJS=$(ETH_SRCS:.c=.o) + +# +# Targets +# + +compile: $(JNI_LIB) java_comp + +$(JNI_LIB): $(JNI_OBJS) $(CORE_OBJS) + cc -dynamiclib -o $(JNI_LIB) $(JNI_OBJS) $(CORE_OBJS) + +java_comp: FORCE + @mkdir -p build + @echo "Crypto Java" + @javac -d build $(JAVA_SRCS) + +jni_hdr_crypto: FORCE + @echo Crypto JNI Headers + @(cd build/com/breadwallet/crypto/core/bitcoin/jni; \ + for class in *.class; do \ + javah -jni -d $(DIR)/$(JNI_SDIR) -classpath $(DIR)/build com.breadwallet.crypto.core.bitcoin.jni.$${class%%.class}; \ + done) + @(cd build/com/breadwallet/crypto/core/common/jni; \ + for class in *.class; do \ + javah -jni -d $(DIR)/$(JNI_SDIR) -classpath $(DIR)/build com.breadwallet.crypto.core.common.jni.$${class%%.class}; \ + done) + @(cd build/com/breadwallet/crypto/core/jni; \ + for class in *.class; do \ + javah -jni -d $(DIR)/$(JNI_SDIR) -classpath $(DIR)/build com.breadwallet.crypto.core.jni.$${class%%.class}; \ + done) + + +jni_hdr: java_comp jni_hdr_crypto + +clean: + rm -rf build $(JNI_OBJS) $(CORE_OBJS) $(JAVA_OBJS) $(JNI_LIB) + +FORCE: + +# test: $(JNI_LIB) java_comp +# java -Xss1m -Dwallet.test -classpath build -Djava.library.path=. \ +# com.breadwallet.core.test.BRWalletManager $(ARGS) # -D. + +# debug: $(JNI_LIB) java_comp +# java -Xss1m -Xdebug -Xrunjdwp:transport=dt_socket,address=8008,server=y,suspend=n \ +# -Dwallet.test -classpath build -Djava.library.path=. \ +# com.breadwallet.core.test.BRWalletManager $(ARGS) # -D. diff --git a/Java/Crypto/CMakeLists.txt b/Java/Crypto/CMakeLists.txt index 5f072611b..638f68973 100644 --- a/Java/Crypto/CMakeLists.txt +++ b/Java/Crypto/CMakeLists.txt @@ -64,6 +64,8 @@ target_sources (crypto src/main/cpp/core/BRTransaction.h src/main/cpp/core/BRWallet.c src/main/cpp/core/BRWallet.h + src/main/cpp/core/BRWalletManager.c + src/main/cpp/core/BRWalletManager.h src/main/cpp/core/support/BRFileService.c src/main/cpp/core/support/BRFileService.h @@ -229,7 +231,25 @@ target_sources (crypto # Crypto JNI target_sources (crypto - PUBLIC) + PUBLIC + src/main/cpp/jni/BRCryptoJni.h + src/main/cpp/jni/BRCryptoJni.c + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.h + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.h + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.c + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.c + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h + src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.c + src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.h + src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c + src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.h + src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c) include_directories(src/main/cpp/core/) include_directories(src/main/cpp/core/ethereum/) diff --git a/Java/Crypto/src/main/cpp/jni/BRCryptoJni.c b/Java/Crypto/src/main/cpp/jni/BRCryptoJni.c new file mode 100644 index 000000000..67efa7e76 --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/BRCryptoJni.c @@ -0,0 +1,62 @@ +// Created by Michael Carrara on 3/19/2019 +// Copyright (c) 2019 breadwallet LLC. +// +// 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. + +#include +#include +#include +#include "BRCryptoJni.h" + +// TODO: Re-write using personal coding style + +static JavaVM *jvm = NULL; + +extern +JNIEnv *getEnv() { + JNIEnv *env; + + if (NULL == jvm) return NULL; + + int status = (*jvm)->GetEnv(jvm, (void **) &env, JNI_VERSION_1_6); + + if (status != JNI_OK) + status = (*jvm)->AttachCurrentThread(jvm, (JNIEnv **) &env, NULL); + + return status == JNI_OK ? env : NULL; +} + +extern +void releaseEnv () { + if (NULL != jvm) + (*jvm)->DetachCurrentThread (jvm); +} + +JNIEXPORT jint JNICALL +JNI_OnLoad (JavaVM *theJvm, void *reserved) { + JNIEnv *env = 0; + + if ((*theJvm)->GetEnv(theJvm, (void **)&env, JNI_VERSION_1_6)) { + return JNI_ERR; + } + + jvm = theJvm; + + return JNI_VERSION_1_6; +} diff --git a/Java/Crypto/src/main/cpp/jni/BRCryptoJni.h b/Java/Crypto/src/main/cpp/jni/BRCryptoJni.h new file mode 100644 index 000000000..ccae77163 --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/BRCryptoJni.h @@ -0,0 +1,51 @@ +// Created by Michael Carrara on 3/19/2019 +// Copyright (c) 2019 breadwallet LLC. +// +// 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. + +#ifndef CRYPTOJNI_BRCRYPTOJVM_H +#define CRYPTOJNI_BRCRYPTOJVM_H + +#include +#include + +// TODO: Re-write using personal coding style + +/** + * + * @return + */ +extern JNIEnv * +getEnv(); + +extern void +releaseEnv (); + +/** + * + * @param env + * @param thisObject + * @return + */ +extern void * +getJNIReference ( + JNIEnv *env, + jobject thisObject); + +#endif //CRYPTOJNI_BRCRYPTOJVM_H diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c new file mode 100644 index 000000000..fec51de05 --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c @@ -0,0 +1,36 @@ +// Created by Michael Carrara on 3/19/2019 +// Copyright (c) 2019 breadwallet LLC. +// +// 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. + +#include +#include + +#include "BRChainParams.h" + +#include "com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.h" + +JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams_createTestnetChainParams ( + JNIEnv * env, + jclass thisClass) +{ + BRChainParams *result = (BRChainParams *) calloc (1, sizeof (BRChainParams)); + memcpy (result, &BRTestNetParams, sizeof (BRChainParams)); + return (jlong) result; +} \ No newline at end of file diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.h new file mode 100644 index 000000000..d835c707e --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams */ + +#ifndef _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams +#define _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams + * Method: createTestnetChainParams + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams_createTestnetChainParams + (JNIEnv *, jclass); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c new file mode 100644 index 000000000..76c7403f8 --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c @@ -0,0 +1,46 @@ +// Created by Michael Carrara on 3/19/2019 +// Copyright (c) 2019 breadwallet LLC. +// +// 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. + +#include +#include + +#include "BRBIP32Sequence.h" + +#include "BRCryptoJni.h" +#include "com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.h" + +// TODO: Add defensive checks on inputs +// TODO: Re-write using personal coding style + +JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey_createBitcoinMasterPubKey ( + JNIEnv * env, + jclass thisClass, + jbyteArray seedByteArray) +{ + jsize seedLength = (*env)->GetArrayLength (env, seedByteArray); + jbyte *seedBytes = (*env)->GetByteArrayElements (env, seedByteArray, 0); + + // Allocate, then fill, our BRMasterPubKey result with the computed pubKey + BRMasterPubKey *resKey = (BRMasterPubKey *) calloc (1, sizeof (BRMasterPubKey)); + *resKey = BRBIP32MasterPubKey(seedBytes, seedLength); + + return (jlong) resKey; +} \ No newline at end of file diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.h new file mode 100644 index 000000000..28f483c09 --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey */ + +#ifndef _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey +#define _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey + * Method: createBitcoinMasterPubKey + * Signature: ([B)J + */ +JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey_createBitcoinMasterPubKey + (JNIEnv *, jclass, jbyteArray); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.c new file mode 100644 index 000000000..05e2e636d --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.c @@ -0,0 +1,51 @@ +// Created by Michael Carrara on 3/19/2019 +// Copyright (c) 2019 breadwallet LLC. +// +// 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. + +#include +#include + +#include "BRPeerManager.h" + +#include "BRCryptoJni.h" +#include "com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h" + +// TODO: Add defensive checks on inputs +// TODO: Re-write using personal coding style + +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager_connect ( + JNIEnv * env, + jobject thisObject) +{ + BRPeerManager *peerManager = (BRPeerManager *) getJNIReference(env, thisObject); + BRPeerManagerConnect(peerManager); +} + +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager_disposeNative ( + JNIEnv * env, + jobject thisObject) +{ + BRPeerManager *peerManager = (BRPeerManager *) getJNIReference(env, thisObject); + + if (NULL != peerManager) { + assert (BRPeerStatusDisconnected == BRPeerManagerConnectStatus(peerManager)); + BRPeerManagerFree(peerManager); + } +} \ No newline at end of file diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h new file mode 100644 index 000000000..478d7bb99 --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h @@ -0,0 +1,29 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager */ + +#ifndef _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager +#define _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager + * Method: connect + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager_connect + (JNIEnv *, jobject); + +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager + * Method: disposeNative + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager_disposeNative + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.c new file mode 100644 index 000000000..9918d4bfe --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.c @@ -0,0 +1,40 @@ +// Created by Michael Carrara on 3/19/2019 +// Copyright (c) 2019 breadwallet LLC. +// +// 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. + +#include +#include + +#include "BRWallet.h" + +#include "BRCryptoJni.h" +#include "com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h" + +// TODO: Add defensive checks on inputs +// TODO: Re-write using personal coding style + +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet_disposeNative ( + JNIEnv * env, + jobject thisObject) +{ + BRWallet *wallet = (BRWallet *) getJNIReference(env, thisObject); + + if (NULL != wallet) BRWalletFree(wallet); +} \ No newline at end of file diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h new file mode 100644 index 000000000..89fcb06b1 --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet */ + +#ifndef _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet +#define _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet + * Method: disposeNative + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet_disposeNative + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c new file mode 100644 index 000000000..d99e8e19e --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c @@ -0,0 +1,149 @@ +// Created by Michael Carrara on 3/19/2019 +// Copyright (c) 2019 breadwallet LLC. +// +// 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. + +#include +#include + +#include "BRWalletManager.h" + +#include "BRCryptoJni.h" +#include "com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h" + +// TODO: Add defensive checks on inputs +// TODO: Wire in callbacks to Java layer +// TODO: Re-write using personal coding style +// TODO: Return borrowed CoreBitcoinWallet in getWallet() +// TODO: Return borrowed CoreBitcoinPeerManager in getPeerManager() + +static jclass peerManagerClass; +static jmethodID peerManagerConstructor; + +static jclass walletClass; +static jmethodID walletConstructor; + +static void +transactionEventCallback (BRWalletManager manager, + BRWallet *wallet, + BRTransaction *transaction, + BRTransactionEvent event); + +static void +walletEventCallback (BRWalletManager manager, + BRWallet *wallet, + BRWalletEvent event); + +static void +walletManagerEventCallback (BRWalletManager manager, + BRWalletManagerEvent event); + +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_initializeNative ( + JNIEnv * env, + jclass thisClass) +{ + peerManagerClass = (*env)->FindClass(env, "com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager"); + assert (NULL != peerManagerClass); + peerManagerClass = (*env)->NewGlobalRef (env, peerManagerClass); + + peerManagerConstructor = (*env)->GetMethodID(env, peerManagerClass, "", "(J)V"); + assert (NULL != peerManagerConstructor); + + walletClass = (*env)->FindClass(env, "com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet"); + assert (NULL != walletClass); + walletClass = (*env)->NewGlobalRef (env, walletClass); + + walletConstructor = (*env)->GetMethodID(env, walletClass, "", "(J)V"); + assert (NULL != walletConstructor); +} + +JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_createBitcoinWalletManager ( + JNIEnv * env, + jclass thisClass, + jobject masterPublicKeyObject, + jobject chainParamsObject, + jint earliestKeyTime, + jstring storagePathString) +{ + BRMasterPubKey *masterPubKey = (BRMasterPubKey *) getJNIReference(env, masterPublicKeyObject); + BRChainParams *chainParams = (BRChainParams *) getJNIReference(env, chainParamsObject); + const char *storagePath = (*env)->GetStringUTFChars (env, storagePathString, 0); + assert (earliestKeyTime >= 0); + BRWalletManagerClient client = { transactionEventCallback, walletEventCallback, walletManagerEventCallback }; + + BRWalletManager *walletManager = BRWalletManagerNew(client, *masterPubKey, chainParams, earliestKeyTime, storagePath); + assert (walletManager); + + return (jlong) walletManager; +} + +JNIEXPORT jobject JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_getPeerManager ( + JNIEnv * env, + jobject thisObject) +{ + BRWalletManager *walletManager = (BRWalletManager *) getJNIReference(env, thisObject); + BRPeerManager *peerManager = BRWalletManagerGetPeerManager(walletManager); + return (*env)->NewObject (env, peerManagerClass, peerManagerConstructor, (jlong) peerManager); +} + +JNIEXPORT jobject JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_getWallet ( + JNIEnv * env, + jobject thisObject) +{ + BRWalletManager *walletManager = (BRWalletManager *) getJNIReference(env, thisObject); + BRWallet *wallet= BRWalletManagerGetPeerManager(walletManager); + return (*env)->NewObject (env, walletClass, walletConstructor, (jlong) wallet); +} + +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_connect ( + JNIEnv * env, + jobject thisObject) +{ + BRWalletManager *walletManager = (BRWalletManager *) getJNIReference(env, thisObject); + BRWalletManagerConnect(walletManager); +} + +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_disposeNative ( + JNIEnv * env, + jobject thisObject) +{ + BRWalletManager *walletManager = (BRWalletManager *) getJNIReference(env, thisObject); + if (NULL != walletManager) BRWalletManagerFree(walletManager); +} + +static void +transactionEventCallback (BRWalletManager manager, + BRWallet *wallet, + BRTransaction *transaction, + BRTransactionEvent event) { + printf ("TST: TransactionEvent: %d\n", event.type); +} + +static void +walletEventCallback (BRWalletManager manager, + BRWallet *wallet, + BRWalletEvent event) { + printf ("TST: WalletEvent: %d\n", event.type); +} + +static void +walletManagerEventCallback (BRWalletManager manager, + BRWalletManagerEvent event) { + printf ("TST: WalletManagerEvent: %d\n", event.type); +} diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h new file mode 100644 index 000000000..e72f13a4a --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h @@ -0,0 +1,61 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager */ + +#ifndef _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager +#define _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager + * Method: initializeNative + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_initializeNative + (JNIEnv *, jclass); + +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager + * Method: createBitcoinWalletManager + * Signature: (Lcom/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey;Lcom/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams;ILjava/lang/String;)J + */ +JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_createBitcoinWalletManager + (JNIEnv *, jclass, jobject, jobject, jint, jstring); + +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager + * Method: getPeerManager + * Signature: ()Lcom/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager; + */ +JNIEXPORT jobject JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_getPeerManager + (JNIEnv *, jobject); + +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager + * Method: getWallet + * Signature: ()Lcom/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet; + */ +JNIEXPORT jobject JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_getWallet + (JNIEnv *, jobject); + +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager + * Method: connect + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_connect + (JNIEnv *, jobject); + +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager + * Method: disposeNative + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_disposeNative + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.c new file mode 100644 index 000000000..e69de29bb diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h new file mode 100644 index 000000000..2092281b1 --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h @@ -0,0 +1,13 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient */ + +#ifndef _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient +#define _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient +#ifdef __cplusplus +extern "C" { +#endif +#ifdef __cplusplus +} +#endif +#endif diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c new file mode 100644 index 000000000..163fc1a3d --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c @@ -0,0 +1,70 @@ +// Created by Michael Carrara on 3/19/2019 +// Copyright (c) 2019 breadwallet LLC. +// +// 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. + +#include +#include + +#include "BRCryptoJni.h" +#include "com_breadwallet_crypto_core_common_jni_JniReference.h" + +// TODO: Add defensive checks on inputs +// TODO: Re-write using personal coding style +// TODO: Add concept of owned (to handle case like CoreBitcoinWalletManager::getPeerManager() + +#define JNI_REFERENCE_ADDRESS_FIELD_NAME "jniReferenceAddress" +#define JNI_REFERENCE_ADDRESS_FIELD_TYPE "J" // long + +static jfieldID getJNIReferenceField ( + JNIEnv *env, + jobject thisObject) +{ + jclass thisClass = (*env)->GetObjectClass (env, thisObject); + jfieldID thisFieldId = (*env)->GetFieldID(env, thisClass, + JNI_REFERENCE_ADDRESS_FIELD_NAME, + JNI_REFERENCE_ADDRESS_FIELD_TYPE); + (*env)->DeleteLocalRef (env, thisClass); + return thisFieldId; +} + +static jlong getJNIReferenceAddress ( + JNIEnv *env, + jobject thisObject) +{ + jfieldID coreBRKeyAddressField = getJNIReferenceField(env, thisObject); + assert (NULL != coreBRKeyAddressField); + + return (*env)->GetLongField (env, thisObject, coreBRKeyAddressField); +} + +extern void *getJNIReference ( + JNIEnv *env, + jobject thisObject) +{ + return (void *) getJNIReferenceAddress(env, thisObject); +} + +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_common_jni_JniReference_disposeNative ( + JNIEnv * env, + jobject thisObject) +{ + void *reference = getJNIReference(env, thisObject); + if (NULL != reference) free (reference); +} \ No newline at end of file diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.h new file mode 100644 index 000000000..61e3458ba --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_breadwallet_crypto_core_common_jni_JniReference */ + +#ifndef _Included_com_breadwallet_crypto_core_common_jni_JniReference +#define _Included_com_breadwallet_crypto_core_common_jni_JniReference +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_breadwallet_crypto_core_common_jni_JniReference + * Method: disposeNative + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_common_jni_JniReference_disposeNative + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c new file mode 100644 index 000000000..32f93e06e --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c @@ -0,0 +1,47 @@ +// Created by Michael Carrara on 3/19/2019 +// Copyright (c) 2019 breadwallet LLC. +// +// 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. + +#include +#include + +#include "BRInt.h" +#include "BRBIP39Mnemonic.h" + +#include "com_breadwallet_crypto_core_jni_Bip39.h" + +// TODO: Add defensive checks on inputs +// TODO: Re-write using personal coding style + +JNIEXPORT jbyteArray JNICALL Java_com_breadwallet_crypto_core_jni_Bip39_deriveKey ( + JNIEnv * env, + jclass thisClass, + jstring seedString) +{ + UInt512 seed = UINT512_ZERO; + const char *status = (*env)->GetStringUTFChars (env, seedString, 0); + + BRBIP39DeriveKey(seed.u8, status, NULL); + + jbyteArray seedData = (*env)->NewByteArray (env, sizeof(seed)); + (*env)->SetByteArrayRegion (env, seedData, 0, (jsize) sizeof(seed), (const jbyte *) seed.u8); + + return seedData; +} \ No newline at end of file diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.h new file mode 100644 index 000000000..38239926e --- /dev/null +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_breadwallet_crypto_core_jni_Bip39 */ + +#ifndef _Included_com_breadwallet_crypto_core_jni_Bip39 +#define _Included_com_breadwallet_crypto_core_jni_Bip39 +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_breadwallet_crypto_core_jni_Bip39 + * Method: deriveKey + * Signature: (Ljava/lang/String;)[B + */ +JNIEXPORT jbyteArray JNICALL Java_com_breadwallet_crypto_core_jni_Bip39_deriveKey + (JNIEnv *, jclass, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java index d44635e27..5437344ab 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java @@ -3,5 +3,6 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; public interface Account { + BitcoinMasterPubKey masterPublicKey(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java index f0a794774..2b43a8a8e 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java @@ -2,7 +2,7 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinChainParams; -// TODO: Refactor this class +// TODO: Revisit this class and consider moving to interfaces along with the Visitor pattern public class Network { enum Type { BITCOIN, BITCASH, ETHEREUM} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java index 6bdaaf524..1f79aeb8b 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java @@ -2,20 +2,9 @@ public interface WalletManager { - enum Mode { - API_ONLY, - API_WITH_P2P_SUBMIT, - P2P_WITH_API_SYNC, - P2P_ONLY, - } + enum Mode { API_ONLY, API_WITH_P2P_SUBMIT, P2P_WITH_API_SYNC, P2P_ONLY, } - enum State { - CREATED, - DISCONNECTED, - CONNECTED, - SYNCHING, - DELETED - } + enum State { CREATED, DISCONNECTED, CONNECTED, SYNCING, DELETED } void connect(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java index 6f47f1c30..0c2ba607f 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java @@ -5,7 +5,10 @@ import com.breadwallet.crypto.api.event.WalletManagerEvent; public interface WalletManagerListener { + void handleManagerEvent(WalletManager manager, WalletManagerEvent event); + void handleTransferEvent(WalletManager manager, TransferEvent event); + void handleWalletEvent(WalletManager manager, WalletEvent event); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java index fa3a01c46..40c1152ac 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java @@ -4,9 +4,11 @@ public interface WalletManagerPersistenceClient { + enum ChangeType { ADDED, UPDATED, DELETED } + void savePeers(WalletManager walletManager, Map data); void saveBlocks(WalletManager walletManager, Map data); - // TODO: Add persistence change type + void changeTransaction(WalletManager walletManager, ChangeType type, String hash, String data); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java index 0f9b7133a..a26a7fe52 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java @@ -1,4 +1,5 @@ package com.breadwallet.crypto.api.bitcoin; +// TODO: Add methods for any information we want to expose public interface BitcoinChainParams { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java index d617026aa..a624016d3 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java @@ -1,4 +1,5 @@ package com.breadwallet.crypto.api.bitcoin; +// TODO: Add methods for any information we want to expose public interface BitcoinMasterPubKey { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java index 49554769f..54d619c33 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java @@ -6,16 +6,4 @@ import java.util.Map; public interface BitcoinPersistenceClient extends WalletManagerPersistenceClient { - - @Override - default void savePeers(WalletManager walletManager, Map data) { - System.out.println("TST: BTC: savePeers"); - } - - @Override - default void saveBlocks(WalletManager walletManager, Map data) { - System.out.println("TST: BTC: saveBlocks"); - } - - // TODO: Add persistence change type } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java index 97b1c780a..d23465cda 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java @@ -1,4 +1,5 @@ package com.breadwallet.crypto.api.event; +// TODO: Create transfer events public interface TransferEvent { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java index 8af13168a..be92f2b12 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java @@ -1,4 +1,5 @@ package com.breadwallet.crypto.api.event; +// TODO: Create wallet events public interface WalletEvent { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java index c345c506e..bc5f8d7d7 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java @@ -1,4 +1,5 @@ package com.breadwallet.crypto.api.event; +// TODO: Create wallet manager events public interface WalletManagerEvent { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java index cadd45416..4a40c9d17 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java @@ -1,19 +1,19 @@ package com.breadwallet.crypto.core; import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; -import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinMasterPubKey; -import com.breadwallet.crypto.core.jni.BIP39; +import com.breadwallet.crypto.core.jni.Bip39; +// TODO: Review visibility (for class, methods, fields, etc.) public class Account implements com.breadwallet.crypto.api.Account { - private final BitcoinMasterPubKey masterPublicKey; + protected final BitcoinMasterPubKey masterPublicKey; public Account(String phrase) { - this(BIP39.deriveKey(phrase)); + this(Bip39.deriveKey(phrase)); } public Account(byte[] seed) { - this.masterPublicKey = new CoreBitcoinMasterPubKey(seed); + this.masterPublicKey = new com.breadwallet.crypto.core.bitcoin.BitcoinMasterPubKey(seed); } @Override diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCrypto.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCrypto.java new file mode 100644 index 000000000..1ed3b072b --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCrypto.java @@ -0,0 +1,9 @@ +package com.breadwallet.crypto.core; + +// TODO: Review visibility (for class, methods, fields, etc.) +public class CoreCrypto { + + public static void init() { + System.loadLibrary("crypto"); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java index 94fb15259..0437441dd 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java @@ -2,6 +2,7 @@ import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinChainParams; +// TODO: Review visibility (for class, methods, fields, etc.) public class BitcoinChainParams implements com.breadwallet.crypto.api.bitcoin.BitcoinChainParams { public static BitcoinChainParams TESTNET = new BitcoinChainParams(CoreBitcoinChainParams.TESTNET); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java index aa99cc566..6026465a5 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java @@ -2,7 +2,9 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinChainParams; +// TODO: Review visibility (for class, methods, fields, etc.) /* package */ class BitcoinChainParamsAdapter { + public static com.breadwallet.crypto.core.bitcoin.BitcoinChainParams from(BitcoinChainParams bcp) { if (bcp instanceof com.breadwallet.crypto.core.bitcoin.BitcoinChainParams) { return (com.breadwallet.crypto.core.bitcoin.BitcoinChainParams) bcp; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java index 3dc080c76..dfad6db02 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java @@ -2,6 +2,8 @@ import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinMasterPubKey; +// TODO: Add parameter validation +// TODO: Review visibility (for class, methods, fields, etc.) public class BitcoinMasterPubKey implements com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey { /* package */ final CoreBitcoinMasterPubKey masterPubKey; @@ -10,7 +12,7 @@ public BitcoinMasterPubKey(byte[] seed) { this(new CoreBitcoinMasterPubKey(seed)); } - public BitcoinMasterPubKey(CoreBitcoinMasterPubKey masterPubKey) { + private BitcoinMasterPubKey(CoreBitcoinMasterPubKey masterPubKey) { this.masterPubKey = masterPubKey; } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java index 6fe34c83f..c2fbff5f4 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java @@ -2,7 +2,9 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; +// TODO: Review visibility (for class, methods, fields, etc.) /* package */ class BitcoinMasterPubKeyAdapter { + public static com.breadwallet.crypto.core.bitcoin.BitcoinMasterPubKey from(BitcoinMasterPubKey mbk) { if (mbk instanceof com.breadwallet.crypto.core.bitcoin.BitcoinMasterPubKey) { return (com.breadwallet.crypto.core.bitcoin.BitcoinMasterPubKey) mbk; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java index 404a257a1..9969aa4e5 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java @@ -2,7 +2,9 @@ import com.breadwallet.crypto.api.Network; +// TODO: Review visibility (for class, methods, fields, etc.) public class BitcoinNetworks { + public static Network TESTNET = new Network( new Network.Bitcoin("BTC Testnet", 0x40, BitcoinChainParams.TESTNET)); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java index 43ede7ab6..d5e700954 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java @@ -2,6 +2,7 @@ import com.breadwallet.crypto.api.Account; import com.breadwallet.crypto.api.Network; +import com.breadwallet.crypto.api.WalletManager; import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; @@ -16,17 +17,19 @@ import java.lang.ref.WeakReference; import java.util.concurrent.Executor; -// TODO: It would be nice to have this exposed via a factory since the ctors are all the same +// TODO: Add parameter validation +// TODO: Review visibility (for class, methods, fields, etc.) +// TODO: Consider exposing this via a registered factory public final class BitcoinWalletManager - extends CommonWalletManager implements CoreBitcoinWalletManagerClient { + extends CommonWalletManager implements WalletManager, CoreBitcoinWalletManagerClient { - private final WeakReference listener; - private final CoreBitcoinWalletManager coreWalletManager; - private final CoreBitcoinPeerManager corePeerManager; - private final CoreBitcoinWallet coreWallet; + protected final WeakReference listener; + protected final CoreBitcoinWalletManager coreWalletManager; + protected final CoreBitcoinPeerManager corePeerManager; + protected final CoreBitcoinWallet coreWallet; - private final BitcoinPersistenceClient persistenceClient; - private final BitcoinBackendClient backendClient; + protected final BitcoinPersistenceClient persistenceClient; + protected final BitcoinBackendClient backendClient; public BitcoinWalletManager(BitcoinWalletManagerListener listener, Account account, @@ -74,7 +77,6 @@ public void connect() { @Override public void handleTransactionEvent() { - // TODO: implement me listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { @@ -85,7 +87,6 @@ public void handleTransactionEvent() { @Override public void handleWalletEvent() { - // TODO: implement me listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { @@ -96,7 +97,6 @@ public void handleWalletEvent() { @Override public void handleWalletManagerEvent() { - // TODO: implement me listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java index fbee5c9d7..6c18b2925 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java @@ -2,12 +2,13 @@ import com.breadwallet.crypto.core.common.jni.JniReference; +// TODO: Review visibility (for class, methods, fields, etc.) public class CoreBitcoinChainParams extends JniReference { - public static CoreBitcoinChainParams TESTNET = new CoreBitcoinChainParams(getTestnetParams()); + public static CoreBitcoinChainParams TESTNET = new CoreBitcoinChainParams(createTestnetChainParams()); - private static native long getTestnetParams(); + private static native long createTestnetChainParams(); protected CoreBitcoinChainParams(long jniReferenceAddress) { super(jniReferenceAddress); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java index 56f7e5532..24006e342 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java @@ -3,6 +3,7 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; import com.breadwallet.crypto.core.common.jni.JniReference; +// TODO: Review visibility (for class, methods, fields, etc.) public class CoreBitcoinMasterPubKey extends JniReference implements BitcoinMasterPubKey { diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java index bcefd9d4e..bcc934d79 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java @@ -2,10 +2,14 @@ import com.breadwallet.crypto.core.common.jni.JniReference; +// TODO: Review visibility (for class, methods, fields, etc.) public class CoreBitcoinPeerManager extends JniReference { + protected CoreBitcoinPeerManager(long jniReferenceAddress) { super(jniReferenceAddress); } public native void connect(); + + protected native void disposeNative(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java index 7d082ffae..c40373f73 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java @@ -2,8 +2,12 @@ import com.breadwallet.crypto.core.common.jni.JniReference; +// TODO: Review visibility (for class, methods, fields, etc.) public class CoreBitcoinWallet extends JniReference { + protected CoreBitcoinWallet(long jniReferenceAddress) { super(jniReferenceAddress); } + + protected native void disposeNative(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java index 4f7d20f32..77ea5e685 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java @@ -4,14 +4,20 @@ import java.lang.ref.WeakReference; -public class CoreBitcoinWalletManager - extends JniReference implements CoreBitcoinWalletManagerClient { +// TODO: Add parameter validation +// TODO: Hook up listener callbacks +// TODO: Review visibility (for class, methods, fields, etc.) +public class CoreBitcoinWalletManager extends JniReference { + + private static native void initializeNative(); private static native long createBitcoinWalletManager(CoreBitcoinMasterPubKey mpk, CoreBitcoinChainParams params, int earliestKeyTime, String storagePath); + static { initializeNative(); } + private final WeakReference client; public CoreBitcoinWalletManager(CoreBitcoinWalletManagerClient client, @@ -30,29 +36,5 @@ public CoreBitcoinWalletManager(CoreBitcoinWalletManagerClient client, public native void connect(); - // CoreBitcoinWalletManagerClient - - @Override - public void handleTransactionEvent() { - CoreBitcoinWalletManagerClient c = client.get(); - if (null != c) { - c.handleTransactionEvent(); - } - } - - @Override - public void handleWalletEvent() { - CoreBitcoinWalletManagerClient c = client.get(); - if (null != c) { - c.handleWalletEvent(); - } - } - - @Override - public void handleWalletManagerEvent() { - CoreBitcoinWalletManagerClient c = client.get(); - if (null != c) { - c.handleWalletManagerEvent(); - } - } + protected native void disposeNative(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java index 7bda8c010..09a64175b 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java @@ -1,5 +1,6 @@ package com.breadwallet.crypto.core.bitcoin.jni; +// TODO: Add parameters to callbacks public interface CoreBitcoinWalletManagerClient { void handleTransactionEvent(); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java index 99e8021d4..40fbe3fe6 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java @@ -7,6 +7,8 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; +// TODO: Add parameter validation +// TODO: Review visibility (for class, methods, fields, etc.) public abstract class CommonWalletManager implements WalletManager { protected static Executor DEFAULT_EXECUTOR = Executors.newSingleThreadExecutor(); @@ -20,14 +22,6 @@ public abstract class CommonWalletManager implements WalletManager { protected State state; - public CommonWalletManager(Account account, - Network network, - Mode mode, - long earliestKeyTime, - String storagePath) { - this(DEFAULT_EXECUTOR, account, network, mode, earliestKeyTime, storagePath); - } - public CommonWalletManager(Executor listenerExecutor, Account account, Network network, diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java index 314596412..864a1a8ef 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java @@ -1,15 +1,11 @@ package com.breadwallet.crypto.core.common.jni; +// TODO: Review visibility (for class, methods, fields, etc.) +// TODO: Add concept of owned (to handle case like CoreBitcoinWalletManager::getPeerManager() public class JniReference { - static { - try { System.loadLibrary("core"); } - catch (UnsatisfiedLinkError e) { - e.printStackTrace(); - System.err.println ("Native code library failed to load.\\n\" + " + e); - } - } protected static boolean SHOW_FINALIZE = false; + /** * C Pointer (as a Java long) to the underlying Breadwallet Core entity allocated from the * C heap memory. The referenced Core entity is used to implement native functions that @@ -30,9 +26,6 @@ protected JniReference () this.jniReferenceAddress = 0; } - // - // - // protected void finalize() throws Throwable { if (SHOW_FINALIZE) System.err.println("Finalize: " + toString()); dispose(); @@ -42,9 +35,9 @@ public void dispose() { disposeNative(); } - public native void disposeNative(); + protected native void disposeNative(); public String toString() { - return getClass().getName() + "@" + Integer.toHexString(hashCode()) + " JNI=" + Long.toHexString(jniReferenceAddress); + return getClass().getName() + "@" + Integer.toHexString(hashCode()); } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java index 795a18a2d..d1ae03d9d 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java @@ -1,5 +1,7 @@ package com.breadwallet.crypto.core.jni; -public class BIP39 { +// TODO: Review visibility (for class, methods, fields, etc.) +public class Bip39 { + public static native byte[] deriveKey(String phrase); } From 2cfc1b8488f99b024b52cd7bba1b9cb92b1174ad Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Mon, 11 Mar 2019 20:13:47 -0400 Subject: [PATCH 04/12] wip: Remove peermanager and wallet --- Java/Crypto.Makefile | 2 - Java/Crypto/CMakeLists.txt | 4 -- ..._core_bitcoin_jni_CoreBitcoinPeerManager.c | 51 ------------------- ..._core_bitcoin_jni_CoreBitcoinPeerManager.h | 29 ----------- ...rypto_core_bitcoin_jni_CoreBitcoinWallet.c | 40 --------------- ...rypto_core_bitcoin_jni_CoreBitcoinWallet.h | 21 -------- ...ore_bitcoin_jni_CoreBitcoinWalletManager.c | 45 ---------------- ...ore_bitcoin_jni_CoreBitcoinWalletManager.h | 24 --------- ...llet_crypto_core_common_jni_JniReference.c | 1 - .../core/bitcoin/BitcoinWalletManager.java | 7 --- .../bitcoin/jni/CoreBitcoinPeerManager.java | 15 ------ .../core/bitcoin/jni/CoreBitcoinWallet.java | 13 ----- .../bitcoin/jni/CoreBitcoinWalletManager.java | 8 --- .../crypto/core/common/jni/JniReference.java | 1 - 14 files changed, 261 deletions(-) delete mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.c delete mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h delete mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.c delete mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java diff --git a/Java/Crypto.Makefile b/Java/Crypto.Makefile index b37ed0e16..6b3a5c217 100644 --- a/Java/Crypto.Makefile +++ b/Java/Crypto.Makefile @@ -36,8 +36,6 @@ JAVA_SRCS=$(JAVA_SDIR)/api/bitcoin/BitcoinBackendClient.java \ $(JAVA_SDIR)/api/WalletManagerPersistenceClient.java \ $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinChainParams.java \ $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinMasterPubKey.java \ - $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinPeerManager.java \ - $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinWallet.java \ $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinWalletManager.java \ $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java \ $(JAVA_SDIR)/core/bitcoin/BitcoinChainParams.java \ diff --git a/Java/Crypto/CMakeLists.txt b/Java/Crypto/CMakeLists.txt index 638f68973..60f5527bf 100644 --- a/Java/Crypto/CMakeLists.txt +++ b/Java/Crypto/CMakeLists.txt @@ -238,10 +238,6 @@ target_sources (crypto src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.h src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c - src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h - src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.c - src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h - src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.c src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.c deleted file mode 100644 index 05e2e636d..000000000 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.c +++ /dev/null @@ -1,51 +0,0 @@ -// Created by Michael Carrara on 3/19/2019 -// Copyright (c) 2019 breadwallet LLC. -// -// 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. - -#include -#include - -#include "BRPeerManager.h" - -#include "BRCryptoJni.h" -#include "com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h" - -// TODO: Add defensive checks on inputs -// TODO: Re-write using personal coding style - -JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager_connect ( - JNIEnv * env, - jobject thisObject) -{ - BRPeerManager *peerManager = (BRPeerManager *) getJNIReference(env, thisObject); - BRPeerManagerConnect(peerManager); -} - -JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager_disposeNative ( - JNIEnv * env, - jobject thisObject) -{ - BRPeerManager *peerManager = (BRPeerManager *) getJNIReference(env, thisObject); - - if (NULL != peerManager) { - assert (BRPeerStatusDisconnected == BRPeerManagerConnectStatus(peerManager)); - BRPeerManagerFree(peerManager); - } -} \ No newline at end of file diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h deleted file mode 100644 index 478d7bb99..000000000 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager.h +++ /dev/null @@ -1,29 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager */ - -#ifndef _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager -#define _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager - * Method: connect - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager_connect - (JNIEnv *, jobject); - -/* - * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager - * Method: disposeNative - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinPeerManager_disposeNative - (JNIEnv *, jobject); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.c deleted file mode 100644 index 9918d4bfe..000000000 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.c +++ /dev/null @@ -1,40 +0,0 @@ -// Created by Michael Carrara on 3/19/2019 -// Copyright (c) 2019 breadwallet LLC. -// -// 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. - -#include -#include - -#include "BRWallet.h" - -#include "BRCryptoJni.h" -#include "com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h" - -// TODO: Add defensive checks on inputs -// TODO: Re-write using personal coding style - -JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet_disposeNative ( - JNIEnv * env, - jobject thisObject) -{ - BRWallet *wallet = (BRWallet *) getJNIReference(env, thisObject); - - if (NULL != wallet) BRWalletFree(wallet); -} \ No newline at end of file diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h deleted file mode 100644 index 89fcb06b1..000000000 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h +++ /dev/null @@ -1,21 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet */ - -#ifndef _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet -#define _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet - * Method: disposeNative - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet_disposeNative - (JNIEnv *, jobject); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c index d99e8e19e..a3bfd771d 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c @@ -30,14 +30,6 @@ // TODO: Add defensive checks on inputs // TODO: Wire in callbacks to Java layer // TODO: Re-write using personal coding style -// TODO: Return borrowed CoreBitcoinWallet in getWallet() -// TODO: Return borrowed CoreBitcoinPeerManager in getPeerManager() - -static jclass peerManagerClass; -static jmethodID peerManagerConstructor; - -static jclass walletClass; -static jmethodID walletConstructor; static void transactionEventCallback (BRWalletManager manager, @@ -54,25 +46,6 @@ static void walletManagerEventCallback (BRWalletManager manager, BRWalletManagerEvent event); -JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_initializeNative ( - JNIEnv * env, - jclass thisClass) -{ - peerManagerClass = (*env)->FindClass(env, "com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager"); - assert (NULL != peerManagerClass); - peerManagerClass = (*env)->NewGlobalRef (env, peerManagerClass); - - peerManagerConstructor = (*env)->GetMethodID(env, peerManagerClass, "", "(J)V"); - assert (NULL != peerManagerConstructor); - - walletClass = (*env)->FindClass(env, "com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet"); - assert (NULL != walletClass); - walletClass = (*env)->NewGlobalRef (env, walletClass); - - walletConstructor = (*env)->GetMethodID(env, walletClass, "", "(J)V"); - assert (NULL != walletConstructor); -} - JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_createBitcoinWalletManager ( JNIEnv * env, jclass thisClass, @@ -93,24 +66,6 @@ JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoin return (jlong) walletManager; } -JNIEXPORT jobject JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_getPeerManager ( - JNIEnv * env, - jobject thisObject) -{ - BRWalletManager *walletManager = (BRWalletManager *) getJNIReference(env, thisObject); - BRPeerManager *peerManager = BRWalletManagerGetPeerManager(walletManager); - return (*env)->NewObject (env, peerManagerClass, peerManagerConstructor, (jlong) peerManager); -} - -JNIEXPORT jobject JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_getWallet ( - JNIEnv * env, - jobject thisObject) -{ - BRWalletManager *walletManager = (BRWalletManager *) getJNIReference(env, thisObject); - BRWallet *wallet= BRWalletManagerGetPeerManager(walletManager); - return (*env)->NewObject (env, walletClass, walletConstructor, (jlong) wallet); -} - JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_connect ( JNIEnv * env, jobject thisObject) diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h index e72f13a4a..aebe43cb7 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h @@ -7,14 +7,6 @@ #ifdef __cplusplus extern "C" { #endif -/* - * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager - * Method: initializeNative - * Signature: ()V - */ -JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_initializeNative - (JNIEnv *, jclass); - /* * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager * Method: createBitcoinWalletManager @@ -23,22 +15,6 @@ JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinW JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_createBitcoinWalletManager (JNIEnv *, jclass, jobject, jobject, jint, jstring); -/* - * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager - * Method: getPeerManager - * Signature: ()Lcom/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager; - */ -JNIEXPORT jobject JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_getPeerManager - (JNIEnv *, jobject); - -/* - * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager - * Method: getWallet - * Signature: ()Lcom/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet; - */ -JNIEXPORT jobject JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_getWallet - (JNIEnv *, jobject); - /* * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager * Method: connect diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c index 163fc1a3d..98a4361a1 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c @@ -27,7 +27,6 @@ // TODO: Add defensive checks on inputs // TODO: Re-write using personal coding style -// TODO: Add concept of owned (to handle case like CoreBitcoinWalletManager::getPeerManager() #define JNI_REFERENCE_ADDRESS_FIELD_NAME "jniReferenceAddress" #define JNI_REFERENCE_ADDRESS_FIELD_TYPE "J" // long diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java index d5e700954..15494bce9 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java @@ -8,8 +8,6 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinChainParams; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinMasterPubKey; -import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinPeerManager; -import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWallet; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWalletManager; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWalletManagerClient; import com.breadwallet.crypto.core.common.CommonWalletManager; @@ -25,8 +23,6 @@ public final class BitcoinWalletManager protected final WeakReference listener; protected final CoreBitcoinWalletManager coreWalletManager; - protected final CoreBitcoinPeerManager corePeerManager; - protected final CoreBitcoinWallet coreWallet; protected final BitcoinPersistenceClient persistenceClient; protected final BitcoinBackendClient backendClient; @@ -61,8 +57,6 @@ public BitcoinWalletManager(Executor listenerExector, this.listener = new WeakReference<>(listener); this.coreWalletManager = new CoreBitcoinWalletManager(this, masterPubKey, chainParams, earliestKeyTime, storagePath); - this.corePeerManager = coreWalletManager.getPeerManager(); - this.coreWallet = coreWalletManager.getWallet(); } // WalletManager @@ -70,7 +64,6 @@ public BitcoinWalletManager(Executor listenerExector, @Override public void connect() { coreWalletManager.connect(); - corePeerManager.connect(); } // CoreWalletManagerClient diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java deleted file mode 100644 index bcc934d79..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinPeerManager.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.breadwallet.crypto.core.bitcoin.jni; - -import com.breadwallet.crypto.core.common.jni.JniReference; - -// TODO: Review visibility (for class, methods, fields, etc.) -public class CoreBitcoinPeerManager extends JniReference { - - protected CoreBitcoinPeerManager(long jniReferenceAddress) { - super(jniReferenceAddress); - } - - public native void connect(); - - protected native void disposeNative(); -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java deleted file mode 100644 index c40373f73..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.breadwallet.crypto.core.bitcoin.jni; - -import com.breadwallet.crypto.core.common.jni.JniReference; - -// TODO: Review visibility (for class, methods, fields, etc.) -public class CoreBitcoinWallet extends JniReference { - - protected CoreBitcoinWallet(long jniReferenceAddress) { - super(jniReferenceAddress); - } - - protected native void disposeNative(); -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java index 77ea5e685..ae372adad 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java @@ -9,15 +9,11 @@ // TODO: Review visibility (for class, methods, fields, etc.) public class CoreBitcoinWalletManager extends JniReference { - private static native void initializeNative(); - private static native long createBitcoinWalletManager(CoreBitcoinMasterPubKey mpk, CoreBitcoinChainParams params, int earliestKeyTime, String storagePath); - static { initializeNative(); } - private final WeakReference client; public CoreBitcoinWalletManager(CoreBitcoinWalletManagerClient client, @@ -30,10 +26,6 @@ public CoreBitcoinWalletManager(CoreBitcoinWalletManagerClient client, this.jniReferenceAddress = createBitcoinWalletManager(mpk, params, earliestKeyTime, storagePath); } - public native CoreBitcoinPeerManager getPeerManager(); - - public native CoreBitcoinWallet getWallet(); - public native void connect(); protected native void disposeNative(); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java index 864a1a8ef..a256c9ad5 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java @@ -1,7 +1,6 @@ package com.breadwallet.crypto.core.common.jni; // TODO: Review visibility (for class, methods, fields, etc.) -// TODO: Add concept of owned (to handle case like CoreBitcoinWalletManager::getPeerManager() public class JniReference { protected static boolean SHOW_FINALIZE = false; From cf75c395695990a06646076d8e0908b65d2bb353 Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Mon, 11 Mar 2019 22:13:58 -0400 Subject: [PATCH 05/12] wip: Add callback from JNI to Java --- .../coredemo/CoreDemoBitcoinClient.java | 22 ++- .../coredemo/WalletNavigationActivity.java | 6 +- Java/Crypto.Makefile | 7 + ...ore_bitcoin_jni_CoreBitcoinWalletManager.c | 133 ++++++++++++-- ...ore_bitcoin_jni_CoreBitcoinWalletManager.h | 16 ++ .../breadwallet/crypto/api/WalletManager.java | 2 + .../crypto/api/WalletManagerListener.java | 4 +- .../crypto/api/event/WalletEvent.java | 1 - .../api/event/WalletEventBalanceUpdated.java | 14 ++ .../crypto/api/event/WalletEventCreated.java | 4 + .../crypto/api/event/WalletEventDeleted.java | 4 + .../crypto/api/event/WalletManagerEvent.java | 1 - .../event/WalletManagerEventConnected.java | 4 + .../event/WalletManagerEventDisconnected.java | 4 + .../event/WalletManagerEventSyncStarted.java | 4 + .../event/WalletManagerEventSyncStopped.java | 14 ++ .../core/bitcoin/BitcoinWalletManager.java | 98 ++++++++++- .../bitcoin/jni/CoreBitcoinChainParams.java | 2 +- .../bitcoin/jni/CoreBitcoinMasterPubKey.java | 2 +- .../bitcoin/jni/CoreBitcoinWalletManager.java | 162 +++++++++++++++++- .../jni/CoreBitcoinWalletManagerClient.java | 14 +- .../crypto/core/common/jni/JniReference.java | 5 - 22 files changed, 478 insertions(+), 45 deletions(-) create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventBalanceUpdated.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventCreated.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventDeleted.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventConnected.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventDisconnected.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStarted.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStopped.java diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java index 443e2999c..5212ee5b9 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java @@ -1,6 +1,10 @@ package com.breadwallet.coredemo; +import android.util.Log; + import com.breadwallet.crypto.api.Network; +import com.breadwallet.crypto.api.Transfer; +import com.breadwallet.crypto.api.Wallet; import com.breadwallet.crypto.api.WalletManager; import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; @@ -17,6 +21,8 @@ public class CoreDemoBitcoinClient implements BitcoinWalletManagerListener, BitcoinBackendClient, BitcoinPersistenceClient { + private static final String TAG = "CoreDemoBitcoinClient"; + static { CoreCrypto.init(); } private final WalletManager walletManager; @@ -37,28 +43,32 @@ public void connect() { walletManager.connect(); } + public void disconnect() { + walletManager.disconnect(); + } + // BitcoinWalletManagerListener @Override public void handleManagerEvent(WalletManager manager, WalletManagerEvent event) { - + Log.d(TAG, "handleManagerEvent: " + event.toString()); } @Override - public void handleTransferEvent(WalletManager manager, TransferEvent event) { - + public void handleTransferEvent(WalletManager manager, Wallet wallet, Transfer transfer, TransferEvent event) { + Log.d(TAG, "handleTransferEvent: " + event.toString()); } @Override - public void handleWalletEvent(WalletManager manager, WalletEvent event) { - + public void handleWalletEvent(WalletManager manager, Wallet wallet, WalletEvent event) { + Log.d(TAG, "handleWalletEvent: " + event.toString()); } // BitcoinBackendClient @Override public boolean isReachable() { - return false; + return true; } // BitcoinPersistenceClient diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java index db4806034..08600db44 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java @@ -10,7 +10,6 @@ public class WalletNavigationActivity extends AppCompatActivity { - CoreDemoBitcoinClient btcClient = null; CoreDemoEthereumClient ethClient = null; private void deleteRecursively (File file) { @@ -32,11 +31,12 @@ protected void onCreate(Bundle savedInstanceState) { if (storageFile.exists()) deleteRecursively(storageFile); storageFile.mkdirs(); - btcClient = new CoreDemoBitcoinClient(BitcoinNetworks.TESTNET, + CoreDemoBitcoinClient btcClient = new CoreDemoBitcoinClient(BitcoinNetworks.TESTNET, storageFile.getAbsolutePath(), - "boring head harsh green empty clip fatal typical found crane dinner timber"); + "0xa9de3dbd7d561e67527bc1ecb025c59d53b9f7ef"); btcClient.connect(); + btcClient.disconnect(); System.gc(); } diff --git a/Java/Crypto.Makefile b/Java/Crypto.Makefile index 6b3a5c217..0e26aa8bb 100644 --- a/Java/Crypto.Makefile +++ b/Java/Crypto.Makefile @@ -27,7 +27,14 @@ JAVA_SRCS=$(JAVA_SDIR)/api/bitcoin/BitcoinBackendClient.java \ $(JAVA_SDIR)/api/bitcoin/BitcoinWalletManagerListener.java \ $(JAVA_SDIR)/api/event/TransferEvent.java \ $(JAVA_SDIR)/api/event/WalletEvent.java \ + $(JAVA_SDIR)/api/event/WalletEventBalanceUpdated.java \ + $(JAVA_SDIR)/api/event/WalletEventCreated.java \ + $(JAVA_SDIR)/api/event/WalletEventDeleted.java \ $(JAVA_SDIR)/api/event/WalletManagerEvent.java \ + $(JAVA_SDIR)/api/event/WalletManagerEventConnected.java \ + $(JAVA_SDIR)/api/event/WalletManagerEventDisconnected.java \ + $(JAVA_SDIR)/api/event/WalletManagerEventSyncStarted.java \ + $(JAVA_SDIR)/api/event/WalletManagerEventSyncStopped.java \ $(JAVA_SDIR)/api/Account.java \ $(JAVA_SDIR)/api/Network.java \ $(JAVA_SDIR)/api/WalletManager.java \ diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c index a3bfd771d..1a828c0b0 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c @@ -20,17 +20,34 @@ // THE SOFTWARE. #include +#include #include +#include #include "BRWalletManager.h" #include "BRCryptoJni.h" #include "com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h" +#define LOG_TAG "CoreBitcoinWalletManager" + // TODO: Add defensive checks on inputs -// TODO: Wire in callbacks to Java layer // TODO: Re-write using personal coding style +static jclass trampolineClass = NULL; +static jmethodID trampolineHandleTransactionAdded = NULL; +static jmethodID trampolineHandleTransactionUpdated = NULL; +static jmethodID trampolineHandleTransactionDeleted = NULL; + +static jmethodID trampolineHandleWalletCreated = NULL; +static jmethodID trampolineHandleWalletBalanceUpdated = NULL; +static jmethodID trampolineHandleWalletDeleted = NULL; + +static jmethodID trampolineHandleWalletManagerConnected = NULL; +static jmethodID trampolineHandleWalletManagerDisconnected = NULL; +static jmethodID trampolineHandleWalletManagerSyncStarted = NULL; +static jmethodID trampolineHandleWalletManagerSyncStopped = NULL; + static void transactionEventCallback (BRWalletManager manager, BRWallet *wallet, @@ -46,6 +63,30 @@ static void walletManagerEventCallback (BRWalletManager manager, BRWalletManagerEvent event); +static jmethodID +trampolineOrFatal (JNIEnv *env, const char *name, const char *signature); + +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_initializeNative ( + JNIEnv * env, + jclass thisClass) +{ + if (NULL != trampolineClass) return; + trampolineClass = (*env)->NewGlobalRef(env, thisClass); + + trampolineHandleTransactionAdded = trampolineOrFatal (env, "handleTransactionAdded", "(JJJ)V"); + trampolineHandleTransactionUpdated = trampolineOrFatal (env, "handleTransactionUpdated", "(JJJ)V"); + trampolineHandleTransactionDeleted = trampolineOrFatal (env, "handleTransactionDeleted", "(JJJ)V"); + + trampolineHandleWalletCreated = trampolineOrFatal (env, "handleWalletCreated", "(JJ)V"); + trampolineHandleWalletBalanceUpdated = trampolineOrFatal (env, "handleWalletBalanceUpdated", "(JJJ)V"); + trampolineHandleWalletDeleted = trampolineOrFatal (env, "handleWalletDeleted", "(JJ)V"); + + trampolineHandleWalletManagerConnected = trampolineOrFatal (env, "handleWalletManagerConnected", "(J)V"); + trampolineHandleWalletManagerDisconnected = trampolineOrFatal (env, "handleWalletManagerDisconnected", "(J)V"); + trampolineHandleWalletManagerSyncStarted = trampolineOrFatal (env, "handleWalletManagerSyncStarted", "(J)V"); + trampolineHandleWalletManagerSyncStopped = trampolineOrFatal (env, "handleWalletManagerSyncStopped", "(JI)V"); +} + JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_createBitcoinWalletManager ( JNIEnv * env, jclass thisClass, @@ -74,6 +115,14 @@ JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinW BRWalletManagerConnect(walletManager); } +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_disconnect ( + JNIEnv * env, + jobject thisObject) +{ + BRWalletManager *walletManager = (BRWalletManager *) getJNIReference(env, thisObject); + BRWalletManagerDisconnect(walletManager); +} + JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_disposeNative ( JNIEnv * env, jobject thisObject) @@ -82,23 +131,81 @@ JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinW if (NULL != walletManager) BRWalletManagerFree(walletManager); } + +static jmethodID +trampolineOrFatal (JNIEnv *env, const char *name, const char *signature) { + jmethodID method = (*env)->GetStaticMethodID (env, trampolineClass, name, signature); + assert (NULL != method); + return method; +} + + static void -transactionEventCallback (BRWalletManager manager, - BRWallet *wallet, - BRTransaction *transaction, - BRTransactionEvent event) { - printf ("TST: TransactionEvent: %d\n", event.type); +transactionEventCallback (BRWalletManager wmid, + BRWallet *wid, + BRTransaction *tid, + BRTransactionEvent e) { + JNIEnv *env = getEnv(); + if (NULL == env) return; + + __android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, "TransactionEvent: %d\n", e.type); + + switch (e.type) { + case BITCOIN_TRANSACTION_ADDED: + (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleTransactionAdded, (jlong) wmid, (jlong) wid, (jlong) tid); + break; + case BITCOIN_TRANSACTION_UPDATED: + (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleTransactionUpdated, (jlong) wmid, (jlong) wid, (jlong) tid); + break; + case BITCOIN_TRANSACTION_DELETED: + (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleTransactionDeleted, (jlong) wmid, (jlong) wid, (jlong) tid); + break; + } + } static void -walletEventCallback (BRWalletManager manager, - BRWallet *wallet, - BRWalletEvent event) { - printf ("TST: WalletEvent: %d\n", event.type); +walletEventCallback (BRWalletManager wmid, + BRWallet *wid, + BRWalletEvent e) { + JNIEnv *env = getEnv(); + if (NULL == env) return; + + __android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, "WalletEvent: %d\n", e.type); + + switch (e.type) { + case BITCOIN_WALLET_CREATED: + (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleWalletCreated, (jlong) wmid, (jlong) wid); + break; + case BITCOIN_WALLET_BALANCE_UPDATED: + (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleWalletBalanceUpdated, (jlong) wmid, (jlong) wid, (jlong) e.u.balance.satoshi); + break; + case BITCOIN_WALLET_DELETED: + (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleWalletDeleted, (jlong) wmid, (jlong) wid); + break; + } } static void -walletManagerEventCallback (BRWalletManager manager, - BRWalletManagerEvent event) { - printf ("TST: WalletManagerEvent: %d\n", event.type); +walletManagerEventCallback (BRWalletManager wmid, + BRWalletManagerEvent e) { + JNIEnv *env = getEnv(); + if (NULL == env) return; + + __android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, "WalletManagerEvent: %d\n", e.type); + + switch (e.type) { + case BITCOIN_WALLET_MANAGER_CONNECTED: + (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleWalletManagerConnected, (jlong) wmid); + break; + case BITCOIN_WALLET_MANAGER_DISCONNECTED: + (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleWalletManagerDisconnected, (jlong) wmid); + break; + case BITCOIN_WALLET_MANAGER_SYNC_STARTED: + (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleWalletManagerSyncStarted, (jlong) wmid); + break; + case BITCOIN_WALLET_MANAGER_SYNC_STOPPED: + (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleWalletManagerSyncStopped, (jlong) wmid, (jint) e.u.syncStopped.error); + break; + } } diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h index aebe43cb7..9263f6c5d 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h @@ -15,6 +15,14 @@ extern "C" { JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_createBitcoinWalletManager (JNIEnv *, jclass, jobject, jobject, jint, jstring); +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager + * Method: initializeNative + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_initializeNative + (JNIEnv *, jclass); + /* * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager * Method: connect @@ -23,6 +31,14 @@ JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoin JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_connect (JNIEnv *, jobject); +/* + * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager + * Method: disconnect + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager_disconnect + (JNIEnv *, jobject); + /* * Class: com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager * Method: disposeNative diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java index 1f79aeb8b..10510164f 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java @@ -7,4 +7,6 @@ enum Mode { API_ONLY, API_WITH_P2P_SUBMIT, P2P_WITH_API_SYNC, P2P_ONLY, } enum State { CREATED, DISCONNECTED, CONNECTED, SYNCING, DELETED } void connect(); + + void disconnect(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java index 0c2ba607f..9dd423482 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java @@ -8,7 +8,7 @@ public interface WalletManagerListener { void handleManagerEvent(WalletManager manager, WalletManagerEvent event); - void handleTransferEvent(WalletManager manager, TransferEvent event); + void handleTransferEvent(WalletManager manager, Wallet wallet, Transfer transfer, TransferEvent event); - void handleWalletEvent(WalletManager manager, WalletEvent event); + void handleWalletEvent(WalletManager manager, Wallet wallet, WalletEvent event); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java index be92f2b12..8af13168a 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java @@ -1,5 +1,4 @@ package com.breadwallet.crypto.api.event; -// TODO: Create wallet events public interface WalletEvent { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventBalanceUpdated.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventBalanceUpdated.java new file mode 100644 index 000000000..7f6380048 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventBalanceUpdated.java @@ -0,0 +1,14 @@ +package com.breadwallet.crypto.api.event; + +public class WalletEventBalanceUpdated implements WalletEvent { + + private final long balance; + + public WalletEventBalanceUpdated(long balance) { + this.balance = balance; + } + + public long balance() { + return balance; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventCreated.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventCreated.java new file mode 100644 index 000000000..a915b6642 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventCreated.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.event; + +public class WalletEventCreated implements WalletEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventDeleted.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventDeleted.java new file mode 100644 index 000000000..70077b012 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventDeleted.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.event; + +public class WalletEventDeleted implements WalletEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java index bc5f8d7d7..c345c506e 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java @@ -1,5 +1,4 @@ package com.breadwallet.crypto.api.event; -// TODO: Create wallet manager events public interface WalletManagerEvent { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventConnected.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventConnected.java new file mode 100644 index 000000000..5be80061b --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventConnected.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.event; + +public class WalletManagerEventConnected implements WalletManagerEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventDisconnected.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventDisconnected.java new file mode 100644 index 000000000..5dea8b474 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventDisconnected.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.event; + +public class WalletManagerEventDisconnected implements WalletManagerEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStarted.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStarted.java new file mode 100644 index 000000000..c224a6ff4 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStarted.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.event; + +public class WalletManagerEventSyncStarted implements WalletManagerEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStopped.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStopped.java new file mode 100644 index 000000000..3e6982a5a --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStopped.java @@ -0,0 +1,14 @@ +package com.breadwallet.crypto.api.event; + +public class WalletManagerEventSyncStopped implements WalletManagerEvent { + + private final int errorCode; + + public WalletManagerEventSyncStopped(int errorCode) { + this.errorCode = errorCode; + } + + public int errorCode() { + return errorCode; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java index 15494bce9..145632d17 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java @@ -6,6 +6,14 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; +import com.breadwallet.crypto.api.event.WalletEvent; +import com.breadwallet.crypto.api.event.WalletEventBalanceUpdated; +import com.breadwallet.crypto.api.event.WalletEventCreated; +import com.breadwallet.crypto.api.event.WalletEventDeleted; +import com.breadwallet.crypto.api.event.WalletManagerEventConnected; +import com.breadwallet.crypto.api.event.WalletManagerEventDisconnected; +import com.breadwallet.crypto.api.event.WalletManagerEventSyncStarted; +import com.breadwallet.crypto.api.event.WalletManagerEventSyncStopped; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinChainParams; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinMasterPubKey; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWalletManager; @@ -66,34 +74,112 @@ public void connect() { coreWalletManager.connect(); } + @Override + public void disconnect() { + coreWalletManager.disconnect(); + } + // CoreWalletManagerClient @Override - public void handleTransactionEvent() { + public void handleTransactionAdded(long wid, long tid) { + listenerExecutor.execute(() -> { + BitcoinWalletManagerListener l = listener.get(); + if (l != null) { + // TODO: Implement this + } + }); + } + + @Override + public void handleTransactionUpdated(long wid, long tid) { + listenerExecutor.execute(() -> { + BitcoinWalletManagerListener l = listener.get(); + if (l != null) { + // TODO: Implement this + } + }); + } + + @Override + public void handleTransactionDeleted(long wid, long tid) { + listenerExecutor.execute(() -> { + BitcoinWalletManagerListener l = listener.get(); + if (l != null) { + // TODO: Implement this + } + }); + } + + @Override + public void handleWalletCreated(long wid) { + listenerExecutor.execute(() -> { + BitcoinWalletManagerListener l = listener.get(); + if (l != null) { + // TODO: Get wallet using wid + l.handleWalletEvent(this, null, new WalletEventCreated()); + } + }); + } + + @Override + public void handleWalletBalanceUpdated(long wid, long satoshi) { + listenerExecutor.execute(() -> { + BitcoinWalletManagerListener l = listener.get(); + if (l != null) { + // TODO: Get wallet using wid + l.handleWalletEvent(this, null, new WalletEventBalanceUpdated(satoshi)); + } + }); + } + + @Override + public void handleWalletDeleted(long wid) { + listenerExecutor.execute(() -> { + BitcoinWalletManagerListener l = listener.get(); + if (l != null) { + // TODO: Get wallet using wid + l.handleWalletEvent(this, null, new WalletEventDeleted()); + } + }); + } + + @Override + public void handleWalletManagerConnected() { + listenerExecutor.execute(() -> { + BitcoinWalletManagerListener l = listener.get(); + if (l != null) { + l.handleManagerEvent(this, new WalletManagerEventConnected()); + } + }); + } + + @Override + public void handleWalletManagerDisconnected() { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - l.handleTransferEvent(this, null); + l.handleManagerEvent(this, new WalletManagerEventDisconnected()); } }); } @Override - public void handleWalletEvent() { + public void handleWalletManagerSyncStarted() { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - l.handleWalletEvent(this, null); + l.handleManagerEvent(this, new WalletManagerEventSyncStarted()); } }); } @Override - public void handleWalletManagerEvent() { + public void handleWalletManagerSyncStopped(int errorCode) { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - l.handleManagerEvent(this, null); + l.handleManagerEvent(this, new WalletManagerEventSyncStopped(errorCode)); } }); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java index 6c18b2925..7f41281f5 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java @@ -10,7 +10,7 @@ public class CoreBitcoinChainParams private static native long createTestnetChainParams(); - protected CoreBitcoinChainParams(long jniReferenceAddress) { + private CoreBitcoinChainParams(long jniReferenceAddress) { super(jniReferenceAddress); } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java index 24006e342..da96a2dba 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java @@ -13,7 +13,7 @@ public CoreBitcoinMasterPubKey(byte[] seed) { this(createBitcoinMasterPubKey(seed)); } - protected CoreBitcoinMasterPubKey(long jniReferenceAddress) { + private CoreBitcoinMasterPubKey(long jniReferenceAddress) { super(jniReferenceAddress); } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java index ae372adad..a132ce518 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java @@ -3,17 +3,100 @@ import com.breadwallet.crypto.core.common.jni.JniReference; import java.lang.ref.WeakReference; +import java.util.HashMap; +import java.util.Map; // TODO: Add parameter validation // TODO: Hook up listener callbacks // TODO: Review visibility (for class, methods, fields, etc.) public class CoreBitcoinWalletManager extends JniReference { + private static Map> wmMap = new HashMap<>(); + private static native long createBitcoinWalletManager(CoreBitcoinMasterPubKey mpk, CoreBitcoinChainParams params, int earliestKeyTime, String storagePath); + private static native void initializeNative(); + + private static CoreBitcoinWalletManager lookupWM (long wmid) { + WeakReference wm = wmMap.get(wmid); + return (null== wm ? null : wm.get()); + } + + private static void handleTransactionAdded(long wmid, long wid, long tid) { + CoreBitcoinWalletManager walletManager = lookupWM(wmid); + if (null != walletManager) { + walletManager.handleTransactionAdded(wid, tid); + } + } + + private static void handleTransactionUpdated(long wmid, long wid, long tid) { + CoreBitcoinWalletManager walletManager = lookupWM(wmid); + if (null != walletManager) { + walletManager.handleTransactionUpdated(wid, tid); + } + } + + private static void handleTransactionDeleted(long wmid, long wid, long tid) { + CoreBitcoinWalletManager walletManager = lookupWM(wmid); + if (null != walletManager) { + walletManager.handleTransactionDeleted(wid, tid); + } + } + + private static void handleWalletCreated(long wmid, long wid) { + CoreBitcoinWalletManager walletManager = lookupWM(wmid); + if (null != walletManager) { + walletManager.handleWalletCreated(wid); + } + } + + private static void handleWalletBalanceUpdated(long wmid, long wid, long satoshi) { + CoreBitcoinWalletManager walletManager = lookupWM(wmid); + if (null != walletManager) { + walletManager.handleWalletBalanceUpdated(wid, satoshi); + } + } + + private static void handleWalletDeleted(long wmid, long wid) { + CoreBitcoinWalletManager walletManager = lookupWM(wmid); + if (null != walletManager) { + walletManager.handleWalletDeleted(wid); + } + } + + private static void handleWalletManagerConnected(long wmid) { + CoreBitcoinWalletManager walletManager = lookupWM(wmid); + if (null != walletManager) { + walletManager.handleWalletManagerConnected(); + } + } + + private static void handleWalletManagerDisconnected(long wmid) { + CoreBitcoinWalletManager walletManager = lookupWM(wmid); + if (null != walletManager) { + walletManager.handleWalletManagerDisconnected(); + } + } + + private static void handleWalletManagerSyncStarted(long wmid) { + CoreBitcoinWalletManager walletManager = lookupWM(wmid); + if (null != walletManager) { + walletManager.handleWalletManagerSyncStarted(); + } + } + + private static void handleWalletManagerSyncStopped(long wmid, int errorCode) { + CoreBitcoinWalletManager walletManager = lookupWM(wmid); + if (null != walletManager) { + walletManager.handleWalletManagerSyncStopped(errorCode); + } + } + + static { initializeNative(); } + private final WeakReference client; public CoreBitcoinWalletManager(CoreBitcoinWalletManagerClient client, @@ -21,12 +104,87 @@ public CoreBitcoinWalletManager(CoreBitcoinWalletManagerClient client, CoreBitcoinChainParams params, int earliestKeyTime, String storagePath) { - // client needs to be in place before BRWalletManagerNew is invoked, as it calls back + // ISSUE: we will miss the first event because we are not registerd in the map yet + super(createBitcoinWalletManager(mpk, params, earliestKeyTime, storagePath)); + + wmMap.put(jniReferenceAddress, new WeakReference<> (this)); + this.client = new WeakReference<>(client); - this.jniReferenceAddress = createBitcoinWalletManager(mpk, params, earliestKeyTime, storagePath); } public native void connect(); + public native void disconnect(); + protected native void disposeNative(); + + private void handleTransactionAdded(long wid, long tid) { + CoreBitcoinWalletManagerClient wmc = client.get(); + if (null != wmc) { + wmc.handleTransactionAdded(wid, tid); + } + } + + private void handleTransactionUpdated(long wid, long tid) { + CoreBitcoinWalletManagerClient wmc = client.get(); + if (null != wmc) { + wmc.handleTransactionUpdated(wid, tid); + } + } + + private void handleTransactionDeleted(long wid, long tid) { + CoreBitcoinWalletManagerClient wmc = client.get(); + if (null != wmc) { + wmc.handleTransactionDeleted(wid, tid); + } + } + + private void handleWalletCreated(long wid) { + CoreBitcoinWalletManagerClient wmc = client.get(); + if (null != wmc) { + wmc.handleWalletCreated(wid); + } + } + + private void handleWalletBalanceUpdated(long wid, long satoshi) { + CoreBitcoinWalletManagerClient wmc = client.get(); + if (null != wmc) { + wmc.handleWalletBalanceUpdated(wid, satoshi); + } + } + + private void handleWalletDeleted(long wid) { + CoreBitcoinWalletManagerClient wmc = client.get(); + if (null != wmc) { + wmc.handleWalletDeleted(wid); + } + } + + private void handleWalletManagerConnected() { + CoreBitcoinWalletManagerClient wmc = client.get(); + if (null != wmc) { + wmc.handleWalletManagerConnected(); + } + } + + private void handleWalletManagerDisconnected() { + CoreBitcoinWalletManagerClient wmc = client.get(); + if (null != wmc) { + wmc.handleWalletManagerDisconnected(); + } + } + + private void handleWalletManagerSyncStarted() { + CoreBitcoinWalletManagerClient wmc = client.get(); + if (null != wmc) { + wmc.handleWalletManagerSyncStarted(); + } + } + + private void handleWalletManagerSyncStopped(int errorCode) { + CoreBitcoinWalletManagerClient wmc = client.get(); + if (null != wmc) { + wmc.handleWalletManagerSyncStopped(errorCode); + } + } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java index 09a64175b..88587672c 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java @@ -1,11 +1,17 @@ package com.breadwallet.crypto.core.bitcoin.jni; -// TODO: Add parameters to callbacks public interface CoreBitcoinWalletManagerClient { - void handleTransactionEvent(); + void handleTransactionAdded(long wid, long tid); + void handleTransactionUpdated(long wid, long tid); + void handleTransactionDeleted(long wid, long tid); - void handleWalletEvent(); + void handleWalletCreated(long wid); + void handleWalletBalanceUpdated(long wid, long satoshi); + void handleWalletDeleted(long wid); - void handleWalletManagerEvent(); + void handleWalletManagerConnected(); + void handleWalletManagerDisconnected(); + void handleWalletManagerSyncStarted(); + void handleWalletManagerSyncStopped(int errorCode); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java index a256c9ad5..fcf00200c 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java @@ -20,11 +20,6 @@ protected JniReference (long jniReferenceAddress) this.jniReferenceAddress = jniReferenceAddress; } - protected JniReference () - { - this.jniReferenceAddress = 0; - } - protected void finalize() throws Throwable { if (SHOW_FINALIZE) System.err.println("Finalize: " + toString()); dispose(); From 7d331f6d09af62cd1ac57c78680ba65280d57d81 Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Tue, 12 Mar 2019 21:20:05 -0400 Subject: [PATCH 06/12] wip: Add provider abstraction layer --- .../coredemo/CoreDemoBitcoinClient.java | 17 ++- .../coredemo/WalletNavigationActivity.java | 10 +- Java/Crypto.Makefile | 116 ++++++++++-------- .../crypto/api/AccountFactory.java | 18 +++ .../com/breadwallet/crypto/api/Address.java | 2 +- .../com/breadwallet/crypto/api/Amount.java | 1 + .../com/breadwallet/crypto/api/CryptoApi.java | 17 +++ .../com/breadwallet/crypto/api/Currency.java | 1 + .../com/breadwallet/crypto/api/Network.java | 1 + .../com/breadwallet/crypto/api/Transfer.java | 48 +------- .../java/com/breadwallet/crypto/api/Unit.java | 1 + .../com/breadwallet/crypto/api/Wallet.java | 51 +------- .../crypto/api/WalletManagerFactory.java | 54 ++++++++ .../crypto/api/WalletManagerListener.java | 14 --- .../crypto/api/bitcoin/Bitcoin.java | 18 +++ .../api/bitcoin/BitcoinBackendClient.java | 2 +- .../api/bitcoin/BitcoinPersistenceClient.java | 5 +- .../bitcoin/BitcoinWalletManagerListener.java | 2 +- .../crypto/api/ethereum/Ethereum.java | 22 ---- .../crypto/api/ethereum/Transfer.java | 19 --- .../crypto/api/ethereum/Wallet.java | 47 ------- .../crypto/api/event/WalletEvent.java | 4 - .../api/event/WalletEventBalanceUpdated.java | 14 --- .../crypto/api/event/WalletEventCreated.java | 4 - .../crypto/api/event/WalletEventDeleted.java | 4 - .../crypto/api/event/WalletManagerEvent.java | 4 - .../event/WalletManagerEventConnected.java | 4 - .../event/WalletManagerEventDisconnected.java | 4 - .../event/WalletManagerEventSyncStarted.java | 4 - .../event/WalletManagerEventSyncStopped.java | 14 --- .../transfer}/TransferEvent.java | 2 +- .../wallet/BalanceUpdatedWalletEvent.java | 14 +++ .../api/events/wallet/CreatedWalletEvent.java | 4 + .../api/events/wallet/DeletedWalletEvent.java | 4 + .../crypto/api/events/wallet/WalletEvent.java | 4 + .../ConnectedWalletManagerEvent.java | 4 + .../DisconnectedWalletManagerEvent.java | 4 + .../SyncStartedWalletManagerEvent.java | 4 + .../SyncStoppedWalletManagerEvent.java | 14 +++ .../walletmanager/WalletManagerEvent.java | 4 + .../crypto/api/provider/AccountProvider.java | 10 ++ .../api/provider/BitcoinNetworkProvider.java | 10 ++ .../BitcoinWalletManagerProvider.java | 32 +++++ .../api/provider/CryptoApiProvider.java | 10 ++ .../WalletManagerBackendClient.java | 2 +- .../walletmanager/WalletManagerListener.java | 17 +++ .../WalletManagerPersistenceClient.java | 4 +- .../breadwallet/crypto/core/CoreCrypto.java | 9 -- .../crypto/core/CoreCryptoApi.java | 100 +++++++++++++++ .../core/bitcoin/BitcoinChainParams.java | 1 + .../crypto/core/bitcoin/BitcoinNetworks.java | 10 -- .../core/bitcoin/BitcoinWalletManager.java | 60 +++++---- .../bitcoin/jni/CoreBitcoinWalletManager.java | 2 +- .../core/common/CommonWalletManager.java | 108 ---------------- .../crypto/core/common/jni/JniReference.java | 4 +- 55 files changed, 490 insertions(+), 469 deletions(-) create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/AccountFactory.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerFactory.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Ethereum.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Transfer.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Wallet.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventBalanceUpdated.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventCreated.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventDeleted.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventConnected.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventDisconnected.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStarted.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStopped.java rename Java/Crypto/src/main/java/com/breadwallet/crypto/api/{event => events/transfer}/TransferEvent.java (56%) create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ConnectedWalletManagerEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/DisconnectedWalletManagerEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStoppedWalletManagerEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/AccountProvider.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinNetworkProvider.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinWalletManagerProvider.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java rename Java/Crypto/src/main/java/com/breadwallet/crypto/api/{ => walletmanager}/WalletManagerBackendClient.java (60%) create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/walletmanager/WalletManagerListener.java rename Java/Crypto/src/main/java/com/breadwallet/crypto/api/{ => walletmanager}/WalletManagerPersistenceClient.java (79%) delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCrypto.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java index 5212ee5b9..84ded24d1 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java @@ -2,19 +2,18 @@ import android.util.Log; +import com.breadwallet.crypto.api.AccountFactory; import com.breadwallet.crypto.api.Network; import com.breadwallet.crypto.api.Transfer; import com.breadwallet.crypto.api.Wallet; import com.breadwallet.crypto.api.WalletManager; +import com.breadwallet.crypto.api.WalletManagerFactory; import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; -import com.breadwallet.crypto.api.event.TransferEvent; -import com.breadwallet.crypto.api.event.WalletEvent; -import com.breadwallet.crypto.api.event.WalletManagerEvent; -import com.breadwallet.crypto.core.Account; -import com.breadwallet.crypto.core.CoreCrypto; -import com.breadwallet.crypto.core.bitcoin.BitcoinWalletManager; +import com.breadwallet.crypto.api.events.transfer.TransferEvent; +import com.breadwallet.crypto.api.events.wallet.WalletEvent; +import com.breadwallet.crypto.api.events.walletmanager.WalletManagerEvent; import java.util.Map; @@ -23,14 +22,12 @@ public class CoreDemoBitcoinClient private static final String TAG = "CoreDemoBitcoinClient"; - static { CoreCrypto.init(); } - private final WalletManager walletManager; public CoreDemoBitcoinClient(Network network, String storagePath, String paperKey) { - this.walletManager = new BitcoinWalletManager( + this.walletManager = WalletManagerFactory.create( this, - new Account(paperKey), + AccountFactory.create(paperKey), network, WalletManager.Mode.API_WITH_P2P_SUBMIT, 1543190400, diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java index 08600db44..bc3662378 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java @@ -3,13 +3,16 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; -import com.breadwallet.core.ethereum.BREthereumNetwork; -import com.breadwallet.crypto.core.bitcoin.BitcoinNetworks; +import com.breadwallet.crypto.api.CryptoApi; +import com.breadwallet.crypto.api.bitcoin.Bitcoin; +import com.breadwallet.crypto.core.CoreCryptoApi; import java.io.File; public class WalletNavigationActivity extends AppCompatActivity { + static { CryptoApi.setProvider(new CoreCryptoApi()); } + CoreDemoEthereumClient ethClient = null; private void deleteRecursively (File file) { @@ -31,7 +34,8 @@ protected void onCreate(Bundle savedInstanceState) { if (storageFile.exists()) deleteRecursively(storageFile); storageFile.mkdirs(); - CoreDemoBitcoinClient btcClient = new CoreDemoBitcoinClient(BitcoinNetworks.TESTNET, + CoreDemoBitcoinClient btcClient = new CoreDemoBitcoinClient( + Bitcoin.TESTNET, storageFile.getAbsolutePath(), "0xa9de3dbd7d561e67527bc1ecb025c59d53b9f7ef"); diff --git a/Java/Crypto.Makefile b/Java/Crypto.Makefile index 0e26aa8bb..2295609af 100644 --- a/Java/Crypto.Makefile +++ b/Java/Crypto.Makefile @@ -5,72 +5,90 @@ DIR=$(shell pwd) # JAVA_DIR=${JAVA_HOME} - JAVA_LIB= -JNI_LIB=libCrypto.jnilib +# JNI +JNI_LIB=libCrypto.jnilib JNI_SDIR=Crypto/src/main/cpp/jni - JNI_SRCS= - JNI_OBJS=$(JNI_SRCS:.c=.o) - JNI_HDRS=$(JNI_SRCS:.c=.h) -JAVA_SDIR=Crypto/src/main/java/com/breadwallet/crypto - -JAVA_SRCS=$(JAVA_SDIR)/api/bitcoin/BitcoinBackendClient.java \ - $(JAVA_SDIR)/api/bitcoin/BitcoinChainParams.java \ - $(JAVA_SDIR)/api/bitcoin/BitcoinMasterPubKey.java \ - $(JAVA_SDIR)/api/bitcoin/BitcoinPersistenceClient.java \ - $(JAVA_SDIR)/api/bitcoin/BitcoinWalletManagerListener.java \ - $(JAVA_SDIR)/api/event/TransferEvent.java \ - $(JAVA_SDIR)/api/event/WalletEvent.java \ - $(JAVA_SDIR)/api/event/WalletEventBalanceUpdated.java \ - $(JAVA_SDIR)/api/event/WalletEventCreated.java \ - $(JAVA_SDIR)/api/event/WalletEventDeleted.java \ - $(JAVA_SDIR)/api/event/WalletManagerEvent.java \ - $(JAVA_SDIR)/api/event/WalletManagerEventConnected.java \ - $(JAVA_SDIR)/api/event/WalletManagerEventDisconnected.java \ - $(JAVA_SDIR)/api/event/WalletManagerEventSyncStarted.java \ - $(JAVA_SDIR)/api/event/WalletManagerEventSyncStopped.java \ - $(JAVA_SDIR)/api/Account.java \ - $(JAVA_SDIR)/api/Network.java \ - $(JAVA_SDIR)/api/WalletManager.java \ - $(JAVA_SDIR)/api/WalletManagerBackendClient.java \ - $(JAVA_SDIR)/api/WalletManagerListener.java \ - $(JAVA_SDIR)/api/WalletManagerPersistenceClient.java \ - $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinChainParams.java \ - $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinMasterPubKey.java \ - $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinWalletManager.java \ - $(JAVA_SDIR)/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java \ - $(JAVA_SDIR)/core/bitcoin/BitcoinChainParams.java \ - $(JAVA_SDIR)/core/bitcoin/BitcoinChainParamsAdapter.java \ - $(JAVA_SDIR)/core/bitcoin/BitcoinMasterPubKey.java \ - $(JAVA_SDIR)/core/bitcoin/BitcoinMasterPubKeyAdapter.java \ - $(JAVA_SDIR)/core/bitcoin/BitcoinNetworks.java \ - $(JAVA_SDIR)/core/bitcoin/BitcoinWalletManager.java \ - $(JAVA_SDIR)/core/common/jni/JniReference.java \ - $(JAVA_SDIR)/core/common/CommonWalletManager.java \ - $(JAVA_SDIR)/core/jni/Bip39.java \ - $(JAVA_SDIR)/core/Account.java \ - $(JAVA_SDIR)/core/CoreCrypto.java - -JAVA_OBJS=$(JAVA_SRCS:.java=.class) +# Crypto Api + +JAVA_API_SDIR=Crypto/src/main/java/com/breadwallet/crypto/api +JAVA_API_SRCS=\ + $(JAVA_API_SDIR)/bitcoin/Bitcoin.java \ + $(JAVA_API_SDIR)/bitcoin/BitcoinBackendClient.java \ + $(JAVA_API_SDIR)/bitcoin/BitcoinChainParams.java \ + $(JAVA_API_SDIR)/bitcoin/BitcoinMasterPubKey.java \ + $(JAVA_API_SDIR)/bitcoin/BitcoinPersistenceClient.java \ + $(JAVA_API_SDIR)/bitcoin/BitcoinWalletManagerListener.java \ + $(JAVA_API_SDIR)/events/transfer/TransferEvent.java \ + $(JAVA_API_SDIR)/events/wallet/WalletEvent.java \ + $(JAVA_API_SDIR)/events/wallet/BalanceUpdatedWalletEvent.java \ + $(JAVA_API_SDIR)/events/wallet/CreatedWalletEvent.java \ + $(JAVA_API_SDIR)/events/wallet/DeletedWalletEvent.java \ + $(JAVA_API_SDIR)/events/walletmanager/WalletManagerEvent.java \ + $(JAVA_API_SDIR)/events/walletmanager/ConnectedWalletManagerEvent.java \ + $(JAVA_API_SDIR)/events/walletmanager/DisconnectedWalletManagerEvent.java \ + $(JAVA_API_SDIR)/events/walletmanager/SyncStartedWalletManagerEvent.java \ + $(JAVA_API_SDIR)/events/walletmanager/SyncStoppedWalletManagerEvent.java \ + $(JAVA_API_SDIR)/provider/AccountProvider.java \ + $(JAVA_API_SDIR)/provider/BitcoinNetworkProvider.java \ + $(JAVA_API_SDIR)/provider/BitcoinWalletManagerProvider.java \ + $(JAVA_API_SDIR)/provider/CryptoApiProvider.java \ + $(JAVA_API_SDIR)/walletmanager/WalletManagerBackendClient.java \ + $(JAVA_API_SDIR)/walletmanager/WalletManagerListener.java \ + $(JAVA_API_SDIR)/walletmanager/WalletManagerPersistenceClient.java \ + $(JAVA_API_SDIR)/Account.java \ + $(JAVA_API_SDIR)/AccountFactory.java \ + $(JAVA_API_SDIR)/Address.java \ + $(JAVA_API_SDIR)/Amount.java \ + $(JAVA_API_SDIR)/CryptoApi.java \ + $(JAVA_API_SDIR)/Currency.java \ + $(JAVA_API_SDIR)/Network.java \ + $(JAVA_API_SDIR)/Transfer.java \ + $(JAVA_API_SDIR)/Unit.java \ + $(JAVA_API_SDIR)/Wallet.java \ + $(JAVA_API_SDIR)/WalletManager.java \ + $(JAVA_API_SDIR)/WalletManagerFactory.java +JAVA_API_OBJS=$(JAVA_API_SRCS:.java=.class) + +# Crypto Core + +JAVA_CORE_SDIR=Crypto/src/main/java/com/breadwallet/crypto/core +JAVA_CORE_SRCS=\ + $(JAVA_CORE_SDIR)/bitcoin/jni/CoreBitcoinChainParams.java \ + $(JAVA_CORE_SDIR)/bitcoin/jni/CoreBitcoinMasterPubKey.java \ + $(JAVA_CORE_SDIR)/bitcoin/jni/CoreBitcoinWalletManager.java \ + $(JAVA_CORE_SDIR)/bitcoin/jni/CoreBitcoinWalletManagerClient.java \ + $(JAVA_CORE_SDIR)/bitcoin/BitcoinChainParams.java \ + $(JAVA_CORE_SDIR)/bitcoin/BitcoinChainParamsAdapter.java \ + $(JAVA_CORE_SDIR)/bitcoin/BitcoinMasterPubKey.java \ + $(JAVA_CORE_SDIR)/bitcoin/BitcoinMasterPubKeyAdapter.java \ + $(JAVA_CORE_SDIR)/bitcoin/BitcoinWalletManager.java \ + $(JAVA_CORE_SDIR)/common/jni/JniReference.java \ + $(JAVA_CORE_SDIR)/jni/Bip39.java \ + $(JAVA_CORE_SDIR)/Account.java \ + $(JAVA_CORE_SDIR)/CoreCryptoApi.java +JAVA_CORE_OBJS=$(JAVA_CORE_SRCS:.java=.class) # # Core # +# Bitcoin + C_SDIR=Crypto/src/main/cpp/core CORE_SRCS= - CORE_OBJS=$(CORE_SRCS:.c=.o) -ETH_SRCS= +# Ethereum +ETH_SRCS= ETH_OBJS=$(ETH_SRCS:.c=.o) # @@ -85,7 +103,7 @@ $(JNI_LIB): $(JNI_OBJS) $(CORE_OBJS) java_comp: FORCE @mkdir -p build @echo "Crypto Java" - @javac -d build $(JAVA_SRCS) + @javac -d build $(JAVA_API_SRCS) $(JAVA_CORE_SRCS) jni_hdr_crypto: FORCE @echo Crypto JNI Headers @@ -106,7 +124,7 @@ jni_hdr_crypto: FORCE jni_hdr: java_comp jni_hdr_crypto clean: - rm -rf build $(JNI_OBJS) $(CORE_OBJS) $(JAVA_OBJS) $(JNI_LIB) + rm -rf build $(JNI_OBJS) $(CORE_OBJS) $(JAVA_API_OBJS) $(JAVA_CORE_OBJS) $(JNI_LIB) FORCE: diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/AccountFactory.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/AccountFactory.java new file mode 100644 index 000000000..99343035c --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/AccountFactory.java @@ -0,0 +1,18 @@ +package com.breadwallet.crypto.api; + +public final class AccountFactory { + + public static Account create(String phrase) { + return CryptoApi + .getCryptoApiProvider() + .accountProvider() + .create(phrase); + } + + public static Account create(byte[] seed) { + return CryptoApi + .getCryptoApiProvider() + .accountProvider() + .create(seed); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Address.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Address.java index 82ada1389..e3231e008 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Address.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Address.java @@ -1,4 +1,4 @@ package com.breadwallet.crypto.api; -public class Address { +public interface Address { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java index e04b030a6..78c368834 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java @@ -3,6 +3,7 @@ import java.math.BigInteger; public class Amount { + public final BigInteger value; // UInt256 *core public final Unit unit; public final boolean isNegative; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java new file mode 100644 index 000000000..2370e0d9c --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java @@ -0,0 +1,17 @@ +package com.breadwallet.crypto.api; + +import com.breadwallet.crypto.api.provider.CryptoApiProvider; + +// TODO: Review visibility (for class, methods, fields, etc.) +public class CryptoApi { + + private static CryptoApiProvider cryptoApiProvider; + + public static void setProvider(CryptoApiProvider provider) { + cryptoApiProvider = provider; + } + + public static CryptoApiProvider getCryptoApiProvider() { + return cryptoApiProvider; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java index 516cb2cf9..68a9b4570 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java @@ -1,6 +1,7 @@ package com.breadwallet.crypto.api; public class Currency { + public final String code; public final String symbol; public final String name; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java index 2b43a8a8e..87e8ddbc1 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Network.java @@ -4,6 +4,7 @@ // TODO: Revisit this class and consider moving to interfaces along with the Visitor pattern public class Network { + enum Type { BITCOIN, BITCASH, ETHEREUM} public static final class Bitcoin { diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Transfer.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Transfer.java index 920aad880..ca484b4e2 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Transfer.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Transfer.java @@ -1,50 +1,14 @@ package com.breadwallet.crypto.api; -public abstract class Transfer { - public Wallet wallet; +public interface Transfer { - public Address source; + Wallet wallet(); - public Address target; + Address source(); - public Amount amount; + Address target(); - public Amount fee; + Amount amount(); - /* - /// The owning wallet - var wallet: Wallet { get } - - /// The source pays the fee and sends the amount. - var source: Address? { get } - - /// The target receives the amount - var target: Address? { get } - - /// The amount to transfer - var amount: Amount { get } - - /// The fee paid - before the transfer is confirmed, this is the estimated fee. - var fee: Amount { get } - - /// The basis for the fee. - var feeBasis: TransferFeeBasis { get } - - /// An optional confirmation. - var confirmation: TransferConfirmation? { get } - - /// An optional hash - var hash: TransferHash? { get } - - /// The current state - var state: TransferState { get } -*/ - - protected Transfer(Wallet wallet, Address source, Address target, Amount amount, Amount fee) { - this.wallet = wallet; - this.source = source; - this.target = target; - this.amount = amount; - this.fee = fee; - } + Amount fee(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java index a0e206be2..01332e8c0 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java @@ -1,6 +1,7 @@ package com.breadwallet.crypto.api; public class Unit { + public final Currency currency; public final String name; public final String symbol; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java index 0fba6a09e..93440e59a 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java @@ -1,53 +1,14 @@ package com.breadwallet.crypto.api; -public abstract class Wallet { - public final WalletManager manager; +public interface Wallet { - public final String name; + WalletManager manager(); - public final Currency currency; + String name(); - public abstract Amount getBalance (); + Currency currency(); - public abstract Transfer[] getTransfers(); + Amount getBalance(); - - /* - /// The owning manager - var manager: WalletManager { get } - - /// The name - var name: String { get } - - /// The current balance for currency - var balance: Amount { get } - - /// The transfers of currency yielding `balance` - var transfers: [Transfer] { get } - - /// Use a hash to lookup a transfer - func lookup (transfer: TransferHash) -> Transfer? - - /// The current state. - var state: WalletState { get } - - /// The default TransferFeeBasis for created transfers. - var defaultFeeBasis: TransferFeeBasis { get set } - - /// The default TransferFactory for creating transfers. - var transferFactory: TransferFactory { get set } - - // func sign (transfer: Transfer) - // submit - // ... cancel, replace - if appropriate - - /// An address suitable for a transfer target (receiving). - var target: Address { get } -*/ - - public Wallet (WalletManager manager, Currency currency, String name) { - this.manager = manager; - this.currency = currency; - this.name = name; - } + Transfer[] getTransfers(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerFactory.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerFactory.java new file mode 100644 index 000000000..0b682b586 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerFactory.java @@ -0,0 +1,54 @@ +package com.breadwallet.crypto.api; + +import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; + +import java.util.concurrent.Executor; + +public final class WalletManagerFactory { + + public static WalletManager create(BitcoinWalletManagerListener listener, + Account account, + Network network, + WalletManager.Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient) { + return CryptoApi + .getCryptoApiProvider() + .bitcoinWalletManagerProvider() + .create(listener, + account, + network, + mode, + earliestKeyTime, + storagePath, + persistenceClient, + backendClient); + } + + public static WalletManager create(Executor executor, + BitcoinWalletManagerListener listener, + Account account, + Network network, + WalletManager.Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient) { + return CryptoApi + .getCryptoApiProvider() + .bitcoinWalletManagerProvider() + .create(executor, + listener, + account, + network, + mode, + earliestKeyTime, + storagePath, + persistenceClient, + backendClient); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java deleted file mode 100644 index 9dd423482..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerListener.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.breadwallet.crypto.api; - -import com.breadwallet.crypto.api.event.TransferEvent; -import com.breadwallet.crypto.api.event.WalletEvent; -import com.breadwallet.crypto.api.event.WalletManagerEvent; - -public interface WalletManagerListener { - - void handleManagerEvent(WalletManager manager, WalletManagerEvent event); - - void handleTransferEvent(WalletManager manager, Wallet wallet, Transfer transfer, TransferEvent event); - - void handleWalletEvent(WalletManager manager, Wallet wallet, WalletEvent event); -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java new file mode 100644 index 000000000..6911c2e8d --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java @@ -0,0 +1,18 @@ +package com.breadwallet.crypto.api.bitcoin; + +import com.breadwallet.crypto.api.CryptoApi; +import com.breadwallet.crypto.api.Currency; +import com.breadwallet.crypto.api.Network; +import com.breadwallet.crypto.api.Unit; + +public interface Bitcoin { + + Currency CURRENCY = new Currency ("BTC", "\u20BF", "Bitcoin", 8, "SAT", "sat"); + + Unit SAT = CURRENCY.baseUnit; + Unit BITCOIN = CURRENCY.defaultUnit; + + Network TESTNET = CryptoApi.getCryptoApiProvider().bitcoinNetworkProvider().testnet(); + + Network MAINNET = CryptoApi.getCryptoApiProvider().bitcoinNetworkProvider().mainnet(); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinBackendClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinBackendClient.java index 426493446..60eaeb754 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinBackendClient.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinBackendClient.java @@ -1,6 +1,6 @@ package com.breadwallet.crypto.api.bitcoin; -import com.breadwallet.crypto.api.WalletManagerBackendClient; +import com.breadwallet.crypto.api.walletmanager.WalletManagerBackendClient; public interface BitcoinBackendClient extends WalletManagerBackendClient { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java index 54d619c33..0d55ced6c 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinPersistenceClient.java @@ -1,9 +1,6 @@ package com.breadwallet.crypto.api.bitcoin; -import com.breadwallet.crypto.api.WalletManagerPersistenceClient; -import com.breadwallet.crypto.api.WalletManager; - -import java.util.Map; +import com.breadwallet.crypto.api.walletmanager.WalletManagerPersistenceClient; public interface BitcoinPersistenceClient extends WalletManagerPersistenceClient { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinWalletManagerListener.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinWalletManagerListener.java index a6c93c0d0..36dc5d57f 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinWalletManagerListener.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinWalletManagerListener.java @@ -1,6 +1,6 @@ package com.breadwallet.crypto.api.bitcoin; -import com.breadwallet.crypto.api.WalletManagerListener; +import com.breadwallet.crypto.api.walletmanager.WalletManagerListener; public interface BitcoinWalletManagerListener extends WalletManagerListener { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Ethereum.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Ethereum.java deleted file mode 100644 index 79db012f1..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Ethereum.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.breadwallet.crypto.api.ethereum; - -import com.breadwallet.crypto.api.Currency; -import com.breadwallet.crypto.api.Network; -import com.breadwallet.crypto.api.Unit; - -public interface Ethereum { - Currency currency = new Currency ("ETH", "Ξ", "Ethereum", 18, "WEI", "wei"); - interface Units { - Unit WEI = currency.baseUnit; - Unit ETHER = currency.defaultUnit; - - Unit GWEI = new Unit ("GWEI", "gwei", 1000000000, WEI); - } - - interface Networks { - Network mainnet = new Network(new Network.Ethereum("Eth Mainnet", 1)); - Network ropsten = new Network(new Network.Ethereum("Eth Ropsten", 3)); - Network rinkeby = new Network(new Network.Ethereum("Eth Rinkeby", 4)); - } -} - diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Transfer.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Transfer.java deleted file mode 100644 index a93940fa5..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Transfer.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.breadwallet.crypto.api.ethereum; - -import com.breadwallet.crypto.api.Address; -import com.breadwallet.crypto.api.Amount; -import com.breadwallet.crypto.api.Wallet; - -class Transfer extends com.breadwallet.crypto.api.Transfer { - long core; - - private Transfer(long core, Wallet wallet, Address source, Address target, Amount amount, Amount fee) { - super(wallet, source, target, amount, fee); - this.core = core; - } - - protected Transfer (Wallet wallet, long core) { - this (core, wallet, null, null, null, null); - } - -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Wallet.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Wallet.java deleted file mode 100644 index e06a465fc..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/ethereum/Wallet.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.breadwallet.crypto.api.ethereum; - -import com.breadwallet.crypto.api.Amount; -import com.breadwallet.crypto.api.Currency; -import com.breadwallet.crypto.api.WalletManager; - -public class Wallet extends com.breadwallet.crypto.api.Wallet { - long core; - - /// Balance - - protected Amount balance; - - @Override - public Amount getBalance() { - return balance; - } - - protected void setBalance(Amount balance) { - this.balance = balance; - } - - /// Transfers - protected Transfer[] transfers; - - public Transfer[] getTransfers() { - return transfers; - } - - public void setTransfers(Transfer[] transfers) { - this.transfers = transfers; - } - - - public Wallet(long core, WalletManager manager, Currency currency, String name, Amount balance, Transfer[] transfers) { - super(manager, currency, name); - this.core = core; - this.balance = balance; - this.transfers = transfers; - } - - public Wallet (WalletManager manager, Currency currency, long core) { - this (core, manager, currency, currency.name, - new Amount (0, currency.baseUnit), - new Transfer[]{}); - } -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java deleted file mode 100644 index 8af13168a..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEvent.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto.api.event; - -public interface WalletEvent { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventBalanceUpdated.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventBalanceUpdated.java deleted file mode 100644 index 7f6380048..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventBalanceUpdated.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.breadwallet.crypto.api.event; - -public class WalletEventBalanceUpdated implements WalletEvent { - - private final long balance; - - public WalletEventBalanceUpdated(long balance) { - this.balance = balance; - } - - public long balance() { - return balance; - } -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventCreated.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventCreated.java deleted file mode 100644 index a915b6642..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventCreated.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto.api.event; - -public class WalletEventCreated implements WalletEvent { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventDeleted.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventDeleted.java deleted file mode 100644 index 70077b012..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletEventDeleted.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto.api.event; - -public class WalletEventDeleted implements WalletEvent { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java deleted file mode 100644 index c345c506e..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEvent.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto.api.event; - -public interface WalletManagerEvent { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventConnected.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventConnected.java deleted file mode 100644 index 5be80061b..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventConnected.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto.api.event; - -public class WalletManagerEventConnected implements WalletManagerEvent { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventDisconnected.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventDisconnected.java deleted file mode 100644 index 5dea8b474..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventDisconnected.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto.api.event; - -public class WalletManagerEventDisconnected implements WalletManagerEvent { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStarted.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStarted.java deleted file mode 100644 index c224a6ff4..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStarted.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto.api.event; - -public class WalletManagerEventSyncStarted implements WalletManagerEvent { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStopped.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStopped.java deleted file mode 100644 index 3e6982a5a..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/WalletManagerEventSyncStopped.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.breadwallet.crypto.api.event; - -public class WalletManagerEventSyncStopped implements WalletManagerEvent { - - private final int errorCode; - - public WalletManagerEventSyncStopped(int errorCode) { - this.errorCode = errorCode; - } - - public int errorCode() { - return errorCode; - } -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java similarity index 56% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java index d23465cda..55c0204e3 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/event/TransferEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java @@ -1,4 +1,4 @@ -package com.breadwallet.crypto.api.event; +package com.breadwallet.crypto.api.events.transfer; // TODO: Create transfer events public interface TransferEvent { diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java new file mode 100644 index 000000000..207d773be --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java @@ -0,0 +1,14 @@ +package com.breadwallet.crypto.api.events.wallet; + +public class BalanceUpdatedWalletEvent implements WalletEvent { + + private final long balance; + + public BalanceUpdatedWalletEvent(long balance) { + this.balance = balance; + } + + public long balance() { + return balance; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java new file mode 100644 index 000000000..079a80df0 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.events.wallet; + +public class CreatedWalletEvent implements WalletEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java new file mode 100644 index 000000000..26fd5f9b5 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.events.wallet; + +public class DeletedWalletEvent implements WalletEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java new file mode 100644 index 000000000..1f505f55e --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.events.wallet; + +public interface WalletEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ConnectedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ConnectedWalletManagerEvent.java new file mode 100644 index 000000000..db92a6bb6 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ConnectedWalletManagerEvent.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.events.walletmanager; + +public class ConnectedWalletManagerEvent implements WalletManagerEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/DisconnectedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/DisconnectedWalletManagerEvent.java new file mode 100644 index 000000000..b2b556986 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/DisconnectedWalletManagerEvent.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.events.walletmanager; + +public class DisconnectedWalletManagerEvent implements WalletManagerEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java new file mode 100644 index 000000000..c093c9a6b --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.events.walletmanager; + +public class SyncStartedWalletManagerEvent implements WalletManagerEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStoppedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStoppedWalletManagerEvent.java new file mode 100644 index 000000000..c335c437c --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStoppedWalletManagerEvent.java @@ -0,0 +1,14 @@ +package com.breadwallet.crypto.api.events.walletmanager; + +public class SyncStoppedWalletManagerEvent implements WalletManagerEvent { + + private final int errorCode; + + public SyncStoppedWalletManagerEvent(int errorCode) { + this.errorCode = errorCode; + } + + public int errorCode() { + return errorCode; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java new file mode 100644 index 000000000..ca0a91b8a --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java @@ -0,0 +1,4 @@ +package com.breadwallet.crypto.api.events.walletmanager; + +public interface WalletManagerEvent { +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/AccountProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/AccountProvider.java new file mode 100644 index 000000000..2ea00f53a --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/AccountProvider.java @@ -0,0 +1,10 @@ +package com.breadwallet.crypto.api.provider; + +import com.breadwallet.crypto.api.Account; + +public interface AccountProvider { + + Account create(String phrase); + + Account create(byte[] seed); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinNetworkProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinNetworkProvider.java new file mode 100644 index 000000000..14975ebd2 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinNetworkProvider.java @@ -0,0 +1,10 @@ +package com.breadwallet.crypto.api.provider; + +import com.breadwallet.crypto.api.Network; + +public interface BitcoinNetworkProvider { + + Network testnet(); + + Network mainnet(); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinWalletManagerProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinWalletManagerProvider.java new file mode 100644 index 000000000..0dfa3ff1e --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinWalletManagerProvider.java @@ -0,0 +1,32 @@ +package com.breadwallet.crypto.api.provider; + +import com.breadwallet.crypto.api.Account; +import com.breadwallet.crypto.api.Network; +import com.breadwallet.crypto.api.WalletManager; +import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; + +import java.util.concurrent.Executor; + +public interface BitcoinWalletManagerProvider { + + WalletManager create(BitcoinWalletManagerListener listener, + Account account, + Network network, + WalletManager.Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient); + + WalletManager create(Executor listenerExecutor, + BitcoinWalletManagerListener listener, + Account account, + Network network, + WalletManager.Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java new file mode 100644 index 000000000..e82864989 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java @@ -0,0 +1,10 @@ +package com.breadwallet.crypto.api.provider; + +public interface CryptoApiProvider { + + AccountProvider accountProvider(); + + BitcoinWalletManagerProvider bitcoinWalletManagerProvider(); + + BitcoinNetworkProvider bitcoinNetworkProvider(); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerBackendClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/walletmanager/WalletManagerBackendClient.java similarity index 60% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerBackendClient.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/walletmanager/WalletManagerBackendClient.java index d220a0e5a..bebd76ded 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerBackendClient.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/walletmanager/WalletManagerBackendClient.java @@ -1,4 +1,4 @@ -package com.breadwallet.crypto.api; +package com.breadwallet.crypto.api.walletmanager; public interface WalletManagerBackendClient { diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/walletmanager/WalletManagerListener.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/walletmanager/WalletManagerListener.java new file mode 100644 index 000000000..e845a83ce --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/walletmanager/WalletManagerListener.java @@ -0,0 +1,17 @@ +package com.breadwallet.crypto.api.walletmanager; + +import com.breadwallet.crypto.api.Transfer; +import com.breadwallet.crypto.api.Wallet; +import com.breadwallet.crypto.api.WalletManager; +import com.breadwallet.crypto.api.events.transfer.TransferEvent; +import com.breadwallet.crypto.api.events.wallet.WalletEvent; +import com.breadwallet.crypto.api.events.walletmanager.WalletManagerEvent; + +public interface WalletManagerListener { + + void handleManagerEvent(WalletManager manager, WalletManagerEvent event); + + void handleTransferEvent(WalletManager manager, Wallet wallet, Transfer transfer, TransferEvent event); + + void handleWalletEvent(WalletManager manager, Wallet wallet, WalletEvent event); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/walletmanager/WalletManagerPersistenceClient.java similarity index 79% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/walletmanager/WalletManagerPersistenceClient.java index 40c1152ac..90fc8eb9f 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerPersistenceClient.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/walletmanager/WalletManagerPersistenceClient.java @@ -1,4 +1,6 @@ -package com.breadwallet.crypto.api; +package com.breadwallet.crypto.api.walletmanager; + +import com.breadwallet.crypto.api.WalletManager; import java.util.Map; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCrypto.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCrypto.java deleted file mode 100644 index 1ed3b072b..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCrypto.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.breadwallet.crypto.core; - -// TODO: Review visibility (for class, methods, fields, etc.) -public class CoreCrypto { - - public static void init() { - System.loadLibrary("crypto"); - } -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java new file mode 100644 index 000000000..3e72f4b35 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java @@ -0,0 +1,100 @@ +package com.breadwallet.crypto.core; + +import com.breadwallet.crypto.api.*; +import com.breadwallet.crypto.api.Account; +import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; +import com.breadwallet.crypto.api.provider.AccountProvider; +import com.breadwallet.crypto.api.provider.BitcoinNetworkProvider; +import com.breadwallet.crypto.api.provider.BitcoinWalletManagerProvider; +import com.breadwallet.crypto.api.provider.CryptoApiProvider; +import com.breadwallet.crypto.core.bitcoin.BitcoinChainParams; +import com.breadwallet.crypto.core.bitcoin.BitcoinWalletManager; + +import java.util.concurrent.Executor; + +// TODO: Add guard around initialization occuring once +// TODO: Review visibility (for class, methods, fields, etc.) +public class CoreCryptoApi implements CryptoApiProvider { + + static { + System.loadLibrary("crypto"); + } + + @Override + public AccountProvider accountProvider() { + return new AccountProvider() { + @Override + public Account create(String phrase) { + return new com.breadwallet.crypto.core.Account(phrase); + } + + @Override + public Account create(byte[] seed) { + return new com.breadwallet.crypto.core.Account(seed); + } + }; + } + + @Override + public BitcoinWalletManagerProvider bitcoinWalletManagerProvider() { + return new BitcoinWalletManagerProvider() { + @Override + public WalletManager create(BitcoinWalletManagerListener listener, + Account account, + Network network, + WalletManager.Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient) { + return new BitcoinWalletManager(listener, + account, + network, + mode, + earliestKeyTime, + storagePath, + persistenceClient, + backendClient); + } + + @Override + public WalletManager create(Executor executor, + BitcoinWalletManagerListener listener, + Account account, + Network network, + WalletManager.Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient) { + return new BitcoinWalletManager(executor, + listener, + account, + network, + mode, + earliestKeyTime, + storagePath, + persistenceClient, + backendClient); + } + }; + } + + @Override + public BitcoinNetworkProvider bitcoinNetworkProvider() { + return new BitcoinNetworkProvider() { + @Override + public Network testnet() { + return new Network(new Network.Bitcoin("BTC Testnet", 0x40, BitcoinChainParams.TESTNET)); + } + + @Override + public Network mainnet() { + // TODO: Implement this + return null; + } + }; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java index 0437441dd..794066877 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java @@ -2,6 +2,7 @@ import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinChainParams; +// TODO: Add MAINNET // TODO: Review visibility (for class, methods, fields, etc.) public class BitcoinChainParams implements com.breadwallet.crypto.api.bitcoin.BitcoinChainParams { diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java deleted file mode 100644 index 9969aa4e5..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinNetworks.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.breadwallet.crypto.core.bitcoin; - -import com.breadwallet.crypto.api.Network; - -// TODO: Review visibility (for class, methods, fields, etc.) -public class BitcoinNetworks { - - public static Network TESTNET = new Network( - new Network.Bitcoin("BTC Testnet", 0x40, BitcoinChainParams.TESTNET)); -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java index 145632d17..fa4b42531 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java @@ -6,28 +6,29 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; -import com.breadwallet.crypto.api.event.WalletEvent; -import com.breadwallet.crypto.api.event.WalletEventBalanceUpdated; -import com.breadwallet.crypto.api.event.WalletEventCreated; -import com.breadwallet.crypto.api.event.WalletEventDeleted; -import com.breadwallet.crypto.api.event.WalletManagerEventConnected; -import com.breadwallet.crypto.api.event.WalletManagerEventDisconnected; -import com.breadwallet.crypto.api.event.WalletManagerEventSyncStarted; -import com.breadwallet.crypto.api.event.WalletManagerEventSyncStopped; +import com.breadwallet.crypto.api.events.wallet.BalanceUpdatedWalletEvent; +import com.breadwallet.crypto.api.events.wallet.CreatedWalletEvent; +import com.breadwallet.crypto.api.events.wallet.DeletedWalletEvent; +import com.breadwallet.crypto.api.events.walletmanager.ConnectedWalletManagerEvent; +import com.breadwallet.crypto.api.events.walletmanager.DisconnectedWalletManagerEvent; +import com.breadwallet.crypto.api.events.walletmanager.SyncStartedWalletManagerEvent; +import com.breadwallet.crypto.api.events.walletmanager.SyncStoppedWalletManagerEvent; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinChainParams; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinMasterPubKey; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWalletManager; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWalletManagerClient; -import com.breadwallet.crypto.core.common.CommonWalletManager; import java.lang.ref.WeakReference; import java.util.concurrent.Executor; +import java.util.concurrent.Executors; // TODO: Add parameter validation // TODO: Review visibility (for class, methods, fields, etc.) // TODO: Consider exposing this via a registered factory public final class BitcoinWalletManager - extends CommonWalletManager implements WalletManager, CoreBitcoinWalletManagerClient { + implements WalletManager, CoreBitcoinWalletManagerClient { + + protected static Executor DEFAULT_EXECUTOR = Executors.newSingleThreadExecutor(); protected final WeakReference listener; protected final CoreBitcoinWalletManager coreWalletManager; @@ -35,6 +36,15 @@ public final class BitcoinWalletManager protected final BitcoinPersistenceClient persistenceClient; protected final BitcoinBackendClient backendClient; + protected final Account account; + protected final Network network; + protected final Mode mode; + protected final long earliestKeyTime; + protected final String storagePath; + protected final Executor listenerExecutor; + + protected State state; + public BitcoinWalletManager(BitcoinWalletManagerListener listener, Account account, Network network, @@ -46,7 +56,7 @@ public BitcoinWalletManager(BitcoinWalletManagerListener listener, this(DEFAULT_EXECUTOR, listener, account, network, mode, earliestKeyTime, storagePath, persistenceClient, backendClient); } - public BitcoinWalletManager(Executor listenerExector, + public BitcoinWalletManager(Executor listenerExecutor, BitcoinWalletManagerListener listener, Account account, Network network, @@ -55,14 +65,20 @@ public BitcoinWalletManager(Executor listenerExector, String storagePath, BitcoinPersistenceClient persistenceClient, BitcoinBackendClient backendClient) { - super(listenerExector, account, network, mode, earliestKeyTime, storagePath); + CoreBitcoinChainParams chainParams = BitcoinChainParamsAdapter.from(network.chainParams()).chainParams; + CoreBitcoinMasterPubKey masterPubKey = BitcoinMasterPubKeyAdapter.from(account.masterPublicKey()).masterPubKey; + + this.listenerExecutor = listenerExecutor; + this.account = account; + this.network = network; + this.mode = mode; + this.earliestKeyTime = earliestKeyTime; + this.storagePath = storagePath; + this.state = State.CREATED; this.persistenceClient = persistenceClient; this.backendClient = backendClient; - CoreBitcoinChainParams chainParams = BitcoinChainParamsAdapter.from(network.chainParams()).chainParams; - CoreBitcoinMasterPubKey masterPubKey = BitcoinMasterPubKeyAdapter.from(account.masterPublicKey()).masterPubKey; - this.listener = new WeakReference<>(listener); this.coreWalletManager = new CoreBitcoinWalletManager(this, masterPubKey, chainParams, earliestKeyTime, storagePath); } @@ -117,7 +133,7 @@ public void handleWalletCreated(long wid) { BitcoinWalletManagerListener l = listener.get(); if (l != null) { // TODO: Get wallet using wid - l.handleWalletEvent(this, null, new WalletEventCreated()); + l.handleWalletEvent(this, null, new CreatedWalletEvent()); } }); } @@ -128,7 +144,7 @@ public void handleWalletBalanceUpdated(long wid, long satoshi) { BitcoinWalletManagerListener l = listener.get(); if (l != null) { // TODO: Get wallet using wid - l.handleWalletEvent(this, null, new WalletEventBalanceUpdated(satoshi)); + l.handleWalletEvent(this, null, new BalanceUpdatedWalletEvent(satoshi)); } }); } @@ -139,7 +155,7 @@ public void handleWalletDeleted(long wid) { BitcoinWalletManagerListener l = listener.get(); if (l != null) { // TODO: Get wallet using wid - l.handleWalletEvent(this, null, new WalletEventDeleted()); + l.handleWalletEvent(this, null, new DeletedWalletEvent()); } }); } @@ -149,7 +165,7 @@ public void handleWalletManagerConnected() { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - l.handleManagerEvent(this, new WalletManagerEventConnected()); + l.handleManagerEvent(this, new ConnectedWalletManagerEvent()); } }); } @@ -159,7 +175,7 @@ public void handleWalletManagerDisconnected() { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - l.handleManagerEvent(this, new WalletManagerEventDisconnected()); + l.handleManagerEvent(this, new DisconnectedWalletManagerEvent()); } }); } @@ -169,7 +185,7 @@ public void handleWalletManagerSyncStarted() { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - l.handleManagerEvent(this, new WalletManagerEventSyncStarted()); + l.handleManagerEvent(this, new SyncStartedWalletManagerEvent()); } }); } @@ -179,7 +195,7 @@ public void handleWalletManagerSyncStopped(int errorCode) { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - l.handleManagerEvent(this, new WalletManagerEventSyncStopped(errorCode)); + l.handleManagerEvent(this, new SyncStoppedWalletManagerEvent(errorCode)); } }); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java index a132ce518..5a9ea3869 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java @@ -11,7 +11,7 @@ // TODO: Review visibility (for class, methods, fields, etc.) public class CoreBitcoinWalletManager extends JniReference { - private static Map> wmMap = new HashMap<>(); + private static final Map> wmMap = new HashMap<>(); private static native long createBitcoinWalletManager(CoreBitcoinMasterPubKey mpk, CoreBitcoinChainParams params, diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java deleted file mode 100644 index 40fbe3fe6..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/CommonWalletManager.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.breadwallet.crypto.core.common; - -import com.breadwallet.crypto.api.Account; -import com.breadwallet.crypto.api.Network; -import com.breadwallet.crypto.api.WalletManager; - -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; - -// TODO: Add parameter validation -// TODO: Review visibility (for class, methods, fields, etc.) -public abstract class CommonWalletManager implements WalletManager { - - protected static Executor DEFAULT_EXECUTOR = Executors.newSingleThreadExecutor(); - - protected final Account account; - protected final Network network; - protected final Mode mode; - protected final long earliestKeyTime; - protected final String storagePath; - protected final Executor listenerExecutor; - - protected State state; - - public CommonWalletManager(Executor listenerExecutor, - Account account, - Network network, - Mode mode, - long earliestKeyTime, - String storagePath) { - // TODO: Check arguments for validity (i.e. null, correctness, etc.) - this.listenerExecutor = listenerExecutor; - this.account = account; - this.network = network; - this.mode = mode; - this.earliestKeyTime = earliestKeyTime; - this.storagePath = storagePath; - this.state = State.CREATED; - } - - -// public interface Listener { -// -// } -// -// public interface WalletManagerPersistenceClient { -// -// } -// -// public interface BackendClient { -// -// } -// -// public final Account account; -// -// public final Network network; -// -// public final com.breadwallet.crypto.WalletManager.Listener listener; - -/* - /// The listener receives Wallet, Transfer and perhaps other asynchronous events. - var listener: WalletManagerListener { get } - - /// The account - var account: Account { get } - - /// The network - var network: Network { get } - - /// The primaryWallet - var primaryWallet: Wallet { get } - - /// The managed wallets - often will just be [primaryWallet] - var wallets: [Wallet] { get } - - // The mode determines how the manager manages the account and wallets on network - var mode: WalletManagerMode { get } - - // The file-system path to use for persistent storage. - var path: String { get } // persistent storage - - var state: WalletManagerState { get } - - #if false - /// The default WalletFactory for creating wallets. - var walletFactory: WalletFactory { get set } - #endif - - /// Connect to network and begin managing wallets for account - func connect () - - /// Disconnect from the network. - func disconnect () - - /// isConnected - /// sync(...) - /// isSyncing - - /// sign(transfer) - /// submit(transfer) -*/ -// -// protected WalletManager (com.breadwallet.crypto.WalletManager.Listener listener, Account account, Network network) { -// this.listener = listener; -// this.account = account; -// this.network = network; -// } -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java index fcf00200c..340ae157c 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java @@ -15,7 +15,7 @@ public class JniReference { */ protected long jniReferenceAddress; - protected JniReference (long jniReferenceAddress) + protected JniReference(long jniReferenceAddress) { this.jniReferenceAddress = jniReferenceAddress; } @@ -25,7 +25,7 @@ protected void finalize() throws Throwable { dispose(); } - public void dispose() { + protected void dispose() { disposeNative(); } From af47c2a54c1e2c1332f928eef3460e2150b8b752 Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Tue, 12 Mar 2019 21:21:05 -0400 Subject: [PATCH 07/12] wip: Strip out author --- Java/Crypto/src/main/cpp/jni/BRCryptoJni.c | 1 - Java/Crypto/src/main/cpp/jni/BRCryptoJni.h | 1 - ..._breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c | 1 - ...breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c | 1 - ...readwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c | 1 - .../jni/com_breadwallet_crypto_core_common_jni_JniReference.c | 1 - .../src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c | 1 - 7 files changed, 7 deletions(-) diff --git a/Java/Crypto/src/main/cpp/jni/BRCryptoJni.c b/Java/Crypto/src/main/cpp/jni/BRCryptoJni.c index 67efa7e76..70ea9bd39 100644 --- a/Java/Crypto/src/main/cpp/jni/BRCryptoJni.c +++ b/Java/Crypto/src/main/cpp/jni/BRCryptoJni.c @@ -1,4 +1,3 @@ -// Created by Michael Carrara on 3/19/2019 // Copyright (c) 2019 breadwallet LLC. // // Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/Java/Crypto/src/main/cpp/jni/BRCryptoJni.h b/Java/Crypto/src/main/cpp/jni/BRCryptoJni.h index ccae77163..d4d5cba6e 100644 --- a/Java/Crypto/src/main/cpp/jni/BRCryptoJni.h +++ b/Java/Crypto/src/main/cpp/jni/BRCryptoJni.h @@ -1,4 +1,3 @@ -// Created by Michael Carrara on 3/19/2019 // Copyright (c) 2019 breadwallet LLC. // // Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c index fec51de05..b675e68d0 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c @@ -1,4 +1,3 @@ -// Created by Michael Carrara on 3/19/2019 // Copyright (c) 2019 breadwallet LLC. // // Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c index 76c7403f8..8b6d09d0d 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c @@ -1,4 +1,3 @@ -// Created by Michael Carrara on 3/19/2019 // Copyright (c) 2019 breadwallet LLC. // // Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c index 1a828c0b0..e34d29d5f 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c @@ -1,4 +1,3 @@ -// Created by Michael Carrara on 3/19/2019 // Copyright (c) 2019 breadwallet LLC. // // Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c index 98a4361a1..8e51c5719 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c @@ -1,4 +1,3 @@ -// Created by Michael Carrara on 3/19/2019 // Copyright (c) 2019 breadwallet LLC. // // Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c index 32f93e06e..1b2269f4b 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c @@ -1,4 +1,3 @@ -// Created by Michael Carrara on 3/19/2019 // Copyright (c) 2019 breadwallet LLC. // // Permission is hereby granted, free of charge, to any person obtaining a copy From 6bd9af55eea5d179910bf071af51139438159dad Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Tue, 12 Mar 2019 21:41:04 -0400 Subject: [PATCH 08/12] wip: Refactor factory interface --- .../coredemo/CoreDemoBitcoinClient.java | 7 ++- .../coredemo/WalletNavigationActivity.java | 2 +- .../com/breadwallet/crypto/api/Account.java | 3 ++ .../crypto/api/AccountFactory.java | 18 ------- .../com/breadwallet/crypto/api/CryptoApi.java | 5 +- .../breadwallet/crypto/api/WalletManager.java | 4 ++ .../crypto/api/WalletManagerFactory.java | 54 ------------------- .../crypto/api/bitcoin/Bitcoin.java | 4 +- .../BitcoinWalletManagerProvider.java | 32 ----------- .../api/provider/CryptoApiProvider.java | 4 +- ...workProvider.java => NetworkProvider.java} | 2 +- .../api/provider/WalletManagerProvider.java | 32 +++++++++++ .../crypto/core/CoreCryptoApi.java | 48 ++++++++--------- 13 files changed, 75 insertions(+), 140 deletions(-) delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/AccountFactory.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerFactory.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinWalletManagerProvider.java rename Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/{BitcoinNetworkProvider.java => NetworkProvider.java} (76%) create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/WalletManagerProvider.java diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java index 84ded24d1..3da15a73f 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java @@ -2,12 +2,11 @@ import android.util.Log; -import com.breadwallet.crypto.api.AccountFactory; +import com.breadwallet.crypto.api.Account; import com.breadwallet.crypto.api.Network; import com.breadwallet.crypto.api.Transfer; import com.breadwallet.crypto.api.Wallet; import com.breadwallet.crypto.api.WalletManager; -import com.breadwallet.crypto.api.WalletManagerFactory; import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; @@ -25,9 +24,9 @@ public class CoreDemoBitcoinClient private final WalletManager walletManager; public CoreDemoBitcoinClient(Network network, String storagePath, String paperKey) { - this.walletManager = WalletManagerFactory.create( + this.walletManager = WalletManager.FACTORY.createBitcoinWalletManager( this, - AccountFactory.create(paperKey), + Account.FACTORY.create(paperKey), network, WalletManager.Mode.API_WITH_P2P_SUBMIT, 1543190400, diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java index bc3662378..7bfc49d9a 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java @@ -11,7 +11,7 @@ public class WalletNavigationActivity extends AppCompatActivity { - static { CryptoApi.setProvider(new CoreCryptoApi()); } + static { CryptoApi.init(new CoreCryptoApi()); } CoreDemoEthereumClient ethClient = null; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java index 5437344ab..6c5525832 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java @@ -1,8 +1,11 @@ package com.breadwallet.crypto.api; import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; +import com.breadwallet.crypto.api.provider.AccountProvider; public interface Account { + AccountProvider FACTORY = CryptoApi.cryptoApiProvider().accountProvider(); + BitcoinMasterPubKey masterPublicKey(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/AccountFactory.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/AccountFactory.java deleted file mode 100644 index 99343035c..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/AccountFactory.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.breadwallet.crypto.api; - -public final class AccountFactory { - - public static Account create(String phrase) { - return CryptoApi - .getCryptoApiProvider() - .accountProvider() - .create(phrase); - } - - public static Account create(byte[] seed) { - return CryptoApi - .getCryptoApiProvider() - .accountProvider() - .create(seed); - } -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java index 2370e0d9c..8ee2b550b 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java @@ -2,16 +2,17 @@ import com.breadwallet.crypto.api.provider.CryptoApiProvider; +// TODO: Add guard around initialization occuring once // TODO: Review visibility (for class, methods, fields, etc.) public class CryptoApi { private static CryptoApiProvider cryptoApiProvider; - public static void setProvider(CryptoApiProvider provider) { + public static void init(CryptoApiProvider provider) { cryptoApiProvider = provider; } - public static CryptoApiProvider getCryptoApiProvider() { + public static CryptoApiProvider cryptoApiProvider() { return cryptoApiProvider; } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java index 10510164f..acd8eeb26 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java @@ -1,7 +1,11 @@ package com.breadwallet.crypto.api; +import com.breadwallet.crypto.api.provider.WalletManagerProvider; + public interface WalletManager { + WalletManagerProvider FACTORY = CryptoApi.cryptoApiProvider().walletManagerProvider(); + enum Mode { API_ONLY, API_WITH_P2P_SUBMIT, P2P_WITH_API_SYNC, P2P_ONLY, } enum State { CREATED, DISCONNECTED, CONNECTED, SYNCING, DELETED } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerFactory.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerFactory.java deleted file mode 100644 index 0b682b586..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManagerFactory.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.breadwallet.crypto.api; - -import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; -import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; -import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; - -import java.util.concurrent.Executor; - -public final class WalletManagerFactory { - - public static WalletManager create(BitcoinWalletManagerListener listener, - Account account, - Network network, - WalletManager.Mode mode, - int earliestKeyTime, - String storagePath, - BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient) { - return CryptoApi - .getCryptoApiProvider() - .bitcoinWalletManagerProvider() - .create(listener, - account, - network, - mode, - earliestKeyTime, - storagePath, - persistenceClient, - backendClient); - } - - public static WalletManager create(Executor executor, - BitcoinWalletManagerListener listener, - Account account, - Network network, - WalletManager.Mode mode, - int earliestKeyTime, - String storagePath, - BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient) { - return CryptoApi - .getCryptoApiProvider() - .bitcoinWalletManagerProvider() - .create(executor, - listener, - account, - network, - mode, - earliestKeyTime, - storagePath, - persistenceClient, - backendClient); - } -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java index 6911c2e8d..b3c146f94 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java @@ -12,7 +12,7 @@ public interface Bitcoin { Unit SAT = CURRENCY.baseUnit; Unit BITCOIN = CURRENCY.defaultUnit; - Network TESTNET = CryptoApi.getCryptoApiProvider().bitcoinNetworkProvider().testnet(); + Network TESTNET = CryptoApi.cryptoApiProvider().networkProvider().testnet(); - Network MAINNET = CryptoApi.getCryptoApiProvider().bitcoinNetworkProvider().mainnet(); + Network MAINNET = CryptoApi.cryptoApiProvider().networkProvider().mainnet(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinWalletManagerProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinWalletManagerProvider.java deleted file mode 100644 index 0dfa3ff1e..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinWalletManagerProvider.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.breadwallet.crypto.api.provider; - -import com.breadwallet.crypto.api.Account; -import com.breadwallet.crypto.api.Network; -import com.breadwallet.crypto.api.WalletManager; -import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; -import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; -import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; - -import java.util.concurrent.Executor; - -public interface BitcoinWalletManagerProvider { - - WalletManager create(BitcoinWalletManagerListener listener, - Account account, - Network network, - WalletManager.Mode mode, - int earliestKeyTime, - String storagePath, - BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient); - - WalletManager create(Executor listenerExecutor, - BitcoinWalletManagerListener listener, - Account account, - Network network, - WalletManager.Mode mode, - int earliestKeyTime, - String storagePath, - BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient); -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java index e82864989..b84288626 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java @@ -4,7 +4,7 @@ public interface CryptoApiProvider { AccountProvider accountProvider(); - BitcoinWalletManagerProvider bitcoinWalletManagerProvider(); + WalletManagerProvider walletManagerProvider(); - BitcoinNetworkProvider bitcoinNetworkProvider(); + NetworkProvider networkProvider(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinNetworkProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/NetworkProvider.java similarity index 76% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinNetworkProvider.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/NetworkProvider.java index 14975ebd2..910255ba4 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/BitcoinNetworkProvider.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/NetworkProvider.java @@ -2,7 +2,7 @@ import com.breadwallet.crypto.api.Network; -public interface BitcoinNetworkProvider { +public interface NetworkProvider { Network testnet(); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/WalletManagerProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/WalletManagerProvider.java new file mode 100644 index 000000000..9e1f90e58 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/WalletManagerProvider.java @@ -0,0 +1,32 @@ +package com.breadwallet.crypto.api.provider; + +import com.breadwallet.crypto.api.Account; +import com.breadwallet.crypto.api.Network; +import com.breadwallet.crypto.api.WalletManager; +import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; + +import java.util.concurrent.Executor; + +public interface WalletManagerProvider { + + WalletManager createBitcoinWalletManager(BitcoinWalletManagerListener listener, + Account account, + Network network, + WalletManager.Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient); + + WalletManager createBitcoinWalletManager(Executor listenerExecutor, + BitcoinWalletManagerListener listener, + Account account, + Network network, + WalletManager.Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java index 3e72f4b35..8939c6d8d 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java @@ -6,15 +6,15 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; import com.breadwallet.crypto.api.provider.AccountProvider; -import com.breadwallet.crypto.api.provider.BitcoinNetworkProvider; -import com.breadwallet.crypto.api.provider.BitcoinWalletManagerProvider; +import com.breadwallet.crypto.api.provider.NetworkProvider; +import com.breadwallet.crypto.api.provider.WalletManagerProvider; import com.breadwallet.crypto.api.provider.CryptoApiProvider; import com.breadwallet.crypto.core.bitcoin.BitcoinChainParams; import com.breadwallet.crypto.core.bitcoin.BitcoinWalletManager; import java.util.concurrent.Executor; -// TODO: Add guard around initialization occuring once +// TODO: Init providers once, instead of on each invocation // TODO: Review visibility (for class, methods, fields, etc.) public class CoreCryptoApi implements CryptoApiProvider { @@ -38,17 +38,17 @@ public Account create(byte[] seed) { } @Override - public BitcoinWalletManagerProvider bitcoinWalletManagerProvider() { - return new BitcoinWalletManagerProvider() { + public WalletManagerProvider walletManagerProvider() { + return new WalletManagerProvider() { @Override - public WalletManager create(BitcoinWalletManagerListener listener, - Account account, - Network network, - WalletManager.Mode mode, - int earliestKeyTime, - String storagePath, - BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient) { + public WalletManager createBitcoinWalletManager(BitcoinWalletManagerListener listener, + Account account, + Network network, + WalletManager.Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient) { return new BitcoinWalletManager(listener, account, network, @@ -60,15 +60,15 @@ public WalletManager create(BitcoinWalletManagerListener listener, } @Override - public WalletManager create(Executor executor, - BitcoinWalletManagerListener listener, - Account account, - Network network, - WalletManager.Mode mode, - int earliestKeyTime, - String storagePath, - BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient) { + public WalletManager createBitcoinWalletManager(Executor executor, + BitcoinWalletManagerListener listener, + Account account, + Network network, + WalletManager.Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient) { return new BitcoinWalletManager(executor, listener, account, @@ -83,8 +83,8 @@ public WalletManager create(Executor executor, } @Override - public BitcoinNetworkProvider bitcoinNetworkProvider() { - return new BitcoinNetworkProvider() { + public NetworkProvider networkProvider() { + return new NetworkProvider() { @Override public Network testnet() { return new Network(new Network.Bitcoin("BTC Testnet", 0x40, BitcoinChainParams.TESTNET)); From cc807a5b792a60c1f2e4d7f05212fd7490960220 Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Wed, 13 Mar 2019 21:15:29 -0400 Subject: [PATCH 09/12] wip: Refactor components and handle TODOs --- .../coredemo/CoreDemoBitcoinClient.java | 6 +- .../coredemo/WalletNavigationActivity.java | 2 +- Java/Crypto.Makefile | 20 ++- Java/Crypto/CMakeLists.txt | 2 - ..._core_bitcoin_jni_CoreBitcoinChainParams.c | 4 +- ...core_bitcoin_jni_CoreBitcoinMasterPubKey.c | 6 +- ...ore_bitcoin_jni_CoreBitcoinWalletManager.c | 23 +--- ...tcoin_jni_CoreBitcoinWalletManagerClient.c | 0 ...tcoin_jni_CoreBitcoinWalletManagerClient.h | 13 -- ...llet_crypto_core_common_jni_JniReference.c | 3 - .../com_breadwallet_crypto_core_jni_Bip39.c | 9 +- .../com/breadwallet/crypto/api/Account.java | 4 +- .../com/breadwallet/crypto/api/Amount.java | 5 + .../com/breadwallet/crypto/api/Bitcoin.java | 13 ++ .../com/breadwallet/crypto/api/CryptoApi.java | 24 +++- .../com/breadwallet/crypto/api/Currency.java | 6 + .../java/com/breadwallet/crypto/api/Unit.java | 5 + .../com/breadwallet/crypto/api/Wallet.java | 4 +- .../breadwallet/crypto/api/WalletManager.java | 4 +- .../crypto/api/bitcoin/Bitcoin.java | 18 --- .../api/bitcoin/BitcoinChainParams.java | 1 - .../api/bitcoin/BitcoinMasterPubKey.java | 1 - .../events/transfer/CreatedTransferEvent.java | 9 ++ .../events/transfer/DeletedTransferEvent.java | 9 ++ .../api/events/transfer/TransferEvent.java | 2 +- .../wallet/BalanceUpdatedWalletEvent.java | 17 ++- .../api/events/wallet/CreatedWalletEvent.java | 5 + .../api/events/wallet/DeletedWalletEvent.java | 5 + .../crypto/api/events/wallet/WalletEvent.java | 1 + .../ChangedWalletManagerEvent.java | 27 ++++ .../ConnectedWalletManagerEvent.java | 4 - .../DisconnectedWalletManagerEvent.java | 4 - .../SyncEndedWalletManagerEvent.java | 15 +++ .../SyncStartedWalletManagerEvent.java | 5 + .../SyncStoppedWalletManagerEvent.java | 14 -- .../walletmanager/WalletManagerEvent.java | 1 + .../AccountFactory.java} | 4 +- .../NetworkFactory.java} | 4 +- .../WalletManagerFactory.java} | 19 +-- .../api/provider/CryptoApiProvider.java | 10 -- .../com/breadwallet/crypto/core/Account.java | 3 +- .../crypto/core/CoreCryptoApi.java | 55 +++----- .../core/bitcoin/BitcoinChainParams.java | 11 +- .../bitcoin/BitcoinChainParamsAdapter.java | 3 +- .../core/bitcoin/BitcoinMasterPubKey.java | 14 +- .../bitcoin/BitcoinMasterPubKeyAdapter.java | 3 +- .../crypto/core/bitcoin/BitcoinWallet.java | 47 +++++++ .../core/bitcoin/BitcoinWalletManager.java | 122 ++++++++++-------- .../bitcoin/jni/CoreBitcoinChainParams.java | 4 +- .../bitcoin/jni/CoreBitcoinMasterPubKey.java | 10 +- .../core/bitcoin/jni/CoreBitcoinWallet.java | 20 +++ .../bitcoin/jni/CoreBitcoinWalletManager.java | 33 +++-- .../jni/CoreBitcoinWalletManagerClient.java | 14 +- .../crypto/core/common/jni/JniReference.java | 2 +- .../breadwallet/crypto/core/jni/BIP39.java | 3 +- 55 files changed, 383 insertions(+), 289 deletions(-) delete mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.c delete mode 100644 Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/Bitcoin.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/CreatedTransferEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/DeletedTransferEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ChangedWalletManagerEvent.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ConnectedWalletManagerEvent.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/DisconnectedWalletManagerEvent.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncEndedWalletManagerEvent.java delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStoppedWalletManagerEvent.java rename Java/Crypto/src/main/java/com/breadwallet/crypto/api/{provider/AccountProvider.java => factories/AccountFactory.java} (59%) rename Java/Crypto/src/main/java/com/breadwallet/crypto/api/{provider/NetworkProvider.java => factories/NetworkFactory.java} (54%) rename Java/Crypto/src/main/java/com/breadwallet/crypto/api/{provider/WalletManagerProvider.java => factories/WalletManagerFactory.java} (51%) delete mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWallet.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java index 3da15a73f..07f6615db 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java @@ -15,6 +15,7 @@ import com.breadwallet.crypto.api.events.walletmanager.WalletManagerEvent; import java.util.Map; +import java.util.concurrent.Executors; public class CoreDemoBitcoinClient implements BitcoinWalletManagerListener, BitcoinBackendClient, BitcoinPersistenceClient { @@ -25,14 +26,15 @@ public class CoreDemoBitcoinClient public CoreDemoBitcoinClient(Network network, String storagePath, String paperKey) { this.walletManager = WalletManager.FACTORY.createBitcoinWalletManager( - this, Account.FACTORY.create(paperKey), network, WalletManager.Mode.API_WITH_P2P_SUBMIT, 1543190400, storagePath, this, - this); + this, + this, + Executors.newSingleThreadExecutor()); } public void connect() { diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java index 7bfc49d9a..271e79ad7 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java @@ -4,7 +4,7 @@ import android.os.Bundle; import com.breadwallet.crypto.api.CryptoApi; -import com.breadwallet.crypto.api.bitcoin.Bitcoin; +import com.breadwallet.crypto.api.Bitcoin; import com.breadwallet.crypto.core.CoreCryptoApi; import java.io.File; diff --git a/Java/Crypto.Makefile b/Java/Crypto.Makefile index 2295609af..d96417101 100644 --- a/Java/Crypto.Makefile +++ b/Java/Crypto.Makefile @@ -19,7 +19,6 @@ JNI_HDRS=$(JNI_SRCS:.c=.h) JAVA_API_SDIR=Crypto/src/main/java/com/breadwallet/crypto/api JAVA_API_SRCS=\ - $(JAVA_API_SDIR)/bitcoin/Bitcoin.java \ $(JAVA_API_SDIR)/bitcoin/BitcoinBackendClient.java \ $(JAVA_API_SDIR)/bitcoin/BitcoinChainParams.java \ $(JAVA_API_SDIR)/bitcoin/BitcoinMasterPubKey.java \ @@ -31,29 +30,26 @@ JAVA_API_SRCS=\ $(JAVA_API_SDIR)/events/wallet/CreatedWalletEvent.java \ $(JAVA_API_SDIR)/events/wallet/DeletedWalletEvent.java \ $(JAVA_API_SDIR)/events/walletmanager/WalletManagerEvent.java \ - $(JAVA_API_SDIR)/events/walletmanager/ConnectedWalletManagerEvent.java \ - $(JAVA_API_SDIR)/events/walletmanager/DisconnectedWalletManagerEvent.java \ + $(JAVA_API_SDIR)/events/walletmanager/ChangedWalletManagerEvent.java \ $(JAVA_API_SDIR)/events/walletmanager/SyncStartedWalletManagerEvent.java \ - $(JAVA_API_SDIR)/events/walletmanager/SyncStoppedWalletManagerEvent.java \ - $(JAVA_API_SDIR)/provider/AccountProvider.java \ - $(JAVA_API_SDIR)/provider/BitcoinNetworkProvider.java \ - $(JAVA_API_SDIR)/provider/BitcoinWalletManagerProvider.java \ - $(JAVA_API_SDIR)/provider/CryptoApiProvider.java \ + $(JAVA_API_SDIR)/events/walletmanager/SyncEndedWalletManagerEvent.java \ + $(JAVA_API_SDIR)/factories/AccountFactory.java \ + $(JAVA_API_SDIR)/factories/NetworkFactory.java \ + $(JAVA_API_SDIR)/factories/WalletManagerFactory.java \ $(JAVA_API_SDIR)/walletmanager/WalletManagerBackendClient.java \ $(JAVA_API_SDIR)/walletmanager/WalletManagerListener.java \ $(JAVA_API_SDIR)/walletmanager/WalletManagerPersistenceClient.java \ $(JAVA_API_SDIR)/Account.java \ - $(JAVA_API_SDIR)/AccountFactory.java \ $(JAVA_API_SDIR)/Address.java \ $(JAVA_API_SDIR)/Amount.java \ + $(JAVA_API_SDIR)/Bitcoin.java \ $(JAVA_API_SDIR)/CryptoApi.java \ $(JAVA_API_SDIR)/Currency.java \ $(JAVA_API_SDIR)/Network.java \ $(JAVA_API_SDIR)/Transfer.java \ $(JAVA_API_SDIR)/Unit.java \ $(JAVA_API_SDIR)/Wallet.java \ - $(JAVA_API_SDIR)/WalletManager.java \ - $(JAVA_API_SDIR)/WalletManagerFactory.java + $(JAVA_API_SDIR)/WalletManager.java JAVA_API_OBJS=$(JAVA_API_SRCS:.java=.class) # Crypto Core @@ -62,12 +58,14 @@ JAVA_CORE_SDIR=Crypto/src/main/java/com/breadwallet/crypto/core JAVA_CORE_SRCS=\ $(JAVA_CORE_SDIR)/bitcoin/jni/CoreBitcoinChainParams.java \ $(JAVA_CORE_SDIR)/bitcoin/jni/CoreBitcoinMasterPubKey.java \ + $(JAVA_CORE_SDIR)/bitcoin/jni/CoreBitcoinWallet.java \ $(JAVA_CORE_SDIR)/bitcoin/jni/CoreBitcoinWalletManager.java \ $(JAVA_CORE_SDIR)/bitcoin/jni/CoreBitcoinWalletManagerClient.java \ $(JAVA_CORE_SDIR)/bitcoin/BitcoinChainParams.java \ $(JAVA_CORE_SDIR)/bitcoin/BitcoinChainParamsAdapter.java \ $(JAVA_CORE_SDIR)/bitcoin/BitcoinMasterPubKey.java \ $(JAVA_CORE_SDIR)/bitcoin/BitcoinMasterPubKeyAdapter.java \ + $(JAVA_CORE_SDIR)/bitcoin/BitcoinWallet.java \ $(JAVA_CORE_SDIR)/bitcoin/BitcoinWalletManager.java \ $(JAVA_CORE_SDIR)/common/jni/JniReference.java \ $(JAVA_CORE_SDIR)/jni/Bip39.java \ diff --git a/Java/Crypto/CMakeLists.txt b/Java/Crypto/CMakeLists.txt index 60f5527bf..9a7f65647 100644 --- a/Java/Crypto/CMakeLists.txt +++ b/Java/Crypto/CMakeLists.txt @@ -240,8 +240,6 @@ target_sources (crypto src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinMasterPubKey.c src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c - src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h - src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.c src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.h src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.h diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c index b675e68d0..4281368d8 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c @@ -18,8 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include -#include +#include IsSameObject (env, seedByteArray, NULL)); + jsize seedLength = (*env)->GetArrayLength (env, seedByteArray); jbyte *seedBytes = (*env)->GetByteArrayElements (env, seedByteArray, 0); - // Allocate, then fill, our BRMasterPubKey result with the computed pubKey BRMasterPubKey *resKey = (BRMasterPubKey *) calloc (1, sizeof (BRMasterPubKey)); *resKey = BRBIP32MasterPubKey(seedBytes, seedLength); diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c index e34d29d5f..0465269a8 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.c @@ -19,20 +19,13 @@ // THE SOFTWARE. #include -#include #include -#include #include "BRWalletManager.h" #include "BRCryptoJni.h" #include "com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManager.h" -#define LOG_TAG "CoreBitcoinWalletManager" - -// TODO: Add defensive checks on inputs -// TODO: Re-write using personal coding style - static jclass trampolineClass = NULL; static jmethodID trampolineHandleTransactionAdded = NULL; static jmethodID trampolineHandleTransactionUpdated = NULL; @@ -69,7 +62,6 @@ JNIEXPORT void JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinW JNIEnv * env, jclass thisClass) { - if (NULL != trampolineClass) return; trampolineClass = (*env)->NewGlobalRef(env, thisClass); trampolineHandleTransactionAdded = trampolineOrFatal (env, "handleTransactionAdded", "(JJJ)V"); @@ -94,14 +86,17 @@ JNIEXPORT jlong JNICALL Java_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoin jint earliestKeyTime, jstring storagePathString) { + assert (!(*env)->IsSameObject (env, masterPublicKeyObject, NULL)); + assert (!(*env)->IsSameObject (env, chainParamsObject, NULL)); + assert (!(*env)->IsSameObject (env, storagePathString, NULL)); + assert (earliestKeyTime >= 0); + BRMasterPubKey *masterPubKey = (BRMasterPubKey *) getJNIReference(env, masterPublicKeyObject); BRChainParams *chainParams = (BRChainParams *) getJNIReference(env, chainParamsObject); const char *storagePath = (*env)->GetStringUTFChars (env, storagePathString, 0); - assert (earliestKeyTime >= 0); - BRWalletManagerClient client = { transactionEventCallback, walletEventCallback, walletManagerEventCallback }; + BRWalletManagerClient client = { transactionEventCallback, walletEventCallback, walletManagerEventCallback }; BRWalletManager *walletManager = BRWalletManagerNew(client, *masterPubKey, chainParams, earliestKeyTime, storagePath); - assert (walletManager); return (jlong) walletManager; } @@ -147,8 +142,6 @@ transactionEventCallback (BRWalletManager wmid, JNIEnv *env = getEnv(); if (NULL == env) return; - __android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, "TransactionEvent: %d\n", e.type); - switch (e.type) { case BITCOIN_TRANSACTION_ADDED: (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleTransactionAdded, (jlong) wmid, (jlong) wid, (jlong) tid); @@ -170,8 +163,6 @@ walletEventCallback (BRWalletManager wmid, JNIEnv *env = getEnv(); if (NULL == env) return; - __android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, "WalletEvent: %d\n", e.type); - switch (e.type) { case BITCOIN_WALLET_CREATED: (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleWalletCreated, (jlong) wmid, (jlong) wid); @@ -191,8 +182,6 @@ walletManagerEventCallback (BRWalletManager wmid, JNIEnv *env = getEnv(); if (NULL == env) return; - __android_log_print (ANDROID_LOG_DEBUG, LOG_TAG, "WalletManagerEvent: %d\n", e.type); - switch (e.type) { case BITCOIN_WALLET_MANAGER_CONNECTED: (*env)->CallStaticVoidMethod(env, trampolineClass, trampolineHandleWalletManagerConnected, (jlong) wmid); diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.c deleted file mode 100644 index e69de29bb..000000000 diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h deleted file mode 100644 index 2092281b1..000000000 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h +++ /dev/null @@ -1,13 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient */ - -#ifndef _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient -#define _Included_com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient -#ifdef __cplusplus -extern "C" { -#endif -#ifdef __cplusplus -} -#endif -#endif diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c index 8e51c5719..f15c7941e 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_common_jni_JniReference.c @@ -24,9 +24,6 @@ #include "BRCryptoJni.h" #include "com_breadwallet_crypto_core_common_jni_JniReference.h" -// TODO: Add defensive checks on inputs -// TODO: Re-write using personal coding style - #define JNI_REFERENCE_ADDRESS_FIELD_NAME "jniReferenceAddress" #define JNI_REFERENCE_ADDRESS_FIELD_TYPE "J" // long diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c index 1b2269f4b..229ce02be 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_jni_Bip39.c @@ -26,16 +26,15 @@ #include "com_breadwallet_crypto_core_jni_Bip39.h" -// TODO: Add defensive checks on inputs -// TODO: Re-write using personal coding style - JNIEXPORT jbyteArray JNICALL Java_com_breadwallet_crypto_core_jni_Bip39_deriveKey ( JNIEnv * env, jclass thisClass, jstring seedString) { - UInt512 seed = UINT512_ZERO; - const char *status = (*env)->GetStringUTFChars (env, seedString, 0); + assert (!(*env)->IsSameObject (env, thisClass, NULL)); + + UInt512 seed = UINT512_ZERO; + const char *status = (*env)->GetStringUTFChars (env, seedString, 0); BRBIP39DeriveKey(seed.u8, status, NULL); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java index 6c5525832..5660c6e4c 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java @@ -1,11 +1,11 @@ package com.breadwallet.crypto.api; import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; -import com.breadwallet.crypto.api.provider.AccountProvider; +import com.breadwallet.crypto.api.factories.AccountFactory; public interface Account { - AccountProvider FACTORY = CryptoApi.cryptoApiProvider().accountProvider(); + AccountFactory FACTORY = CryptoApi.provider().accountFactory(); BitcoinMasterPubKey masterPublicKey(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java index 78c368834..ef6f7fafe 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Amount.java @@ -17,4 +17,9 @@ private Amount (BigInteger value, Unit unit) { public Amount (long value, Unit unit) { this (BigInteger.valueOf(value), unit); } + + @Override + public String toString() { + return String.format("Amount(value = %s, unit = %s, negative = %b)", value, unit, isNegative); + } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Bitcoin.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Bitcoin.java new file mode 100644 index 000000000..0498d3866 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Bitcoin.java @@ -0,0 +1,13 @@ +package com.breadwallet.crypto.api; + +public interface Bitcoin { + + Currency CURRENCY = new Currency ("BTC", "\u20BF", "Bitcoin", 8, "SAT", "sat"); + + Unit SATOSHI = CURRENCY.baseUnit; + Unit BITCOIN = CURRENCY.defaultUnit; + + Network TESTNET = CryptoApi.provider().networkFactory().testnet(); + + Network MAINNET = CryptoApi.provider().networkFactory().mainnet(); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java index 8ee2b550b..464c7f609 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java @@ -1,18 +1,28 @@ package com.breadwallet.crypto.api; -import com.breadwallet.crypto.api.provider.CryptoApiProvider; +import com.breadwallet.crypto.api.factories.AccountFactory; +import com.breadwallet.crypto.api.factories.NetworkFactory; +import com.breadwallet.crypto.api.factories.WalletManagerFactory; // TODO: Add guard around initialization occuring once -// TODO: Review visibility (for class, methods, fields, etc.) public class CryptoApi { - private static CryptoApiProvider cryptoApiProvider; + public interface Provider { - public static void init(CryptoApiProvider provider) { - cryptoApiProvider = provider; + AccountFactory accountFactory(); + + WalletManagerFactory walletManagerFactory(); + + NetworkFactory networkFactory(); + } + + private static Provider provider; + + public static void init(Provider provider) { + CryptoApi.provider = provider; } - public static CryptoApiProvider cryptoApiProvider() { - return cryptoApiProvider; + /* package */ static Provider provider() { + return provider; } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java index 68a9b4570..45af7386c 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Currency.java @@ -18,4 +18,10 @@ public Currency(String code, String symbol, String name, int decimals, String ba this.baseUnit = new Unit (this, baseUnitName, baseUnitSymbol); this.defaultUnit = new Unit (code, symbol, (long) Math.pow (10, decimals), this.baseUnit); } + + @Override + public String toString() { + return String.format("Currency(code = %s, symbol = %s, name = %s, decimals = %d, base = %s, default = %s)", + code, symbol, name, decimals, baseUnit, defaultUnit); + } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java index 01332e8c0..5b9e48a4a 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Unit.java @@ -30,4 +30,9 @@ public Unit(String name, String symbol, long scale, Unit base) { this.base = base; } + @Override + public String toString() { + return String.format("Unit(currency = %s, name = %s, symbol = %s, scale = %d, base = %s)", + currency, name, symbol, scale, base); + } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java index 93440e59a..246065f4f 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Wallet.java @@ -8,7 +8,7 @@ public interface Wallet { Currency currency(); - Amount getBalance(); + Amount balance(); - Transfer[] getTransfers(); + Transfer[] transfers(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java index acd8eeb26..9a9ff5821 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java @@ -1,10 +1,10 @@ package com.breadwallet.crypto.api; -import com.breadwallet.crypto.api.provider.WalletManagerProvider; +import com.breadwallet.crypto.api.factories.WalletManagerFactory; public interface WalletManager { - WalletManagerProvider FACTORY = CryptoApi.cryptoApiProvider().walletManagerProvider(); + WalletManagerFactory FACTORY = CryptoApi.provider().walletManagerFactory(); enum Mode { API_ONLY, API_WITH_P2P_SUBMIT, P2P_WITH_API_SYNC, P2P_ONLY, } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java deleted file mode 100644 index b3c146f94..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/Bitcoin.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.breadwallet.crypto.api.bitcoin; - -import com.breadwallet.crypto.api.CryptoApi; -import com.breadwallet.crypto.api.Currency; -import com.breadwallet.crypto.api.Network; -import com.breadwallet.crypto.api.Unit; - -public interface Bitcoin { - - Currency CURRENCY = new Currency ("BTC", "\u20BF", "Bitcoin", 8, "SAT", "sat"); - - Unit SAT = CURRENCY.baseUnit; - Unit BITCOIN = CURRENCY.defaultUnit; - - Network TESTNET = CryptoApi.cryptoApiProvider().networkProvider().testnet(); - - Network MAINNET = CryptoApi.cryptoApiProvider().networkProvider().mainnet(); -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java index a26a7fe52..0f9b7133a 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinChainParams.java @@ -1,5 +1,4 @@ package com.breadwallet.crypto.api.bitcoin; -// TODO: Add methods for any information we want to expose public interface BitcoinChainParams { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java index a624016d3..d617026aa 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/bitcoin/BitcoinMasterPubKey.java @@ -1,5 +1,4 @@ package com.breadwallet.crypto.api.bitcoin; -// TODO: Add methods for any information we want to expose public interface BitcoinMasterPubKey { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/CreatedTransferEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/CreatedTransferEvent.java new file mode 100644 index 000000000..78ed14a0b --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/CreatedTransferEvent.java @@ -0,0 +1,9 @@ +package com.breadwallet.crypto.api.events.transfer; + +public class CreatedTransferEvent implements TransferEvent { + + @Override + public String toString() { + return "CreatedTransferEvent"; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/DeletedTransferEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/DeletedTransferEvent.java new file mode 100644 index 000000000..72f5e86ac --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/DeletedTransferEvent.java @@ -0,0 +1,9 @@ +package com.breadwallet.crypto.api.events.transfer; + +public class DeletedTransferEvent implements TransferEvent { + + @Override + public String toString() { + return "DeletedTransferEvent"; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java index 55c0204e3..d3a019bea 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java @@ -1,5 +1,5 @@ package com.breadwallet.crypto.api.events.transfer; -// TODO: Create transfer events +// TODO: Add changed event public interface TransferEvent { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java index 207d773be..db105bd06 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java @@ -1,14 +1,21 @@ package com.breadwallet.crypto.api.events.wallet; +import com.breadwallet.crypto.api.Amount; + public class BalanceUpdatedWalletEvent implements WalletEvent { - private final long balance; + private final Amount amount; + + public BalanceUpdatedWalletEvent(Amount amount) { + this.amount = amount; + } - public BalanceUpdatedWalletEvent(long balance) { - this.balance = balance; + public Amount amount() { + return amount; } - public long balance() { - return balance; + @Override + public String toString() { + return String.format("BalanceUpdatedWalletEvent(amount = %s)", amount); } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java index 079a80df0..574b6699d 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java @@ -1,4 +1,9 @@ package com.breadwallet.crypto.api.events.wallet; public class CreatedWalletEvent implements WalletEvent { + + @Override + public String toString() { + return "CreatedWalletEvent"; + } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java index 26fd5f9b5..239dbc34b 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java @@ -1,4 +1,9 @@ package com.breadwallet.crypto.api.events.wallet; public class DeletedWalletEvent implements WalletEvent { + + @Override + public String toString() { + return "DeletedWalletEvent"; + } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java index 1f505f55e..d06811d20 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java @@ -1,4 +1,5 @@ package com.breadwallet.crypto.api.events.wallet; +// TODO: Add transferAdded, transferChanged, transferDeleted & feeBasisUpdated events public interface WalletEvent { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ChangedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ChangedWalletManagerEvent.java new file mode 100644 index 000000000..894c66953 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ChangedWalletManagerEvent.java @@ -0,0 +1,27 @@ +package com.breadwallet.crypto.api.events.walletmanager; + +import com.breadwallet.crypto.api.WalletManager.State; + +public class ChangedWalletManagerEvent implements WalletManagerEvent { + + private final State oldState; + private final State newState; + + public ChangedWalletManagerEvent(State oldState, State newState) { + this.oldState = oldState; + this.newState = newState; + } + + public State oldState() { + return oldState; + } + + public State newState() { + return newState; + } + + @Override + public String toString() { + return String.format("ChangedWalletManagerEvent(old: %s, new: %s)", oldState, newState); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ConnectedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ConnectedWalletManagerEvent.java deleted file mode 100644 index db92a6bb6..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ConnectedWalletManagerEvent.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto.api.events.walletmanager; - -public class ConnectedWalletManagerEvent implements WalletManagerEvent { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/DisconnectedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/DisconnectedWalletManagerEvent.java deleted file mode 100644 index b2b556986..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/DisconnectedWalletManagerEvent.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.breadwallet.crypto.api.events.walletmanager; - -public class DisconnectedWalletManagerEvent implements WalletManagerEvent { -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncEndedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncEndedWalletManagerEvent.java new file mode 100644 index 000000000..3b3d559be --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncEndedWalletManagerEvent.java @@ -0,0 +1,15 @@ +package com.breadwallet.crypto.api.events.walletmanager; + +public class SyncEndedWalletManagerEvent implements WalletManagerEvent { + + private final String error; + + public SyncEndedWalletManagerEvent(String error) { + this.error = error; + } + + @Override + public String toString() { + return String.format("SyncEndedWalletManagerEvent(err: %s)", error); + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java index c093c9a6b..243d24e6c 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java @@ -1,4 +1,9 @@ package com.breadwallet.crypto.api.events.walletmanager; public class SyncStartedWalletManagerEvent implements WalletManagerEvent { + + @Override + public String toString() { + return "SyncStartedWalletManagerEvent"; + } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStoppedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStoppedWalletManagerEvent.java deleted file mode 100644 index c335c437c..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStoppedWalletManagerEvent.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.breadwallet.crypto.api.events.walletmanager; - -public class SyncStoppedWalletManagerEvent implements WalletManagerEvent { - - private final int errorCode; - - public SyncStoppedWalletManagerEvent(int errorCode) { - this.errorCode = errorCode; - } - - public int errorCode() { - return errorCode; - } -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java index ca0a91b8a..08d8bb04b 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java @@ -1,4 +1,5 @@ package com.breadwallet.crypto.api.events.walletmanager; +// TODO: Add walletAdded, walletChanged, walletDeleted, syncStarted, syncProgress & syncEnded events public interface WalletManagerEvent { } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/AccountProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/AccountFactory.java similarity index 59% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/AccountProvider.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/AccountFactory.java index 2ea00f53a..7f44c54e0 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/AccountProvider.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/AccountFactory.java @@ -1,8 +1,8 @@ -package com.breadwallet.crypto.api.provider; +package com.breadwallet.crypto.api.factories; import com.breadwallet.crypto.api.Account; -public interface AccountProvider { +public interface AccountFactory { Account create(String phrase); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/NetworkProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/NetworkFactory.java similarity index 54% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/NetworkProvider.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/NetworkFactory.java index 910255ba4..7b746c452 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/NetworkProvider.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/NetworkFactory.java @@ -1,8 +1,8 @@ -package com.breadwallet.crypto.api.provider; +package com.breadwallet.crypto.api.factories; import com.breadwallet.crypto.api.Network; -public interface NetworkProvider { +public interface NetworkFactory { Network testnet(); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/WalletManagerProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/WalletManagerFactory.java similarity index 51% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/WalletManagerProvider.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/WalletManagerFactory.java index 9e1f90e58..a1621d4c3 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/WalletManagerProvider.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/WalletManagerFactory.java @@ -1,4 +1,4 @@ -package com.breadwallet.crypto.api.provider; +package com.breadwallet.crypto.api.factories; import com.breadwallet.crypto.api.Account; import com.breadwallet.crypto.api.Network; @@ -9,24 +9,15 @@ import java.util.concurrent.Executor; -public interface WalletManagerProvider { +public interface WalletManagerFactory { - WalletManager createBitcoinWalletManager(BitcoinWalletManagerListener listener, - Account account, + WalletManager createBitcoinWalletManager(Account account, Network network, WalletManager.Mode mode, int earliestKeyTime, String storagePath, BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient); - - WalletManager createBitcoinWalletManager(Executor listenerExecutor, + BitcoinBackendClient backendClient, BitcoinWalletManagerListener listener, - Account account, - Network network, - WalletManager.Mode mode, - int earliestKeyTime, - String storagePath, - BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient); + Executor listenerExecutor); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java deleted file mode 100644 index b84288626..000000000 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/provider/CryptoApiProvider.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.breadwallet.crypto.api.provider; - -public interface CryptoApiProvider { - - AccountProvider accountProvider(); - - WalletManagerProvider walletManagerProvider(); - - NetworkProvider networkProvider(); -} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java index 4a40c9d17..98159c130 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java @@ -3,10 +3,9 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; import com.breadwallet.crypto.core.jni.Bip39; -// TODO: Review visibility (for class, methods, fields, etc.) public class Account implements com.breadwallet.crypto.api.Account { - protected final BitcoinMasterPubKey masterPublicKey; + private final BitcoinMasterPubKey masterPublicKey; public Account(String phrase) { this(Bip39.deriveKey(phrase)); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java index 8939c6d8d..fa73a0a40 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java @@ -5,26 +5,24 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; -import com.breadwallet.crypto.api.provider.AccountProvider; -import com.breadwallet.crypto.api.provider.NetworkProvider; -import com.breadwallet.crypto.api.provider.WalletManagerProvider; -import com.breadwallet.crypto.api.provider.CryptoApiProvider; +import com.breadwallet.crypto.api.factories.AccountFactory; +import com.breadwallet.crypto.api.factories.NetworkFactory; +import com.breadwallet.crypto.api.factories.WalletManagerFactory; import com.breadwallet.crypto.core.bitcoin.BitcoinChainParams; import com.breadwallet.crypto.core.bitcoin.BitcoinWalletManager; import java.util.concurrent.Executor; // TODO: Init providers once, instead of on each invocation -// TODO: Review visibility (for class, methods, fields, etc.) -public class CoreCryptoApi implements CryptoApiProvider { +public class CoreCryptoApi implements CryptoApi.Provider { static { System.loadLibrary("crypto"); } @Override - public AccountProvider accountProvider() { - return new AccountProvider() { + public AccountFactory accountFactory() { + return new AccountFactory() { @Override public Account create(String phrase) { return new com.breadwallet.crypto.core.Account(phrase); @@ -38,53 +36,34 @@ public Account create(byte[] seed) { } @Override - public WalletManagerProvider walletManagerProvider() { - return new WalletManagerProvider() { + public WalletManagerFactory walletManagerFactory() { + return new WalletManagerFactory() { @Override - public WalletManager createBitcoinWalletManager(BitcoinWalletManagerListener listener, - Account account, + public WalletManager createBitcoinWalletManager(Account account, Network network, WalletManager.Mode mode, int earliestKeyTime, String storagePath, BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient) { - return new BitcoinWalletManager(listener, - account, - network, - mode, - earliestKeyTime, - storagePath, - persistenceClient, - backendClient); - } - - @Override - public WalletManager createBitcoinWalletManager(Executor executor, + BitcoinBackendClient backendClient, BitcoinWalletManagerListener listener, - Account account, - Network network, - WalletManager.Mode mode, - int earliestKeyTime, - String storagePath, - BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient) { - return new BitcoinWalletManager(executor, - listener, - account, + Executor listenerExecutor) { + return new BitcoinWalletManager(account, network, mode, earliestKeyTime, storagePath, persistenceClient, - backendClient); + backendClient, + listener, + listenerExecutor); } }; } @Override - public NetworkProvider networkProvider() { - return new NetworkProvider() { + public NetworkFactory networkFactory() { + return new NetworkFactory() { @Override public Network testnet() { return new Network(new Network.Bitcoin("BTC Testnet", 0x40, BitcoinChainParams.TESTNET)); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java index 794066877..e2e9cd369 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParams.java @@ -2,15 +2,18 @@ import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinChainParams; -// TODO: Add MAINNET -// TODO: Review visibility (for class, methods, fields, etc.) -public class BitcoinChainParams implements com.breadwallet.crypto.api.bitcoin.BitcoinChainParams { +public final class BitcoinChainParams implements com.breadwallet.crypto.api.bitcoin.BitcoinChainParams { + // TODO: Add MAINNET public static BitcoinChainParams TESTNET = new BitcoinChainParams(CoreBitcoinChainParams.TESTNET); - /* package */ final CoreBitcoinChainParams chainParams; + private final CoreBitcoinChainParams chainParams; private BitcoinChainParams(CoreBitcoinChainParams chainParams) { this.chainParams = chainParams; } + + /* package */ CoreBitcoinChainParams chainParams() { + return chainParams; + } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java index 6026465a5..7cd0f4b73 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinChainParamsAdapter.java @@ -2,8 +2,7 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinChainParams; -// TODO: Review visibility (for class, methods, fields, etc.) -/* package */ class BitcoinChainParamsAdapter { +/* package */ final class BitcoinChainParamsAdapter { public static com.breadwallet.crypto.core.bitcoin.BitcoinChainParams from(BitcoinChainParams bcp) { if (bcp instanceof com.breadwallet.crypto.core.bitcoin.BitcoinChainParams) { diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java index dfad6db02..e66db9e64 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java @@ -2,17 +2,17 @@ import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinMasterPubKey; -// TODO: Add parameter validation -// TODO: Review visibility (for class, methods, fields, etc.) -public class BitcoinMasterPubKey implements com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey { +public final class BitcoinMasterPubKey implements com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey { - /* package */ final CoreBitcoinMasterPubKey masterPubKey; + private final CoreBitcoinMasterPubKey masterPubKey; public BitcoinMasterPubKey(byte[] seed) { - this(new CoreBitcoinMasterPubKey(seed)); + // TODO: Add additional seed validation, if possible + if (null == seed) throw new IllegalArgumentException("Invalid seed"); + this.masterPubKey = new CoreBitcoinMasterPubKey(seed); } - private BitcoinMasterPubKey(CoreBitcoinMasterPubKey masterPubKey) { - this.masterPubKey = masterPubKey; + /* package */ CoreBitcoinMasterPubKey masterPubKey() { + return masterPubKey; } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java index c2fbff5f4..b68338925 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKeyAdapter.java @@ -2,8 +2,7 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; -// TODO: Review visibility (for class, methods, fields, etc.) -/* package */ class BitcoinMasterPubKeyAdapter { +/* package */ final class BitcoinMasterPubKeyAdapter { public static com.breadwallet.crypto.core.bitcoin.BitcoinMasterPubKey from(BitcoinMasterPubKey mbk) { if (mbk instanceof com.breadwallet.crypto.core.bitcoin.BitcoinMasterPubKey) { diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWallet.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWallet.java new file mode 100644 index 000000000..b6f672025 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWallet.java @@ -0,0 +1,47 @@ +package com.breadwallet.crypto.core.bitcoin; + +import com.breadwallet.crypto.api.Amount; +import com.breadwallet.crypto.api.Currency; +import com.breadwallet.crypto.api.Transfer; +import com.breadwallet.crypto.api.Wallet; +import com.breadwallet.crypto.api.WalletManager; +import com.breadwallet.crypto.api.Bitcoin; +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWallet; + +public final class BitcoinWallet implements Wallet { + + private final CoreBitcoinWallet wallet; + private final BitcoinWalletManager walletManager; + + /* packate */ BitcoinWallet(CoreBitcoinWallet wallet, BitcoinWalletManager walletManager) { + this.wallet = wallet; + this.walletManager = walletManager; + } + + @Override + public WalletManager manager() { + return walletManager; + } + + @Override + public String name() { + return Bitcoin.CURRENCY.name; + } + + @Override + public Currency currency() { + return Bitcoin.CURRENCY; + } + + @Override + public Amount balance() { + // TODO: Implement this + return null; + } + + @Override + public Transfer[] transfers() { + // TODO: Implement this + return new Transfer[0]; + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java index fa4b42531..9eed00c27 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java @@ -1,72 +1,68 @@ package com.breadwallet.crypto.core.bitcoin; import com.breadwallet.crypto.api.Account; +import com.breadwallet.crypto.api.Amount; import com.breadwallet.crypto.api.Network; +import com.breadwallet.crypto.api.Wallet; import com.breadwallet.crypto.api.WalletManager; +import com.breadwallet.crypto.api.Bitcoin; import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; import com.breadwallet.crypto.api.events.wallet.BalanceUpdatedWalletEvent; import com.breadwallet.crypto.api.events.wallet.CreatedWalletEvent; import com.breadwallet.crypto.api.events.wallet.DeletedWalletEvent; -import com.breadwallet.crypto.api.events.walletmanager.ConnectedWalletManagerEvent; -import com.breadwallet.crypto.api.events.walletmanager.DisconnectedWalletManagerEvent; +import com.breadwallet.crypto.api.events.walletmanager.ChangedWalletManagerEvent; +import com.breadwallet.crypto.api.events.walletmanager.SyncEndedWalletManagerEvent; import com.breadwallet.crypto.api.events.walletmanager.SyncStartedWalletManagerEvent; -import com.breadwallet.crypto.api.events.walletmanager.SyncStoppedWalletManagerEvent; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinChainParams; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinMasterPubKey; +import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWallet; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWalletManager; import com.breadwallet.crypto.core.bitcoin.jni.CoreBitcoinWalletManagerClient; import java.lang.ref.WeakReference; import java.util.concurrent.Executor; -import java.util.concurrent.Executors; -// TODO: Add parameter validation -// TODO: Review visibility (for class, methods, fields, etc.) -// TODO: Consider exposing this via a registered factory -public final class BitcoinWalletManager - implements WalletManager, CoreBitcoinWalletManagerClient { +// TODO: Verify assumption that CoreBitcoinWalletManagerClient callbacks are on single native thread +public final class BitcoinWalletManager implements WalletManager, CoreBitcoinWalletManagerClient { - protected static Executor DEFAULT_EXECUTOR = Executors.newSingleThreadExecutor(); + private final WeakReference listener; + private final CoreBitcoinWalletManager coreWalletManager; - protected final WeakReference listener; - protected final CoreBitcoinWalletManager coreWalletManager; + private final BitcoinPersistenceClient persistenceClient; + private final BitcoinBackendClient backendClient; - protected final BitcoinPersistenceClient persistenceClient; - protected final BitcoinBackendClient backendClient; + private final Account account; + private final Network network; + private final Mode mode; + private final long earliestKeyTime; + private final String storagePath; + private final Executor listenerExecutor; - protected final Account account; - protected final Network network; - protected final Mode mode; - protected final long earliestKeyTime; - protected final String storagePath; - protected final Executor listenerExecutor; + private State state; - protected State state; - - public BitcoinWalletManager(BitcoinWalletManagerListener listener, - Account account, + public BitcoinWalletManager(Account account, Network network, Mode mode, int earliestKeyTime, String storagePath, BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient) { - this(DEFAULT_EXECUTOR, listener, account, network, mode, earliestKeyTime, storagePath, persistenceClient, backendClient); - } - - public BitcoinWalletManager(Executor listenerExecutor, + BitcoinBackendClient backendClient, BitcoinWalletManagerListener listener, - Account account, - Network network, - Mode mode, - int earliestKeyTime, - String storagePath, - BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient) { - CoreBitcoinChainParams chainParams = BitcoinChainParamsAdapter.from(network.chainParams()).chainParams; - CoreBitcoinMasterPubKey masterPubKey = BitcoinMasterPubKeyAdapter.from(account.masterPublicKey()).masterPubKey; + Executor listenerExecutor) { + if (null == account) throw new IllegalArgumentException("Invalid seed"); + if (null == network) throw new IllegalArgumentException("Invalid network"); + if (null == mode) throw new IllegalArgumentException("Invalid mode"); + // TODO: Add validation on earliestKeyTime + if (null == storagePath) throw new IllegalArgumentException("Invalid storage path"); + if (null == persistenceClient) throw new IllegalArgumentException("Invalid persistence client"); + if (null == backendClient) throw new IllegalArgumentException("Invalid backend client"); + if (null == listener) throw new IllegalArgumentException("Invalid listener"); + if (null == listenerExecutor) throw new IllegalArgumentException("Invalid listener executor"); + + CoreBitcoinChainParams chainParams = BitcoinChainParamsAdapter.from(network.chainParams()).chainParams(); + CoreBitcoinMasterPubKey masterPubKey = BitcoinMasterPubKeyAdapter.from(account.masterPublicKey()).masterPubKey(); this.listenerExecutor = listenerExecutor; this.account = account; @@ -98,7 +94,7 @@ public void disconnect() { // CoreWalletManagerClient @Override - public void handleTransactionAdded(long wid, long tid) { + public void handleTransactionAdded(CoreBitcoinWallet coreWallet) { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { @@ -108,7 +104,7 @@ public void handleTransactionAdded(long wid, long tid) { } @Override - public void handleTransactionUpdated(long wid, long tid) { + public void handleTransactionUpdated(CoreBitcoinWallet coreWallet) { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { @@ -118,7 +114,7 @@ public void handleTransactionUpdated(long wid, long tid) { } @Override - public void handleTransactionDeleted(long wid, long tid) { + public void handleTransactionDeleted(CoreBitcoinWallet coreWallet) { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { @@ -128,74 +124,94 @@ public void handleTransactionDeleted(long wid, long tid) { } @Override - public void handleWalletCreated(long wid) { + public void handleWalletCreated(CoreBitcoinWallet coreWallet) { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - // TODO: Get wallet using wid - l.handleWalletEvent(this, null, new CreatedWalletEvent()); + Wallet wallet = new BitcoinWallet(coreWallet, this); + l.handleWalletEvent(this, wallet, new CreatedWalletEvent()); } }); } @Override - public void handleWalletBalanceUpdated(long wid, long satoshi) { + public void handleWalletBalanceUpdated(CoreBitcoinWallet coreWallet, long satoshi) { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { // TODO: Get wallet using wid - l.handleWalletEvent(this, null, new BalanceUpdatedWalletEvent(satoshi)); + Wallet wallet = new BitcoinWallet(coreWallet, this); + Amount amount = new Amount(satoshi, Bitcoin.SATOSHI); + l.handleWalletEvent(this, wallet, new BalanceUpdatedWalletEvent(amount)); } }); } @Override - public void handleWalletDeleted(long wid) { + public void handleWalletDeleted(CoreBitcoinWallet coreWallet) { listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - // TODO: Get wallet using wid - l.handleWalletEvent(this, null, new DeletedWalletEvent()); + Wallet wallet = new BitcoinWallet(coreWallet, this); + l.handleWalletEvent(this, wallet, new DeletedWalletEvent()); } }); } @Override public void handleWalletManagerConnected() { + State oldState = state; + state = State.CONNECTED; + State newState = state; + listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - l.handleManagerEvent(this, new ConnectedWalletManagerEvent()); + l.handleManagerEvent(this, new ChangedWalletManagerEvent(oldState, newState)); } }); } @Override public void handleWalletManagerDisconnected() { + State oldState = state; + state = State.DISCONNECTED; + State newState = state; + listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - l.handleManagerEvent(this, new DisconnectedWalletManagerEvent()); + l.handleManagerEvent(this, new ChangedWalletManagerEvent(oldState, newState)); } }); } @Override public void handleWalletManagerSyncStarted() { + State oldState = state; + state = State.SYNCING; + State newState = state; + listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { + l.handleManagerEvent(this, new ChangedWalletManagerEvent(oldState, newState)); l.handleManagerEvent(this, new SyncStartedWalletManagerEvent()); } }); } @Override - public void handleWalletManagerSyncStopped(int errorCode) { + public void handleWalletManagerSyncStopped(String error) { + State oldState = state; + state = State.CONNECTED; + State newState = state; + listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - l.handleManagerEvent(this, new SyncStoppedWalletManagerEvent(errorCode)); + l.handleManagerEvent(this, new ChangedWalletManagerEvent(oldState, newState)); + l.handleManagerEvent(this, new SyncEndedWalletManagerEvent(error)); } }); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java index 7f41281f5..ef0d4d5bf 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinChainParams.java @@ -2,9 +2,7 @@ import com.breadwallet.crypto.core.common.jni.JniReference; -// TODO: Review visibility (for class, methods, fields, etc.) -public class CoreBitcoinChainParams - extends JniReference { +public final class CoreBitcoinChainParams extends JniReference { public static CoreBitcoinChainParams TESTNET = new CoreBitcoinChainParams(createTestnetChainParams()); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java index da96a2dba..aebb8f4df 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinMasterPubKey.java @@ -3,17 +3,11 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; import com.breadwallet.crypto.core.common.jni.JniReference; -// TODO: Review visibility (for class, methods, fields, etc.) -public class CoreBitcoinMasterPubKey - extends JniReference implements BitcoinMasterPubKey { +public final class CoreBitcoinMasterPubKey extends JniReference implements BitcoinMasterPubKey { private static native long createBitcoinMasterPubKey (byte[] seed); public CoreBitcoinMasterPubKey(byte[] seed) { - this(createBitcoinMasterPubKey(seed)); - } - - private CoreBitcoinMasterPubKey(long jniReferenceAddress) { - super(jniReferenceAddress); + super(createBitcoinMasterPubKey(seed)); } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java new file mode 100644 index 000000000..9bc43a92c --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWallet.java @@ -0,0 +1,20 @@ +package com.breadwallet.crypto.core.bitcoin.jni; + +import com.breadwallet.crypto.core.common.jni.JniReference; + +public class CoreBitcoinWallet extends JniReference { + + // Maintain a reference to the owning wallet manager to avoid a user after free + private final CoreBitcoinWalletManager owner; + + /* package */ CoreBitcoinWallet(long jniReferenceAddress, CoreBitcoinWalletManager owner) { + super(jniReferenceAddress); + this.owner = owner; + } + + @Override + protected void dispose() { + // Intentionally omit call to disposeNative() as the jniReferenceAddress is borrowed + // from the owning wallet manager + } +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java index 5a9ea3869..3ee433ac8 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManager.java @@ -6,10 +6,7 @@ import java.util.HashMap; import java.util.Map; -// TODO: Add parameter validation -// TODO: Hook up listener callbacks -// TODO: Review visibility (for class, methods, fields, etc.) -public class CoreBitcoinWalletManager extends JniReference { +public final class CoreBitcoinWalletManager extends JniReference { private static final Map> wmMap = new HashMap<>(); @@ -104,7 +101,8 @@ public CoreBitcoinWalletManager(CoreBitcoinWalletManagerClient client, CoreBitcoinChainParams params, int earliestKeyTime, String storagePath) { - // ISSUE: we will miss the first event because we are not registerd in the map yet + // ISSUE: we miss the first event because we are not registerd in the map yet, look into + // solutions super(createBitcoinWalletManager(mpk, params, earliestKeyTime, storagePath)); wmMap.put(jniReferenceAddress, new WeakReference<> (this)); @@ -116,47 +114,57 @@ public CoreBitcoinWalletManager(CoreBitcoinWalletManagerClient client, public native void disconnect(); + @Override protected native void disposeNative(); private void handleTransactionAdded(long wid, long tid) { CoreBitcoinWalletManagerClient wmc = client.get(); if (null != wmc) { - wmc.handleTransactionAdded(wid, tid); + // TODO: Use tid to create transfer wrapper + CoreBitcoinWallet wallet = new CoreBitcoinWallet(wid, this); + wmc.handleTransactionAdded(wallet); } } private void handleTransactionUpdated(long wid, long tid) { CoreBitcoinWalletManagerClient wmc = client.get(); if (null != wmc) { - wmc.handleTransactionUpdated(wid, tid); + // TODO: Use tid to create transfer wrapper + CoreBitcoinWallet wallet = new CoreBitcoinWallet(wid, this); + wmc.handleTransactionUpdated(wallet); } } private void handleTransactionDeleted(long wid, long tid) { CoreBitcoinWalletManagerClient wmc = client.get(); if (null != wmc) { - wmc.handleTransactionDeleted(wid, tid); + // TODO: Use tid to create transfer wrapper + CoreBitcoinWallet wallet = new CoreBitcoinWallet(wid, this); + wmc.handleTransactionDeleted(wallet); } } private void handleWalletCreated(long wid) { CoreBitcoinWalletManagerClient wmc = client.get(); if (null != wmc) { - wmc.handleWalletCreated(wid); + CoreBitcoinWallet wallet = new CoreBitcoinWallet(wid, this); + wmc.handleWalletCreated(wallet); } } private void handleWalletBalanceUpdated(long wid, long satoshi) { CoreBitcoinWalletManagerClient wmc = client.get(); if (null != wmc) { - wmc.handleWalletBalanceUpdated(wid, satoshi); + CoreBitcoinWallet wallet = new CoreBitcoinWallet(wid, this); + wmc.handleWalletBalanceUpdated(wallet, satoshi); } } private void handleWalletDeleted(long wid) { CoreBitcoinWalletManagerClient wmc = client.get(); if (null != wmc) { - wmc.handleWalletDeleted(wid); + CoreBitcoinWallet wallet = new CoreBitcoinWallet(wid, this); + wmc.handleWalletDeleted(wallet); } } @@ -184,7 +192,8 @@ private void handleWalletManagerSyncStarted() { private void handleWalletManagerSyncStopped(int errorCode) { CoreBitcoinWalletManagerClient wmc = client.get(); if (null != wmc) { - wmc.handleWalletManagerSyncStopped(errorCode); + // TODO: Convert error code to string properly + wmc.handleWalletManagerSyncStopped(Integer.toString(errorCode)); } } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java index 88587672c..302ffec3f 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/jni/CoreBitcoinWalletManagerClient.java @@ -2,16 +2,16 @@ public interface CoreBitcoinWalletManagerClient { - void handleTransactionAdded(long wid, long tid); - void handleTransactionUpdated(long wid, long tid); - void handleTransactionDeleted(long wid, long tid); + void handleTransactionAdded(CoreBitcoinWallet wallet); + void handleTransactionUpdated(CoreBitcoinWallet wallet); + void handleTransactionDeleted(CoreBitcoinWallet wallet); - void handleWalletCreated(long wid); - void handleWalletBalanceUpdated(long wid, long satoshi); - void handleWalletDeleted(long wid); + void handleWalletCreated(CoreBitcoinWallet wallet); + void handleWalletBalanceUpdated(CoreBitcoinWallet wallet, long satoshi); + void handleWalletDeleted(CoreBitcoinWallet wallet); void handleWalletManagerConnected(); void handleWalletManagerDisconnected(); void handleWalletManagerSyncStarted(); - void handleWalletManagerSyncStopped(int errorCode); + void handleWalletManagerSyncStopped(String error); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java index 340ae157c..5da9731a7 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/common/jni/JniReference.java @@ -1,6 +1,5 @@ package com.breadwallet.crypto.core.common.jni; -// TODO: Review visibility (for class, methods, fields, etc.) public class JniReference { protected static boolean SHOW_FINALIZE = false; @@ -31,6 +30,7 @@ protected void dispose() { protected native void disposeNative(); + @Override public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java index d1ae03d9d..772a96dbc 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java @@ -1,6 +1,7 @@ package com.breadwallet.crypto.core.jni; -// TODO: Review visibility (for class, methods, fields, etc.) +// TODO: Expose a public static native method that wraps a private static native impl to +// facilitate checks public class Bip39 { public static native byte[] deriveKey(String phrase); From 31670c9430c9d955f23f6fe0138455e792a71400 Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Wed, 13 Mar 2019 21:17:42 -0400 Subject: [PATCH 10/12] wip: Update Crypto Makefile to delete empty jni files --- Java/Crypto.Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Java/Crypto.Makefile b/Java/Crypto.Makefile index d96417101..88607b8d9 100644 --- a/Java/Crypto.Makefile +++ b/Java/Crypto.Makefile @@ -117,6 +117,10 @@ jni_hdr_crypto: FORCE for class in *.class; do \ javah -jni -d $(DIR)/$(JNI_SDIR) -classpath $(DIR)/build com.breadwallet.crypto.core.jni.$${class%%.class}; \ done) + @(cd $(JNI_SDIR); \ + rm -f .h \ + com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWallet.h \ + com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinWalletManagerClient.h) jni_hdr: java_comp jni_hdr_crypto From c37ee9393da965312e20b803356f9c20921db13a Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Wed, 13 Mar 2019 21:46:47 -0400 Subject: [PATCH 11/12] wip: Additional refactoring and cleanup --- .../coredemo/CoreDemoBitcoinClient.java | 7 +- ..._core_bitcoin_jni_CoreBitcoinChainParams.c | 2 +- .../com/breadwallet/crypto/api/Account.java | 14 ++- .../com/breadwallet/crypto/api/Bitcoin.java | 12 +-- .../com/breadwallet/crypto/api/CryptoApi.java | 4 +- .../breadwallet/crypto/api/WalletManager.java | 37 +++++-- .../api/factories/WalletManagerFactory.java | 3 +- .../com/breadwallet/crypto/core/Account.java | 2 +- .../crypto/core/CoreCryptoApi.java | 98 ++++++++++--------- .../core/bitcoin/BitcoinMasterPubKey.java | 2 +- .../core/bitcoin/BitcoinWalletManager.java | 5 +- 11 files changed, 113 insertions(+), 73 deletions(-) diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java index 07f6615db..2116ff531 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java @@ -7,6 +7,7 @@ import com.breadwallet.crypto.api.Transfer; import com.breadwallet.crypto.api.Wallet; import com.breadwallet.crypto.api.WalletManager; +import com.breadwallet.crypto.api.WalletManager.Mode; import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; @@ -25,10 +26,10 @@ public class CoreDemoBitcoinClient private final WalletManager walletManager; public CoreDemoBitcoinClient(Network network, String storagePath, String paperKey) { - this.walletManager = WalletManager.FACTORY.createBitcoinWalletManager( - Account.FACTORY.create(paperKey), + this.walletManager = WalletManager.createBitcoinWalletManager( + Account.create(paperKey), network, - WalletManager.Mode.API_WITH_P2P_SUBMIT, + Mode.API_WITH_P2P_SUBMIT, 1543190400, storagePath, this, diff --git a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c index 4281368d8..2e6da6596 100644 --- a/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c +++ b/Java/Crypto/src/main/cpp/jni/com_breadwallet_crypto_core_bitcoin_jni_CoreBitcoinChainParams.c @@ -18,7 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include #include "BRChainParams.h" diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java index 5660c6e4c..201ce0649 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Account.java @@ -3,9 +3,17 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; import com.breadwallet.crypto.api.factories.AccountFactory; -public interface Account { +public abstract class Account { - AccountFactory FACTORY = CryptoApi.provider().accountFactory(); + private static AccountFactory FACTORY = CryptoApi.provider().accountFactory(); - BitcoinMasterPubKey masterPublicKey(); + public static Account create(String phrase) { + return FACTORY.create(phrase); + } + + public static Account create(byte[] seed) { + return FACTORY.create(seed); + } + + public abstract BitcoinMasterPubKey masterPublicKey(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Bitcoin.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Bitcoin.java index 0498d3866..7178d021e 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Bitcoin.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/Bitcoin.java @@ -1,13 +1,13 @@ package com.breadwallet.crypto.api; -public interface Bitcoin { +public final class Bitcoin { - Currency CURRENCY = new Currency ("BTC", "\u20BF", "Bitcoin", 8, "SAT", "sat"); + public static final Currency CURRENCY = new Currency ("BTC", "\u20BF", "Bitcoin", 8, "SAT", "sat"); - Unit SATOSHI = CURRENCY.baseUnit; - Unit BITCOIN = CURRENCY.defaultUnit; + public static final Unit SATOSHI = CURRENCY.baseUnit; + public static final Unit BITCOIN = CURRENCY.defaultUnit; - Network TESTNET = CryptoApi.provider().networkFactory().testnet(); + public static final Network TESTNET = CryptoApi.provider().networkFactory().testnet(); - Network MAINNET = CryptoApi.provider().networkFactory().mainnet(); + public static final Network MAINNET = CryptoApi.provider().networkFactory().mainnet(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java index 464c7f609..46e5dca9d 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/CryptoApi.java @@ -4,7 +4,6 @@ import com.breadwallet.crypto.api.factories.NetworkFactory; import com.breadwallet.crypto.api.factories.WalletManagerFactory; -// TODO: Add guard around initialization occuring once public class CryptoApi { public interface Provider { @@ -18,7 +17,8 @@ public interface Provider { private static Provider provider; - public static void init(Provider provider) { + public static synchronized void init(Provider provider) { + if (null != CryptoApi.provider) throw new IllegalStateException("Provider already set"); CryptoApi.provider = provider; } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java index 9a9ff5821..9e322b06a 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/WalletManager.java @@ -1,16 +1,41 @@ package com.breadwallet.crypto.api; +import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; +import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; import com.breadwallet.crypto.api.factories.WalletManagerFactory; -public interface WalletManager { +import java.util.concurrent.Executor; - WalletManagerFactory FACTORY = CryptoApi.provider().walletManagerFactory(); +public abstract class WalletManager { - enum Mode { API_ONLY, API_WITH_P2P_SUBMIT, P2P_WITH_API_SYNC, P2P_ONLY, } + private static final WalletManagerFactory FACTORY = CryptoApi.provider().walletManagerFactory(); - enum State { CREATED, DISCONNECTED, CONNECTED, SYNCING, DELETED } + public static WalletManager createBitcoinWalletManager(Account account, + Network network, + Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient, + BitcoinWalletManagerListener listener, + Executor listenerExecutor) { + return FACTORY.createBitcoinWalletManager(account, + network, + mode, + earliestKeyTime, + storagePath, + persistenceClient, + backendClient, + listener, + listenerExecutor); + } - void connect(); + public enum Mode { API_ONLY, API_WITH_P2P_SUBMIT, P2P_WITH_API_SYNC, P2P_ONLY, } - void disconnect(); + public enum State { CREATED, DISCONNECTED, CONNECTED, SYNCING, DELETED } + + public abstract void connect(); + + public abstract void disconnect(); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/WalletManagerFactory.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/WalletManagerFactory.java index a1621d4c3..7f5bf81fd 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/WalletManagerFactory.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/factories/WalletManagerFactory.java @@ -3,6 +3,7 @@ import com.breadwallet.crypto.api.Account; import com.breadwallet.crypto.api.Network; import com.breadwallet.crypto.api.WalletManager; +import com.breadwallet.crypto.api.WalletManager.Mode; import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; @@ -13,7 +14,7 @@ public interface WalletManagerFactory { WalletManager createBitcoinWalletManager(Account account, Network network, - WalletManager.Mode mode, + Mode mode, int earliestKeyTime, String storagePath, BitcoinPersistenceClient persistenceClient, diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java index 98159c130..9c81fe9ad 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/Account.java @@ -3,7 +3,7 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinMasterPubKey; import com.breadwallet.crypto.core.jni.Bip39; -public class Account implements com.breadwallet.crypto.api.Account { +public final class Account extends com.breadwallet.crypto.api.Account { private final BitcoinMasterPubKey masterPublicKey; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java index fa73a0a40..d6a9e2a8f 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/CoreCryptoApi.java @@ -2,6 +2,7 @@ import com.breadwallet.crypto.api.*; import com.breadwallet.crypto.api.Account; +import com.breadwallet.crypto.api.WalletManager.Mode; import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; @@ -13,67 +14,72 @@ import java.util.concurrent.Executor; -// TODO: Init providers once, instead of on each invocation public class CoreCryptoApi implements CryptoApi.Provider { static { System.loadLibrary("crypto"); } + private final AccountFactory accountFactory = new AccountFactory() { + @Override + public Account create(String phrase) { + return new com.breadwallet.crypto.core.Account(phrase); + } + + @Override + public Account create(byte[] seed) { + return new com.breadwallet.crypto.core.Account(seed); + } + }; + + private final WalletManagerFactory walletManagerFactory = new WalletManagerFactory() { + @Override + public WalletManager createBitcoinWalletManager(Account account, + Network network, + Mode mode, + int earliestKeyTime, + String storagePath, + BitcoinPersistenceClient persistenceClient, + BitcoinBackendClient backendClient, + BitcoinWalletManagerListener listener, + Executor listenerExecutor) { + return new BitcoinWalletManager(account, + network, + mode, + earliestKeyTime, + storagePath, + persistenceClient, + backendClient, + listener, + listenerExecutor); + } + }; + + private final NetworkFactory networkFactory = new NetworkFactory() { + @Override + public Network testnet() { + return new Network(new Network.Bitcoin("BTC Testnet", 0x40, BitcoinChainParams.TESTNET)); + } + + @Override + public Network mainnet() { + // TODO: Implement this + return null; + } + }; + @Override public AccountFactory accountFactory() { - return new AccountFactory() { - @Override - public Account create(String phrase) { - return new com.breadwallet.crypto.core.Account(phrase); - } - - @Override - public Account create(byte[] seed) { - return new com.breadwallet.crypto.core.Account(seed); - } - }; + return accountFactory; } @Override public WalletManagerFactory walletManagerFactory() { - return new WalletManagerFactory() { - @Override - public WalletManager createBitcoinWalletManager(Account account, - Network network, - WalletManager.Mode mode, - int earliestKeyTime, - String storagePath, - BitcoinPersistenceClient persistenceClient, - BitcoinBackendClient backendClient, - BitcoinWalletManagerListener listener, - Executor listenerExecutor) { - return new BitcoinWalletManager(account, - network, - mode, - earliestKeyTime, - storagePath, - persistenceClient, - backendClient, - listener, - listenerExecutor); - } - }; + return walletManagerFactory; } @Override public NetworkFactory networkFactory() { - return new NetworkFactory() { - @Override - public Network testnet() { - return new Network(new Network.Bitcoin("BTC Testnet", 0x40, BitcoinChainParams.TESTNET)); - } - - @Override - public Network mainnet() { - // TODO: Implement this - return null; - } - }; + return networkFactory; } } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java index e66db9e64..7996ed087 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinMasterPubKey.java @@ -7,7 +7,7 @@ public final class BitcoinMasterPubKey implements com.breadwallet.crypto.api.bit private final CoreBitcoinMasterPubKey masterPubKey; public BitcoinMasterPubKey(byte[] seed) { - // TODO: Add additional seed validation, if possible + // TODO: Add additional seed validation, if necessary if (null == seed) throw new IllegalArgumentException("Invalid seed"); this.masterPubKey = new CoreBitcoinMasterPubKey(seed); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java index 9eed00c27..fb3767095 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/bitcoin/BitcoinWalletManager.java @@ -25,7 +25,7 @@ import java.util.concurrent.Executor; // TODO: Verify assumption that CoreBitcoinWalletManagerClient callbacks are on single native thread -public final class BitcoinWalletManager implements WalletManager, CoreBitcoinWalletManagerClient { +public final class BitcoinWalletManager extends WalletManager implements CoreBitcoinWalletManagerClient { private final WeakReference listener; private final CoreBitcoinWalletManager coreWalletManager; @@ -54,7 +54,7 @@ public BitcoinWalletManager(Account account, if (null == account) throw new IllegalArgumentException("Invalid seed"); if (null == network) throw new IllegalArgumentException("Invalid network"); if (null == mode) throw new IllegalArgumentException("Invalid mode"); - // TODO: Add validation on earliestKeyTime + if (earliestKeyTime < 0) throw new IllegalArgumentException("Invalid earliest key time"); if (null == storagePath) throw new IllegalArgumentException("Invalid storage path"); if (null == persistenceClient) throw new IllegalArgumentException("Invalid persistence client"); if (null == backendClient) throw new IllegalArgumentException("Invalid backend client"); @@ -139,7 +139,6 @@ public void handleWalletBalanceUpdated(CoreBitcoinWallet coreWallet, long satosh listenerExecutor.execute(() -> { BitcoinWalletManagerListener l = listener.get(); if (l != null) { - // TODO: Get wallet using wid Wallet wallet = new BitcoinWallet(coreWallet, this); Amount amount = new Amount(satoshi, Bitcoin.SATOSHI); l.handleWalletEvent(this, wallet, new BalanceUpdatedWalletEvent(amount)); From a4a6f734bdba43c471fbd86a803131b311e3a1ef Mon Sep 17 00:00:00 2001 From: Michael Carrara Date: Thu, 14 Mar 2019 21:05:13 -0400 Subject: [PATCH 12/12] wip: Add visitor pattern to events --- .../coredemo/CoreDemoBitcoinClient.java | 68 ++++++++++++++++++- .../coredemo/WalletNavigationActivity.java | 1 + .../events/transfer/CreatedTransferEvent.java | 5 ++ .../events/transfer/DeletedTransferEvent.java | 5 ++ .../api/events/transfer/TransferEvent.java | 2 + .../events/transfer/TransferEventVisitor.java | 8 +++ .../wallet/BalanceUpdatedWalletEvent.java | 5 ++ .../api/events/wallet/CreatedWalletEvent.java | 5 ++ .../api/events/wallet/DeletedWalletEvent.java | 5 ++ .../crypto/api/events/wallet/WalletEvent.java | 2 + .../api/events/wallet/WalletEventVisitor.java | 10 +++ .../ChangedWalletManagerEvent.java | 5 ++ .../SyncEndedWalletManagerEvent.java | 5 ++ .../SyncStartedWalletManagerEvent.java | 5 ++ .../walletmanager/WalletManagerEvent.java | 2 + .../WalletManagerEventVisitor.java | 10 +++ .../core/jni/{BIP39.java => Bip39.java} | 0 17 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEventVisitor.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEventVisitor.java create mode 100644 Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEventVisitor.java rename Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/{BIP39.java => Bip39.java} (100%) diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java index 2116ff531..0c64bad9b 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/CoreDemoBitcoinClient.java @@ -11,9 +11,20 @@ import com.breadwallet.crypto.api.bitcoin.BitcoinBackendClient; import com.breadwallet.crypto.api.bitcoin.BitcoinPersistenceClient; import com.breadwallet.crypto.api.bitcoin.BitcoinWalletManagerListener; +import com.breadwallet.crypto.api.events.transfer.CreatedTransferEvent; +import com.breadwallet.crypto.api.events.transfer.DeletedTransferEvent; import com.breadwallet.crypto.api.events.transfer.TransferEvent; +import com.breadwallet.crypto.api.events.transfer.TransferEventVisitor; +import com.breadwallet.crypto.api.events.wallet.BalanceUpdatedWalletEvent; +import com.breadwallet.crypto.api.events.wallet.CreatedWalletEvent; +import com.breadwallet.crypto.api.events.wallet.DeletedWalletEvent; import com.breadwallet.crypto.api.events.wallet.WalletEvent; +import com.breadwallet.crypto.api.events.wallet.WalletEventVisitor; +import com.breadwallet.crypto.api.events.walletmanager.ChangedWalletManagerEvent; +import com.breadwallet.crypto.api.events.walletmanager.SyncEndedWalletManagerEvent; +import com.breadwallet.crypto.api.events.walletmanager.SyncStartedWalletManagerEvent; import com.breadwallet.crypto.api.events.walletmanager.WalletManagerEvent; +import com.breadwallet.crypto.api.events.walletmanager.WalletManagerEventVisitor; import java.util.Map; import java.util.concurrent.Executors; @@ -50,17 +61,68 @@ public void disconnect() { @Override public void handleManagerEvent(WalletManager manager, WalletManagerEvent event) { - Log.d(TAG, "handleManagerEvent: " + event.toString()); + // toy example of the visitor pattern; generic can be used to extract information + event.accept(new WalletManagerEventVisitor() { + @Override + public Void visit(ChangedWalletManagerEvent event) { + Log.d(TAG, "handleManagerEvent: " + event.toString()); + return null; + } + + @Override + public Void visit(SyncEndedWalletManagerEvent event) { + Log.d(TAG, "handleManagerEvent: " + event.toString()); + return null; + } + + @Override + public Void visit(SyncStartedWalletManagerEvent event) { + Log.d(TAG, "handleManagerEvent: " + event.toString()); + return null; + } + }); } @Override public void handleTransferEvent(WalletManager manager, Wallet wallet, Transfer transfer, TransferEvent event) { - Log.d(TAG, "handleTransferEvent: " + event.toString()); + // toy example of the visitor pattern; generic can be used to extract information + event.accept(new TransferEventVisitor() { + @Override + public Void visit(CreatedTransferEvent event) { + Log.d(TAG, "handleTransferEvent: " + event.toString()); + return null; + } + + @Override + public Void visit(DeletedTransferEvent event) { + Log.d(TAG, "handleTransferEvent: " + event.toString()); + return null; + } + }); } @Override public void handleWalletEvent(WalletManager manager, Wallet wallet, WalletEvent event) { - Log.d(TAG, "handleWalletEvent: " + event.toString()); + // toy example of the visitor pattern; generic can be used to extract information + event.accept(new WalletEventVisitor() { + @Override + public Void visit(BalanceUpdatedWalletEvent event) { + Log.d(TAG, "handleWalletEvent: " + event.toString()); + return null; + } + + @Override + public Void visit(CreatedWalletEvent event) { + Log.d(TAG, "handleWalletEvent: " + event.toString()); + return null; + } + + @Override + public Void visit(DeletedWalletEvent event) { + Log.d(TAG, "handleWalletEvent: " + event.toString()); + return null; + } + }); } // BitcoinBackendClient diff --git a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java index 271e79ad7..79fc227b0 100644 --- a/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java +++ b/Java/CoreDemo/src/main/java/com/breadwallet/coredemo/WalletNavigationActivity.java @@ -3,6 +3,7 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import com.breadwallet.core.ethereum.BREthereumNetwork; import com.breadwallet.crypto.api.CryptoApi; import com.breadwallet.crypto.api.Bitcoin; import com.breadwallet.crypto.core.CoreCryptoApi; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/CreatedTransferEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/CreatedTransferEvent.java index 78ed14a0b..8f179ac5b 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/CreatedTransferEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/CreatedTransferEvent.java @@ -2,6 +2,11 @@ public class CreatedTransferEvent implements TransferEvent { + @Override + public T accept(TransferEventVisitor visitor) { + return visitor.visit(this); + } + @Override public String toString() { return "CreatedTransferEvent"; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/DeletedTransferEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/DeletedTransferEvent.java index 72f5e86ac..a0d76fb9b 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/DeletedTransferEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/DeletedTransferEvent.java @@ -2,6 +2,11 @@ public class DeletedTransferEvent implements TransferEvent { + @Override + public T accept(TransferEventVisitor visitor) { + return visitor.visit(this); + } + @Override public String toString() { return "DeletedTransferEvent"; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java index d3a019bea..4e3e30061 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEvent.java @@ -2,4 +2,6 @@ // TODO: Add changed event public interface TransferEvent { + + T accept(TransferEventVisitor visitor); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEventVisitor.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEventVisitor.java new file mode 100644 index 000000000..51f85a8ab --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/transfer/TransferEventVisitor.java @@ -0,0 +1,8 @@ +package com.breadwallet.crypto.api.events.transfer; + +public interface TransferEventVisitor { + + T visit(CreatedTransferEvent event); + + T visit(DeletedTransferEvent event); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java index db105bd06..7a7da3950 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/BalanceUpdatedWalletEvent.java @@ -14,6 +14,11 @@ public Amount amount() { return amount; } + @Override + public T accept(WalletEventVisitor visitor) { + return visitor.visit(this); + } + @Override public String toString() { return String.format("BalanceUpdatedWalletEvent(amount = %s)", amount); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java index 574b6699d..a6c1fd8ef 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/CreatedWalletEvent.java @@ -2,6 +2,11 @@ public class CreatedWalletEvent implements WalletEvent { + @Override + public T accept(WalletEventVisitor visitor) { + return visitor.visit(this); + } + @Override public String toString() { return "CreatedWalletEvent"; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java index 239dbc34b..2a67dd823 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/DeletedWalletEvent.java @@ -2,6 +2,11 @@ public class DeletedWalletEvent implements WalletEvent { + @Override + public T accept(WalletEventVisitor visitor) { + return visitor.visit(this); + } + @Override public String toString() { return "DeletedWalletEvent"; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java index d06811d20..5ad6de0f1 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEvent.java @@ -2,4 +2,6 @@ // TODO: Add transferAdded, transferChanged, transferDeleted & feeBasisUpdated events public interface WalletEvent { + + T accept(WalletEventVisitor visitor); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEventVisitor.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEventVisitor.java new file mode 100644 index 000000000..6527dfafa --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/wallet/WalletEventVisitor.java @@ -0,0 +1,10 @@ +package com.breadwallet.crypto.api.events.wallet; + +public interface WalletEventVisitor { + + T visit(BalanceUpdatedWalletEvent event); + + T visit(CreatedWalletEvent event); + + T visit(DeletedWalletEvent event); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ChangedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ChangedWalletManagerEvent.java index 894c66953..d4dfebcc6 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ChangedWalletManagerEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/ChangedWalletManagerEvent.java @@ -20,6 +20,11 @@ public State newState() { return newState; } + @Override + public T accept(WalletManagerEventVisitor visitor) { + return visitor.visit(this); + } + @Override public String toString() { return String.format("ChangedWalletManagerEvent(old: %s, new: %s)", oldState, newState); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncEndedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncEndedWalletManagerEvent.java index 3b3d559be..a3726e3b2 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncEndedWalletManagerEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncEndedWalletManagerEvent.java @@ -8,6 +8,11 @@ public SyncEndedWalletManagerEvent(String error) { this.error = error; } + @Override + public T accept(WalletManagerEventVisitor visitor) { + return visitor.visit(this); + } + @Override public String toString() { return String.format("SyncEndedWalletManagerEvent(err: %s)", error); diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java index 243d24e6c..8e8a9acff 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/SyncStartedWalletManagerEvent.java @@ -2,6 +2,11 @@ public class SyncStartedWalletManagerEvent implements WalletManagerEvent { + @Override + public T accept(WalletManagerEventVisitor visitor) { + return visitor.visit(this); + } + @Override public String toString() { return "SyncStartedWalletManagerEvent"; diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java index 08d8bb04b..98d571403 100644 --- a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEvent.java @@ -2,4 +2,6 @@ // TODO: Add walletAdded, walletChanged, walletDeleted, syncStarted, syncProgress & syncEnded events public interface WalletManagerEvent { + + T accept(WalletManagerEventVisitor visitor); } diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEventVisitor.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEventVisitor.java new file mode 100644 index 000000000..acb6666d6 --- /dev/null +++ b/Java/Crypto/src/main/java/com/breadwallet/crypto/api/events/walletmanager/WalletManagerEventVisitor.java @@ -0,0 +1,10 @@ +package com.breadwallet.crypto.api.events.walletmanager; + +public interface WalletManagerEventVisitor { + + T visit(ChangedWalletManagerEvent event); + + T visit(SyncEndedWalletManagerEvent event); + + T visit(SyncStartedWalletManagerEvent event); +} diff --git a/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java b/Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/Bip39.java similarity index 100% rename from Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/BIP39.java rename to Java/Crypto/src/main/java/com/breadwallet/crypto/core/jni/Bip39.java