# Oracle Binary Encoding (OBI)
Oracle Binary Encoding (OBI) is the standard way to serialized and deserialize binary data in the BandChain ecosystem. Similar to Ethereum's Contract ABI Specification or Google's ProtoBuf, an OBI schema explains how a data object, in any supported programming language, can be encoded to and decoded from plain bytes.
OBI is designed with the following properties in mind:
- Compactness: OBI schema will be stored on-chain and passed around between blockchains. Thus, it is important to keep the size of the schema specification as small as possible.
- Simplicity & Portability: As a blockchain-agnostic protocol, OBI serialization and deserialization must be easy to implement in any environment. Consequently, complex platform-specific features are not supported.
- Readability: Lastly, OBI is intended to be used as a communication tool between oracle script creators and smart contract developers. It must be intuitive for readers to understand the OBI underlying objects from reading the schema.
An OBI schema is a non self-describing binary serialization format of multiple objects. Some particular notes:
- An OBI schema consists of one or more individual schemas. In most cases, an OBI schema will consist of two individual schemas: the input type and the output type.
- 6 sizes (8-bit, 16-bit, 32-bit, 64-bit, 128-bit, and 256-bit) of signed and unsigned integers are supported. There are all serialized into big-endian bytes.
- Strings, bytes, vectors are serialized with their length as u32 first, followed by their contents.
- Structs are serialized field by field in the declaration order.
# Backus–Naur Form (BNF) Grammar Specification
Below is the Backus–Naur form (BNF) grammar of an OBI schema.
# Pseudocode Implementation
Below is a example pseudocode implementation of OBI schema declaration and the corresponding serializing function in a somewhat broken function language 😛. Deserialization function is essentially the inverse of the serialization function.
# OBI Schema Examples
As an example, below is an example OBI schema of an oracle script to fetch the price of a cryptocurreny, which is then multiplied by a certain multiplier. The OBI itself schema consists of two inner schemas, one for the inputs to the oracle script and the other for the output.
- The input is a struct with 2 fields: a string symbol and a u64 multiplier.
- The output is a struct with 2 fields: a u64 final price and a vector of struct each has string name and u64 timestamp.
# Example Object Serialization
# Reference Implementations
OBI serialization libraries are being actively developed in multiple programming languages. Head over to BandChain's OBI module to see all currently available implementation.