Search in sources :

Example 1 with UnsignedLong

use of com.mobilecoin.lib.UnsignedLong 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)

Aggregations

Nullable (androidx.annotation.Nullable)1 WorkerThread (androidx.annotation.WorkerThread)1 AccountSnapshot (com.mobilecoin.lib.AccountSnapshot)1 OwnedTxOut (com.mobilecoin.lib.OwnedTxOut)1 UnsignedLong (com.mobilecoin.lib.UnsignedLong)1 AttestationException (com.mobilecoin.lib.exceptions.AttestationException)1 InvalidFogResponse (com.mobilecoin.lib.exceptions.InvalidFogResponse)1 NetworkException (com.mobilecoin.lib.exceptions.NetworkException)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 MobileCoinLedger (org.thoughtcrime.securesms.payments.proto.MobileCoinLedger)1 Uint64RangeException (org.whispersystems.signalservice.api.util.Uint64RangeException)1