@bsv/expo-wallet-toolbox 0.4.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 +547 -0
- package/README.md +19 -1
- package/assets/sounds/vault-deposit.wav +0 -0
- package/assets/sounds/vault-withdraw.wav +0 -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 +16 -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/VaultContext.tsx +8 -11
- package/core/context/WalletConnectionContext.tsx +137 -52
- package/core/context/WalletContext.tsx +464 -30
- 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/hooks/useConfirmationSound.ts +14 -8
- package/core/i18n/translations.tsx +4562 -986
- 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 +116 -15
- package/core/localpay/build.ts +674 -37
- package/core/localpay/codec.ts +68 -5
- package/core/localpay/pending.ts +240 -22
- package/core/localpay/pendingAborts.ts +5 -1
- package/core/localpay/settlementAck.ts +149 -0
- package/core/localpay/types.ts +14 -1
- package/core/localpay/verify.ts +118 -69
- package/core/mandala/abortGuard.ts +134 -0
- package/core/mandala/basketMigration.ts +166 -0
- package/core/mandala/bundle.ts +244 -0
- package/core/mandala/createRuntime.ts +2437 -0
- package/core/mandala/drain.ts +670 -0
- package/core/mandala/permissionModule.ts +642 -0
- package/core/mandala/resendTransfer.ts +175 -0
- package/core/mandala/runtime.ts +357 -0
- package/core/mandala/settlementStore.ts +412 -0
- package/core/mandala/types.ts +290 -0
- 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/cancelParked.ts +94 -1
- package/core/offline/payerHold.ts +152 -12
- package/core/offline/plan.ts +1 -1
- package/core/offline/tokenFrames.ts +197 -0
- package/core/parsePeerPayURI.ts +70 -17
- package/core/pay/rails/address.ts +51 -3
- package/core/pay/rails/handle.ts +88 -9
- package/core/pay/rails/index.ts +16 -6
- package/core/pay/rails/nearby.ts +114 -4
- package/core/pay/recordProof.ts +2 -6
- package/core/services/arcadeBroadcastProvider.ts +1 -4
- package/core/services/capWalletArgs.ts +8 -9
- package/core/services/exchangeRate.ts +1 -1
- package/core/services/externalOrigin.ts +42 -0
- package/core/services/vault/VaultKeyService.ts +912 -251
- package/core/services/vault/ceremony.ts +435 -367
- package/core/services/vault/ceremonyHost.ts +27 -21
- package/core/services/vault/devMock.ts +30 -7
- package/core/services/vault/driver.ts +95 -41
- package/core/services/vault/guard.ts +514 -56
- package/core/services/vault/hardwareLease.ts +22 -0
- package/core/services/vault/mockYubiKey.ts +258 -90
- package/core/services/vault/pivReset.ts +145 -0
- package/core/services/vault/r1comb.ts +976 -0
- package/core/services/vault/session.ts +93 -26
- package/core/services/vault/transfers.ts +2617 -747
- package/core/services/vault/types.ts +100 -43
- package/core/services/vault/vaultStore.ts +991 -74
- package/core/services/walletConnectionValidation.ts +293 -0
- package/core/services/walletServiceConfig.ts +31 -20
- package/core/storage/StorageExpoSQLite.ts +420 -87
- package/core/storage/dbImage.ts +48 -0
- package/core/storage/methods/findSql.ts +3 -54
- package/core/storage/methods/listActionsSql.ts +8 -5
- package/core/storage/methods/listOutputsSql.ts +9 -5
- package/core/storage/methods/offlineActions.ts +15 -0
- package/core/storage/methods/processOfflineActions.ts +265 -9
- package/core/storage/methods/reviewStatusSql.ts +7 -2
- package/core/storage/methods/walletBalanceSql.ts +16 -4
- package/core/storage/schema/createTables.ts +152 -0
- package/core/storage/sqlUpdateValue.ts +24 -0
- package/core/toolboxConfig.ts +182 -1
- package/core/toolboxTypes.ts +40 -0
- package/core/userAvatar.ts +79 -0
- package/core/walletMonitor.ts +8 -3
- package/core/walletRepair/runWalletCheck.ts +19 -2
- package/package.json +31 -30
- package/ui/activityFilter.ts +115 -0
- package/ui/components/QRScanner.tsx +1 -1
- package/ui/components/pay/AdmissionNotice.tsx +99 -0
- package/ui/components/pay/AssetPicker.tsx +184 -0
- package/ui/components/pay/AvailableBalance.tsx +33 -2
- package/ui/components/pay/HandleReceive.tsx +213 -50
- package/ui/components/pay/NearbyFlow.tsx +616 -69
- package/ui/components/pay/PayForm.tsx +73 -8
- package/ui/components/pay/PaymentSuccessOverlay.tsx +89 -8
- package/ui/components/pay/RecipientField.tsx +132 -73
- package/ui/components/pay/RequestHub.tsx +68 -8
- package/ui/components/pay/ResultBanner.tsx +41 -4
- package/ui/components/pay/StepBar.tsx +32 -0
- package/ui/components/pay/UniversalSend.tsx +1118 -86
- package/ui/components/pay/tokenSendCopy.ts +116 -0
- package/ui/components/pay/useRecipientInput.ts +36 -7
- package/ui/components/ui/PencilEditField.tsx +109 -0
- package/ui/components/ui/PermissionSheet.tsx +65 -4
- package/ui/components/ui/Sheet.tsx +9 -2
- package/ui/components/ui/SlideOverFromRight.tsx +86 -0
- package/ui/components/vault/EnrollWizard.tsx +1432 -488
- package/ui/components/vault/KeyChooser.tsx +96 -0
- package/ui/components/vault/VaultCeremonySheet.tsx +107 -43
- package/ui/components/vault/vaultErrorCopy.ts +145 -0
- package/ui/components/wallet/ActivityRow.tsx +283 -97
- package/ui/components/wallet/AmountInput.tsx +92 -28
- package/ui/components/wallet/AssetAmount.tsx +95 -0
- package/ui/components/wallet/AssetSwitcherDropdown.tsx +261 -0
- 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/components/wallet/tokenEviction.ts +65 -0
- package/ui/hooks/useContactsStore.ts +10 -0
- package/ui/hooks/useExportWalletData.ts +39 -0
- package/ui/hooks/useMandala.ts +320 -0
- package/ui/hooks/useOnline.ts +65 -20
- package/ui/hooks/useVaultBalance.ts +105 -7
- package/ui/hooks/useVaultCoverage.ts +52 -0
- package/ui/importDatabases.ts +7 -3
- package/ui/index.ts +75 -7
- package/ui/screens/ConnectionsScreen.tsx +46 -29
- 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/PairScreen.tsx +12 -12
- package/ui/screens/PayScreen.tsx +145 -12
- package/ui/screens/ProfileScreen.tsx +783 -0
- package/ui/screens/SettingsScreen.tsx +18 -9
- package/ui/screens/TransactionDetailScreen.tsx +419 -0
- package/ui/screens/TrustScreen.tsx +1 -1
- package/ui/screens/VaultScreen.tsx +925 -171
- package/ui/screens/VaultTransferScreen.tsx +376 -123
- package/ui/screens/WalletCheckScreen.tsx +10 -1
- package/ui/screens/WalletConfigScreen.tsx +80 -16
- package/ui/screens/WalletHomeScreen.tsx +915 -139
- package/ui/screens/homeBadges.ts +9 -1
- package/ui/screens/tokenRowTitle.ts +68 -0
- package/ui/tokenFormat.ts +108 -0
- package/ui/tokenSeen.ts +75 -0
- package/ui/tokenStatus.ts +100 -0
- package/ui/txStatus.ts +43 -5
- package/assets/sounds/vault-close.wav +0 -0
- package/assets/sounds/vault-open.wav +0 -0
- package/core/services/vault/k1.ts +0 -75
- package/core/services/vault/sealing.ts +0 -84
- package/core/services/vault/vaultDerivation.ts +0 -152
- package/core/services/vault/vaultPassphrase.ts +0 -287
- package/ui/components/vault/PassphraseField.tsx +0 -306
- package/ui/components/vault/PhraseBackupSheet.tsx +0 -227
- package/ui/screens/VaultRecoverScreen.tsx +0 -226
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { PublicKey, type WalletProtocol } from '@bsv/sdk'
|
|
2
|
+
import { parseExternalOrigin, type ExternalOrigin } from './externalOrigin'
|
|
3
|
+
|
|
4
|
+
const KB = 1024
|
|
5
|
+
const MB = 1024 * KB
|
|
6
|
+
|
|
7
|
+
/** Pairing QR codes are normally valid for two minutes. A short upper bound
|
|
8
|
+
* prevents a signed QR from becoming a practically permanent bearer token
|
|
9
|
+
* while leaving ample room for clock skew and custom deployments. */
|
|
10
|
+
export const MAX_PAIRING_EXPIRY_HORIZON_SECONDS = 15 * 60
|
|
11
|
+
export const MAX_PAIRING_TOPIC_BYTES = 128
|
|
12
|
+
export const MAX_PAIRING_ORIGIN_BYTES = 2048
|
|
13
|
+
export const MAX_PAIRING_PROTOCOL_BYTES = 512
|
|
14
|
+
export const MAX_PAIRING_SIGNATURE_CHARS = 128
|
|
15
|
+
export const MAX_RELAY_URL_BYTES = 2048
|
|
16
|
+
export const MAX_RELAY_RESPONSE_BYTES = 4 * KB
|
|
17
|
+
|
|
18
|
+
/** The largest encrypted RPC body accepted by the mobile bridge. The normal
|
|
19
|
+
* wallet-relay traffic is orders of magnitude smaller; 5 MiB still admits a
|
|
20
|
+
* one-megabyte AtomicBEEF represented as a JSON number array. */
|
|
21
|
+
export const MAX_RPC_PLAINTEXT_BYTES = 5 * MB
|
|
22
|
+
// @bsv/sdk authenticated encryption adds 48 bytes today. Keep a small explicit
|
|
23
|
+
// allowance so a future compatible envelope does not fail at the boundary.
|
|
24
|
+
export const MAX_RPC_CIPHERTEXT_BYTES = MAX_RPC_PLAINTEXT_BYTES + 256
|
|
25
|
+
export const MAX_RPC_CIPHERTEXT_CHARS = Math.ceil(MAX_RPC_CIPHERTEXT_BYTES * 4 / 3)
|
|
26
|
+
export const MAX_RPC_WIRE_CHARS = MAX_RPC_CIPHERTEXT_CHARS + 1024
|
|
27
|
+
export const MAX_IN_FLIGHT_RPC = 4
|
|
28
|
+
|
|
29
|
+
export interface ConnectParams {
|
|
30
|
+
topic: string
|
|
31
|
+
backendIdentityKey: string
|
|
32
|
+
protocolID: string
|
|
33
|
+
origin: string
|
|
34
|
+
expiry?: string
|
|
35
|
+
sig?: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ValidatedConnectParams {
|
|
39
|
+
params: Required<ConnectParams>
|
|
40
|
+
external: ExternalOrigin
|
|
41
|
+
protocolID: WalletProtocol
|
|
42
|
+
expiry: number
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const PAIRING_SIGNATURE_DOMAIN = 'bsv-wallet-pairing-v1'
|
|
46
|
+
|
|
47
|
+
/** Exact transcript signed by the desktop pairing peer. Every field that
|
|
48
|
+
* chooses the transport endpoint or its BRC-42 key namespace is included. */
|
|
49
|
+
export function buildPairingSignatureMessage(
|
|
50
|
+
params: Pick<Required<ConnectParams>, 'topic' | 'backendIdentityKey' | 'protocolID' | 'origin' | 'expiry'>
|
|
51
|
+
): string {
|
|
52
|
+
return [
|
|
53
|
+
PAIRING_SIGNATURE_DOMAIN,
|
|
54
|
+
params.topic,
|
|
55
|
+
params.backendIdentityKey,
|
|
56
|
+
params.protocolID,
|
|
57
|
+
params.origin,
|
|
58
|
+
params.expiry
|
|
59
|
+
].join('|')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function requireBoundedString(value: unknown, name: string, maxBytes: number, minBytes = 1): string {
|
|
63
|
+
if (typeof value !== 'string') throw new Error(`${name} must be a string`)
|
|
64
|
+
// UTF-8 is never shorter than the UTF-16 code-unit count. This rejects a
|
|
65
|
+
// giant value before TextEncoder would allocate another giant buffer.
|
|
66
|
+
if (value.length > maxBytes) throw new Error(`${name} is too long`)
|
|
67
|
+
const bytes = new TextEncoder().encode(value).length
|
|
68
|
+
if (bytes < minBytes || bytes > maxBytes) throw new Error(`${name} has an invalid length`)
|
|
69
|
+
return value
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function parseWalletProtocol(raw: unknown): { raw: string; value: WalletProtocol } {
|
|
73
|
+
const encoded = requireBoundedString(raw, 'protocolID', MAX_PAIRING_PROTOCOL_BYTES)
|
|
74
|
+
let parsed: unknown
|
|
75
|
+
try {
|
|
76
|
+
parsed = JSON.parse(encoded)
|
|
77
|
+
} catch {
|
|
78
|
+
throw new Error('protocolID is not valid JSON')
|
|
79
|
+
}
|
|
80
|
+
if (!Array.isArray(parsed) || parsed.length !== 2 || !Number.isInteger(parsed[0]) ||
|
|
81
|
+
parsed[0] < 0 || parsed[0] > 2 || typeof parsed[1] !== 'string') {
|
|
82
|
+
throw new Error('protocolID must be a valid [security level, protocol name] tuple')
|
|
83
|
+
}
|
|
84
|
+
const name = parsed[1]
|
|
85
|
+
if (name.length < 5 || name.length > 400 || name !== name.trim().toLowerCase() ||
|
|
86
|
+
!/^[a-z0-9 ]+$/.test(name) || name.includes(' ') || name.endsWith(' protocol')) {
|
|
87
|
+
throw new Error('protocolID contains an invalid protocol name')
|
|
88
|
+
}
|
|
89
|
+
const value = [parsed[0], name] as WalletProtocol
|
|
90
|
+
if (JSON.stringify(value) !== encoded) throw new Error('protocolID must use canonical JSON encoding')
|
|
91
|
+
return { raw: encoded, value }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function validateBackendIdentityKey(raw: unknown): string {
|
|
95
|
+
const key = requireBoundedString(raw, 'backendIdentityKey', 66, 66)
|
|
96
|
+
if (!/^(02|03)[0-9a-f]{64}$/.test(key)) {
|
|
97
|
+
throw new Error('Backend identity key must be a canonical compressed secp256k1 public key')
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
if (PublicKey.fromString(key).toString() !== key) throw new Error('non-canonical key')
|
|
101
|
+
} catch {
|
|
102
|
+
throw new Error('Backend identity key is not a valid secp256k1 point')
|
|
103
|
+
}
|
|
104
|
+
return key
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function validateMobileIdentityKey(raw: unknown): string {
|
|
108
|
+
try {
|
|
109
|
+
return validateBackendIdentityKey(raw)
|
|
110
|
+
} catch {
|
|
111
|
+
throw new Error('Mobile identity key must be a canonical compressed secp256k1 public key')
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function validatePairingTopic(raw: unknown): string {
|
|
116
|
+
const topic = requireBoundedString(raw, 'topic', MAX_PAIRING_TOPIC_BYTES)
|
|
117
|
+
// The signature payload uses `|` separators and the topic is used in URL
|
|
118
|
+
// path/query components. Restrict it to an unambiguous, percent-free token.
|
|
119
|
+
if (!/^[A-Za-z0-9_-]+$/.test(topic)) throw new Error('Pairing topic contains invalid characters')
|
|
120
|
+
return topic
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function validateCanonicalExternalOrigin(raw: unknown): ExternalOrigin {
|
|
124
|
+
const origin = requireBoundedString(raw, 'origin', MAX_PAIRING_ORIGIN_BYTES)
|
|
125
|
+
const external = parseExternalOrigin(origin)
|
|
126
|
+
if (external.origin !== origin) throw new Error('Origin must use its canonical HTTPS form')
|
|
127
|
+
return external
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function validateSignature(raw: unknown): string {
|
|
131
|
+
const sig = requireBoundedString(raw, 'sig', MAX_PAIRING_SIGNATURE_CHARS)
|
|
132
|
+
if (!/^[A-Za-z0-9_-]+$/.test(sig) || sig.length % 4 === 1) {
|
|
133
|
+
throw new Error('Pairing signature is not canonical base64url')
|
|
134
|
+
}
|
|
135
|
+
// A DER ECDSA signature is normally 70-72 bytes. These wider bounds reject
|
|
136
|
+
// empty/truncated and oversized values before the SDK's base64 decoder.
|
|
137
|
+
const decodedBytes = Math.floor(sig.length * 3 / 4)
|
|
138
|
+
if (decodedBytes < 8 || decodedBytes > 80) throw new Error('Pairing signature has an invalid length')
|
|
139
|
+
return sig
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function validateExpiry(raw: unknown, nowMs: number): { raw: string; value: number } {
|
|
143
|
+
const expiryRaw = requireBoundedString(raw, 'expiry', 11)
|
|
144
|
+
if (!/^[1-9][0-9]{0,10}$/.test(expiryRaw)) throw new Error('Pairing expiry must be canonical Unix seconds')
|
|
145
|
+
const expiry = Number(expiryRaw)
|
|
146
|
+
const nowSeconds = Math.floor(nowMs / 1000)
|
|
147
|
+
if (!Number.isSafeInteger(expiry) || expiry <= nowSeconds) throw new Error('QR code has expired')
|
|
148
|
+
if (expiry > nowSeconds + MAX_PAIRING_EXPIRY_HORIZON_SECONDS) {
|
|
149
|
+
throw new Error('QR code expiry is too far in the future')
|
|
150
|
+
}
|
|
151
|
+
return { raw: expiryRaw, value: expiry }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function validateConnectParams(raw: ConnectParams, nowMs = Date.now()): ValidatedConnectParams {
|
|
155
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
156
|
+
throw new Error('Pairing parameters are invalid')
|
|
157
|
+
}
|
|
158
|
+
const topic = validatePairingTopic(raw.topic)
|
|
159
|
+
const backendIdentityKey = validateBackendIdentityKey(raw.backendIdentityKey)
|
|
160
|
+
const protocol = parseWalletProtocol(raw.protocolID)
|
|
161
|
+
const external = validateCanonicalExternalOrigin(raw.origin)
|
|
162
|
+
const origin = external.origin
|
|
163
|
+
const expiry = validateExpiry(raw.expiry, nowMs)
|
|
164
|
+
const sig = validateSignature(raw.sig)
|
|
165
|
+
return {
|
|
166
|
+
params: { topic, backendIdentityKey, protocolID: protocol.raw, origin, expiry: expiry.raw, sig },
|
|
167
|
+
external,
|
|
168
|
+
protocolID: protocol.value,
|
|
169
|
+
expiry: expiry.value
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function validateStoredConnectionFields(raw: {
|
|
174
|
+
sessionId: unknown
|
|
175
|
+
backendIdentityKey: unknown
|
|
176
|
+
mobileIdentityKey: unknown
|
|
177
|
+
protocolID: unknown
|
|
178
|
+
origin: unknown
|
|
179
|
+
}): {
|
|
180
|
+
topic: string
|
|
181
|
+
backendIdentityKey: string
|
|
182
|
+
mobileIdentityKey: string
|
|
183
|
+
protocolIDRaw: string
|
|
184
|
+
protocolID: WalletProtocol
|
|
185
|
+
external: ExternalOrigin
|
|
186
|
+
} {
|
|
187
|
+
if (raw === null || typeof raw !== 'object') throw new Error('Stored connection is invalid')
|
|
188
|
+
const topic = validatePairingTopic(raw.sessionId)
|
|
189
|
+
const backendIdentityKey = validateBackendIdentityKey(raw.backendIdentityKey)
|
|
190
|
+
const mobileIdentityKey = validateMobileIdentityKey(raw.mobileIdentityKey)
|
|
191
|
+
const protocol = parseWalletProtocol(raw.protocolID)
|
|
192
|
+
const external = validateCanonicalExternalOrigin(raw.origin)
|
|
193
|
+
return { topic, backendIdentityKey, mobileIdentityKey, protocolIDRaw: protocol.raw, protocolID: protocol.value, external }
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** SecureStore data can be corrupted or restored independently of the
|
|
197
|
+
* connection database. Only canonical sequence values that can be incremented
|
|
198
|
+
* safely for the next authenticated message are accepted. */
|
|
199
|
+
export function validateStoredConnectionSequence(raw: unknown): number {
|
|
200
|
+
if (raw === null || raw === undefined) return 0
|
|
201
|
+
if (typeof raw !== 'string' || !/^(0|[1-9][0-9]*)$/.test(raw)) {
|
|
202
|
+
throw new Error('Stored connection sequence is invalid')
|
|
203
|
+
}
|
|
204
|
+
const sequence = Number(raw)
|
|
205
|
+
if (!Number.isSafeInteger(sequence) || sequence < 0 || sequence >= Number.MAX_SAFE_INTEGER) {
|
|
206
|
+
throw new Error('Stored connection sequence is invalid')
|
|
207
|
+
}
|
|
208
|
+
return sequence
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function validateRelayUrl(raw: unknown): string {
|
|
212
|
+
const value = requireBoundedString(raw, 'relay URL', MAX_RELAY_URL_BYTES)
|
|
213
|
+
let url: URL
|
|
214
|
+
try {
|
|
215
|
+
url = new URL(value)
|
|
216
|
+
} catch {
|
|
217
|
+
throw new Error('Origin server returned an invalid relay URL')
|
|
218
|
+
}
|
|
219
|
+
if (url.protocol !== 'wss:' || !url.hostname) throw new Error('Relay URL must use wss://')
|
|
220
|
+
if (url.username || url.password) throw new Error('Relay URL must not include credentials')
|
|
221
|
+
if (url.pathname !== '/' || url.search || url.hash) {
|
|
222
|
+
throw new Error('Relay URL must be a bare WSS origin')
|
|
223
|
+
}
|
|
224
|
+
return url.origin
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function parseRelayResponse(raw: unknown): string {
|
|
228
|
+
const text = requireBoundedString(raw, 'relay response', MAX_RELAY_RESPONSE_BYTES)
|
|
229
|
+
let data: unknown
|
|
230
|
+
try {
|
|
231
|
+
data = JSON.parse(text)
|
|
232
|
+
} catch {
|
|
233
|
+
throw new Error('Origin server returned invalid relay JSON')
|
|
234
|
+
}
|
|
235
|
+
if (data === null || typeof data !== 'object' || Array.isArray(data) ||
|
|
236
|
+
typeof (data as { relay?: unknown }).relay !== 'string') {
|
|
237
|
+
throw new Error('Origin server did not return a relay URL')
|
|
238
|
+
}
|
|
239
|
+
return validateRelayUrl((data as { relay: string }).relay)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function buildRelayWebSocketUrl(relay: string, topic: string): string {
|
|
243
|
+
const url = new URL('/ws', `${validateRelayUrl(relay)}/`)
|
|
244
|
+
url.searchParams.set('topic', validatePairingTopic(topic))
|
|
245
|
+
url.searchParams.set('role', 'mobile')
|
|
246
|
+
return url.toString()
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export interface BoundedWireEnvelope {
|
|
250
|
+
topic: string
|
|
251
|
+
ciphertext: string
|
|
252
|
+
mobileIdentityKey?: string
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function parseBoundedWireEnvelope(raw: unknown, expectedTopic: string): BoundedWireEnvelope {
|
|
256
|
+
if (typeof raw !== 'string' || raw.length === 0 || raw.length > MAX_RPC_WIRE_CHARS) {
|
|
257
|
+
throw new Error('Wallet relay message exceeds the transport limit')
|
|
258
|
+
}
|
|
259
|
+
let parsed: unknown
|
|
260
|
+
try {
|
|
261
|
+
parsed = JSON.parse(raw)
|
|
262
|
+
} catch {
|
|
263
|
+
throw new Error('Wallet relay message is not valid JSON')
|
|
264
|
+
}
|
|
265
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
266
|
+
throw new Error('Wallet relay message is not an object')
|
|
267
|
+
}
|
|
268
|
+
const envelope = parsed as Record<string, unknown>
|
|
269
|
+
for (const key of Object.keys(envelope)) {
|
|
270
|
+
if (key !== 'topic' && key !== 'ciphertext' && key !== 'mobileIdentityKey') {
|
|
271
|
+
throw new Error(`Wallet relay message contains unknown field ${key}`)
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (envelope.topic !== expectedTopic) throw new Error('Wallet relay message has the wrong topic')
|
|
275
|
+
if (typeof envelope.ciphertext !== 'string' || envelope.ciphertext.length === 0 ||
|
|
276
|
+
envelope.ciphertext.length > MAX_RPC_CIPHERTEXT_CHARS ||
|
|
277
|
+
envelope.ciphertext.length % 4 === 1 ||
|
|
278
|
+
Math.floor(envelope.ciphertext.length * 3 / 4) > MAX_RPC_CIPHERTEXT_BYTES ||
|
|
279
|
+
!/^[A-Za-z0-9_-]+$/.test(envelope.ciphertext)) {
|
|
280
|
+
throw new Error('Wallet relay ciphertext is invalid or too large')
|
|
281
|
+
}
|
|
282
|
+
const mobileIdentityKey = envelope.mobileIdentityKey === undefined
|
|
283
|
+
? undefined
|
|
284
|
+
: validateMobileIdentityKey(envelope.mobileIdentityKey)
|
|
285
|
+
return { topic: expectedTopic, ciphertext: envelope.ciphertext, mobileIdentityKey }
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function requireBoundedPlaintext(plaintext: unknown): number[] {
|
|
289
|
+
if (!Array.isArray(plaintext) || plaintext.length > MAX_RPC_PLAINTEXT_BYTES) {
|
|
290
|
+
throw new Error('Wallet relay plaintext exceeds the transport limit')
|
|
291
|
+
}
|
|
292
|
+
return plaintext
|
|
293
|
+
}
|
|
@@ -5,12 +5,9 @@ import {
|
|
|
5
5
|
ChaintracksServiceClient,
|
|
6
6
|
Services
|
|
7
7
|
} from '@bsv/wallet-toolbox-mobile'
|
|
8
|
-
import type {
|
|
9
|
-
BsvExchangeRate,
|
|
10
|
-
WalletServicesOptions
|
|
11
|
-
} from '@bsv/wallet-toolbox-mobile/out/src/sdk'
|
|
12
|
-
import type { ChaintracksClientApi } from '@bsv/wallet-toolbox-mobile/out/src/services/chaintracker/chaintracks/Api/ChaintracksClientApi'
|
|
8
|
+
import type { ChaintracksClientApi } from '@bsv/wallet-toolbox-mobile'
|
|
13
9
|
import type { ChainTracker } from '@bsv/sdk'
|
|
10
|
+
import type { BsvExchangeRate, WalletServicesOptions } from '../toolboxTypes'
|
|
14
11
|
|
|
15
12
|
/**
|
|
16
13
|
* The chaintracks URL for a network — the one place this table is defined.
|
|
@@ -80,14 +77,36 @@ export function createServiceOptions(
|
|
|
80
77
|
}
|
|
81
78
|
}
|
|
82
79
|
|
|
80
|
+
// One resolved Arcade endpoint feeds BOTH option slots. `arcUrl` is the
|
|
81
|
+
// toolbox's generic ARC slot (broadcast fallback, `services.arcTaal`).
|
|
82
|
+
// `arcadeUrl` is what makes `Services` construct its `Arcade` provider —
|
|
83
|
+
// and that provider is the only `getMerklePath` source that can answer for
|
|
84
|
+
// a transaction the moment Arcade sees it mined (GET /v1/tx/{txid} carries
|
|
85
|
+
// the BUMP), and the only thing `TaskArcadeSSE` will subscribe to. Without
|
|
86
|
+
// it every proof lookup went straight to WhatsOnChain/Bitails, and the
|
|
87
|
+
// monitor logged "no arcadeUrl configured; SSE disabled" on every start —
|
|
88
|
+
// a nosend received while online sat unrecognised for 12 h on 2026-09-18
|
|
89
|
+
// while the explorer showed it with 6 confirmations.
|
|
90
|
+
const arcadeUrl =
|
|
91
|
+
arcUrlOverride ??
|
|
92
|
+
svc.arcUrl ??
|
|
93
|
+
(network === 'main'
|
|
94
|
+
? 'https://arcade-v2-us-1.bsvblockchain.tech'
|
|
95
|
+
: network === 'test'
|
|
96
|
+
? 'https://arcade-v2-testnet-us-1.bsvblockchain.tech'
|
|
97
|
+
: 'https://arcade-v2-ttn-us-1.bsvblockchain.tech')
|
|
98
|
+
const arcConfig = {
|
|
99
|
+
apiKey: arcApiKeyOverride ?? svc.arcApiKey ?? '',
|
|
100
|
+
// Must equal the Monitor's callbackToken so Arcade routes this wallet's
|
|
101
|
+
// status events to its SSE subscription (WalletServicesOptions.arcadeConfig).
|
|
102
|
+
callbackToken
|
|
103
|
+
}
|
|
104
|
+
const arcade = { arcUrl: arcadeUrl, arcConfig, arcadeUrl, arcadeConfig: arcConfig }
|
|
105
|
+
|
|
83
106
|
if (network === 'main') {
|
|
84
107
|
return {
|
|
85
108
|
...base,
|
|
86
|
-
|
|
87
|
-
arcConfig: {
|
|
88
|
-
apiKey: arcApiKeyOverride ?? svc.arcApiKey ?? '',
|
|
89
|
-
callbackToken
|
|
90
|
-
},
|
|
109
|
+
...arcade,
|
|
91
110
|
bsvUpdateMsecs: 60 * 60 * 1000,
|
|
92
111
|
fiatUpdateMsecs: 60 * 60 * 1000,
|
|
93
112
|
whatsOnChainApiKey: svc.whatsOnChainApiKey ?? '',
|
|
@@ -99,11 +118,7 @@ export function createServiceOptions(
|
|
|
99
118
|
if (network === 'test') {
|
|
100
119
|
return {
|
|
101
120
|
...base,
|
|
102
|
-
|
|
103
|
-
arcConfig: {
|
|
104
|
-
apiKey: arcApiKeyOverride ?? svc.arcApiKey ?? '',
|
|
105
|
-
callbackToken
|
|
106
|
-
},
|
|
121
|
+
...arcade,
|
|
107
122
|
bsvUpdateMsecs: 60 * 60 * 1000000,
|
|
108
123
|
fiatUpdateMsecs: 60 * 60 * 1000000,
|
|
109
124
|
whatsOnChainApiKey: svc.whatsOnChainApiKey ?? '',
|
|
@@ -115,11 +130,7 @@ export function createServiceOptions(
|
|
|
115
130
|
// teratest
|
|
116
131
|
return {
|
|
117
132
|
...base,
|
|
118
|
-
|
|
119
|
-
arcConfig: {
|
|
120
|
-
apiKey: arcApiKeyOverride ?? svc.arcApiKey ?? '',
|
|
121
|
-
callbackToken
|
|
122
|
-
},
|
|
133
|
+
...arcade,
|
|
123
134
|
bsvUpdateMsecs: 60 * 60 * 1000000,
|
|
124
135
|
fiatUpdateMsecs: 60 * 60 * 1000000,
|
|
125
136
|
whatsOnChainApiKey: svc.whatsOnChainApiKey ?? '',
|