org.bitcoinj.wallet.listeners.KeyChainEventListener Java Examples

The following examples show how to use org.bitcoinj.wallet.listeners.KeyChainEventListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KeyChainGroupTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void events() throws Exception {
    // Check that events are registered with the right chains and that if a chain is added, it gets the event
    // listeners attached properly even post-hoc.
    final AtomicReference<ECKey> ran = new AtomicReference<>(null);
    final KeyChainEventListener listener = new KeyChainEventListener() {
        @Override
        public void onKeysAdded(List<ECKey> keys) {
            ran.set(keys.get(0));
        }
    };
    group.addEventListener(listener, Threading.SAME_THREAD);
    ECKey key = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertEquals(key, ran.getAndSet(null));
    ECKey key2 = new ECKey();
    group.importKeys(key2);
    assertEquals(key2, ran.getAndSet(null));
    group.removeEventListener(listener);
    ECKey key3 = new ECKey();
    group.importKeys(key3);
    assertNull(ran.get());
}
 
Example #2
Source File: KeyChainGroupTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void events() throws Exception {
    // Check that events are registered with the right chains and that if a chain is added, it gets the event
    // listeners attached properly even post-hoc.
    final AtomicReference<ECKey> ran = new AtomicReference<>(null);
    final KeyChainEventListener listener = new KeyChainEventListener() {
        @Override
        public void onKeysAdded(List<ECKey> keys) {
            ran.set(keys.get(0));
        }
    };
    group.addEventListener(listener, Threading.SAME_THREAD);
    ECKey key = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertEquals(key, ran.getAndSet(null));
    ECKey key2 = new ECKey();
    group.importKeys(key2);
    assertEquals(key2, ran.getAndSet(null));
    group.removeEventListener(listener);
    ECKey key3 = new ECKey();
    group.importKeys(key3);
    assertNull(ran.get());
}
 
Example #3
Source File: KeyChainGroupTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void events() throws Exception {
    // Check that events are registered with the right chains and that if a chain is added, it gets the event
    // listeners attached properly even post-hoc.
    final AtomicReference<ECKey> ran = new AtomicReference<>(null);
    final KeyChainEventListener listener = new KeyChainEventListener() {
        @Override
        public void onKeysAdded(List<ECKey> keys) {
            ran.set(keys.get(0));
        }
    };
    group.addEventListener(listener, Threading.SAME_THREAD);
    ECKey key = group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertEquals(key, ran.getAndSet(null));
    ECKey key2 = new ECKey();
    group.importKeys(key2);
    assertEquals(key2, ran.getAndSet(null));
    group.removeEventListener(listener);
    ECKey key3 = new ECKey();
    group.importKeys(key3);
    assertNull(ran.get());
}
 
Example #4
Source File: BasicKeyChain.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void queueOnKeysAdded(final List<ECKey> keys) {
    checkState(lock.isHeldByCurrentThread());
    for (final ListenerRegistration<KeyChainEventListener> registration : listeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onKeysAdded(keys);
            }
        });
    }
}
 
Example #5
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void keyEvents() throws Exception {
    // Check that we can register an event listener, generate some keys and the callbacks are invoked properly.
    wallet = new Wallet(PARAMS);
    final List<ECKey> keys = Lists.newLinkedList();
    wallet.addKeyChainEventListener(Threading.SAME_THREAD, new KeyChainEventListener() {
        @Override
        public void onKeysAdded(List<ECKey> k) {
            keys.addAll(k);
        }
    });
    wallet.freshReceiveKey();
    assertEquals(1, keys.size());
}
 
Example #6
Source File: KeyChainGroup.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds an HD chain to the chains list, and make it the default chain (from which keys are issued).
 * Useful for adding a complex pre-configured keychain, such as a married wallet.
 */
