Skip to content

Aptos

When running DApp in the mobile app and Chrome browser with Chrome Extension installed, global objects can be obtained bitkeep.aptos Make subsequent API calls .

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

Connecting to Bitget Wallet

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();

Sending a Transaction

After your web app is connected to Bitget Wallet, the web app can prompt the user to sign and send transactions to the Aptos blockchain.

Bitget Wallet API handles the transactions in two ways:

  1. Sign a transaction and submit it to the Aptos blockchain. Return a pending transaction to the web app.
  2. Sign a transaction but do not submit the transaction to the Aptos blockchain. Return the signed transaction to the web app for the web app to submit the transaction.

See the below examples for both the options.

Note:

For more on Aptos transactions, see the Aptos SDKs and Transactions guide from Aptos.

Sign and submit

The below code example shows how to use the signAndSubmitTransaction() API to sign the 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: We don't recommend using this because in most cases you don't need it, and it isn't super safe for users. They will receive an extra warning for this.

The below code example shows how to use the signTransaction() API to only sign the 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 Messages

A web app can also request the user to sign a message, by using Bitget Wallet API: provider.signMessage(payload: SignMessagePayload)

  • signMessage(payload: SignMessagePayload) prompts the user with the payload.message to be signed
  • 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!" }) This would generate the fullMessage to be signed and returned as the signature:

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

Verifying

The most common use case for signing a message is to verify ownership of a private resource.

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 Listening

onNetworkChange() and network()

A DApp may want to make sure a user is on the right network. In this case, you will need to check what network the wallet is using.

network

We support network: Mainnet | Devnet

'Testnet' has been modified to 'Mainnet'`

Default networks provided by the Bitget Wallet:

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

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

// event listener for network changing
window.bitkeep.aptos.onNetworkChange((newNetwork) => {
  network = newNetwork; // { networkName: 'Mainnet' }
});
// default networks in the wallet
enum Network {
  Mainnet = 'Mainnet'
  Devnet = 'Devnet'
}

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

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

onAccountChange()

In Bitget Wallet, a user may change accounts while interacting with your app. To check for these events, listen to them with: onAccountChange.

ts
// 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();
  }
});

Errors

When making requests to Bitget Wallet API, you may receive an error. The following is a partial list of the possible errors and their corresponding codes:

js
 {
    code: 4100,
    message:"The requested method and/or account has not been authorized by the user."
 }
 {
    code: 4100,
    message:"The requested method and/or account has not been authorized by the user."
 }
  • 4100 The requested method and/or account has not been authorized by the user.
  • 4000 No accounts found.
  • 4001 The user rejected the request