@bsv/expo-wallet-toolbox 0.5.0 → 0.7.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 +255 -0
- package/README.md +19 -0
- package/core/amountFormatHelpers.ts +238 -34
- package/core/backup/RemoteSyncReader.ts +2 -5
- package/core/backup/codec.ts +2 -2
- package/core/backup/push.ts +1 -1
- package/core/config.tsx +12 -5
- package/core/contacts/contactActivity.ts +153 -0
- package/core/contacts/contactCache.ts +41 -0
- package/core/contacts/contactsStore.ts +183 -0
- package/core/context/WalletContext.tsx +55 -10
- 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 +52 -14
- package/core/headers/headerStore.ts +7 -10
- package/core/i18n/translations.tsx +1234 -74
- package/core/identity/contactLink.ts +36 -0
- package/core/identity/handleRegistry/client.ts +285 -0
- package/core/identity/handleRegistry/profileCert.ts +254 -0
- package/core/identity/handleRegistry/registration.ts +559 -0
- package/core/identity/handleRegistry/resolver.ts +247 -0
- package/core/identity/handleRegistry/rules.ts +104 -0
- package/core/index.ts +20 -2
- package/core/localpay/pendingAborts.ts +5 -1
- package/core/monitor/MonitorSupervisor.ts +121 -0
- package/core/monitor/TaskBackupPush.ts +1 -1
- package/core/monitor/TaskCreditInbox.ts +1 -1
- package/core/monitor/TaskDrainOutbox.ts +1 -1
- package/core/monitor/TaskSendOffline.ts +1 -1
- package/core/net/online.ts +49 -0
- package/core/numberFormat.ts +19 -0
- package/core/offline/plan.ts +1 -1
- package/core/pay/recordProof.ts +2 -6
- package/core/services/arcadeBroadcastProvider.ts +1 -4
- package/core/services/exchangeRate.ts +1 -1
- package/core/services/externalOrigin.ts +5 -2
- package/core/services/vault/transfers.ts +2 -2
- package/core/services/walletServiceConfig.ts +31 -20
- package/core/storage/StorageExpoSQLite.ts +41 -34
- package/core/storage/methods/findSql.ts +3 -0
- package/core/storage/methods/listActionsSql.ts +8 -5
- package/core/storage/methods/listOutputsSql.ts +2 -2
- package/core/storage/methods/processOfflineActions.ts +2 -3
- package/core/storage/methods/reviewStatusSql.ts +7 -2
- package/core/storage/schema/createTables.ts +29 -0
- package/core/toolboxConfig.ts +70 -0
- package/core/toolboxTypes.ts +40 -0
- package/core/userAvatar.ts +79 -0
- package/core/walletMonitor.ts +8 -3
- package/package.json +7 -7
- package/ui/activityFilter.ts +115 -0
- package/ui/components/pay/PaymentSuccessOverlay.tsx +46 -6
- package/ui/components/pay/RecipientField.tsx +84 -70
- package/ui/components/pay/StepBar.tsx +32 -0
- package/ui/components/pay/UniversalSend.tsx +677 -149
- package/ui/components/ui/PencilEditField.tsx +109 -0
- package/ui/components/ui/SlideOverFromRight.tsx +86 -0
- package/ui/components/wallet/ActivityRow.tsx +222 -120
- package/ui/components/wallet/AmountInput.tsx +18 -6
- 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/hooks/useContactsStore.ts +10 -0
- package/ui/hooks/useMandala.ts +18 -1
- package/ui/hooks/useOnline.ts +65 -20
- package/ui/index.ts +26 -1
- package/ui/screens/ContactScreen.tsx +289 -0
- package/ui/screens/ContactsScreen.tsx +373 -0
- package/ui/screens/IdentifierScreen.tsx +234 -0
- package/ui/screens/NewContactScreen.tsx +362 -0
- package/ui/screens/PayScreen.tsx +20 -7
- package/ui/screens/ProfileScreen.tsx +783 -0
- package/ui/screens/TransactionDetailScreen.tsx +419 -0
- package/ui/screens/TrustScreen.tsx +1 -1
- package/ui/screens/WalletConfigScreen.tsx +42 -0
- package/ui/screens/WalletHomeScreen.tsx +528 -83
- package/ui/tokenStatus.ts +21 -0
- package/ui/txStatus.ts +43 -5
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which host answers for a domain.
|
|
3
|
+
*
|
|
4
|
+
* Our own domain is pinned by configuration: the host was stated by the build,
|
|
5
|
+
* so asking DNS about it would only add a way to be told something else. Every
|
|
6
|
+
* other domain resolves the way Paymail does — DNSSEC-authenticated SRV, then
|
|
7
|
+
* the well-known capability document — because a foreign domain is the one
|
|
8
|
+
* place this feature trusts an answer it did not bring with it.
|
|
9
|
+
*
|
|
10
|
+
* The AD requirement has one exception, and it is not a loophole: an SRV whose
|
|
11
|
+
* target IS the domain names no new host, so an unauthenticated answer moves
|
|
12
|
+
* nothing that `https://<domain>` would not already have reached.
|
|
13
|
+
*/
|
|
14
|
+
import { BRFC_LOOKUP, BRFC_REVERSE_LOOKUP, looksLikeDomain } from './rules'
|
|
15
|
+
|
|
16
|
+
/** The domain this build's own handles live under, and the host serving it. */
|
|
17
|
+
export interface RegistryPin {
|
|
18
|
+
readonly domain: string
|
|
19
|
+
readonly url: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The two routes, already substituted and encoded. */
|
|
23
|
+
export interface RegistryEndpoints {
|
|
24
|
+
readonly domain: string
|
|
25
|
+
search(query: string): string
|
|
26
|
+
reverse(pubkey: string): string
|
|
27
|
+
/** True for the configured domain, which skipped discovery entirely. */
|
|
28
|
+
readonly pinned: boolean
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface RegistryResolver {
|
|
32
|
+
resolve(domain: string): Promise<RegistryEndpoints | null>
|
|
33
|
+
/** Test seam, and the way a "try again" control forgets a failed lookup. */
|
|
34
|
+
clearCache(): void
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const REGISTRY_TIMEOUT_MS = 8000
|
|
38
|
+
export const RESOLVER_SUCCESS_TTL_MS = 10 * 60 * 1000
|
|
39
|
+
export const RESOLVER_FAILURE_TTL_MS = 60 * 1000
|
|
40
|
+
export const DOH_ENDPOINTS: readonly string[] = ['https://cloudflare-dns.com/dns-query', 'https://dns.google/resolve']
|
|
41
|
+
|
|
42
|
+
const SRV_TYPE = 33
|
|
43
|
+
const NOERROR = 0
|
|
44
|
+
const NXDOMAIN = 3
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* One attempt, bounded — headers AND body. There is no shared fetch helper in
|
|
48
|
+
* this codebase — every call site hand-rolls this — so this feature's two
|
|
49
|
+
* networking modules share exactly one copy of it rather than two.
|
|
50
|
+
*
|
|
51
|
+
* A caller's own signal is composed, not replaced: the deadline is ours to add,
|
|
52
|
+
* but a screen that cancels on unmount must still cancel the request rather than
|
|
53
|
+
* hold it to the full eight seconds. `core/backup/client.ts` links the two the
|
|
54
|
+
* same way — and, for the same reason it does, the deadline outlives the
|
|
55
|
+
* headers: every caller in this feature reads a JSON body, and a connection
|
|
56
|
+
* that delivers headers and then stalls (a captive portal, a half-closed load
|
|
57
|
+
* balancer, a mobile link that drops mid-body) leaves `json()` pending for
|
|
58
|
+
* ever. `registration.ts` serialises every write behind one promise, so a
|
|
59
|
+
* single stalled body would wedge every later write for the life of the
|
|
60
|
+
* process, with no toast and no timeout.
|
|
61
|
+
*/
|
|
62
|
+
export async function fetchWithTimeout(fetchImpl: typeof fetch, url: string, init?: RequestInit): Promise<Response> {
|
|
63
|
+
const controller = new AbortController()
|
|
64
|
+
const caller = init?.signal ?? null
|
|
65
|
+
const onAbort = () => controller.abort(caller?.reason)
|
|
66
|
+
if (caller?.aborted) onAbort()
|
|
67
|
+
caller?.addEventListener('abort', onAbort)
|
|
68
|
+
const release = () => caller?.removeEventListener('abort', onAbort)
|
|
69
|
+
const startedAt = Date.now()
|
|
70
|
+
const timer = setTimeout(() => controller.abort(), REGISTRY_TIMEOUT_MS)
|
|
71
|
+
let response: Response
|
|
72
|
+
try {
|
|
73
|
+
response = await fetchImpl(url, { ...init, signal: controller.signal })
|
|
74
|
+
} catch (e) {
|
|
75
|
+
release()
|
|
76
|
+
throw e
|
|
77
|
+
} finally {
|
|
78
|
+
clearTimeout(timer)
|
|
79
|
+
}
|
|
80
|
+
const readJson = typeof response.json === 'function' ? response.json.bind(response) : null
|
|
81
|
+
if (!readJson) {
|
|
82
|
+
release()
|
|
83
|
+
return response
|
|
84
|
+
}
|
|
85
|
+
// Rebinding the reader rather than buffering the body here: what each caller
|
|
86
|
+
// asks for is all that is ever allocated. The abort is what stops a real
|
|
87
|
+
// stalled stream; the rejection is what stops a transport that ignores it.
|
|
88
|
+
response.json = async () => {
|
|
89
|
+
let bodyTimer: ReturnType<typeof setTimeout> | undefined
|
|
90
|
+
try {
|
|
91
|
+
return await new Promise((resolve, reject) => {
|
|
92
|
+
bodyTimer = setTimeout(
|
|
93
|
+
() => {
|
|
94
|
+
controller.abort()
|
|
95
|
+
reject(new Error(`handleRegistry: no body from ${url} after ${REGISTRY_TIMEOUT_MS}ms`))
|
|
96
|
+
},
|
|
97
|
+
Math.max(0, REGISTRY_TIMEOUT_MS - (Date.now() - startedAt))
|
|
98
|
+
)
|
|
99
|
+
void readJson().then(resolve, reject)
|
|
100
|
+
})
|
|
101
|
+
} finally {
|
|
102
|
+
if (bodyTimer !== undefined) clearTimeout(bodyTimer)
|
|
103
|
+
release()
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return response
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
interface SrvRecord {
|
|
110
|
+
priority: number
|
|
111
|
+
weight: number
|
|
112
|
+
port: number
|
|
113
|
+
target: string
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
interface DohAnswer {
|
|
117
|
+
type?: number
|
|
118
|
+
data?: string
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
interface DohResponse {
|
|
122
|
+
Status?: number
|
|
123
|
+
AD?: boolean
|
|
124
|
+
Answer?: DohAnswer[]
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function parseSrv(data: string): SrvRecord | null {
|
|
128
|
+
const parts = data.trim().split(/\s+/)
|
|
129
|
+
if (parts.length < 4) return null
|
|
130
|
+
const priority = Number(parts[0])
|
|
131
|
+
const weight = Number(parts[1])
|
|
132
|
+
const port = Number(parts[2])
|
|
133
|
+
const target = parts[3].replace(/\.$/, '').toLowerCase()
|
|
134
|
+
if (!Number.isFinite(priority) || !Number.isFinite(weight) || target === '') return null
|
|
135
|
+
// A port that cannot be dialled does not belong in a URL: `:70000`, `:-1` and
|
|
136
|
+
// `:44.3` are each read by some parser as something other than a port.
|
|
137
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) return null
|
|
138
|
+
return { priority, weight, port, target }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function templateOf(value: unknown, placeholder: string): string | null {
|
|
142
|
+
if (typeof value !== 'string' || !value.startsWith('https://')) return null
|
|
143
|
+
return value.includes(placeholder) ? value : null
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function createRegistryResolver(args: {
|
|
147
|
+
pinned?: RegistryPin
|
|
148
|
+
fetchImpl?: typeof fetch
|
|
149
|
+
now?: () => number
|
|
150
|
+
dohEndpoints?: readonly string[]
|
|
151
|
+
}): RegistryResolver {
|
|
152
|
+
const fetchImpl = args.fetchImpl ?? ((input: RequestInfo | URL, init?: RequestInit) => fetch(input, init))
|
|
153
|
+
const now = args.now ?? (() => Date.now())
|
|
154
|
+
const dohEndpoints = args.dohEndpoints ?? DOH_ENDPOINTS
|
|
155
|
+
const pinnedDomain = args.pinned?.domain.trim().toLowerCase() ?? ''
|
|
156
|
+
const pinnedOrigin = args.pinned?.url.trim().replace(/\/+$/, '') ?? ''
|
|
157
|
+
const cache = new Map<string, { at: number; value: RegistryEndpoints | null }>()
|
|
158
|
+
|
|
159
|
+
const pinnedEndpoints: RegistryEndpoints | null =
|
|
160
|
+
pinnedDomain !== '' && pinnedOrigin !== ''
|
|
161
|
+
? {
|
|
162
|
+
domain: pinnedDomain,
|
|
163
|
+
search: query => `${pinnedOrigin}/api/handle/${encodeURIComponent(query)}`,
|
|
164
|
+
reverse: pubkey => `${pinnedOrigin}/api/identityKey/${encodeURIComponent(pubkey)}`,
|
|
165
|
+
pinned: true
|
|
166
|
+
}
|
|
167
|
+
: null
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The SRV answer, or the Paymail fallback, or null when DNS said nothing usable.
|
|
171
|
+
*
|
|
172
|
+
* Only NOERROR and NXDOMAIN are answers. Any other status — SERVFAIL, which
|
|
173
|
+
* is what a resolver returns for a DNSSEC-bogus zone under `do=1`, or REFUSED
|
|
174
|
+
* — carries no Answer either, so treating it as "no SRV" would quietly turn a
|
|
175
|
+
* DNSSEC failure into `https://<domain>`. It is a failure of that resolver
|
|
176
|
+
* instead, and the next one gets the question.
|
|
177
|
+
*/
|
|
178
|
+
async function lookupSrv(domain: string): Promise<{ target: string; port: number } | null> {
|
|
179
|
+
const fallback = { target: domain, port: 443 }
|
|
180
|
+
for (const endpoint of dohEndpoints) {
|
|
181
|
+
let body: DohResponse
|
|
182
|
+
try {
|
|
183
|
+
const url = `${endpoint}?name=${encodeURIComponent(`_bsvalias._tcp.${domain}`)}&type=SRV&do=1`
|
|
184
|
+
const res = await fetchWithTimeout(fetchImpl, url, { headers: { accept: 'application/dns-json' } })
|
|
185
|
+
if (!res.ok) continue
|
|
186
|
+
body = (await res.json()) as DohResponse
|
|
187
|
+
} catch {
|
|
188
|
+
continue
|
|
189
|
+
}
|
|
190
|
+
if (body.Status !== NOERROR && body.Status !== NXDOMAIN) continue
|
|
191
|
+
const records = (body.Answer ?? [])
|
|
192
|
+
.filter(answer => answer.type === SRV_TYPE && typeof answer.data === 'string')
|
|
193
|
+
.map(answer => parseSrv(answer.data as string))
|
|
194
|
+
.filter((record): record is SrvRecord => record !== null)
|
|
195
|
+
if (body.Status === NXDOMAIN || records.length === 0) return fallback
|
|
196
|
+
records.sort((a, b) => a.priority - b.priority || b.weight - a.weight)
|
|
197
|
+
const best = records[0]
|
|
198
|
+
if (body.AD !== true && best.target !== domain) return null
|
|
199
|
+
return { target: best.target, port: best.port }
|
|
200
|
+
}
|
|
201
|
+
return null
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async function discover(domain: string): Promise<RegistryEndpoints | null> {
|
|
205
|
+
const host = await lookupSrv(domain)
|
|
206
|
+
if (!host) return null
|
|
207
|
+
const origin = `https://${host.target}${host.port === 443 ? '' : `:${host.port}`}`
|
|
208
|
+
let capabilities: Record<string, unknown>
|
|
209
|
+
try {
|
|
210
|
+
const res = await fetchWithTimeout(fetchImpl, `${origin}/.well-known/bsvalias`, {
|
|
211
|
+
headers: { accept: 'application/json' }
|
|
212
|
+
})
|
|
213
|
+
if (!res.ok) return null
|
|
214
|
+
const body = (await res.json()) as { capabilities?: Record<string, unknown> }
|
|
215
|
+
capabilities = body?.capabilities ?? {}
|
|
216
|
+
} catch {
|
|
217
|
+
return null
|
|
218
|
+
}
|
|
219
|
+
const searchTemplate = templateOf(capabilities[BRFC_LOOKUP], '{query}')
|
|
220
|
+
const reverseTemplate = templateOf(capabilities[BRFC_REVERSE_LOOKUP], '{pubkey}')
|
|
221
|
+
if (!searchTemplate || !reverseTemplate) return null
|
|
222
|
+
return {
|
|
223
|
+
domain,
|
|
224
|
+
search: query => searchTemplate.replace('{query}', encodeURIComponent(query)),
|
|
225
|
+
reverse: pubkey => reverseTemplate.replace('{pubkey}', encodeURIComponent(pubkey)),
|
|
226
|
+
pinned: false
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
async resolve(rawDomain) {
|
|
232
|
+
const domain = rawDomain.trim().toLowerCase()
|
|
233
|
+
if (pinnedEndpoints && domain === pinnedDomain) return pinnedEndpoints
|
|
234
|
+
// Ahead of the cache, not inside discovery: a half-typed domain is not an
|
|
235
|
+
// answer, and the cache has no eviction — search resolves as the user types.
|
|
236
|
+
if (!looksLikeDomain(domain)) return null
|
|
237
|
+
const hit = cache.get(domain)
|
|
238
|
+
if (hit && now() - hit.at < (hit.value ? RESOLVER_SUCCESS_TTL_MS : RESOLVER_FAILURE_TTL_MS)) return hit.value
|
|
239
|
+
const value = await discover(domain)
|
|
240
|
+
cache.set(domain, { at: now(), value })
|
|
241
|
+
return value
|
|
242
|
+
},
|
|
243
|
+
clearCache() {
|
|
244
|
+
cache.clear()
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handle-registry rules — the constants, and the string checks that are pure.
|
|
3
|
+
*
|
|
4
|
+
* Every value here mirrors a rule the registry enforces on write
|
|
5
|
+
* (go-message-box-server `pkg/handles`, `pkg/profilecert`). Restating them on
|
|
6
|
+
* this side is not distrust of the server: a client that cannot tell a valid
|
|
7
|
+
* handle from an invalid one has to ask the network what the user just typed,
|
|
8
|
+
* once per keystroke.
|
|
9
|
+
*
|
|
10
|
+
* "Handle" already names the identity-key pay rail in this codebase
|
|
11
|
+
* (`RailId 'handle'`, core/pay/rails/handle.ts). Nothing here reuses that word
|
|
12
|
+
* as a rail — a handle is the local part of a `handle@domain` paymail.
|
|
13
|
+
*/
|
|
14
|
+
import { Utils } from '@bsv/sdk'
|
|
15
|
+
|
|
16
|
+
/** base64(SHA-256("public profile lookup")) — the profile certificate's type. */
|
|
17
|
+
export const PROFILE_CERT_TYPE = 'SbatVXXssDW3AO0J9bxIljkHGbPBGCVAXg94gXFf0cE='
|
|
18
|
+
/** BRFC id of the `public profile lookup` capability — search. */
|
|
19
|
+
export const BRFC_LOOKUP = '0ace65da5987'
|
|
20
|
+
/** BRFC id of the `public profile reverse lookup` capability — key → profile. */
|
|
21
|
+
export const BRFC_REVERSE_LOOKUP = '43dcf83ddc5f'
|
|
22
|
+
/** There is no on-chain revocation: a profile is replaced or tombstoned. */
|
|
23
|
+
export const ZERO_OUTPOINT = `${'0'.repeat(64)}.0`
|
|
24
|
+
|
|
25
|
+
export const MAX_FIELDS = 32
|
|
26
|
+
/** Exclusive — go-sdk's own `CertificateFieldNameUnder50Bytes`. */
|
|
27
|
+
export const MAX_FIELD_NAME_BYTES = 50
|
|
28
|
+
export const MAX_FIELD_VALUE_BYTES = 1024
|
|
29
|
+
export const MAX_CERT_BODY_BYTES = 16384
|
|
30
|
+
|
|
31
|
+
/** Shorter than this and the search route answers nothing at all. */
|
|
32
|
+
export const SEARCH_MIN_QUERY_LENGTH = 2
|
|
33
|
+
/** Longer than this and the search route answers nothing at all. */
|
|
34
|
+
export const MAX_SEARCH_QUERY_LENGTH = 32
|
|
35
|
+
/**
|
|
36
|
+
* The most rows the search route ever answers with (`searchLimit` in
|
|
37
|
+
* go-message-box-server's own handler).
|
|
38
|
+
*
|
|
39
|
+
* Restated on this side because the host that answers a search is not always
|
|
40
|
+
* ours: a complete foreign domain the user typed is resolved and asked, and
|
|
41
|
+
* nothing binds it to our server's limit. Every row costs one ECDSA
|
|
42
|
+
* verification on the only thread the recipient field has, so an uncapped
|
|
43
|
+
* answer is an unbounded freeze rather than a long list.
|
|
44
|
+
*/
|
|
45
|
+
export const MAX_SEARCH_RESULTS = 10
|
|
46
|
+
|
|
47
|
+
const HANDLE_FORMAT = /^[a-z0-9][a-z0-9._-]{1,30}[a-z0-9]$/
|
|
48
|
+
const DOMAIN_FORMAT = /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)+$/
|
|
49
|
+
const SEARCH_QUERY_FORMAT = /^[a-z0-9._-]+$/
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Path segments a query must never become, because the search route never sees
|
|
53
|
+
* them. `GET /api/handle/available` is mounted ahead of
|
|
54
|
+
* `GET /api/handle/{query}` and answers `400 ERR_INVALID_LOOKUP`; `..` does not
|
|
55
|
+
* even leave the device intact, since the URL parser collapses a double-dot
|
|
56
|
+
* segment (`https://host/api/handle/..` is `https://host/api/`, which nothing
|
|
57
|
+
* serves) and encoding cannot rescue it — the spec reads `%2e%2e` as the same
|
|
58
|
+
* segment. Either one `search` would surface as "the registry is down", for an
|
|
59
|
+
* ordinary English word or for two dots. A lone `.` is already too short.
|
|
60
|
+
*/
|
|
61
|
+
const RESERVED_QUERY_PATHS = new Set(['available', '..'])
|
|
62
|
+
|
|
63
|
+
/** 3-32 characters of a-z 0-9 . _ - beginning and ending with a letter or digit. */
|
|
64
|
+
export function isValidHandleFormat(handle: string): boolean {
|
|
65
|
+
return HANDLE_FORMAT.test(handle)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A dotted, all-lowercase domain. Says nothing about whether it resolves. */
|
|
69
|
+
export function looksLikeDomain(text: string): boolean {
|
|
70
|
+
return DOMAIN_FORMAT.test(text.trim().toLowerCase())
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Whether the registry would actually search for this, rather than answer an
|
|
75
|
+
* empty array or a 400. Restating the server's own gate here is what keeps a
|
|
76
|
+
* routine word — a too-long paste, an accent, a slash — from costing a request
|
|
77
|
+
* and, for the reserved paths, from raising an outage banner.
|
|
78
|
+
*/
|
|
79
|
+
export function isRoutableSearchQuery(query: string): boolean {
|
|
80
|
+
if (query.length < SEARCH_MIN_QUERY_LENGTH || query.length > MAX_SEARCH_QUERY_LENGTH) return false
|
|
81
|
+
if (!SEARCH_QUERY_FORMAT.test(query)) return false
|
|
82
|
+
return !RESERVED_QUERY_PATHS.has(query)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* `handle@domain`, trimmed and lowercased, or null when it is not one.
|
|
87
|
+
*
|
|
88
|
+
* Lowercasing here is what makes a typed `Dee@Example.com` comparable with the
|
|
89
|
+
* exact-lowercase `paymail` field the registry stores — the comparison the
|
|
90
|
+
* trust model turns on.
|
|
91
|
+
*/
|
|
92
|
+
export function parsePaymail(text: string): { handle: string; domain: string } | null {
|
|
93
|
+
const parts = text.trim().toLowerCase().split('@')
|
|
94
|
+
if (parts.length !== 2) return null
|
|
95
|
+
const [handle, domain] = parts
|
|
96
|
+
if (!isValidHandleFormat(handle) || !looksLikeDomain(domain)) return null
|
|
97
|
+
return { handle, domain }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** UTF-8 length, the unit every server-side size limit counts in. `Buffer` does
|
|
101
|
+
* not exist in React Native, so this goes through the SDK's own encoder. */
|
|
102
|
+
export function utf8ByteLength(text: string): number {
|
|
103
|
+
return Utils.toArray(text, 'utf8').length
|
|
104
|
+
}
|
package/core/index.ts
CHANGED
|
@@ -12,7 +12,7 @@ export { configureNewHeaderPolling } from './walletMonitor'
|
|
|
12
12
|
export { TaskSendOffline } from './monitor/TaskSendOffline'
|
|
13
13
|
export { TaskCreditInbox } from './monitor/TaskCreditInbox'
|
|
14
14
|
export { TaskDrainOutbox, drainUnsentEntries } from './monitor/TaskDrainOutbox'
|
|
15
|
-
export { getOnline, subscribeOnline } from './net/online'
|
|
15
|
+
export { getOnline, subscribeOnline, probeOnline } from './net/online'
|
|
16
16
|
export * from './logging'
|
|
17
17
|
export * from './diskSpace'
|
|
18
18
|
export type { AppChain, WalletChain } from './config'
|
|
@@ -33,12 +33,13 @@ export {
|
|
|
33
33
|
getBackupUrl,
|
|
34
34
|
getServiceConfig,
|
|
35
35
|
getMandalaEndpoints,
|
|
36
|
+
getHandleRegistryConfig,
|
|
36
37
|
isMandalaAvailable,
|
|
37
38
|
isVaultEnabled,
|
|
38
39
|
isVaultAvailable,
|
|
39
40
|
resetToolboxConfig
|
|
40
41
|
} from './toolboxConfig'
|
|
41
|
-
export type { ToolboxConfig, ToolboxServiceConfig, MandalaEndpointConfig } from './toolboxConfig'
|
|
42
|
+
export type { ToolboxConfig, ToolboxServiceConfig, MandalaEndpointConfig, HandleRegistryConfig } from './toolboxConfig'
|
|
42
43
|
|
|
43
44
|
// Theme tokens and providers
|
|
44
45
|
export { ThemeProvider, useTheme } from './theme/ThemeContext'
|
|
@@ -52,9 +53,11 @@ export { useThemeStyles } from './theme/useThemeStyles'
|
|
|
52
53
|
export {
|
|
53
54
|
formatAmount,
|
|
54
55
|
formatAmountParts,
|
|
56
|
+
splitAmountFraction,
|
|
55
57
|
formatAmountInInputUnit,
|
|
56
58
|
formatSatoshisAsFiat,
|
|
57
59
|
formatSatoshisAsBsv,
|
|
60
|
+
formatSatoshisExact,
|
|
58
61
|
formatSatoshisAsBsvDecimal,
|
|
59
62
|
parseDisplayToSatoshis,
|
|
60
63
|
getUnitLabel,
|
|
@@ -168,6 +171,9 @@ export {
|
|
|
168
171
|
type CoverVerifier
|
|
169
172
|
} from './mandala/bundle'
|
|
170
173
|
|
|
174
|
+
// Contacts
|
|
175
|
+
export { mergeContactCache, type ContactCache } from './contacts/contactCache'
|
|
176
|
+
|
|
171
177
|
// Local SQLite storage layer
|
|
172
178
|
export { StorageExpoSQLite } from './storage/StorageExpoSQLite'
|
|
173
179
|
export type { StorageExpoSQLiteOptions, TokenTransactionRepair } from './storage/StorageExpoSQLite'
|
|
@@ -401,3 +407,15 @@ export {
|
|
|
401
407
|
} from './context/WalletContext'
|
|
402
408
|
export type { WalletManagersSlice, WalletStatusSlice } from './context/WalletContext'
|
|
403
409
|
export { usePermissionQueue } from './hooks/usePermissionQueue'
|
|
410
|
+
|
|
411
|
+
export { getNumberLocale } from './numberFormat'
|
|
412
|
+
|
|
413
|
+
export {
|
|
414
|
+
getUserAvatarIcon,
|
|
415
|
+
loadUserAvatarIcon,
|
|
416
|
+
setUserAvatarIcon,
|
|
417
|
+
subscribeUserAvatar,
|
|
418
|
+
useUserAvatarIcon,
|
|
419
|
+
type AvatarIcon,
|
|
420
|
+
type AvatarIconFamily
|
|
421
|
+
} from './userAvatar'
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* A failed abort is a stuck UTXO, not a lost payment. The decline still
|
|
5
5
|
* stands; the reference is retried on the next wallet build.
|
|
6
6
|
*/
|
|
7
|
+
import { ADMIN_ORIGINATOR, LEGACY_ADMIN_ORIGINATOR } from '../config'
|
|
8
|
+
|
|
7
9
|
interface StorageLike {
|
|
8
10
|
getKeyValue: (key: string) => Promise<string | undefined>
|
|
9
11
|
setKeyValue: (key: string, value: string) => Promise<void>
|
|
@@ -28,7 +30,9 @@ export async function loadPendingAborts(storage: StorageLike): Promise<PendingAb
|
|
|
28
30
|
const reference = (item as { reference?: unknown }).reference
|
|
29
31
|
const originator = (item as { originator?: unknown }).originator
|
|
30
32
|
if (typeof reference === 'string' && reference && typeof originator === 'string') {
|
|
31
|
-
|
|
33
|
+
// An abort queued before the internal label moved into hostname space
|
|
34
|
+
// would otherwise fail originator validation on every replay, forever.
|
|
35
|
+
out.push({ reference, originator: originator === LEGACY_ADMIN_ORIGINATOR ? ADMIN_ORIGINATOR : originator })
|
|
32
36
|
}
|
|
33
37
|
}
|
|
34
38
|
return out
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs the wallet monitor's pass loop under a watchdog.
|
|
3
|
+
*
|
|
4
|
+
* `Monitor.startTasks()` is `while (running) { await runOnce(); await wait }`.
|
|
5
|
+
* Two things end that loop with no signal to anyone:
|
|
6
|
+
*
|
|
7
|
+
* · a task whose `await` never resolves — runOnce runs tasks sequentially
|
|
8
|
+
* with no per-task timeout, so one hung network or SQLite call parks every
|
|
9
|
+
* other task behind it, TaskSendWaiting included;
|
|
10
|
+
* · `runOnce`'s own catch block calling `logEvent`, which is a SQLite write —
|
|
11
|
+
* if that throws, the error escapes `startTasks` and the loop is gone.
|
|
12
|
+
*
|
|
13
|
+
* Seen 2026-09-18: Clock ticks stopped at 13:29:01 while the app stayed in
|
|
14
|
+
* use; two ordinary BSV sends made minutes later sat at `sending` until the
|
|
15
|
+
* app was force-quit, because nothing but the monitor broadcasts an `unsent`
|
|
16
|
+
* request.
|
|
17
|
+
*
|
|
18
|
+
* So the loop is driven from here instead. Each completed pass stamps
|
|
19
|
+
* `lastPassAt`; the host polls `isStalled` and calls `restart`, which starts a
|
|
20
|
+
* new generation of the loop on the SAME monitor. The old generation, if its
|
|
21
|
+
* hung await ever resolves, sees the generation change and exits rather than
|
|
22
|
+
* running alongside the new one. A pass that throws is logged and does not end
|
|
23
|
+
* the loop.
|
|
24
|
+
*
|
|
25
|
+
* `_tasksRunning` and `_tasksRunningPromise` are kept in step with what
|
|
26
|
+
* `Monitor.startTasks` would have set, because `stopTasks()` clears the first
|
|
27
|
+
* and `stopMonitorAndDrain` awaits the second.
|
|
28
|
+
*/
|
|
29
|
+
import type { Monitor } from '@bsv/wallet-toolbox-mobile'
|
|
30
|
+
|
|
31
|
+
/** No completed pass for this long, while the app is active, means the loop is gone. */
|
|
32
|
+
export const MONITOR_STALL_MS = 120_000
|
|
33
|
+
|
|
34
|
+
/** The private loop state the toolbox's own `startTasks` maintains. */
|
|
35
|
+
interface LoopState {
|
|
36
|
+
_tasksRunning: boolean
|
|
37
|
+
_tasksRunningPromise?: Promise<void>
|
|
38
|
+
resolveCompletion?: () => void
|
|
39
|
+
runOnce(): Promise<void>
|
|
40
|
+
options: { taskRunWaitMsecs: number }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class MonitorSupervisor {
|
|
44
|
+
/** When the most recent pass completed (or the loop was started/touched). */
|
|
45
|
+
lastPassAt: number
|
|
46
|
+
/** How many times `restart` has run. Exposed for logging and tests. */
|
|
47
|
+
restarts = 0
|
|
48
|
+
private generation = 0
|
|
49
|
+
private monitor: Monitor | undefined
|
|
50
|
+
|
|
51
|
+
constructor(
|
|
52
|
+
private readonly now: () => number = () => Date.now(),
|
|
53
|
+
private readonly wait: (ms: number) => Promise<void> = ms => new Promise(resolve => setTimeout(resolve, ms)),
|
|
54
|
+
private readonly warn: (message: string, error?: unknown) => void = (m, e) => console.warn(m, e ?? '')
|
|
55
|
+
) {
|
|
56
|
+
this.lastPassAt = this.now()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Begin driving `monitor`. Replaces `monitor.startTasks()`; never awaited. */
|
|
60
|
+
start(monitor: Monitor): void {
|
|
61
|
+
this.monitor = monitor
|
|
62
|
+
const state = monitor as unknown as LoopState
|
|
63
|
+
state._tasksRunning = true
|
|
64
|
+
state._tasksRunningPromise = new Promise<void>(resolve => {
|
|
65
|
+
state.resolveCompletion = resolve
|
|
66
|
+
})
|
|
67
|
+
this.lastPassAt = this.now()
|
|
68
|
+
void this.loop(state, ++this.generation)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Something else proves the loop is not stalled — the app just came back to
|
|
73
|
+
* the foreground and the loop's timers were frozen with it. Resets the clock
|
|
74
|
+
* so the first watchdog check after a resume cannot fire on a gap that was
|
|
75
|
+
* the OS's, not the monitor's.
|
|
76
|
+
*/
|
|
77
|
+
touch(): void {
|
|
78
|
+
this.lastPassAt = this.now()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
isStalled(stallMs: number = MONITOR_STALL_MS): boolean {
|
|
82
|
+
const state = this.monitor as unknown as LoopState | undefined
|
|
83
|
+
if (!state?._tasksRunning) return false
|
|
84
|
+
return this.now() - this.lastPassAt > stallMs
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Start a new generation of the loop on the current monitor. The stalled
|
|
89
|
+
* generation is abandoned: when (if) its await resolves it exits on the
|
|
90
|
+
* generation check. Returns false when there is no running monitor to restart.
|
|
91
|
+
*/
|
|
92
|
+
restart(): boolean {
|
|
93
|
+
const state = this.monitor as unknown as LoopState | undefined
|
|
94
|
+
if (!state?._tasksRunning) return false
|
|
95
|
+
this.restarts++
|
|
96
|
+
this.lastPassAt = this.now()
|
|
97
|
+
void this.loop(state, ++this.generation)
|
|
98
|
+
return true
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private async loop(state: LoopState, generation: number): Promise<void> {
|
|
102
|
+
while (state._tasksRunning && generation === this.generation) {
|
|
103
|
+
try {
|
|
104
|
+
await state.runOnce()
|
|
105
|
+
} catch (e) {
|
|
106
|
+
// The toolbox loop dies here. A failed pass is a failed pass; the next
|
|
107
|
+
// one runs.
|
|
108
|
+
this.warn('[MonitorSupervisor] monitor pass failed', e)
|
|
109
|
+
}
|
|
110
|
+
if (generation !== this.generation) return
|
|
111
|
+
this.lastPassAt = this.now()
|
|
112
|
+
await this.wait(state.options.taskRunWaitMsecs)
|
|
113
|
+
}
|
|
114
|
+
// Only the live generation may report completion: an abandoned one waking
|
|
115
|
+
// up late must not resolve a drain that belongs to its successor.
|
|
116
|
+
if (generation === this.generation && state.resolveCompletion) {
|
|
117
|
+
state.resolveCompletion()
|
|
118
|
+
state.resolveCompletion = undefined
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* a pass takes at most one chunk and the interval floor keeps it off the JS thread the rest
|
|
14
14
|
* of the time.
|
|
15
15
|
*/
|
|
16
|
-
import { WalletMonitorTask } from '@bsv/wallet-toolbox-mobile
|
|
16
|
+
import { WalletMonitorTask } from '@bsv/wallet-toolbox-mobile'
|
|
17
17
|
import type { Monitor } from '@bsv/wallet-toolbox-mobile'
|
|
18
18
|
import { MIN_PUSH_INTERVAL_MS } from '../backup/constants'
|
|
19
19
|
import type { PushResult } from '../backup/push'
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* and rebuilt on network switches and wallet rebuilds, and a pending inbox
|
|
12
12
|
* must survive that.
|
|
13
13
|
*/
|
|
14
|
-
import { WalletMonitorTask } from '@bsv/wallet-toolbox-mobile
|
|
14
|
+
import { WalletMonitorTask } from '@bsv/wallet-toolbox-mobile'
|
|
15
15
|
import type { Monitor } from '@bsv/wallet-toolbox-mobile'
|
|
16
16
|
|
|
17
17
|
export interface CreditInboxTaskResult {
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* and rebuilt on network switches and wallet rebuilds, and a pending outbox
|
|
12
12
|
* must survive that.
|
|
13
13
|
*/
|
|
14
|
-
import { WalletMonitorTask } from '@bsv/wallet-toolbox-mobile
|
|
14
|
+
import { WalletMonitorTask } from '@bsv/wallet-toolbox-mobile'
|
|
15
15
|
import type { Monitor } from '@bsv/wallet-toolbox-mobile'
|
|
16
16
|
import type { OutboxEntry } from '../peerpay/outbox'
|
|
17
17
|
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
* reports online seconds before routes actually work — which is exactly the
|
|
20
20
|
* "payments sit at nosend forever" bug this rewrite removes.
|
|
21
21
|
*/
|
|
22
|
-
import { WalletMonitorTask } from '@bsv/wallet-toolbox-mobile
|
|
22
|
+
import { WalletMonitorTask } from '@bsv/wallet-toolbox-mobile'
|
|
23
23
|
import type { Monitor } from '@bsv/wallet-toolbox-mobile'
|
|
24
24
|
import type { ProcessOfflineActionsResult } from '../storage/methods/processOfflineActions'
|
|
25
25
|
|
package/core/net/online.ts
CHANGED
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* retry, a wrong "offline" hides the online rails from someone who has signal.
|
|
15
15
|
*/
|
|
16
16
|
import NetInfo from '@react-native-community/netinfo'
|
|
17
|
+
import { DEFAULT_MESSAGEBOX_URL } from '../config'
|
|
18
|
+
import { DEFAULT_ARC_URLS } from '../constants'
|
|
17
19
|
|
|
18
20
|
export interface OnlineState {
|
|
19
21
|
isConnected: boolean | null
|
|
@@ -32,3 +34,50 @@ export async function getOnline(): Promise<boolean> {
|
|
|
32
34
|
export function subscribeOnline(cb: (online: boolean) => void): () => void {
|
|
33
35
|
return NetInfo.addEventListener(state => cb(isOnlineState(state)))
|
|
34
36
|
}
|
|
37
|
+
|
|
38
|
+
/** Longest a live request may take before it counts as "nothing got out". */
|
|
39
|
+
export const PROBE_TIMEOUT_MS = 4000
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Hosts the wallet already talks to, so asking them adds no third party and no
|
|
43
|
+
* new traffic pattern. HTTPS, so a captive portal cannot answer for them.
|
|
44
|
+
*/
|
|
45
|
+
const PROBE_URLS = [DEFAULT_ARC_URLS.main, DEFAULT_MESSAGEBOX_URL]
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A live request, for when NetInfo's verdict is offline and something needs to
|
|
49
|
+
* know whether to believe it.
|
|
50
|
+
*
|
|
51
|
+
* That verdict is not a measurement of the moment. On iOS it is a cached
|
|
52
|
+
* reachability snapshot — the OS delivers no callbacks while the app is
|
|
53
|
+
* suspended, and NetInfo's cache is never re-read — plus an HTTP probe of a
|
|
54
|
+
* Google host the wallet has no other use for. Either can say offline for a
|
|
55
|
+
* device whose connection works, and NetInfo will not correct itself until the
|
|
56
|
+
* next network change. This asks the wallet's own hosts instead: any HTTP
|
|
57
|
+
* response, a 404 included, proves DNS, a route and TLS all worked, so one
|
|
58
|
+
* answer from any host is enough. It only ever upgrades a verdict to online,
|
|
59
|
+
* never the reverse.
|
|
60
|
+
*/
|
|
61
|
+
export function probeOnline(timeoutMs: number = PROBE_TIMEOUT_MS): Promise<boolean> {
|
|
62
|
+
return new Promise(resolve => {
|
|
63
|
+
const controller = new AbortController()
|
|
64
|
+
let failed = 0
|
|
65
|
+
const finish = (online: boolean) => {
|
|
66
|
+
clearTimeout(timer)
|
|
67
|
+
controller.abort()
|
|
68
|
+
resolve(online)
|
|
69
|
+
}
|
|
70
|
+
const timer = setTimeout(() => finish(false), timeoutMs)
|
|
71
|
+
for (const url of PROBE_URLS) {
|
|
72
|
+
Promise.resolve()
|
|
73
|
+
.then(() => fetch(url, { method: 'HEAD', cache: 'no-store', credentials: 'omit', signal: controller.signal }))
|
|
74
|
+
.then(
|
|
75
|
+
() => finish(true),
|
|
76
|
+
() => {
|
|
77
|
+
failed += 1
|
|
78
|
+
if (failed === PROBE_URLS.length) finish(false)
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which separators money is printed with: always the device's own locale.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** The device's own locale, resolved once. */
|
|
6
|
+
function detectDeviceLocale(): string {
|
|
7
|
+
try {
|
|
8
|
+
return Intl.NumberFormat().resolvedOptions().locale?.split('-u-')[0] || 'en-US'
|
|
9
|
+
} catch {
|
|
10
|
+
return 'en-US'
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const deviceLocale = detectDeviceLocale()
|
|
15
|
+
|
|
16
|
+
/** The locale every formatter in `amountFormatHelpers` prints through. */
|
|
17
|
+
export function getNumberLocale(): string {
|
|
18
|
+
return deviceLocale
|
|
19
|
+
}
|