On this page
Getting Started
Enable the extension, select an account, and connect an API client to the intended chain. Construct transaction payloads from current chain metadata; the abbreviated hex values in this guide are placeholders.
Learn how to detect, connect, and interact with Polkadot and Substrate chains.
OneKey implements the Polkadot.js extension interface, for the operations documented here.
Provider Detection
// OneKey registers as a Polkadot.js extension
const provider = window.$onekey?.polkadot
// Or use the standard web3Enable API
import { web3Enable, web3Accounts } from '@polkadot/extension-dapp'Enable Extension
// Using OneKey provider directly
const enabled = await provider.web3Enable('My dApp')
if (enabled) {
const accounts = await provider.web3Accounts()
console.log('Accounts:', accounts)
}Using Polkadot.js Extension API (Recommended)
import { web3Enable, web3Accounts } from '@polkadot/extension-dapp'
// Enable all extensions (including OneKey)
const extensions = await web3Enable('My dApp')
if (extensions.length === 0) {
throw new Error('No extension found')
}
// Get all accounts
const allAccounts = await web3Accounts()
console.log('Accounts:', allAccounts)Get Accounts
const accounts = await provider.web3Accounts()
accounts.forEach(account => {
console.log({
address: account.address, // Substrate address
name: account.name, // Account name
type: account.type, // 'sr25519' | 'ed25519' | 'ecdsa'
genesisHash: account.genesisHash, // Optional chain genesis
})
})Subscribe to Account Changes
const unsubscribe = provider.web3AccountsSubscribe((accounts) => {
console.log('Accounts changed:', accounts)
})
// Later: unsubscribe()Using with Polkadot.js API
Setup
npm install @polkadot/api @polkadot/extension-dappConnect and Sign Transaction
import { ApiPromise, WsProvider } from '@polkadot/api'
import { web3Enable, web3FromAddress, web3Accounts } from '@polkadot/extension-dapp'
// Enable extensions
await web3Enable('My dApp')
// Get accounts
const accounts = await web3Accounts()
const account = accounts[0]
if (!account) throw new Error("No account was selected")
// Connect to chain
const wsProvider = new WsProvider('wss://rpc.polkadot.io')
const api = await ApiPromise.create({ provider: wsProvider })
// Get injector
const injector = await web3FromAddress(account.address)
// Create and sign transaction
const tx = api.tx.balances.transferKeepAlive(
recipientAddress,
'10000000000' // 1 DOT = 10^10 planck on Polkadot
)
// Sign and send
const hash = await tx.signAndSend(
account.address,
{ signer: injector.signer }
)
console.log('Transaction hash:', hash.toHex())Last updated on