Skip to content

Commit

Permalink
replace listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarguindzberg committed Mar 9, 2019
1 parent 257550a commit d307fa8
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions core/src/main/java/bisq/core/btc/model/AddressEntryList.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import com.google.protobuf.Message;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.crypto.DeterministicKey;
import org.bitcoinj.wallet.Wallet;

Expand Down Expand Up @@ -123,22 +125,41 @@ public void onWalletReady(Wallet wallet) {
persist();
}

wallet.addCoinsReceivedEventListener((w, tx, prevBalance, newBalance) -> {
updateList(tx);
});
wallet.addCoinsSentEventListener((w, tx, prevBalance, newBalance) -> {
updateList(tx);
});

// We add a confidence listener to get notified about potential new transactions and
// add an address entry list in case it does not exist yet. This is mainly needed for restore from seed words
// but can help as well in case the addressEntry list would miss an address where the wallet was received
// funds (e.g. if the user sends funds to an address which has not been provided in the main UI - like from the
// wallet details window).
wallet.addTransactionConfidenceEventListener((w, tx) -> {
tx.getOutputs().stream()
.filter(output -> output.isMine(wallet))
.map(output -> output.getAddressFromP2PKHScript(wallet.getNetworkParameters()))
.filter(Objects::nonNull)
.filter(address -> !listContainsEntryWithAddress(address.toBase58()))
.map(address -> (DeterministicKey) wallet.findKeyFromPubHash(address.getHash160()))
.filter(Objects::nonNull)
.map(deterministicKey -> new AddressEntry(deterministicKey, AddressEntry.Context.AVAILABLE))
.forEach(addressEntry -> list.add(addressEntry));
});
// wallet.addTransactionConfidenceEventListener((w, tx) -> {
// tx.getOutputs().stream()
// .filter(output -> output.isMine(wallet))
// .map(output -> output.getAddressFromP2PKHScript(wallet.getNetworkParameters()))
// .filter(Objects::nonNull)
// .filter(address -> !listContainsEntryWithAddress(address.toBase58()))
// .map(address -> (DeterministicKey) wallet.findKeyFromPubHash(address.getHash160()))
// .filter(Objects::nonNull)
// .map(deterministicKey -> new AddressEntry(deterministicKey, AddressEntry.Context.AVAILABLE))
// .forEach(addressEntry -> list.add(addressEntry));
// });
}

private void updateList(Transaction tx) {
tx.getOutputs().stream()
.filter(output -> output.isMine(wallet))
.map(output -> output.getAddressFromP2PKHScript(wallet.getNetworkParameters()))
.filter(Objects::nonNull)
.filter(address -> !listContainsEntryWithAddress(address.toBase58()))
.map(address -> (DeterministicKey) wallet.findKeyFromPubHash(address.getHash160()))
.filter(Objects::nonNull)
.map(deterministicKey -> new AddressEntry(deterministicKey, AddressEntry.Context.AVAILABLE))
.forEach(addressEntry -> list.add(addressEntry));
}

private boolean listContainsEntryWithAddress(String addressString) {
Expand Down

0 comments on commit d307fa8

Please sign in to comment.