Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@cityofzion/neo3-parser

Package Overview
Dependencies
Maintainers
6
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cityofzion/neo3-parser

latest
Source
npmnpm
Version
1.8.1
Version published
Maintainers
6
Created
Source

Neo3-parser - A declarative SmartContract Parsing Spec.
Made with ❤ by COZ.IO

Neo3-parser

Neo3-parser is a specification of how SmartContract client SDKs can interact with different parsing libraries such as Neon-JS.

Visit the main page of the project.

How to parse responses

After invoking a contract you'll get the results on a stack. Use the parseRpcResponse(field: RpcResponse, parseConfig?: ParseConfig) function on each result of the stack to get the results parsed.

How to use parseConfig

The parseConfig object has the following properties:

PropertyDescription
type: stringa valid ABI Type
hint?: stringa type that extends from the ABI type
generic?: ParseConfigyou only need to pass this prop if type is "Array"
genericKey?: ParseConfigyou only need to pass this prop if type is "Map"
genericItem?: ParseConfigyou only need to pass this prop if type is "Map"
union?: ParseConfig[]you only need to pass this prop if type is "Any" and you expect to get multiple types

Note: check HINT_TYPES to see what types are available to use on hint.

Example

// Simulating a result stack after invoking a contract
const stackResult = [ 
  {
    type: "ByteString",
    value: "AAECAwQFBgcICQoLDA0ODxAREhM=" // This value is a Hash160
  } 
]

// Call parseRpcResponse on an item of the stack
const response = Neo3Parser.parseRpcResponse(stackResult[0])

console.log(response) 
// Expected output: "☺☻♥♦♣♠\n♫☼►◄↕‼"

// You can use the `parseConfig` parameter to change how to parse the response
const responseHash160 = Neo3Parser.parseRpcResponse(stackResult[0], { type: "Hash160"})

console.log(responseHash160)
// Expected output: "0x131211100f0e0d0c0b0a09080706050403020100"

// Adding a hint might also change how it is parsed
const responseHash160LE = Neo3Parser.parseRpcResponse(stackResult[0], { type: "Hash160", hint: "ScriptHashLittleEndian"})

console.log(responseHash160LE)
// Expected output: "000102030405060708090a0b0c0d0e0f10111213"

Using neo3-boa to get the parseConfig

If you compiled your smart contract with Neo3-boa you can use the ABI inside the .manifest.json file as the parseConfig.

For example, compiling this smart contract will generate the following file:

{
    "name": "ManifestTypeHintFromUInt160ToScriptHashLittleEndian",
    "groups": [],
    "abi": {
        "methods": [
            {
                "name": "Main",
                "offset": 0,
                "parameters": [],
                "safe": false,
                "returntype": "Hash160",
                "returnhint": "ScriptHashLittleEndian"
            }
        ],
        "events": []
    },
    "permissions": [
        {
            "contract": "*",
            "methods": "*"
        }
    ],
    "trusts": [],
    "features": {},
    "supportedstandards": [],
    "extra": null
}

Then, you can copy the properties of the method you want to parse that have the return prefix, in this example it should be returntype and returnhint. Remove the return prefix and attribute it to a ParseConfig variable in your TypeScript file.

const parseConfigFromNeo3boa = {
  "type": "Hash160",
  "hint": "ScriptHashLittleEndian"
}

const responseHash160LE = Neo3Parser.parseRpcResponse(stackResult[0], parseConfigFromNeo3boa)

console.log(responseHash160LE)
// Expected output: "000102030405060708090a0b0c0d0e0f10111213"

FAQs

Package last updated on 10 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts