Search in sources :

Example 1 with KeyParameter

use of org.spongycastle.crypto.params.KeyParameter in project bitsquare by bitsquare.

the class WalletService method getSendRequestForMultipleAddresses.

private Wallet.SendRequest getSendRequestForMultipleAddresses(Set<String> fromAddresses, String toAddress, Coin amount, @Nullable String changeAddress, @Nullable KeyParameter aesKey) throws AddressFormatException, AddressEntryException, InsufficientMoneyException {
    Transaction tx = new Transaction(params);
    Preconditions.checkArgument(Restrictions.isAboveDust(amount), "The amount is too low (dust limit).");
    tx.addOutput(amount, new Address(params, toAddress));
    Wallet.SendRequest sendRequest = Wallet.SendRequest.forTx(tx);
    sendRequest.aesKey = aesKey;
    sendRequest.shuffleOutputs = false;
    Set<AddressEntry> addressEntries = fromAddresses.stream().map(address -> {
        Optional<AddressEntry> addressEntryOptional = findAddressEntry(address, AddressEntry.Context.AVAILABLE);
        if (!addressEntryOptional.isPresent())
            addressEntryOptional = findAddressEntry(address, AddressEntry.Context.OFFER_FUNDING);
        if (!addressEntryOptional.isPresent())
            addressEntryOptional = findAddressEntry(address, AddressEntry.Context.TRADE_PAYOUT);
        if (!addressEntryOptional.isPresent())
            addressEntryOptional = findAddressEntry(address, AddressEntry.Context.ARBITRATOR);
        return addressEntryOptional;
    }).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
    if (addressEntries.isEmpty())
        throw new AddressEntryException("No Addresses for withdraw  found in our wallet");
    sendRequest.coinSelector = new MultiAddressesCoinSelector(params, addressEntries);
    Optional<AddressEntry> addressEntryOptional = Optional.empty();
    AddressEntry changeAddressAddressEntry = null;
    if (changeAddress != null)
        addressEntryOptional = findAddressEntry(changeAddress, AddressEntry.Context.AVAILABLE);
    if (addressEntryOptional.isPresent()) {
        changeAddressAddressEntry = addressEntryOptional.get();
    } else {
        ArrayList<AddressEntry> list = new ArrayList<>(addressEntries);
        if (!list.isEmpty())
            changeAddressAddressEntry = list.get(0);
    }
    checkNotNull(changeAddressAddressEntry, "change address must not be null");
    sendRequest.changeAddress = changeAddressAddressEntry.getAddress();
    sendRequest.feePerKb = FeePolicy.getNonTradeFeePerKb();
    return sendRequest;
}
Also used : RegTestParams(org.bitcoinj.params.RegTestParams) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) Socks5ProxyProvider(io.bitsquare.network.Socks5ProxyProvider) StringUtils(org.apache.commons.lang3.StringUtils) InetAddress(java.net.InetAddress) Socks5Proxy(com.runjva.sourceforge.jsocks.protocol.Socks5Proxy) ResultHandler(io.bitsquare.common.handlers.ResultHandler) KeyCrypterScrypt(org.bitcoinj.crypto.KeyCrypterScrypt) KeyParameter(org.spongycastle.crypto.params.KeyParameter) DeterministicKey(org.bitcoinj.crypto.DeterministicKey) FileUtil(io.bitsquare.storage.FileUtil) DnsLookupTor(io.bitsquare.network.DnsLookupTor) TxConfidenceListener(io.bitsquare.btc.listeners.TxConfidenceListener) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Preferences(io.bitsquare.user.Preferences) DeterministicSeed(org.bitcoinj.wallet.DeterministicSeed) MainNetParams(org.bitcoinj.params.MainNetParams) NotNull(org.jetbrains.annotations.NotNull) java.util(java.util) ErrorMessageHandler(io.bitsquare.common.handlers.ErrorMessageHandler) Timer(io.bitsquare.common.Timer) BalanceListener(io.bitsquare.btc.listeners.BalanceListener) TestNet3Params(org.bitcoinj.params.TestNet3Params) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) Named(javax.inject.Named) Socks5MultiDiscovery(io.bitsquare.network.Socks5MultiDiscovery) AddressConfidenceListener(io.bitsquare.btc.listeners.AddressConfidenceListener) ExceptionHandler(io.bitsquare.common.handlers.ExceptionHandler) Nullable(javax.annotation.Nullable) Log(io.bitsquare.app.Log) javafx.beans.property(javafx.beans.property) Logger(org.slf4j.Logger) UserThread(io.bitsquare.common.UserThread) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FutureCallback(com.google.common.util.concurrent.FutureCallback) File(java.io.File) Service(com.google.common.util.concurrent.Service) org.bitcoinj.core(org.bitcoinj.core) Threading(org.bitcoinj.utils.Threading) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) Paths(java.nio.file.Paths) Preconditions(com.google.common.base.Preconditions) Storage(io.bitsquare.storage.Storage) NetworkOptionKeys(io.bitsquare.network.NetworkOptionKeys) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 2 with KeyParameter

