mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-15 03:35:52 +00:00
Split wallet events into single method interfaces
This commit is contained in:
parent
8841371922
commit
aca39ee9de
@ -43,6 +43,7 @@ import java.util.concurrent.*;
|
|||||||
import java.util.concurrent.locks.*;
|
import java.util.concurrent.locks.*;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.*;
|
import static com.google.common.base.Preconditions.*;
|
||||||
|
import org.bitcoinj.wallet.KeyChainEventListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Runs a set of connections to the P2P network, brings up connections to replace disconnected nodes and manages
|
* <p>Runs a set of connections to the P2P network, brings up connections to replace disconnected nodes and manages
|
||||||
@ -166,15 +167,19 @@ public class PeerGroup implements TransactionBroadcaster {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private int minBroadcastConnections = 0;
|
private int minBroadcastConnections = 0;
|
||||||
private final WalletEventListener walletEventListener = new AbstractWalletEventListener() {
|
private final ScriptsChangeEventListener walletScriptEventListener = new ScriptsChangeEventListener() {
|
||||||
@Override public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
|
@Override public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
|
||||||
recalculateFastCatchupAndFilter(FilterRecalculateMode.SEND_IF_CHANGED);
|
recalculateFastCatchupAndFilter(FilterRecalculateMode.SEND_IF_CHANGED);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final KeyChainEventListener walletKeyEventListener = new KeyChainEventListener() {
|
||||||
@Override public void onKeysAdded(List<ECKey> keys) {
|
@Override public void onKeysAdded(List<ECKey> keys) {
|
||||||
recalculateFastCatchupAndFilter(FilterRecalculateMode.SEND_IF_CHANGED);
|
recalculateFastCatchupAndFilter(FilterRecalculateMode.SEND_IF_CHANGED);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final WalletCoinsReceivedEventListener walletCoinsReceivedEventListener = new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
// We received a relevant transaction. We MAY need to recalculate and resend the Bloom filter, but only
|
// We received a relevant transaction. We MAY need to recalculate and resend the Bloom filter, but only
|
||||||
@ -1084,7 +1089,9 @@ public class PeerGroup implements TransactionBroadcaster {
|
|||||||
checkState(!wallets.contains(wallet));
|
checkState(!wallets.contains(wallet));
|
||||||
wallets.add(wallet);
|
wallets.add(wallet);
|
||||||
wallet.setTransactionBroadcaster(this);
|
wallet.setTransactionBroadcaster(this);
|
||||||
wallet.addEventListener(walletEventListener, Threading.SAME_THREAD);
|
wallet.addCoinsReceivedEventListener(Threading.SAME_THREAD, walletCoinsReceivedEventListener);
|
||||||
|
wallet.addKeyChainEventListener(Threading.SAME_THREAD, walletKeyEventListener);
|
||||||
|
wallet.addScriptChangeEventListener(Threading.SAME_THREAD, walletScriptEventListener);
|
||||||
addPeerFilterProvider(wallet);
|
addPeerFilterProvider(wallet);
|
||||||
for (Peer peer : peers) {
|
for (Peer peer : peers) {
|
||||||
peer.addWallet(wallet);
|
peer.addWallet(wallet);
|
||||||
@ -1154,7 +1161,9 @@ public class PeerGroup implements TransactionBroadcaster {
|
|||||||
public void removeWallet(Wallet wallet) {
|
public void removeWallet(Wallet wallet) {
|
||||||
wallets.remove(checkNotNull(wallet));
|
wallets.remove(checkNotNull(wallet));
|
||||||
peerFilterProviders.remove(wallet);
|
peerFilterProviders.remove(wallet);
|
||||||
wallet.removeEventListener(walletEventListener);
|
wallet.removeCoinsReceivedEventListener(walletCoinsReceivedEventListener);
|
||||||
|
wallet.removeKeyChainEventListener(walletKeyEventListener);
|
||||||
|
wallet.removeScriptChangeEventListener(walletScriptEventListener);
|
||||||
wallet.setTransactionBroadcaster(null);
|
wallet.setTransactionBroadcaster(null);
|
||||||
for (Peer peer : peers) {
|
for (Peer peer : peers) {
|
||||||
peer.removeWallet(wallet);
|
peer.removeWallet(wallet);
|
||||||
|
@ -25,12 +25,7 @@ import com.google.common.util.concurrent.*;
|
|||||||
import com.google.protobuf.*;
|
import com.google.protobuf.*;
|
||||||
import net.jcip.annotations.*;
|
import net.jcip.annotations.*;
|
||||||
import org.bitcoin.protocols.payments.Protos.*;
|
import org.bitcoin.protocols.payments.Protos.*;
|
||||||
import org.bitcoinj.core.listeners.NewBestBlockListener;
|
import org.bitcoinj.core.listeners.*;
|
||||||
import org.bitcoinj.core.listeners.ReorganizeListener;
|
|
||||||
import org.bitcoinj.core.listeners.TransactionReceivedInBlockListener;
|
|
||||||
import org.bitcoinj.core.listeners.WalletEventListener;
|
|
||||||
import org.bitcoinj.core.listeners.WalletChangeEventListener;
|
|
||||||
import org.bitcoinj.core.listeners.WalletCoinEventListener;
|
|
||||||
import org.bitcoinj.core.TransactionConfidence.*;
|
import org.bitcoinj.core.TransactionConfidence.*;
|
||||||
import org.bitcoinj.crypto.*;
|
import org.bitcoinj.crypto.*;
|
||||||
import org.bitcoinj.script.*;
|
import org.bitcoinj.script.*;
|
||||||
@ -160,8 +155,18 @@ public class Wallet extends BaseTaggableObject
|
|||||||
private int lastBlockSeenHeight;
|
private int lastBlockSeenHeight;
|
||||||
private long lastBlockSeenTimeSecs;
|
private long lastBlockSeenTimeSecs;
|
||||||
|
|
||||||
private CopyOnWriteArrayList<ListenerRegistration<WalletChangeEventListener>> changeListeners;
|
private final CopyOnWriteArrayList<ListenerRegistration<WalletChangeEventListener>> changeListeners
|
||||||
private CopyOnWriteArrayList<ListenerRegistration<WalletCoinEventListener>> coinListeners;
|
= new CopyOnWriteArrayList<ListenerRegistration<WalletChangeEventListener>>();
|
||||||
|
private final CopyOnWriteArrayList<ListenerRegistration<WalletCoinsReceivedEventListener>> coinsReceivedListeners
|
||||||
|
= new CopyOnWriteArrayList<ListenerRegistration<WalletCoinsReceivedEventListener>>();
|
||||||
|
private final CopyOnWriteArrayList<ListenerRegistration<WalletCoinsSentEventListener>> coinsSentListeners
|
||||||
|
= new CopyOnWriteArrayList<ListenerRegistration<WalletCoinsSentEventListener>>();
|
||||||
|
private final CopyOnWriteArrayList<ListenerRegistration<WalletReorganizeEventListener>> reorganizeListeners
|
||||||
|
= new CopyOnWriteArrayList<ListenerRegistration<WalletReorganizeEventListener>>();
|
||||||
|
private final CopyOnWriteArrayList<ListenerRegistration<ScriptsChangeEventListener>> scriptChangeListeners
|
||||||
|
= new CopyOnWriteArrayList<ListenerRegistration<ScriptsChangeEventListener>>();
|
||||||
|
private final CopyOnWriteArrayList<ListenerRegistration<TransactionConfidenceEventListener>> transactionConfidenceListeners
|
||||||
|
= new CopyOnWriteArrayList<ListenerRegistration<TransactionConfidenceEventListener>>();
|
||||||
|
|
||||||
// A listener that relays confidence changes from the transaction confidence object to the wallet event listener,
|
// A listener that relays confidence changes from the transaction confidence object to the wallet event listener,
|
||||||
// as a convenience to API users so they don't have to register on every transaction themselves.
|
// as a convenience to API users so they don't have to register on every transaction themselves.
|
||||||
@ -281,8 +286,6 @@ public class Wallet extends BaseTaggableObject
|
|||||||
pending = new HashMap<Sha256Hash, Transaction>();
|
pending = new HashMap<Sha256Hash, Transaction>();
|
||||||
dead = new HashMap<Sha256Hash, Transaction>();
|
dead = new HashMap<Sha256Hash, Transaction>();
|
||||||
transactions = new HashMap<Sha256Hash, Transaction>();
|
transactions = new HashMap<Sha256Hash, Transaction>();
|
||||||
changeListeners = new CopyOnWriteArrayList<ListenerRegistration<WalletChangeEventListener>>();
|
|
||||||
coinListeners = new CopyOnWriteArrayList<ListenerRegistration<WalletCoinEventListener>>();
|
|
||||||
extensions = new HashMap<String, WalletExtension>();
|
extensions = new HashMap<String, WalletExtension>();
|
||||||
// Use a linked hash map to ensure ordering of event listeners is correct.
|
// Use a linked hash map to ensure ordering of event listeners is correct.
|
||||||
confidenceChanged = new LinkedHashMap<Transaction, TransactionConfidence.Listener.ChangeReason>();
|
confidenceChanged = new LinkedHashMap<Transaction, TransactionConfidence.Listener.ChangeReason>();
|
||||||
@ -2435,7 +2438,24 @@ public class Wallet extends BaseTaggableObject
|
|||||||
*/
|
*/
|
||||||
public void addEventListener(WalletEventListener listener) {
|
public void addEventListener(WalletEventListener listener) {
|
||||||
addChangeEventListener(Threading.USER_THREAD, listener);
|
addChangeEventListener(Threading.USER_THREAD, listener);
|
||||||
addCoinEventListener(Threading.USER_THREAD, listener);
|
addCoinsReceivedEventListener(Threading.USER_THREAD, listener);
|
||||||
|
addCoinsSentEventListener(Threading.USER_THREAD, listener);
|
||||||
|
addKeyChainEventListener(Threading.USER_THREAD, listener);
|
||||||
|
addReorganizeEventListener(Threading.USER_THREAD, listener);
|
||||||
|
addScriptChangeEventListener(Threading.USER_THREAD, listener);
|
||||||
|
addTransactionConfidenceEventListener(Threading.USER_THREAD, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Use the more specific listener methods instead */
|
||||||
|
@Deprecated
|
||||||
|
public void addEventListener(WalletEventListener listener, Executor executor) {
|
||||||
|
addCoinsReceivedEventListener(executor, listener);
|
||||||
|
addCoinsSentEventListener(executor, listener);
|
||||||
|
addChangeEventListener(executor, listener);
|
||||||
|
addKeyChainEventListener(executor, listener);
|
||||||
|
addReorganizeEventListener(executor, listener);
|
||||||
|
addScriptChangeEventListener(executor, listener);
|
||||||
|
addTransactionConfidenceEventListener(executor, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2446,14 +2466,6 @@ public class Wallet extends BaseTaggableObject
|
|||||||
addChangeEventListener(Threading.USER_THREAD, listener);
|
addChangeEventListener(Threading.USER_THREAD, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds an event listener object. Methods on this object are called when something interesting happens,
|
|
||||||
* like receiving money. Runs the listener methods in the user thread.
|
|
||||||
*/
|
|
||||||
public void addCoinEventListener(WalletCoinEventListener listener) {
|
|
||||||
addCoinEventListener(Threading.USER_THREAD, listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an event listener object. Methods on this object are called when something interesting happens,
|
* Adds an event listener object. Methods on this object are called when something interesting happens,
|
||||||
* like receiving money. The listener is executed by the given executor.
|
* like receiving money. The listener is executed by the given executor.
|
||||||
@ -2461,32 +2473,122 @@ public class Wallet extends BaseTaggableObject
|
|||||||
public void addChangeEventListener(Executor executor, WalletChangeEventListener listener) {
|
public void addChangeEventListener(Executor executor, WalletChangeEventListener listener) {
|
||||||
// This is thread safe, so we don't need to take the lock.
|
// This is thread safe, so we don't need to take the lock.
|
||||||
changeListeners.add(new ListenerRegistration<WalletChangeEventListener>(listener, executor));
|
changeListeners.add(new ListenerRegistration<WalletChangeEventListener>(listener, executor));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener object called when coins are received.
|
||||||
|
* Runs the listener methods in the user thread.
|
||||||
|
*/
|
||||||
|
public void addCoinsReceivedEventListener(WalletCoinsReceivedEventListener listener) {
|
||||||
|
addCoinsReceivedEventListener(Threading.USER_THREAD, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener object called when coins are received.
|
||||||
|
* The listener is executed by the given executor.
|
||||||
|
*/
|
||||||
|
public void addCoinsReceivedEventListener(Executor executor, WalletCoinsReceivedEventListener listener) {
|
||||||
|
// This is thread safe, so we don't need to take the lock.
|
||||||
|
coinsReceivedListeners.add(new ListenerRegistration<WalletCoinsReceivedEventListener>(listener, executor));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener object called when coins are sent.
|
||||||
|
* Runs the listener methods in the user thread.
|
||||||
|
*/
|
||||||
|
public void addCoinsSentEventListener(WalletCoinsSentEventListener listener) {
|
||||||
|
addCoinsSentEventListener(Threading.USER_THREAD, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener object called when coins are sent.
|
||||||
|
* The listener is executed by the given executor.
|
||||||
|
*/
|
||||||
|
public void addCoinsSentEventListener(Executor executor, WalletCoinsSentEventListener listener) {
|
||||||
|
// This is thread safe, so we don't need to take the lock.
|
||||||
|
coinsSentListeners.add(new ListenerRegistration<WalletCoinsSentEventListener>(listener, executor));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener object. Methods on this object are called when keys are
|
||||||
|
* added. The listener is executed in the user thread.
|
||||||
|
*/
|
||||||
|
public void addKeyChainEventListener(KeyChainEventListener listener) {
|
||||||
|
keyChainGroup.addEventListener(listener, Threading.USER_THREAD);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener object. Methods on this object are called when keys are
|
||||||
|
* added. The listener is executed by the given executor.
|
||||||
|
*/
|
||||||
|
public void addKeyChainEventListener(Executor executor, KeyChainEventListener listener) {
|
||||||
keyChainGroup.addEventListener(listener, executor);
|
keyChainGroup.addEventListener(listener, executor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener object. Methods on this object are called when something interesting happens,
|
||||||
|
* like receiving money. Runs the listener methods in the user thread.
|
||||||
|
*/
|
||||||
|
public void addReorganizeEventListener(WalletReorganizeEventListener listener) {
|
||||||
|
addReorganizeEventListener(Threading.USER_THREAD, listener);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an event listener object. Methods on this object are called when something interesting happens,
|
* Adds an event listener object. Methods on this object are called when something interesting happens,
|
||||||
* like receiving money. The listener is executed by the given executor.
|
* like receiving money. The listener is executed by the given executor.
|
||||||
*/
|
*/
|
||||||
public void addCoinEventListener(Executor executor, WalletCoinEventListener listener) {
|
public void addReorganizeEventListener(Executor executor, WalletReorganizeEventListener listener) {
|
||||||
// This is thread safe, so we don't need to take the lock.
|
// This is thread safe, so we don't need to take the lock.
|
||||||
coinListeners.add(new ListenerRegistration<WalletCoinEventListener>(listener, executor));
|
reorganizeListeners.add(new ListenerRegistration<WalletReorganizeEventListener>(listener, executor));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Use the more specific listener methods instead */
|
/**
|
||||||
@Deprecated
|
* Adds an event listener object. Methods on this object are called when scripts
|
||||||
public void addEventListener(WalletEventListener listener, Executor executor) {
|
* watched by this wallet change. Runs the listener methods in the user thread.
|
||||||
addCoinEventListener(executor, listener);
|
*/
|
||||||
addChangeEventListener(executor, listener);
|
public void addScriptsChangeEventListener(ScriptsChangeEventListener listener) {
|
||||||
|
addScriptChangeEventListener(Threading.USER_THREAD, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener object. Methods on this object are called when scripts
|
||||||
|
* watched by this wallet change. The listener is executed by the given executor.
|
||||||
|
*/
|
||||||
|
public void addScriptChangeEventListener(Executor executor, ScriptsChangeEventListener listener) {
|
||||||
|
// This is thread safe, so we don't need to take the lock.
|
||||||
|
scriptChangeListeners.add(new ListenerRegistration<ScriptsChangeEventListener>(listener, executor));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener object. Methods on this object are called when confidence
|
||||||
|
* of a transaction changes. Runs the listener methods in the user thread.
|
||||||
|
*/
|
||||||
|
public void addTransactionConfidenceEventListener(TransactionConfidenceEventListener listener) {
|
||||||
|
addTransactionConfidenceEventListener(Threading.USER_THREAD, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener object. Methods on this object are called when confidence
|
||||||
|
* of a transaction changes. The listener is executed by the given executor.
|
||||||
|
*/
|
||||||
|
public void addTransactionConfidenceEventListener(Executor executor, TransactionConfidenceEventListener listener) {
|
||||||
|
// This is thread safe, so we don't need to take the lock.
|
||||||
|
transactionConfidenceListeners.add(new ListenerRegistration<TransactionConfidenceEventListener>(listener, executor));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
|
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
|
||||||
* was never added.
|
* was never added.
|
||||||
|
* @deprecated use the fine-grain event listeners instead.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public boolean removeEventListener(WalletEventListener listener) {
|
public boolean removeEventListener(WalletEventListener listener) {
|
||||||
return removeChangeEventListener(listener) ||
|
return removeChangeEventListener(listener) ||
|
||||||
removeCoinEventListener(listener);
|
removeCoinsReceivedEventListener(listener) ||
|
||||||
|
removeCoinsSentEventListener(listener) ||
|
||||||
|
removeKeyChainEventListener(listener) ||
|
||||||
|
removeReorganizeEventListener(listener) ||
|
||||||
|
removeTransactionConfidenceEventListener(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2494,7 +2596,6 @@ public class Wallet extends BaseTaggableObject
|
|||||||
* was never added.
|
* was never added.
|
||||||
*/
|
*/
|
||||||
public boolean removeChangeEventListener(WalletChangeEventListener listener) {
|
public boolean removeChangeEventListener(WalletChangeEventListener listener) {
|
||||||
keyChainGroup.removeEventListener(listener);
|
|
||||||
return ListenerRegistration.removeFromList(listener, changeListeners);
|
return ListenerRegistration.removeFromList(listener, changeListeners);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2502,13 +2603,53 @@ public class Wallet extends BaseTaggableObject
|
|||||||
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
|
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
|
||||||
* was never added.
|
* was never added.
|
||||||
*/
|
*/
|
||||||
public boolean removeCoinEventListener(WalletCoinEventListener listener) {
|
public boolean removeCoinsReceivedEventListener(WalletCoinsReceivedEventListener listener) {
|
||||||
return ListenerRegistration.removeFromList(listener, coinListeners);
|
return ListenerRegistration.removeFromList(listener, coinsReceivedListeners);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
|
||||||
|
* was never added.
|
||||||
|
*/
|
||||||
|
public boolean removeCoinsSentEventListener(WalletCoinsSentEventListener listener) {
|
||||||
|
return ListenerRegistration.removeFromList(listener, coinsSentListeners);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
|
||||||
|
* was never added.
|
||||||
|
*/
|
||||||
|
public boolean removeKeyChainEventListener(KeyChainEventListener listener) {
|
||||||
|
return keyChainGroup.removeEventListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
|
||||||
|
* was never added.
|
||||||
|
*/
|
||||||
|
public boolean removeReorganizeEventListener(WalletReorganizeEventListener listener) {
|
||||||
|
return ListenerRegistration.removeFromList(listener, reorganizeListeners);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
|
||||||
|
* was never added.
|
||||||
|
*/
|
||||||
|
public boolean removeScriptChangeEventListener(ScriptsChangeEventListener listener) {
|
||||||
|
return ListenerRegistration.removeFromList(listener, scriptChangeListeners);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the given event listener object. Returns true if the listener was removed, false if that listener
|
||||||
|
* was never added.
|
||||||
|
*/
|
||||||
|
public boolean removeTransactionConfidenceEventListener(TransactionConfidenceEventListener listener) {
|
||||||
|
return ListenerRegistration.removeFromList(listener, transactionConfidenceListeners);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void queueOnTransactionConfidenceChanged(final Transaction tx) {
|
private void queueOnTransactionConfidenceChanged(final Transaction tx) {
|
||||||
checkState(lock.isHeldByCurrentThread());
|
checkState(lock.isHeldByCurrentThread());
|
||||||
for (final ListenerRegistration<WalletChangeEventListener> registration : changeListeners) {
|
for (final ListenerRegistration<TransactionConfidenceEventListener> registration : transactionConfidenceListeners) {
|
||||||
if (registration.executor == Threading.SAME_THREAD) {
|
if (registration.executor == Threading.SAME_THREAD) {
|
||||||
registration.listener.onTransactionConfidenceChanged(this, tx);
|
registration.listener.onTransactionConfidenceChanged(this, tx);
|
||||||
} else {
|
} else {
|
||||||
@ -2540,7 +2681,7 @@ public class Wallet extends BaseTaggableObject
|
|||||||
|
|
||||||
protected void queueOnCoinsReceived(final Transaction tx, final Coin balance, final Coin newBalance) {
|
protected void queueOnCoinsReceived(final Transaction tx, final Coin balance, final Coin newBalance) {
|
||||||
checkState(lock.isHeldByCurrentThread());
|
checkState(lock.isHeldByCurrentThread());
|
||||||
for (final ListenerRegistration<WalletCoinEventListener> registration : coinListeners) {
|
for (final ListenerRegistration<WalletCoinsReceivedEventListener> registration : coinsReceivedListeners) {
|
||||||
registration.executor.execute(new Runnable() {
|
registration.executor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -2552,7 +2693,7 @@ public class Wallet extends BaseTaggableObject
|
|||||||
|
|
||||||
protected void queueOnCoinsSent(final Transaction tx, final Coin prevBalance, final Coin newBalance) {
|
protected void queueOnCoinsSent(final Transaction tx, final Coin prevBalance, final Coin newBalance) {
|
||||||
checkState(lock.isHeldByCurrentThread());
|
checkState(lock.isHeldByCurrentThread());
|
||||||
for (final ListenerRegistration<WalletCoinEventListener> registration : coinListeners) {
|
for (final ListenerRegistration<WalletCoinsSentEventListener> registration : coinsSentListeners) {
|
||||||
registration.executor.execute(new Runnable() {
|
registration.executor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -2565,7 +2706,7 @@ public class Wallet extends BaseTaggableObject
|
|||||||
protected void queueOnReorganize() {
|
protected void queueOnReorganize() {
|
||||||
checkState(lock.isHeldByCurrentThread());
|
checkState(lock.isHeldByCurrentThread());
|
||||||
checkState(insideReorg);
|
checkState(insideReorg);
|
||||||
for (final ListenerRegistration<WalletChangeEventListener> registration : changeListeners) {
|
for (final ListenerRegistration<WalletReorganizeEventListener> registration : reorganizeListeners) {
|
||||||
registration.executor.execute(new Runnable() {
|
registration.executor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -2576,7 +2717,7 @@ public class Wallet extends BaseTaggableObject
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void queueOnScriptsChanged(final List<Script> scripts, final boolean isAddingScripts) {
|
protected void queueOnScriptsChanged(final List<Script> scripts, final boolean isAddingScripts) {
|
||||||
for (final ListenerRegistration<WalletChangeEventListener> registration : changeListeners) {
|
for (final ListenerRegistration<ScriptsChangeEventListener> registration : scriptChangeListeners) {
|
||||||
registration.executor.execute(new Runnable() {
|
registration.executor.execute(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -65,6 +65,9 @@ public abstract class AbstractWalletEventListener extends AbstractKeyChainEventL
|
|||||||
onChange();
|
onChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default method called on change events.
|
||||||
|
*/
|
||||||
public void onChange() {
|
public void onChange() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.core.listeners;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.Wallet;
|
||||||
|
import org.bitcoinj.script.Script;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Implementors are called when the contents of the wallet changes, for instance due to receiving/sending money
|
||||||
|
* or a block chain re-organize. It may be convenient to derive from {@link AbstractWalletEventListener} instead.</p>
|
||||||
|
*/
|
||||||
|
public interface ScriptsChangeEventListener {
|
||||||
|
/**
|
||||||
|
* Called whenever a new watched script is added to the wallet.
|
||||||
|
*
|
||||||
|
* @param isAddingScripts will be true if added scripts, false if removed scripts.
|
||||||
|
*/
|
||||||
|
void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts);
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.core.listeners;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.Transaction;
|
||||||
|
import org.bitcoinj.core.Wallet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Implementors are called when confidence of a transaction changes.</p>
|
||||||
|
*/
|
||||||
|
public interface TransactionConfidenceEventListener {
|
||||||
|
/**
|
||||||
|
* <p>Called when a transaction changes its confidence level. You can also attach event listeners to
|
||||||
|
* the individual transactions, if you don't care about all of them. Usually you would save the wallet to disk after
|
||||||
|
* receiving this callback unless you already set up autosaving.</p>
|
||||||
|
*
|
||||||
|
* <p>You should pay attention to this callback in case a transaction becomes <i>dead</i>, that is, a transaction
|
||||||
|
* you believed to be active (send or receive) becomes overridden by the network. This can happen if</p>
|
||||||
|
*
|
||||||
|
* <ol>
|
||||||
|
* <li>You are sharing keys between wallets and accidentally create/broadcast a double spend.</li>
|
||||||
|
* <li>Somebody is attacking the network and reversing transactions, ie, the user is a victim of fraud.</li>
|
||||||
|
* <li>A bug: for example you create a transaction, broadcast it but fail to commit it. The {@link Wallet}
|
||||||
|
* will then re-use the same outputs when creating the next spend.</li>
|
||||||
|
* </ol><p>
|
||||||
|
*
|
||||||
|
* <p>To find if the transaction is dead, you can use <tt>tx.getConfidence().getConfidenceType() ==
|
||||||
|
* TransactionConfidence.ConfidenceType.DEAD</tt>. If it is, you should notify the user
|
||||||
|
* in some way so they know the thing they bought may not arrive/the thing they sold should not be dispatched.</p>
|
||||||
|
*
|
||||||
|
* <p>Note that this callback will be invoked for every transaction in the wallet, for every new block that is
|
||||||
|
* received (because the depth has changed). <b>If you want to update a UI view from the contents of the wallet
|
||||||
|
* it is more efficient to use onWalletChanged instead.</b></p>
|
||||||
|
*/
|
||||||
|
void onTransactionConfidenceChanged(Wallet wallet, Transaction tx);
|
||||||
|
}
|
@ -16,56 +16,13 @@
|
|||||||
|
|
||||||
package org.bitcoinj.core.listeners;
|
package org.bitcoinj.core.listeners;
|
||||||
|
|
||||||
import org.bitcoinj.core.Transaction;
|
|
||||||
import org.bitcoinj.core.Wallet;
|
import org.bitcoinj.core.Wallet;
|
||||||
import org.bitcoinj.script.Script;
|
|
||||||
import org.bitcoinj.wallet.KeyChainEventListener;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Implementors are called when the contents of the wallet changes, for instance due to receiving/sending money
|
* <p>Implementors are called when the contents of the wallet changes, for instance due to receiving/sending money
|
||||||
* or a block chain re-organize. It may be convenient to derive from {@link AbstractWalletEventListener} instead.</p>
|
* or a block chain re-organize. It may be convenient to derive from {@link AbstractWalletEventListener} instead.</p>
|
||||||
*/
|
*/
|
||||||
public interface WalletChangeEventListener extends KeyChainEventListener {
|
public interface WalletChangeEventListener {
|
||||||
// TODO: Finish onReorganize to be more useful.
|
|
||||||
/**
|
|
||||||
* <p>This is called when a block is received that triggers a block chain re-organization.</p>
|
|
||||||
*
|
|
||||||
* <p>A re-organize means that the consensus (chain) of the network has diverged and now changed from what we
|
|
||||||
* believed it was previously. Usually this won't matter because the new consensus will include all our old
|
|
||||||
* transactions assuming we are playing by the rules. However it's theoretically possible for our balance to
|
|
||||||
* change in arbitrary ways, most likely, we could lose some money we thought we had.</p>
|
|
||||||
*
|
|
||||||
* <p>It is safe to use methods of wallet whilst inside this callback.</p>
|
|
||||||
*/
|
|
||||||
void onReorganize(Wallet wallet);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Called when a transaction changes its confidence level. You can also attach event listeners to
|
|
||||||
* the individual transactions, if you don't care about all of them. Usually you would save the wallet to disk after
|
|
||||||
* receiving this callback unless you already set up autosaving.</p>
|
|
||||||
*
|
|
||||||
* <p>You should pay attention to this callback in case a transaction becomes <i>dead</i>, that is, a transaction
|
|
||||||
* you believed to be active (send or receive) becomes overridden by the network. This can happen if</p>
|
|
||||||
*
|
|
||||||
* <ol>
|
|
||||||
* <li>You are sharing keys between wallets and accidentally create/broadcast a double spend.</li>
|
|
||||||
* <li>Somebody is attacking the network and reversing transactions, ie, the user is a victim of fraud.</li>
|
|
||||||
* <li>A bug: for example you create a transaction, broadcast it but fail to commit it. The {@link Wallet}
|
|
||||||
* will then re-use the same outputs when creating the next spend.</li>
|
|
||||||
* </ol><p>
|
|
||||||
*
|
|
||||||
* <p>To find if the transaction is dead, you can use <tt>tx.getConfidence().getConfidenceType() ==
|
|
||||||
* TransactionConfidence.ConfidenceType.DEAD</tt>. If it is, you should notify the user
|
|
||||||
* in some way so they know the thing they bought may not arrive/the thing they sold should not be dispatched.</p>
|
|
||||||
*
|
|
||||||
* <p>Note that this callback will be invoked for every transaction in the wallet, for every new block that is
|
|
||||||
* received (because the depth has changed). <b>If you want to update a UI view from the contents of the wallet
|
|
||||||
* it is more efficient to use onWalletChanged instead.</b></p>
|
|
||||||
*/
|
|
||||||
void onTransactionConfidenceChanged(Wallet wallet, Transaction tx);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Designed for GUI applications to refresh their transaction lists. This callback is invoked in the following
|
* <p>Designed for GUI applications to refresh their transaction lists. This callback is invoked in the following
|
||||||
* situations:</p>
|
* situations:</p>
|
||||||
@ -85,11 +42,4 @@ public interface WalletChangeEventListener extends KeyChainEventListener {
|
|||||||
* rather than one per transaction per block. Note that this is <b>not</b> called when a key is added. </p>
|
* rather than one per transaction per block. Note that this is <b>not</b> called when a key is added. </p>
|
||||||
*/
|
*/
|
||||||
void onWalletChanged(Wallet wallet);
|
void onWalletChanged(Wallet wallet);
|
||||||
|
|
||||||
/**
|
|
||||||
* Called whenever a new watched script is added to the wallet.
|
|
||||||
*
|
|
||||||
* @param isAddingScripts will be true if added scripts, false if removed scripts.
|
|
||||||
*/
|
|
||||||
void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts);
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.core.listeners;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.Coin;
|
||||||
|
import org.bitcoinj.core.Transaction;
|
||||||
|
import org.bitcoinj.core.Wallet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Implementors are called when the contents of the wallet changes, for instance due to receiving/sending money
|
||||||
|
* or a block chain re-organize. It may be convenient to derive from {@link AbstractWalletEventListener} instead.</p>
|
||||||
|
*/
|
||||||
|
public interface WalletCoinsReceivedEventListener {
|
||||||
|
/**
|
||||||
|
* This is called when a transaction is seen that sends coins <b>to</b> this wallet, either because it
|
||||||
|
* was broadcast across the network or because a block was received. If a transaction is seen when it was broadcast,
|
||||||
|
* onCoinsReceived won't be called again when a block containing it is received. If you want to know when such a
|
||||||
|
* transaction receives its first confirmation, register a {@link TransactionConfidence} event listener using
|
||||||
|
* the object retrieved via {@link org.bitcoinj.core.Transaction#getConfidence()}. It's safe to modify the
|
||||||
|
* wallet in this callback, for example, by spending the transaction just received.
|
||||||
|
*
|
||||||
|
* @param wallet The wallet object that received the coins
|
||||||
|
* @param tx The transaction which sent us the coins.
|
||||||
|
* @param prevBalance Balance before the coins were received.
|
||||||
|
* @param newBalance Current balance of the wallet. This is the 'estimated' balance.
|
||||||
|
*/
|
||||||
|
void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance);
|
||||||
|
}
|
@ -19,30 +19,12 @@ package org.bitcoinj.core.listeners;
|
|||||||
import org.bitcoinj.core.Coin;
|
import org.bitcoinj.core.Coin;
|
||||||
import org.bitcoinj.core.Transaction;
|
import org.bitcoinj.core.Transaction;
|
||||||
import org.bitcoinj.core.Wallet;
|
import org.bitcoinj.core.Wallet;
|
||||||
import org.bitcoinj.script.Script;
|
|
||||||
import org.bitcoinj.wallet.KeyChainEventListener;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Implementors are called when the contents of the wallet changes, for instance due to receiving/sending money
|
* <p>Implementors are called when the contents of the wallet changes, for instance due to receiving/sending money
|
||||||
* or a block chain re-organize. It may be convenient to derive from {@link AbstractWalletEventListener} instead.</p>
|
* or a block chain re-organize. It may be convenient to derive from {@link AbstractWalletEventListener} instead.</p>
|
||||||
*/
|
*/
|
||||||
public interface WalletCoinEventListener {
|
public interface WalletCoinsSentEventListener {
|
||||||
/**
|
|
||||||
* This is called when a transaction is seen that sends coins <b>to</b> this wallet, either because it
|
|
||||||
* was broadcast across the network or because a block was received. If a transaction is seen when it was broadcast,
|
|
||||||
* onCoinsReceived won't be called again when a block containing it is received. If you want to know when such a
|
|
||||||
* transaction receives its first confirmation, register a {@link TransactionConfidence} event listener using
|
|
||||||
* the object retrieved via {@link org.bitcoinj.core.Transaction#getConfidence()}. It's safe to modify the
|
|
||||||
* wallet in this callback, for example, by spending the transaction just received.
|
|
||||||
*
|
|
||||||
* @param wallet The wallet object that received the coins
|
|
||||||
* @param tx The transaction which sent us the coins.
|
|
||||||
* @param prevBalance Balance before the coins were received.
|
|
||||||
* @param newBalance Current balance of the wallet. This is the 'estimated' balance.
|
|
||||||
*/
|
|
||||||
void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is called when a transaction is seen that sends coins <b>from</b> this wallet, either
|
* This is called when a transaction is seen that sends coins <b>from</b> this wallet, either
|
@ -16,9 +16,16 @@
|
|||||||
|
|
||||||
package org.bitcoinj.core.listeners;
|
package org.bitcoinj.core.listeners;
|
||||||
|
|
||||||
|
import org.bitcoinj.wallet.KeyChainEventListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Common interface for wallet changes and transactions. For fine-grain
|
* <p>Common interface for wallet changes and transactions.</p>
|
||||||
* events please use the super-interfaces.</p>
|
* @deprecated Use the superinterfaces directly instead.
|
||||||
*/
|
*/
|
||||||
public interface WalletEventListener extends WalletChangeEventListener, WalletCoinEventListener {
|
@Deprecated
|
||||||
|
public interface WalletEventListener extends
|
||||||
|
KeyChainEventListener, WalletChangeEventListener,
|
||||||
|
WalletCoinsReceivedEventListener, WalletCoinsSentEventListener,
|
||||||
|
WalletReorganizeEventListener, ScriptsChangeEventListener,
|
||||||
|
TransactionConfidenceEventListener {
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.core.listeners;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.Wallet;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Implementors are called when the wallet is reorganized.</p>
|
||||||
|
*/
|
||||||
|
public interface WalletReorganizeEventListener {
|
||||||
|
// TODO: Finish onReorganize to be more useful.
|
||||||
|
/**
|
||||||
|
* <p>This is called when a block is received that triggers a block chain re-organization.</p>
|
||||||
|
*
|
||||||
|
* <p>A re-organize means that the consensus (chain) of the network has diverged and now changed from what we
|
||||||
|
* believed it was previously. Usually this won't matter because the new consensus will include all our old
|
||||||
|
* transactions assuming we are playing by the rules. However it's theoretically possible for our balance to
|
||||||
|
* change in arbitrary ways, most likely, we could lose some money we thought we had.</p>
|
||||||
|
*
|
||||||
|
* <p>It is safe to use methods of wallet whilst inside this callback.</p>
|
||||||
|
*/
|
||||||
|
void onReorganize(Wallet wallet);
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.jni;
|
||||||
|
|
||||||
|
import org.bitcoinj.wallet.KeyChainEventListener;
|
||||||
|
import org.bitcoinj.core.ECKey;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event listener that relays events to a native C++ object. A pointer to that object is stored in
|
||||||
|
* this class using JNI on the native side, thus several instances of this can point to different actual
|
||||||
|
* native implementations.
|
||||||
|
*/
|
||||||
|
public class NativeKeyChainEventListener implements KeyChainEventListener {
|
||||||
|
public long ptr;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public native void onKeysAdded(List<ECKey> keys);
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.jni;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.listeners.ScriptsChangeEventListener;
|
||||||
|
import org.bitcoinj.core.Wallet;
|
||||||
|
import org.bitcoinj.script.Script;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event listener that relays events to a native C++ object. A pointer to that object is stored in
|
||||||
|
* this class using JNI on the native side, thus several instances of this can point to different actual
|
||||||
|
* native implementations.
|
||||||
|
*/
|
||||||
|
public class NativeScriptsChangeEventListener implements ScriptsChangeEventListener {
|
||||||
|
public long ptr;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public native void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts);
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.jni;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.listeners.TransactionConfidenceEventListener;
|
||||||
|
import org.bitcoinj.core.Transaction;
|
||||||
|
import org.bitcoinj.core.Wallet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event listener that relays events to a native C++ object. A pointer to that object is stored in
|
||||||
|
* this class using JNI on the native side, thus several instances of this can point to different actual
|
||||||
|
* native implementations.
|
||||||
|
*/
|
||||||
|
public class NativeTransactionConfidenceEventListener implements TransactionConfidenceEventListener {
|
||||||
|
public long ptr;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public native void onTransactionConfidenceChanged(Wallet wallet, Transaction tx);
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.jni;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.listeners.WalletChangeEventListener;
|
||||||
|
import org.bitcoinj.core.Wallet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event listener that relays events to a native C++ object. A pointer to that object is stored in
|
||||||
|
* this class using JNI on the native side, thus several instances of this can point to different actual
|
||||||
|
* native implementations.
|
||||||
|
*/
|
||||||
|
public class NativeWalletChangeEventListener implements WalletChangeEventListener {
|
||||||
|
public long ptr;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public native void onWalletChanged(Wallet wallet);
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.jni;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
|
||||||
|
import org.bitcoinj.core.Coin;
|
||||||
|
import org.bitcoinj.core.Transaction;
|
||||||
|
import org.bitcoinj.core.Wallet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event listener that relays events to a native C++ object. A pointer to that object is stored in
|
||||||
|
* this class using JNI on the native side, thus several instances of this can point to different actual
|
||||||
|
* native implementations.
|
||||||
|
*/
|
||||||
|
public class NativeWalletCoinsReceivedEventListener implements WalletCoinsReceivedEventListener {
|
||||||
|
public long ptr;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public native void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance);
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.jni;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsSentEventListener;
|
||||||
|
import org.bitcoinj.core.Coin;
|
||||||
|
import org.bitcoinj.core.Transaction;
|
||||||
|
import org.bitcoinj.core.Wallet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event listener that relays events to a native C++ object. A pointer to that object is stored in
|
||||||
|
* this class using JNI on the native side, thus several instances of this can point to different actual
|
||||||
|
* native implementations.
|
||||||
|
*/
|
||||||
|
public class NativeWalletCoinsSentEventListener implements WalletCoinsSentEventListener {
|
||||||
|
public long ptr;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public native void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance);
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.bitcoinj.jni;
|
||||||
|
|
||||||
|
import org.bitcoinj.core.listeners.WalletReorganizeEventListener;
|
||||||
|
import org.bitcoinj.core.Wallet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An event listener that relays events to a native C++ object. A pointer to that object is stored in
|
||||||
|
* this class using JNI on the native side, thus several instances of this can point to different actual
|
||||||
|
* native implementations.
|
||||||
|
*/
|
||||||
|
public class NativeWalletReorganizeEventListener implements WalletReorganizeEventListener {
|
||||||
|
public long ptr;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public native void onReorganize(Wallet wallet);
|
||||||
|
}
|
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.bitcoinj.protocols.channels;
|
package org.bitcoinj.protocols.channels;
|
||||||
|
|
||||||
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
|
|
||||||
import org.bitcoinj.core.*;
|
import org.bitcoinj.core.*;
|
||||||
import org.bitcoinj.crypto.TransactionSignature;
|
import org.bitcoinj.crypto.TransactionSignature;
|
||||||
import org.bitcoinj.script.Script;
|
import org.bitcoinj.script.Script;
|
||||||
@ -37,6 +36,7 @@ import javax.annotation.Nullable;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.*;
|
import static com.google.common.base.Preconditions.*;
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>A payment channel is a method of sending money to someone such that the amount of money you send can be adjusted
|
* <p>A payment channel is a method of sending money to someone such that the amount of money you send can be adjusted
|
||||||
@ -173,7 +173,7 @@ public class PaymentChannelClientState {
|
|||||||
if (storedChannel != null && storedChannel.close != null) {
|
if (storedChannel != null && storedChannel.close != null) {
|
||||||
watchCloseConfirmations();
|
watchCloseConfirmations();
|
||||||
}
|
}
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(Threading.SAME_THREAD, new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
synchronized (PaymentChannelClientState.this) {
|
synchronized (PaymentChannelClientState.this) {
|
||||||
@ -189,7 +189,7 @@ public class PaymentChannelClientState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, Threading.SAME_THREAD);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void watchCloseConfirmations() {
|
private void watchCloseConfirmations() {
|
||||||
|
@ -17,7 +17,10 @@
|
|||||||
|
|
||||||
package org.bitcoinj.core;
|
package org.bitcoinj.core;
|
||||||
|
|
||||||
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
|
import org.bitcoinj.core.listeners.WalletChangeEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.WalletReorganizeEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.TransactionConfidenceEventListener;
|
||||||
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
||||||
import org.bitcoinj.params.UnitTestParams;
|
import org.bitcoinj.params.UnitTestParams;
|
||||||
import org.bitcoinj.store.MemoryBlockStore;
|
import org.bitcoinj.store.MemoryBlockStore;
|
||||||
@ -76,11 +79,13 @@ public class ChainSplitTest {
|
|||||||
// (receiving coins). Checking that we understand reversed spends is in testForking2.
|
// (receiving coins). Checking that we understand reversed spends is in testForking2.
|
||||||
final AtomicBoolean reorgHappened = new AtomicBoolean();
|
final AtomicBoolean reorgHappened = new AtomicBoolean();
|
||||||
final AtomicInteger walletChanged = new AtomicInteger();
|
final AtomicInteger walletChanged = new AtomicInteger();
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addReorganizeEventListener(new WalletReorganizeEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onReorganize(Wallet wallet) {
|
public void onReorganize(Wallet wallet) {
|
||||||
reorgHappened.set(true);
|
reorgHappened.set(true);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
wallet.addChangeEventListener(new WalletChangeEventListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onWalletChanged(Wallet wallet) {
|
public void onWalletChanged(Wallet wallet) {
|
||||||
@ -297,10 +302,9 @@ public class ChainSplitTest {
|
|||||||
// double spend on the new best chain.
|
// double spend on the new best chain.
|
||||||
|
|
||||||
final boolean[] eventCalled = new boolean[1];
|
final boolean[] eventCalled = new boolean[1];
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addTransactionConfidenceEventListener(new TransactionConfidenceEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
||||||
super.onTransactionConfidenceChanged(wallet, tx);
|
|
||||||
if (tx.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.DEAD)
|
if (tx.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.DEAD)
|
||||||
eventCalled[0] = true;
|
eventCalled[0] = true;
|
||||||
}
|
}
|
||||||
@ -338,7 +342,7 @@ public class ChainSplitTest {
|
|||||||
// double spend on the new best chain.
|
// double spend on the new best chain.
|
||||||
final Transaction[] eventDead = new Transaction[1];
|
final Transaction[] eventDead = new Transaction[1];
|
||||||
final Transaction[] eventReplacement = new Transaction[1];
|
final Transaction[] eventReplacement = new Transaction[1];
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addTransactionConfidenceEventListener(new TransactionConfidenceEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
||||||
if (tx.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.DEAD) {
|
if (tx.getConfidence().getConfidenceType() == TransactionConfidence.ConfidenceType.DEAD) {
|
||||||
@ -400,7 +404,7 @@ public class ChainSplitTest {
|
|||||||
// Check that as the chain forks and re-orgs, the confidence data associated with each transaction is
|
// Check that as the chain forks and re-orgs, the confidence data associated with each transaction is
|
||||||
// maintained correctly.
|
// maintained correctly.
|
||||||
final ArrayList<Transaction> txns = new ArrayList<Transaction>(3);
|
final ArrayList<Transaction> txns = new ArrayList<Transaction>(3);
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
txns.add(tx);
|
txns.add(tx);
|
||||||
@ -558,12 +562,12 @@ public class ChainSplitTest {
|
|||||||
// transactions would be. Also check that a dead coinbase on a sidechain is resurrected if the sidechain
|
// transactions would be. Also check that a dead coinbase on a sidechain is resurrected if the sidechain
|
||||||
// becomes the best chain once more. Finally, check that dependent transactions are killed recursively.
|
// becomes the best chain once more. Finally, check that dependent transactions are killed recursively.
|
||||||
final ArrayList<Transaction> txns = new ArrayList<Transaction>(3);
|
final ArrayList<Transaction> txns = new ArrayList<Transaction>(3);
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(Threading.SAME_THREAD, new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
txns.add(tx);
|
txns.add(tx);
|
||||||
}
|
}
|
||||||
}, Threading.SAME_THREAD);
|
});
|
||||||
|
|
||||||
Block b1 = unitTestParams.getGenesisBlock().createNextBlock(someOtherGuy);
|
Block b1 = unitTestParams.getGenesisBlock().createNextBlock(someOtherGuy);
|
||||||
final ECKey coinsTo2 = wallet.freshReceiveKey();
|
final ECKey coinsTo2 = wallet.freshReceiveKey();
|
||||||
|
@ -527,7 +527,7 @@ public class PeerTest extends TestWithNetworkConnections {
|
|||||||
// Check that if we request dependency download to be disabled and receive a relevant tx, things work correctly.
|
// Check that if we request dependency download to be disabled and receive a relevant tx, things work correctly.
|
||||||
Transaction tx = FakeTxBuilder.createFakeTx(params, COIN, address);
|
Transaction tx = FakeTxBuilder.createFakeTx(params, COIN, address);
|
||||||
final Transaction[] result = new Transaction[1];
|
final Transaction[] result = new Transaction[1];
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
result[0] = tx;
|
result[0] = tx;
|
||||||
@ -644,7 +644,7 @@ public class PeerTest extends TestWithNetworkConnections {
|
|||||||
ECKey key = wallet.freshReceiveKey();
|
ECKey key = wallet.freshReceiveKey();
|
||||||
peer.addWallet(wallet);
|
peer.addWallet(wallet);
|
||||||
final Transaction[] vtx = new Transaction[1];
|
final Transaction[] vtx = new Transaction[1];
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
vtx[0] = tx;
|
vtx[0] = tx;
|
||||||
@ -696,7 +696,7 @@ public class PeerTest extends TestWithNetworkConnections {
|
|||||||
wallet.setAcceptRiskyTransactions(shouldAccept);
|
wallet.setAcceptRiskyTransactions(shouldAccept);
|
||||||
peer.addWallet(wallet);
|
peer.addWallet(wallet);
|
||||||
final Transaction[] vtx = new Transaction[1];
|
final Transaction[] vtx = new Transaction[1];
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
vtx[0] = tx;
|
vtx[0] = tx;
|
||||||
@ -775,7 +775,7 @@ public class PeerTest extends TestWithNetworkConnections {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void exceptionListener() throws Exception {
|
public void exceptionListener() throws Exception {
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
throw new NullPointerException("boo!");
|
throw new NullPointerException("boo!");
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
package org.bitcoinj.core;
|
package org.bitcoinj.core;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.*;
|
import com.google.common.util.concurrent.*;
|
||||||
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
|
import org.bitcoinj.core.listeners.TransactionConfidenceEventListener;
|
||||||
import org.bitcoinj.testing.*;
|
import org.bitcoinj.testing.*;
|
||||||
import org.bitcoinj.utils.*;
|
import org.bitcoinj.utils.*;
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
@ -198,7 +198,7 @@ public class TransactionBroadcastTest extends TestWithPeerGroup {
|
|||||||
|
|
||||||
// Check that the wallet informs us of changes in confidence as the transaction ripples across the network.
|
// Check that the wallet informs us of changes in confidence as the transaction ripples across the network.
|
||||||
final Transaction[] transactions = new Transaction[1];
|
final Transaction[] transactions = new Transaction[1];
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addTransactionConfidenceEventListener(new TransactionConfidenceEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
||||||
transactions[0] = tx;
|
transactions[0] = tx;
|
||||||
|
@ -17,8 +17,10 @@
|
|||||||
|
|
||||||
package org.bitcoinj.core;
|
package org.bitcoinj.core;
|
||||||
|
|
||||||
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
|
import org.bitcoinj.core.listeners.WalletChangeEventListener;
|
||||||
import org.bitcoinj.core.listeners.WalletCoinEventListener;
|
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsSentEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.TransactionConfidenceEventListener;
|
||||||
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
||||||
import org.bitcoinj.core.Wallet.SendRequest;
|
import org.bitcoinj.core.Wallet.SendRequest;
|
||||||
import org.bitcoinj.crypto.*;
|
import org.bitcoinj.crypto.*;
|
||||||
@ -412,16 +414,11 @@ public class WalletTest extends TestWithWallet {
|
|||||||
|
|
||||||
private static void broadcastAndCommit(Wallet wallet, Transaction t) throws Exception {
|
private static void broadcastAndCommit(Wallet wallet, Transaction t) throws Exception {
|
||||||
final LinkedList<Transaction> txns = Lists.newLinkedList();
|
final LinkedList<Transaction> txns = Lists.newLinkedList();
|
||||||
wallet.addCoinEventListener(new WalletCoinEventListener() {
|
wallet.addCoinsSentEventListener(new WalletCoinsSentEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
txns.add(tx);
|
txns.add(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
t.getConfidence().markBroadcastBy(new PeerAddress(params, InetAddress.getByAddress(new byte[]{1,2,3,4})));
|
t.getConfidence().markBroadcastBy(new PeerAddress(params, InetAddress.getByAddress(new byte[]{1,2,3,4})));
|
||||||
@ -559,26 +556,27 @@ public class WalletTest extends TestWithWallet {
|
|||||||
final Coin[] bigints = new Coin[4];
|
final Coin[] bigints = new Coin[4];
|
||||||
final Transaction[] txn = new Transaction[2];
|
final Transaction[] txn = new Transaction[2];
|
||||||
final LinkedList<Transaction> confTxns = new LinkedList<Transaction>();
|
final LinkedList<Transaction> confTxns = new LinkedList<Transaction>();
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
super.onCoinsReceived(wallet, tx, prevBalance, newBalance);
|
|
||||||
bigints[0] = prevBalance;
|
bigints[0] = prevBalance;
|
||||||
bigints[1] = newBalance;
|
bigints[1] = newBalance;
|
||||||
txn[0] = tx;
|
txn[0] = tx;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
wallet.addCoinsSentEventListener(new WalletCoinsSentEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
super.onCoinsSent(wallet, tx, prevBalance, newBalance);
|
|
||||||
bigints[2] = prevBalance;
|
bigints[2] = prevBalance;
|
||||||
bigints[3] = newBalance;
|
bigints[3] = newBalance;
|
||||||
txn[1] = tx;
|
txn[1] = tx;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
wallet.addTransactionConfidenceEventListener(new TransactionConfidenceEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
||||||
super.onTransactionConfidenceChanged(wallet, tx);
|
|
||||||
confTxns.add(tx);
|
confTxns.add(tx);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -813,17 +811,18 @@ public class WalletTest extends TestWithWallet {
|
|||||||
final Transaction[] eventDead = new Transaction[1];
|
final Transaction[] eventDead = new Transaction[1];
|
||||||
final Transaction[] eventReplacement = new Transaction[1];
|
final Transaction[] eventReplacement = new Transaction[1];
|
||||||
final int[] eventWalletChanged = new int[1];
|
final int[] eventWalletChanged = new int[1];
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addTransactionConfidenceEventListener(new TransactionConfidenceEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
||||||
super.onTransactionConfidenceChanged(wallet, tx);
|
|
||||||
if (tx.getConfidence().getConfidenceType() ==
|
if (tx.getConfidence().getConfidenceType() ==
|
||||||
TransactionConfidence.ConfidenceType.DEAD) {
|
TransactionConfidence.ConfidenceType.DEAD) {
|
||||||
eventDead[0] = tx;
|
eventDead[0] = tx;
|
||||||
eventReplacement[0] = tx.getConfidence().getOverridingTransaction();
|
eventReplacement[0] = tx.getConfidence().getOverridingTransaction();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
wallet.addChangeEventListener(new WalletChangeEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onWalletChanged(Wallet wallet) {
|
public void onWalletChanged(Wallet wallet) {
|
||||||
eventWalletChanged[0]++;
|
eventWalletChanged[0]++;
|
||||||
@ -1277,7 +1276,7 @@ public class WalletTest extends TestWithWallet {
|
|||||||
final boolean[] flags = new boolean[2];
|
final boolean[] flags = new boolean[2];
|
||||||
final Transaction[] notifiedTx = new Transaction[1];
|
final Transaction[] notifiedTx = new Transaction[1];
|
||||||
final int[] walletChanged = new int[1];
|
final int[] walletChanged = new int[1];
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
// Check we got the expected transaction.
|
// Check we got the expected transaction.
|
||||||
@ -1289,7 +1288,9 @@ public class WalletTest extends TestWithWallet {
|
|||||||
flags[1] = tx.isPending();
|
flags[1] = tx.isPending();
|
||||||
notifiedTx[0] = tx;
|
notifiedTx[0] = tx;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
wallet.addChangeEventListener(new WalletChangeEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onWalletChanged(Wallet wallet) {
|
public void onWalletChanged(Wallet wallet) {
|
||||||
walletChanged[0]++;
|
walletChanged[0]++;
|
||||||
@ -1345,18 +1346,13 @@ public class WalletTest extends TestWithWallet {
|
|||||||
// Check that if we receive a pending tx we did not send, it updates our spent flags correctly.
|
// Check that if we receive a pending tx we did not send, it updates our spent flags correctly.
|
||||||
final Transaction[] txn = new Transaction[1];
|
final Transaction[] txn = new Transaction[1];
|
||||||
final Coin[] bigints = new Coin[2];
|
final Coin[] bigints = new Coin[2];
|
||||||
wallet.addCoinEventListener(new WalletCoinEventListener() {
|
wallet.addCoinsSentEventListener(new WalletCoinsSentEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
txn[0] = tx;
|
txn[0] = tx;
|
||||||
bigints[0] = prevBalance;
|
bigints[0] = prevBalance;
|
||||||
bigints[1] = newBalance;
|
bigints[1] = newBalance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
|
||||||
// Do nothing
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
// Receive some coins.
|
// Receive some coins.
|
||||||
Coin nanos = COIN;
|
Coin nanos = COIN;
|
||||||
@ -1399,15 +1395,16 @@ public class WalletTest extends TestWithWallet {
|
|||||||
t2.addInput(doubleSpentOut);
|
t2.addInput(doubleSpentOut);
|
||||||
|
|
||||||
final Transaction[] called = new Transaction[2];
|
final Transaction[] called = new Transaction[2];
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
called[0] = tx;
|
called[0] = tx;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
wallet.addTransactionConfidenceEventListener(new TransactionConfidenceEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
||||||
super.onTransactionConfidenceChanged(wallet, tx);
|
|
||||||
if (tx.getConfidence().getConfidenceType() ==
|
if (tx.getConfidence().getConfidenceType() ==
|
||||||
TransactionConfidence.ConfidenceType.DEAD) {
|
TransactionConfidence.ConfidenceType.DEAD) {
|
||||||
called[0] = tx;
|
called[0] = tx;
|
||||||
@ -2951,30 +2948,20 @@ public class WalletTest extends TestWithWallet {
|
|||||||
@Test
|
@Test
|
||||||
public void exceptionsDoNotBlockAllListeners() throws Exception {
|
public void exceptionsDoNotBlockAllListeners() throws Exception {
|
||||||
// Check that if a wallet listener throws an exception, the others still run.
|
// Check that if a wallet listener throws an exception, the others still run.
|
||||||
wallet.addCoinEventListener(new WalletCoinEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
log.info("onCoinsReceived 1");
|
log.info("onCoinsReceived 1");
|
||||||
throw new RuntimeException("barf");
|
throw new RuntimeException("barf");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
|
||||||
// Do nothing
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
final AtomicInteger flag = new AtomicInteger();
|
final AtomicInteger flag = new AtomicInteger();
|
||||||
wallet.addCoinEventListener(new WalletCoinEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
log.info("onCoinsReceived 2");
|
log.info("onCoinsReceived 2");
|
||||||
flag.incrementAndGet();
|
flag.incrementAndGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
|
||||||
// Do nothing
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN);
|
sendMoneyToWallet(COIN, AbstractBlockChain.NewBlockType.BEST_CHAIN);
|
||||||
@ -3419,12 +3406,12 @@ public class WalletTest extends TestWithWallet {
|
|||||||
// Check that we can register an event listener, generate some keys and the callbacks are invoked properly.
|
// Check that we can register an event listener, generate some keys and the callbacks are invoked properly.
|
||||||
wallet = new Wallet(params);
|
wallet = new Wallet(params);
|
||||||
final List<ECKey> keys = Lists.newLinkedList();
|
final List<ECKey> keys = Lists.newLinkedList();
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addKeyChainEventListener(Threading.SAME_THREAD, new KeyChainEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onKeysAdded(List<ECKey> k) {
|
public void onKeysAdded(List<ECKey> k) {
|
||||||
keys.addAll(k);
|
keys.addAll(k);
|
||||||
}
|
}
|
||||||
}, Threading.SAME_THREAD);
|
});
|
||||||
wallet.freshReceiveKey();
|
wallet.freshReceiveKey();
|
||||||
assertEquals(1, keys.size());
|
assertEquals(1, keys.size());
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
package org.bitcoinj.store;
|
package org.bitcoinj.store;
|
||||||
|
|
||||||
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
|
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
|
||||||
import org.bitcoinj.core.*;
|
import org.bitcoinj.core.*;
|
||||||
import org.bitcoinj.core.Transaction.Purpose;
|
import org.bitcoinj.core.Transaction.Purpose;
|
||||||
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
||||||
@ -212,7 +212,7 @@ public class WalletProtobufSerializerTest {
|
|||||||
BlockChain chain = new BlockChain(params, myWallet, new MemoryBlockStore(params));
|
BlockChain chain = new BlockChain(params, myWallet, new MemoryBlockStore(params));
|
||||||
|
|
||||||
final ArrayList<Transaction> txns = new ArrayList<Transaction>(2);
|
final ArrayList<Transaction> txns = new ArrayList<Transaction>(2);
|
||||||
myWallet.addEventListener(new AbstractWalletEventListener() {
|
myWallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
txns.add(tx);
|
txns.add(tx);
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
|
|
||||||
package org.bitcoinj.examples;
|
package org.bitcoinj.examples;
|
||||||
|
|
||||||
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
|
|
||||||
import org.bitcoinj.core.*;
|
import org.bitcoinj.core.*;
|
||||||
import org.bitcoinj.crypto.KeyCrypterException;
|
import org.bitcoinj.crypto.KeyCrypterException;
|
||||||
import org.bitcoinj.kits.WalletAppKit;
|
import org.bitcoinj.kits.WalletAppKit;
|
||||||
@ -32,6 +31,7 @@ import com.google.common.util.concurrent.MoreExecutors;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ForwardingService demonstrates basic usage of the library. It sits on the network and when it receives coins, simply
|
* ForwardingService demonstrates basic usage of the library. It sits on the network and when it receives coins, simply
|
||||||
@ -79,7 +79,7 @@ public class ForwardingService {
|
|||||||
kit.awaitRunning();
|
kit.awaitRunning();
|
||||||
|
|
||||||
// We want to know when we receive money.
|
// We want to know when we receive money.
|
||||||
kit.wallet().addEventListener(new AbstractWalletEventListener() {
|
kit.wallet().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
// Runs in the dedicated "user thread" (see bitcoinj docs for more info on this).
|
// Runs in the dedicated "user thread" (see bitcoinj docs for more info on this).
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
|
|
||||||
package org.bitcoinj.examples;
|
package org.bitcoinj.examples;
|
||||||
|
|
||||||
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
|
|
||||||
import org.bitcoinj.core.*;
|
import org.bitcoinj.core.*;
|
||||||
import org.bitcoinj.kits.WalletAppKit;
|
import org.bitcoinj.kits.WalletAppKit;
|
||||||
import org.bitcoinj.params.TestNet3Params;
|
import org.bitcoinj.params.TestNet3Params;
|
||||||
@ -22,6 +21,11 @@ import org.bitcoinj.script.Script;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsSentEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.ScriptsChangeEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.TransactionConfidenceEventListener;
|
||||||
|
import org.bitcoinj.wallet.KeyChainEventListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The following example shows how to use the by bitcoinj provided WalletAppKit.
|
* The following example shows how to use the by bitcoinj provided WalletAppKit.
|
||||||
@ -57,9 +61,43 @@ public class Kit {
|
|||||||
kit.startAsync();
|
kit.startAsync();
|
||||||
kit.awaitRunning();
|
kit.awaitRunning();
|
||||||
|
|
||||||
// To observe wallet events (like coins received) we implement a EventListener class that extends the AbstractWalletEventListener bitcoinj then calls the different functions from the EventListener class
|
kit.wallet().addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
WalletListener wListener = new WalletListener();
|
@Override
|
||||||
kit.wallet().addEventListener(wListener);
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
|
System.out.println("-----> coins resceived: " + tx.getHashAsString());
|
||||||
|
System.out.println("received: " + tx.getValue(wallet));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
kit.wallet().addCoinsSentEventListener(new WalletCoinsSentEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
|
System.out.println("coins sent");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
kit.wallet().addKeyChainEventListener(new KeyChainEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onKeysAdded(List<ECKey> keys) {
|
||||||
|
System.out.println("new key added");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
kit.wallet().addScriptsChangeEventListener(new ScriptsChangeEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
|
||||||
|
System.out.println("new script added");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
kit.wallet().addTransactionConfidenceEventListener(new TransactionConfidenceEventListener() {
|
||||||
|
@Override
|
||||||
|
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
||||||
|
System.out.println("-----> confidence changed: " + tx.getHashAsString());
|
||||||
|
TransactionConfidence confidence = tx.getConfidence();
|
||||||
|
System.out.println("new block depth: " + confidence.getDepthInBlocks());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Ready to run. The kit syncs the blockchain and our wallet event listener gets notified when something happens.
|
// Ready to run. The kit syncs the blockchain and our wallet event listener gets notified when something happens.
|
||||||
// To test everything we create and print a fresh receiving address. Send some coins to that address and see if everything works.
|
// To test everything we create and print a fresh receiving address. Send some coins to that address and see if everything works.
|
||||||
@ -71,44 +109,4 @@ public class Kit {
|
|||||||
//kit.awaitTerminated();
|
//kit.awaitTerminated();
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Wallet event listener its implementations get called on wallet changes.
|
|
||||||
static class WalletListener extends AbstractWalletEventListener {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
|
||||||
System.out.println("-----> coins resceived: " + tx.getHashAsString());
|
|
||||||
System.out.println("received: " + tx.getValue(wallet));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
|
||||||
System.out.println("-----> confidence changed: " + tx.getHashAsString());
|
|
||||||
TransactionConfidence confidence = tx.getConfidence();
|
|
||||||
System.out.println("new block depth: " + confidence.getDepthInBlocks());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
|
||||||
System.out.println("coins sent");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReorganize(Wallet wallet) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onWalletChanged(Wallet wallet) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onKeysAdded(List<ECKey> keys) {
|
|
||||||
System.out.println("new key added");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
|
|
||||||
System.out.println("new script added");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,13 @@
|
|||||||
|
|
||||||
package org.bitcoinj.examples;
|
package org.bitcoinj.examples;
|
||||||
|
|
||||||
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
|
|
||||||
import org.bitcoinj.core.*;
|
import org.bitcoinj.core.*;
|
||||||
import org.bitcoinj.params.TestNet3Params;
|
import org.bitcoinj.params.TestNet3Params;
|
||||||
import org.bitcoinj.store.BlockStore;
|
import org.bitcoinj.store.BlockStore;
|
||||||
import org.bitcoinj.store.MemoryBlockStore;
|
import org.bitcoinj.store.MemoryBlockStore;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RefreshWallet loads a wallet, then processes the block chain to update the transaction pools within it.
|
* RefreshWallet loads a wallet, then processes the block chain to update the transaction pools within it.
|
||||||
@ -42,7 +42,7 @@ public class RefreshWallet {
|
|||||||
final PeerGroup peerGroup = new PeerGroup(params, chain);
|
final PeerGroup peerGroup = new PeerGroup(params, chain);
|
||||||
peerGroup.startAsync();
|
peerGroup.startAsync();
|
||||||
|
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public synchronized void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public synchronized void onCoinsReceived(Wallet w, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
System.out.println("\nReceived tx " + tx.getHashAsString());
|
System.out.println("\nReceived tx " + tx.getHashAsString());
|
||||||
|
@ -49,7 +49,6 @@ import joptsimple.OptionSpec;
|
|||||||
import joptsimple.util.DateConverter;
|
import joptsimple.util.DateConverter;
|
||||||
|
|
||||||
import org.bitcoinj.core.listeners.AbstractPeerDataEventListener;
|
import org.bitcoinj.core.listeners.AbstractPeerDataEventListener;
|
||||||
import org.bitcoinj.core.listeners.AbstractWalletEventListener;
|
|
||||||
import org.bitcoinj.core.listeners.DownloadProgressTracker;
|
import org.bitcoinj.core.listeners.DownloadProgressTracker;
|
||||||
import org.bitcoinj.wallet.MarriedKeyChain;
|
import org.bitcoinj.wallet.MarriedKeyChain;
|
||||||
import org.bitcoinj.wallet.Protos;
|
import org.bitcoinj.wallet.Protos;
|
||||||
@ -78,6 +77,10 @@ import java.util.logging.LogManager;
|
|||||||
|
|
||||||
import static org.bitcoinj.core.Coin.parseCoin;
|
import static org.bitcoinj.core.Coin.parseCoin;
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
import org.bitcoinj.core.listeners.WalletChangeEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsReceivedEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.WalletCoinsSentEventListener;
|
||||||
|
import org.bitcoinj.core.listeners.WalletReorganizeEventListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A command line tool for manipulating wallets and working with Bitcoin.
|
* A command line tool for manipulating wallets and working with Bitcoin.
|
||||||
@ -1109,25 +1112,20 @@ public class WalletTool {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case WALLET_TX:
|
case WALLET_TX:
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
||||||
private void handleTx(Transaction tx) {
|
|
||||||
System.out.println(tx.getHashAsString());
|
|
||||||
latch.countDown(); // Wake up main thread.
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
// Runs in a peer thread.
|
// Runs in a peer thread.
|
||||||
super.onCoinsReceived(wallet, tx, prevBalance, newBalance);
|
System.out.println(tx.getHashAsString());
|
||||||
handleTx(tx);
|
latch.countDown(); // Wake up main thread.
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
wallet.addCoinsSentEventListener(new WalletCoinsSentEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance,
|
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
Coin newBalance) {
|
|
||||||
// Runs in a peer thread.
|
// Runs in a peer thread.
|
||||||
super.onCoinsSent(wallet, tx, prevBalance, newBalance);
|
System.out.println(tx.getHashAsString());
|
||||||
handleTx(tx);
|
latch.countDown(); // Wake up main thread.
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
@ -1151,18 +1149,11 @@ public class WalletTool {
|
|||||||
latch.countDown();
|
latch.countDown();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
final WalletEventListener listener = new WalletEventListener(latch);
|
||||||
@Override
|
wallet.addCoinsReceivedEventListener(listener);
|
||||||
public synchronized void onChange() {
|
wallet.addCoinsSentEventListener(listener);
|
||||||
super.onChange();
|
wallet.addChangeEventListener(listener);
|
||||||
saveWallet(walletFile);
|
wallet.addReorganizeEventListener(listener);
|
||||||
Coin balance = wallet.getBalance(Wallet.BalanceType.ESTIMATED);
|
|
||||||
if (condition.matchBitcoins(balance)) {
|
|
||||||
System.out.println(balance.toFriendlyString());
|
|
||||||
latch.countDown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1474,4 +1465,42 @@ public class WalletTool {
|
|||||||
System.out.println("Clearing creation time.");
|
System.out.println("Clearing creation time.");
|
||||||
seed.setCreationTimeSeconds(creationTime);
|
seed.setCreationTimeSeconds(creationTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static synchronized void onChange(final CountDownLatch latch) {
|
||||||
|
saveWallet(walletFile);
|
||||||
|
Coin balance = wallet.getBalance(Wallet.BalanceType.ESTIMATED);
|
||||||
|
if (condition.matchBitcoins(balance)) {
|
||||||
|
System.out.println(balance.toFriendlyString());
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class WalletEventListener implements WalletChangeEventListener, WalletCoinsReceivedEventListener,
|
||||||
|
WalletCoinsSentEventListener, WalletReorganizeEventListener {
|
||||||
|
private final CountDownLatch latch;
|
||||||
|
|
||||||
|
private WalletEventListener(final CountDownLatch latch) {
|
||||||
|
this.latch = latch;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWalletChanged(Wallet wallet) {
|
||||||
|
onChange(latch);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
|
onChange(latch);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||||
|
onChange(latch);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReorganize(Wallet wallet) {
|
||||||
|
onChange(latch);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import javafx.beans.property.SimpleDoubleProperty;
|
|||||||
import javafx.beans.property.SimpleObjectProperty;
|
import javafx.beans.property.SimpleObjectProperty;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import org.bitcoinj.core.listeners.WalletChangeEventListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that exposes relevant bitcoin stuff as JavaFX bindable properties.
|
* A class that exposes relevant bitcoin stuff as JavaFX bindable properties.
|
||||||
@ -28,13 +29,12 @@ public class BitcoinUIModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final void setWallet(Wallet wallet) {
|
public final void setWallet(Wallet wallet) {
|
||||||
wallet.addEventListener(new AbstractWalletEventListener() {
|
wallet.addChangeEventListener(Platform::runLater, new WalletChangeEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onWalletChanged(Wallet wallet) {
|
public void onWalletChanged(Wallet wallet) {
|
||||||
super.onWalletChanged(wallet);
|
|
||||||
update(wallet);
|
update(wallet);
|
||||||
}
|
}
|
||||||
}, Platform::runLater);
|
});
|
||||||
update(wallet);
|
update(wallet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user