public void addAndActivateHDChain(DeterministicKeyChain chain) {
    log.info("Creating and activating a new HD chain: {}", chain);
    for (ListenerRegistration<KeyChainEventListener> registration : basic.getListeners())
        chain.addEventListener(registration.listener, registration.executor);
    if (lookaheadSize >= 0)
        chain.setLookaheadSize(lookaheadSize);
    if (lookaheadThreshold >= 0)
        chain.setLookaheadThreshold(lookaheadThreshold);
    chains.add(chain);
}
 
Example #7
Source File: KeyChainGroup.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds an HD chain to the chains list, and make it the default chain (from which keys are issued).
 * Useful for adding a complex pre-configured keychain, such as a married wallet.
 */
public void addAndActivateHDChain(DeterministicKeyChain chain) {
    log.info("Creating and activating a new HD chain: {}", chain);
    for (ListenerRegistration<KeyChainEventListener> registration : basic.getListeners())
        chain.addEventListener(registration.listener, registration.executor);
    if (lookaheadSize >= 0)
        chain.setLookaheadSize(lookaheadSize);
    if (lookaheadThreshold >= 0)
        chain.setLookaheadThreshold(lookaheadThreshold);
    chains.add(chain);
}
 
Example #8
Source File: KeyChainGroup.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/** Adds a listener for events that are run when keys are added, on the given executor. */
public void addEventListener(KeyChainEventListener listener, Executor executor) {
    checkNotNull(listener);
    checkNotNull(executor);
    basic.addEventListener(listener, executor);
    for (DeterministicKeyChain chain : chains)
        chain.addEventListener(listener, executor);
}
 
Example #9
Source File: KeyChainGroup.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/** Removes a listener for events that are run when keys are added. */
public boolean removeEventListener(KeyChainEventListener listener) {
    checkNotNull(listener);
    for (DeterministicKeyChain chain : chains)
        chain.removeEventListener(listener);
    return basic.removeEventListener(listener);
}
 
Example #10
Source File: KeyChainGroup.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/** Removes a listener for events that are run when keys are added. */
public boolean removeEventListener(KeyChainEventListener listener) {
    checkNotNull(listener);
    for (DeterministicKeyChain chain : chains)
        chain.removeEventListener(listener);
    return basic.removeEventListener(listener);
}
 
Example #11
Source File: KeyChainGroup.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/** Adds a listener for events that are run when keys are added, on the given executor. */
public void addEventListener(KeyChainEventListener listener, Executor executor) {
    checkNotNull(listener);
    checkNotNull(executor);
    basic.addEventListener(listener, executor);
    for (DeterministicKeyChain chain : chains)
        chain.addEventListener(listener, executor);
}
 
Example #12
Source File: KeyChainGroup.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds an HD chain to the chains list, and make it the default chain (from which keys are issued).
 * Useful for adding a complex pre-configured keychain, such as a married wallet.
 */
public void addAndActivateHDChain(DeterministicKeyChain chain) {
    log.info("Creating and activating a new HD chain: {}", chain);
    for (ListenerRegistration<KeyChainEventListener> registration : basic.getListeners())
        chain.addEventListener(registration.listener, registration.executor);
    if (lookaheadSize >= 0)
        chain.setLookaheadSize(lookaheadSize);
    if (lookaheadThreshold >= 0)
        chain.setLookaheadThreshold(lookaheadThreshold);
    chains.add(chain);
}
 
Example #13
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void keyEvents() throws Exception {
    // Check that we can register an event listener, generate some keys and the callbacks are invoked properly.
    wallet = new Wallet(UNITTEST);
    final List<ECKey> keys = Lists.newLinkedList();
    wallet.addKeyChainEventListener(Threading.SAME_THREAD, new KeyChainEventListener() {
        @Override
        public void onKeysAdded(List<ECKey> k) {
            keys.addAll(k);
        }
    });
    wallet.freshReceiveKey();
    assertEquals(1, keys.size());
}
 
