Library for interaction with the Swarm Browser Extension, from dApps or other browser extensions.
The library can be installed via npm:
npm install --save @ethersphere/swarm-extension
All interaction with the Swarm browser extension is established through the Swarm class:
import { Swarm } from '@fairdatasociety/swarm-extension'
By default the class will connect to the Swarm browser extension using its ID from the Google store. If you are running your version of the extension the class can be configured with a different extension ID.
const swarm = new Swarm() // Using the default Swarm ID from the Google store
const swarm = new Swarm('Swarm Extension ID...') // Using custom Swarm ID
To test if connection with the Swarm extension is established, call the echo
method:
const text = await swarm.echo<string>('test')
console.log(text) // 'test'
Before interacting with the library, Dapp should register new session by calling:
await swarm.register()
or new session will be implicitly created when calling any method for the first time.
After registering a new session, session ID will be available as:
swarm.sessionId
There are four different objects available in the Swarm calass:
- swarm.bzzLink - utility functions for converting bzz links
- swarm.localStorage - methods for interacting with local storage
- swarm.postageBatch - checking postage batch status
- swarm.web2Helper - getting information about bee URL
Once when the instance of the Swarm class is not needed anymore, connection with the extension can be terminated.
swarm.closeConnection()
To enable Swarm HTML features, include the swarm-html.js
script into HTML page. For more details check Swarm HTML in the main readme
To watch for changes in the source code and recompile the library on change:
npm start
To build the library:
npm run build