Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
1 change: 1 addition & 0 deletions Java/CoreDemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.breadwallet.coredemo;

import android.util.Log;

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.WalletManager.Mode;
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;

public class CoreDemoBitcoinClient
implements BitcoinWalletManagerListener, BitcoinBackendClient, BitcoinPersistenceClient {

private static final String TAG = "CoreDemoBitcoinClient";

private final WalletManager walletManager;

public CoreDemoBitcoinClient(Network network, String storagePath, String paperKey) {
this.walletManager = WalletManager.createBitcoinWalletManager(
Account.create(paperKey),
network,
Mode.API_WITH_P2P_SUBMIT,
1543190400,
storagePath,
this,
this,
this,
Executors.newSingleThreadExecutor());
}

public void connect() {
walletManager.connect();
}

public void disconnect() {
walletManager.disconnect();
}

// BitcoinWalletManagerListener

@Override
public void handleManagerEvent(WalletManager manager, WalletManagerEvent event) {
// toy example of the visitor pattern; generic can be used to extract information
event.accept(new WalletManagerEventVisitor<Void>() {
@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) {
// toy example of the visitor pattern; generic can be used to extract information
event.accept(new TransferEventVisitor<Void>() {
@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) {
// toy example of the visitor pattern; generic can be used to extract information
event.accept(new WalletEventVisitor<Void>() {
@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

@Override
public boolean isReachable() {
return true;
}

// BitcoinPersistenceClient

@Override
public void savePeers(WalletManager walletManager, Map<String, String> data) {

}

@Override
public void saveBlocks(WalletManager walletManager, Map<String, String> data) {

}

@Override
public void changeTransaction(WalletManager walletManager, ChangeType type, String hash, String data) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
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;

import java.io.File;

public class WalletNavigationActivity extends AppCompatActivity {
static { System.loadLibrary("core"); }

static CoreDemoEthereumClient client = null;
static { CryptoApi.init(new CoreCryptoApi()); }

CoreDemoEthereumClient ethClient = null;

private void deleteRecursively (File file) {
if (file.isDirectory())
Expand All @@ -24,23 +28,47 @@ 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();

CoreDemoBitcoinClient btcClient = new CoreDemoBitcoinClient(
Bitcoin.TESTNET,
storageFile.getAbsolutePath(),
"0xa9de3dbd7d561e67527bc1ecb025c59d53b9f7ef");

btcClient.connect();
btcClient.disconnect();

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);
Expand Down
140 changes: 140 additions & 0 deletions Java/Crypto.Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
DIR=$(shell pwd)

#
# Java
#

JAVA_DIR=${JAVA_HOME}
JAVA_LIB=

# 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)

# Crypto Api

JAVA_API_SDIR=Crypto/src/main/java/com/breadwallet/crypto/api
JAVA_API_SRCS=\
$(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/ChangedWalletManagerEvent.java \
$(JAVA_API_SDIR)/events/walletmanager/SyncStartedWalletManagerEvent.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)/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_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/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 \
$(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)

# Ethereum

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_API_SRCS) $(JAVA_CORE_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)
@(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

clean:
rm -rf build $(JNI_OBJS) $(CORE_OBJS) $(JAVA_API_OBJS) $(JAVA_CORE_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.
Loading