Adattatori di archiviazione
Guida di integrazione e riferimento API: Adattatori di archiviazione.
Questo riferimento API dettagliato è mantenuto in inglese e segue il codice SDK collegato. Gli esempi con variabili applicative richiedono il proprio firmatario, chiavi, archiviazione o interfaccia.
The interface
interface KeyValueStore {
get<T>(key: string): Promise<T | undefined>;
set<T>(key: string, value: T): Promise<void>;
remove(key: string): Promise<void>;
keys(): Promise<string[]>;
subscribe?(listener: (key: string) => void): () => void;
}subscribe is optional — not every backend can report changes. Check for it before calling.
Implementations behave as a value store, not a reference store: what comes back out of get is unaffected by later mutation of what went into set, and vice versa. A backend that serializes gets that for free; MemoryStore deep clones to match it, so a test that passes against MemoryStore passes against the real thing.
MemoryStore
In-memory, for tests and for state that should not outlive the process.
import { MemoryStore } from 'nostr-wot-sdk/storage';
const store = new MemoryStore({ seeded: 'value' });
await store.set('profile', { name: 'leon' });
await store.get('profile'); // { name: 'leon' } — a fresh copy each timenamespaced
Views one store as a private sub-store, prefixing every key with ${prefix}:. Two subsystems sharing one physical store cannot read or clobber each other by picking the same key name.
import { namespaced } from 'nostr-wot-sdk/storage';
const signer = namespaced(store, 'signer');
const cache = namespaced(store, 'cache');
await signer.set('key', 'a'); // lands at `signer:key`
await cache.set('key', 'b'); // lands at `cache:key`
await signer.keys(); // ['key'] — unprefixed, and only its ownkeys() reports unprefixed keys and filters out every other namespace. subscribe, when the underlying store has one, does the same. Nesting composes: namespaced(namespaced(s, 'a'), 'b') writes a:b:key.
Keys are free-form and may contain the : separator. The prefix is percent-escaped so that they can, which keeps the mapping from (prefix, key) to storage key injective — without it, namespaced(s, 'a').set('b:c', …) and namespaced(s, 'a:b').set('c', …) would both write a:b:c and silently clobber each other.
Host requirements
The portable account, vault, permission and signing modules use injected storage and host ports. They require TextEncoder, TextDecoder, secure crypto.getRandomValues, timers and AbortController; post-quantum paths additionally require atob/btoa or the supported Buffer fallback. Initialize the necessary React Native/Hermes polyfills before importing these modules, including react-native-get-random-values for secure randomness. Do not substitute a non-cryptographic random source.
The portable cryptographic core does not require the DOM, WebExtension APIs or WebCrypto subtle. Platform adapters such as WebAuthn ceremonies and relay transports have their own requirements. See the identity runtime guide.
This package itself uses neither TextEncoder nor TextDecoder; MemoryStore clones with a JSON round trip, which is also what every real backend does on the way to disk.