Skip to content

StarkNet

Get the injected object

Dapp can access the injected object in two ways, namely

js
const provider = window.starknet_bitkeep; //It is recommended to use this method
const provider = window.starknet;
const provider = window.starknet_bitkeep; //It is recommended to use this method
const provider = window.starknet;

Inject object properties and methods

  • name - string:Wallet name, value is'Bitget Wallet'.
  • icon - string:wallet icon
  • version - string:version
  • chainId - string:Only supports mainnet, value is SN_MAIN
  • isConnected - boolean::Is the current wallet connected
  • selectedAddress - string:The wallet address currently selected by the user
  • account - Account:Access the account object, which is inherited from the Account of starknet.js. For specific properties and methods on the instance, please refer to starknet.js
  • provider - Provider:To access the provider object, use RpcProvider of starknet.js. For specific properties and methods on the instance, please refer to starknet.js
  • enable - () => [string]:Used to connect to the wallet. After a successful call, the BitgetWallet wallet connection page will be evoked. The user can decide whether to connect to the current DApp. If the user agrees, a single-item array of the selected address will be returned.
  • on - (event, callback) => void:Add event listener
    • accountsChanged Event: This event is triggered when the user switches accounts and returns an array of new addresses; when the connection is disconnected, an empty array is returned.
  • off - (event, callback) => void:Remove event listener
  • disconnect - Disconnect wallet

Connected to BitgetWallet

parameters

  • options - object:Optional
    • starknetVersion - v4 | v5:default v5
js
const [public_address] = await provider.enable();
//const [public_address] = await provider.enable({ starknetVersion: 'v4' });

const account = provider.account;
// account = {
//     address: "0x04a6f...52f4d801c84",
//     cairoVersion: "0",
//     sdkVersion: "v5",
//     ...
// }

provider.on("accountsChanged", (event) => {
    // cb(event)...
});
const [public_address] = await provider.enable();
//const [public_address] = await provider.enable({ starknetVersion: 'v4' });

const account = provider.account;
// account = {
//     address: "0x04a6f...52f4d801c84",
//     cairoVersion: "0",
//     sdkVersion: "v5",
//     ...
// }

provider.on("accountsChanged", (event) => {
    // cb(event)...
});

Add token

parameters

  • type - string:wallet_watchAsset
  • params - object:Token Infos
js
const res = await provider.request({
    type: 'wallet_watchAsset',
    params: {
        type: 'ERC20',
        options: {
            address: '0x0A4E1BdFA75292A98C15870AeF24bd94BFFe0Bd4',
            symbol: 'FOTA',
            name: 'FOTA'
        }
    }
})
const res = await provider.request({
    type: 'wallet_watchAsset',
    params: {
        type: 'ERC20',
        options: {
            address: '0x0A4E1BdFA75292A98C15870AeF24bd94BFFe0Bd4',
            symbol: 'FOTA',
            name: 'FOTA'
        }
    }
})

sign

parameters

  • typedData - object:The object to be signed, refer to EIP-712

return value

  • signature - string[]:The result of the signature, including two items
js
const typedData = {
    domain: {
        name: "Starknet demo app",
        version: "1",
        chainId: 'SN_MAIN',
    },
    types: {
        StarkNetDomain: [
        { name: "name", type: "felt" },
        { name: "version", type: "felt" },
        { name: "chainId", type: "felt" },
        ],
        Message: [{ name: "message", type: "felt" }],
    },
    message: {
        message: 'sign message test',
    },
    primaryType: "Message",
};
const [r, s] = await provider.signMessage(typedData);
const typedData = {
    domain: {
        name: "Starknet demo app",
        version: "1",
        chainId: 'SN_MAIN',
    },
    types: {
        StarkNetDomain: [
        { name: "name", type: "felt" },
        { name: "version", type: "felt" },
        { name: "chainId", type: "felt" },
        ],
        Message: [{ name: "message", type: "felt" }],
    },
    message: {
        message: 'sign message test',
    },
    primaryType: "Message",
};
const [r, s] = await provider.signMessage(typedData);

Contract call

Perform one or more calls. If there is only one call, transactions is an object with the properties described below. If there are multiple calls, an array of objects.

parameters

  • transactions - object:
    • contractAddress - string:The address of the contract
    • entrypoint - string:The entry point of the contract
    • calldata - array:call data
    • signature - array:signature
  • abi - The contract’s abi, optional

return value

  • result - object
    • transaction_hash - string:Transaction hash
js
// Take StarkGate: ETH Token as an example
const transactions = {
    contractAddress: '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7',
    entrypoint: 'transfer',
    calldata: CallData.compile({
        recipient: '0x05b98d6ccbb660c3ca3c699f8dc0d2c8f58c539feac4fe2d57def7d2fa7312d1',
        amount: cairo.uint256(1000000000n)
    })
}
// "transactions": {
//     "contractAddress": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
//     "entrypoint": "transfer",
//     "calldata": [
//         "2589407029262891725723779965976037245771646239489440786683818579381309346513",
//         "1000000000",
//         "0"
//     ]
// },
const res = await provider.execute(transactions);
// Take StarkGate: ETH Token as an example
const transactions = {
    contractAddress: '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7',
    entrypoint: 'transfer',
    calldata: CallData.compile({
        recipient: '0x05b98d6ccbb660c3ca3c699f8dc0d2c8f58c539feac4fe2d57def7d2fa7312d1',
        amount: cairo.uint256(1000000000n)
    })
}
// "transactions": {
//     "contractAddress": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
//     "entrypoint": "transfer",
//     "calldata": [
//         "2589407029262891725723779965976037245771646239489440786683818579381309346513",
//         "1000000000",
//         "0"
//     ]
// },
const res = await provider.execute(transactions);

For other properties and methods on starknet.account and starknet.provider, see starknet.js