@findeth/abi
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -13,2 +13,4 @@ "use strict"; | ||
var _boolean = require("./boolean"); | ||
var _bytes = require("./bytes"); | ||
@@ -65,2 +67,3 @@ | ||
address: { | ||
dynamic: false, | ||
encode: _address.encodeAddress, | ||
@@ -74,2 +77,7 @@ decode: _address.decodeAddress | ||
}, | ||
bool: { | ||
dynamic: false, | ||
encode: _boolean.encodeBoolean, | ||
decode: _boolean.decodeBoolean | ||
}, | ||
bytes: { | ||
@@ -81,2 +89,3 @@ dynamic: true, | ||
fixedBytes: { | ||
dynamic: false, | ||
encode: _fixedBytes.encodeFixedBytes, | ||
@@ -86,2 +95,3 @@ decode: _fixedBytes.decodeFixedBytes | ||
number: { | ||
dynamic: false, | ||
encode: _number.encodeNumber, | ||
@@ -106,3 +116,3 @@ decode: _number.decodeNumber | ||
if ((0, _number.isNumber)(type) || type === 'bool') { | ||
if ((0, _number.isNumber)(type)) { | ||
return parsers.number; | ||
@@ -109,0 +119,0 @@ } |
@@ -10,4 +10,12 @@ "use strict"; | ||
const getBooleanValue = value => { | ||
if (typeof value === 'string') { | ||
return value === 'true' || value === 'yes'; | ||
} | ||
return value; | ||
}; | ||
const encodeBoolean = (buffer, value) => { | ||
return (0, _number.encodeNumber)(buffer, value ? 1n : 0n, 'uint256'); | ||
return (0, _number.encodeNumber)(buffer, getBooleanValue(value) ? 1 : 0, 'uint256'); | ||
}; | ||
@@ -14,0 +22,0 @@ |
@@ -7,11 +7,11 @@ "use strict"; | ||
var _contract = require("./contract"); | ||
var _input = require("./input"); | ||
Object.keys(_contract).forEach(function (key) { | ||
Object.keys(_input).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _contract[key]) return; | ||
if (key in exports && exports[key] === _input[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _contract[key]; | ||
return _input[key]; | ||
} | ||
@@ -46,2 +46,15 @@ }); | ||
}); | ||
var _parser = require("./parser"); | ||
Object.keys(_parser).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _parser[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _parser[key]; | ||
} | ||
}); | ||
}); | ||
//# sourceMappingURL=index.js.map |
import { concat, toBuffer, toNumber } from '../utils'; | ||
import { decodeAddress, encodeAddress } from './address'; | ||
import { decodeBoolean, encodeBoolean } from './boolean'; | ||
import { decodeBytes, encodeBytes } from './bytes'; | ||
@@ -38,2 +39,3 @@ import { decodeFixedBytes, encodeFixedBytes, isFixedBytes } from './fixed-bytes'; | ||
address: { | ||
dynamic: false, | ||
encode: encodeAddress, | ||
@@ -47,2 +49,7 @@ decode: decodeAddress | ||
}, | ||
bool: { | ||
dynamic: false, | ||
encode: encodeBoolean, | ||
decode: decodeBoolean | ||
}, | ||
bytes: { | ||
@@ -54,2 +61,3 @@ dynamic: true, | ||
fixedBytes: { | ||
dynamic: false, | ||
encode: encodeFixedBytes, | ||
@@ -59,2 +67,3 @@ decode: decodeFixedBytes | ||
number: { | ||
dynamic: false, | ||
encode: encodeNumber, | ||
@@ -78,3 +87,3 @@ decode: decodeNumber | ||
if (isNumber(type) || type === 'bool') { | ||
if (isNumber(type)) { | ||
return parsers.number; | ||
@@ -81,0 +90,0 @@ } |
import { decodeNumber, encodeNumber } from './number'; | ||
const getBooleanValue = value => { | ||
if (typeof value === 'string') { | ||
return value === 'true' || value === 'yes'; | ||
} | ||
return value; | ||
}; | ||
export const encodeBoolean = (buffer, value) => { | ||
return encodeNumber(buffer, value ? 1n : 0n, 'uint256'); | ||
return encodeNumber(buffer, getBooleanValue(value) ? 1 : 0, 'uint256'); | ||
}; | ||
@@ -5,0 +14,0 @@ export const decodeBoolean = (value, buffer) => { |
@@ -1,4 +0,5 @@ | ||
export * from './contract'; | ||
export * from './input'; | ||
export * from './mappings'; | ||
export * from './narrow'; | ||
export * from './parser'; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@findeth/abi", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "A tiny Solidity ABI encoder and decoder", | ||
@@ -72,3 +72,3 @@ "author": "Maarten Zuidhoorn <maarten@zuidhoorn.com>", | ||
"eslint": "^7.2.0", | ||
"eslint-config-prettier": "^7.2.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
"eslint-plugin-import": "^2.21.2", | ||
@@ -75,0 +75,0 @@ "eslint-plugin-jest": "^24.1.0", |
import { pack, unpack } from './parsers/array'; | ||
import { Type, TypeMapper, Narrow } from './types'; | ||
import { Type, TypeMapper, Narrow, InputTypeMap } from './types'; | ||
@@ -7,3 +7,6 @@ /** | ||
*/ | ||
export const encode = <T extends Array<Type | string>>(types: Narrow<T>, values: TypeMapper<T>): Uint8Array => { | ||
export const encode = <T extends Array<Type | string>>( | ||
types: Narrow<T>, | ||
values: TypeMapper<T, InputTypeMap> | ||
): Uint8Array => { | ||
return pack(new Uint8Array(0), values, types); | ||
@@ -10,0 +13,0 @@ }; |
@@ -1,3 +0,3 @@ | ||
import { toHex } from '../utils'; | ||
import { encodeAddress } from './address'; | ||
import { fromHex, toHex } from '../utils'; | ||
import { decodeAddress, encodeAddress } from './address'; | ||
@@ -11,1 +11,8 @@ describe('encodeAddress', () => { | ||
}); | ||
describe('decodeAddress', () => { | ||
it('encodes an address', () => { | ||
const buffer = fromHex('0000000000000000000000004bbeeb066ed09b7aed07bf39eee0460dfa261520'); | ||
expect(decodeAddress(buffer, buffer, 'address')).toBe('0x4bbeeb066ed09b7aed07bf39eee0460dfa261520'); | ||
}); | ||
}); |
@@ -0,5 +1,5 @@ | ||
import { DecodeFunction, EncodeFunction } from '../types'; | ||
import { concat, fromHex, stripPrefix, toHex } from '../utils'; | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
export const encodeAddress: EncodeFunction = (buffer: Uint8Array, value: string): Uint8Array => { | ||
export const encodeAddress: EncodeFunction<string> = (buffer: Uint8Array, value: string): Uint8Array => { | ||
if (value.length !== 42) { | ||
@@ -14,5 +14,5 @@ throw new Error('Invalid address length'); | ||
export const decodeAddress: DecodeFunction = (value: Uint8Array): string => { | ||
export const decodeAddress: DecodeFunction<string> = (value: Uint8Array): string => { | ||
const addressBuffer = value.subarray(-20); | ||
return `0x${toHex(addressBuffer)}`; | ||
}; |
@@ -43,2 +43,11 @@ import { fromHex, toHex } from '../utils'; | ||
describe('decodeArray', () => { | ||
it('decodes an array', () => { | ||
const value = fromHex('0000000000000000000000000000000000000000000000000000000000000020'); | ||
const buffer = fromHex( | ||
'00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000003666f6f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000036261720000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000362617a0000000000000000000000000000000000000000000000000000000000' | ||
); | ||
expect(decodeArray(value, buffer, 'string[]')).toStrictEqual(['foo', 'bar', 'baz']); | ||
}); | ||
it('throws if a type is not an array type', () => { | ||
@@ -45,0 +54,0 @@ const buffer = fromHex( |
@@ -0,7 +1,8 @@ | ||
import { DecodeFunction, EncodeFunction } from '../types'; | ||
import { concat, toBuffer, toNumber } from '../utils'; | ||
import { decodeAddress, encodeAddress } from './address'; | ||
import { decodeBoolean, encodeBoolean } from './boolean'; | ||
import { decodeBytes, encodeBytes } from './bytes'; | ||
import { decodeFixedBytes, encodeFixedBytes, isFixedBytes } from './fixed-bytes'; | ||
import { decodeNumber, encodeNumber, isNumber } from './number'; | ||
import { DecodeFunction, EncodeFunction, Parser } from './parser'; | ||
import { decodeString, encodeString } from './string'; | ||
@@ -31,3 +32,7 @@ | ||
export const encodeArray: EncodeFunction = (buffer: Uint8Array, values: unknown[], type: string): Uint8Array => { | ||
export const encodeArray: EncodeFunction<unknown[]> = ( | ||
buffer: Uint8Array, | ||
values: unknown[], | ||
type: string | ||
): Uint8Array => { | ||
if (!isArray(type)) { | ||
@@ -45,3 +50,7 @@ throw new Error('Invalid type: type is not array'); | ||
export const decodeArray: DecodeFunction = (value: Uint8Array, buffer: Uint8Array, type: string): unknown[] => { | ||
export const decodeArray: DecodeFunction<unknown[]> = ( | ||
value: Uint8Array, | ||
buffer: Uint8Array, | ||
type: string | ||
): unknown[] => { | ||
if (!isArray(type)) { | ||
@@ -64,4 +73,5 @@ throw new Error('Invalid type: type is not array'); | ||
*/ | ||
const parsers: Record<string, Parser> = { | ||
const parsers = { | ||
address: { | ||
dynamic: false, | ||
encode: encodeAddress, | ||
@@ -75,2 +85,7 @@ decode: decodeAddress | ||
}, | ||
bool: { | ||
dynamic: false, | ||
encode: encodeBoolean, | ||
decode: decodeBoolean | ||
}, | ||
bytes: { | ||
@@ -82,2 +97,3 @@ dynamic: true, | ||
fixedBytes: { | ||
dynamic: false, | ||
encode: encodeFixedBytes, | ||
@@ -87,2 +103,3 @@ decode: decodeFixedBytes | ||
number: { | ||
dynamic: false, | ||
encode: encodeNumber, | ||
@@ -98,2 +115,4 @@ decode: decodeNumber | ||
export type ParserType = keyof typeof parsers; | ||
/** | ||
@@ -105,7 +124,9 @@ * Get a parser for a type. Throws an error if the parser could not be found. | ||
*/ | ||
export const getParser = (type: string): Parser => { | ||
if (parsers[type]) { | ||
return parsers[type]; | ||
export const getParser = (type: string) => { | ||
if (parsers[type as ParserType]) { | ||
return parsers[type as ParserType]; | ||
} | ||
// TODO: Figure out type issues | ||
// bytes[n] | ||
@@ -117,3 +138,3 @@ if (isFixedBytes(type)) { | ||
// u?int[n], bool | ||
if (isNumber(type) || type === 'bool') { | ||
if (isNumber(type)) { | ||
return parsers.number; | ||
@@ -150,3 +171,3 @@ } | ||
*/ | ||
export const pack = (buffer: Uint8Array, values: unknown[], types: Readonly<string[]>): Uint8Array => { | ||
export const pack = (buffer: Uint8Array, values: unknown[], types: string[]): Uint8Array => { | ||
const { | ||
@@ -159,3 +180,4 @@ staticBuffer: packedStaticBuffer, | ||
const parser = getParser(type); | ||
const value = values[index]; | ||
// TODO: Solve type issue | ||
const value = values[index] as any; | ||
@@ -214,3 +236,3 @@ if (parser.dynamic) { | ||
export const unpack = (buffer: Uint8Array, types: Readonly<string[]>): unknown[] => { | ||
export const unpack = (buffer: Uint8Array, types: string[]): unknown[] => { | ||
const iterator = iterate(buffer, 32); | ||
@@ -217,0 +239,0 @@ |
@@ -1,6 +0,6 @@ | ||
import { fromHex, toHex } from '../utils/buffer'; | ||
import { fromHex, toHex } from '../utils'; | ||
import { decodeBoolean, encodeBoolean } from './boolean'; | ||
describe('encodeBoolean', () => { | ||
it('encodes a boolean as a uint256 number', () => { | ||
it('encodes a boolean', () => { | ||
expect(toHex(encodeBoolean(new Uint8Array(0), true, 'bool'))).toBe( | ||
@@ -13,6 +13,21 @@ '0000000000000000000000000000000000000000000000000000000000000001' | ||
}); | ||
it('encodes a string', () => { | ||
expect(toHex(encodeBoolean(new Uint8Array(0), 'true', 'bool'))).toBe( | ||
'0000000000000000000000000000000000000000000000000000000000000001' | ||
); | ||
expect(toHex(encodeBoolean(new Uint8Array(0), 'yes', 'bool'))).toBe( | ||
'0000000000000000000000000000000000000000000000000000000000000001' | ||
); | ||
expect(toHex(encodeBoolean(new Uint8Array(0), 'false', 'bool'))).toBe( | ||
'0000000000000000000000000000000000000000000000000000000000000000' | ||
); | ||
expect(toHex(encodeBoolean(new Uint8Array(0), 'foo bar', 'bool'))).toBe( | ||
'0000000000000000000000000000000000000000000000000000000000000000' | ||
); | ||
}); | ||
}); | ||
describe('decodeBoolean', () => { | ||
it('decodes a boolean as a uint256 number', () => { | ||
it('decodes a boolean', () => { | ||
const trueValue = fromHex('0000000000000000000000000000000000000000000000000000000000000001'); | ||
@@ -19,0 +34,0 @@ expect(decodeBoolean(trueValue, trueValue, 'bool')).toBe(true); |
@@ -0,10 +1,18 @@ | ||
import { BooleanInput, DecodeFunction, EncodeFunction } from '../types'; | ||
import { decodeNumber, encodeNumber } from './number'; | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
export const encodeBoolean: EncodeFunction = (buffer: Uint8Array, value: boolean | string): Uint8Array => { | ||
return encodeNumber(buffer, value ? 1n : 0n, 'uint256'); | ||
const getBooleanValue = (value: BooleanInput): boolean => { | ||
if (typeof value === 'string') { | ||
return value === 'true' || value === 'yes'; | ||
} | ||
return value; | ||
}; | ||
export const decodeBoolean: DecodeFunction = (value: Uint8Array, buffer: Uint8Array): boolean => { | ||
export const encodeBoolean: EncodeFunction<BooleanInput> = (buffer: Uint8Array, value: BooleanInput): Uint8Array => { | ||
return encodeNumber(buffer, getBooleanValue(value) ? 1 : 0, 'uint256'); | ||
}; | ||
export const decodeBoolean: DecodeFunction<boolean> = (value: Uint8Array, buffer: Uint8Array): boolean => { | ||
return decodeNumber(value, buffer, 'uint256') === 1n; | ||
}; |
@@ -0,5 +1,5 @@ | ||
import { BytesInput, DecodeFunction, EncodeFunction } from '../types'; | ||
import { addPadding, concat, toBuffer, toNumber } from '../utils'; | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
export const encodeBytes: EncodeFunction = (buffer: Uint8Array, value: string | Uint8Array): Uint8Array => { | ||
export const encodeBytes: EncodeFunction<BytesInput> = (buffer: Uint8Array, value: BytesInput): Uint8Array => { | ||
const bufferValue = toBuffer(value); | ||
@@ -11,4 +11,3 @@ const paddedSize = Math.ceil(bufferValue.byteLength / 32) * 32; | ||
// TODO: This may not work properly yet | ||
export const decodeBytes: DecodeFunction = (value: Uint8Array, buffer: Uint8Array): Uint8Array => { | ||
export const decodeBytes: DecodeFunction<Uint8Array> = (value: Uint8Array, buffer: Uint8Array): Uint8Array => { | ||
const pointer = Number(toNumber(value.subarray(0, 32))); | ||
@@ -15,0 +14,0 @@ const length = Number(toNumber(buffer.subarray(pointer, pointer + 32))); |
@@ -1,2 +0,2 @@ | ||
import { fromHex, toHex } from '../utils/buffer'; | ||
import { fromHex, toHex } from '../utils'; | ||
import { decodeFixedBytes, encodeFixedBytes } from './fixed-bytes'; | ||
@@ -3,0 +3,0 @@ |
@@ -0,3 +1,3 @@ | ||
import { BytesInput, DecodeFunction, EncodeFunction } from '../types'; | ||
import { addPadding, concat, toBuffer } from '../utils'; | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
@@ -32,5 +32,5 @@ const BYTES_REGEX = /^bytes([0-9]{1,2})$/; | ||
export const encodeFixedBytes: EncodeFunction = ( | ||
export const encodeFixedBytes: EncodeFunction<BytesInput> = ( | ||
buffer: Uint8Array, | ||
value: string | Uint8Array, | ||
value: BytesInput, | ||
type: string | ||
@@ -48,3 +48,7 @@ ): Uint8Array => { | ||
export const decodeFixedBytes: DecodeFunction = (value: Uint8Array, _: Uint8Array, type: string): Uint8Array => { | ||
export const decodeFixedBytes: DecodeFunction<Uint8Array> = ( | ||
value: Uint8Array, | ||
_: Uint8Array, | ||
type: string | ||
): Uint8Array => { | ||
const length = getByteLength(type); | ||
@@ -51,0 +55,0 @@ |
@@ -0,3 +1,3 @@ | ||
import { DecodeFunction, EncodeFunction, BytesInput } from '../types'; | ||
import { decodeFixedBytes, encodeFixedBytes } from './fixed-bytes'; | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
export const encodeFunction: EncodeFunction = (buffer: Uint8Array, value: string | Uint8Array): Uint8Array => { | ||
export const encodeFunction: EncodeFunction<BytesInput> = (buffer: Uint8Array, value: BytesInput): Uint8Array => { | ||
return encodeFixedBytes(buffer, value, 'bytes24'); | ||
@@ -23,4 +23,4 @@ }; | ||
*/ | ||
export const decodeFunction: DecodeFunction = (value: Uint8Array, buffer: Uint8Array): Uint8Array => { | ||
export const decodeFunction: DecodeFunction<Uint8Array> = (value: Uint8Array, buffer: Uint8Array): Uint8Array => { | ||
return decodeFixedBytes(value, buffer, 'bytes24'); | ||
}; |
@@ -0,3 +1,3 @@ | ||
import { NumberInput, DecodeFunction, EncodeFunction } from '../types'; | ||
import { concat, toBuffer, toNumber, fromTwosComplement, toTwosComplement } from '../utils'; | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
@@ -31,3 +31,3 @@ const NUMBER_REGEX = /^u?int([0-9]*)?$/; | ||
const asNumber = (value: string | bigint): bigint => { | ||
const asNumber = (value: NumberInput): bigint => { | ||
if (typeof value === 'bigint') { | ||
@@ -40,3 +40,7 @@ return value; | ||
export const encodeNumber: EncodeFunction = (buffer: Uint8Array, value: string | bigint, type: string): Uint8Array => { | ||
export const encodeNumber: EncodeFunction<NumberInput> = ( | ||
buffer: Uint8Array, | ||
value: NumberInput, | ||
type: string | ||
): Uint8Array => { | ||
const numberValue = asNumber(value); | ||
@@ -55,3 +59,3 @@ | ||
export const decodeNumber: DecodeFunction = (value: Uint8Array, _, type: string): bigint => { | ||
export const decodeNumber: DecodeFunction<bigint> = (value: Uint8Array, _, type: string): bigint => { | ||
if (isSigned(type)) { | ||
@@ -58,0 +62,0 @@ return fromTwosComplement(value); |
@@ -0,6 +1,6 @@ | ||
import { DecodeFunction, EncodeFunction } from '../types'; | ||
import { fromUtf8, toUtf8 } from '../utils'; | ||
import { decodeBytes, encodeBytes } from './bytes'; | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
export const encodeString: EncodeFunction = (buffer: Uint8Array, value: string): Uint8Array => { | ||
export const encodeString: EncodeFunction<string> = (buffer: Uint8Array, value: string): Uint8Array => { | ||
const bufferValue = fromUtf8(value); | ||
@@ -10,4 +10,4 @@ return encodeBytes(buffer, bufferValue, 'bytes'); | ||
export const decodeString: DecodeFunction = (value: Uint8Array, buffer: Uint8Array): string => { | ||
export const decodeString: DecodeFunction<string> = (value: Uint8Array, buffer: Uint8Array): string => { | ||
return toUtf8(decodeBytes(value, buffer, 'string')); | ||
}; |
@@ -1,3 +0,4 @@ | ||
export * from './contract'; | ||
export * from './input'; | ||
export * from './mappings'; | ||
export * from './narrow'; | ||
export * from './parser'; |
@@ -0,1 +1,9 @@ | ||
import type { decodeAddress, encodeAddress } from '../parsers/address'; | ||
import type { decodeBoolean, encodeBoolean } from '../parsers/boolean'; | ||
import type { decodeBytes, encodeBytes } from '../parsers/bytes'; | ||
import type { decodeFunction, encodeFunction } from '../parsers/function'; | ||
import type { decodeNumber, encodeNumber } from '../parsers/number'; | ||
import type { decodeString, encodeString } from '../parsers/string'; | ||
import type { DecodeFunction, EncodeFunction } from './parser'; | ||
// prettier-ignore | ||
@@ -10,14 +18,19 @@ type ByteLength = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32; | ||
interface StaticTypes { | ||
address: string; | ||
bool: boolean; | ||
bytes: Uint8Array; | ||
function: Uint8Array; | ||
string: string; | ||
} | ||
export type Type = keyof OutputTypeMap; | ||
export type TypeMapper<I extends any[], T = OutputTypeMap> = Mapper<T, I>; | ||
type TypeMapWithoutArrays = StaticTypes & | ||
DynamicType<Bytes, Uint8Array> & | ||
DynamicType<Integer, bigint> & | ||
DynamicType<UnsignedInteger, bigint>; | ||
/** | ||
* An object type with most possible ABI types, and their respective TypeScript type. Note that some dynamic types, like | ||
* `<type>[<length>]` and `fixed<M>x<N>` are not supported, and `unknown` is used instead. | ||
*/ | ||
export type OutputTypeMap = WithArrayTypes< | ||
GenericTypeMap< | ||
typeof decodeAddress, | ||
typeof decodeBoolean, | ||
typeof decodeBytes, | ||
typeof decodeFunction, | ||
typeof decodeNumber, | ||
typeof decodeString | ||
> | ||
>; | ||
@@ -27,10 +40,37 @@ /** | ||
* `<type>[<length>]` and `fixed<M>x<N>` are not supported, and `unknown` is used instead. | ||
* | ||
* Accepts multiple input types for certain ABI types, like strings, bytes, numbers. | ||
*/ | ||
export type TypeMap = TypeMapWithoutArrays & | ||
{ | ||
[K in keyof TypeMapWithoutArrays as `${K}[]`]: Array<TypeMapWithoutArrays[K]>; | ||
}; | ||
export type InputTypeMap = WithArrayTypes< | ||
GenericTypeMap< | ||
typeof encodeAddress, | ||
typeof encodeBoolean, | ||
typeof encodeBytes, | ||
typeof encodeFunction, | ||
typeof encodeNumber, | ||
typeof encodeString | ||
> | ||
>; | ||
export type Type = keyof TypeMap; | ||
export type TypeMapper<I extends any[]> = Mapper<TypeMap, I>; | ||
/** | ||
* Generic type map which is used to generate the input and output type map. | ||
*/ | ||
type GenericTypeMap< | ||
AddressFunction, | ||
BooleanFunction, | ||
BytesFunction, | ||
FunctionFunction, | ||
NumberFunction, | ||
StringFunction | ||
> = { | ||
address: ExtractGeneric<AddressFunction>; | ||
bool: ExtractGeneric<BooleanFunction>; | ||
bytes: ExtractGeneric<BytesFunction>; | ||
function: ExtractGeneric<FunctionFunction>; | ||
int: ExtractGeneric<NumberFunction>; | ||
string: ExtractGeneric<StringFunction>; | ||
uint: ExtractGeneric<NumberFunction>; | ||
} & DynamicType<Bytes, ExtractGeneric<BytesFunction>> & | ||
DynamicType<Integer, ExtractGeneric<NumberFunction>> & | ||
DynamicType<UnsignedInteger, ExtractGeneric<NumberFunction>>; | ||
@@ -50,1 +90,14 @@ /** | ||
}; | ||
/** | ||
* Helper type that adds an array type for each of the specified keys and types. | ||
*/ | ||
type WithArrayTypes<T> = T & | ||
{ | ||
[K in keyof T as `${string & K}[]`]: Array<T[K]>; | ||
}; | ||
/** | ||
* Helper type that extracts the input or output from an EncodeFunction or DecodeFunction. | ||
*/ | ||
type ExtractGeneric<T> = T extends DecodeFunction<infer O> ? O : T extends EncodeFunction<infer I> ? I : never; |
@@ -1,9 +0,9 @@ | ||
import { TypeMapper, Narrow } from './types'; | ||
import { TypeMapper, Narrow, InputTypeMap } from './types'; | ||
/** | ||
* Encode the data with the provided types. | ||
*/ | ||
export declare const encode: <T extends string[]>(types: import("./types").Try<T, [], (T extends [] ? [] : never) | (T extends import("./types").Narrowable ? T : never) | { [K in keyof T]: T[K] extends Function ? T[K] : (T[K] extends [] ? [] : never) | (T[K] extends import("./types").Narrowable ? T[K] : never) | { [K_1 in keyof T[K]]: T[K][K_1] extends Function ? T[K][K_1] : (T[K][K_1] extends [] ? [] : never) | (T[K][K_1] extends import("./types").Narrowable ? T[K][K_1] : never) | { [K_2 in keyof T[K][K_1]]: T[K][K_1][K_2] extends Function ? T[K][K_1][K_2] : (T[K][K_1][K_2] extends [] ? [] : never) | (T[K][K_1][K_2] extends import("./types").Narrowable ? T[K][K_1][K_2] : never) | { [K_3 in keyof T[K][K_1][K_2]]: T[K][K_1][K_2][K_3] extends Function ? T[K][K_1][K_2][K_3] : (T[K][K_1][K_2][K_3] extends [] ? [] : never) | (T[K][K_1][K_2][K_3] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3] : never) | { [K_4 in keyof T[K][K_1][K_2][K_3]]: T[K][K_1][K_2][K_3][K_4] extends Function ? T[K][K_1][K_2][K_3][K_4] : (T[K][K_1][K_2][K_3][K_4] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4] : never) | { [K_5 in keyof T[K][K_1][K_2][K_3][K_4]]: T[K][K_1][K_2][K_3][K_4][K_5] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5] : (T[K][K_1][K_2][K_3][K_4][K_5] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5] : never) | { [K_6 in keyof T[K][K_1][K_2][K_3][K_4][K_5]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6] : never) | { [K_7 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : never) | { [K_8 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : never) | { [K_9 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : never) | { [K_10 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] : never) | any; }; }; }; }; }; }; }; }; }; }; }>, values: { [K_11 in keyof T]: T[K_11] extends T[number] ? import("./types").TypeMap[T[K_11]] : unknown; }) => Uint8Array; | ||
export declare const encode: <T extends string[]>(types: import("./types").Try<T, [], (T extends [] ? [] : never) | (T extends import("./types").Narrowable ? T : never) | { [K in keyof T]: T[K] extends Function ? T[K] : (T[K] extends [] ? [] : never) | (T[K] extends import("./types").Narrowable ? T[K] : never) | { [K_1 in keyof T[K]]: T[K][K_1] extends Function ? T[K][K_1] : (T[K][K_1] extends [] ? [] : never) | (T[K][K_1] extends import("./types").Narrowable ? T[K][K_1] : never) | { [K_2 in keyof T[K][K_1]]: T[K][K_1][K_2] extends Function ? T[K][K_1][K_2] : (T[K][K_1][K_2] extends [] ? [] : never) | (T[K][K_1][K_2] extends import("./types").Narrowable ? T[K][K_1][K_2] : never) | { [K_3 in keyof T[K][K_1][K_2]]: T[K][K_1][K_2][K_3] extends Function ? T[K][K_1][K_2][K_3] : (T[K][K_1][K_2][K_3] extends [] ? [] : never) | (T[K][K_1][K_2][K_3] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3] : never) | { [K_4 in keyof T[K][K_1][K_2][K_3]]: T[K][K_1][K_2][K_3][K_4] extends Function ? T[K][K_1][K_2][K_3][K_4] : (T[K][K_1][K_2][K_3][K_4] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4] : never) | { [K_5 in keyof T[K][K_1][K_2][K_3][K_4]]: T[K][K_1][K_2][K_3][K_4][K_5] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5] : (T[K][K_1][K_2][K_3][K_4][K_5] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5] : never) | { [K_6 in keyof T[K][K_1][K_2][K_3][K_4][K_5]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6] : never) | { [K_7 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : never) | { [K_8 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : never) | { [K_9 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : never) | { [K_10 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] : never) | any; }; }; }; }; }; }; }; }; }; }; }>, values: { [K_11 in keyof T]: T[K_11] extends T[number] ? InputTypeMap[T[K_11]] : unknown; }) => Uint8Array; | ||
/** | ||
* Decode the data with the provided types. | ||
*/ | ||
export declare const decode: <T extends string[]>(types: import("./types").Try<T, [], (T extends [] ? [] : never) | (T extends import("./types").Narrowable ? T : never) | { [K in keyof T]: T[K] extends Function ? T[K] : (T[K] extends [] ? [] : never) | (T[K] extends import("./types").Narrowable ? T[K] : never) | { [K_1 in keyof T[K]]: T[K][K_1] extends Function ? T[K][K_1] : (T[K][K_1] extends [] ? [] : never) | (T[K][K_1] extends import("./types").Narrowable ? T[K][K_1] : never) | { [K_2 in keyof T[K][K_1]]: T[K][K_1][K_2] extends Function ? T[K][K_1][K_2] : (T[K][K_1][K_2] extends [] ? [] : never) | (T[K][K_1][K_2] extends import("./types").Narrowable ? T[K][K_1][K_2] : never) | { [K_3 in keyof T[K][K_1][K_2]]: T[K][K_1][K_2][K_3] extends Function ? T[K][K_1][K_2][K_3] : (T[K][K_1][K_2][K_3] extends [] ? [] : never) | (T[K][K_1][K_2][K_3] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3] : never) | { [K_4 in keyof T[K][K_1][K_2][K_3]]: T[K][K_1][K_2][K_3][K_4] extends Function ? T[K][K_1][K_2][K_3][K_4] : (T[K][K_1][K_2][K_3][K_4] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4] : never) | { [K_5 in keyof T[K][K_1][K_2][K_3][K_4]]: T[K][K_1][K_2][K_3][K_4][K_5] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5] : (T[K][K_1][K_2][K_3][K_4][K_5] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5] : never) | { [K_6 in keyof T[K][K_1][K_2][K_3][K_4][K_5]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6] : never) | { [K_7 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : never) | { [K_8 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : never) | { [K_9 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : never) | { [K_10 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] : never) | any; }; }; }; }; }; }; }; }; }; }; }>, buffer: Uint8Array) => { [K_11 in keyof T]: T[K_11] extends T[number] ? import("./types").TypeMap[T[K_11]] : unknown; }; | ||
export declare const decode: <T extends string[]>(types: import("./types").Try<T, [], (T extends [] ? [] : never) | (T extends import("./types").Narrowable ? T : never) | { [K in keyof T]: T[K] extends Function ? T[K] : (T[K] extends [] ? [] : never) | (T[K] extends import("./types").Narrowable ? T[K] : never) | { [K_1 in keyof T[K]]: T[K][K_1] extends Function ? T[K][K_1] : (T[K][K_1] extends [] ? [] : never) | (T[K][K_1] extends import("./types").Narrowable ? T[K][K_1] : never) | { [K_2 in keyof T[K][K_1]]: T[K][K_1][K_2] extends Function ? T[K][K_1][K_2] : (T[K][K_1][K_2] extends [] ? [] : never) | (T[K][K_1][K_2] extends import("./types").Narrowable ? T[K][K_1][K_2] : never) | { [K_3 in keyof T[K][K_1][K_2]]: T[K][K_1][K_2][K_3] extends Function ? T[K][K_1][K_2][K_3] : (T[K][K_1][K_2][K_3] extends [] ? [] : never) | (T[K][K_1][K_2][K_3] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3] : never) | { [K_4 in keyof T[K][K_1][K_2][K_3]]: T[K][K_1][K_2][K_3][K_4] extends Function ? T[K][K_1][K_2][K_3][K_4] : (T[K][K_1][K_2][K_3][K_4] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4] : never) | { [K_5 in keyof T[K][K_1][K_2][K_3][K_4]]: T[K][K_1][K_2][K_3][K_4][K_5] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5] : (T[K][K_1][K_2][K_3][K_4][K_5] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5] : never) | { [K_6 in keyof T[K][K_1][K_2][K_3][K_4][K_5]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6] : never) | { [K_7 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : never) | { [K_8 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : never) | { [K_9 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : never) | { [K_10 in keyof T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]]: T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends Function ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] : (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends [] ? [] : never) | (T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends import("./types").Narrowable ? T[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] : never) | any; }; }; }; }; }; }; }; }; }; }; }>, buffer: Uint8Array) => { [K_11 in keyof T]: T[K_11] extends T[number] ? import("./types").OutputTypeMap[T[K_11]] : unknown; }; |
@@ -1,3 +0,3 @@ | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
export declare const encodeAddress: EncodeFunction; | ||
export declare const decodeAddress: DecodeFunction; | ||
import { DecodeFunction, EncodeFunction } from '../types'; | ||
export declare const encodeAddress: EncodeFunction<string>; | ||
export declare const decodeAddress: DecodeFunction<string>; |
@@ -1,2 +0,2 @@ | ||
import { DecodeFunction, EncodeFunction, Parser } from './parser'; | ||
import { DecodeFunction, EncodeFunction } from '../types'; | ||
/** | ||
@@ -16,5 +16,46 @@ * Check if a type is an array type. | ||
export declare const getType: (type: string) => string; | ||
export declare const encodeArray: EncodeFunction; | ||
export declare const decodeArray: DecodeFunction; | ||
export declare const encodeArray: EncodeFunction<unknown[]>; | ||
export declare const decodeArray: DecodeFunction<unknown[]>; | ||
/** | ||
* All available parsers. | ||
*/ | ||
declare const parsers: { | ||
address: { | ||
dynamic: boolean; | ||
encode: EncodeFunction<string>; | ||
decode: DecodeFunction<string>; | ||
}; | ||
array: { | ||
dynamic: boolean; | ||
encode: EncodeFunction<unknown[]>; | ||
decode: DecodeFunction<unknown[]>; | ||
}; | ||
bool: { | ||
dynamic: boolean; | ||
encode: EncodeFunction<import("../types").BooleanInput>; | ||
decode: DecodeFunction<boolean>; | ||
}; | ||
bytes: { | ||
dynamic: boolean; | ||
encode: EncodeFunction<import("../types").BytesInput>; | ||
decode: DecodeFunction<Uint8Array>; | ||
}; | ||
fixedBytes: { | ||
dynamic: boolean; | ||
encode: EncodeFunction<import("../types").BytesInput>; | ||
decode: DecodeFunction<Uint8Array>; | ||
}; | ||
number: { | ||
dynamic: boolean; | ||
encode: EncodeFunction<import("../types").NumberInput>; | ||
decode: DecodeFunction<bigint>; | ||
}; | ||
string: { | ||
dynamic: boolean; | ||
encode: EncodeFunction<string>; | ||
decode: DecodeFunction<string>; | ||
}; | ||
}; | ||
export declare type ParserType = keyof typeof parsers; | ||
/** | ||
* Get a parser for a type. Throws an error if the parser could not be found. | ||
@@ -25,3 +66,23 @@ * | ||
*/ | ||
export declare const getParser: (type: string) => Parser; | ||
export declare const getParser: (type: string) => { | ||
dynamic: boolean; | ||
encode: EncodeFunction<string>; | ||
decode: DecodeFunction<string>; | ||
} | { | ||
dynamic: boolean; | ||
encode: EncodeFunction<unknown[]>; | ||
decode: DecodeFunction<unknown[]>; | ||
} | { | ||
dynamic: boolean; | ||
encode: EncodeFunction<import("../types").BooleanInput>; | ||
decode: DecodeFunction<boolean>; | ||
} | { | ||
dynamic: boolean; | ||
encode: EncodeFunction<import("../types").BytesInput>; | ||
decode: DecodeFunction<Uint8Array>; | ||
} | { | ||
dynamic: boolean; | ||
encode: EncodeFunction<import("../types").NumberInput>; | ||
decode: DecodeFunction<bigint>; | ||
}; | ||
export declare type UpdateFunction = (buffer: Uint8Array) => Uint8Array; | ||
@@ -40,3 +101,3 @@ /** | ||
*/ | ||
export declare const pack: (buffer: Uint8Array, values: unknown[], types: Readonly<string[]>) => Uint8Array; | ||
export declare const pack: (buffer: Uint8Array, values: unknown[], types: string[]) => Uint8Array; | ||
/** | ||
@@ -50,2 +111,3 @@ * Iterate over a `Buffer` with provided `chunkSize`. | ||
export declare function iterate(buffer: Uint8Array, chunkSize: number): Generator<Uint8Array, Uint8Array, void>; | ||
export declare const unpack: (buffer: Uint8Array, types: Readonly<string[]>) => unknown[]; | ||
export declare const unpack: (buffer: Uint8Array, types: string[]) => unknown[]; | ||
export {}; |
@@ -1,3 +0,3 @@ | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
export declare const encodeBoolean: EncodeFunction; | ||
export declare const decodeBoolean: DecodeFunction; | ||
import { BooleanInput, DecodeFunction, EncodeFunction } from '../types'; | ||
export declare const encodeBoolean: EncodeFunction<BooleanInput>; | ||
export declare const decodeBoolean: DecodeFunction<boolean>; |
@@ -1,3 +0,3 @@ | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
export declare const encodeBytes: EncodeFunction; | ||
export declare const decodeBytes: DecodeFunction; | ||
import { BytesInput, DecodeFunction, EncodeFunction } from '../types'; | ||
export declare const encodeBytes: EncodeFunction<BytesInput>; | ||
export declare const decodeBytes: DecodeFunction<Uint8Array>; |
@@ -1,2 +0,2 @@ | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
import { BytesInput, DecodeFunction, EncodeFunction } from '../types'; | ||
export declare const isFixedBytes: (type: string) => boolean; | ||
@@ -11,3 +11,3 @@ /** | ||
export declare const getByteLength: (type: string) => number; | ||
export declare const encodeFixedBytes: EncodeFunction; | ||
export declare const decodeFixedBytes: DecodeFunction; | ||
export declare const encodeFixedBytes: EncodeFunction<BytesInput>; | ||
export declare const decodeFixedBytes: DecodeFunction<Uint8Array>; |
@@ -1,2 +0,2 @@ | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
import { DecodeFunction, EncodeFunction, BytesInput } from '../types'; | ||
/** | ||
@@ -9,3 +9,3 @@ * Encode a function type to a Buffer. This is equivalent to the `bytes24` type. | ||
*/ | ||
export declare const encodeFunction: EncodeFunction; | ||
export declare const encodeFunction: EncodeFunction<BytesInput>; | ||
/** | ||
@@ -18,2 +18,2 @@ * Decode a function type from a Buffer. This is equivalent to the `bytes24` type. | ||
*/ | ||
export declare const decodeFunction: DecodeFunction; | ||
export declare const decodeFunction: DecodeFunction<Uint8Array>; |
@@ -1,6 +0,6 @@ | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
import { NumberInput, DecodeFunction, EncodeFunction } from '../types'; | ||
export declare const isNumber: (type: string) => boolean; | ||
export declare const getBitLength: (type: string) => number; | ||
export declare const inRange: (value: bigint, type: string) => boolean; | ||
export declare const encodeNumber: EncodeFunction; | ||
export declare const decodeNumber: DecodeFunction; | ||
export declare const encodeNumber: EncodeFunction<NumberInput>; | ||
export declare const decodeNumber: DecodeFunction<bigint>; |
@@ -1,3 +0,3 @@ | ||
import { DecodeFunction, EncodeFunction } from './parser'; | ||
export declare const encodeString: EncodeFunction; | ||
export declare const decodeString: DecodeFunction; | ||
import { DecodeFunction, EncodeFunction } from '../types'; | ||
export declare const encodeString: EncodeFunction<string>; | ||
export declare const decodeString: DecodeFunction<string>; |
@@ -1,3 +0,4 @@ | ||
export * from './contract'; | ||
export * from './input'; | ||
export * from './mappings'; | ||
export * from './narrow'; | ||
export * from './parser'; |
@@ -0,1 +1,8 @@ | ||
import type { decodeAddress, encodeAddress } from '../parsers/address'; | ||
import type { decodeBoolean, encodeBoolean } from '../parsers/boolean'; | ||
import type { decodeBytes, encodeBytes } from '../parsers/bytes'; | ||
import type { decodeFunction, encodeFunction } from '../parsers/function'; | ||
import type { decodeNumber, encodeNumber } from '../parsers/number'; | ||
import type { decodeString, encodeString } from '../parsers/string'; | ||
import type { DecodeFunction, EncodeFunction } from './parser'; | ||
declare type ByteLength = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32; | ||
@@ -6,10 +13,4 @@ declare type IntegerLength = 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 | 88 | 96 | 104 | 112 | 120 | 128 | 136 | 144 | 152 | 160 | 168 | 176 | 184 | 192 | 200 | 208 | 216 | 224 | 232 | 240 | 248 | 256; | ||
declare type UnsignedInteger = `uint${IntegerLength}`; | ||
interface StaticTypes { | ||
address: string; | ||
bool: boolean; | ||
bytes: Uint8Array; | ||
function: Uint8Array; | ||
string: string; | ||
} | ||
declare type TypeMapWithoutArrays = StaticTypes & DynamicType<Bytes, Uint8Array> & DynamicType<Integer, bigint> & DynamicType<UnsignedInteger, bigint>; | ||
export declare type Type = keyof OutputTypeMap; | ||
export declare type TypeMapper<I extends any[], T = OutputTypeMap> = Mapper<T, I>; | ||
/** | ||
@@ -19,8 +20,23 @@ * An object type with most possible ABI types, and their respective TypeScript type. Note that some dynamic types, like | ||
*/ | ||
export declare type TypeMap = TypeMapWithoutArrays & { | ||
[K in keyof TypeMapWithoutArrays as `${K}[]`]: Array<TypeMapWithoutArrays[K]>; | ||
}; | ||
export declare type Type = keyof TypeMap; | ||
export declare type TypeMapper<I extends any[]> = Mapper<TypeMap, I>; | ||
export declare type OutputTypeMap = WithArrayTypes<GenericTypeMap<typeof decodeAddress, typeof decodeBoolean, typeof decodeBytes, typeof decodeFunction, typeof decodeNumber, typeof decodeString>>; | ||
/** | ||
* An object type with most possible ABI types, and their respective TypeScript type. Note that some dynamic types, like | ||
* `<type>[<length>]` and `fixed<M>x<N>` are not supported, and `unknown` is used instead. | ||
* | ||
* Accepts multiple input types for certain ABI types, like strings, bytes, numbers. | ||
*/ | ||
export declare type InputTypeMap = WithArrayTypes<GenericTypeMap<typeof encodeAddress, typeof encodeBoolean, typeof encodeBytes, typeof encodeFunction, typeof encodeNumber, typeof encodeString>>; | ||
/** | ||
* Generic type map which is used to generate the input and output type map. | ||
*/ | ||
declare type GenericTypeMap<AddressFunction, BooleanFunction, BytesFunction, FunctionFunction, NumberFunction, StringFunction> = { | ||
address: ExtractGeneric<AddressFunction>; | ||
bool: ExtractGeneric<BooleanFunction>; | ||
bytes: ExtractGeneric<BytesFunction>; | ||
function: ExtractGeneric<FunctionFunction>; | ||
int: ExtractGeneric<NumberFunction>; | ||
string: ExtractGeneric<StringFunction>; | ||
uint: ExtractGeneric<NumberFunction>; | ||
} & DynamicType<Bytes, ExtractGeneric<BytesFunction>> & DynamicType<Integer, ExtractGeneric<NumberFunction>> & DynamicType<UnsignedInteger, ExtractGeneric<NumberFunction>>; | ||
/** | ||
* Helper type to generate an object type from a union. | ||
@@ -37,2 +53,12 @@ */ | ||
}; | ||
/** | ||
* Helper type that adds an array type for each of the specified keys and types. | ||
*/ | ||
declare type WithArrayTypes<T> = T & { | ||
[K in keyof T as `${string & K}[]`]: Array<T[K]>; | ||
}; | ||
/** | ||
* Helper type that extracts the input or output from an EncodeFunction or DecodeFunction. | ||
*/ | ||
declare type ExtractGeneric<T> = T extends DecodeFunction<infer O> ? O : T extends EncodeFunction<infer I> ? I : never; | ||
export {}; |
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
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
175843
123
2389