org.bitcoinj.crypto.KeyCrypterException Java Examples

The following examples show how to use org.bitcoinj.crypto.KeyCrypterException. 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: ForwardingService.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
private static void forwardCoins(Transaction tx) {
    try {
        Coin value = tx.getValueSentToMe(kit.wallet());
        System.out.println("Forwarding " + value.toFriendlyString());
        // Now send the coins back! Send with a small fee attached to ensure rapid confirmation.
        final Coin amountToSend = value.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
        final Wallet.SendResult sendResult = kit.wallet().sendCoins(kit.peerGroup(), forwardingAddress, amountToSend);
        checkNotNull(sendResult);  // We should never try to send more coins than we have!
        System.out.println("Sending ...");
        // Register a callback that is invoked when the transaction has propagated across the network.
        // This shows a second style of registering ListenableFuture callbacks, it works when you don't
        // need access to the object the future returns.
        sendResult.broadcastComplete.addListener(new Runnable() {
            @Override
            public void run() {
                // The wallet has changed now, it'll get auto saved shortly or when the app shuts down.
                System.out.println("Sent coins onwards! Transaction hash is " + sendResult.tx.getHashAsString());
            }
        }, MoreExecutors.sameThreadExecutor());
    } catch (KeyCrypterException | InsufficientMoneyException e) {
        // We don't use encrypted wallets in this example - can never happen.
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: ForwardingService.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
private static void forwardCoins(Transaction tx) {
    try {
        Coin value = tx.getValueSentToMe(kit.wallet());
        System.out.println("Forwarding " + value.toFriendlyString());
        // Now send the coins back! Send with a small fee attached to ensure rapid confirmation.
        final Coin amountToSend = value.subtract(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE);
        final Wallet.SendResult sendResult = kit.wallet().sendCoins(kit.peerGroup(), forwardingAddress, amountToSend);
        checkNotNull(sendResult);  // We should never try to send more coins than we have!
        System.out.println("Sending ...");
        // Register a callback that is invoked when the transaction has propagated across the network.
        // This shows a second style of registering ListenableFuture callbacks, it works when you don't
        // need access to the object the future returns.
        sendResult.broadcastComplete.addListener(new Runnable() {
            @Override
            public void run() {
                // The wallet has changed now, it'll get auto saved shortly or when the app shuts down.
                System.out.println("Sent coins onwards! Transaction hash is " + sendResult.tx.getHashAsString());
            }
        }, MoreExecutors.sameThreadExecutor());
    } catch (KeyCrypterException | InsufficientMoneyException e) {
        // We don't use encrypted wallets in this example - can never happen.
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: Network.java    From cate with MIT License 6 votes vote down vote up
/**
 * Queue a request to decrypt this wallet. This returns immediately, as the
 * actual work is done on the network thread in order to ensure the thread
 * context is correct. Unhandled errors are reported back to Network.
 *
 * @param password password to decrypt the wallet with
 * @param onSuccess callback on success
 * @param onWalletNotEncrypted callback if the wallet is not encrypted
 * @param onCrypterError callback in case of an error in the key crypter
 * @param timeout timeout on queueing the work request
 * @param timeUnit time unit for the timeout
 */
public void decrypt(String password, Consumer<Object> onSuccess,
        Consumer<Object> onWalletNotEncrypted,
        Consumer<KeyCrypterException> onCrypterError,
        final long timeout, final TimeUnit timeUnit) {
    this.networkExecutor.execute((Runnable) () -> {
        final Wallet wallet = wallet();
        if (!wallet.isEncrypted()) {
            onCrypterError.accept(null);
        } else {
            final KeyCrypter keyCrypter = wallet().getKeyCrypter();

            if (keyCrypter == null) {
                throw new IllegalStateException("Wallet is encrypted but has no key crypter.");
            } else {
                try {
                    wallet().decrypt(keyCrypter.deriveKey(password));
                    encrypted.set(false);
                    onSuccess.accept(null);
                } catch (KeyCrypterException ex) {
                    onCrypterError.accept(ex);
                }
            }
        }
    });
}
 
Example #4
Source File: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void cannotImportEncryptedKey() {
    final ECKey key1 = new ECKey();
    chain.importKeys(ImmutableList.of(key1));
    chain = chain.toEncrypted("foobar");
    ECKey encryptedKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertTrue(encryptedKey.isEncrypted());

    BasicKeyChain chain2 = new BasicKeyChain();
    chain2.importKeys(ImmutableList.of(encryptedKey));
}
 
Example #5
Source File: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void cannotMixParams() throws Exception {
    chain = chain.toEncrypted("foobar");
    KeyCrypterScrypt scrypter = new KeyCrypterScrypt(2);    // Some bogus params.
    ECKey key1 = new ECKey().encrypt(scrypter, scrypter.deriveKey("other stuff"));
    chain.importKeys(key1);
}
 
Example #6
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void cannotImportEncryptedKey() {
    final ECKey key1 = new ECKey();
    chain.importKeys(ImmutableList.of(key1));
    chain = chain.toEncrypted("foobar");
    ECKey encryptedKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertTrue(encryptedKey.isEncrypted());

    BasicKeyChain chain2 = new BasicKeyChain();
    chain2.importKeys(ImmutableList.of(encryptedKey));
}
 
Example #7
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void cannotMixParams() throws Exception {
    chain = chain.toEncrypted("foobar");
    KeyCrypterScrypt scrypter = new KeyCrypterScrypt(2);    // Some bogus params.
    ECKey key1 = new ECKey().encrypt(scrypter, scrypter.deriveKey("other stuff"));
    chain.importKeys(key1);
}
 
Example #8
Source File: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void cannotImportEncryptedKey() {
    final ECKey key1 = new ECKey();
    chain.importKeys(ImmutableList.of(key1));
    chain = chain.toEncrypted("foobar");
    ECKey encryptedKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
    assertTrue(encryptedKey.isEncrypted());

    BasicKeyChain chain2 = new BasicKeyChain();
    chain2.importKeys(ImmutableList.of(encryptedKey));
}
 
Example #9
Source File: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = KeyCrypterException.class)
public void cannotMixParams() throws Exception {
    chain = chain.toEncrypted("foobar");
    KeyCrypterScrypt scrypter = new KeyCrypterScrypt(2);    // Some bogus params.
    ECKey key1 = new ECKey().encrypt(scrypter, scrypter.deriveKey("other stuff"));
    chain.importKeys(key1);
}