Binance chain wallet api

Содержание
  1. Command Line Interface (CLI)
  2. Where can I download the Binance Chain CLI?
  3. CLI installation
  4. Where to connect
  5. Which Chain-ID to use
  6. Which Private Key To Use
  7. Key manager
  8. Local key
  9. Ledger key
  10. How to use
  11. CLI Reference
  12. Use CLI for Different Blockchains
  13. Binance Chain Documentation
  14. What are Binance Chain and Binance DEX?
  15. What is Binance Smart Chain?
  16. What can I do with Binance Chain?
  17. What can I do with Binance Smart Chain?
  18. Comparision Between BC and BSC
  19. BNB (and Other Coins) MainNet Switch
  20. BNB Staking
  21. Trading on Binance DEX
  22. Technology Details
  23. Get Started with Binance Chain
  24. Wallet
  25. Chain Explorer
  26. REST API
  27. Node RPC
  28. Advanced Ways To Use Blockchain
  29. Run your own full node
  30. Run your own light client
  31. Access via Node Command Line Interface (CLI)
  32. Use SDKs
  33. Blockchain Details
  34. Developer
  35. Development Progress
  36. Difference with MetaMask
  37. Inpage injected object
  38. BinanceChain.request(
  39. BinanceChain.request()
  40. Upcoming Breaking Changes
  41. Basic Usage
  42. Chain IDs
  43. Properties
  44. BinanceChain.chainId
  45. BinanceChain.autoRefreshOnNetworkChange
  46. Methods
  47. BinanceChain.isConnected()
  48. BinanceChain.request(args)
  49. Example
  50. BinanceChain.bnbSign(address: string, message: string): Promise
  51. BinanceChain.switchNetwork(networkId: string): Promise
  52. BinanceChain.requestAccounts()
  53. BinanceChain.transfer()>
  54. Events
  55. connect
  56. disconnect
  57. accountsChanged
  58. chainChanged
  59. message
  60. Errors
  61. Using the Provider
  62. Legacy API
  63. Legacy Properties
  64. BinanceChain.networkVersion (DEPRECATED)
  65. BinanceChain.selectedAddress (DEPRECATED)
  66. Legacy Methods
  67. BinanceChain.enable() (DEPRECATED)
  68. BinanceChain.sendAsync() (DEPRECATED)
  69. BinanceChain.send() (DEPRECATED)
  70. Legacy Events
  71. close (DEPRECATED)
  72. chainIdChanged (DEPRECATED)
  73. networkChanged (DEPRECATED)
  74. notification (DEPRECATED)

Command Line Interface (CLI)

Binance Chain CLI is one of several ways to interact with Binance Chain.

Binance Chain CLI can be used as a local wallet, you can manage your keys via Binance CLI. You can add a new key or restore your key from mnemonic words. And you can list your keys and show specified key info.

With Binance Chain CLI, you can send transactions to Binance Chain, like placing an order, transferring token, issuing token and so on. Actually you can do almost everything you can do with Binance Chain web wallet. What is more, you can propose to list trading pairs and join chain governance.

In addition, you can do some simple queries through CLI. For example, you can query your account’s balance, transaction detail by transaction hash and etc.

Where can I download the Binance Chain CLI?

You can download Binance Chain CLI releases here: https://github.com/binance-chain/node-binary

CLI installation

Once you have downloaded the binary for your platform, you may drop it into any folder you like. Open a terminal window or cmd.exe in that folder, and then follow the examples on the documentation site.

On Windows you would most likely use this instead:

On Windows you would most likely use this instead:

Where to connect

You can manage you keys locally without connecting to any node. But if you want to interact with Binance Chain, you need to connect to one of Binance Chain full nodes.

You can run your own full node, which may automatically connect to Binance Chain, and you can run your CLI there with your own full node. In the mean time, you can connect to any full nodes provided by other people.

You could query this API for for a list of full node on mainnet: https://dex.binance.org/api/v1/peers.

Full nodes will be denoted as the format of ip:port , you could use access_addr as your peer to get connected, such as https://dataseed4.defibit.io:443

Please note that there are two types of nodes that offer RPC services. Some support TLS and others don’t.

Which Chain-ID to use

chain-id is an important field to fill for every transaction sent from bnbcli . The chain-id for mainnet is Binance-Chain-Tigris , and is Binance-Chain-Ganges for testnet. Please choose the correct one in your case.

Which Private Key To Use

There are two ways for you to sign transaction:

  • Use Local Keystore File

This is the default way to sign transactions with bnbcli . It will use the encrypted keystore files saved at bnbcli home. You need to specify which key to decrypt with —from .

  • Use Private Key in Hardware Wallet

First, you must add —ledger when creating a new address. For example,

You could also specify —account and —index to generate more addresses. After the generation, you could use Ledger for signing.

When you use the address to sign transactions, bnbcli will send transactions to Ledger and get signatures. Then bnbcli build the signed transactions and boardcast them to full nodes.

Key manager

Here we support two types of key: local key and leder key

Local key

Ledger key

Before creating a new ledger key, make sure you have performed these steps: 1. Ledger device is installed binance ledger app and the version should be later or equal to v1.1.3. 2. Connect your ledger device to your machine and input pin code to unlock it. 3. Open the binance ledger app on your ledger device.

  • Sign transaction with a ledger key

Taking transfer transaction for example, please follow these steps: 1. Execute command in your console:

How to use

When you have downloaded Binance Chain CLI, you can use help subcommand to see all the available commands:

Note:there is one special flag —trust-node of most subcommands, if not enabled which is by default the CLI will take an extra 2-4 seconds to verify blockchain proof at current height. You can enable that flag if the peer is trustful so that most commands will accomplish in 500 milliseconds. If your node cannot prove the transaction, there will be the following notice:

CLI Reference

For detailed usage, you can refer to:

Use CLI for Different Blockchains

bnbcli will save data about validatorset changes at home of bnbcli . If you want to use bnbcli for different blockchains, for example, you want to change from testnet to mainnet, the data will be stale. In order to switch between blockchains, you need clean the data folder with rm -rf

/.bnbcli/.bnblite/ or create a new home folder for bnbcli with —home flag. If you forget to specify a different home folder path, then you will not be able to make queries with bnbcli .

Источник

Binance Chain Documentation

Welcome to the Binance Chain documentation site!

Please note that both the Binance Chain software and this documentation site will improve over time and is still a work-in-progress.
Be sure to engage with our community channels to stay updated.

Have fun trading and see you on chain!

What are Binance Chain and Binance DEX?

Binance Chain is a blockchain software system developed by Binance and its community. Binance DEX refers to the decentralized exchange features developed on top of Binance Chain.

Please read the FAQ to get started.

What is Binance Smart Chain?

Binance Smart Chain is an innovative solution to bring programmability and interoperability to Binance Chain. Binance Smart Chain relies on a system of 21 validators with Proof of Staked Authority (PoSA) consensus that can support short block time and lower fees. The most bonded validator candidates of staking will become validators and produce blocks. The double-sign detection and other slashing logic guarantee security, stability, and chain finality.

Please read the FAQ to get started.

What can I do with Binance Chain?

The purpose of the new blockchain and DEX is to create an alternative marketplace for issuing and exchanging digital assets in a decentralized manner.

For traders, you can:

  • Propose to create trading pairs between two different tokens
  • Send orders to buy or sell assets through trading pairs created on the chain withSDK
  • Watch the DEX market to confirm price and market activity of certain assets

For developers, you can also:

  • Explore the transaction history and blocks on the chain, via Binance Chain Explore, API and node RPC interfaces.
  • Run a full node to listen to and broadcast live updates on transactions, blocks, and consensus activities
  • Extract other data of Binance Chain via full node or APIs
  • Develop tools and application to help users use Binance Chain and Binance DEX

What can I do with Binance Smart Chain?

Binance Smart Chain (BSC) is best described as a blockchain that runs in parallel to the Binance Chain. Unlike Binance Chain, BSC boasts smart contract functionality and compatibility with the Ethereum Virtual Machine (EVM). The design goal here was to leave the high throughput of Binance Chain intact while introducing smart contracts into its ecosystem.

Because BSC is EVM-compatible, it launched with support for the rich universe of Ethereum tools and DApps. In theory, this makes it easy for developers to port their projects over from Ethereum. For users, it means that applications like MetaMask can be easily configured to work with BSC. Seriously – it’s just a matter of tweaking a couple of settings. Check out Use MetaMask for Binance Smart Chain to get started.

  • Send and receive BNB and other BEP2 tokens cross-chain
  • Explore the transaction history and blocks on the chain, via bscscan, API and node RPC interfaces.
  • Stake you BNB to earn some block rewards

Developers can also:

  • Issue new tokens to digitalize assets
  • Migrate existing DApps
  • Run a full node to listen to and broadcast live updates on transactions, blocks, and consensus activities
  • Become a validator of BSC
  • Develop wallets and tools to help users use Dapps

Comparision Between BC and BSC

Binance Chain Binance Smart Chain
Consensus DPoS PoSA
No. of Validators 11 up to 21
Mean Block Time getOwner and decimals . Read the full proposal here: https://github.com/binance-chain/BEPs/blob/master/BEP20.md

BNB (and Other Coins) MainNet Switch

Binance Coin (BNB) was an ERC20 token on the Ethereum network.
After the launch of Binance Chain, Binance Coin (BNB) is being converted into native BNB tokens on the main network via the exchange platform at binance.com, a pragmatic and efficient way to perform the initial token swap.

Binance Chain is ready for other projects to migrate their tokens to take advantage of performant transactions with more liquidity options and native marketplace features.
More information about how projects can set themselves up for this (via binance.com or partners) will come soon.

BNB Staking

Please read the guide here

Trading on Binance DEX

Binance DEX is the native marketplace on Binance Chain, allowing you to exchange digital assets issued and listed on it.
The matching happens within the blockchain nodes and all of the transactions are recorded on-chain, therefore forming a complete, auditable ledger of activity.

ATTENTION: The match logic on DEX is quite different from normal centralized exchange.
Please go over the trading and match spec below to get the best interests for your orders.

Technology Details

Continue reading below if you are interested in what is happening under the hood!

  • Binance Chain as a Block Chain: about consensus, software stack, network layout and roles.
  • Connect to Binance Chain: how to connect to Binance Chain and DEX via different ways.
  • Run a Binance Chain full node: how to run a full node and become part of the p2p network of Binance Chain.
  • Run a Binance Chain light client: how to run a light client.
  • Binance Chain Governance: about proposal, vote, and join as a validator.

Источник

Get Started with Binance Chain

Wallet

The easiest way to use Binance Chain is through a wallet with support for Binance Chain accounts and transfers.

For example, Binance provides a Web Wallet at https://www.binance.org
Binance also provides Web Wallet for testnet at https://testnet.binance.org
Both provide the functions below:

  • Generating crypto keys and addresses, which serves as the base of a wallet
  • Showing the balances of assets on the addresses
  • Sending and receiving assets

Binance Chain Web Wallet also presents a trading UI similar to Binance.com, where you can examine market data and manage your orders to trade among the listed assets.
Learn about the list of wallet available here.

Chain Explorer

Chain Explorer provides a portal to explore blocks and transaction details.
On Binance Chain Explorer, you can also check different asset types, the distribution of their ownerships, and owners’ transactions.

REST API

There are Accelerated Nodes which provide advanced API services for the public.
Here is a list of all the Rest API information Accelerated Node provides: paths

Node RPC

There are data seed nodes in the network which allow users to perform low-level operations like executing ABCI queries, viewing network/consensus state or broadcasting a transaction. If you run a full node by yourself, you can also use those RPC functions. Here is a list of all the Node RPC services it provides: node-rpc

Advanced Ways To Use Blockchain

Run your own full node

Please refer to this guide about how to run your own node.

Run your own light client

Please refer to this guide about how to run your own light client.

Access via Node Command Line Interface (CLI)

A Command Line Interface is available for Linux and Mac platforms.
Please refer to the CLI Reference.

Use SDKs

SDKs are also provided as a starting point for your apps.
There are two advanced SDK solutions for Binance chain: Java and Golang.
Both solutions provide functions for:

  • Create wallets and manage keys
  • Encode/sign transactions and submit to Binance Chain/DEX, including Transfer, New Order, Cancel Order, etc.
  • Communicate with Binance Chain/DEX Node RPC calls through public node RPC services or your own private full nodes

Please refer to specific SDK documentation for more information:

Blockchain Details

Please check the technical details for more technical information.

Источник

Developer

Binance Chain Wallet injects a global API into websites visited by its users at window.BinanceChain .

This API specification borrows heavily from API MetaMask provided, considering the massive adoption. So Web3 site developers can easily connect their dApps with the Binance Chain Wallet. The APIs allow websites to request users’ Binance Smart Chain addresses, read data from the blockchain the user is connected to, and prompt the users to sign messages and transactions.

The presence of the provider object window.BinanceChain indicates a Binance Chain/Binance Smart Chain user.

The API this extension wallet provides includes API specified by EIP-1193 and API defined by MetaMask (including some massively relied legacy ones).

Development Progress

Currently (version 1.112.8) as Binance Chain Wallet natively supports Binance Chain, we are planning to open a series of APIs for dApp developers to interact with Binance Chain. At the end of the day, most APIs available in Binance Chain javascript sdk would be available.

Currently, only the following is supported: * transfer

Difference with MetaMask

Please read through this section if you are a web3 developer who has integrated with MetaMask and interested in integrating with Binance Chain Wallet.

Inpage injected object

The biggest difference between Binance Chain Wallet and MetaMask is we inject BinanceChain rather than ethereum (or web3 ) to the web page. So user could keep two extensions at the same time.

BinanceChain.request(

We haven’t supported the full complex signing data APIs MetaMask provided, while we only provide standard eth_sign JSON-RPC call.

The message item in params for this request on MetaMask has to be hex-encoded keccak256 hash (otherwise the API would silent failure for dapp https://www.reddit.com/r/Metamask/comments/9wp7kj/eth_sign_not_working/). In contrast, web3 developers could pass any message in plaintext to this method, and our UI would render it as it is to the wallet users.

BinanceChain.request()

When this API is invoked without the user’s approval, MetaMask would return an empty array.

In contrast, we would ask the user to unlock his wallet and return the address user connected to.

Upcoming Breaking Changes

On November 16, 2020, MetaMask is making changes to their provider API that will be breaking for some web3 sites. These changes are upcoming, but you can prepare for them today. Follow this GitHub issue for updates.

In this implementation, some APIs will be supported as Legacy API (For example we will still implement the chainIdChanged on BinanceChain object until MetaMask formally deprecates it).

Basic Usage

For any non-trivial Binance Smart Chain web application — a.k.a. web3 site — to work, you will have to:

  1. Detect the Binance Smart Chain provider ( window.BinanceChain )
  2. Detect which Binance Smart Chain network the user is connected to
  3. Get the user’s Binance Smart Chain account(s)

You can learn how to accomplish the 2 and 3 from above list by reviewing the snippet in the Using the Provider section.

The provider API is all you need to create a full-featured web3 application.

That said, many developers use a convenience library, such as ethers and web3.js, instead of using the provider directly. If you need higher-level abstractions than those provided by this API, we recommend that you use a convenience library.

Today, many dApps are built on top of the higher-level API provided by web3-react or use-wallet (which builds on top of web3-react).

We made a tiny lib bsc-connector that implements the AbstractConnector interface of web3-react library. You can add this to your project in parallel with injected-connector by: yarn add @binance-chain/bsc-connector or npm i @binance-chain/bsc-connector .

There is an example in use-wallet origin repo shows how to ‘inject’ a customized web3-react connector to UseWalletProvider :

Chain IDs

At the moment, the BinanceChain.chainId property and the chainChanged event should be preferred over the eth_chainId RPC method.

Their chain ID values are correctly formatted, per the table below.

eth_chainId returns an incorrectly formatted (0-padded) chain ID for the chains in the table below, e.g. 0x01 instead of 0x1 . See the Upcoming Breaking Changes section for details on when the eth_chainId RPC method will be fixed.

Custom RPC endpoints are not affected; they always return the chain ID specified by the user.

These are the IDs of the Binance Smart chains that Binance Chain Wallet supports by default.

Hex Decimal Network
0x38 56 Binance Smart Chain Main Network (bsc-mainnet)
0x61 97 Binance Smart Chain Test Network (bsc-testnet)

This API can also return chain ids of Binance Chains if users switch to them. The possible return value would be: | Chain Id | Network | | ——————— | —————————————- | | Binance-Chain-Tigris | Binance Chain Main Network (bbc-mainnet) | | Binance-Chain-Ganges | Binance Chain Test Network (bbc-testnet) |

Properties

BinanceChain.chainId

The value of this property can change at any time, and should not be exclusively relied upon. See the chainChanged event for details.

NOTE: See the Chain IDs section for important information about the Binance Chain Wallet provider’s chain IDs.

A hexadecimal string representing the current chain ID.

BinanceChain.autoRefreshOnNetworkChange

As the consumer of this API, it is your responsibility to handle chain changes using the chainChanged event. We recommend reloading the page on chainChange unless you have a good reason not to.

Please refer to MetaMask Doc, the only difference is we injected a different object.

To disable this behavior, set this property to false immediately after detecting the provider:

Methods

BinanceChain.isConnected()

Please refer to MetaMask Doc, the only difference is we injected a different object.

BinanceChain.request(args)

Please refer to MetaMask Doc, the only difference is we injected a different object.

We use this method to wrap an RPC API, Please see the Ethereum wiki.

Important methods from this API include:

Example

The code snippet below is as same as MetaMask’s example, the only difference is we injected a different object.

BinanceChain.bnbSign(address: string, message: string): Promise

We prepared an example for this API, please check out: https://github.com/binance-chain/js-eth-personal-sign-examples for more detail

Like eth_sign enable dapp to verify a user has control over an ethereum address by signing an arbitrary message. We provide this method for dapp developers to request the signature of arbitrary messages for Binance Chain (BC) and Binance Smart Chain (BSC).

If address parameter is a binance chain address (start with bnb or tbnb ), we will simply calculate sha256 hash of the message and let user sign the hash with his binance chain address’ private key. Note: Binance Chain uses the same elliptic curve ( secp256k1 ) as Ethereum.

If address parameter is a binance smart chain address (start with 0x ), the message would be hashed in the same way with eth_sign .

The returned publicKey would be a compressed encoded format (a hex string encoded 33 bytes starting with «0x02», «0x03») for Binance Chain. And uncompressed encoded format (a hex string encoded 65 bytes starting with «0x04»).

The returned signature would be bytes encoded in hex string starting with 0x . For BinanceChain, its r,s catenated 64 bytes in total. For Binance Smart Chain, like eth_sign , its r, s catenated 64 bytes and a recover byte in the end.

DApp developers should verify whether the returned public key can be converted into the address user claimed in addition to an ECDSA signature verification. Because any plugin can inject the same object BinanceChain as Binance Chain Wallet.

BinanceChain.switchNetwork(networkId: string): Promise

As Binance Chain Wallet natively supports Binance Chain and Binance Smart Chain which are heterogeneous blockchains run in parallel. APIs would be different in any situation. (We would open APIs for Binance Chain very soon).

Developers could judge which network is selected by user currently via BinanceChain.chainId or listening to the chainChanged event via BinanceChain.on(‘chainChanged’, callback) .

To request for network switching, developers could invoke this API with bbc-mainnet (Binance Chain Main Network), bsc-mainnet (Binance Smart Chain Main Network), bbc-testnet (Binance Chain Test Network), bsc-testnet (Binance Smart Chain Test Network) to prompt user to agree on network switching.

BinanceChain.requestAccounts()

Request all accounts maintained by this extension.

The id in response would be used as accountId for the APIs for Binance Chain.

This method would return an array of Account:

BinanceChain.transfer()>

Transfer certain amount of asset (BNB or BEP2) on Binance Chain.

accountId could be retrieved from the BinanceChain.requestAccounts API ( id field of each account)

networkId could be bbc-mainnet or bbc-testnet

Events

Please refer to MetaMask Doc, the only difference is we injected a different object.

connect

Please refer to MetaMask Doc, the only difference is we injected a different object.

disconnect

Please refer to MetaMask Doc, the only difference is we injected a different object.

accountsChanged

Please refer to MetaMask Doc, the only difference is we injected a different object.

chainChanged

Please refer to MetaMask Doc, the only difference is we injected a different object.

message

Please refer to MetaMask Doc, the only difference is we injected a different object.

Errors

Please refer to MetaMask Doc, the only difference is we injected a different object.

Using the Provider

This snippet explains how to accomplish the three most common requirements for web3 sites:

  • Detect which BinanceChain network the user is connected to
  • Get the user’s BinanceChain account(s)

Legacy API

You should never rely on any of these methods, properties, or events in practice.

This section documents MetaMask’s legacy provider API.

To be compatible with existing dApps that support MetaMask, Binance Chain Wallet implements them as well, but please don’t rely on them. We may deprecate them soon in the future.

Legacy Properties

BinanceChain.networkVersion (DEPRECATED)

Please refer to MetaMask Doc, the only difference is we injected a different object.

BinanceChain.selectedAddress (DEPRECATED)

Please refer to MetaMask Doc, the only difference is we injected a different object.

Legacy Methods

BinanceChain.enable() (DEPRECATED)

Please refer to MetaMask Doc, the only difference is we injected a different object.

BinanceChain.sendAsync() (DEPRECATED)

Please refer to MetaMask Doc, the only difference is we injected a different object.

BinanceChain.send() (DEPRECATED)

Please refer to MetaMask Doc, the only difference is we injected a different object.

This method behaves unpredictably and should be avoided at all costs. It is essentially an overloaded version of BinanceChain.sendAsync() .

BinanceChain.send() can be called in three different ways:

You can think of these signatures as follows:

This signature is exactly like BinanceChain.sendAsync()

This signature is like an async BinanceChain.sendAsync() with method and params as arguments, instead of a JSON-RPC payload and callback

This signature enables you to call the following RPC methods synchronously:

  • eth_coinbase
  • eth_uninstallFilter
  • net_version
  • Legacy Events

    close (DEPRECATED)

    Please refer to MetaMask Doc, the only difference is we injected a different object.

    chainIdChanged (DEPRECATED)

    Please refer to MetaMask Doc, the only difference is we injected a different object.

    networkChanged (DEPRECATED)

    Please refer to MetaMask Doc, the only difference is we injected a different object.

    notification (DEPRECATED)

    Please refer to MetaMask Doc, the only difference is we injected a different object.

    Источник

    Читайте также:  Лучшие сервисы для инвестиций

    Оцените статью