@bsv/expo-wallet-toolbox 0.21.0 → 0.23.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.
Files changed (133) hide show
  1. package/CHANGELOG.md +222 -0
  2. package/README.md +82 -13
  3. package/core/bridge/BrowserBridgeProvider.tsx +273 -0
  4. package/core/bridge/bindings.ts +459 -0
  5. package/core/bridge/conformance.ts +222 -0
  6. package/core/bridge/consent.ts +494 -0
  7. package/core/bridge/consentSheetSource.ts +98 -0
  8. package/core/bridge/dispatcher.ts +621 -0
  9. package/core/bridge/inbox.ts +464 -0
  10. package/core/bridge/index.ts +48 -0
  11. package/core/bridge/loopback/cors.ts +57 -0
  12. package/core/bridge/loopback/endpoints.ts +11 -0
  13. package/core/bridge/loopback/hostCheck.ts +25 -0
  14. package/core/bridge/loopback/index.ts +7 -0
  15. package/core/bridge/loopback/launchProof.ts +166 -0
  16. package/core/bridge/loopback/router.ts +770 -0
  17. package/core/bridge/loopback/routerTypes.ts +115 -0
  18. package/core/bridge/loopback/status.ts +52 -0
  19. package/core/bridge/memoryInboxStore.ts +222 -0
  20. package/core/bridge/permissionPolicy.ts +247 -0
  21. package/core/bridge/policy.ts +72 -0
  22. package/core/bridge/protocol/base64url.ts +64 -0
  23. package/core/bridge/protocol/connectionError.ts +79 -0
  24. package/core/bridge/protocol/constants.ts +21 -0
  25. package/core/bridge/protocol/continuity.ts +21 -0
  26. package/core/bridge/protocol/descriptor.ts +395 -0
  27. package/core/bridge/protocol/envelopes.ts +188 -0
  28. package/core/bridge/protocol/errorCodec.ts +164 -0
  29. package/core/bridge/protocol/json.ts +237 -0
  30. package/core/bridge/protocol/limits.ts +43 -0
  31. package/core/bridge/protocol/methods.ts +55 -0
  32. package/core/bridge/protocol/origin.ts +130 -0
  33. package/core/bridge/runtime.ts +642 -0
  34. package/core/bridge/runtimeFlags.ts +34 -0
  35. package/core/bridge/testing/conformanceCases.ts +149 -0
  36. package/core/bridge/testing/pageClient.ts +44 -0
  37. package/core/bridge/unverifiedManager.ts +97 -0
  38. package/core/bridge/unverifiedOriginator.ts +79 -0
  39. package/core/config.tsx +1 -16
  40. package/core/contacts/contactActivity.ts +48 -28
  41. package/core/contacts/contactsStore.ts +2 -2
  42. package/core/context/WalletConnectionContext.tsx +7 -1
  43. package/core/context/WalletContext.tsx +420 -348
  44. package/core/context/sessionTeardown.ts +46 -0
  45. package/core/demo/demoAssets.ts +8 -10
  46. package/core/demo/demoLedger.ts +32 -38
  47. package/core/demo/demoMandalaRuntime.ts +67 -49
  48. package/core/demo/index.ts +1 -1
  49. package/core/hooks/usePermissionQueue.ts +89 -34
  50. package/core/i18n/translations.tsx +983 -12
  51. package/core/index.ts +70 -37
  52. package/core/localpay/build.ts +110 -569
  53. package/core/localpay/codec.ts +190 -93
  54. package/core/localpay/pending.ts +217 -202
  55. package/core/localpay/session.ts +99 -25
  56. package/core/localpay/types.ts +6 -5
  57. package/core/localpay/verify.ts +118 -173
  58. package/core/mandala/abortGuard.ts +97 -120
  59. package/core/mandala/accessPolicy.ts +133 -0
  60. package/core/mandala/assets.ts +370 -0
  61. package/core/mandala/colorGuard.ts +178 -0
  62. package/core/mandala/colorModule.ts +82 -0
  63. package/core/mandala/createRuntime.ts +483 -2392
  64. package/core/mandala/handle.ts +694 -0
  65. package/core/mandala/kvStore.ts +105 -0
  66. package/core/mandala/nearby.ts +185 -0
  67. package/core/mandala/receipt.ts +121 -0
  68. package/core/mandala/runtime.ts +166 -312
  69. package/core/mandala/settle.ts +540 -0
  70. package/core/mandala/transferJournal.ts +395 -0
  71. package/core/mandala/types.ts +61 -311
  72. package/core/networks.ts +5 -5
  73. package/core/offline/cancelParked.ts +80 -63
  74. package/core/offline/payerHold.ts +52 -118
  75. package/core/parsePeerPayURI.ts +25 -12
  76. package/core/pay/rails/handle.ts +15 -14
  77. package/core/pay/rails/index.ts +5 -3
  78. package/core/pay/rails/nearby.ts +25 -87
  79. package/core/profiles/assertProfileEmpty.ts +19 -10
  80. package/core/services/autoApprovePolicy.ts +7 -1
  81. package/core/services/capWalletArgs.ts +2 -1
  82. package/core/services/permissionPrompts.ts +132 -0
  83. package/core/services/signingPermissionPolicy.ts +230 -2
  84. package/core/services/vault/guard.ts +15 -4
  85. package/core/services/walletArgLimits.ts +17 -2
  86. package/core/storage/StorageExpoSQLite.ts +93 -229
  87. package/core/storage/encryptedDbImage.ts +325 -0
  88. package/core/storage/methods/processOfflineActions.ts +86 -136
  89. package/core/storage/schema/createTables.ts +9 -184
  90. package/core/toolboxConfig.ts +18 -1
  91. package/package.json +9 -8
  92. package/ui/bridgeSheet/ContextRelay.tsx +86 -0
  93. package/ui/bridgeSheet/relayedContexts.ts +21 -0
  94. package/ui/components/pay/AssetPicker.tsx +2 -2
  95. package/ui/components/pay/HandleReceive.tsx +4 -4
  96. package/ui/components/pay/NearbyFlow.tsx +324 -239
  97. package/ui/components/pay/RequestHub.tsx +10 -0
  98. package/ui/components/pay/UniversalSend.tsx +31 -28
  99. package/ui/components/pay/useRecipientInput.ts +2 -2
  100. package/ui/components/ui/PermissionSheet.tsx +625 -315
  101. package/ui/components/ui/Sheet.tsx +59 -7
  102. package/ui/components/ui/colorPrompt.ts +236 -0
  103. package/ui/components/ui/sheetResumeSignal.ts +19 -0
  104. package/ui/components/wallet/AmountInput.tsx +9 -6
  105. package/ui/components/wallet/AssetAmount.tsx +6 -6
  106. package/ui/components/wallet/AssetSwitcherDropdown.tsx +26 -7
  107. package/ui/components/wallet/Balance.tsx +3 -1
  108. package/ui/components/wallet/ContactActivityList.tsx +10 -13
  109. package/ui/components/wallet/tokenEviction.ts +9 -9
  110. package/ui/exportDatabases.ts +94 -5
  111. package/ui/hooks/useExportWalletData.ts +37 -24
  112. package/ui/hooks/useMandala.ts +6 -6
  113. package/ui/hooks/useSpendableBalance.ts +3 -1
  114. package/ui/importDatabases.ts +124 -29
  115. package/ui/index.ts +4 -0
  116. package/ui/screens/PayScreen.tsx +14 -20
  117. package/ui/screens/SettingsScreen.tsx +3 -1
  118. package/ui/screens/WalletCheckScreen.tsx +7 -2
  119. package/ui/screens/WalletConfigScreen.tsx +13 -1
  120. package/ui/screens/WalletHomeScreen.tsx +40 -14
  121. package/ui/screens/tokenRowTitle.ts +28 -15
  122. package/ui/tokenFormat.ts +133 -69
  123. package/ui/tokenStatus.ts +5 -8
  124. package/core/localpay/settlementAck.ts +0 -155
  125. package/core/mandala/basketMigration.ts +0 -166
  126. package/core/mandala/bundle.ts +0 -244
  127. package/core/mandala/drain.ts +0 -754
  128. package/core/mandala/inboxPrehold.ts +0 -193
  129. package/core/mandala/permissionModule.ts +0 -931
  130. package/core/mandala/resendTransfer.ts +0 -178
  131. package/core/mandala/settlementStore.ts +0 -434
  132. package/core/mandala/token.ts +0 -45
  133. package/core/offline/tokenFrames.ts +0 -197
