2.2 KiB
2.2 KiB
@polkadot/extension-dapp
A basic extractor that manipulates the window.injectedWeb3 to retrieve all the providers added to the page. It has a number of utilities -
web3Enable(dappName: string): Promise<InjectedExtension[]>- to be called before anything else, retrieves the list of all injected extensions/providersweb3Accounts(): Promise<InjectedAccountWithMeta[]>- returns a list of all the injected accounts, accross all extensions (source in meta)web3AccountsSubscribe(cb: (accounts: InjectedAccountWithMeta[]) => any): Promise<Unsubcall>- subscribes to the accounts accross all extensions, returning a full list as changes are madeweb3FromAddress(address: string): Promise<InjectedExtension>- Retrieves a provider for a specific addressweb3FromSource(name: string): Promise<InjectedExtension>- Retrieves a provider identified by the nameisWeb3Injected: boolean- boolean to indicate ifinjectedWeb3was found on the pageweb3EnablePromise: Promise<InjectedExtension[]> | null-nullor the promise as a result of the last call toweb3Enable
Usage
import { web3Accounts, web3Enable, web3FromAddress } from '@polkadot/extension-dapp';
// returns an array of all the injected sources
// (this needs to be called first, before other requests)
const allInjected = await web3Enable('my cool dapp');
// returns an array of { address, meta: { name, source } }
// meta.source contains the name of the extension that provides this account
const allAccounts = await web3Accounts();
// finds an injector for an address
const injector = await web3FromAddress('5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE');
// sets the signer for the address on the @polkadot/api
api.setSigner(injector.signer);
// sign and send out transaction - notice here that the address of the account (as retrieved injected)
// is passed through as the param to the `signAndSend`, the API then calls the extension to present
// to the user and get it signed. Once completex, the api sends the tx + signature via the normal process
api.tx.balances
.transfer('5C5555yEXUcmEJ5kkcCMvdZjUo7NGJiQJMS7vZXEeoMhj3VQ', 123456)
.signAndSend('5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE', (status) => { ... });