@okwallet/extension

1.0.19-beta • Public • Published

Introduction

OKX Wallet provides official SDK packages of multiple chains including Ethereum, Aptos, Tron and Solana. Developers can interact with different on-chain projects through OKX Wallet SDK.

Installation

Yarn

$ yarn add @okwallet/extension

npm

$ npm install --save @okwallet/extension
// https://www.npmjs.com/package/@okwallet/extension

Usage

ES6

import okxWeb3 from '@okwallet/extension';
console.log(okxWeb3);

Basic APIs

init

The initial access into OKX Wallet extension. You can achieve the following two functions with this method:

  1. Connecting to wallet
  2. Monitoring wallet account changes

Error Information

All errors follow this interface:

interface ProviderRpcError extends Error {
  message: string;
  code: number;
  data?: unknown;
}

Returned Error Example

{
  code: 4001;
  message: 'Tx Signature: User denied transaction signature.';
  stack: 'Error: Tx Signature: User denied transaction signature.';
}

Example

const success = (wallet) => {
  // return wallet account information
  console.log(wallet);
};
const changed = (wallet) => {
  // return wallet account information
  // if there is no wallet is connecting, it will be null
  console.log(wallet);
};
const error = (error) => {
  // Error returned when rejected
  console.error(error);
};
const uninstall = () => {};

okxWeb3
  .init({
    success,
    changed,
    error,
    uninstall,
  })
  .then((wallet) => {
    // return wallet account information
    console.log(wallet);
  })
  .catch((error) => {
    // Error returned when rejected
    console.error(error);
  });

disconnect

Disconnect with wallet

import okWeb3 from '@okwallet/extension';

// only support extension
await okWeb3.disconnect();

addListener

import okWeb3 from '@okwallet/extension';

// add
okWeb3.addListener('connect', (isConnected) => {
  console.log(isConnected); // boolean
});

okWeb3.addListener('disconnect', () => {});

okWeb3.addListener('connectWallet', (wallet) => {
  console.log(wallet);
  // [
  //   {
  //     keyringName: 'WalletA',
  //     name: 'Account01',
  //     address: [
  //       {
  //         address: '0x81Fc6F6E44a2313743bCAA060681d24597aDbDfB',
  //         coinType: '60',
  //         name: 'okc',
  //       }
  //     ]
  //   }
  //   ...
  // ]
});

okWeb3.addListener('walletChanged', (wallet) => {
  console.log(wallet);
});

okWeb3.addListener('accountChanged', (wallet) => {
  console.log(wallet);
});

okWeb3.addListener('networkChanged', (chainId) => {
  console.log(chainId);
});

okWeb3.addListener('streamFailed', () => {});

removeListener

import okWeb3 from '@okwallet/extension';

const changedCb = (wallet) => {
  console.log(wallet);
};
// add
okWeb3.addListener('walletChanged', changedCb);
// remove
okWeb3.removeListener('walletChanged', changedCb);

getAddress

CHAINS objects are the correspondence between the chain and its name:

Key Value
BITCOIN bitcoin
ETHEREUM ethereum
APTOS aptos
OKC okexchain
BSC bsc
TRON tron
SOLANA solana
POLYGON polygon
AVAX avax
FTM ftm
ARBITRUM arbitrum
OPTIMISM optimism
CRONOS cronos
KLAYTN klaytn
KCC kcc
METIS metis
BOBA boba
GNOSIS gnosis
RONIN ronin
MOONRIVER moonriver
MOONBEAM moonbeam
HARMONY harmony
LITECOIN litecoin
BITCOINCASH bitcoincash
BITCOINSV bitcoinsv
ETHW ethw
ETHF ethf
import okWeb3 from '@okwallet/extension';

const { CHAINS } = okWeb3;

// get solana chain address
okWeb3
  .getAddress(CHAINS.SOLANA)
  .then((addr) => {
    // return solana address
    console.log(addr);
  })
  .catch((error) => {
    // Error returned when rejected
    console.log(error);
  });

// get addresses of all chains
okWeb3
  .getAddress()
  .then((addrMap) => {
    // return chain-address map
    console.log(addrMap);
  })
  .catch((error) => {
    // Error returned when rejected
    console.log(error);
  });

Send Transaction

EVM

import okWeb3 from '@okwallet/extension';

const { CHAINS } = okWeb3;

const payload = {
  from: '0x2f00171409b0d4f08da9abe0b53815fa3c5c6596',
  to: '0xd8E06c62ff0F6816487973d6EB7018C854599931',
  data: '0x',
  value: '0x00',
};

okWeb3
  .sendTransaction({ chainName: CHAINS.OKC, payload })
  .then((res) => {
    console.log(res.hash);
  })
  .catch((error) => {
    console.log(error);
  });

Solana

import okWeb3 from '@okwallet/extension';
import {
  Transaction,
  SystemProgram,
  LAMPORTS_PER_SOL,
  PublicKey,
} from '@solana/web3.js';

