Search in sources :

Example 1 with AttestationException

use of com.mobilecoin.lib.exceptions.AttestationException 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));
    }
}
Also used : PendingTransaction(com.mobilecoin.lib.PendingTransaction) AttestationException(com.mobilecoin.lib.exceptions.AttestationException) FogReportException(com.mobilecoin.lib.exceptions.FogReportException) SerializationException(com.mobilecoin.lib.exceptions.SerializationException) FeeRejectedException(com.mobilecoin.lib.exceptions.FeeRejectedException) InvalidTransactionException(com.mobilecoin.lib.exceptions.InvalidTransactionException) InvalidFogResponse(com.mobilecoin.lib.exceptions.InvalidFogResponse) TransactionBuilderException(com.mobilecoin.lib.exceptions.TransactionBuilderException) Money(org.whispersystems.signalservice.api.payments.Money) FragmentedAccountException(com.mobilecoin.lib.exceptions.FragmentedAccountException) InsufficientFundsException(com.mobilecoin.lib.exceptions.InsufficientFundsException) BigInteger(java.math.BigInteger) NetworkException(com.mobilecoin.lib.exceptions.NetworkException) TimeoutException(java.util.concurrent.TimeoutException) WorkerThread(androidx.annotation.WorkerThread)

Example 2 with AttestationException

use of com.mobilecoin.lib.exceptions.AttestationException in project Signal-Android by WhisperSystems.

the class MobileCoinTestNetConfig method getConfig.

@Override
@NonNull
ClientConfig getConfig() {
    try {
        byte[] mrEnclaveConsensus = Hex.toByteArray("9659ea738275b3999bf1700398b60281be03af5cb399738a89b49ea2496595af");
        byte[] mrEnclaveReport = Hex.toByteArray("a4764346f91979b4906d4ce26102228efe3aba39216dec1e7d22e6b06f919f11");
        byte[] mrEnclaveLedger = Hex.toByteArray("768f7bea6171fb83d775ee8485e4b5fcebf5f664ca7e8b9ceef9c7c21e9d9bf3");
        byte[] mrEnclaveView = Hex.toByteArray("e154f108c7758b5aa7161c3824c176f0c20f63012463bf3cc5651e678f02fb9e");
        byte[] mrSigner = Hex.toByteArray("bf7fa957a6a94acb588851bc8767e0ca57706c79f4fc2aa6bcb993012c3c386c");
        Set<X509Certificate> trustRoots = getTrustRoots(R.raw.signal_mobilecoin_authority);
        ClientConfig config = new ClientConfig();
        String[] hardeningAdvisories = { "INTEL-SA-00334" };
        config.logAdapter = new MobileCoinLogAdapter();
        config.fogView = new ClientConfig.Service().withTrustRoots(trustRoots).withVerifier(new Verifier().withMrEnclave(mrEnclaveView, null, hardeningAdvisories).withMrSigner(mrSigner, FOG_VIEW_PRODUCT_ID, SECURITY_VERSION, null, hardeningAdvisories));
        config.fogLedger = new ClientConfig.Service().withTrustRoots(trustRoots).withVerifier(new Verifier().withMrEnclave(mrEnclaveLedger, null, hardeningAdvisories).withMrSigner(mrSigner, FOG_LEDGER_PRODUCT_ID, SECURITY_VERSION, null, hardeningAdvisories));
        config.consensus = new ClientConfig.Service().withTrustRoots(trustRoots).withVerifier(new Verifier().withMrEnclave(mrEnclaveConsensus, null, hardeningAdvisories).withMrSigner(mrSigner, CONSENSUS_PRODUCT_ID, SECURITY_VERSION, null, hardeningAdvisories));
        config.report = new ClientConfig.Service().withVerifier(new Verifier().withMrEnclave(mrEnclaveReport, null, hardeningAdvisories).withMrSigner(mrSigner, FOG_REPORT_PRODUCT_ID, SECURITY_VERSION, null, hardeningAdvisories));
        return config;
    } catch (AttestationException ex) {
        throw new IllegalStateException();
    }
}
Also used : AttestationException(com.mobilecoin.lib.exceptions.AttestationException) ClientConfig(com.mobilecoin.lib.ClientConfig) Verifier(com.mobilecoin.lib.Verifier) X509Certificate(java.security.cert.X509Certificate) NonNull(androidx.annotation.NonNull)

Example 3 with AttestationException

use of com.mobilecoin.lib.exceptions.AttestationException in project Signal-Android by WhisperSystems.

the class MobileCoinMainNetConfig method getConfig.

