Skip to content

Aptos

When running a DApp in the Bitget Wallet App or in the Chrome browser with the Chrome Extension installed, you can access the global object window.bitkeep.aptos for subsequent API calls.

js
const provider = window.bitkeep.aptos
const provider = window.bitkeep.aptos

Injected Object Properties and Methods

  • connect - () => Promise<{ publicKey: string; address: string; }>:Connection method, returns public key and address
  • account - () => Promise:Retrieves account information
  • isConnected - () => Promise:Retrieves connection status
  • network - () => Promise:Retrieves the current network
  • onAccountChange - Function:Callback for account changes
  • onNetworkChange - Function:Callback for network changes
  • onDisconnect - Function:Callback for disconnection
  • signTransaction - (transaction, options) => Promise:Wallet signing method, takes transaction information and additional options
  • signMessage - (msg) => Promise:Signs a message
  • disconnect - () => Promise:Disconnects the connection

Connect Wallet

Return Value

  • result - object
    • publicKey - string
    • address - string
js
try {
  const response = await provider.connect();
  console.log(response); // { address: string, address: string }

  const account = await provider.account();
  console.log(account); // { address: string, address: string }
} catch (error) {
  // { code: 4001, message: "User rejected the request."}
}
try {
  const response = await provider.connect();
  console.log(response); // { address: string, address: string }

  const account = await provider.account();
  console.log(account); // { address: string, address: string }
} catch (error) {
  // { code: 4001, message: "User rejected the request."}
}

Get Wallet Address

js
const account = await provider.account();
const account = await provider.account();

Send Transaction

Once the DApp successfully connects to Bitget Wallet, it can send transactions to the Aptos blockchain.

Bitget Wallet API handles transactions in two ways:

  1. Sign the transaction and submit it to the Aptos blockchain. Returns a pending transaction to the DApp.
  2. Sign a transaction but do not submit it to the Aptos blockchain. Returns the signed transaction, which the DApp then submits.

See the examples below to understand these two options.

Sign and submit

The following code example shows how to use the signAndSubmitTransaction() API to sign a transaction and send it to the Aptos blockchain.

js
const wallet = getAptosWallet(); // see "Connecting"

// Example Transaction, following an [EntryFunctionPayload](https://github.com/aptos-labs/aptos-core/blob/main/ecosystem/typescript/sdk/src/generated/models/EntryFunctionPayload.ts#L8-L21)
const transaction = {
    arguments: [address, '717'],
    function: '0x1::coin::transfer',
    type: 'entry_function_payload',
    type_arguments: ['0x1::aptos_coin::TestCoin'],
};

/**
 *  Custom gas fee
 *  default {
        "gas_unit_price":"100",
        "max_gas_amount":"10000"
    }
 */
const options = {
    gas_unit_price:100,
    max_gas_amount:10000
} 

try {
    const pendingTransaction = await window.bitkeep.aptos.signAndSubmitTransaction(transaction);

    // const pendingTransaction = await window.bitkeep.aptos.signAndSubmitTransaction(transaction, options);

    // In most cases a dApp will want to wait for the transaction, in these cases you can use the typescript sdk
    const client = new AptosClient('https://testnet.aptoslabs.com');
    client.waitForTransaction(pendingTransaction.hash);
} catch (error) {
    // see "Errors"
}
const wallet = getAptosWallet(); // see "Connecting"

// Example Transaction, following an [EntryFunctionPayload](https://github.com/aptos-labs/aptos-core/blob/main/ecosystem/typescript/sdk/src/generated/models/EntryFunctionPayload.ts#L8-L21)
const transaction = {
    arguments: [address, '717'],
    function: '0x1::coin::transfer',
    type: 'entry_function_payload',
    type_arguments: ['0x1::aptos_coin::TestCoin'],
};

/**
 *  Custom gas fee
 *  default {
        "gas_unit_price":"100",
        "max_gas_amount":"10000"
    }
 */
const options = {
    gas_unit_price:100,
    max_gas_amount:10000
} 

try {
    const pendingTransaction = await window.bitkeep.aptos.signAndSubmitTransaction(transaction);

    // const pendingTransaction = await window.bitkeep.aptos.signAndSubmitTransaction(transaction, options);

    // In most cases a dApp will want to wait for the transaction, in these cases you can use the typescript sdk
    const client = new AptosClient('https://testnet.aptoslabs.com');
    client.waitForTransaction(pendingTransaction.hash);
} catch (error) {
    // see "Errors"
}

Sign Only

Important: Not recommended as it is not absolutely safe for users. The wallet will also issue additional warnings.

The following code example shows how to use the signTransaction() API to sign a transaction without submitting it to the Aptos blockchain.

js
const wallet = getAptosWallet(); // see "Connecting"

// Example Transaction
const transaction = {
    arguments: [address, '717'],
    function: '0x1::coin::transfer',
    type: 'entry_function_payload',
    type_arguments: ['0x1::aptos_coin::TestCoin'],
};

/** Custom gas fee
 *  default {
        "gas_unit_price":"100",
        "max_gas_amount":"10000"
    }
 */
const options = {

    gas_unit_price: 100,
    max_gas_amount:10000
} 

try {
    const signTransaction = await window.bitkeep.aptos.signTransaction(transaction)
    // const signTransaction = await window.bitkeep.aptos.signTransaction(transaction, options)
} catch (error) {
    // see "Errors"
}
const wallet = getAptosWallet(); // see "Connecting"