use of org.spongycastle.crypto.params.KeyParameter in project conceal by facebook.

the class BouncyCastleHelper method bouncyCastleEncrypt.

public static Result bouncyCastleEncrypt(byte[] data, byte[] key, byte[] iv, byte[] aadData) throws UnsupportedEncodingException, InvalidCipherTextException {
    GCMBlockCipher gcm = new GCMBlockCipher(new AESEngine());
    byte[] gcmOut = new byte[CryptoTestUtils.NUM_DATA_BYTES + CryptoConfig.KEY_128.tagLength];
    KeyParameter keyParameter = new KeyParameter(key);
    // Add aad data.
    AEADParameters params = new AEADParameters(keyParameter, CryptoConfig.KEY_128.tagLength * 8, iv, aadData);
    // Init encryption.
    gcm.init(true, params);
    int written = gcm.processBytes(data, 0, data.length, gcmOut, 0);
    written += gcm.doFinal(gcmOut, written);
    byte[] bouncyCastleOut = Arrays.copyOfRange(gcmOut, 0, written);
    byte[] cipherText = Arrays.copyOfRange(bouncyCastleOut, 0, CryptoTestUtils.NUM_DATA_BYTES);
    byte[] tag = Arrays.copyOfRange(bouncyCastleOut, CryptoTestUtils.NUM_DATA_BYTES, bouncyCastleOut.length);
    return new Result(cipherText, tag);
}
Also used : AESEngine(org.spongycastle.crypto.engines.AESEngine) AEADParameters(org.spongycastle.crypto.params.AEADParameters) KeyParameter(org.spongycastle.crypto.params.KeyParameter) GCMBlockCipher(org.spongycastle.crypto.modes.GCMBlockCipher)

Example 3 with KeyParameter

use of org.spongycastle.crypto.params.KeyParameter in project bitsquare by bitsquare.

the class ScryptUtil method deriveKeyWithScrypt.

public static void deriveKeyWithScrypt(KeyCrypterScrypt keyCrypterScrypt, String password, DeriveKeyResultHandler resultHandler) {
    Utilities.getThreadPoolExecutor("ScryptUtil:deriveKeyWithScrypt-%d", 1, 2, 5L).submit(() -> {
        try {
            log.debug("Doing key derivation");
            long start = System.currentTimeMillis();
            KeyParameter aesKey = keyCrypterScrypt.deriveKey(password);
            long duration = System.currentTimeMillis() - start;
            log.debug("Key derivation took {} msec", duration);
            UserThread.execute(() -> {
                try {
                    resultHandler.handleResult(aesKey);
                } catch (Throwable t) {
                    t.printStackTrace();
                    log.error("Executing task failed. " + t.getMessage());
                }
            });
        } catch (Throwable t) {
            t.printStackTrace();
            log.error("Executing task failed. " + t.getMessage());
        }
    });
}
Also used : KeyParameter(org.spongycastle.crypto.params.KeyParameter)

Aggregations

KeyParameter (org.spongycastle.crypto.params.KeyParameter)2 Preconditions (com.google.common.base.Preconditions)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 Service (com.google.common.util.concurrent.Service)1 Socks5Proxy (com.runjva.sourceforge.jsocks.protocol.Socks5Proxy)1 Log (io.bitsquare.app.Log)1 AddressConfidenceListener (io.bitsquare.btc.listeners.AddressConfidenceListener)1 BalanceListener (io.bitsquare.btc.listeners.BalanceListener)1 TxConfidenceListener (io.bitsquare.btc.listeners.TxConfidenceListener)1 Timer (io.bitsquare.common.Timer)1 UserThread (io.bitsquare.common.UserThread)1 ErrorMessageHandler (io.bitsquare.common.handlers.ErrorMessageHandler)1 ExceptionHandler (io.bitsquare.common.handlers.ExceptionHandler)1 ResultHandler (io.bitsquare.common.handlers.ResultHandler)1 DnsLookupTor (io.bitsquare.network.DnsLookupTor)1 NetworkOptionKeys (io.bitsquare.network.NetworkOptionKeys)1 Socks5MultiDiscovery (io.bitsquare.network.Socks5MultiDiscovery)1