const { CHAINS } = okWeb3;

const transaction = new Transaction({
  feePayer: okxwallet.solana.publicKey,
}).add(
  SystemProgram.transfer({
    fromPubkey: okxwallet.solana.publicKey,
    toPubkey: new PublicKey('CS8ifB68oddKXdW87RAyrxFSoz1DMMcX9WsWeAgbYDCC'),
    lamports: LAMPORTS_PER_SOL / 1000,
  })
);

okWeb3
  .sendTransaction({ chainName: CHAINS.SOLANA, payload: { transaction } })
  .then((res) => {
    console.log(res.hash);
  })
  .catch((error) => {
    console.log(error);
  });

Aptos

const { CHAINS } = okWeb3;
const payload = {
  from: '0x2f7a44297ab64890dfb618202dd552033551b71ee9b36ad7d8b2ff8b805e0281',
  function: '0x1::aptos_account::transfer',
  arguments: [
    '0xd8a97d57565ca32babc4fca6498dbc3599ee4c23690287f76941df23c6c3aca3',
    '500000',
  ],
};

okWeb3
  .sendTransaction({ chainName: CHAINS.APTOS, payload })
  .then((res) => {
    console.log(res.hash);
  })
  .catch((error) => {
    console.log(error);
  });

Sign Personal Message

EVM

import okWeb3 from '@okwallet/extension';

const { CHAINS } = okWeb3;
const payload = {
  message: 'test message',
  account: '0xec92d8ea11587892f0516c19dcb1f8fd72993584',
};
okWeb3
  .signMessage({ chainName: CHAINS.OKC, payload })
  .then((signature) => {
    console.log(signature);
  })
  .catch((error) => {
    console.log(error);
  });

Solana

import okWeb3 from '@okwallet/extension';

const { CHAINS } = okWeb3;
const payload = {
  message: 'test message',
};
okWeb3
  .signMessage({ chainName: CHAINS.SOLANA, payload })
  .then((signature) => {
    console.log(signature);
  })
  .catch((error) => {
    console.log(error);
  });

Aptos

import okWeb3 from '@okwallet/extension';
const { CHAINS } = okWeb3;

const payload = {
  message: 'test message',
  nonce: 1,
};
okWeb3
  .signMessage({ chainName: CHAINS.APTOS, payload })
  .then((signature) => {
    console.log(signature);
  })
  .catch((error) => {
    console.log(error);
  });

Aptos

Special methods for Aptos are shown below. Methods of okWeb3.aptos objects are required to call Aptos chain.

Register Coin

import okWeb3 from '@okwallet/extension';

okWeb3.aptos
  .registerAptosCoin({
    contractAddress:
      '0xa2eda21a58856fda86451436513b867c97eecb4ba099da5775520e0f7492e852::coin::T',
  })
  .then((hash) => {
    console.log(hash);
  })
  .catch((error) => {
    console.log(error);
  });

Acquire Transaction Status by Hash

import okWeb3 from '@okwallet/extension';

okWeb3.aptos
  .getAptosTransaction(
    '0x042d2a5ce883e4c8e13351b9053ec0ec86728a88ba07331cd5389816930bdb31'
  )
  .then((status) => {
    console.log(status);
  })
  .catch((error) => {
    console.log(error);
  });

Acquire Balance

  • Main chain tokens
import okWeb3 from '@okwallet/extension';

okWeb3.aptos
  .getAptosBalance({
    address:
      '0xcd30fbbda98b2aed026772c13e5ed90a7f056b589ef9e78cd96415e1af12451c',
    currency: '0x1::aptos_coin::AptosCoin',
  })
  .then((value) => {
    alert(value);
  })
  .catch((error) => {
    console.log(error);
  });
  • Token
import okWeb3 from '@okwallet/extension';

okWeb3.aptos
  .getAptosBalance({
    address:
      '0x253869f4b71ac5786c49c42b51faa0d0a58ff551cb9a7c267aaaa42018c89dd7',
    currency:
      '0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDT',
  })
  .then((value) => {
    console.log(value);
  })
  .catch((error) => {
    console.log(error);
  });

Check and Register Coin

import okWeb3 from '@okwallet/extension';

okWeb3.aptos
  .checkAndRegAptosCoin({
    address:
      '0x253869f4b71ac5786c49c42b51faa0d0a58ff551cb9a7c267aaaa42018c89dd7',
    currency:
      '0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDT',
  })
  .then((res) => {
    const { registered, hash } = res;
    console.log(registered);
    if (!registered) {
      console.log(res.hash);
    }
  })
  .catch((error) => {
    console.log(error);
  });

Readme

Keywords

Package Sidebar

Install

npm i @okwallet/extension

Weekly Downloads

23

Version

1.0.19-beta

License

MIT

Unpacked Size

3.17 MB

Total Files

17

Last publish

Collaborators

  • linus.wang