Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
@aeternity/aepp-calldata
Advanced tools
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.
npm install -P @aeternity/aepp-calldata
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
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
Example:
const {Encoder} = require('@aeternity/aepp-calldata')
const ACI = require('./Test.json')
const encoder = new Encoder(ACI)
const data = encoder.decodeEvent('Test', 'cb_dHJpZ2dlcmVk1FYuYA==', [
34853523142692495808479485503424878684430196596020091237715106250497712463899n,
17
])
console.log(data)
Expected output:
{EventTwo: [17n, 'triggered']}
Using the library involves data types and their mappings from Sophia to JavaScript and vice versa.
Sophia Type | Sophia Example | Javascript type | Javascript Example |
---|---|---|---|
int | 63 , -63 | BigInt | 63n , -63n |
bool | true , false | Boolean | true , false |
string | "whoolymoly" | String | "whoolymoly" |
bytes | #beef | BigInt | BigInt("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} | Map, Object, Array | new Map([[7, false]]) , {7: false} , [[7, false]] |
record | {x = 0, y = 0} | Object (POJO) | {x: 0, y: 0} |
variant | Some(404) , None | Object (POJO) | {'Some': [404]} , {'None': []} , 404 , undefined |
bits | Bits.none , Bits.all Bits.set(Bits.none, 0) | BigInt | 0b0n , -1n , 0b00000001n |
hash | #001234d | BigInt | BigInt("0x001234d") |
signature | #001234d | BigInt | BigInt("0x001234d") |
address | ak_2gx9MEFxKvY9vMG5YnqnXWv1hCsX7rgnfvBLJS4aQurustR1rt | String | ak_2gx9MEFxKvY9vMG5YnqnXWv1hCsX7rgnfvBLJS4aQurustR1rt |
Some
) and array of variant arguments as it's value.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.This project follows the semantic versioning guidelines. Refer to the CHANGELOG for more information about releases.
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
decodeEvent(contractName: string, data: string, topics: Array<BigInt>): string
decodeString(data: string): Buffer
decodeFateString(data: string): string
where Data: Boolean | BigInt | String | Array | Map | Object
Error names are also part of the public API and it is guaranteed to get the same error name between compatible versions.
Since error classes are not exported as public API, the library users should rely only on Error.name
property to handle exceptions.
Please also note that error messages are NOT part of the public API and they may change any time between versions without notice.
Please make sure you get familiar with Contributing Guidelines first.
npm install
make tests
Verify browser compatibility with:
make browser-tests
To see the test coverage run:
make coverage
FAQs
Aeternity data serialization library
The npm package @aeternity/aepp-calldata receives a total of 124 weekly downloads. As such, @aeternity/aepp-calldata popularity was classified as not popular.
We found that @aeternity/aepp-calldata demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.