OneKey Docs
  • 👋GUIDE
    • Introduction
    • Web App Integration Developer
    • Hardware Integration Developer
    • External Contribution Guide
    • Common Terms
  • 🔮Connect To Software
    • Compatible with Metamask
      • detectEthereumProvider
    • Support Wallet Kit
      • Web3 Onboard
      • Rainbowkit
      • Aptos Wallet Adapter
      • Web3Modal
    • WebApp Connect OneKey
      • ETH
        • Provider API
        • RPC API
        • Accessing Accounts
        • Sending Transactions
        • Signing Data
      • BTC
        • Guide
        • API Reference
          • requestAccounts
          • getAccounts
          • getNetwork
          • switchNetwork
          • getPublicKey
          • getBalance
          • getInscriptions
          • sendBitcoin
          • sendInscription
          • signMessage
          • pushTx
          • signPsbt
          • signPsbts
          • pushPsbt
          • inscribeTransfer (Deprecated)
        • Event
      • Nostr
        • Guide
        • API Reference
          • getPublicKey
          • signEvent
          • signSchnorr
          • getRelays
          • nip04.encrypt
          • nip04.decrypt
        • Event
      • WebLN
        • Guide
        • API Reference
          • enable
          • getInfo
          • makeInvoice
          • sendPayment
          • signMessage
          • verifyMessage
          • lnurl
          • getBalance
        • Event
      • NEAR
        • Introduction
        • Integrating
          • Install the Provider SDK
          • Detecting the Provider
          • Establishing a Connection
          • Accessing Accounts
          • Detecting Provider Network
          • Watch Accounts & Network Status
          • Sending Transactions
            • Create Transaction
            • Sign and Send Transaction
            • Signing Transaction
          • Signing Messages
          • RPC API Calling
          • Debug Logging
          • Migrate from Near Web Wallet
        • Reference
          • API Reference
          • Trouble Shooting
        • Resources
          • Example & Demo
          • FAQ
      • SOLANA
        • Detecting the Provider
        • Establishing a Connection
        • Sending a Transaction
        • Signing a Message
    • Using WalletConnect
      • ETH
      • APTOS
        • Wallet Connect API
  • 📟Connect to Hardware
    • Hardware SDK
      • Started
      • Install SDK
      • Config Event
      • Common Params
      • Path Params
      • Error Code
      • API Reference
        • Basic API
          • Init SDK
          • Search Devices
          • Get Features
          • Get Passphrase State
          • Cancel Request
          • Response UI Event
        • Device API
          • deviceSupportFeatures
          • checkBridgeStatus
          • checkTransportRelease
          • checkFirmwareRelease
          • checkBLEFirmwareRelease
          • deviceChangePin
          • deviceReset
          • deviceSettings
          • deviceUpdateReboot
          • deviceVerify
          • deviceWipe
          • firmwareUpdate
        • Bitcoin & Bitcoin forks
          • btcGetAddress
          • btcGetPublicKey
          • btcSignMessage
          • btcSignTransaction
          • btcVerifyMessage
        • Ethereum & EVM
          • evmGetPublicKey
          • evmGetAddress
          • evmSignTransaction
          • evmSignTypedData
          • evmSignMessage
          • evmVerifyMessage
        • Algorand
          • algoGetAddress
          • algoSignTransaction
        • Aptos
          • aptosGetAddress
          • aptosGetPublicKey
          • aptosSignMessage
          • aptosSignTransaction
        • Alephium
          • alephiumGetAddress
          • alephiumSignMessage
          • alephiumSignTransaction
        • Cardano
          • cardanoGetAddress
          • cardanoGetPublicKey
          • cardanoSignMessage
          • cardanoSignTransaction
        • Conflux
          • confluxGetAddress
          • confluxSignMessage
          • confluxSignMessageCIP23
          • confluxSignTransaction
        • Cosmos
          • cosmosGetAddress
          • cosmosGetPublicKey
          • cosmosSignTransaction
        • Dynex
          • dnxGetAddress
          • dnxSignTransaction
        • FileCoin
          • filecoinGetAddress
          • filecoinSignTransaction
        • Kaspa
          • kaspaGetAddress
          • kaspaSignTransaction
        • Near
          • nearGetAddress
          • nearSignTransaction
        • Nervos
          • nervosGetAddress
          • nervosSignTransaction
        • Nostr
          • nostrGetPublicKey
          • nostrSignEvent
          • nostrSignSchnorr
          • nostrEncryptMessage
          • nostrDecryptMessage
        • NEM
          • nemGetAddress
          • nemSignTransaction
        • Nexa
          • nexaGetAddress
          • nexaSignTransaction
        • Polkadot
          • polkadotGetAddress
          • polkadotSignTransaction
        • Ripple
          • xrpGetAddress
          • xrpSignTransaction
        • Scdo
          • scdoGetAddress
          • scdoSignMessage
          • scdoSignTransaction
        • Solana
          • solGetAddress
          • solSignTransaction
        • Starcoin
          • startcoinGetAddress
          • starcoinGetPublicKey
          • starcoinSignMessage
          • starcoinSignTransaction
          • starcoinVerifyMessage
        • Stellar
          • stellarGetAddress
          • stellarSignTransaction
        • Sui
          • suiGetAddress
          • suiGetPublicKey
          • suiSignMessage
          • suiSignTransaction
        • Tron
          • tronGetAddress
          • tronSignMessage
          • tronSignTransaction
        • Ton
          • tonGetAddress
          • tonSignMessage
          • tonSignProof
      • Advanced
        • Passphrase
        • Common SDK Guide
        • Low-level transport plugin
        • OneKey Message Protocol
      • Tutorial: Get started with OneKey
    • Air Gap SDK
      • Started
      • Tutorial: Wallet Integration
      • API Reference
        • Basic API
          • CryptoHDkey
          • CryptoCoinInfo
          • CryptoKeypath
        • Ethereum & EVM
          • EthSignRequest
          • EthSignature
  • ⛳Best Practice
    • Registering Your Contract's Method Names
    • Registering Tokens with Users
    • Defining Your App's Icon
    • Migration from MetaMask
  • 🎨Logo Assets
  • 🧙Support
  • 🕹️Trouble Shooting
  • ❤️Contribute
