Socket
Socket
Sign inDemoInstall

@ganache/utils

Package Overview
Dependencies
Maintainers
4
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ganache/utils - npm Package Compare versions

Comparing version 0.4.0 to 0.4.1

lib/src/things/json-rpc/input-parsers.js

4

lib/index.js

@@ -20,5 +20,7 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.PromiEvent = exports.JsonRpcErrorCode = exports.makeResponse = exports.makeRequest = exports.makeError = void 0;
exports.PromiEvent = exports.JsonRpcErrorCode = exports.makeResponse = exports.makeRequest = exports.makeError = exports.JsonRpcType = void 0;
__exportStar(require("./src/types"), exports);
__exportStar(require("./src/utils"), exports);
var json_rpc_1 = require("./src/things/json-rpc");
Object.defineProperty(exports, "JsonRpcType", { enumerable: true, get: function () { return json_rpc_1.JsonRpcType; } });
__exportStar(require("./src/things/subscription"), exports);

@@ -25,0 +27,0 @@ __exportStar(require("./src/things/json-rpc/json-rpc-quantity"), exports);

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonRpcType = void 0;
var json_rpc_base_types_1 = require("./json-rpc-base-types");
Object.defineProperty(exports, "JsonRpcType", { enumerable: true, get: function () { return json_rpc_base_types_1.BaseJsonRpcType; } });
//# sourceMappingURL=index.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseJsonRpcType = exports.toBuffers = exports.toStrings = exports.bufCache = exports.strCache = void 0;
const utils_1 = require("../../utils");
const utils_2 = require("../../utils");
const constants_1 = require("../../utils/constants");
exports.strCache = new WeakMap();
exports.bufCache = new WeakMap();
exports.toStrings = new WeakMap();
exports.toBuffers = new WeakMap();
exports.BaseJsonRpcType = void 0;
const input_parsers_1 = require("./input-parsers");
const inspect = Symbol.for("nodejs.util.inspect.custom");
class BaseJsonRpcType {
constructor(value) {
const self = this;
if (Buffer.isBuffer(value)) {
exports.toStrings.set(this, () => value.toString("hex"));
exports.bufCache.set(this, value);
self[Symbol.toStringTag] = "Buffer";
if (value == null) {
this.bufferValue = null;
}
else if (Buffer.isBuffer(value)) {
// empty buffer should be treated as null
this.bufferValue = value.length === 0 ? null : value;
}
else {
const type = typeof value;
switch (type) {
switch (typeof value) {
case "string":
this.bufferValue = (0, input_parsers_1.parseAndValidateStringInput)(value);
break;
case "number":
if (value % 1 !== 0) {
throw new Error("`Cannot wrap a decimal value as a json-rpc type`");
}
exports.toStrings.set(this, () => value.toString(16));
exports.toBuffers.set(this, () => value === 0 ? constants_1.BUFFER_EMPTY : (0, utils_2.uintToBuffer)(value));
this.bufferValue = (0, input_parsers_1.parseAndValidateNumberInput)(value);
break;
case "bigint":
exports.toStrings.set(this, () => value.toString(16));
exports.toBuffers.set(this, () => value === 0n ? constants_1.BUFFER_EMPTY : (0, utils_1.bigIntToBuffer)(value));
this.bufferValue = (0, input_parsers_1.parseAndValidateBigIntInput)(value);
break;
case "string": {
// handle hex-encoded string
if (value.indexOf("0x") === 0) {
exports.toStrings.set(this, () => value.toLowerCase().slice(2));
exports.strCache.set(this, value.toLowerCase());
exports.toBuffers.set(this, () => {
let fixedValue = value.slice(2);
if (fixedValue.length % 2 === 1) {
fixedValue = "0" + fixedValue;
}
return Buffer.from(fixedValue, "hex");
});
}
else {
throw new Error(`cannot convert string value "${value}" into type \`${this.constructor.name}\`; strings must be hex-encoded and prefixed with "0x".`);
}
break;
}
default:
// handle undefined/null
if (value == null) {
// This is a weird thing that returns undefined/null for a call
// to toString().
this.toString = () => value;
exports.bufCache.set(this, constants_1.BUFFER_EMPTY);
break;
}
throw new Error(`Cannot wrap a "${type}" as a json-rpc type`);
throw new Error(`Cannot wrap a "${typeof value}" as a json-rpc type`);
}
self[Symbol.toStringTag] = type;
}
this.value = value;
}
// used to make console.log debugging a little easier
[(Symbol.toStringTag, inspect)](_depth, _options) {
return this.value;
[inspect](_depth, _options) {
return `[${this.constructor.name}] ${this.toString()}`;
}
toString() {
let str = exports.strCache.get(this);
if (str === void 0) {
str = "0x" + exports.toStrings.get(this)();
exports.strCache.set(this, str);
if (this.bufferValue == null) {
return null;
}
return str;
return `0x${this.bufferValue.toString("hex")}`;
}
toBuffer() {
let buf = exports.bufCache.get(this);
if (buf === void 0) {
buf = exports.toBuffers.get(this)();
exports.bufCache.set(this, buf);
}
return buf;
return this.bufferValue;
}
valueOf() {
return this.value;
return this.bufferValue;
}

@@ -94,3 +51,3 @@ toJSON() {

isNull() {
return this.value == null;
return this.bufferValue == null;
}

@@ -97,0 +54,0 @@ }

@@ -5,8 +5,3 @@ "use strict";

const json_rpc_base_types_1 = require("./json-rpc-base-types");
const json_rpc_base_types_2 = require("./json-rpc-base-types");
function validateByteLength(byteLength) {
if (typeof byteLength !== "number" || !(byteLength >= 0)) {
throw new Error(`byteLength must be a number greater than or equal to 0, provided: ${byteLength}`);
}
}
const constants_1 = require("../../utils/constants");
class Data extends json_rpc_base_types_1.BaseJsonRpcType {

@@ -19,35 +14,23 @@ constructor(value, _byteLength) {

}
if (_byteLength !== undefined) {
validateByteLength(_byteLength);
}
}
toString(byteLength) {
if (byteLength === undefined) {
byteLength = this._byteLength;
const length = byteLength || this._byteLength;
if (this.bufferValue == null) {
return "0x";
}
if (byteLength === undefined && json_rpc_base_types_2.strCache.has(this)) {
return json_rpc_base_types_2.strCache.get(this);
if (length === undefined) {
return super.toString();
}
else {
let str = json_rpc_base_types_2.toStrings.get(this)();
let length = str.length;
if (length % 2 === 1) {
length++;
str = `0${str}`;
}
if (byteLength !== undefined) {
validateByteLength(byteLength);
const strLength = byteLength * 2;
const padBy = strLength - length;
if (padBy < 0) {
// if our hex-encoded data is longer than it should be, truncate it:
str = str.slice(0, strLength);
}
else if (padBy > 0) {
// if our hex-encoded data is shorter than it should be, pad it:
str = "0".repeat(padBy) + str;
}
}
return `0x${str}`;
const strValue = this.bufferValue.toString("hex");
return `0x${Data.stringToFixedByteLength(strValue, length)}`;
}
toBuffer(byteLength) {
if (this.bufferValue == null) {
return constants_1.BUFFER_EMPTY;
}
const length = byteLength || this._byteLength;
if (length == undefined || length === this.bufferValue.length) {
return this.bufferValue;
}
return Data.bufferToFixedByteLength(this.bufferValue, length);
}

@@ -57,4 +40,39 @@ static from(value, byteLength) {

}
static stringToFixedByteLength(value, byteLength) {
const desiredCharLength = byteLength * 2;
if (desiredCharLength === value.length) {
return value;
}
const padCharCount = desiredCharLength - value.length;
let fixedLengthValue;
if (padCharCount > 0) {
fixedLengthValue = "0".repeat(padCharCount) + value;
}
else {
fixedLengthValue = value.slice(0, desiredCharLength);
}
return fixedLengthValue;
}
static bufferToFixedByteLength(value, byteLength) {
if (byteLength === value.length) {
return value;
}
const fixedLengthValue = Buffer.allocUnsafe(byteLength);
const sourceStart = 0;
const targetStart = value.length > byteLength ? 0 : byteLength - value.length;
if (targetStart > 0) {
fixedLengthValue.fill(0, 0, targetStart);
}
value.copy(fixedLengthValue, targetStart, sourceStart, byteLength);
return fixedLengthValue;
}
static toBuffer(value, byteLength) {
return Data.from(value, byteLength).toBuffer();
}
static toString(value, byteLength) {
return Data.from(value, byteLength).toString();
}
}
exports.Data = Data;
Data.Empty = Data.from(constants_1.BUFFER_EMPTY);
//# sourceMappingURL=json-rpc-data.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Quantity = void 0;
const utils_1 = require("../../utils");
const buffer_to_bigint_1 = require("../../utils/buffer-to-bigint");
const json_rpc_base_types_1 = require("./json-rpc-base-types");
// TODO(perf): rewrite this stuff since it isn't really caching anything
const constants_1 = require("../../utils/constants");
class Quantity extends json_rpc_base_types_1.BaseJsonRpcType {
constructor() {
super(...arguments);
constructor(value, nullable) {
super(value);
this._nullable = false;
if (value === "0x") {
throw new Error('Cannot wrap "0x" as a json-rpc Quantity type; strings must contain at least one hexadecimal character.');
}
this._nullable = nullable;
}

@@ -15,78 +19,70 @@ static from(value, nullable = false) {

return value;
const q = new Quantity(value);
q._nullable = nullable;
return q;
return new Quantity(value, nullable);
}
toString() {
// TODO(perf): memoize this stuff
if (Buffer.isBuffer(this.value)) {
let val = this.value.toString("hex").replace(/^(?:0+(.+?))?$/, "$1");
if (val === "") {
if (this._nullable) {
return null;
}
// RPC Quantities must represent `0` as `0x0`
return "0x0";
}
return `0x${val}`;
if (this.bufferValue == null) {
return this._nullable ? null : Quantity.ZERO_VALUE_STRING;
}
else if (this.value == null) {
return "0x";
const firstNonZeroByte = this.findFirstNonZeroByteIndex();
// bufferValue is empty, or contains only 0 bytes
if (firstNonZeroByte === this.bufferValue.length) {
return Quantity.ZERO_VALUE_STRING;
}
else {
return super.toString();
let value = this.bufferValue.toString("hex", firstNonZeroByte);
// only need to check the first char, as we have already skipped 0 bytes in call to this.bufferValue.toString().
if (value[0] === "0") {
value = value.slice(1);
}
return `0x${value}`;
}
toBuffer() {
// 0x0, 0x00, 0x000, etc should return BUFFER_EMPTY
if (Buffer.isBuffer(this.value)) {
// trim zeros from start
let best = 0;
for (best = 0; best < this.value.length; best++) {
if (this.value[best] !== 0)
break;
}
if (best > 0) {
return this.value.slice(best);
}
else {
return this.value;
}
if (this.bufferValue == null) {
return constants_1.BUFFER_EMPTY;
}
else if (typeof this.value === "string") {
let val = this.value.slice(2).replace(/^(?:0+(.+?))?$/, "$1");
if (val === "" || val === "0") {
return utils_1.BUFFER_EMPTY;
}
const firstNonZeroByte = this.findFirstNonZeroByteIndex();
if (firstNonZeroByte > 0) {
return this.bufferValue.subarray(firstNonZeroByte);
}
else if (this.value === 0 || this.value === 0n) {
return utils_1.BUFFER_EMPTY;
else {
return this.bufferValue;
}
return super.toBuffer();
}
toBigInt() {
const value = this.value;
// TODO(perf): memoize this stuff
if (Buffer.isBuffer(value)) {
const bigInt = (0, utils_1.bufferToBigInt)(value);
return bigInt == null ? (this._nullable ? null : 0n) : bigInt;
if (this.bufferValue == null) {
return this._nullable ? null : 0n;
}
else {
return value == null ? (this._nullable ? null : 0n) : BigInt(value);
if (this.bufferValue.length === 0) {
return 0n;
}
return (0, buffer_to_bigint_1.bufferToBigInt)(this.bufferValue);
}
toNumber() {
// TODO(perf): memoize this stuff
return typeof this.value === "number"
? this.value
: Number(this.toBigInt());
if (this.bufferValue == null) {
return this._nullable ? null : 0;
}
const firstNonZeroByte = this.findFirstNonZeroByteIndex();
const length = this.bufferValue.length - firstNonZeroByte;
if (length === 0) {
return 0;
}
let result;
// buffer.readUIntBE only supports up to 48 bits, so if larger then we need to convert to bigint first
if (length > 6) {
const trimmedBuffer = firstNonZeroByte === 0
? this.bufferValue
: this.bufferValue.subarray(firstNonZeroByte, length);
result = Number((0, buffer_to_bigint_1.bufferToBigInt)(trimmedBuffer));
if (!Number.isSafeInteger(result)) {
console.warn(`0x${this.bufferValue.toString("hex")} is too large - the maximum safe integer value is 0${Number.MAX_SAFE_INTEGER.toString(16)}`);
}
}
else {
result = this.bufferValue.readUIntBE(firstNonZeroByte, length);
}
return result;
}
valueOf() {
const value = this.value;
if (value === null) {
return value;
if (this.bufferValue == null) {
return null;
}
else if (value === undefined) {
return value;
}
else {

@@ -96,5 +92,30 @@ return this.toBigInt();

}
findFirstNonZeroByteIndex() {
let firstNonZeroByte = 0;
for (firstNonZeroByte = 0; firstNonZeroByte < this.bufferValue.length; firstNonZeroByte++) {
if (this.bufferValue[firstNonZeroByte] !== 0)
break;
}
return firstNonZeroByte;
}
static toBuffer(value, nullable) {
return Quantity.from(value, nullable).toBuffer();
}
static toString(value, nullable) {
return Quantity.from(value, nullable).toString();
}
static toNumber(value, nullable) {
return Quantity.from(value, nullable).toNumber();
}
static toBigInt(value, nullable) {
return Quantity.from(value, nullable).toBigInt();
}
}
exports.Quantity = Quantity;
Quantity.Empty = Quantity.from(constants_1.BUFFER_EMPTY, true);
Quantity.Zero = Quantity.from(constants_1.BUFFER_ZERO);
Quantity.One = Quantity.from(1);
Quantity.Gwei = Quantity.from(1000000000);
Quantity.ZERO_VALUE_STRING = "0x0";
exports.default = Quantity;
//# sourceMappingURL=json-rpc-quantity.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KNOWN_CHAINIDS = exports.WEI = exports.RPCQUANTITY_GWEI = exports.RPCQUANTITY_ONE = exports.RPCQUANTITY_ZERO = exports.RPCQUANTITY_EMPTY = exports.DATA_EMPTY = exports.BUFFER_8_ZERO = exports.BUFFER_32_ZERO = exports.BUFFER_ZERO = exports.BUFFER_EMPTY = exports.ACCOUNT_ZERO = exports.BUFFER_256_ZERO = void 0;
const json_rpc_data_1 = require("../things/json-rpc/json-rpc-data");
const json_rpc_quantity_1 = require("../things/json-rpc/json-rpc-quantity");
exports.KNOWN_CHAINIDS = exports.WEI = exports.BUFFER_8_ZERO = exports.BUFFER_32_ZERO = exports.BUFFER_ZERO = exports.BUFFER_EMPTY = exports.ACCOUNT_ZERO = exports.BUFFER_256_ZERO = void 0;
exports.BUFFER_256_ZERO = Buffer.allocUnsafe(256).fill(0);

@@ -12,9 +10,4 @@ exports.ACCOUNT_ZERO = exports.BUFFER_256_ZERO.slice(0, 20);

exports.BUFFER_8_ZERO = exports.BUFFER_256_ZERO.slice(0, 8);
exports.DATA_EMPTY = json_rpc_data_1.Data.from(exports.BUFFER_EMPTY);
exports.RPCQUANTITY_EMPTY = json_rpc_quantity_1.Quantity.from(exports.BUFFER_EMPTY, true);
exports.RPCQUANTITY_ZERO = json_rpc_quantity_1.Quantity.from(exports.BUFFER_ZERO);
exports.RPCQUANTITY_ONE = json_rpc_quantity_1.Quantity.from(1n);
exports.RPCQUANTITY_GWEI = json_rpc_quantity_1.Quantity.from(1000000000);
exports.WEI = 1000000000000000000n;
exports.KNOWN_CHAINIDS = new Set([1, 3, 4, 5, 42, 11155111]);
//# sourceMappingURL=constants.js.map

@@ -6,3 +6,3 @@ {

},
"version": "0.4.0",
"version": "0.4.1",
"description": "Utility functions for @ganache packages",

@@ -68,3 +68,3 @@ "author": "David Murdoch <david@trufflesuite.com> (https://davidmurdoch.com)",

},
"gitHead": "9416c03726a44b5ee81f16293a6f7dcc38e6854e"
"gitHead": "6eeaf3addc8a9f9b8c1a7c7625f4bb3f7d488b29"
}

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

export { JsonRpcType } from "./json-rpc-base-types";
export { BaseJsonRpcType as JsonRpcType } from "./json-rpc-base-types";
//# sourceMappingURL=index.d.ts.map
/// <reference types="node" />
export declare const strCache: WeakMap<object, any>;
export declare const bufCache: WeakMap<object, any>;
export declare const toStrings: WeakMap<object, any>;
export declare const toBuffers: WeakMap<object, any>;
import { JsonRpcInputArg } from "./input-parsers";
declare const inspect: unique symbol;
export declare class BaseJsonRpcType<T extends number | bigint | string | Buffer = number | bigint | string | Buffer> {
[Symbol.toStringTag]: string;
protected value: T;
export declare class BaseJsonRpcType {
protected bufferValue: Buffer | null;
private [inspect];
constructor(value: T);
constructor(value: JsonRpcInputArg);
toString(): string | null;
toBuffer(): Buffer;
valueOf(): T | null;
valueOf(): any;
toJSON(): string | null;
isNull(): boolean;
}
export declare type JsonRpcType<T extends number | bigint | string | Buffer> = BaseJsonRpcType<T>;
export {};
//# sourceMappingURL=json-rpc-base-types.d.ts.map
/// <reference types="node" />
import { BaseJsonRpcType } from "./json-rpc-base-types";
export declare type JsonRpcDataInputArg = string | Buffer;
export declare class Data extends BaseJsonRpcType {
private _byteLength?;
constructor(value: string | Buffer, _byteLength?: number);
toString(byteLength?: number): string;
static from<T extends string | Buffer = string | Buffer>(value: T, byteLength?: number): Data;
static Empty: Data;
constructor(value: JsonRpcDataInputArg, _byteLength?: number);
toString(byteLength?: number): string | null;
toBuffer(byteLength?: number): Buffer;
static from(value: JsonRpcDataInputArg, byteLength?: number): Data;
private static stringToFixedByteLength;
private static bufferToFixedByteLength;
static toBuffer(value: JsonRpcDataInputArg, byteLength?: number): Buffer;
static toString(value: JsonRpcDataInputArg, byteLength?: number): string;
}
//# sourceMappingURL=json-rpc-data.d.ts.map
/// <reference types="node" />
import { BaseJsonRpcType } from "./json-rpc-base-types";
import { JsonRpcInputArg } from "./input-parsers";
export declare class Quantity extends BaseJsonRpcType {
static Empty: Quantity;
static Zero: Quantity;
static One: Quantity;
static Gwei: Quantity;
private static ZERO_VALUE_STRING;
_nullable: boolean;
static from(value: number | bigint | string | Buffer, nullable?: boolean): Quantity;
static from(value: JsonRpcInputArg, nullable?: boolean): Quantity;
constructor(value: JsonRpcInputArg, nullable?: boolean);
toString(): string | null;

@@ -11,4 +18,9 @@ toBuffer(): Buffer;

valueOf(): bigint;
private findFirstNonZeroByteIndex;
static toBuffer(value: JsonRpcInputArg, nullable?: boolean): Buffer;
static toString(value: JsonRpcInputArg, nullable?: boolean): string;
static toNumber(value: JsonRpcInputArg, nullable?: boolean): number;
static toBigInt(value: JsonRpcInputArg, nullable?: boolean): bigint;
}
export default Quantity;
//# sourceMappingURL=json-rpc-quantity.d.ts.map
/// <reference types="node" />
import { Data } from "../things/json-rpc/json-rpc-data";
import { Quantity } from "../things/json-rpc/json-rpc-quantity";
export declare const BUFFER_256_ZERO: Buffer;

@@ -10,9 +8,4 @@ export declare const ACCOUNT_ZERO: Buffer;

export declare const BUFFER_8_ZERO: Buffer;
export declare const DATA_EMPTY: Data;
export declare const RPCQUANTITY_EMPTY: Quantity;
export declare const RPCQUANTITY_ZERO: Quantity;
export declare const RPCQUANTITY_ONE: Quantity;
export declare const RPCQUANTITY_GWEI: Quantity;
export declare const WEI: 1000000000000000000n;
export declare const KNOWN_CHAINIDS: Set<number>;
//# sourceMappingURL=constants.d.ts.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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