Get Started pages already show how to access Binance Chain and DEX via Wallet and Explorers. Here we would like to dive into some technology details for access in a programming way.
There are 3 ways to read and write data from Binance chain:
The Accelerated Node
infrastructure provide easy access via http REST API and WebSocket
push APIs. There are mulitple endpoints from different validator infrastructures. Please
check the Web API Reference
There are public data seed nodes that joins the Binance Chain network. They usually provide RPC calls. Please check the Node RPC Reference.
You can also run a full node by yourself, so that you will have a local server to send RPC requests and read Chain information off.
Essentially command line interfaces are just tools that wrap the incoming command line arguments and call RPCs. Please check the Command Line Referenace.
You can only write to Binance Chain via Transactions
. Both Web API and Node RPC provide
a broadcastTx
API to submit a signed and encoded
transaction onto the Binance Chain. The detailed process is outlined below:
The transaction message and related information will be packed into payload
, which is the so called Standard Transaction
.
The transaction body, memo, signature, etc. all fill in the Standard Transaction
, encode and then broadcast out together onto Binance Chain.
Encoding defines the way how transactions are serialized and transferred between clients and nodes, and different nodes themselves. here is a detailed specification on the transaction types and encoding logic.
Signature is the evidence to prove the sender owns the transaction. It will be created from the actions outlined below:
-
Compose a data structure. please note
msgs
,memo
,source
,data
are the same as in the abovepayload
.chain_id
: a string, unique ID for the Chain, it stays the same for most time, but may vary as Binance Chain evolves;account_number
: a string for a 64-bit integer, an identifier number associated with the signing addresssequence
: a string for a a 64-bit integer, please check the belowmemo
: a string, a short sentence of remark for the transactionmsgs
: a byte array, json encoded transaction messages, please check the encoding section.source
: a string for a 64 bits integer, which is an identifier for transaction incoming toolsdata
: byte array, reserved for future use
-
Encode the above data structure in json, with ordered key, Specifically:
- Maps have their keys sorted lexicographically
- Structs keys are marshalled in the order defined in the struct
-
Sign SHA256 of the encoded byte array, to create an ECDSA signature on curve Secp256k1 and serialize the
R
andS
result into a 64-byte array. (bothR
andS
are encoded into 32-byte big endian integers, and thenR
is put into the first 32 bytes andS
are put into the last 32 bytes of the byte array. In order to breakS
's malleability,S
set tocurve.Order() - S
ifS > curnve.Order()/2
.)
The signature
will be encoded together with transaction message and sent as payload
to Binance Chain node via RPC or http REST API, as described in the above section.
After Account
is created, besides the balances, Account
also contains:
- Account Number: an internal identifier for the account
- Sequence Number: an ever-changing integer.
The Sequence Number is the way how Binance Chain prevents Replay Attack
(the idea is borrowed from Cosmos
network, but varies a bit in handling). Every transaction should have a new Sequence Number
increased by
1 from the current latest sequence number of the Account
, and after this transaction is recorded on the
block chain, the Sequence Number
will be set to the same number as the one of latest transaction.
This logic forces the client to be aware of the current Sequence Number
, either by reading from the
blockchain via API, or keep the counting locally by themselves. The recommended way is to keep
counting locally and re-synchronize from the blockchain periodically.
SDK in different languages are provided to simplify use of APIs to access Binance Chain.