JavaScript access to the blockchain

Raw Messages

Using Flowee server APIs directly

The methods and services that FloweeJS provides are all built around what the Flowee servers provide. Services like the Blockchain service are implemented in JavaScript using the raw messages we describe on this page.

People that want to understand this structure and how to talk to a Flowee server directly have found the right place.

Networking messages

The basics of the APIs that are provided by the Flowee servers are documented here. The basic concept is that we send messages and we get replies for those messages. What we then need is a way to understand and built messages.

The message code is as simple as:

/**
 * Flowee Message Packet
 * @param {object} header Header Object containing enumerated values
 * @param {object} body Body Object containing enumerated values
 * @example
 * let m = new Message(1, 2);
 * m.body[7] = 1;
 */
class Message {
  constructor(serviceId, messageId) {
    this.header = {};
    if (typeof serviceId === 'number')
        this.header["serviceId"] = serviceId;
    if (typeof messageId === 'number')
        this.header["messageId"] = messageId;
    this.body = {};
  }
}

To see this in action take a look at this simple example:

/**
 * Get Blockchain Information
 * @example
 * let blockChainInfo = await flowee.getBlockChainInfo();
 */
async getBlockChainInfo() {
  let reply = await this.network.sendMessage(new Message(1, 0));
  return {
    difficulty: reply[64],
    medianTime: reply[65],
    chainWork: reply["66str"],
    chain: reply[67],
    blocks: reply[68],
    headers: reply[69],
    bestBlockHash: reply[70],
    bestBlockId: reply["70str"],
  }
}

service-id / message-id

Messages are like commands, or requests, but since there are so many we have to send them to a specific service and have a specific command. These are encoded by service-id for which service want to use and message-id to indicate which question or command we send.

These are numbers (for speed) and these numbers can be found on the API docs page. Our example used service-id = 1 and message-id = 0 which the docs show means BlockChain Service and message-id is GetBlockchainInfo

The reply lists various fields, also by number, and the above example shows clearly how they are fetched from our reply object.

Decoding help

The GetBlockchainInfo example sends an empty message and parses a reply. The actual reply object, when printed, has a lot of duplicate data in order to make decoding the data a lot easier.

{
  '64': 186732424053.789,
  '65': 1616692810,
  '66': Uint8Array(32) [
    245, 89, 237, 165, 103, 169, 221, 32, 99, 124, 98,   1,   0,   0,   0,   0,  0,  0,
      0,  0,   0,   0,   0,   0,   0,  0,  0, 0,  0,   0,   0,   0
  ],
  '67': 'main',
  '68': 680313,
  '69': 680313,
  '70': Uint8Array(32) [
    235,  32, 187, 56,  62, 221, 128,  8, 51, 97, 132, 134,  5,   6,  75,  20, 67, 23,
    207, 121, 174, 27, 249,   2,   0,  0,  0, 0,   0,   0,  0,   0
  ],
  '71': 0.9999950700659075,
  serviceId: 1,
  messageId: 1,
  header: {},
  '70str': '000000000000000002f91bae79cf1743144b0605868461330880dd3e38bb20eb',
  '66str': '000000000000000000000000000000000000000001627c6320dda967a5ed59f5',
  body: [
    { key: '67', value: 'main' },
    { key: '68', value: 680313 },
    { key: '69', value: 680313 },
    {
      key: '70',
      value: [Uint8Array],
      string: '000000000000000002f91bae79cf1743144b0605868461330880dd3e38bb20eb'
    },
    { key: '64', value: 186732424053.789 },
    { key: '65', value: 1616692810 },
    { key: '71', value: 0.9999950700659075 },
    {
      key: '66',
      value: [Uint8Array],
      string: '000000000000000000000000000000000000000001627c6320dda967a5ed59f5'
    }
  ]
}

We help you in this way:

  • The most accurate data is on the body object, which is a list of key/value pairs. We also place those on the reply object itself to allow lookup by id directly.
    Be aware that replies that return lists of things (like transactions) will repeat keys, then you will not be able to use the value on the keys but must use the body list.
  • bytearrays like the sha256 (used for most transaction-id and blockId etc) get a nice string version added. So 70 (BestBlockHash) gets a 70str which is a user-printable string for that hash.

Creating messages

In almost all cases you will limit yourself to adding items on the messsage.body object and you can do this in several ways.

  1. message.body[7] = 110;
    This style is the simplest. We create a key/value pair where the key is 7 and the value is 110.
  2. message.body[4] = Buffer.from(myTxid, 'hex').reverse();
    This style is effectively the same as the 1st, but it is worth mentioning because this is the most common way to convert a hash.
  3. message.body[45] = ["first", "second"];
    This allows the request we send to the server to have multiple items with the same key.