As stated in the introduction, eosjs
integrates with EOSIO-based blockchains using the EOSIO Nodeos RPC API.
In general, there are two objects that are used to interact with a blockchain via eosjs
: the JsonRpc
object, and the Api
object.
JsonRpc
The JsonRpc
object is typically used when signing is not necessary. Some examples include getting block information, getting transaction information, or getting table information.
The requests made by the JsonRpc
object will either use a built-in fetch
library, or the fetch
library passed in by the user to issue requests to the endpoint specified when instantiating the JsonRpc
object. When the various methods (get_abi, get_account, get_block_header_state, etc) of the JsonRpc
object are invoked, the calls are delegated to the JsonRpc
object's fetch function, which in turn, delegate the requests to the fetch
library.
Api
The Api
object is typically used when transacting on an EOSIO-based blockchain. Some examples include staking, creating an account, or proposing multi-sig transactions.
The typical use of the Api
object is to call its transact
method. This method performs a number of steps depending on the input passed to it:
- The
transact
method first checks if the chainId was set in theApi
constructor, and if not, uses theJsonRpc
object'sget_info
method to retrieve the chainId. -
The
transact
method then checks if theexpireSeconds
and eitherblocksBehind
oruseLastIrreversible
fields are set and well-formed in the optional configuration object, as specified in How to Submit a Transaction.- If so, either the last_irreversible_block_num or the block blocksBehind the head block retrieved from
JsonRpc
'sget_info
is set as the reference block and the transaction header is serialized using this reference block and theexpireSeconds
field.
- If so, either the last_irreversible_block_num or the block blocksBehind the head block retrieved from
- The
transact
method then checks if the appropriate TAPOS fields are present in the transaction (they can either be specified directly in the transaction or in the optional configuration object) and throws an Error if not. - The necessary
abi
s for a transaction are then retrieved for the case whentransact
is expected to sign the transaction. - The
actions
are serialized using theeosjs-serialize
ser
object. - The entire transaction is then serialized, also using the
eosjs-serialize
ser
object. - The transaction is then optionally signed, using the
signatureProvider
, the previously retrievedabi
s, the private keys of thesignatureProvider
, and thechainId
. - The transaction is then optionally compressed, using the
deflate
function of a Javascript zlib library. - The transaction is then optionally broadcasted using
JsonRpc
'spush_transaction
.