@bsv/expo-wallet-toolbox 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,6 +48,7 @@ import { makeCreditClassifier } from '../../../core/pay/creditErrors'
48
48
  import { satoshisFromToken } from '../../../core/pay/tokenAmount'
49
49
  import { userFacingPayError } from '../../../core/pay/userError'
50
50
  import { creditInboxOnce, type CreditInboxResult } from '../../../core/pay/creditInbox'
51
+ import { inboxSeenStore } from '../../../core/directpay/receive'
51
52
  import { setReceiveInboxFocused } from '../../../core/pay/receiveFocus'
52
53
  import { TaskCreditInbox } from '../../../core/monitor/TaskCreditInbox'
53
54
  import { useOnline } from '../../hooks/useOnline'
@@ -547,16 +548,20 @@ export default function HandleReceive({
547
548
  [selectedNetwork]
548
549
  )
549
550
 
551
+ // With direct payments on, a payment that arrived live as well is credited,
552
+ // and celebrated, once (see core/directpay/receive.ts).
553
+ const seen = useMemo(() => inboxSeenStore(identityKey, selectedNetwork), [identityKey, selectedNetwork])
554
+
550
555
  const internalize = useCallback(
551
556
  async (payment: IncomingPayment) => {
552
557
  const client = peerPayClient
553
558
  if (!client || !wallet) throw new Error(t('wallet_not_ready'))
554
- await internalizeIncoming(wallet as any, client, adminOriginator, payment, repairBeef)
559
+ return await internalizeIncoming(wallet as any, client, adminOriginator, payment, repairBeef, seen)
555
560
  },
556
- [peerPayClient, wallet, adminOriginator, t, repairBeef]
561
+ [peerPayClient, wallet, adminOriginator, t, repairBeef, seen]
557
562
  )
558
563
 
559
- const applyCreditResult = useCallback((outcome: CreditInboxResult) => {
564
+ const applyCreditResult = useCallback((outcome: CreditInboxResult, duplicates: ReadonlySet<string> = new Set()) => {
560
565
  TaskCreditInbox.lastAttentionCount = outcome.attentionCount
561
566
  attemptsRef.current = outcome.attempts
562
567
  setAttempts(outcome.attempts)
@@ -568,7 +573,10 @@ export default function HandleReceive({
568
573
  // looked at — and whether it landed is the one thing a payee must never
569
574
  // be left unsure about. Everything not left in the attempt map was
570
575
  // credited, so that is what the figure sums.
571
- const credited = outcome.payments.filter(p => !outcome.attempts[String(p.messageId)])
576
+ // A duplicate (taken live first) left the box but was celebrated then.
577
+ const credited = outcome.payments.filter(
578
+ p => !outcome.attempts[String(p.messageId)] && !duplicates.has(String(p.messageId))
579
+ )
572
580
  setReceived({
573
581
  amount: credited.reduce((sum, p) => sum + (satoshisFromToken(p.token)?.satoshis ?? 0), 0),
574
582
  count: outcome.accepted
@@ -584,15 +592,20 @@ export default function HandleReceive({
584
592
  getOnline: () => online,
585
593
  peekLastMissHeight
586
594
  })
595
+ const duplicates = new Set<string>()
587
596
  const outcome = await creditInboxOnce({
588
597
  client,
589
598
  messageBoxUrl,
590
599
  storage: storage ?? undefined,
591
600
  force,
592
601
  classify,
593
- accept: payment => acceptWithRetry(client, messageBoxUrl, payment, internalize)
602
+ accept: async payment => {
603
+ const credited = await acceptWithRetry(client, messageBoxUrl, payment, internalize)
604
+ if (credited === 'duplicate') duplicates.add(String(payment.messageId))
605
+ return credited
606
+ }
594
607
  })
595
- applyCreditResult(outcome)
608
+ applyCreditResult(outcome, duplicates)
596
609
  return outcome
597
610
  },
598
611
  [peerPayClient, messageBoxUrl, storage, online, peekLastMissHeight, internalize, applyCreditResult]
@@ -18,6 +18,7 @@ import { PeerPayClient } from '@bsv/message-box-client'
18
18
  import { showAlert } from '../components/ui/AlertCard'
19
19
  import { makeCreditClassifier } from '../../core/pay/creditErrors'
20
20
  import { creditInboxOnce } from '../../core/pay/creditInbox'
21
+ import { inboxSeenStore } from '../../core/directpay/receive'
21
22
  import { makeBeefRepair } from '../../core/pay/beefRepair'
22
23
  import {
23
24
  acceptWithRetry,
@@ -199,6 +200,12 @@ function useWalletCheckPorts(): WalletCheckPorts {
199
200
  })
200
201
  const repairBeef = makeBeefRepair({ woc: wocConfigFor(selectedNetwork), online: getOnline })
201
202
  const classify = await makeCreditClassifier({ getOnline, peekLastMissHeight })
203
+ // With direct payments on, one that arrived live as well is credited once.
204
+ const identity = await wallet
205
+ .getPublicKey({ identityKey: true }, adminOriginator)
206
+ .then(r => r.publicKey)
207
+ .catch(() => undefined)
208
+ const seen = inboxSeenStore(identity, selectedNetwork)
202
209
  const outcome = await creditInboxOnce({
203
210
  client,
204
211
  messageBoxUrl,
@@ -206,7 +213,7 @@ function useWalletCheckPorts(): WalletCheckPorts {
206
213
  classify,
207
214
  accept: payment =>
208
215
  acceptWithRetry(client, messageBoxUrl, payment, p =>
209
- internalizeIncoming(wallet as never, client, adminOriginator, p, repairBeef)
216
+ internalizeIncoming(wallet as never, client, adminOriginator, p, repairBeef, seen)
210
217
  )
211
218
  })
212
219
  return { accepted: outcome.accepted }