@findeth/abi
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -32,2 +32,15 @@ "use strict"; | ||
}); | ||
var _narrow = require("./narrow"); | ||
Object.keys(_narrow).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _narrow[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _narrow[key]; | ||
} | ||
}); | ||
}); | ||
//# sourceMappingURL=index.js.map |
export * from './contract'; | ||
export * from './mappings'; | ||
export * from './narrow'; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@findeth/abi", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "A tiny Solidity ABI encoder and decoder", | ||
@@ -5,0 +5,0 @@ "author": "Maarten Zuidhoorn <maarten@zuidhoorn.com>", |
@@ -6,3 +6,3 @@ import { decode, encode } from './abi'; | ||
it('encodes simple values', () => { | ||
expect(toHex(encode(['uint256', 'uint256'] as const, [12345n, 12345n]))).toBe( | ||
expect(toHex(encode(['uint256', 'uint256'], [12345n, 12345n]))).toBe( | ||
'00000000000000000000000000000000000000000000000000000000000030390000000000000000000000000000000000000000000000000000000000003039' | ||
@@ -13,6 +13,6 @@ ); | ||
it('encodes array values', () => { | ||
expect(toHex(encode(['uint256', 'uint256[]', 'uint256'] as const, [12345n, [67890n, 67890n], 12345n]))).toBe( | ||
expect(toHex(encode(['uint256', 'uint256[]', 'uint256'], [12345n, [67890n, 67890n], 12345n]))).toBe( | ||
'000000000000000000000000000000000000000000000000000000000000303900000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000003039000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000109320000000000000000000000000000000000000000000000000000000000010932' | ||
); | ||
expect(toHex(encode(['string', 'string', 'string'] as const, ['foo', 'bar', 'baz']))).toBe( | ||
expect(toHex(encode(['string', 'string', 'string'], ['foo', 'bar', 'baz']))).toBe( | ||
'000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000003666f6f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000036261720000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000362617a0000000000000000000000000000000000000000000000000000000000' | ||
@@ -25,10 +25,13 @@ ); | ||
toHex( | ||
encode(['uint256', 'uint256[][]', 'uint256'] as const, [ | ||
12345n, | ||
encode( | ||
['uint256', 'uint256[][]', 'uint256'], | ||
[ | ||
[54321n, 12345n], | ||
[67890n, 98760n] | ||
], | ||
12345n | ||
]) | ||
12345n, | ||
[ | ||
[54321n, 12345n], | ||
[67890n, 98760n] | ||
], | ||
12345n | ||
] | ||
) | ||
) | ||
@@ -42,3 +45,3 @@ ).toBe( | ||
const bytes = fromHex('123456789abcdef123456789abcdef123456789abcde'); | ||
expect(toHex(encode(['bytes'] as const, [bytes]))).toBe( | ||
expect(toHex(encode(['bytes'], [bytes]))).toBe( | ||
'00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000016123456789abcdef123456789abcdef123456789abcde00000000000000000000' | ||
@@ -50,3 +53,3 @@ ); | ||
const string = 'foo bar baz qux quux corge'; | ||
expect(toHex(encode(['string'] as const, [string]))).toBe( | ||
expect(toHex(encode(['string'], [string]))).toBe( | ||
'0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a666f6f206261722062617a20717578207175757820636f726765000000000000' | ||
@@ -62,3 +65,3 @@ ); | ||
); | ||
expect(decode(['address', 'uint256'] as const, buffer)).toStrictEqual([ | ||
expect(decode(['address', 'uint256'], buffer)).toStrictEqual([ | ||
'0x6b175474e89094c44da98b954eedeac495271d0f', | ||
@@ -73,7 +76,3 @@ 12345n | ||
); | ||
expect(decode(['uint256', 'uint256[]', 'uint256'] as const, buffer)).toStrictEqual([ | ||
12345n, | ||
[67890n, 67890n], | ||
12345n | ||
]); | ||
expect(decode(['uint256', 'uint256[]', 'uint256'], buffer)).toStrictEqual([12345n, [67890n, 67890n], 12345n]); | ||
expect( | ||
@@ -93,4 +92,4 @@ decode( | ||
); | ||
expect(toHex(decode(['bytes'] as const, buffer)[0])).toBe('123456789abcdef123456789abcdef123456789abcde'); | ||
expect(toHex(decode(['bytes'], buffer)[0])).toBe('123456789abcdef123456789abcdef123456789abcde'); | ||
}); | ||
}); |
import { pack, unpack } from './parsers/array'; | ||
import { Type, TypeMapper } from './types'; | ||
import { Type, TypeMapper, Narrow } from './types'; | ||
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
export const encode = <T extends Readonly<Array<Type | string>>>(types: T, values: TypeMapper<T>): Uint8Array => { | ||
export const encode = <T extends Array<Type | string>>(types: Narrow<T>, values: TypeMapper<T>): Uint8Array => { | ||
return pack(new Uint8Array(0), values, types); | ||
@@ -15,4 +15,4 @@ }; | ||
*/ | ||
export const decode = <T extends Readonly<Array<Type | string>>>(types: T, buffer: Uint8Array): TypeMapper<T> => { | ||
export const decode = <T extends Array<Type | string>>(types: Narrow<T>, buffer: Uint8Array): TypeMapper<T> => { | ||
return unpack(buffer, types) as TypeMapper<T>; | ||
}; |
export * from './contract'; | ||
export * from './mappings'; | ||
export * from './narrow'; |
@@ -33,3 +33,3 @@ // prettier-ignore | ||
export type Type = keyof TypeMap; | ||
export type TypeMapper<I extends Readonly<any[]>> = Mapper<TypeMap, Writable<I>>; | ||
export type TypeMapper<I extends any[]> = Mapper<TypeMap, I>; | ||
@@ -49,6 +49,1 @@ /** | ||
}; | ||
/** | ||
* Helper type that removes a readonly modifier from a field. | ||
*/ | ||
type Writable<T> = { -readonly [P in keyof T]: T[P] }; |
@@ -1,9 +0,9 @@ | ||
import { TypeMapper } from './types'; | ||
import { TypeMapper, Narrow } from './types'; | ||
/** | ||
* Encode the data with the provided types. | ||
*/ | ||
export declare const encode: <T extends readonly string[]>(types: T, values: { [K in keyof { -readonly [P in keyof T]: T[P]; }]: { -readonly [P in keyof T]: T[P]; }[K] extends { -readonly [P in keyof T]: T[P]; }[number] ? import("./types").TypeMap[{ -readonly [P in keyof T]: T[P]; }[K]] : 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] ? import("./types").TypeMap[T[K_11]] : unknown; }) => Uint8Array; | ||
/** | ||
* Decode the data with the provided types. | ||
*/ | ||
export declare const decode: <T extends readonly string[]>(types: T, buffer: Uint8Array) => { [K in keyof { -readonly [P in keyof T]: T[P]; }]: { -readonly [P in keyof T]: T[P]; }[K] extends { -readonly [P in keyof T]: T[P]; }[number] ? import("./types").TypeMap[{ -readonly [P in keyof T]: T[P]; }[K]] : 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").TypeMap[T[K_11]] : unknown; }; |
export * from './contract'; | ||
export * from './mappings'; | ||
export * from './narrow'; |
@@ -22,3 +22,3 @@ 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; | ||
export declare type Type = keyof TypeMap; | ||
export declare type TypeMapper<I extends Readonly<any[]>> = Mapper<TypeMap, Writable<I>>; | ||
export declare type TypeMapper<I extends any[]> = Mapper<TypeMap, I>; | ||
/** | ||
@@ -36,8 +36,2 @@ * Helper type to generate an object type from a union. | ||
}; | ||
/** | ||
* Helper type that removes a readonly modifier from a field. | ||
*/ | ||
declare type Writable<T> = { | ||
-readonly [P in keyof T]: T[P]; | ||
}; | ||
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
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
171009
129
2505