Example #14
Source File: BasicKeyChain.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private void queueOnKeysAdded(final List<ECKey> keys) {
    checkState(lock.isHeldByCurrentThread());
    for (final ListenerRegistration<KeyChainEventListener> registration : listeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onKeysAdded(keys);
            }
        });
    }
}
 
Example #15
Source File: BasicKeyChain.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void queueOnKeysAdded(final List<ECKey> keys) {
    checkState(lock.isHeldByCurrentThread());
    for (final ListenerRegistration<KeyChainEventListener> registration : listeners) {
        registration.executor.execute(new Runnable() {
            @Override
            public void run() {
                registration.listener.onKeysAdded(keys);
            }
        });
    }
}
 
Example #16
Source File: KeyChainGroup.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Removes a listener for events that are run when keys are added.
 */
public boolean removeEventListener(KeyChainEventListener listener) {
    checkNotNull(listener);
    for (DeterministicKeyChain chain : chains)
        chain.removeEventListener(listener);
    return basic.removeEventListener(listener);
}
 
Example #17
Source File: KeyChainGroup.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds a listener for events that are run when keys are added, on the given executor.
 */
public void addEventListener(KeyChainEventListener listener, Executor executor) {
    checkNotNull(listener);
    checkNotNull(executor);
    basic.addEventListener(listener, executor);
    for (DeterministicKeyChain chain : chains)
        chain.addEventListener(listener, executor);
}
 
Example #18
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void keyEvents() throws Exception {
    // Check that we can register an event listener, generate some keys and the callbacks are invoked properly.
    wallet = new Wallet(PARAMS);
    final List<ECKey> keys = Lists.newLinkedList();
    wallet.addKeyChainEventListener(Threading.SAME_THREAD, new KeyChainEventListener() {
        @Override
        public void onKeysAdded(List<ECKey> k) {
            keys.addAll(k);
        }
    });
    wallet.freshReceiveKey();
    assertEquals(1, keys.size());
}
 
Example #19
Source File: DeterministicKeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean removeEventListener(KeyChainEventListener listener) {
    return basicKeyChain.removeEventListener(listener);
}
 
Example #20
Source File: KeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/** Adds a listener for events that are run when keys are added, on the user thread. */
void addEventListener(KeyChainEventListener listener);
 
Example #21
Source File: KeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/** Adds a listener for events that are run when keys are added, on the given executor. */
void addEventListener(KeyChainEventListener listener, Executor executor);
 
Example #22
Source File: KeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/** Removes a listener for events that are run when keys are added. */
boolean removeEventListener(KeyChainEventListener listener);
 
Example #23
Source File: DeterministicKeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void addEventListener(KeyChainEventListener listener, Executor executor) {
    basicKeyChain.addEventListener(listener, executor);
}
 
Example #24
Source File: BasicKeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public List<ListenerRegistration<KeyChainEventListener>> getListeners() {
    return new ArrayList<>(listeners);
}
 
Example #25
Source File: BasicKeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void addEventListener(KeyChainEventListener listener) {
    addEventListener(listener, Threading.USER_THREAD);
}
 
Example #26
Source File: DeterministicKeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void addEventListener(KeyChainEventListener listener) {
    basicKeyChain.addEventListener(listener);
}
 
Example #27
Source File: BasicKeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void addEventListener(KeyChainEventListener listener, Executor executor) {
    listeners.add(new ListenerRegistration<>(listener, executor));
}
 
Example #28
Source File: BasicKeyChain.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean removeEventListener(KeyChainEventListener listener) {
    return ListenerRegistration.removeFromList(listener, listeners);
}
 
Example #29
Source File: KeyChainGroup.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/** Adds a listener for events that are run when keys are added, on the user thread. */
public void addEventListener(KeyChainEventListener listener) {
    addEventListener(listener, Threading.USER_THREAD);
}
 
Example #30
Source File: KeyChain.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/** Adds a listener for events that are run when keys are added, on the given executor. */
void addEventListener(KeyChainEventListener listener, Executor executor);