@@ -15,6 +15,7 @@ import PayCellRow from './PayCellRow'
15
15
  import { ConsequenceNote, PayAmountField, PayField } from './PayForm'
16
16
  import { spacing } from '@bsv/expo-wallet-toolbox'
17
17
  import type { TokenAssetStatus, TokenBalance } from '../../../core/mandala/runtime'
18
+ import { tokenUnitsFromField } from '../../tokenFormat'
18
19
 
19
20
  export type RequestMethod = 'get-nearby' | 'get-handle' | 'get-address'
20
21
 
@@ -55,6 +56,15 @@ export function requestSatsFrom(text: string): number | undefined {
55
56
  return Number.isFinite(n) && n > 0 ? n : undefined
56
57
  }
57
58
 
59
+ /**
60
+ * Exact token units from the hub's raw field in token mode, or undefined for
61
+ * an open request. The field emits whole units as plain digits; anything else
62
+ * is not a figure, and nothing is rounded into one or passed through a number.
63
+ */
64
+ export function requestUnitsFrom(text: string): bigint | undefined {
65
+ return tokenUnitsFromField(text)
66
+ }
67
+
58
68
  export default function RequestHub({
59
69
  requestSats,
60
70
  onChangeRequestSats,
@@ -43,7 +43,7 @@ import { useAssetStatus, useMandala } from '../../hooks/useMandala'
43
43
  import { useSpendableBalance } from '../../hooks/useSpendableBalance'
44
44
  import { useContactsStore } from '../../hooks/useContactsStore'
45
45
  import ContactSigil from '../wallet/ContactSigil'
46
- import { formatTokenAmount, formatTokenAmountWithUnit, shortAssetId } from '../../tokenFormat'
46
+ import { formatTokenAmount, formatTokenAmountWithUnit, shortAssetId, tokenUnitsFromField } from '../../tokenFormat'
47
47
  import { abbreviateKey } from '../../../core/pay/counterparty'
48
48
  import type { ContactRow } from '../../../core/contacts/contactsStore'
49
49
  import { createHandleRegistryClient } from '../../../core/identity/handleRegistry/client'
@@ -240,13 +240,13 @@ export interface UniversalSendProps {
240
240
  /** Prefilled amount in satoshis from a peerpay link or `?sats=`. */
241
241
  initialSats?: number
242
242
  /**
243
- * Prefilled TOKEN amount from a peerpay link's `asset=`/`amount=`: base units
244
- * of that asset. Takes precedence over `initialSats` — a link names one money.
243
+ * Prefilled TOKEN amount from a peerpay link's `asset=`/`amount=`: exact
244
+ * token units of that asset. Takes precedence over `initialSats` — a link names one money.
245
245
  * The figure only ever shows while that asset is the one selected (see the
246
246
  * unit tag on the figure below), so a link for a token this wallet turns out
247
247
  * not to hold seeds nothing.
248
248
  */
249
- initialTokenAmount?: { assetId: string; baseUnits: number }
249
+ initialTokenAmount?: { assetId: string; units: bigint }
250
250
  /**
251
251
  * The asset this payment is denominated in. `null` is BSV — the default, and
252
252
  * what every existing flow assumes. Controlled by the Pay screen so the
@@ -275,9 +275,9 @@ interface Figure {
275
275
  unit: string | null
276
276
  }
277
277
 
278
- function seedFigure(sats?: number, token?: { assetId: string; baseUnits: number }): Figure {
279
- if (token && Number.isFinite(token.baseUnits) && token.baseUnits > 0) {
280
- return { text: String(Math.round(token.baseUnits)), unit: token.assetId }
278
+ function seedFigure(sats?: number, token?: { assetId: string; units: bigint }): Figure {
279
+ if (token && typeof token.units === 'bigint' && token.units > 0n) {
280
+ return { text: token.units.toString(), unit: token.assetId }
281
281
  }
282
282
  if (sats && sats > 0) return { text: String(sats), unit: null }
283
283
  return { text: '', unit: null }
@@ -782,17 +782,17 @@ function UniversalSendInner(
782
782
  // Keyed on the figure's parts, not the object: a host that rebuilds the
783
783
  // token-amount object every render must not reseed over what is being typed.
784
784
  const initialTokenAssetId = initialTokenAmount?.assetId
785
- const initialTokenBaseUnits = initialTokenAmount?.baseUnits
785
+ const initialTokenUnits = initialTokenAmount?.units
786
786
  useEffect(() => {
787
787
  setFigure(
788
788
  seedFigure(
789
789
  initialSats,
790
- initialTokenAssetId !== undefined && initialTokenBaseUnits !== undefined
791
- ? { assetId: initialTokenAssetId, baseUnits: initialTokenBaseUnits }
790
+ initialTokenAssetId !== undefined && initialTokenUnits !== undefined
791
+ ? { assetId: initialTokenAssetId, units: initialTokenUnits }
792
792
  : undefined
793
793
  )
794
794
  )
795
- }, [initialSats, initialTokenAssetId, initialTokenBaseUnits])
795
+ }, [initialSats, initialTokenAssetId, initialTokenUnits])
796
796
  useEffect(() => {
797
797
  setNotice(initialNotice ? { type: 'error', message: initialNotice } : null)
798
798
  }, [initialNotice])
@@ -976,7 +976,7 @@ function UniversalSendInner(
976
976
  * allowed to promise "nothing was sent".
977
977
  */
978
978
  const sendToken = useCallback(
979
- async (to: Extract<RecipientTarget, { kind: 'handle' }>, baseUnits: number): Promise<boolean> => {
979
+ async (to: Extract<RecipientTarget, { kind: 'handle' }>, units: bigint): Promise<boolean> => {
980
980
  const runtime = mandala.runtime
981
981
  if (!runtime || !asset) throw new Error(t('wallet_not_ready'))
982
982
  const ctx = {
@@ -988,7 +988,7 @@ function UniversalSendInner(
988
988
  const result = await runtime.sendToHandle({
989
989
  assetId: asset.assetId,
990
990
  recipientIdentityKey: to.identityKey,
991
- baseUnits,
991
+ units,
992
992
  ...(note.trim() ? { note: note.trim() } : {})
993
993
  })
994
994
  if (result.kind !== 'sent') {
@@ -1010,7 +1010,7 @@ function UniversalSendInner(
1010
1010
  // Never falls back to the satoshi renderer: a token figure it could not
1011
1011
  // format would print as satoshis, which is a wrong number rather than a
1012
1012
  // missing one.
1013
- amountText: formatTokenAmountWithUnit(baseUnits, asset) ?? t('token_row_amount_pending'),
1013
+ amountText: formatTokenAmountWithUnit(units, asset) ?? t('token_row_amount_pending'),
1014
1014
  // `notified === false` takes priority: the money moved either way, but
1015
1015
  // the recipient has not been TOLD it did (the lib's own notify retries
1016
1016
  // in the background — see `TokenSendResult.notified`), and that is the
@@ -1058,10 +1058,12 @@ function UniversalSendInner(
1058
1058
  flashResult({ type: 'error', message: t('wallet_not_ready') })
1059
1059
  return
1060
1060
  }
1061
- // The same integer either way — satoshis on the BSV rail, base units of the
1062
- // asset on the token rail. The field emits whole units in both modes.
1061
+ // Satoshis on the BSV rail; exact bigint token units on the token rail,
1062
+ // never through a number (a figure past 2^53 would round). The field
1063
+ // emits whole units as plain digits in both modes.
1063
1064
  const amount = Math.round(Number(sendAmount))
1064
- if (!Number.isFinite(amount) || amount <= 0) {
1065
+ const units = asset ? tokenUnitsFromField(sendAmount) : undefined
1066
+ if (asset ? units === undefined : !Number.isFinite(amount) || amount <= 0) {
1065
1067
  flashResult({ type: 'error', message: t('enter_valid_amount') })
1066
1068
  return
1067
1069
  }
@@ -1072,8 +1074,8 @@ function UniversalSendInner(
1072
1074
  if (asset) {
1073
1075
  // D4: a token has no address rail, and `canSend` has already refused
1074
1076
  // one. This is the second gate, because the first is a render.
1075
- if (target.kind !== 'handle') return
1076
- const sent = await sendToken(target, amount)
1077
+ if (target.kind !== 'handle' || units === undefined) return
1078
+ const sent = await sendToken(target, units)
1077
1079
  if (!sent) return
1078
1080
  } else if (target.kind === 'handle') await sendHandle(target, amount)
1079
1081
  else await sendAddress(target, amount)
@@ -1222,7 +1224,8 @@ function UniversalSendInner(
1222
1224
  [storage, wallet, adminOriginator, peerPayClient, handleRetry, loadOutbox, t]
1223
1225
  )
1224
1226
 
1225
- const amountOk = Number(sendAmount) > 0
1227
+ // Token mode: an exact bigint figure (a number would round past 2^53).
1228
+ const amountOk = asset ? tokenUnitsFromField(sendAmount) !== undefined : Number(sendAmount) > 0
1226
1229
  const isHandle = target?.kind === 'handle'
1227
1230
  const isAddress = target?.kind === 'address'
1228
1231
  /**
@@ -1246,8 +1249,8 @@ function UniversalSendInner(
1246
1249
  // Advisory, and it may only ever say "no". Nothing here promises a send will
1247
1250
  // work; `resolveAssetState` and the registry both fail open, so a pre-flight
1248
1251
  // that said "this will work" would be the one claim they cannot support.
1249
- const baseUnits = asset ? Math.round(Number(sendAmount)) || 0 : 0
1250
- const overBalance = !!holding && baseUnits > holding.baseUnits
1252
+ const tokenUnits = asset ? (tokenUnitsFromField(sendAmount) ?? 0n) : 0n
1253
+ const overBalance = !!holding && tokenUnits > holding.units
1251
1254
  /**
1252
1255
  * The runtime's plain reason this recipient cannot be paid in this asset —
1253
1256
  * an address (D4), an unregistered identity, a blocked one.
@@ -1271,7 +1274,7 @@ function UniversalSendInner(
1271
1274
  // Only asked once the recipient resolved to a handle — an address is already
1272
1275
  // refused by D4 above, and half-typed text is not a recipient yet.
1273
1276
  const recipientRegistryRefused = !!asset && target?.kind === 'handle' && recipientAdmitted === false
1274
- const frozenBaseUnits = assetStatus?.frozenBaseUnits ?? 0
1277
+ const frozenUnits = assetStatus?.frozenUnits ?? 0n
1275
1278
  const tokenFormValid =
1276
1279
  !!asset &&
1277
1280
  isHandle &&
@@ -1333,10 +1336,10 @@ function UniversalSendInner(
1333
1336
  }
1334
1337
  : overBalance
1335
1338
  ? { key: 'pay_asset_over_balance', values: { ticker: asset.ticker } }
1336
- : frozenBaseUnits > 0
1339
+ : frozenUnits > 0n
1337
1340
  ? {
1338
1341
  key: 'pay_asset_frozen_note',
1339
- values: { ticker: asset.ticker, amount: formatTokenAmount(frozenBaseUnits, asset.decimals) ?? '' }
1342
+ values: { ticker: asset.ticker, amount: formatTokenAmount(frozenUnits, asset.decimals) ?? '' }
1340
1343
  }
1341
1344
  : spendableSats === 0
1342
1345
  ? { key: 'pay_asset_needs_bsv', values: { ticker: asset.ticker }, action: 'get-bsv' }
@@ -1376,7 +1379,7 @@ function UniversalSendInner(
1376
1379
  // "12.00 USD" on a BSV send reads as a stablecoin.
1377
1380
  const sendSats = Math.round(Number(sendAmount)) || 0
1378
1381
  const reviewAmount = asset
1379
- ? { value: formatTokenAmount(baseUnits, asset.decimals) ?? '', unit: asset.ticker }
1382
+ ? { value: formatTokenAmount(tokenUnits, asset.decimals) ?? '', unit: asset.ticker }
1380
1383
  : { value: formatSatoshisAsBsvDecimal(sendSats), unit: 'BSV' }
1381
1384
  // XR-044: the last on-screen chance to catch a look-alike asset before Send
1382
1385
  // — issuerName is unverified, but assetId cannot collide, so the review
@@ -1525,9 +1528,9 @@ function UniversalSendInner(
1525
1528
  value={sendAmount}
1526
1529
  onChangeText={setSendAmount}
1527
1530
  asset={asset ? { ticker: asset.ticker, decimals: asset.decimals } : undefined}
1528
- maxValue={holding ? String(holding.baseUnits) : undefined}
1531
+ maxValue={holding ? holding.units.toString() : undefined}
1529
1532
  availableText={
1530
- holding ? (formatTokenAmount(holding.baseUnits, holding.asset.decimals) ?? undefined) : undefined
1533
+ holding ? (formatTokenAmount(holding.units, holding.asset.decimals) ?? undefined) : undefined
1531
1534
  }
1532
1535
  />
1533
1536
 
@@ -62,14 +62,14 @@ export interface UseRecipientInputOptions {
62
62
  * request that has to wait (holdings not known yet) is applied only if the
63
63
  * recipient is still the one the link named.
64
64
  */
65
- export type PeerPayRequest = { identityKey: string; sats?: number; asset?: string; amount?: number }
65
+ export type PeerPayRequest = { identityKey: string; sats?: number; asset?: string; amount?: bigint }
66
66
 
67
67
  /** The request a classified handle carries, or null when it named no money at all. */
68
68
  function requestOf(input: {
69
69
  identityKey: string
70
70
  sats?: number
71
71
  asset?: string
72
- amount?: number
72
+ amount?: bigint
73
73
  }): PeerPayRequest | null {
74
74
  if (input.sats === undefined && input.asset === undefined) return null
75
75
  return {