Socket
Socket
Sign inDemoInstall

abi-coder

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abi-coder - npm Package Compare versions

Comparing version 4.1.1 to 5.0.0

3

lib/index.d.ts

@@ -1,2 +0,2 @@

import { JsonFragment, ParamType } from '@ethersproject/abi';
import { JsonFragment, ParamType } from 'ethers';
interface FunctionData {

@@ -45,4 +45,5 @@ name: string;

private static getSignature;
private static getInputSignature;
}
export { Coder, Constructor, Event, EventEncoding, FunctionData, FunctionOutputData, ValueMap, };
//# sourceMappingURL=index.d.ts.map

@@ -1,18 +0,4 @@

"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Coder = void 0;
var abi_1 = require("@ethersproject/abi");
var keccak256_1 = require("@ethersproject/keccak256");
var strings_1 = require("@ethersproject/strings");
var Coder = /** @class */ (function () {
function Coder(abi) {
import { AbiCoder, ParamType, Result, keccak256, toUtf8Bytes, } from 'ethers';
class Coder {
constructor(abi) {
Object.defineProperty(this, "abi", {

@@ -26,353 +12,282 @@ enumerable: true,

}
Object.defineProperty(Coder.prototype, "getFunctionSelector", {
enumerable: false,
configurable: true,
writable: true,
value: function (name) {
var func = this.getFunctionByName(name);
var jsonInputs = func === null || func === void 0 ? void 0 : func.inputs;
if (!jsonInputs) {
throw Error;
}
var inputs = jsonInputs.map(function (input) { return abi_1.ParamType.fromObject(input); });
var signature = Coder.getSignature(name, inputs);
var hash = sha3(signature);
return hash.substring(0, 10);
getFunctionSelector(name) {
const func = this.getFunctionByName(name);
const jsonInputs = func?.inputs;
if (!jsonInputs) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "getEventTopic", {
enumerable: false,
configurable: true,
writable: true,
value: function (name) {
var event = this.getEventByName(name);
var jsonInputs = event === null || event === void 0 ? void 0 : event.inputs;
if (!jsonInputs) {
throw Error;
}
var inputs = jsonInputs.map(function (input) { return abi_1.ParamType.fromObject(input); });
var signature = Coder.getSignature(name, inputs);
return sha3(signature);
const inputs = jsonInputs.map((input) => ParamType.from(input));
const signature = Coder.getSignature(name, inputs);
const hash = sha3(signature);
return hash.substring(0, 10);
}
getEventTopic(name) {
const event = this.getEventByName(name);
const jsonInputs = event?.inputs;
if (!jsonInputs) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "decodeConstructor", {
enumerable: false,
configurable: true,
writable: true,
value: function (data) {
var constructor = this.getConstructor();
var jsonInputs = constructor === null || constructor === void 0 ? void 0 : constructor.inputs;
if (!jsonInputs) {
throw Error;
}
var inputs = jsonInputs.map(function (input) { return abi_1.ParamType.fromObject(input); });
var result = abi_1.defaultAbiCoder.decode(inputs, data);
var values = toValueMap(result, inputs);
return {
inputs: inputs,
values: values,
};
const inputs = jsonInputs.map((input) => ParamType.from(input, true));
const signature = Coder.getSignature(name, inputs);
return sha3(signature);
}
decodeConstructor(data) {
const constructor = this.getConstructor();
const jsonInputs = constructor?.inputs;
if (!jsonInputs) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "decodeEvent", {
enumerable: false,
configurable: true,
writable: true,
value: function (topics, data) {
var event = this.getEventByTopic(topics[0]);
var dataTopics = topics.slice(1);
var jsonInputs = event === null || event === void 0 ? void 0 : event.inputs;
if (!jsonInputs) {
throw Error;
}
var inputs = jsonInputs.map(function (input) { return abi_1.ParamType.fromObject(input); });
// Decode topics
var topicInputs = inputs.filter(function (input) { return input.indexed; });
var topicResult = topicInputs.map(function (input, index) {
var topic = dataTopics[index];
var params = abi_1.defaultAbiCoder.decode([input], topic);
var param = params[0];
return param;
});
// Decode data
var dataInputs = inputs.filter(function (input) { return !input.indexed; });
var dataResult = abi_1.defaultAbiCoder.decode(dataInputs, data);
// Concat
if (!event.name) {
throw Error;
}
var topicIndex = 0;
var dataIndex = 0;
var result = [];
for (var _i = 0, inputs_1 = inputs; _i < inputs_1.length; _i++) {
var input = inputs_1[_i];
if (input.indexed) {
result.push(topicResult[topicIndex]);
topicIndex++;
}
else {
result.push(dataResult[dataIndex]);
dataIndex++;
}
}
var values = toValueMap(result, inputs);
return {
name: event.name,
inputs: inputs,
values: values,
};
const inputs = jsonInputs.map((input) => ParamType.from(input));
const result = AbiCoder.defaultAbiCoder().decode(inputs, data);
const values = toValueMap(result, inputs);
return {
inputs,
values,
};
}
decodeEvent(topics, data) {
const event = this.getEventByTopic(topics[0]);
const [, ...dataTopics] = topics;
const jsonInputs = event?.inputs;
if (!jsonInputs) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "decodeFunction", {
enumerable: false,
configurable: true,
writable: true,
value: function (data) {
var selector = data.substring(0, 10);
var func = this.getFunctionBySelector(selector);
// Decode calldata using function inputs
var jsonInputs = func === null || func === void 0 ? void 0 : func.inputs;
if (!jsonInputs) {
throw Error;
const inputs = jsonInputs.map((input) => ParamType.from(input, true));
// Decode topics
const topicInputs = inputs.filter((input) => input.indexed);
const topicResult = topicInputs.map((input, index) => {
const topic = dataTopics[index];
const params = AbiCoder.defaultAbiCoder().decode([input], topic);
const [param] = params;
return param;
});
// Decode data
const dataInputs = inputs.filter((input) => !input.indexed);
const dataResult = AbiCoder.defaultAbiCoder().decode(dataInputs, data);
// Concat
if (!event.name) {
throw Error;
}
let topicIndex = 0;
let dataIndex = 0;
const result = new Result();
for (const input of inputs) {
if (input.indexed) {
result.push(topicResult[topicIndex]);
topicIndex++;
}
var inputs = jsonInputs.map(function (input) { return abi_1.ParamType.fromObject(input); });
var calldata = "0x".concat(data.substring(10));
var result = abi_1.defaultAbiCoder.decode(inputs, calldata);
var values = toValueMap(result, inputs);
if (!func.name) {
throw Error;
else {
result.push(dataResult[dataIndex]);
dataIndex++;
}
return {
name: func.name,
inputs: inputs,
values: values,
};
}
});
Object.defineProperty(Coder.prototype, "decodeFunctionOutput", {
enumerable: false,
configurable: true,
writable: true,
value: function (name, data) {
var func = this.getFunctionByName(name);
var jsonOutputs = func === null || func === void 0 ? void 0 : func.outputs;
if (!jsonOutputs) {
throw Error;
}
var outputs = jsonOutputs.map(function (output) { return abi_1.ParamType.fromObject(output); });
var result = abi_1.defaultAbiCoder.decode(outputs, data);
var values = toValueMap(result, outputs);
return {
name: name,
outputs: outputs,
values: values,
};
const values = toValueMap(result, inputs);
return {
name: event.name,
inputs,
values,
};
}
decodeFunction(data) {
const selector = data.substring(0, 10);
const func = this.getFunctionBySelector(selector);
// Decode calldata using function inputs
const jsonInputs = func?.inputs;
if (!jsonInputs) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "encodeConstructor", {
enumerable: false,
configurable: true,
writable: true,
value: function (valueMap) {
var constructor = this.getConstructor();
var jsonInputs = constructor === null || constructor === void 0 ? void 0 : constructor.inputs;
if (!jsonInputs) {
throw Error;
}
var inputs = jsonInputs.map(function (input) { return abi_1.ParamType.fromObject(input); });
var values = toValues(valueMap, inputs);
return abi_1.defaultAbiCoder.encode(inputs, values);
const inputs = jsonInputs.map((input) => ParamType.from(input));
const calldata = `0x${data.substring(10)}`;
const result = AbiCoder.defaultAbiCoder().decode(inputs, calldata);
const values = toValueMap(result, inputs);
if (!func.name) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "encodeEvent", {
enumerable: false,
configurable: true,
writable: true,
value: function (name, values) {
var event = this.getEventByName(name);
var jsonInputs = event === null || event === void 0 ? void 0 : event.inputs;
if (!jsonInputs) {
throw Error;
return {
name: func.name,
inputs,
values,
};
}
decodeFunctionOutput(name, data) {
const func = this.getFunctionByName(name);
const jsonOutputs = func?.outputs;
if (!jsonOutputs) {
throw Error;
}
const outputs = jsonOutputs.map((output) => ParamType.from(output));
const result = AbiCoder.defaultAbiCoder().decode(outputs, data);
const values = toValueMap(result, outputs);
return {
name,
outputs,
values,
};
}
encodeConstructor(valueMap) {
const constructor = this.getConstructor();
const jsonInputs = constructor?.inputs;
if (!jsonInputs) {
throw Error;
}
const inputs = jsonInputs.map((input) => ParamType.from(input));
const values = toValues(valueMap, inputs);
return AbiCoder.defaultAbiCoder().encode(inputs, values);
}
encodeEvent(name, values) {
const event = this.getEventByName(name);
const jsonInputs = event?.inputs;
if (!jsonInputs) {
throw Error;
}
const inputs = jsonInputs.map((input) => ParamType.from(input, true));
const eventSignature = Coder.getSignature(name, inputs);
const eventTopic = sha3(eventSignature);
// Group params by type
const topicResult = new Result();
const dataResult = [];
for (let i = 0; i < inputs.length; i++) {
const input = inputs[i];
const value = values[input.name];
if (input.indexed) {
topicResult.push(value);
}
var inputs = jsonInputs.map(function (input) { return abi_1.ParamType.fromObject(input); });
var eventSignature = Coder.getSignature(name, inputs);
var eventTopic = sha3(eventSignature);
// Group params by type
var topicResult = [];
var dataResult = [];
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
var value = values[input.name];
if (input.indexed) {
topicResult.push(value);
}
else {
dataResult.push(value);
}
else {
dataResult.push(value);
}
// Encode topic params
var topicInputs = inputs.filter(function (input) { return input.indexed; });
var dataTopics = topicInputs.map(function (input, index) {
return abi_1.defaultAbiCoder.encode([input], [topicResult[index]]);
});
var topics = __spreadArray([eventTopic], dataTopics, true);
// Encode data params
var dataInputs = inputs.filter(function (input) { return !input.indexed; });
var data = abi_1.defaultAbiCoder.encode(dataInputs, dataResult);
return {
topics: topics,
data: data,
};
}
});
Object.defineProperty(Coder.prototype, "encodeFunction", {
enumerable: false,
configurable: true,
writable: true,
value: function (name, valueMap) {
var func = this.getFunctionByName(name);
var jsonInputs = func === null || func === void 0 ? void 0 : func.inputs;
if (!jsonInputs) {
throw Error;
}
var inputs = jsonInputs.map(function (input) { return abi_1.ParamType.fromObject(input); });
var signature = Coder.getSignature(name, inputs);
var selector = sha3(signature).substring(2, 10);
var values = toValues(valueMap, inputs);
var argumentString = abi_1.defaultAbiCoder.encode(inputs, values);
var argumentData = argumentString.substring(2);
var inputData = "0x".concat(selector).concat(argumentData);
return inputData;
// Encode topic params
const topicInputs = inputs.filter((input) => input.indexed);
const dataTopics = topicInputs.map((input, index) => {
return AbiCoder.defaultAbiCoder().encode([input], [topicResult[index]]);
});
const topics = [eventTopic, ...dataTopics];
// Encode data params
const dataInputs = inputs.filter((input) => !input.indexed);
const data = AbiCoder.defaultAbiCoder().encode(dataInputs, dataResult);
return {
topics,
data,
};
}
encodeFunction(name, valueMap) {
const func = this.getFunctionByName(name);
const jsonInputs = func?.inputs;
if (!jsonInputs) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "encodeFunctionOutput", {
enumerable: false,
configurable: true,
writable: true,
value: function (name, valueMap) {
var func = this.getFunctionByName(name);
var jsonOutputs = func.outputs;
if (!jsonOutputs) {
throw Error;
}
var outputs = jsonOutputs.map(function (output) { return abi_1.ParamType.fromObject(output); });
var values = toValues(valueMap, outputs);
return abi_1.defaultAbiCoder.encode(outputs, values);
const inputs = jsonInputs.map((input) => ParamType.from(input));
const signature = Coder.getSignature(name, inputs);
const selector = sha3(signature).substring(2, 10);
const values = toValues(valueMap, inputs);
const argumentString = AbiCoder.defaultAbiCoder().encode(inputs, values);
const argumentData = argumentString.substring(2);
const inputData = `0x${selector}${argumentData}`;
return inputData;
}
encodeFunctionOutput(name, valueMap) {
const func = this.getFunctionByName(name);
const jsonOutputs = func.outputs;
if (!jsonOutputs) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "getConstructor", {
enumerable: false,
configurable: true,
writable: true,
value: function () {
var constructor = this.abi.find(function (item) { return item.type === 'constructor'; });
if (!constructor) {
throw Error;
}
return constructor;
const outputs = jsonOutputs.map((output) => ParamType.from(output));
const values = toValues(valueMap, outputs);
return AbiCoder.defaultAbiCoder().encode(outputs, values);
}
getConstructor() {
const constructor = this.abi.find((item) => item.type === 'constructor');
if (!constructor) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "getFunctionByName", {
enumerable: false,
configurable: true,
writable: true,
value: function (name) {
var func = this.abi.find(function (item) { return item.type === 'function' && item.name === name; });
if (!func) {
throw Error;
return constructor;
}
getFunctionByName(name) {
const func = this.abi.find((item) => item.type === 'function' && item.name === name);
if (!func) {
throw Error;
}
return func;
}
getFunctionBySelector(selector) {
const functions = this.abi.filter((item) => item.type === 'function' || item.type === 'error');
const func = functions.find((func) => {
const name = func.name;
const jsonInputs = func.inputs;
if (!name || !jsonInputs) {
return false;
}
return func;
const inputs = jsonInputs.map((input) => ParamType.from(input));
const signature = Coder.getSignature(name, inputs);
const hash = sha3(signature);
const funcSelector = hash.substring(0, 10);
return funcSelector === selector;
});
if (!func) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "getFunctionBySelector", {
enumerable: false,
configurable: true,
writable: true,
value: function (selector) {
var functions = this.abi.filter(function (item) { return item.type === 'function' || item.type === 'error'; });
var func = functions.find(function (func) {
var name = func.name;
var jsonInputs = func.inputs;
if (!name || !jsonInputs) {
return false;
}
var inputs = jsonInputs.map(function (input) { return abi_1.ParamType.fromObject(input); });
var signature = Coder.getSignature(name, inputs);
var hash = sha3(signature);
var funcSelector = hash.substring(0, 10);
return funcSelector === selector;
});
if (!func) {
throw Error;
return func;
}
getEventByName(name) {
const event = this.abi.find((item) => item.type === 'event' && item.name === name);
if (!event) {
throw Error;
}
return event;
}
getEventByTopic(topic) {
const events = this.abi.filter((item) => item.type === 'event');
const event = events.find((event) => {
const name = event.name;
const jsonInputs = event.inputs;
if (!name || !jsonInputs) {
return false;
}
return func;
const inputs = jsonInputs.map((input) => ParamType.from(input, true));
const signature = Coder.getSignature(name, inputs);
const eventTopic = sha3(signature);
return eventTopic === topic;
});
if (!event) {
throw Error;
}
});
Object.defineProperty(Coder.prototype, "getEventByName", {
enumerable: false,
configurable: true,
writable: true,
value: function (name) {
var event = this.abi.find(function (item) { return item.type === 'event' && item.name === name; });
if (!event) {
return event;
}
static getSignature(name, inputs) {
const inputSignatures = [];
for (const input of inputs) {
const inputSignature = this.getInputSignature(input);
inputSignatures.push(inputSignature);
}
const inputString = inputSignatures.join(',');
const functionSignature = `${name}(${inputString})`;
return functionSignature;
}
static getInputSignature(input) {
if (input.baseType === 'array') {
const arityString = input.arrayLength && input.arrayLength >= 0
? `[${input.arrayLength}]`
: '[]';
if (!input.arrayChildren) {
throw Error;
}
return event;
return `${this.getInputSignature(input.arrayChildren)}${arityString}`;
}
});
Object.defineProperty(Coder.prototype, "getEventByTopic", {
enumerable: false,
configurable: true,
writable: true,
value: function (topic) {
var events = this.abi.filter(function (item) { return item.type === 'event'; });
var event = events.find(function (event) {
var name = event.name;
var jsonInputs = event.inputs;
if (!name || !jsonInputs) {
return false;
}
var inputs = jsonInputs.map(function (input) { return abi_1.ParamType.fromObject(input); });
var signature = Coder.getSignature(name, inputs);
var eventTopic = sha3(signature);
return eventTopic === topic;
});
if (!event) {
if (input.baseType === 'tuple') {
if (!input.components) {
throw Error;
}
return event;
return `(${input.components
.map((childInput) => this.getInputSignature(childInput))
.join(',')})`;
}
});
Object.defineProperty(Coder, "getSignature", {
enumerable: false,
configurable: true,
writable: true,
value: function (name, inputs) {
var types = [];
for (var _i = 0, inputs_2 = inputs; _i < inputs_2.length; _i++) {
var input = inputs_2[_i];
if (input.type.startsWith('tuple')) {
var tupleString = Coder.getSignature('', input.components);
var arrayArityString = input.type.substring('tuple'.length);
var type = "".concat(tupleString).concat(arrayArityString);
types.push(type);
}
else {
types.push(input.type);
}
}
var typeString = types.join(',');
var functionSignature = "".concat(name, "(").concat(typeString, ")");
return functionSignature;
}
});
return Coder;
}());
exports.Coder = Coder;
return input.type;
}
}
function sha3(input) {
return (0, keccak256_1.keccak256)((0, strings_1.toUtf8Bytes)(input));
return keccak256(toUtf8Bytes(input));
}
function toValueMap(values, inputs) {
return Object.fromEntries(values.map(function (value, index) {
var input = inputs[index];
return Object.fromEntries(values.toArray().map((value, index) => {
const input = inputs[index];
return [input.name, value];

@@ -382,6 +297,7 @@ }));

function toValues(valueMap, inputs) {
return inputs.map(function (input) {
return inputs.map((input) => {
return valueMap[input.name];
});
}
export { Coder, };
//# sourceMappingURL=index.js.map
{
"name": "abi-coder",
"version": "4.1.1",
"version": "5.0.0",
"description": "ABI function, event, and contrustor encoder/decoder",

@@ -16,3 +16,8 @@ "author": "Timur Badretdinov <destinerx@gmail.com>",

},
"main": "lib/index.js",
"type": "module",
"files": [
"lib/"
],
"main": "./lib/index.js",
"exports": "./lib/index.js",
"scripts": {

@@ -26,9 +31,3 @@ "test": "vitest run",

},
"dependencies": {
"@ethersproject/abi": "^5.0.0",
"@ethersproject/keccak256": "^5.0.0",
"@ethersproject/strings": "^5.0.0"
},
"devDependencies": {
"@ethersproject/bignumber": "5.6.2",
"@types/node": "18.7.14",

@@ -44,4 +43,7 @@ "@typescript-eslint/eslint-plugin": "5.36.1",

"typescript": "4.8.4",
"vitest": "0.23.4"
"vitest": "^0.28.5"
},
"peerDependencies": {
"ethers": "^6.0.0"
}
}

@@ -11,6 +11,6 @@ # ABI Coder

* Function coding
* Event coding
* Constructor coding
* TS support
- Function coding
- Event coding
- Constructor coding
- TS support

@@ -21,2 +21,6 @@ ## Installation

> This package requires ethers V6. If you use ethers V5, you need to install abi-coder V4.
> This package is a pure ESM package. Follow [this guide](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) for more info.
## Example

@@ -35,8 +39,8 @@

erc20Coder.decodeEvent(
[
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x000000000000000000000000b5cfcb4d4745cbbd252945856e1b6eaadcf2fc4e',
'0x000000000000000000000000694c6aea9444876d4fa9375fc9089c370f8e9eda',
],
'0x0000000000000000000000000000000000000000000000370c9b5ef669c35300',
[
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x000000000000000000000000b5cfcb4d4745cbbd252945856e1b6eaadcf2fc4e',
'0x000000000000000000000000694c6aea9444876d4fa9375fc9089c370f8e9eda',
],
'0x0000000000000000000000000000000000000000000000370c9b5ef669c35300',
);

@@ -56,17 +60,17 @@ /*

erc20Coder.encodeFunction({
name: 'transfer',
inputs: [
{
name: 'from',
type: 'address',
},
{
name: 'amount',
type: 'uint256',
},
],
values: [
'0x694c6aea9444876d4fA9375fC9089C370F8E9edA',
'1015479348216300000000',
],
name: 'transfer',
inputs: [
{
name: 'from',
type: 'address',
},
{
name: 'amount',
type: 'uint256',
},
],
values: [
'0x694c6aea9444876d4fA9375fC9089C370F8E9edA',
'1015479348216300000000',
],
});

@@ -78,15 +82,15 @@ // '0xa9059cbb000000000000000000000000694c6aea9444876d4fa9375fc9089c370f8e9eda0000000000000000000000000000000000000000000000370c9b5ef669c35300'

* Coder(abi)
* utils
* getFunctionSelector(name: string): string
* getEventTopic(name: string): string
* decoding
* decodeConstructor(data: string): Constructor
* decodeEvent(topics: string[], data: string): Event
* decodeFunction(data: string): FunctionData
* decodeFunctionOutput(name: string, data: string): FunctionOutputData
* encoding
* encodeConstructor(constructorData: Constructor): string
* encodeEvent(eventData: Event): EventEncoding
* encodeFunction(functionData: FunctionData): string
* encodeFunctionOutput(functionData: FunctionOutputData): string
- Coder(abi)
- utils
- getFunctionSelector(name: string): string
- getEventTopic(name: string): string
- decoding
- decodeConstructor(data: string): Constructor
- decodeEvent(topics: string[], data: string): Event
- decodeFunction(data: string): FunctionData
- decodeFunctionOutput(name: string, data: string): FunctionOutputData
- encoding
- encodeConstructor(constructorData: Constructor): string
- encodeEvent(eventData: Event): EventEncoding
- encodeFunction(functionData: FunctionData): string
- encodeFunctionOutput(functionData: FunctionOutputData): string

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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