use of org.thoughtcrime.securesms.payments.Payment in project Signal-Android by WhisperSystems.
the class LedgerReconcile method reconcile.
@WorkerThread
@NonNull
private static List<Payment> reconcile(@NonNull Collection<? extends Payment> allLocalPaymentTransactions, @NonNull List<MobileCoinLedgerWrapper.OwnedTxo> allTxOuts) {
List<? extends Payment> nonFailedLocalPayments = Stream.of(allLocalPaymentTransactions).filter(i -> i.getState() != State.FAILED).toList();
Set<ByteString> allKnownPublicKeys = new HashSet<>(nonFailedLocalPayments.size());
Set<ByteString> allKnownKeyImages = new HashSet<>(nonFailedLocalPayments.size());
for (Payment paymentTransaction : nonFailedLocalPayments) {
PaymentMetaData.MobileCoinTxoIdentification txoIdentification = paymentTransaction.getPaymentMetaData().getMobileCoinTxoIdentification();
allKnownPublicKeys.addAll(txoIdentification.getPublicKeyList());
allKnownKeyImages.addAll(txoIdentification.getKeyImagesList());
}
Set<MobileCoinLedgerWrapper.OwnedTxo> knownTxosByKeyImage = Stream.of(allTxOuts).filter(t -> allKnownKeyImages.contains(t.getKeyImage())).collect(Collectors.toSet());
Set<MobileCoinLedgerWrapper.OwnedTxo> knownTxosByPublicKeys = Stream.of(allTxOuts).filter(t -> allKnownPublicKeys.contains(t.getPublicKey())).collect(Collectors.toSet());
// any TXO that we can't pair up the pub key for, we don't have detail for how it got into the account
Set<MobileCoinLedgerWrapper.OwnedTxo> unknownTxOutsReceived = new HashSet<>(allTxOuts);
unknownTxOutsReceived.removeAll(knownTxosByPublicKeys);
// any TXO that we can't pair up the keyimage for, we don't have detail for how it got spent
Set<MobileCoinLedgerWrapper.OwnedTxo> unknownTxOutsSpent = Stream.of(allTxOuts).filter(MobileCoinLedgerWrapper.OwnedTxo::isSpent).collect(Collectors.toSet());
unknownTxOutsSpent.removeAll(knownTxosByKeyImage);
if (unknownTxOutsReceived.isEmpty() && unknownTxOutsSpent.isEmpty()) {
return Stream.of(allLocalPaymentTransactions).map(t -> (Payment) t).toList();
}
List<DetailedTransaction> detailedTransactions = reconstructAllTransactions(unknownTxOutsReceived, unknownTxOutsSpent);
List<Payment> reconstructedPayments = new ArrayList<>(detailedTransactions.size());
List<Payment> blockDecoratedLocalPayments = decoratePaymentsWithBlockIndexes(allLocalPaymentTransactions, allTxOuts);
for (DetailedTransaction detailedTransaction : detailedTransactions) {
reconstructedPayments.add(new ReconstructedPayment(detailedTransaction.blockDetail.getBlockIndex(), detailedTransaction.blockDetail.getBlockTimestampOrZero(), detailedTransaction.transaction.getDirection(), detailedTransaction.transaction.getValue()));
}
Collections.sort(reconstructedPayments, Payment.DESCENDING_BLOCK_INDEX);
return ZipList.zipList(blockDecoratedLocalPayments, reconstructedPayments, Payment.DESCENDING_BLOCK_INDEX_UNKNOWN_FIRST);
}
use of org.thoughtcrime.securesms.payments.Payment in project Signal-Android by WhisperSystems.
the class LedgerReconcile method decoratePaymentsWithBlockIndexes.
private static List<Payment> decoratePaymentsWithBlockIndexes(@NonNull Collection<? extends Payment> localPaymentTransactions, @NonNull List<MobileCoinLedgerWrapper.OwnedTxo> allTxOuts) {
List<Payment> result = new ArrayList<>(localPaymentTransactions.size());
Map<ByteString, MobileCoinLedgerWrapper.OwnedTxo> blockDetailMap = new HashMap<>(allTxOuts.size() * 2);
for (MobileCoinLedgerWrapper.OwnedTxo txo : allTxOuts) {
blockDetailMap.put(txo.getPublicKey(), txo);
blockDetailMap.put(txo.getKeyImage(), txo);
}
for (Payment local : localPaymentTransactions) {
result.add(findBlock(local, blockDetailMap));
}
return result;
}
use of org.thoughtcrime.securesms.payments.Payment in project Signal-Android by WhisperSystems.
the class LedgerReconcileTest method empty_lists.
@Test
public void empty_lists() {
List<Payment> payments = reconcile(Collections.emptyList(), new MobileCoinLedgerWrapper(MobileCoinLedger.getDefaultInstance()));
assertEquals(Collections.emptyList(), payments);
}
use of org.thoughtcrime.securesms.payments.Payment in project Signal-Android by WhisperSystems.
the class LedgerReconcileTest method single_spent_transaction_on_ledger.
@Test
public void single_spent_transaction_on_ledger() {
MobileCoinLedger ledger = ledger(spentTxo(mob(10), keyImage(1), publicKey(2), block(1), block(2)));
List<Payment> payments = reconcile(Collections.emptyList(), new MobileCoinLedgerWrapper(ledger));
assertEquals(2, payments.size());
Payment payment1 = payments.get(0);
assertEquals(mob(10), payment1.getAmount());
assertEquals(mob(-10), payment1.getAmountWithDirection());
assertEquals(Direction.SENT, payment1.getDirection());
assertEquals(mob(0), payment1.getFee());
assertEquals(Payee.UNKNOWN, payment1.getPayee());
assertEquals(State.SUCCESSFUL, payment1.getState());
assertEquals(UuidUtil.UNKNOWN_UUID, payment1.getUuid());
assertEquals("", payment1.getNote());
Payment payment2 = payments.get(1);
assertEquals(mob(10), payment2.getAmount());
assertEquals(mob(10), payment2.getAmountWithDirection());
assertEquals(Direction.RECEIVED, payment2.getDirection());
assertEquals(mob(0), payment2.getFee());
assertEquals(Payee.UNKNOWN, payment2.getPayee());
assertEquals(State.SUCCESSFUL, payment2.getState());
assertEquals(UuidUtil.UNKNOWN_UUID, payment2.getUuid());
assertEquals("", payment2.getNote());
}
use of org.thoughtcrime.securesms.payments.Payment in project Signal-Android by WhisperSystems.
the class LedgerReconcileTest method one_received_and_one_spent_transaction_on_ledger_only_different_blocks.
@Test
public void one_received_and_one_spent_transaction_on_ledger_only_different_blocks() {
MobileCoinLedger ledger = ledger(spentTxo(mob(2.5), keyImage(1), publicKey(2), block(1), block(2)), unspentTxo(mob(1), keyImage(3), publicKey(4), block(3)));
List<Payment> payments = reconcile(Collections.emptyList(), new MobileCoinLedgerWrapper(ledger));
assertEquals(3, payments.size());
assertEquals(mob(1), payments.get(0).getAmountWithDirection());
assertEquals(mob(-2.5), payments.get(1).getAmountWithDirection());
assertEquals(mob(2.5), payments.get(2).getAmountWithDirection());
}
Aggregations