Package Exports
- @unisat/permission-service
- @unisat/permission-service/adapters
Readme
@unisat/permission-service
Cross-platform permission management service for web3 applications.
Features
- Cross-platform: Works in browsers, Node.js, and browser extensions
- Storage Adapters: Flexible storage backend (memory, localStorage, extension storage)
- LRU Cache: Efficient permission caching with automatic cleanup
- Type Safe: Full TypeScript support
- Site Management: Pin, unpin, and organize connected sites
Installation
npm install @unisat/permission-service
# or
yarn add @unisat/permission-serviceQuick Start
import { PermissionService, MemoryStorageAdapter, ChainType } from '@unisat/permission-service';
// Create with memory storage
const permissionService = new PermissionService({
storage: new MemoryStorageAdapter(),
storageKey: 'permissions'
});
// Initialize
await permissionService.init();
// Add a connected site
await permissionService.addConnectedSite(
'https://example.com',
'Example DApp',
'https://example.com/icon.png',
ChainType.BITCOIN_MAINNET
);
// Check permissions
const hasPermission = permissionService.hasPermission('https://example.com');
console.log(hasPermission); // trueStorage Adapters
Memory Storage
import { MemoryStorageAdapter } from '@unisat/permission-service/adapters';
const storage = new MemoryStorageAdapter();LocalStorage (Browser)
import { LocalStorageAdapter } from '@unisat/permission-service/adapters';
const storage = new LocalStorageAdapter();Extension Storage
import { ExtensionPersistStoreAdapter } from '@unisat/permission-service/adapters';
import { createPersistStore } from '@/background/utils';
const storage = new ExtensionPersistStoreAdapter(createPersistStore, 'permissions');API Reference
PermissionService
Main service class for managing permissions.
Methods
init(): Initialize the serviceaddConnectedSite(origin, name, icon, chain): Add a connected sitehasPermission(origin): Check if origin has permissiongetSite(origin): Get site informationgetConnectedSites(): Get all connected sitesremoveConnectedSite(origin): Remove a connected sitetopConnectedSite(origin): Pin site to topunpinConnectedSite(origin): Unpin site
License
MIT