@Override
@NonNull
ClientConfig getConfig() {
    try {
        byte[] mrEnclaveConsensus = Hex.toByteArray("e66db38b8a43a33f6c1610d335a361963bb2b31e056af0dc0a895ac6c857cab9");
        byte[] mrEnclaveConsensusNew = Hex.toByteArray("653228afd2b02a6c28f1dc3b108b1dfa457d170b32ae8ec2978f941bd1655c83");
        byte[] mrEnclaveReport = Hex.toByteArray("709ab90621e3a8d9eb26ed9e2830e091beceebd55fb01c5d7c31d27e83b9b0d1");
        byte[] mrEnclaveReportNew = Hex.toByteArray("f3f7e9a674c55fb2af543513527b6a7872de305bac171783f6716a0bf6919499");
        byte[] mrEnclaveLedger = Hex.toByteArray("511eab36de691ded50eb08b173304194da8b9d86bfdd7102001fe6bb279c3666");
        byte[] mrEnclaveLedgerNew = Hex.toByteArray("89db0d1684fcc98258295c39f4ab68f7de5917ef30f0004d9a86f29930cebbbd");
        byte[] mrEnclaveView = Hex.toByteArray("ddd59da874fdf3239d5edb1ef251df07a8728c9ef63057dd0b50ade5a9ddb041");
        byte[] mrEnclaveViewNew = Hex.toByteArray("dd84abda7f05116e21fcd1ee6361b0ec29445fff0472131eaf37bf06255b567a");
        Set<X509Certificate> trustRoots = getTrustRoots(R.raw.signal_mobilecoin_authority);
        ClientConfig config = new ClientConfig();
        String[] hardeningAdvisories = { "INTEL-SA-00334" };
        config.logAdapter = new MobileCoinLogAdapter();
        config.fogView = new ClientConfig.Service().withTrustRoots(trustRoots).withVerifier(new Verifier().withMrEnclave(mrEnclaveView, null, hardeningAdvisories).withMrEnclave(mrEnclaveViewNew, null, hardeningAdvisories));
        config.fogLedger = new ClientConfig.Service().withTrustRoots(trustRoots).withVerifier(new Verifier().withMrEnclave(mrEnclaveLedger, null, hardeningAdvisories).withMrEnclave(mrEnclaveLedgerNew, null, hardeningAdvisories));
        config.consensus = new ClientConfig.Service().withTrustRoots(trustRoots).withVerifier(new Verifier().withMrEnclave(mrEnclaveConsensus, null, hardeningAdvisories).withMrEnclave(mrEnclaveConsensusNew, null, hardeningAdvisories));
        config.report = new ClientConfig.Service().withVerifier(new Verifier().withMrEnclave(mrEnclaveReport, null, hardeningAdvisories).withMrEnclave(mrEnclaveReportNew, null, hardeningAdvisories));
        return config;
    } catch (AttestationException ex) {
        throw new IllegalStateException();
    }
}
Also used : AttestationException(com.mobilecoin.lib.exceptions.AttestationException) ClientConfig(com.mobilecoin.lib.ClientConfig) Verifier(com.mobilecoin.lib.Verifier) X509Certificate(java.security.cert.X509Certificate) NonNull(androidx.annotation.NonNull)

Example 4 with AttestationException

use of com.mobilecoin.lib.exceptions.AttestationException 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);
    }
}
Also used : AttestationException(com.mobilecoin.lib.exceptions.AttestationException) UnsignedLong(com.mobilecoin.lib.UnsignedLong) AccountSnapshot(com.mobilecoin.lib.AccountSnapshot) OwnedTxOut(com.mobilecoin.lib.OwnedTxOut) IOException(java.io.IOException) Uint64RangeException(org.whispersystems.signalservice.api.util.Uint64RangeException) MobileCoinLedger(org.thoughtcrime.securesms.payments.proto.MobileCoinLedger) InvalidFogResponse(com.mobilecoin.lib.exceptions.InvalidFogResponse) BigInteger(java.math.BigInteger) NetworkException(com.mobilecoin.lib.exceptions.NetworkException) WorkerThread(androidx.annotation.WorkerThread) Nullable(androidx.annotation.Nullable)

Example 5 with AttestationException

use of com.mobilecoin.lib.exceptions.AttestationException 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();
    }
}
Also used : AmountDecoderException(com.mobilecoin.lib.exceptions.AmountDecoderException) Receipt(com.mobilecoin.lib.Receipt) SerializationException(com.mobilecoin.lib.exceptions.SerializationException) AttestationException(com.mobilecoin.lib.exceptions.AttestationException) IOException(java.io.IOException) InvalidFogResponse(com.mobilecoin.lib.exceptions.InvalidFogResponse) BigInteger(java.math.BigInteger) InvalidReceiptException(com.mobilecoin.lib.exceptions.InvalidReceiptException) NetworkException(com.mobilecoin.lib.exceptions.NetworkException) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Aggregations

AttestationException (com.mobilecoin.lib.exceptions.AttestationException)6 NonNull (androidx.annotation.NonNull)4 WorkerThread (androidx.annotation.WorkerThread)4 InvalidFogResponse (com.mobilecoin.lib.exceptions.InvalidFogResponse)4 NetworkException (com.mobilecoin.lib.exceptions.NetworkException)4 SerializationException (com.mobilecoin.lib.exceptions.SerializationException)3 IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)3 ClientConfig (com.mobilecoin.lib.ClientConfig)2 PendingTransaction (com.mobilecoin.lib.PendingTransaction)2 Verifier (com.mobilecoin.lib.Verifier)2 X509Certificate (java.security.cert.X509Certificate)2 Nullable (androidx.annotation.Nullable)1 AccountSnapshot (com.mobilecoin.lib.AccountSnapshot)1 OwnedTxOut (com.mobilecoin.lib.OwnedTxOut)1 Receipt (com.mobilecoin.lib.Receipt)1 Transaction (com.mobilecoin.lib.Transaction)1 UnsignedLong (com.mobilecoin.lib.UnsignedLong)1 AmountDecoderException (com.mobilecoin.lib.exceptions.AmountDecoderException)1 FeeRejectedException (com.mobilecoin.lib.exceptions.FeeRejectedException)1