// Example Transaction
const transaction = {
    arguments: [address, '717'],
    function: '0x1::coin::transfer',
    type: 'entry_function_payload',
    type_arguments: ['0x1::aptos_coin::TestCoin'],
};

/** Custom gas fee
 *  default {
        "gas_unit_price":"100",
        "max_gas_amount":"10000"
    }
 */
const options = {

    gas_unit_price: 100,
    max_gas_amount:10000
} 

try {
    const signTransaction = await window.bitkeep.aptos.signTransaction(transaction)
    // const signTransaction = await window.bitkeep.aptos.signTransaction(transaction, options)
} catch (error) {
    // see "Errors"
}

Signing Message

A DApp requests the user to sign information using the Bitget Wallet API: provider.signMessage(payload: SignMessagePayload).

  • signMessage(payload: SignMessagePayload) prompts the user to sign payload.message
  • returns Promise<SignMessageResponse>

Types:

ts
export interface SignMessagePayload {
  address?: boolean; // Should we include the address of the account in the message
  application?: boolean; // Should we include the domain of the dapp
  chainId?: boolean; // Should we include the current chain id the wallet is connected to
  message: string; // The message to be signed and displayed to the user
  nonce: string; // A nonce the dapp should generate
}

export interface SignMessageResponse {
  address: string;
  application: string;
  chainId: number;
  fullMessage: string; // The message that was generated to sign
  message: string; // The message passed in by the user
  nonce: string;
  prefix: string; // Should always be APTOS
  signature: string; // The signed full message
}
export interface SignMessagePayload {
  address?: boolean; // Should we include the address of the account in the message
  application?: boolean; // Should we include the domain of the dapp
  chainId?: boolean; // Should we include the current chain id the wallet is connected to
  message: string; // The message to be signed and displayed to the user
  nonce: string; // A nonce the dapp should generate
}

export interface SignMessageResponse {
  address: string;
  application: string;
  chainId: number;
  fullMessage: string; // The message that was generated to sign
  message: string; // The message passed in by the user
  nonce: string;
  prefix: string; // Should always be APTOS
  signature: string; // The signed full message
}

Example Message

signMessage({nonce: 1234034, message: "Welcome to dapp!" }) Generates a fullMessage to be signed and returns it as a signature:

text
    APTOS
    nonce: 1234034
    message: Welcome to dapp!
    APTOS
    nonce: 1234034
    message: Welcome to dapp!

Verification

Signing information is intended to verify ownership of private resources.

js
import nacl from 'tweetnacl';

const message = "hello";
const nonce = "random_string"

try {
  const response = await window.bitkeep.aptos.signMessage({
    message,
    nonce,
  });
  const { publicKey } = await window.bitkeep.aptos.account();
  // Remove the 0x prefix
  const key = publicKey!.slice(2, 66);
  const verified = nacl.sign.detached.verify(Buffer.from(response.fullMessage),
                                             Buffer.from(response.signature, 'hex'),
                                             Buffer.from(key, 'hex'));
  console.log(verified);
} catch (error) {
  console.error(error);
}
import nacl from 'tweetnacl';

const message = "hello";
const nonce = "random_string"

try {
  const response = await window.bitkeep.aptos.signMessage({
    message,
    nonce,
  });
  const { publicKey } = await window.bitkeep.aptos.account();
  // Remove the 0x prefix
  const key = publicKey!.slice(2, 66);
  const verified = nacl.sign.detached.verify(Buffer.from(response.fullMessage),
                                             Buffer.from(response.signature, 'hex'),
                                             Buffer.from(key, 'hex'));
  console.log(verified);
} catch (error) {
  console.error(error);
}

Event Listeners

network

We support network: Mainnet | Devnet

'Testnet' has been modified to 'Mainnet'`

Default networks provided by Bitget Wallet:

ts
// default networks in the wallet
enum Network {
  Mainnet = 'Mainnet'
  Devnet = 'Devnet'
}
// default networks in the wallet
enum Network {
  Mainnet = 'Mainnet'
  Devnet = 'Devnet'
}

onNetworkChange() & network()

The DApp needs to ensure that the user is connected to the target network, so it needs to get the current network, switch networks, and listen for network changes.

js
// Current network
let network = await window.bitkeep.aptos.network();

// event listener for network changing
window.bitkeep.aptos.onNetworkChange((newNetwork) => {
  network = newNetwork; // { networkName: 'Mainnet' }
});
// Current network
let network = await window.bitkeep.aptos.network();

// event listener for network changing
window.bitkeep.aptos.onNetworkChange((newNetwork) => {
  network = newNetwork; // { networkName: 'Mainnet' }
});

onAccountChange()

The user may change accounts while interacting with the DApp, and this can be monitored via onAccountChange.

js
// get current account
let currentAccount = await window.bitkeep.aptos.account();

// event listener for disconnecting
window.bitkeep.aptos.onAccountChange((newAccount) => {
  // If the new account has already connected to your app then the newAccount will be returned
  if (newAccount) {
    currentAccount = newAccount;
  } else {
    // Otherwise you will need to ask to connect to the new account
    currentAccount = window.bitkeep.aptos.connect();
  }
});
// get current account
let currentAccount = await window.bitkeep.aptos.account();

// event listener for disconnecting
window.bitkeep.aptos.onAccountChange((newAccount) => {
  // If the new account has already connected to your app then the newAccount will be returned
  if (newAccount) {
    currentAccount = newAccount;
  } else {
    // Otherwise you will need to ask to connect to the new account
    currentAccount = window.bitkeep.aptos.connect();
  }
});