Giter Club home page Giter Club logo

example-eosjs-decode-hex's Introduction

dfuse Decode Hex Data (using eosjs)

This quick repository showcase how to decode hexadecimal string data into a JSON structure.

To accomplish that, we fetch the right ABI type from a particular contract, one that encoded the given data. From the ABI, we extract the type we want to deserialize.

We then use eosjs to deserialize the hexadecimal data into a JSON string value directly.

To run the example, clone this repository, then code at the root of the project then:

yarn install
yarn run ts-node index.ts

Code

Sample code recopied here as a quick reference.

import { Api, JsonRpc } from 'eosjs';
import fetch from 'node-fetch'
import { JsSignatureProvider } from 'eosjs/dist/eosjs-jssig';
import { getTypesFromAbi, createInitialTypes, hexToUint8Array, SerialBuffer } from 'eosjs/dist/eosjs-serialize';
import { TextEncoder, TextDecoder } from 'util';

async function main(): Promise<void> {
    const signatureProvider = new JsSignatureProvider([]);
    const rpc = new JsonRpc('https://mainnet.eos.dfuse.io', { fetch: fetch as any });
    const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder() as any, textEncoder: new TextEncoder() });

    const abi = await api.getAbi("eosio.token")

    const builtinTypes = createInitialTypes()
    const types = getTypesFromAbi(builtinTypes, abi)

    const hexData = "000090e602ea30550000000000ea3055a08601000000000004454f530000000005656f736a73"
    const data = hexToUint8Array(hexData);

    const buffer = new SerialBuffer({ textDecoder: new TextDecoder() as any, textEncoder: new TextEncoder() });
    buffer.pushArray(data);

    // You would use the struct representing the table row in your own code
    const transferType = types.get("transfer")
    if (transferType === undefined) {
        console.log("Type 'transfer' does not exist on 'eosio.token' ABI")
        return
    }

    const transfer = transferType.deserialize(buffer);

    console.log("Deserialized hex data")
    console.log(` From: ${transfer.from}`)
    console.log(` To: ${transfer.to}`)
    console.log(` Quantity: ${transfer.quantity}`)
    console.log(` Memo: ${transfer.memo}`)
    console.log()
}

main().then(() => {
   process.exit(0)
}).catch((error) => {
    console.log("An error occurred", error)
    process.exit(1)
})

example-eosjs-decode-hex's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.