Powered by GitBook
On this page
  • Best Practices
  • Other options
  • warning

Was this helpful?

Edit on GitHub
  1. Best Practice

Migration from MetaMask

If there is already a Dapp Web Application compatible with the latest MetaMask (Metamask v8 or later) before, it will be very easy to be compatible with the OneKey browser Extension.

The latest MetaMask will inject the window.ethereum object to make the page manipulate the user's account in the metamask and obtain configuration information. Similarly, while window.onekey maintains a large number of APIs compatible with window.ethereum, and OneKey Browser Extension also injects window.ethereum to ensure the compatibility of other Dapps. Users can manually turn on "Alternative MetaMask" in OneKey browser Extension v2.0.1 or later to prevent possible conflicts during co-installation with MetaMask.

Best Practices

It is recommended that Dapp developers completely distinguish the two objects of window.onekey and window.ethereum when calling internal methods, namely:

  • If you want the logic triggered by the OneKey browser Extension will completely use window.onekey for subsequent operations.

  • If you want the logic triggered by the MetaMask browser Extension will completely use window.ethereum for subsequent operations.At the same time, through the following JS code, you can ensure that the user wakes up MetaMask.

// If you want to operate the methods and logic of MetaMask related providersif (window.ethereum && window.ethereum.switchProvider) {  window.ethereum.switchProvider('metamask');}// After the switchProvider is executed, the subsequent operation window.ethereum must be the metamask injected into the page

Other options

Of course, in order to be compatible with earlier Dapps that are not compatible with window.onekey, the OneKey browser Extension will also add window.ethereum to the page to assist in operations.

Use the window.ethereum.switchProvider method to switch between the variables injected by the OneKey browser Extension and the variables injected by MetaMask.

warning

window.ethereum.switchProvider is the new content of OneKey browser plugin v2.0.1 and later, and it is also a unique method of OneKey browser Extension.

If the user does not install the OneKey browser Extension and only installs MetaMask, the method does not exist. Please make sure that the method is accessible before calling.

// connect MetaMaskfunction showInstallMessage(){  alert('Please Install MetaMask here: https://metamask.io/');}
if (!window.ethereum) {  showInstallMessage();  return;}
// DO NOT call methods or accessing properties before switchProvider()if (window.ethereum && window.ethereum.switchProvider) {  window.ethereum.switchProvider('metamask');}
if (!window.ethereum.isMetaMask) {  showInstallMessage();  return;}
// call window.ethereum methods...
// connect OneKeyfunction showInstallMessage(){  alert('Please Install MetaMask here: https://onekey.so/');}
if (!window.ethereum) {  showInstallMessage();  return;}
// DO NOT call methods or accessing properties before switchProvider()if (window.ethereum && window.ethereum.switchProvider) {  window.ethereum.switchProvider('onekey');}
if (!window.ethereum.isOneKey) {  showInstallMessage();  return;}
// call window.ethereum methods...

Developers must ensure that all relevant business codes that access window.ethereum must be executed after switchProvider.

Otherwise, the code executed before switchProvider may cause an exception because of the uncertainty of the extension instance corresponding to window.ethereum.

PreviousDefining Your App's IconNextTrouble Shooting

Last updated 2 years ago

Was this helpful?

⛳