Skip to content

StarkNet

在 Bitget Wallet App 及安装 Chrome Extension 的 Chrome 浏览器中运行 DApp 时,可获得全局对象 window.bitkeep.starkstarknet_bitkeepnet 并进行后续的 API 调用。

js
const provider = window.starknet_bitkeep; //推荐使用该方法
const provider = window.starknet;
const provider = window.starknet_bitkeep; //推荐使用该方法
const provider = window.starknet;

注入对象的属性和方法

  • name - string:钱包名称,值为'Bitget Wallet'.
  • icon - string:钱包的图标
  • version - string:版本号
  • chainId - string:仅支持主网,值为 SN_MAIN
  • isConnected - boolean::当前钱包是否已连接上
  • selectedAddress - string:用户当前选中的钱包地址
  • account - Account:访问账户对象,继承自 starknet.js的 Account,实例上的具体属性和方法,可参考 starknet.js 的文档
  • provider - Provider:访问 provider 对象,使用的是 starknet.js的 RpcProvider,实例上的具体属性和方法,可参考 starknet.js 的文档
  • enable - () => [string]:用于连接钱包,成功调用后,会唤起BitgetWallet连接钱包页面,用户可以决定是否连接当前 DApp,如果用户同意,将会返回选中地址的单项数组
  • on - (event, callback) => void:添加事件监听
    • accountsChanged 事件:当用户切换账户时会触发该事件,并返回新地址的数组;当断开连接时,会返回空数组。
  • off - (event, callback) => void:移除事件监听
  • disconnect - 断开钱包连接

连接到BitgetWallet

参数

  • options - object:可选的
    • starknetVersion - v4 | v5:默认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)...
});

添加代币

参数

  • type - string:wallet_watchAsset
  • params - object:Token信息
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'
        }
    }
})

签名

参数

  • typedData - object:要签名的对象,参考 EIP-712 标准

返回值

  • signature - string[]:签名的结果,包含两项
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);

合约调用

执行一个或多个调用。如果只有一个调用,则 transactions 就是一个对象,其包含的属性会在下面说明。如果有多个调用,则是一个对象的数组。

参数

  • transactions - object:
    • contractAddress - string:合约的地址
    • entrypoint - string:合约的入口点
    • calldata - array:调用数据
    • signature - array:签名
  • abi - 合约的 abi,可选的

返回值

  • result - object
    • transaction_hash - string:交易的 hash
js
// 以StarkGate: ETH Token为例
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);
// 以StarkGate: ETH Token为例
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);

starknet.account 和 starknet.provider 上的其他属性和方法,请查看 starknet.js 文档。