use of com.mobilecoin.lib.exceptions.NetworkException in project Signal-Android by WhisperSystems.
the class Wallet method sendPayment.
@WorkerThread
private void sendPayment(@NonNull MobileCoinPublicAddress to, @NonNull Money.MobileCoin amount, @NonNull Money.MobileCoin totalFee, boolean defragmentFirst, @NonNull List<TransactionSubmissionResult> results) {
Money.MobileCoin defragmentFees = Money.MobileCoin.ZERO;
if (defragmentFirst) {
try {
defragmentFees = defragment(amount, results);
} catch (InsufficientFundsException e) {
Log.w(TAG, "Insufficient funds", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.INSUFFICIENT_FUNDS, true));
return;
} catch (TimeoutException | InvalidTransactionException | InvalidFogResponse | AttestationException | TransactionBuilderException | NetworkException | FogReportException e) {
Log.w(TAG, "Defragment failed", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, true));
return;
}
}
Money.MobileCoin feeMobileCoin = totalFee.subtract(defragmentFees).requireMobileCoin();
BigInteger picoMob = amount.requireMobileCoin().toPicoMobBigInteger();
PendingTransaction pendingTransaction = null;
Log.i(TAG, String.format("Total fee advised: %s\nDefrag fees: %s\nTransaction fee: %s", totalFee, defragmentFees, feeMobileCoin));
if (!feeMobileCoin.isPositive()) {
Log.i(TAG, "No fee left after defrag");
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, false));
return;
}
try {
pendingTransaction = mobileCoinClient.prepareTransaction(to.getAddress(), picoMob, feeMobileCoin.toPicoMobBigInteger());
} catch (InsufficientFundsException e) {
Log.w(TAG, "Insufficient funds", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.INSUFFICIENT_FUNDS, false));
} catch (FeeRejectedException e) {
Log.w(TAG, "Fee rejected " + totalFee, e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, false));
} catch (InvalidFogResponse | FogReportException e) {
Log.w(TAG, "Invalid fog response", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, false));
} catch (FragmentedAccountException e) {
if (defragmentFirst) {
Log.w(TAG, "Account is fragmented, but already tried to defragment", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, false));
} else {
Log.i(TAG, "Account is fragmented, defragmenting and retrying");
sendPayment(to, amount, totalFee, true, results);
}
} catch (AttestationException e) {
Log.w(TAG, "Attestation problem", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, false));
} catch (NetworkException e) {
Log.w(TAG, "Network problem", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, false));
} catch (TransactionBuilderException e) {
Log.w(TAG, "Builder problem", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, false));
}
if (pendingTransaction == null) {
Log.w(TAG, "Failed to create pending transaction");
return;
}
try {
Log.i(TAG, "Submitting transaction");
mobileCoinClient.submitTransaction(pendingTransaction.getTransaction());
Log.i(TAG, "Transaction submitted");
results.add(TransactionSubmissionResult.successfullySubmitted(new PaymentTransactionId.MobileCoin(pendingTransaction.getTransaction().toByteArray(), pendingTransaction.getReceipt().toByteArray(), feeMobileCoin)));
} catch (NetworkException e) {
Log.w(TAG, "Network problem", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.NETWORK_FAILURE, false));
} catch (InvalidTransactionException e) {
Log.w(TAG, "Invalid transaction", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, false));
} catch (AttestationException e) {
Log.w(TAG, "Attestation problem", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, false));
} catch (SerializationException e) {
Log.w(TAG, "Serialization problem", e);
results.add(TransactionSubmissionResult.failure(TransactionSubmissionResult.ErrorCode.GENERIC_FAILURE, false));
}
}
use of com.mobilecoin.lib.exceptions.NetworkException in project Signal-Android by WhisperSystems.
the class Wallet method getFullLedger.
@WorkerThread
@NonNull
private MobileCoinLedgerWrapper getFullLedger(boolean retryOnAuthFailure) {
PaymentsValues paymentsValues = SignalStore.paymentsValues();
try {
MobileCoinLedgerWrapper ledger = tryGetFullLedger(null);
paymentsValues.setMobileCoinFullLedger(Objects.requireNonNull(ledger));
} catch (IOException e) {
if ((retryOnAuthFailure && e.getCause() instanceof NetworkException) && (((NetworkException) e.getCause()).statusCode == 401)) {
Log.w(TAG, "Failed to get up to date ledger, due to temp auth failure, retrying", e);
return getFullLedger(false);
} else {
Log.w(TAG, "Failed to get up to date ledger", e);
}
}
return getCachedLedger();
}
use of com.mobilecoin.lib.exceptions.NetworkException in project Signal-Android by WhisperSystems.
the class Wallet method tryGetFullLedger.
@WorkerThread
@Nullable
public MobileCoinLedgerWrapper tryGetFullLedger(@Nullable Long minimumBlockIndex) throws IOException {
try {
MobileCoinLedger.Builder builder = MobileCoinLedger.newBuilder();
BigInteger totalUnspent = BigInteger.ZERO;
long highestBlockTimeStamp = 0;
UnsignedLong highestBlockIndex = UnsignedLong.ZERO;
final long asOfTimestamp = System.currentTimeMillis();
AccountSnapshot accountSnapshot = mobileCoinClient.getAccountSnapshot();
final BigInteger minimumTxFee = mobileCoinClient.getOrFetchMinimumTxFee();
if (minimumBlockIndex != null) {
long snapshotBlockIndex = accountSnapshot.getBlockIndex().longValue();
if (snapshotBlockIndex < minimumBlockIndex) {
Log.d(TAG, "Waiting for block index");
return null;
}
}
for (OwnedTxOut txOut : accountSnapshot.getAccountActivity().getAllTxOuts()) {
MobileCoinLedger.OwnedTXO.Builder txoBuilder = MobileCoinLedger.OwnedTXO.newBuilder().setAmount(Uint64Util.bigIntegerToUInt64(txOut.getValue())).setReceivedInBlock(getBlock(txOut.getReceivedBlockIndex(), txOut.getReceivedBlockTimestamp())).setKeyImage(ByteString.copyFrom(txOut.getKeyImage().getData())).setPublicKey(ByteString.copyFrom(txOut.getPublicKey().getKeyBytes()));
if (txOut.getSpentBlockIndex() != null && (minimumBlockIndex == null || txOut.isSpent(UnsignedLong.valueOf(minimumBlockIndex)))) {
txoBuilder.setSpentInBlock(getBlock(txOut.getSpentBlockIndex(), txOut.getSpentBlockTimestamp()));
builder.addSpentTxos(txoBuilder);
} else {
totalUnspent = totalUnspent.add(txOut.getValue());
builder.addUnspentTxos(txoBuilder);
}
if (txOut.getSpentBlockIndex() != null && txOut.getSpentBlockIndex().compareTo(highestBlockIndex) > 0) {
highestBlockIndex = txOut.getSpentBlockIndex();
}
if (txOut.getReceivedBlockIndex().compareTo(highestBlockIndex) > 0) {
highestBlockIndex = txOut.getReceivedBlockIndex();
}
if (txOut.getSpentBlockTimestamp() != null && txOut.getSpentBlockTimestamp().getTime() > highestBlockTimeStamp) {
highestBlockTimeStamp = txOut.getSpentBlockTimestamp().getTime();
}
if (txOut.getReceivedBlockTimestamp() != null && txOut.getReceivedBlockTimestamp().getTime() > highestBlockTimeStamp) {
highestBlockTimeStamp = txOut.getReceivedBlockTimestamp().getTime();
}
}
builder.setBalance(Uint64Util.bigIntegerToUInt64(totalUnspent)).setTransferableBalance(Uint64Util.bigIntegerToUInt64(accountSnapshot.getTransferableAmount(minimumTxFee))).setAsOfTimeStamp(asOfTimestamp).setHighestBlock(MobileCoinLedger.Block.newBuilder().setBlockNumber(highestBlockIndex.longValue()).setTimestamp(highestBlockTimeStamp));
return new MobileCoinLedgerWrapper(builder.build());
} catch (InvalidFogResponse e) {
Log.w(TAG, "Problem getting ledger", e);
throw new IOException(e);
} catch (NetworkException e) {
Log.w(TAG, "Network problem getting ledger", e);
if (e.statusCode == 401) {
Log.d(TAG, "Reauthorizing client");
reauthorizeClient();
}
throw new IOException(e);
} catch (AttestationException e) {
Log.w(TAG, "Attestation problem getting ledger", e);
throw new IOException(e);
} catch (Uint64RangeException e) {
throw new AssertionError(e);
}
}
use of com.mobilecoin.lib.exceptions.NetworkException in project Signal-Android by WhisperSystems.
the class Wallet method getReceivedTransactionStatus.
@WorkerThread
@NonNull
public ReceivedTransactionStatus getReceivedTransactionStatus(@NonNull byte[] receiptBytes) throws IOException {
try {
Receipt receipt = Receipt.fromBytes(receiptBytes);
Receipt.Status status = mobileCoinClient.getReceiptStatus(receipt);
switch(status) {
case UNKNOWN:
Log.w(TAG, "Unknown received Transaction Status");
return ReceivedTransactionStatus.inProgress();
case FAILED:
return ReceivedTransactionStatus.failed();
case RECEIVED:
BigInteger amount = receipt.getAmount(account);
return ReceivedTransactionStatus.complete(Money.picoMobileCoin(amount), status.getBlockIndex().longValue());
default:
throw new IllegalStateException("Unknown Transaction Status: " + status);
}
} catch (SerializationException | InvalidFogResponse | InvalidReceiptException e) {
Log.w(TAG, e);
return ReceivedTransactionStatus.failed();
} catch (NetworkException | AttestationException e) {
throw new IOException(e);
} catch (AmountDecoderException e) {
Log.w(TAG, "Failed to decode amount", e);
return ReceivedTransactionStatus.failed();
}
}
use of com.mobilecoin.lib.exceptions.NetworkException in project Signal-Android by WhisperSystems.
the class Wallet method getSentTransactionStatus.
@WorkerThread
@NonNull
public TransactionStatusResult getSentTransactionStatus(@NonNull PaymentTransactionId transactionId) throws IOException {
try {
PaymentTransactionId.MobileCoin mobcoinTransaction = (PaymentTransactionId.MobileCoin) transactionId;
Transaction transaction = Transaction.fromBytes(mobcoinTransaction.getTransaction());
Transaction.Status status = mobileCoinClient.getAccountSnapshot().getTransactionStatus(transaction);
switch(status) {
case UNKNOWN:
Log.w(TAG, "Unknown sent Transaction Status");
return TransactionStatusResult.inProgress();
case FAILED:
return TransactionStatusResult.failed();
case ACCEPTED:
return TransactionStatusResult.complete(status.getBlockIndex().longValue());
default:
throw new IllegalStateException("Unknown Transaction Status: " + status);
}
} catch (SerializationException | InvalidFogResponse e) {
Log.w(TAG, e);
return TransactionStatusResult.failed();
} catch (NetworkException | AttestationException e) {
Log.w(TAG, e);
throw new IOException(e);
}
}
Aggregations