@bsv/expo-wallet-toolbox 0.5.0 → 0.9.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.
- package/CHANGELOG.md +2000 -0
- package/README.md +66 -1
- package/core/amountFormatHelpers.ts +258 -36
- package/core/backup/RemoteSyncReader.ts +133 -24
- package/core/backup/appData.ts +157 -0
- package/core/backup/client.ts +15 -1
- package/core/backup/codec.ts +149 -11
- package/core/backup/constants.ts +30 -1
- package/core/backup/cursor.ts +31 -2
- package/core/backup/push.ts +71 -5
- package/core/backup/restore.ts +104 -16
- package/core/backup/restoreOnImport.ts +94 -30
- package/core/backup/status.ts +13 -8
- package/core/config.tsx +12 -5
- package/core/constants.ts +14 -1
- package/core/contacts/contactActivity.ts +153 -0
- package/core/contacts/contactCache.ts +41 -0
- package/core/contacts/contactsStore.ts +183 -0
- package/core/context/ExchangeRateContext.tsx +34 -27
- package/core/context/LocalStorageProvider.tsx +43 -40
- package/core/context/UserContext.tsx +18 -7
- package/core/context/VaultContext.tsx +49 -2
- package/core/context/WalletConnectionContext.tsx +203 -11
- package/core/context/WalletContext.tsx +685 -171
- package/core/demo/demoAssets.ts +60 -0
- package/core/demo/demoLedger.ts +240 -0
- package/core/demo/demoMandalaRuntime.ts +96 -0
- package/core/demo/demoMode.ts +38 -0
- package/core/demo/demoWallet.ts +25 -0
- package/core/demo/index.ts +26 -0
- package/core/demo/useDemo.ts +59 -0
- package/core/headers/OfflineFirstChaintracks.ts +99 -17
- package/core/headers/headerStore.ts +33 -14
- package/core/headers/syncHeaders.ts +12 -1
- package/core/i18n/translations.tsx +2186 -98
- package/core/identity/contactLink.ts +36 -0
- package/core/identity/handleRegistry/client.ts +314 -0
- package/core/identity/handleRegistry/keyPinStore.ts +70 -0
- package/core/identity/handleRegistry/profileCert.ts +254 -0
- package/core/identity/handleRegistry/registration.ts +559 -0
- package/core/identity/handleRegistry/resolver.ts +315 -0
- package/core/identity/handleRegistry/rules.ts +104 -0
- package/core/index.ts +53 -5
- package/core/localpay/build.ts +225 -24
- package/core/localpay/pending.ts +110 -7
- package/core/localpay/pendingAbortAuthority.ts +95 -0
- package/core/localpay/pendingAborts.ts +266 -26
- package/core/localpay/session.ts +42 -17
- package/core/localpay/settlementAck.ts +10 -1
- package/core/localpay/transport/socket.ts +52 -3
- package/core/localpay/verify.ts +100 -10
- package/core/mandala/abortGuard.ts +96 -20
- package/core/mandala/createRuntime.ts +206 -33
- package/core/mandala/drain.ts +89 -11
- package/core/mandala/inboxPrehold.ts +186 -0
- package/core/mandala/permissionModule.ts +202 -20
- package/core/mandala/settlementStore.ts +24 -2
- package/core/mandala/types.ts +40 -5
- package/core/monitor/MonitorSupervisor.ts +121 -0
- package/core/monitor/TaskBackupPush.ts +15 -1
- package/core/monitor/TaskCreditInbox.ts +1 -1
- package/core/monitor/TaskDrainOutbox.ts +15 -1
- package/core/monitor/TaskSendOffline.ts +15 -1
- package/core/net/online.ts +49 -0
- package/core/net/publicDestination.ts +91 -0
- package/core/net/validateArcUrl.ts +22 -0
- package/core/numberFormat.ts +19 -0
- package/core/offline/cancelParked.ts +134 -13
- package/core/offline/payerHold.ts +100 -24
- package/core/offline/plan.ts +37 -8
- package/core/pay/beefRepair.ts +6 -2
- package/core/pay/damagedInbox.ts +24 -0
- package/core/pay/rails/address.ts +107 -12
- package/core/pay/rails/handle.ts +117 -22
- package/core/pay/rails/index.ts +45 -7
- package/core/pay/rails/nativeIntent.ts +61 -0
- package/core/pay/receiveHistory.ts +93 -0
- package/core/pay/recordProof.ts +21 -7
- package/core/pay/refreshProofGuard.ts +42 -1
- package/core/pay/watchlist.ts +5 -2
- package/core/peerpay/control.ts +17 -18
- package/core/peerpay/handleResendRequests.ts +58 -4
- package/core/peerpay/outbox.ts +3 -1
- package/core/recovery/backupMaterial.ts +61 -0
- package/core/recovery/createWallet.ts +95 -0
- package/core/recovery/recoverWallet.ts +120 -0
- package/core/recovery/restoreWallet.ts +170 -0
- package/core/recovery/secret.ts +92 -0
- package/core/recovery/shareCollector.ts +71 -0
- package/core/recovery/shareParsing.ts +87 -0
- package/core/recovery/shares.ts +116 -0
- package/core/recovery/useRecoveryDeps.ts +70 -0
- package/core/services/arcTokenStorage.ts +52 -0
- package/core/services/arcadeBroadcastProvider.ts +233 -52
- package/core/services/autoApprovePolicy.ts +105 -0
- package/core/services/callbackToken.ts +19 -0
- package/core/services/capWalletArgs.ts +14 -2
- package/core/services/connectionAuthority.ts +98 -0
- package/core/services/exchangeRate.ts +36 -13
- package/core/services/externalOrigin.ts +16 -2
- package/core/services/secrets/kek.ts +126 -29
- package/core/services/secrets/migration.ts +16 -4
- package/core/services/secrets/store.ts +42 -14
- package/core/services/vault/VaultKeyService.ts +228 -45
- package/core/services/vault/chainRecovery.ts +483 -0
- package/core/services/vault/driver.ts +9 -1
- package/core/services/vault/enrolledSerialRegistry.ts +150 -0
- package/core/services/vault/guard.ts +218 -32
- package/core/services/vault/hardwareLease.ts +19 -0
- package/core/services/vault/metaAuthority.ts +210 -0
- package/core/services/vault/pivReset.ts +64 -23
- package/core/services/vault/r1comb.ts +139 -3
- package/core/services/vault/session.ts +25 -4
- package/core/services/vault/transfers.ts +868 -172
- package/core/services/vault/types.ts +19 -4
- package/core/services/vault/vaultStore.ts +256 -29
- package/core/services/walletArgLimits.ts +45 -2
- package/core/services/walletConnectionValidation.ts +76 -6
- package/core/services/walletServiceConfig.ts +40 -27
- package/core/storage/StorageExpoSQLite.ts +88 -47
- package/core/storage/methods/findSql.ts +24 -2
- package/core/storage/methods/listActionsSql.ts +8 -5
- package/core/storage/methods/listOutputsSql.ts +2 -2
- package/core/storage/methods/offlineActions.ts +10 -1
- package/core/storage/methods/processOfflineActions.ts +117 -25
- package/core/storage/methods/reviewStatusSql.ts +7 -2
- package/core/storage/schema/createTables.ts +155 -2
- package/core/stores/ConnectionStore.ts +34 -0
- package/core/toolboxConfig.ts +104 -0
- package/core/toolboxTypes.ts +40 -0
- package/core/userAvatar.ts +79 -0
- package/core/walletDbRegistry.ts +63 -0
- package/core/walletMonitor.ts +25 -4
- package/core/walletRepair/releaseStuckReservations.ts +72 -0
- package/core/walletRepair/shouldMarkUnspendable.ts +20 -0
- package/core/walletRepair/spenderConsumesOutpoint.ts +29 -0
- package/package.json +7 -7
- package/ui/activityFilter.ts +115 -0
- package/ui/backupShares.ts +26 -169
- package/ui/components/pay/AddressReceive.tsx +13 -6
- package/ui/components/pay/AssetPicker.tsx +30 -9
- package/ui/components/pay/MessageBoxConfig.tsx +16 -0
- package/ui/components/pay/NearbyFlow.tsx +480 -243
- package/ui/components/pay/PaymentSuccessOverlay.tsx +85 -10
- package/ui/components/pay/RecipientField.tsx +84 -70
- package/ui/components/pay/StepBar.tsx +32 -0
- package/ui/components/pay/UniversalSend.tsx +827 -205
- package/ui/components/ui/AlertCard.tsx +38 -5
- package/ui/components/ui/PencilEditField.tsx +109 -0
- package/ui/components/ui/PermissionSheet.tsx +197 -66
- package/ui/components/ui/SlideOverFromRight.tsx +86 -0
- package/ui/components/vault/EnrollWizard.tsx +61 -13
- package/ui/components/vault/vaultErrorCopy.ts +3 -1
- package/ui/components/wallet/ActivityRow.tsx +227 -123
- package/ui/components/wallet/AmountInput.tsx +26 -8
- package/ui/components/wallet/AssetSwitcherDropdown.tsx +22 -2
- package/ui/components/wallet/Balance.tsx +42 -23
- package/ui/components/wallet/BiometricAdvisoryModal.tsx +16 -3
- package/ui/components/wallet/ContactActivityList.tsx +149 -0
- package/ui/components/wallet/ContactSigil.tsx +49 -0
- package/ui/components/wallet/EditPictureSheet.tsx +99 -0
- package/ui/components/wallet/FitAmount.tsx +148 -0
- package/ui/components/wallet/IconPickerSheet.tsx +168 -0
- package/ui/components/wallet/IdentifierRow.tsx +69 -0
- package/ui/components/wallet/ImportFromBackupPrompt.tsx +35 -13
- package/ui/components/wallet/ProfileButton.tsx +62 -0
- package/ui/components/wallet/UserAvatar.tsx +85 -0
- package/ui/components/wallet/avatarIcons.ts +122 -0
- package/ui/exportTransactions.ts +36 -4
- package/ui/hooks/useContactsStore.ts +10 -0
- package/ui/hooks/useExportWalletData.ts +25 -2
- package/ui/hooks/useMandala.ts +18 -1
- package/ui/hooks/useOnline.ts +65 -20
- package/ui/importDatabases.ts +339 -24
- package/ui/index.ts +33 -2
- package/ui/isImageUrl.ts +6 -0
- package/ui/printRecoveryShares.ts +9 -10
- package/ui/recoveryPrompts.ts +91 -0
- package/ui/resolveIdentity.ts +31 -9
- package/ui/screens/ConnectionsScreen.tsx +86 -19
- package/ui/screens/ContactScreen.tsx +289 -0
- package/ui/screens/ContactsScreen.tsx +373 -0
- package/ui/screens/IdentifierScreen.tsx +256 -0
- package/ui/screens/NewContactScreen.tsx +362 -0
- package/ui/screens/PairScreen.tsx +74 -2
- package/ui/screens/PayScreen.tsx +20 -7
- package/ui/screens/ProfileScreen.tsx +783 -0
- package/ui/screens/SettingsScreen.tsx +10 -4
- package/ui/screens/TransactionDetailScreen.tsx +419 -0
- package/ui/screens/TrustScreen.tsx +100 -5
- package/ui/screens/VaultScreen.tsx +410 -61
- package/ui/screens/VaultTransferScreen.tsx +59 -7
- package/ui/screens/WalletCheckScreen.tsx +17 -6
- package/ui/screens/WalletConfigScreen.tsx +124 -28
- package/ui/screens/WalletHomeScreen.tsx +640 -104
- package/ui/screens/detailActionKeys.ts +55 -0
- package/ui/tokenFormat.ts +44 -4
- package/ui/tokenStatus.ts +21 -0
- package/ui/txStatus.ts +48 -5
- package/core/storage/LocalStorageAdapter.ts +0 -58
package/README.md
CHANGED
|
@@ -171,7 +171,9 @@ configureToolbox({
|
|
|
171
171
|
arcApiKey: process.env.EXPO_PUBLIC_ARC_API_KEY,
|
|
172
172
|
chaintracksUrl: process.env.EXPO_PUBLIC_CHAINTRACKS_URL,
|
|
173
173
|
whatsOnChainApiKey: process.env.EXPO_PUBLIC_WOC_API_KEY,
|
|
174
|
-
|
|
174
|
+
// A distinct key: TAAL and WhatsOnChain are different origins, and this
|
|
175
|
+
// one is never used as a fallback for the other.
|
|
176
|
+
taalApiKey: process.env.EXPO_PUBLIC_TAAL_API_KEY
|
|
175
177
|
}
|
|
176
178
|
}
|
|
177
179
|
})
|
|
@@ -194,6 +196,25 @@ handshake posts to the origin root, so a path, query or fragment is rejected.
|
|
|
194
196
|
Every `services` field is optional and falls back to the built-in public
|
|
195
197
|
endpoint for that chain. Chains are `main`, `test` and `teratest`.
|
|
196
198
|
|
|
199
|
+
`handleRegistry` names the paymail domain this build's handles live under and
|
|
200
|
+
the host that serves it, per chain:
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
configureToolbox({
|
|
204
|
+
backupUrl: null,
|
|
205
|
+
handleRegistry: {
|
|
206
|
+
test: { domain: 'deggen.com', url: 'https://messagebox.bsvblockchain.tech' }
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Both fields are required together — a domain with no URL is a registry nothing
|
|
212
|
+
can reach, and a URL with no domain is a host whose certificates nothing can be
|
|
213
|
+
checked against — and the URL must be `https` unless it points at `localhost`,
|
|
214
|
+
`127.0.0.1`, `10.0.2.2` or RFC 1918 space, which is a development allowance. A
|
|
215
|
+
chain with no complete entry shows "not available yet" on the Profile screen
|
|
216
|
+
and adds no registry tier to Pay's recipient search.
|
|
217
|
+
|
|
197
218
|
Reading configuration before `configureToolbox` has run **throws**. An
|
|
198
219
|
unconfigured build fails loudly rather than behaving like a deliberately
|
|
199
220
|
disabled one.
|
|
@@ -541,6 +562,50 @@ are required — both hard-won during this package's extraction:
|
|
|
541
562
|
|
|
542
563
|
## Breaking changes
|
|
543
564
|
|
|
565
|
+
**0.9.0:** `useWallet().logout` now returns `Promise<boolean>` (it was
|
|
566
|
+
`() => void`). It resolves once teardown has finished, and resolves to
|
|
567
|
+
`false` when the secrets layer could not confirm erasure. Await it before
|
|
568
|
+
treating sign-out or Delete Wallet as complete. The
|
|
569
|
+
`@bsv/wallet-toolbox-mobile` peer range moves to ^2.14.3, and hosts must copy
|
|
570
|
+
`patches/@bsv+wallet-toolbox-mobile+2.14.3.patch` in place of the 2.14.0
|
|
571
|
+
patch. 0.8.0 was never published, so hosts upgrading from 0.7.0 get its
|
|
572
|
+
breaking changes below as well.
|
|
573
|
+
|
|
574
|
+
**0.8.0:** three API points break. `DerivingWallet` (the interface
|
|
575
|
+
`core/localpay/verify.ts`'s `verifyFramePayment` accepts) gains a required
|
|
576
|
+
`getServices(): { getChainTracker(): Promise<ChainTracker> | ChainTracker }`
|
|
577
|
+
— a host that built its own object against the old, narrower shape (just
|
|
578
|
+
`getPublicKey`) must add it before its build will typecheck.
|
|
579
|
+
`RestoreWalletDeps` gains a required `hasStoredIdentity(): Promise<boolean>`
|
|
580
|
+
and `RestorePrompts` gains a required `confirmReplace(): Promise<'replace' |
|
|
581
|
+
'keep'>` — a host supplying its own `RestoreWalletDeps`, or building its own
|
|
582
|
+
`RestorePrompts` instead of using `ui/recoveryPrompts.ts`'s
|
|
583
|
+
`restorePrompts(t)`, must implement both. `core/storage/LocalStorageAdapter.ts`
|
|
584
|
+
and its exports (`initializeLocalStorage`, `isLocalStorage`,
|
|
585
|
+
`getStorageDisplayName`, `LocalStorageConfig`) are removed — dead code with
|
|
586
|
+
no real caller in this app, but a host that imported these names directly
|
|
587
|
+
gets a build error.
|
|
588
|
+
|
|
589
|
+
`restoreWallet`/`recoverWallet` also change host-visible behaviour for
|
|
590
|
+
anything that goes through them (the app's own mnemonic and scan-shares
|
|
591
|
+
screens, and any host reusing the `core/recovery/` module directly):
|
|
592
|
+
recovering or importing over an already-built wallet now rebuilds instead of
|
|
593
|
+
silently no-op'ing, storing one secret kind now deletes the other (exactly
|
|
594
|
+
one secret survives a restore), a failed backup replay's "skip" now also
|
|
595
|
+
works for hex/WIF imports, an attestation failure after a successful build
|
|
596
|
+
is reported non-fatally instead of failing the whole attempt, and
|
|
597
|
+
`recoverWallet` now asks the new `confirmReplace` prompt before overwriting
|
|
598
|
+
a secret already on the device — declining cancels before anything is
|
|
599
|
+
written. An external pairing deep link no longer auto-connects from
|
|
600
|
+
Connections; it now lands on `/pair` and requires the same explicit
|
|
601
|
+
Approve/Reject tap a scanned QR gets. `validateShareCompatibility` is
|
|
602
|
+
deprecated (still works, returns English prose) in favour of
|
|
603
|
+
`checkShareCompatibility`, which returns a code for the caller to translate.
|
|
604
|
+
See the CHANGELOG's 0.8.0 entry for the full list, including the additive
|
|
605
|
+
review-fix changes (Nearby/QR payment verification, offline-drain and
|
|
606
|
+
declined-payment handling, auto-approve bounds, exchange-rate/header/backup
|
|
607
|
+
fixes, and more) that ship alongside these three breaking points.
|
|
608
|
+
|
|
544
609
|
**0.4.0:** the package no longer reads `process.env` at all. `DEFAULT_BACKUP_URL`
|
|
545
610
|
is removed, and the `EXPO_PUBLIC_*` service reads in `walletServiceConfig` are
|
|
546
611
|
gone. The host supplies these through `configureToolbox({ backupUrl, services })`
|
|
@@ -1,24 +1,88 @@
|
|
|
1
|
-
|
|
2
|
-
const getLocaleDefault = (): string => {
|
|
3
|
-
try {
|
|
4
|
-
// Try to get locale using Intl API if available
|
|
5
|
-
return Intl.NumberFormat().resolvedOptions().locale?.split('-u-')[0] || 'en-US'
|
|
6
|
-
} catch {
|
|
7
|
-
// Fallback to en-US if Intl is not fully supported
|
|
8
|
-
return 'en-US'
|
|
9
|
-
}
|
|
10
|
-
}
|
|
1
|
+
import { getNumberLocale } from './numberFormat'
|
|
11
2
|
|
|
12
|
-
|
|
3
|
+
/**
|
|
4
|
+
* The locale every figure on this screen is printed through.
|
|
5
|
+
*
|
|
6
|
+
* A function, not a module constant: the separators are a user preference now
|
|
7
|
+
* (see `core/numberFormat.ts`), so a figure formatted at module-load time
|
|
8
|
+
* would be stuck with whatever the device implied at launch.
|
|
9
|
+
*/
|
|
10
|
+
const locale = (): string => getNumberLocale()
|
|
13
11
|
|
|
14
12
|
const SATS_PER_BSV = 100_000_000
|
|
15
13
|
const CENT_THRESHOLD = 0.01
|
|
16
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Above this many currency units, a fiat figure is abbreviated and its minor
|
|
17
|
+
* units are dropped. Below it the cents are kept exactly: those are the
|
|
18
|
+
* balances a holder reconciles against a bank app, and `$1,234.56` rounded to
|
|
19
|
+
* `$1.2k` is no longer the same number to them.
|
|
20
|
+
*/
|
|
21
|
+
const FIAT_ABBREVIATE_ABOVE = 100_000
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `1500 -> "1.5k"`, `1_000_000 -> "1M"`, `2_400_000_000 -> "2.4B"`.
|
|
25
|
+
*
|
|
26
|
+
* At most one decimal place and never a trailing `.0`, because the point of
|
|
27
|
+
* the short form is to be read at a glance. Values under 1000 are returned
|
|
28
|
+
* grouped and unabbreviated — there is nothing to save.
|
|
29
|
+
*/
|
|
30
|
+
const ABBREVIATION_STEPS: { limit: number; divisor: number; suffix: string }[] = [
|
|
31
|
+
{ limit: 1_000_000_000, divisor: 1_000_000_000, suffix: 'B' },
|
|
32
|
+
{ limit: 1_000_000, divisor: 1_000_000, suffix: 'M' },
|
|
33
|
+
{ limit: 1_000, divisor: 1_000, suffix: 'k' }
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The magnitude `value` should be shown at. `suffix` is `''` when the value is
|
|
38
|
+
* small enough to print in full. Shared by the satoshi and fiat paths so the
|
|
39
|
+
* two can never disagree about where a 'k' becomes an 'M'.
|
|
40
|
+
*/
|
|
41
|
+
const scaleForAbbreviation = (value: number): { scaled: number; suffix: string } => {
|
|
42
|
+
const abs = Math.abs(value)
|
|
43
|
+
for (let i = 0; i < ABBREVIATION_STEPS.length; i++) {
|
|
44
|
+
const { limit, divisor, suffix } = ABBREVIATION_STEPS[i]
|
|
45
|
+
if (abs < limit) continue
|
|
46
|
+
// One decimal, trimmed: 1.0k reads as noise, 1.5k does not.
|
|
47
|
+
const scaled = Math.round((abs / divisor) * 10) / 10
|
|
48
|
+
// 999,950 rounds to 1000k; that is 1M.
|
|
49
|
+
const up = ABBREVIATION_STEPS[i - 1]
|
|
50
|
+
if (scaled >= 1000 && up) return { scaled: Math.round((abs / up.divisor) * 10) / 10, suffix: up.suffix }
|
|
51
|
+
return { scaled, suffix }
|
|
52
|
+
}
|
|
53
|
+
return { scaled: abs, suffix: '' }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const abbreviateNumber = (value: number): string => {
|
|
57
|
+
const { scaled, suffix } = scaleForAbbreviation(value)
|
|
58
|
+
if (!suffix) return formatSatoshisLocale(Math.abs(value))
|
|
59
|
+
return `${Number.isInteger(scaled) ? String(scaled) : formatDecimalLocale(scaled, 1)}${suffix}`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A number at fixed decimals in the active locale's separators. */
|
|
63
|
+
const formatDecimalLocale = (value: number, digits: number): string => {
|
|
64
|
+
try {
|
|
65
|
+
return new Intl.NumberFormat(locale(), {
|
|
66
|
+
minimumFractionDigits: digits,
|
|
67
|
+
maximumFractionDigits: digits,
|
|
68
|
+
useGrouping: true
|
|
69
|
+
}).format(value)
|
|
70
|
+
} catch {
|
|
71
|
+
return value.toFixed(digits)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
17
75
|
export type UsdToFiat = Record<string, number>
|
|
18
76
|
|
|
19
77
|
export type AmountFormatOptions = {
|
|
20
78
|
showPlus?: boolean
|
|
79
|
+
/** "sats" instead of "satoshis". Never changes the figure. */
|
|
21
80
|
abbreviate?: boolean
|
|
81
|
+
/**
|
|
82
|
+
* Shorten large figures to k/M/B. Only for dense lists (activity rows): an
|
|
83
|
+
* approval, a balance or a transaction detail must show the exact amount.
|
|
84
|
+
*/
|
|
85
|
+
compact?: boolean
|
|
22
86
|
showFiatAsInteger?: boolean
|
|
23
87
|
usdToFiat?: UsdToFiat
|
|
24
88
|
}
|
|
@@ -29,11 +93,7 @@ export const isFiatCurrency = (currency: string): boolean => Boolean(currency) &
|
|
|
29
93
|
* Satoshis per 1 unit of `currency`. USD is the WhatsOnChain rate; other fiat
|
|
30
94
|
* is that rate divided by the USD→fiat cross (1 USD = `usdToFiat[code]` units).
|
|
31
95
|
*/
|
|
32
|
-
export const satoshisPerFiatUnit = (
|
|
33
|
-
currency: string,
|
|
34
|
-
satoshisPerUSD: number,
|
|
35
|
-
usdToFiat: UsdToFiat = {}
|
|
36
|
-
): number => {
|
|
96
|
+
export const satoshisPerFiatUnit = (currency: string, satoshisPerUSD: number, usdToFiat: UsdToFiat = {}): number => {
|
|
37
97
|
if (currency === 'USD') return satoshisPerUSD
|
|
38
98
|
const fx = usdToFiat[currency]
|
|
39
99
|
if (!(satoshisPerUSD > 0) || !(fx > 0)) return 0
|
|
@@ -42,7 +102,7 @@ export const satoshisPerFiatUnit = (
|
|
|
42
102
|
|
|
43
103
|
export const fiatFractionDigits = (currency: string): number => {
|
|
44
104
|
try {
|
|
45
|
-
const digits = new Intl.NumberFormat(
|
|
105
|
+
const digits = new Intl.NumberFormat(locale(), {
|
|
46
106
|
style: 'currency',
|
|
47
107
|
currency
|
|
48
108
|
}).resolvedOptions().maximumFractionDigits
|
|
@@ -52,6 +112,65 @@ export const fiatFractionDigits = (currency: string): number => {
|
|
|
52
112
|
}
|
|
53
113
|
}
|
|
54
114
|
|
|
115
|
+
/**
|
|
116
|
+
* The symbol this wallet prints for a currency, whatever the locale would.
|
|
117
|
+
*
|
|
118
|
+
* `currencyDisplay: 'narrowSymbol'` is not enough: a locale narrows only as
|
|
119
|
+
* far as it can still disambiguate, so an `en-NL` device renders USD as
|
|
120
|
+
* "US$" — which is a correct answer to a question nobody asked. The wallet
|
|
121
|
+
* shows one currency at a time and says which in Settings, so the short
|
|
122
|
+
* symbol is never ambiguous here.
|
|
123
|
+
*
|
|
124
|
+
* Currencies absent from this table keep whatever the locale produces, which
|
|
125
|
+
* for a code like CHF is already the code itself.
|
|
126
|
+
*/
|
|
127
|
+
const PREFERRED_SYMBOL: Record<string, string> = {
|
|
128
|
+
USD: '$',
|
|
129
|
+
AUD: '$',
|
|
130
|
+
CAD: '$',
|
|
131
|
+
NZD: '$',
|
|
132
|
+
BRL: 'R$',
|
|
133
|
+
EUR: '€',
|
|
134
|
+
GBP: '£',
|
|
135
|
+
JPY: '¥',
|
|
136
|
+
CNY: '¥',
|
|
137
|
+
INR: '₹',
|
|
138
|
+
RUB: '₽',
|
|
139
|
+
PLN: 'zł',
|
|
140
|
+
IDR: 'Rp'
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Format through Intl, then swap the currency token for our own symbol.
|
|
145
|
+
*
|
|
146
|
+
* Rebuilt from `formatToParts` rather than string-replaced so the locale keeps
|
|
147
|
+
* deciding WHERE the symbol sits and whether a space follows it — only the
|
|
148
|
+
* glyph is ours.
|
|
149
|
+
*/
|
|
150
|
+
const formatCurrencyParts = (
|
|
151
|
+
value: number,
|
|
152
|
+
loc: string,
|
|
153
|
+
currency: string,
|
|
154
|
+
options: Intl.NumberFormatOptions
|
|
155
|
+
): string => {
|
|
156
|
+
const preferred = PREFERRED_SYMBOL[currency]
|
|
157
|
+
const parts = new Intl.NumberFormat(loc, options).formatToParts(value)
|
|
158
|
+
return parts.map(p => (p.type === 'currency' && preferred ? preferred : p.value)).join('')
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Digits only, in the locale's separators, for when currency formatting fails. */
|
|
162
|
+
const formatPlainDigits = (value: number, loc: string, minDigits: number, maxDigits: number): string => {
|
|
163
|
+
try {
|
|
164
|
+
return new Intl.NumberFormat(loc, {
|
|
165
|
+
minimumFractionDigits: minDigits,
|
|
166
|
+
maximumFractionDigits: maxDigits,
|
|
167
|
+
useGrouping: true
|
|
168
|
+
}).format(value)
|
|
169
|
+
} catch {
|
|
170
|
+
return value.toFixed(minDigits)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
55
174
|
// Format number as currency with fallback for platforms where Intl is not fully supported
|
|
56
175
|
const formatCurrency = (
|
|
57
176
|
value: number,
|
|
@@ -67,6 +186,12 @@ const formatCurrency = (
|
|
|
67
186
|
const options: Intl.NumberFormatOptions = {
|
|
68
187
|
currency,
|
|
69
188
|
style: 'currency',
|
|
189
|
+
// The separator preference is expressed as a locale, and a locale also
|
|
190
|
+
// carries that language's NAME for the currency — fr-FR renders USD as
|
|
191
|
+
// "$US". The UI's language is chosen elsewhere, so pin the short symbol
|
|
192
|
+
// and let the locale govern only the digits, which is all the
|
|
193
|
+
// preference was ever about.
|
|
194
|
+
currencyDisplay: 'narrowSymbol',
|
|
70
195
|
minimumFractionDigits: minDigits
|
|
71
196
|
}
|
|
72
197
|
|
|
@@ -74,10 +199,14 @@ const formatCurrency = (
|
|
|
74
199
|
options.maximumFractionDigits = maxDigits
|
|
75
200
|
}
|
|
76
201
|
|
|
77
|
-
|
|
78
|
-
formatted = formatter.format(abs)
|
|
202
|
+
formatted = formatCurrencyParts(abs, locale, currency, options)
|
|
79
203
|
} catch {
|
|
80
|
-
|
|
204
|
+
// Hermes lacks `formatToParts` / `narrowSymbol` on some builds. Keep our
|
|
205
|
+
// symbol in front of plain locale digits rather than dropping to the ISO
|
|
206
|
+
// code ("USD 3.45"); only a currency with no symbol of ours shows its code.
|
|
207
|
+
const digits = formatPlainDigits(abs, locale, minDigits, maxDigits ?? minDigits)
|
|
208
|
+
const preferred = PREFERRED_SYMBOL[currency]
|
|
209
|
+
formatted = preferred ? `${preferred}${digits}` : `${currency} ${digits}`
|
|
81
210
|
}
|
|
82
211
|
// Same sign convention as the satoshi formatter, so a column of mixed
|
|
83
212
|
// currencies reads one way: minus for money out, plus for money in when the
|
|
@@ -93,7 +222,7 @@ const fiatSign = (value: number, showPlus: boolean): string => (value < 0 ? '-'
|
|
|
93
222
|
*/
|
|
94
223
|
const formatSatoshisLocale = (satoshis: number): string => {
|
|
95
224
|
try {
|
|
96
|
-
return new Intl.NumberFormat(
|
|
225
|
+
return new Intl.NumberFormat(locale(), {
|
|
97
226
|
maximumFractionDigits: 0,
|
|
98
227
|
useGrouping: true
|
|
99
228
|
}).format(satoshis)
|
|
@@ -108,7 +237,7 @@ const formatSatoshisLocale = (satoshis: number): string => {
|
|
|
108
237
|
*/
|
|
109
238
|
const formatBsvLocale = (bsvValue: number): string => {
|
|
110
239
|
try {
|
|
111
|
-
return new Intl.NumberFormat(
|
|
240
|
+
return new Intl.NumberFormat(locale(), {
|
|
112
241
|
minimumFractionDigits: 0,
|
|
113
242
|
maximumFractionDigits: 8,
|
|
114
243
|
useGrouping: true
|
|
@@ -130,7 +259,8 @@ export const formatSatoshisAsFiat = (
|
|
|
130
259
|
satoshisPerUnit: number,
|
|
131
260
|
showFiatAsInteger = false,
|
|
132
261
|
currency = 'USD',
|
|
133
|
-
showPlus = false
|
|
262
|
+
showPlus = false,
|
|
263
|
+
compact = false
|
|
134
264
|
): string => {
|
|
135
265
|
if (!Number.isInteger(Number(satoshis)) || !satoshisPerUnit || satoshisPerUnit <= 0) {
|
|
136
266
|
return '...'
|
|
@@ -145,17 +275,32 @@ export const formatSatoshisAsFiat = (
|
|
|
145
275
|
const threshold = digits === 0 ? 1 : 1 / factor
|
|
146
276
|
|
|
147
277
|
if (v > 0 && v < threshold && !showFiatAsInteger) {
|
|
148
|
-
const smallest = formatCurrency(threshold,
|
|
278
|
+
const smallest = formatCurrency(threshold, locale(), currency, digits, digits)
|
|
149
279
|
return `< ${fiatSign(raw, showPlus)}${smallest}`
|
|
150
280
|
}
|
|
151
281
|
|
|
152
282
|
const sign = raw < 0 ? -1 : 1
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Only large figures shorten. A balance a holder reconciles against a bank
|
|
286
|
+
* app keeps its exact minor units, because `$1,234.56` shown as `$1.2k` is
|
|
287
|
+
* not the same number to the person reading it — it is a different number
|
|
288
|
+
* that happens to be close. Past `FIAT_ABBREVIATE_ABOVE` the cents have
|
|
289
|
+
* stopped being the point and the width has started to be.
|
|
290
|
+
*/
|
|
291
|
+
if (compact && v >= FIAT_ABBREVIATE_ABOVE) {
|
|
292
|
+
const { scaled, suffix } = scaleForAbbreviation(v)
|
|
293
|
+
const shortDigits = Number.isInteger(scaled) ? 0 : 1
|
|
294
|
+
const body = formatCurrency(sign * scaled, locale(), currency, shortDigits, shortDigits, showPlus)
|
|
295
|
+
return `${body}${suffix}`
|
|
296
|
+
}
|
|
297
|
+
|
|
153
298
|
const rounded = (sign * Math.ceil(Math.abs(raw) * factor)) / factor
|
|
154
299
|
|
|
155
300
|
const minDigits = showFiatAsInteger ? 0 : digits
|
|
156
301
|
const maxDigits = showFiatAsInteger ? 0 : digits
|
|
157
302
|
|
|
158
|
-
return formatCurrency(rounded,
|
|
303
|
+
return formatCurrency(rounded, locale(), currency, minDigits, maxDigits, showPlus)
|
|
159
304
|
}
|
|
160
305
|
|
|
161
306
|
/**
|
|
@@ -165,7 +310,23 @@ export const formatSatoshisAsFiat = (
|
|
|
165
310
|
*
|
|
166
311
|
* All formatting is locale-aware.
|
|
167
312
|
*/
|
|
168
|
-
|
|
313
|
+
/**
|
|
314
|
+
* The exact satoshi count, never switched to BSV or shortened: `+1,234,567 sats`.
|
|
315
|
+
* For the one place a holder checks what the transaction actually moved.
|
|
316
|
+
*/
|
|
317
|
+
export const formatSatoshisExact = (satoshis: number, showPlus = false): { value: string; unit: string } => {
|
|
318
|
+
const n = Number(satoshis)
|
|
319
|
+
if (!Number.isInteger(n)) return { value: '---', unit: 'sats' }
|
|
320
|
+
const sign = n < 0 ? '-' : showPlus && n > 0 ? '+' : ''
|
|
321
|
+
return { value: `${sign}${formatSatoshisLocale(Math.abs(n))}`, unit: 'sats' }
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export const formatSatoshisAsBsv = (
|
|
325
|
+
satoshis: number,
|
|
326
|
+
showPlus = false,
|
|
327
|
+
abbreviate = false,
|
|
328
|
+
compact = false
|
|
329
|
+
): string => {
|
|
169
330
|
const numValue = Number(satoshis)
|
|
170
331
|
if (!Number.isInteger(numValue)) return '---'
|
|
171
332
|
|
|
@@ -175,11 +336,15 @@ export const formatSatoshisAsBsv = (satoshis: number, showPlus = false, abbrevia
|
|
|
175
336
|
if (absValue >= SATS_PER_BSV) {
|
|
176
337
|
// Display as BSV
|
|
177
338
|
const bsvValue = absValue / SATS_PER_BSV
|
|
178
|
-
|
|
339
|
+
// A whole-BSV figure abbreviates on the same ladder once it passes 1000,
|
|
340
|
+
// so "1,000 BSV" reads "1k BSV" rather than growing a digit per decade.
|
|
341
|
+
const body = compact && bsvValue >= 1000 ? abbreviateNumber(bsvValue) : formatBsvLocale(bsvValue)
|
|
342
|
+
return `${sign}${body} BSV`
|
|
179
343
|
} else {
|
|
180
|
-
// Display as satoshis
|
|
344
|
+
// Display as satoshis. `abbreviate` shortens the label, `compact` the figure.
|
|
181
345
|
const label = abbreviate ? 'sats' : 'satoshis'
|
|
182
|
-
|
|
346
|
+
const body = compact ? abbreviateNumber(absValue) : formatSatoshisLocale(absValue)
|
|
347
|
+
return `${sign}${body} ${label}`
|
|
183
348
|
}
|
|
184
349
|
}
|
|
185
350
|
|
|
@@ -194,14 +359,14 @@ export const formatAmount = (
|
|
|
194
359
|
satoshisPerUSD: number = 0,
|
|
195
360
|
options: AmountFormatOptions = {}
|
|
196
361
|
): string => {
|
|
197
|
-
const { showPlus = false, abbreviate = false, showFiatAsInteger = false, usdToFiat = {} } = options
|
|
362
|
+
const { showPlus = false, abbreviate = false, compact = false, showFiatAsInteger = false, usdToFiat = {} } = options
|
|
198
363
|
|
|
199
364
|
if (isFiatCurrency(currency)) {
|
|
200
365
|
const per = satoshisPerFiatUnit(currency, satoshisPerUSD, usdToFiat)
|
|
201
|
-
return formatSatoshisAsFiat(satoshis, per, showFiatAsInteger, currency, showPlus)
|
|
366
|
+
return formatSatoshisAsFiat(satoshis, per, showFiatAsInteger, currency, showPlus, compact)
|
|
202
367
|
}
|
|
203
368
|
|
|
204
|
-
return formatSatoshisAsBsv(satoshis, showPlus, abbreviate)
|
|
369
|
+
return formatSatoshisAsBsv(satoshis, showPlus, abbreviate, compact)
|
|
205
370
|
}
|
|
206
371
|
|
|
207
372
|
/**
|
|
@@ -252,7 +417,7 @@ export const formatAmountInInputUnit = (
|
|
|
252
417
|
const amount = Math.abs(n) / per
|
|
253
418
|
const digits = fiatFractionDigits(currency)
|
|
254
419
|
try {
|
|
255
|
-
return new Intl.NumberFormat(
|
|
420
|
+
return new Intl.NumberFormat(locale(), {
|
|
256
421
|
minimumFractionDigits: digits,
|
|
257
422
|
maximumFractionDigits: digits
|
|
258
423
|
}).format(amount)
|
|
@@ -263,10 +428,26 @@ export const formatAmountInInputUnit = (
|
|
|
263
428
|
return formatSatoshisLocale(Math.abs(Math.round(n)))
|
|
264
429
|
}
|
|
265
430
|
|
|
431
|
+
/**
|
|
432
|
+
* The character the active locale uses for a decimal point (e.g. ',' for
|
|
433
|
+
* de-DE), read off a real formatted number rather than hardcoded — a
|
|
434
|
+
* comma-decimal locale typing '3,50' must not have it silently truncated at
|
|
435
|
+
* the comma by `parseFloat`, which only ever recognizes '.'.
|
|
436
|
+
*/
|
|
437
|
+
export const decimalSeparator = (): string => {
|
|
438
|
+
try {
|
|
439
|
+
const part = new Intl.NumberFormat(locale()).formatToParts(1.1).find(p => p.type === 'decimal')
|
|
440
|
+
return part?.value ?? '.'
|
|
441
|
+
} catch {
|
|
442
|
+
return '.'
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
266
446
|
/**
|
|
267
447
|
* Convert a user-entered display value back to integer satoshis.
|
|
268
448
|
* - BSV mode: input is satoshi integers, passthrough
|
|
269
|
-
* - fiat mode: input is a decimal amount in that currency
|
|
449
|
+
* - fiat mode: input is a decimal amount in that currency, typed with the
|
|
450
|
+
* active locale's own decimal separator
|
|
270
451
|
*/
|
|
271
452
|
export const parseDisplayToSatoshis = (
|
|
272
453
|
displayValue: string,
|
|
@@ -278,7 +459,9 @@ export const parseDisplayToSatoshis = (
|
|
|
278
459
|
if (!cleaned) return 0
|
|
279
460
|
|
|
280
461
|
if (isFiatCurrency(currency)) {
|
|
281
|
-
const
|
|
462
|
+
const sep = decimalSeparator()
|
|
463
|
+
const normalized = sep === '.' ? cleaned : cleaned.replace(sep, '.')
|
|
464
|
+
const amount = parseFloat(normalized)
|
|
282
465
|
if (isNaN(amount)) return 0
|
|
283
466
|
const per = satoshisPerFiatUnit(currency, satoshisPerUSD, usdToFiat)
|
|
284
467
|
if (!(per > 0)) return 0
|
|
@@ -295,7 +478,12 @@ export const parseDisplayToSatoshis = (
|
|
|
295
478
|
* In BSV mode, the label depends on the amount (satoshis vs BSV).
|
|
296
479
|
* If no satoshi value is provided, returns "satoshis" (the input label for BSV mode).
|
|
297
480
|
*/
|
|
298
|
-
export const getUnitLabel = (
|
|
481
|
+
export const getUnitLabel = (
|
|
482
|
+
currency: string,
|
|
483
|
+
satoshis?: number,
|
|
484
|
+
abbreviate = false,
|
|
485
|
+
satoshisPerUSD?: number
|
|
486
|
+
): string => {
|
|
299
487
|
if (isFiatCurrency(currency)) {
|
|
300
488
|
if (currency === 'USD' && satoshis !== undefined && satoshisPerUSD && satoshisPerUSD > 0) {
|
|
301
489
|
const usd = Math.abs(satoshis / satoshisPerUSD)
|
|
@@ -314,3 +502,37 @@ export const getUnitLabel = (currency: string, satoshis?: number, abbreviate = f
|
|
|
314
502
|
|
|
315
503
|
// Keep legacy exports for backward compatibility during migration
|
|
316
504
|
export const formatSatoshis = formatSatoshisAsBsv
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Split a formatted figure so its minor units can be set smaller and raised,
|
|
508
|
+
* the way a price tag writes them.
|
|
509
|
+
*
|
|
510
|
+
* `head` is everything up to and including the decimal separator, `frac` the
|
|
511
|
+
* minor-unit digits, `tail` whatever trails them (a suffixed currency symbol
|
|
512
|
+
* in the locales that put it there).
|
|
513
|
+
*
|
|
514
|
+
* Returns `frac: ''` — meaning "draw this as one run" — when there is nothing
|
|
515
|
+
* to raise, and deliberately when the figure is ABBREVIATED: the `.5` of
|
|
516
|
+
* `1.5k` is a magnitude, not cents, and shrinking it would read as `1` with a
|
|
517
|
+
* superscript. Detected by a letter immediately after the digits, which is
|
|
518
|
+
* exactly what the k/M/B suffix is.
|
|
519
|
+
*/
|
|
520
|
+
export const splitAmountFraction = (value: string): { head: string; frac: string; tail: string } => {
|
|
521
|
+
const whole = { head: value, frac: '', tail: '' }
|
|
522
|
+
if (!value) return whole
|
|
523
|
+
let sep: string
|
|
524
|
+
try {
|
|
525
|
+
sep = new Intl.NumberFormat(locale()).formatToParts(1.1).find(p => p.type === 'decimal')?.value ?? '.'
|
|
526
|
+
} catch {
|
|
527
|
+
sep = '.'
|
|
528
|
+
}
|
|
529
|
+
const at = value.lastIndexOf(sep)
|
|
530
|
+
if (at < 0) return whole
|
|
531
|
+
const rest = value.slice(at + sep.length)
|
|
532
|
+
const digits = /^\d+/.exec(rest)?.[0] ?? ''
|
|
533
|
+
if (!digits) return whole
|
|
534
|
+
const tail = rest.slice(digits.length)
|
|
535
|
+
// An abbreviation suffix (k/M/B) means those digits are magnitude, not cents.
|
|
536
|
+
if (/^\p{L}/u.test(tail)) return whole
|
|
537
|
+
return { head: value.slice(0, at + sep.length), frac: digits, tail }
|
|
538
|
+
}
|