UNPKG

@storacha/client

Version:

Client for the storacha.network w3up api

45 lines 1.76 kB
/** * The main entry point for the `@storacha/client` package. * * Use the static {@link create} function to create a new {@link Client} object. * * @module */ import { AgentData } from '@storacha/access/agent'; import { StoreIndexedDB } from '@storacha/access/stores/store-indexeddb'; import { generate } from '@ucanto/principal/rsa'; import { Client } from './client.js'; export * as Result from './result.js'; export * as Account from './account.js'; export * from './ability.js'; export { authorizeContentServe } from './client.js'; /** * Create a new w3up client. * * If no backing store is passed one will be created that is appropriate for * the environment. * * If the backing store is empty, a new signing key will be generated and * persisted to the store. In the browser an unextractable RSA key will be * generated by default. In other environments an Ed25519 key is generated. * * If the backing store already has data stored, it will be loaded and used. * * @type {import('./types.js').ClientFactory} */ export async function create(options = {}) { const store = options.store ?? new StoreIndexedDB('w3up-client'); const raw = await store.load(); if (raw) { const data = AgentData.fromExport(raw, { store }); if (options.principal && data.principal.did() !== options.principal.did()) { throw new Error(`store cannot be used with ${options.principal.did()}, stored principal and passed principal must match`); } return new Client(data, options); } const principal = options.principal ?? (await generate()); const data = await AgentData.create({ principal }, { store }); return new Client(data, options); } export { Client }; //# sourceMappingURL=index.js.map