|
| 1 | +import RNFS from 'react-native-fs'; |
| 2 | +import Realm from 'realm'; |
| 3 | +import Keychain, { ACCESSIBLE, SECURITY_LEVEL } from 'react-native-keychain'; |
| 4 | + |
| 5 | +import { ArkRealmSchemas, ARK_REALM_SCHEMA_VERSION, runArkRealmMigrations } from '@arkade-os/sdk/repositories/realm'; |
| 6 | +import { BoltzRealmSchemas } from '@arkade-os/boltz-swap/repositories/realm'; |
| 7 | +import { randomBytes } from '../../../class/rng'; |
| 8 | +import { uint8ArrayToHex, hexToUint8Array } from '../../uint8array-extras'; |
| 9 | +import { ArkSwapNotificationSuppressionSchema } from './notificationSuppressionRepository'; |
| 10 | + |
| 11 | +const AllArkadeSchemas = [...ArkRealmSchemas, ...BoltzRealmSchemas, ArkSwapNotificationSuppressionSchema]; |
| 12 | + |
| 13 | +// App-owned schemas added on top of the SDK's. Bump when an app-owned schema |
| 14 | +// changes; SDK bumps are handled by ARK_REALM_SCHEMA_VERSION. Realm requires |
| 15 | +// a strictly increasing schemaVersion when objects are added; computing |
| 16 | +// `SDK + offset` keeps the local additions ahead of any future SDK bump. |
| 17 | +const LOCAL_ARK_SCHEMA_OFFSET = 1; |
| 18 | +const ARKADE_REALM_SCHEMA_VERSION = ARK_REALM_SCHEMA_VERSION + LOCAL_ARK_SCHEMA_OFFSET; |
| 19 | + |
| 20 | +const realmInstances: Map<string, Realm> = new Map(); |
| 21 | +const openInFlight: Map<string, Promise<Realm>> = new Map(); |
| 22 | + |
| 23 | +// Files live in a dedicated subdirectory so BlueApp.moveRealmFilesToCacheDirectory() |
| 24 | +// — which sweeps top-level *.realm files from Documents into the OS-purgeable cache |
| 25 | +// — never sees them. RNFS.readDir is non-recursive, so the subdirectory is invisible |
| 26 | +// to that scan. Ark Realm holds non-recoverable swap/claim data and must stay in |
| 27 | +// Documents. |
| 28 | +const arkadeDir = (): string => `${RNFS.DocumentDirectoryPath}/arkade`; |
| 29 | +const realmPathFor = (namespace: string): string => `${arkadeDir()}/arkade-${namespace}.realm`; |
| 30 | +const keychainServiceFor = (namespace: string): string => `arkade_realm_${namespace}`; |
| 31 | + |
| 32 | +async function ensureArkadeDir(): Promise<void> { |
| 33 | + const dir = arkadeDir(); |
| 34 | + if (!(await RNFS.exists(dir))) await RNFS.mkdir(dir); |
| 35 | +} |
| 36 | + |
| 37 | +async function loadOrCreateEncryptionKey(namespace: string): Promise<Uint8Array> { |
| 38 | + const service = keychainServiceFor(namespace); |
| 39 | + |
| 40 | + const credentials = await Keychain.getGenericPassword({ service }); |
| 41 | + if (credentials) return hexToUint8Array(credentials.password); |
| 42 | + |
| 43 | + const buf = await randomBytes(64); |
| 44 | + const password = uint8ArrayToHex(buf); |
| 45 | + |
| 46 | + // Accessibility: match the rest of the app's secret accessibility. RNSecureKeyStore |
| 47 | + // in class/blue-app.ts and hooks/useBiometrics.ts both use WHEN_UNLOCKED_THIS_DEVICE_ONLY; |
| 48 | + // the default of AFTER_FIRST_UNLOCK would expose the Realm key while the device is locked. |
| 49 | + // |
| 50 | + // Security level: preflight via getSecurityLevel() rather than try/catch around |
| 51 | + // SECURE_HARDWARE. getSecurityLevel returns null on iOS (where the option is moot) |
| 52 | + // and the highest supported level on Android. We only opt into SECURE_HARDWARE when |
| 53 | + // the device actually backs it; otherwise let react-native-keychain pick its default. |
| 54 | + // Catching every setGenericPassword error and silently retrying with ANY (the previous |
| 55 | + // shape) downgrades on unrelated failures — preflight surfaces those instead. |
| 56 | + const supportedLevel = await Keychain.getSecurityLevel(); |
| 57 | + const opts: Parameters<typeof Keychain.setGenericPassword>[2] = { |
| 58 | + service, |
| 59 | + accessible: ACCESSIBLE.WHEN_UNLOCKED_THIS_DEVICE_ONLY, |
| 60 | + }; |
| 61 | + if (supportedLevel === SECURITY_LEVEL.SECURE_HARDWARE) { |
| 62 | + opts.securityLevel = SECURITY_LEVEL.SECURE_HARDWARE; |
| 63 | + } |
| 64 | + await Keychain.setGenericPassword(service, password, opts); |
| 65 | + |
| 66 | + return hexToUint8Array(password); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Returns a per-wallet Realm instance keyed by `namespace`. Each Ark wallet |
| 71 | + * gets its own encrypted Realm file and its own Keychain entry so wallets |
| 72 | + * never collide on WalletState/contracts/swaps and storage buckets stay |
| 73 | + * isolated. |
| 74 | + * |
| 75 | + * Concurrent callers for the same namespace receive the same in-flight |
| 76 | + * promise. Errors are surfaced to the caller; the in-flight entry is cleared |
| 77 | + * so a later retry can succeed. |
| 78 | + */ |
| 79 | +export async function getArkadeRealm(namespace: string): Promise<Realm> { |
| 80 | + const cached = realmInstances.get(namespace); |
| 81 | + if (cached && !cached.isClosed) return cached; |
| 82 | + if (cached && cached.isClosed) realmInstances.delete(namespace); |
| 83 | + |
| 84 | + const inFlight = openInFlight.get(namespace); |
| 85 | + if (inFlight) return inFlight; |
| 86 | + |
| 87 | + const opening = (async () => { |
| 88 | + await ensureArkadeDir(); |
| 89 | + |
| 90 | + const encryptionKey = await loadOrCreateEncryptionKey(namespace); |
| 91 | + |
| 92 | + const realm = await Realm.open({ |
| 93 | + schema: AllArkadeSchemas as unknown as Realm.ObjectSchema[], |
| 94 | + schemaVersion: ARKADE_REALM_SCHEMA_VERSION, |
| 95 | + onMigration: (oldRealm, newRealm) => { |
| 96 | + runArkRealmMigrations(oldRealm, newRealm); |
| 97 | + }, |
| 98 | + path: realmPathFor(namespace), |
| 99 | + encryptionKey, |
| 100 | + excludeFromIcloudBackup: true, |
| 101 | + }); |
| 102 | + |
| 103 | + realmInstances.set(namespace, realm); |
| 104 | + return realm; |
| 105 | + })(); |
| 106 | + |
| 107 | + openInFlight.set(namespace, opening); |
| 108 | + try { |
| 109 | + return await opening; |
| 110 | + } finally { |
| 111 | + openInFlight.delete(namespace); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Close the cached Realm for `namespace`, if any. The file and Keychain |
| 117 | + * entry are preserved. |
| 118 | + */ |
| 119 | +export function closeArkadeRealm(namespace: string): void { |
| 120 | + const realm = realmInstances.get(namespace); |
| 121 | + if (realm && !realm.isClosed) { |
| 122 | + realm.removeAllListeners(); |
| 123 | + realm.close(); |
| 124 | + } |
| 125 | + realmInstances.delete(namespace); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Close every cached Arkade Realm instance. Used on app shutdown / sign out. |
| 130 | + */ |
| 131 | +export function closeAllArkadeRealms(): void { |
| 132 | + for (const ns of Array.from(realmInstances.keys())) { |
| 133 | + closeArkadeRealm(ns); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Delete the Realm file and the Keychain entry for `namespace`. Used when |
| 139 | + * an Ark wallet is removed. Failures are logged but do not throw — leaving |
| 140 | + * an orphan file or Keychain entry is preferable to crashing the app's |
| 141 | + * delete path. Ark Realm failures stay scoped to the Ark wallet path. |
| 142 | + * |
| 143 | + * The Keychain encryption key is reset only when the Realm file is gone |
| 144 | + * (or never existed). Resetting the key while the encrypted file remains |
| 145 | + * would leave the user unable to open the orphan on a future re-import: |
| 146 | + * a fresh random key would be generated and the old file's ciphertext |
| 147 | + * could not be decrypted. |
| 148 | + */ |
| 149 | +export async function deleteArkadeRealm(namespace: string): Promise<void> { |
| 150 | + closeArkadeRealm(namespace); |
| 151 | + |
| 152 | + const path = realmPathFor(namespace); |
| 153 | + let realmRemoved = false; |
| 154 | + try { |
| 155 | + // Realm.deleteFile is sync and removes the .realm + .lock + .management |
| 156 | + // siblings in one call. It is forgiving when the file does not exist |
| 157 | + // (no-op), but we guard via Realm.exists to keep behavior explicit. |
| 158 | + if (Realm.exists(path)) { |
| 159 | + Realm.deleteFile({ path }); |
| 160 | + } |
| 161 | + realmRemoved = true; |
| 162 | + } catch (e: any) { |
| 163 | + console.log(`[ArkadeRealm] Realm.deleteFile failed for ${path}:`, e?.message ?? e); |
| 164 | + } |
| 165 | + |
| 166 | + // Best-effort sweep of any sibling files Realm.deleteFile might have left |
| 167 | + // behind. These are not load-bearing for re-import; failures are tolerated. |
| 168 | + for (const suffix of ['.note']) { |
| 169 | + const sibling = `${path}${suffix}`; |
| 170 | + try { |
| 171 | + if (await RNFS.exists(sibling)) await RNFS.unlink(sibling); |
| 172 | + } catch (e: any) { |
| 173 | + console.log(`[ArkadeRealm] failed to delete ${sibling}:`, e?.message ?? e); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if (!realmRemoved) { |
| 178 | + console.log( |
| 179 | + `[ArkadeRealm] keeping encryption key for ${namespace} because Realm file cleanup failed; key preserved so a future delete retry can still decrypt the orphan`, |
| 180 | + ); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + try { |
| 185 | + await Keychain.resetGenericPassword({ service: keychainServiceFor(namespace) }); |
| 186 | + } catch (e: any) { |
| 187 | + console.log(`[ArkadeRealm] failed to reset keychain for ${namespace}:`, e?.message ?? e); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +// Exported for tests only. |
| 192 | +export const __testing__ = { |
| 193 | + realmInstances, |
| 194 | + openInFlight, |
| 195 | + realmPathFor, |
| 196 | + keychainServiceFor, |
| 197 | +}; |
0 commit comments