Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@truffle/abi-utils
Advanced tools
@truffle/abi-utils
Contains a few utilities for dealing with ABIs.
This package contains a few different components:
// handle function entries omitting "type" from JSON const isFunctionEntry = entry.type === "function" || !("type" in entry); // handle: v--- new way v--- old way v--- default const isPayable = entry.stateMutability === "payable" || entry.payable || false; // handle "outputs" possibly being undefined const outputs = entry.outputs || [];
^ Have you ever had to do this sort of thing? :scream:
Solidity's official JSON ABI specification is rather permissive, since it remains backwards compatible with older versions of the language and because it permits omitting fields with default values. This can get annoying if you're programmatically processing ABIs.
:information_source: This package provides a normalize
function to purge
these kinds of inconsistencies.
const { normalize } = require("@truffle/abi-utils");
import { normalize } from "@truffle/abi-utils";
Specifically, this normalizes by:
type
field, since it's optional for
type: "function"
[]
for function outputs
fieldpayable
and constant
fieldsstateMutability
fieldTo use, provide the ABI as a JavaScript array, as the sole argument to the function:
// accepts ABIs from Solidity versions back to 0.4.12 or earlier!
const abi = normalize([{"type": "constructor"/*, ...*/}/*, ...*/);
// don't even worry about it
const isFunctionEntry = entry.type === "function";
const isPayable = entry.stateMutability === "payable";
This package exports the following types for normalized ABIs.
Abi
, to represent the full ABI arrayEntry
, to represent items in ABI arraysFunctionEntry
, to represent named functionsConstructorEntry
, to represent constructorsFallbackEntry
, to represent old or new fallback functionsReceiveEntry
, to represent receive functionsParameter
, to represent parameters defined in entry inputs or outputsEventParameter
, to represent event parametersTo use these, you should first call normalize
, described
above.
import * as Abi from "@truffle/abi-utils";
const abi: Abi.Abi = [{ type: "constructor" /*, ...*/ } /*, ...*/];
const parameter: Abi.Parameter = {
type: "tuple[]",
components: [
/*...*/
]
};
// etc.
This package exports the following functions for computing signatures and selectors:
abiSelector
: This function takes a FunctionEntry
, EventEntry
, or
ErrorEntry
and computes its selector, returned as a hex string. This will
be 4 bytes for a function or error, and 32 bytes for an event.abiSignature
: This function takes a FunctionEntry
, EventEntry
, or
ErrorEntry
and computes its written-out signature (e.g., "setStoredValue(uint256)"
).abiTupleSignature
: This function takes a Parameter[]
and computes the signature
of that tuple on its own; e.g., "(uint256,string)"
for a uint
and a string
.abiTypeSignature
: This function takes an individual Parameter
and computes
the signature of that type on its own; e.g., uint256
for a uint
.In addition, the package also exports the constant ShortSelectorSize
, which
is equal to 4 (the number of bytes in a function or event selector).
Do you need to test all the different kinds of ABIs, including testing your support for the various quirks across different Solidity versions? :flushed:
You can use this package for generating all sorts of random ABIs, random ABI events, random ABI parameter values, etc.
This package provides fast-check arbitraries for property-based testing methodologies. If you're not familiar with fast-check or property-based testing, please see the link above for more information.
import * as fc from "fast-check";
import { Arbitrary } from "@truffle/abi-utils";
// generate 10 random ABIs
const randomAbis = fc.sample(Arbitrary.Abi(), 10);
See this package's internal tests for normalize
for example usage in automated tests.
FAQs
Utilities for working with ABIs
The npm package @truffle/abi-utils receives a total of 20,256 weekly downloads. As such, @truffle/abi-utils popularity was classified as popular.
We found that @truffle/abi-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.