Search in sources :

Example 6 with WalletHistoryStatusEvent

use of com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent in project sparrow by sparrowwallet.

the class BatchedElectrumServerRpc method subscribeScriptHashes.

@Override
@SuppressWarnings("unchecked")
public Map<String, String> subscribeScriptHashes(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes) {
    PagedBatchRequestBuilder<String, String> batchRequest = PagedBatchRequestBuilder.create(transport, idCounter).keysType(String.class).returnType(String.class);
    EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Finding transactions for " + nodeRangesToString(pathScriptHashes.keySet())));
    for (String path : pathScriptHashes.keySet()) {
        batchRequest.add(path, "blockchain.scripthash.subscribe", pathScriptHashes.get(path));
    }
    try {
        return batchRequest.execute();
    } catch (JsonRpcBatchException e) {
        // Even if we have some successes, failure to subscribe for all script hashes will result in outdated wallet view. Don't proceed.
        throw new ElectrumServerRpcException("Failed to subscribe to paths: " + nodeRangesToString((Collection<String>) e.getErrors().keySet()), e);
    } catch (Exception e) {
        throw new ElectrumServerRpcException("Failed to subscribe to paths: " + nodeRangesToString(pathScriptHashes.keySet()), e);
    }
}
Also used : WalletNode.nodeRangesToString(com.sparrowwallet.drongo.wallet.WalletNode.nodeRangesToString) JsonRpcBatchException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException) WalletHistoryStatusEvent(com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException) JsonRpcBatchException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException)

Example 7 with WalletHistoryStatusEvent

use of com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent in project sparrow by sparrowwallet.

the class SimpleElectrumServerRpc method getTransactions.

@Override
public Map<String, String> getTransactions(Transport transport, Wallet wallet, Set<String> txids) {
    JsonRpcClient client = new JsonRpcClient(transport);
    Map<String, String> result = new LinkedHashMap<>();
    for (String txid : txids) {
        EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Retrieving transaction [" + txid.substring(0, 6) + "]"));
        try {
            String rawTxHex = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> client.createRequest().returnAs(String.class).method("blockchain.transaction.get").id(idCounter.incrementAndGet()).params(txid).execute());
            result.put(txid, rawTxHex);
        } catch (ServerException e) {
            // If there is an error with the server connection, don't keep trying - this may take too long given many txids
            throw new ElectrumServerRpcException("Failed to retrieve transaction for txid [" + txid.substring(0, 6) + "]", e);
        } catch (Exception e) {
            result.put(txid, Sha256Hash.ZERO_HASH.toString());
        }
    }
    return result;
}
Also used : WalletHistoryStatusEvent(com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent) JsonRpcClient(com.github.arteam.simplejsonrpc.client.JsonRpcClient) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException)

Example 8 with WalletHistoryStatusEvent

use of com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent in project sparrow by sparrowwallet.

the class SimpleElectrumServerRpc method subscribeScriptHashes.

@Override
public Map<String, String> subscribeScriptHashes(Transport transport, Wallet wallet, Map<String, String> pathScriptHashes) {
    JsonRpcClient client = new JsonRpcClient(transport);
    Map<String, String> result = new LinkedHashMap<>();
    for (String path : pathScriptHashes.keySet()) {
        EventManager.get().post(new WalletHistoryStatusEvent(wallet, true, "Finding transactions for " + path));
        try {
            String scriptHash = new RetryLogic<String>(MAX_RETRIES, RETRY_DELAY, List.of(IllegalStateException.class, IllegalArgumentException.class)).getResult(() -> client.createRequest().returnAs(String.class).method("blockchain.scripthash.subscribe").id(path + "-" + idCounter.incrementAndGet()).params(pathScriptHashes.get(path)).executeNullable());
            result.put(path, scriptHash);
        } catch (Exception e) {
            // Even if we have some successes, failure to subscribe for all script hashes will result in outdated wallet view. Don't proceed.
            throw new ElectrumServerRpcException("Failed to subscribe to path: " + path, e);
        }
    }
    return result;
}
Also used : WalletHistoryStatusEvent(com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent) JsonRpcClient(com.github.arteam.simplejsonrpc.client.JsonRpcClient) JsonRpcException(com.github.arteam.simplejsonrpc.client.exception.JsonRpcException)

Aggregations

JsonRpcException (com.github.arteam.simplejsonrpc.client.exception.JsonRpcException)8 WalletHistoryStatusEvent (com.sparrowwallet.sparrow.event.WalletHistoryStatusEvent)8 JsonRpcClient (com.github.arteam.simplejsonrpc.client.JsonRpcClient)4 JsonRpcBatchException (com.github.arteam.simplejsonrpc.client.exception.JsonRpcBatchException)4 WalletNode.nodeRangesToString (com.sparrowwallet.drongo.wallet.WalletNode.nodeRangesToString)4 BigInteger (java.math.BigInteger)2