Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@aeternity/aepp-calldata

Package Overview
Dependencies
Maintainers
3
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aeternity/aepp-calldata

Aeternity data serialization library

  • 1.0.0-rc1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
391
decreased by-48.89%
Maintainers
3
Weekly downloads
 
Created
Source

Aeternity data serialization

Aeternity contract calldata encoding and results decoding standalone library.

This is Javascript imeplemtnation of data serialization specified in aeternity protocol.

While the only purpose of the library at the moment of this writing is solely to provide ecnoding and decoding respectively of contracts calldata and return data it may evolve to full fledged serialization library of the full protocol specification.

Installation

npm install -P @aeternity/aepp-calldata

Quick Start

There is single module Encoder that should be imported and instanciated. The constructor takes a single argument - Sophia ACI as string.

The encode method is used to encode calldata taking the contract name as first argument, then function name and list of contract call arguments as last argument.

The decode method is used to decode contract call results while the first two arguments are the same as the encoding method the last one is the actuall result to be decoded.

NodeJS example:

const {Encoder} = require('@aeternity/aepp-calldata')
const ACI = require('./Test.json')
const CONTRACT = 'Test'

const encoder = new Encoder(ACI)

const encoded = encoder.encode(CONTRACT, 'test_string', ["whoolymoly"])
console.log(`Encoded data: ${encoded}`)

const decoded = encoder.decode(CONTRACT, 'test_string', 'cb_KXdob29seW1vbHlGazSE')
console.log(`Decoded data: ${decoded}`)

Expected output:

Encoded data: cb_KxHwzCuVGyl3aG9vbHltb2x5zwMSnw==
Decoded data: whoolymoly

Contract call errors

FATE contract call error message is represented as cb_ prefixed base64check encoded string, to get the error as string one can use decodeString shorthand method instead of doing it in their codebase. However, revert messages are FATE string encoded, so a different helper method decodeFateString should be used.

Example:

const {Encoder} = require('@aeternity/aepp-calldata')
const ACI = require('./Test.json')

const encoder = new Encoder(ACI)

// error message
const error = encoder.decodeString('cb_VHlwZSBlcnJvciBvbiBjYWxsOiBbe2J5dGVzLDw8MjQwLDIsLi4uPj59XSBpcyBub3Qgb2YgdHlwZSBbe2J5dGVzLDMyfV3EtJjU')
// note that decodeString returns a Buffer that has to be converted to string
console.log('Error: ' + error.toString())

// revert message
const revert = encoder.decodeFateString('cb_OXJlcXVpcmUgZmFpbGVkarP9mg==')
console.log('Revert: ' + revert)

Expected output:

Error: Type error on call: [{bytes,<<240,2,...>>}] is not of type [{bytes,32}]
Revert: require failed

Data types

Using the library involves data types and their mappings from Sophia to JavaScript and vice versa.

Sophia TypeSophia ExampleJavascript typeJavascript Example
int63, -63BigInt63n, -63n
booltrue, falseBooleantrue, false
string"whoolymoly"String"whoolymoly"
bytes#beefBigIntBigInt("0xbeef")
list[1, 2, 3, 5, 8, 13, 21]Array[1,2,3,5,8,13,21]
tuple(true, false)Array[true, false]
map{[7] = false}Mapnew Map([[7, false]])
record{x = 0, y = 0}Object (POJO){x: 0, y: 0}
variantSome(404), NoneObject (POJO){'Some': [404]}, {'None': []}, 404, undefined
bitsBits.none, Bits.all Bits.set(Bits.none, 0)BigInt0b0n, -1n, 0b00000001n
hash#001234dBigIntBigInt("0x001234d")
signature#001234dBigIntBigInt("0x001234d")
addressak_2gx9MEFxKvY9vMG5YnqnXWv1hCsX7rgnfvBLJS4aQurustR1rtStringak_2gx9MEFxKvY9vMG5YnqnXWv1hCsX7rgnfvBLJS4aQurustR1rt
  • note the fixed structure of variant object with a single key - the variant constructor (i.e. Some) and array of variant arguments as it's value.
  • while Javascript Number and primitive int types can be used as well when BigInt type is expected it's not recommended because of it's Number.MAX_SAFE_INTEGER limitation.

Versioning

This project follows the semantic versioning guidelines. Refer to the CHANGELOG for more information about releases.

Public API

The backward compatibility promise signaled with semantic versioning above is only applied to public API of this library, that is only the module exports and data types listed above.

The public API namely consist of:

  • encode(contractName: string, functionName: string, arguments: Array<Data>): string
  • decode(contractName: string, functionName: string, encodedData: string): Data
  • decodeString(data: string): Buffer
  • decodeFateString(data: string): string

where Data: Boolean | BigInt | String | Array | Map | Object

Development

Please make sure you get familiar with Contributing Guidelines first.

Install

npm install

Tests

make tests

Verify browser compatibility with:

make browser-tests

To see the test coverage run:

make coverage

Keywords

FAQs

Package last updated on 24 Nov 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc