@onrail-xyz/evm 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,264 @@
1
+ import { type Address } from "viem";
2
+ import type { Identity, RoUint8Array, RoTuple, RoNeTuple, RoPair, HeadTail, Opts, OptionalArg } from "@onrail-xyz/utils";
3
+ import type { Conversion, Layout, Struct, DeriveType, Item, NumSizeToPrimitive, WithCustom, UnwrapSingleton } from "@onrail-xyz/binary-layout";
4
+ import { paddingItem, uintItem, switchItem } from "@onrail-xyz/binary-layout";
5
+ import type { KindWithAtomic } from "@onrail-xyz/amount";
6
+ import { type AmountOr } from "@onrail-xyz/common";
7
+ export declare const wordSize = 32;
8
+ export declare const addressSize = 20;
9
+ type SignedSlot = Readonly<{
10
+ binary: "int";
11
+ size: typeof wordSize;
12
+ }>;
13
+ export type SignedSlotItem<I extends Item> = WithCustom<SignedSlot, DeriveType<I>>;
14
+ type SlotPadding = ReturnType<typeof paddingItem>;
15
+ type PaddedStruct<I extends Item> = {
16
+ readonly _padding: SlotPadding;
17
+ readonly item: I;
18
+ };
19
+ type ZeroPaddedItem<I extends Item> = UnwrapSingleton<PaddedStruct<I>>;
20
+ type PaddedItem<I extends Item> = I extends {
21
+ binary: "int";
22
+ } ? SignedSlotItem<I> : ZeroPaddedItem<I>;
23
+ declare const paddedStruct: <const S extends Struct>(layout: S) => {
24
+ readonly _padding: {
25
+ readonly binary: "bytes";
26
+ readonly fixed: Uint8Array<ArrayBuffer>;
27
+ };
28
+ } & S;
29
+ export type PaddedSlotLayout<T extends Struct | Item> = T extends Item ? PaddedItem<T> : T extends Struct ? ReturnType<typeof paddedStruct<T>> : never;
30
+ export declare const paddedSlotLayout: <const T extends Struct | Item>(layoutOrItem: T) => PaddedSlotLayout<T>;
31
+ export type PaddedFields<S extends Struct> = {
32
+ readonly [K in keyof S]: PaddedSlotLayout<S[K]>;
33
+ };
34
+ export declare const paddedFields: <const S extends Struct>(layout: S) => PaddedFields<S>;
35
+ export declare const uint256Item: {
36
+ readonly binary: "uint";
37
+ readonly size: 32;
38
+ };
39
+ export declare const evmAmountItem: <const K extends KindWithAtomic | undefined = undefined>(...kind: OptionalArg<K>) => import("@onrail-xyz/common").AmountItem<32, K>;
40
+ export declare const deadlineItem: import("@onrail-xyz/binary-layout").TimestampItem<{
41
+ readonly size: 32;
42
+ readonly saturateAs: "infinity";
43
+ }>;
44
+ export type Deadline = DeriveType<typeof deadlineItem>;
45
+ export declare const evmTimestampItem: import("@onrail-xyz/binary-layout").TimestampItem<{
46
+ readonly size: 32;
47
+ }>;
48
+ export declare const addressConversion: Conversion<RoUint8Array, Address>;
49
+ declare const _addressItem: {
50
+ readonly binary: "bytes";
51
+ readonly size: 20;
52
+ readonly custom: Conversion<RoUint8Array, `0x${string}`>;
53
+ };
54
+ export interface AddressItem extends Identity<typeof _addressItem> {
55
+ }
56
+ export declare const addressItem: AddressItem;
57
+ export declare const abiAddressItem: Readonly<{
58
+ binary: "bytes";
59
+ layout: PaddedStruct<AddressItem>;
60
+ custom: Conversion<{
61
+ readonly item: `0x${string}`;
62
+ }, `0x${string}`>;
63
+ }>;
64
+ export declare const abiBoolItem: Readonly<{
65
+ binary: "bytes";
66
+ layout: PaddedStruct<{
67
+ readonly binary: "uint";
68
+ readonly size: 1;
69
+ readonly custom: {
70
+ readonly to: (encoded: number) => boolean;
71
+ readonly from: (value: boolean) => number;
72
+ };
73
+ }>;
74
+ custom: Conversion<{
75
+ readonly item: boolean;
76
+ }, boolean>;
77
+ }>;
78
+ declare const rightPaddedBytesLayout: <N extends number>(size: N) => {
79
+ readonly value: {
80
+ readonly binary: "bytes";
81
+ readonly size: N;
82
+ };
83
+ readonly _padding: {
84
+ readonly binary: "bytes";
85
+ readonly fixed: Uint8Array<ArrayBuffer>;
86
+ };
87
+ };
88
+ export type BytesNItem<N extends number> = UnwrapSingleton<ReturnType<typeof rightPaddedBytesLayout<N>>>;
89
+ export declare const bytesNItem: <N extends number>(size: N) => BytesNItem<N>;
90
+ export declare const signatureSize = 65;
91
+ export declare const signatureItem: {
92
+ readonly binary: "bytes";
93
+ readonly size: 65;
94
+ };
95
+ export declare const signatureLayout: {
96
+ readonly r: {
97
+ readonly binary: "bytes";
98
+ readonly size: 32;
99
+ };
100
+ readonly s: {
101
+ readonly binary: "bytes";
102
+ readonly size: 32;
103
+ };
104
+ readonly v: {
105
+ readonly binary: "uint";
106
+ readonly size: 1;
107
+ };
108
+ };
109
+ export declare const abiSignatureLayout: {
110
+ readonly v: Readonly<{
111
+ binary: "bytes";
112
+ layout: PaddedStruct<{
113
+ readonly binary: "uint";
114
+ readonly size: 1;
115
+ }>;
116
+ custom: Conversion<{
117
+ readonly item: number;
118
+ }, number>;
119
+ }>;
120
+ readonly r: {
121
+ readonly binary: "bytes";
122
+ readonly size: 32;
123
+ };
124
+ readonly s: {
125
+ readonly binary: "bytes";
126
+ readonly size: 32;
127
+ };
128
+ };
129
+ export declare const abiSignatureItem: Readonly<{
130
+ binary: "bytes";
131
+ layout: {
132
+ readonly v: Readonly<{
133
+ binary: "bytes";
134
+ layout: PaddedStruct<{
135
+ readonly binary: "uint";
136
+ readonly size: 1;
137
+ }>;
138
+ custom: Conversion<{
139
+ readonly item: number;
140
+ }, number>;
141
+ }>;
142
+ readonly r: {
143
+ readonly binary: "bytes";
144
+ readonly size: 32;
145
+ };
146
+ readonly s: {
147
+ readonly binary: "bytes";
148
+ readonly size: 32;
149
+ };
150
+ };
151
+ custom: Conversion<{
152
+ readonly v: number;
153
+ readonly r: RoUint8Array;
154
+ readonly s: RoUint8Array;
155
+ }, RoUint8Array>;
156
+ }>;
157
+ export declare const compactSignatureItem: Readonly<{
158
+ binary: "bytes";
159
+ layout: {
160
+ readonly r: {
161
+ readonly binary: "bytes";
162
+ readonly size: 32;
163
+ };
164
+ readonly yParityAndS: {
165
+ readonly binary: "bytes";
166
+ readonly size: 32;
167
+ };
168
+ };
169
+ custom: Conversion<{
170
+ readonly r: RoUint8Array;
171
+ readonly yParityAndS: RoUint8Array;
172
+ }, RoUint8Array>;
173
+ }>;
174
+ export declare const selectorLength = 4;
175
+ export declare const selectorOf: (funcSig: string) => Uint8Array<ArrayBufferLike>;
176
+ export declare const selectorItem: (funcSig: string) => {
177
+ readonly binary: "bytes";
178
+ readonly fixed: Uint8Array<ArrayBufferLike>;
179
+ };
180
+ export declare const selectorLayout: (funcSig: string) => <const L extends Struct>(layout: L) => {
181
+ readonly _selector: {
182
+ readonly binary: "bytes";
183
+ readonly fixed: Uint8Array<ArrayBufferLike>;
184
+ };
185
+ } & L;
186
+ export type AbiEncoded<L extends Layout | undefined> = L extends Layout ? DeriveType<L> : RoUint8Array;
187
+ declare const abiEncodedLayout: (offset: number) => {
188
+ readonly offset: {
189
+ readonly binary: "uint";
190
+ readonly size: 32;
191
+ readonly fixed: bigint;
192
+ };
193
+ readonly lengthPadding: {
194
+ readonly binary: "bytes";
195
+ readonly fixed: Uint8Array<ArrayBuffer>;
196
+ };
197
+ readonly item: {
198
+ readonly binary: "bytes";
199
+ readonly size: {
200
+ readonly binary: "uint";
201
+ readonly size: 4;
202
+ };
203
+ };
204
+ readonly postPadding: {
205
+ readonly binary: "bytes";
206
+ };
207
+ };
208
+ export type AbiEncodedBytesItem<L extends Layout | undefined> = WithCustom<ReturnType<typeof abiEncodedLayout>, AbiEncoded<L>>;
209
+ type AbiEncodedOpts = Opts<{
210
+ layout: Layout;
211
+ offset: number;
212
+ }>;
213
+ type ContentOf<O extends AbiEncodedOpts> = "layout" extends keyof O ? O["layout"] : undefined;
214
+ export declare const abiEncodedBytesItem: <const O extends AbiEncodedOpts = {}>(...[opts]: OptionalArg<O>) => AbiEncodedBytesItem<ContentOf<O>>;
215
+ export type AbiParam<N extends string = string, T extends string = string, I extends Item = Item, V = unknown> = {
216
+ readonly name: N;
217
+ readonly type: T;
218
+ readonly item: I;
219
+ readonly __valueType?: V;
220
+ };
221
+ type ValueOf<I extends Item, Stated extends boolean, V> = Stated extends true ? V : DeriveType<I>;
222
+ type ParamFactory<Stated extends boolean, V> = <N extends string, T extends string, const I extends Item>(name: N, type: T, item: I) => AbiParam<N, T, I, ValueOf<I, Stated, V>>;
223
+ type AbiParamFn = ParamFactory<false, "ignored"> & {
224
+ <V>(): ParamFactory<true, V>;
225
+ };
226
+ export declare const abiParam: AbiParamFn;
227
+ export type AbiParamValue<A extends AbiParam> = A extends {
228
+ __valueType?: infer V;
229
+ } ? V : never;
230
+ type ParamOfFactory<Stated extends boolean, V> = <T extends string, const I extends Item>(type: T, item: I) => <N extends string>(name: N) => AbiParam<N, T, I, ValueOf<I, Stated, V>>;
231
+ type ParamOfFn = ParamOfFactory<false, "ignored"> & {
232
+ <V>(): ParamOfFactory<true, V>;
233
+ };
234
+ export declare const paramOf: ParamOfFn;
235
+ export declare const addressParam: <N extends string>(name: N) => AbiParam<N, "address", AddressItem, `0x${string}`>;
236
+ export declare const amountParam: <N extends string, T extends string, const K extends KindWithAtomic | undefined = undefined, S extends number = typeof wordSize>(name: N, type: T, ...args: OptionalArg<K, OptionalArg<S>>) => AbiParam<N, T, import("@onrail-xyz/common").AmountItem<S, K>, AmountOr<K, NumSizeToPrimitive<S>>>;
237
+ export type DynamicAbiParam<N extends string, L extends Layout | undefined> = AbiParam<N, "bytes", AbiEncodedBytesItem<L>, AbiEncoded<L>> & {
238
+ readonly dynamic: {
239
+ readonly layout: L;
240
+ };
241
+ };
242
+ export declare const trailingBytesParam: <N extends string, const L extends Layout | undefined = undefined>(name: N, ...layout: OptionalArg<L>) => DynamicAbiParam<N, L>;
243
+ export type SigVariant<S extends string = string, P extends Struct = Struct> = RoPair<S, P>;
244
+ export type SigVariants = RoNeTuple<SigVariant>;
245
+ export declare const signatureOf: (name: string, params: RoTuple<AbiParam>) => string;
246
+ export type SigNameOf<S extends string> = S extends `${infer N}(${string}` ? N : never;
247
+ export declare const sigNameOf: <S extends string>(funcSig: S) => SigNameOf<S>;
248
+ type JoinTypes<P extends RoTuple<AbiParam>> = P extends HeadTail<P, infer H, infer R> ? R extends readonly [] ? H["type"] : `${H["type"]},${JoinTypes<R>}` : "";
249
+ export type SigVariantOf<N extends string, P extends RoTuple<AbiParam>> = SigVariant<`${N}(${JoinTypes<P>})`, {
250
+ readonly [E in P[number] as E["name"]]: E["item"];
251
+ }>;
252
+ export declare const sigVariant: <N extends string, const P extends RoTuple<AbiParam>>(name: N, ...params: P) => SigVariantOf<N, P>;
253
+ type SwitchRows = Parameters<typeof switchItem>[2];
254
+ type SigRows<IS extends number, V extends SigVariants> = {
255
+ readonly [K in keyof V]: V[K] extends SigVariant<infer S, infer P> ? readonly [NumSizeToPrimitive<IS>, SigNameOf<S>, PaddedFields<P>] : never;
256
+ };
257
+ type SigRowsBound<IS extends number, V extends SigVariants> = SigRows<IS, V> extends infer R extends SwitchRows ? R : never;
258
+ export type SigSwitcher<IS extends number, T extends string> = <const V extends SigVariants>(variants: V) => ReturnType<typeof switchItem<T, ReturnType<typeof uintItem<IS, {}>>, SigRowsBound<IS, V>, {}>>;
259
+ export declare const sigSwitchItem: <IS extends number, T extends string>(idSize: IS, tag: T) => SigSwitcher<IS, T>;
260
+ export type MappingKey = bigint | number | boolean | Address | RoUint8Array;
261
+ export declare const mappingSlot: (key: MappingKey, declareSlot: bigint) => bigint;
262
+ export declare const keccakSlot: (slot: bigint) => bigint;
263
+ export {};
264
+ //# sourceMappingURL=layouting.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layouting.d.ts","sourceRoot":"","sources":["../src/layouting.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAmB,MAAM,MAAM,CAAC;AACrD,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAC/B,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAExF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,IAA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AC,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,GAChE,KAAK;CACV,CAAC;AAIF,KAAK,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC,SAAS,WAAW,IACxD,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC,SAAS,MAAM,CAAC,SAAS,UAAU,GAAG,CAAC,GAAG,KAAK,CAAC;AAKhE,MAAM,MAAM,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IACzD,CAAC,KAAK,CAAC,CAAC,SAAS,WAAW,EAAE,QAAQ,EAAE,CAAC,KACvC,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,EAAE,UAAU,CAAC,OAAO,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,YAAY,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAiBnG,eAAO,MAAM,aAAa,GACxB,EAAE,SAAS,MAAM,EACjB,CAAC,SAAU,MAAM,UACT,EAAE,OAAO,CAAC,KAAG,WAAW,CAAC,EAAE,EAAE,CAAC,CAMU,CAAC;AAQnD,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,YAAY,CAAC;AAE5E,eAAO,MAAM,WAAW,QAAS,UAAU,eAAe,MAAM,KAAG,MA4BlE,CAAC;AAEF,eAAO,MAAM,UAAU,SAAU,MAAM,KAAG,MACmB,CAAC"}
@@ -0,0 +1,238 @@
1
+ import { checksumAddress } from "viem";
2
+ import { utf8, hex, bignum, assertDistinct, argOf } from "@onrail-xyz/utils";
3
+ import { serialize, deserialize, calcStaticSize, paddingItem, unwrapSingleton, isItem, uintItem, switchItem, numSizeToPrimitive, timestampItem, withCustom, hasFixed, customOf, boolItem } from "@onrail-xyz/binary-layout";
4
+ import { amountItem, hashItem } from "@onrail-xyz/common";
5
+ import { keccak256 } from "./hashing.js";
6
+ // ---- Padding ----
7
+ export const wordSize = 32;
8
+ export const addressSize = 20;
9
+ //sub-word layouts are left-padded onto one slot - the ABI's alignment for every static type but
10
+ // bytesN, which `bytesNItem` spells at word width, right-padded; whole-word ones (a signature's
11
+ // three words) are already slot-aligned and pass with empty padding
12
+ const slotPadding = (layout) => {
13
+ const size = calcStaticSize(layout);
14
+ if (size === null || (size > wordSize && size % wordSize !== 0))
15
+ throw new Error("layout must have a static size of at most one word or of whole words");
16
+ return paddingItem(size > wordSize ? 0 : wordSize - size);
17
+ };
18
+ const signedSlotLayout = { binary: "int", size: wordSize };
19
+ const signedSlotItem = (item) => {
20
+ const size = calcStaticSize(item);
21
+ const bound = 1n << BigInt(size * 8 - 1);
22
+ const checkBound = (wide) => {
23
+ if (wide < -bound || wide >= bound)
24
+ throw new Error(`${wide} does not fit a signed ${size * 8}-bit slot`);
25
+ return wide;
26
+ };
27
+ const narrow = (word) => numSizeToPrimitive(checkBound(word), size);
28
+ const widen = (value) => checkBound(BigInt(value));
29
+ //the item's own conversion speaks its declared width, so the widening wraps it on both sides
30
+ // rather than composing through `withCustom`, which only appends
31
+ const own = customOf(item);
32
+ return withCustom(signedSlotLayout, own === undefined
33
+ ? { to: narrow, from: widen }
34
+ : { to: (word) => own.to(narrow(word)),
35
+ from: (value) => widen(own.from(value)) });
36
+ };
37
+ //a slot surfaces exactly the item's value, so an item that surfaces none - one of fixed value,
38
+ // `as` included, since `as` carries `fixed` by construction - has no slot form: it would leave
39
+ // the padded struct with nothing to unwrap to
40
+ const paddedItem = (item) => {
41
+ if (calcStaticSize(item) === null || hasFixed(item))
42
+ throw new Error("a slot item must be statically sized and surface a value");
43
+ return (item.binary === "int"
44
+ ? signedSlotItem(item)
45
+ : unwrapSingleton({ _padding: slotPadding(item), item }));
46
+ };
47
+ //a generated wire-only field leads with an underscore - `_padding`, `_selector` - which keeps
48
+ // it out of the namespace a spec's own fields are spelled in
49
+ const paddedStruct = (layout) => ({ _padding: slotPadding(layout), ...layout });
50
+ export const paddedSlotLayout = (layoutOrItem) => (isItem(layoutOrItem)
51
+ ? paddedItem(layoutOrItem)
52
+ : paddedStruct(layoutOrItem));
53
+ //every field on a slot of its own: the ABI's static parameter list, EIP-712's encodeData
54
+ export const paddedFields = (layout) => Object.fromEntries(Object.entries(layout).map(([name, field]) => [name, paddedSlotLayout(field)]));
55
+ // ---- Value items ----
56
+ export const uint256Item = { binary: "uint", size: wordSize };
57
+ //an amount on a word; narrower amounts are `amountItem(size, kind)` as anywhere else
58
+ export const evmAmountItem = (...kind) => amountItem(wordSize, ...kind);
59
+ //a maxed out deadline is the "never expires" idiom (EIP-2612, EIP-3009), and anything else out
60
+ // that far is not an intended instant either, so the whole high end resolves to the same sentinel
61
+ export const deadlineItem = timestampItem({ size: wordSize, saturateAs: "infinity" });
62
+ export const evmTimestampItem = timestampItem({ size: wordSize });
63
+ //annotated, not `satisfies`: only an annotation gives the value the `Conversion` reference as
64
+ // its type, and that reference is what survives into every item embedding it.
65
+ // The surfaced spelling is EIP-55, which is what viem's own decoders return: a lowercase one
66
+ // would make the same word decode to two strings that compare unequal
67
+ export const addressConversion = {
68
+ to: (encoded) => checksumAddress(hex.encode(encoded, true)),
69
+ from: (addr) => hex.decode(addr),
70
+ };
71
+ const _addressItem = {
72
+ binary: "bytes",
73
+ size: addressSize,
74
+ custom: addressConversion,
75
+ };
76
+ export const addressItem = _addressItem;
77
+ //an address and a bool on their ABI words
78
+ export const abiAddressItem = paddedSlotLayout(addressItem);
79
+ export const abiBoolItem = paddedSlotLayout(boolItem());
80
+ //the ABI's `bytesN`, the one static type it pads on the right: spelled at word width, where a
81
+ // bare bytes item on a slot (an address, say) is left-padded like everything else
82
+ const rightPaddedBytesLayout = (size) => ({
83
+ value: { binary: "bytes", size },
84
+ _padding: paddingItem(wordSize - size),
85
+ });
86
+ export const bytesNItem = (size) => {
87
+ if (!Number.isInteger(size) || size < 1 || size > wordSize)
88
+ throw new Error(`bytes${size} is not an ABI type`);
89
+ return unwrapSingleton(rightPaddedBytesLayout(size));
90
+ };
91
+ // ---- Signatures ----
92
+ export const signatureSize = 65;
93
+ export const signatureItem = { binary: "bytes", size: signatureSize };
94
+ const vItem = { binary: "uint", size: 1 };
95
+ //v is the recovery id in its legacy spelling, 27 + yParity
96
+ const vBase = 27;
97
+ const yParityBit = 0x80;
98
+ export const signatureLayout = { r: hashItem, s: hashItem, v: vItem };
99
+ export const abiSignatureLayout = { v: paddedSlotLayout(vItem), r: hashItem, s: hashItem };
100
+ export const abiSignatureItem = withCustom(abiSignatureLayout, {
101
+ to: (words) => serialize(signatureLayout, words),
102
+ from: (signature) => deserialize(signatureLayout, signature),
103
+ });
104
+ export const compactSignatureItem = withCustom({ r: hashItem, yParityAndS: hashItem }, {
105
+ to: ({ r, yParityAndS }) => {
106
+ const s = Uint8Array.from(yParityAndS);
107
+ s[0] = yParityAndS[0] & ~yParityBit;
108
+ const yParity = yParityAndS[0] >= yParityBit ? 1 : 0;
109
+ return serialize(signatureLayout, { r, s, v: vBase + yParity });
110
+ },
111
+ from: (signature) => {
112
+ const { r, s, v } = deserialize(signatureLayout, signature);
113
+ //the form has one bit for the parity: v must be that bit's legacy spelling, and s must
114
+ // leave the bit free, as a canonical (low) s does
115
+ if (v !== vBase && v !== vBase + 1)
116
+ throw new Error(`v must be ${vBase} or ${vBase + 1}, got ${v}`);
117
+ if (s[0] >= yParityBit)
118
+ throw new Error("s has its top bit set (not a low s), so it has no compact form");
119
+ const yParityAndS = Uint8Array.from(s);
120
+ yParityAndS[0] = s[0] | (v - vBase) * yParityBit;
121
+ return { r, yParityAndS };
122
+ },
123
+ });
124
+ // ---- Function selectors ----
125
+ const funcSigHashPrefix = (funcSig, size) => keccak256(utf8.encode(funcSig)).subarray(0, size);
126
+ export const selectorLength = 4;
127
+ export const selectorOf = (funcSig) => funcSigHashPrefix(funcSig, selectorLength);
128
+ export const selectorItem = (funcSig) => ({ binary: "bytes", fixed: selectorOf(funcSig) });
129
+ export const selectorLayout = (funcSig) => (layout) => ({ _selector: selectorItem(funcSig), ...layout });
130
+ // ---- A single dynamic value ----
131
+ //poor man's abi.encode/decode(bytes): handles a single dynamic value - a trailing argument, or a
132
+ // lone return - whose payload therefore follows the whole head. `offset` is that head's size,
133
+ // its own word included - hence the default, a lone argument - and is the argument's index times
134
+ // the word size only where every preceding argument occupies exactly one slot
135
+ const lengthSize = 4;
136
+ //the payload stays raw bytes in the layout, so its length is at hand to check the padding
137
+ // against; the conversion runs the content layout, if any
138
+ const abiEncodedLayout = (offset) => ({
139
+ offset: { ...uint256Item, fixed: BigInt(offset) },
140
+ lengthPadding: { binary: "bytes", fixed: new Uint8Array(wordSize - lengthSize) },
141
+ item: { binary: "bytes", size: { binary: "uint", size: lengthSize } },
142
+ postPadding: { binary: "bytes" },
143
+ });
144
+ const paddingSizeOf = (dataSize) => (wordSize - dataSize % wordSize) % wordSize;
145
+ export const abiEncodedBytesItem = (...[opts]) => {
146
+ const layout = abiEncodedLayout(opts?.offset ?? wordSize);
147
+ const content = opts?.layout;
148
+ return withCustom(layout, {
149
+ to: (wrapped) => {
150
+ const { item, postPadding } = wrapped;
151
+ if (postPadding.length !== paddingSizeOf(item.length) || postPadding.some(b => b !== 0))
152
+ throw new Error(`abi-encoded bytes: expected ${paddingSizeOf(item.length)} padding bytes`);
153
+ return (content !== undefined ? deserialize(content, item) : item);
154
+ },
155
+ from: (value) => {
156
+ const item = content !== undefined ? serialize(content, value) : value;
157
+ return { item, postPadding: new Uint8Array(paddingSizeOf(item.length)) };
158
+ },
159
+ });
160
+ };
161
+ export const abiParam = ((...args) => args.length === 0
162
+ ? (name, type, item) => ({ name, type, item })
163
+ : { name: args[0], type: args[1], item: args[2] });
164
+ export const paramOf = ((...args) => args.length === 0
165
+ ? (type, item) => (name) => ({ name, type, item })
166
+ : (name) => ({ name, type: args[0], item: args[1] }));
167
+ export const addressParam = paramOf("address", addressItem);
168
+ //the item's width is the domain's range, independent of the ABI type the slot is spelled as: a
169
+ // uint64 amount the contract holds rides a `uint256` word, and the 8-byte item is what rejects
170
+ // a value the contract could not hold. Without a kind the value is what that width derives,
171
+ // as the item's own DeriveType would say
172
+ export const amountParam = (name, type, ...args) => abiParam()(name, type, amountItem((args[1] ?? wordSize), argOf(args)));
173
+ //`bytes` as a function's last parameter - raw, or the serialization of `layout`. The item is a
174
+ // placeholder built at offset zero; `contractFromSpec` rebuilds it once the parameter list
175
+ // fixes the head's size, which the type does not depend on
176
+ export const trailingBytesParam = (name, ...layout) => ({
177
+ ...abiParam(name, "bytes", abiEncodedBytesItem({ layout: argOf(layout) })),
178
+ dynamic: { layout: argOf(layout) },
179
+ });
180
+ //the params' names become a struct's keys, so a repeat would fold two params into one field;
181
+ // every params list passes through here, which is where it is caught
182
+ export const signatureOf = (name, params) => {
183
+ assertDistinct(...params.map(p => p.name));
184
+ return `${name}(${params.map(p => p.type).join(",")})`;
185
+ };
186
+ export const sigNameOf = (funcSig) => {
187
+ const open = funcSig.indexOf("(");
188
+ if (open < 1)
189
+ throw new Error(`not a function signature: ${funcSig}`);
190
+ return funcSig.slice(0, open);
191
+ };
192
+ //a variant from its name and params, so an event or error is spelled once
193
+ export const sigVariant = (name, ...params) => [signatureOf(name, params), Object.fromEntries(params.map(p => [p.name, p.item]))];
194
+ const sigPrefixId = (funcSig, size) => {
195
+ if (size < 1 || size > wordSize)
196
+ throw new Error(`signature prefix size must be within [1, ${wordSize}], got ${size}`);
197
+ return numSizeToPrimitive(bignum.fromBytes(funcSigHashPrefix(funcSig, size)), size);
198
+ };
199
+ //a switch over signature-derived ids, decoding the abi-encoded params of the matched variant.
200
+ // The tag surfaces the signature's own name, Solidity's spelling included - a consumer wanting
201
+ // its discriminants to read uniformly folds the case on its own side, where the convention is:
202
+ // custom errors off revert data (idSize 4 - the selector is all the wire carries), events off
203
+ // topic0 concatenated with the remaining topics and the data - so an event's struct is in
204
+ // wire order, indexed params first, which `sigVariant`'s param order is only when they lead
205
+ // the declaration. topic0 carries the whole hash, so events take idSize 32; a shorter prefix
206
+ // would only trade away collision headroom
207
+ export const sigSwitchItem = (idSize, tag) => (variants) => switchItem(tag, uintItem(idSize), variants.map(([funcSig, params]) => [
208
+ sigPrefixId(funcSig, idSize),
209
+ sigNameOf(funcSig),
210
+ paddedFields(params),
211
+ ]));
212
+ export const mappingSlot = (key, declareSlot) => {
213
+ const buf = new Uint8Array(2 * wordSize);
214
+ if (typeof key === "bigint" || typeof key === "number") {
215
+ const int = BigInt(key);
216
+ if (int < -(1n << 255n) || int >= (1n << 256n))
217
+ throw new Error(`mapping key ${int} does not fit a word`);
218
+ buf.set(bignum.toBytes(int < 0n ? int + (1n << 256n) : int, wordSize));
219
+ }
220
+ else if (typeof key === "boolean")
221
+ buf[wordSize - 1] = key ? 1 : 0;
222
+ else if (typeof key === "string") {
223
+ //viem's Address is any `0x${string}`, so a hex-spelled bytesN would otherwise pass for one
224
+ const address = hex.decode(key);
225
+ if (address.length !== addressSize)
226
+ throw new Error(`a string mapping key is an address, got ${address.length} bytes - ` +
227
+ `pass a bytesN key as a Uint8Array`);
228
+ buf.set(address, wordSize - addressSize);
229
+ }
230
+ else {
231
+ if (key.length > wordSize)
232
+ throw new Error("mapping key must not exceed one word");
233
+ buf.set(key, 0);
234
+ }
235
+ buf.set(bignum.toBytes(declareSlot, wordSize), wordSize);
236
+ return bignum.fromBytes(keccak256(buf));
237
+ };
238
+ export const keccakSlot = (slot) => bignum.fromBytes(keccak256(bignum.toBytes(slot, wordSize)));
@@ -0,0 +1,16 @@
1
+ import type { Hex } from "viem";
2
+ import type { RoArray, RoUint8Array } from "@onrail-xyz/utils";
3
+ import type { DeriveType } from "@onrail-xyz/binary-layout";
4
+ import { type SigVariants } from "./layouting.js";
5
+ export type ViemLog = Readonly<{
6
+ topics: RoArray<Hex>;
7
+ data: Hex;
8
+ }>;
9
+ declare const errorSwitchItem: import("./layouting.js").SigSwitcher<4, "error">;
10
+ declare const eventSwitchItem: import("./layouting.js").SigSwitcher<32, "event">;
11
+ export type ParsedError<V extends SigVariants> = DeriveType<ReturnType<typeof errorSwitchItem<V>>>;
12
+ export type ParsedEvent<V extends SigVariants> = DeriveType<ReturnType<typeof eventSwitchItem<V>>>;
13
+ export declare const buildParseError: <const V extends SigVariants>(errorDefs: V) => (revertData: RoUint8Array) => ParsedError<V>;
14
+ export declare const buildParseEvent: <const V extends SigVariants>(eventDefs: V) => (log: ViemLog) => ParsedEvent<V>;
15
+ export {};
16
+ //# sourceMappingURL=parsing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parsing.d.ts","sourceRoot":"","sources":["../src/parsing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAE/D,OAAO,KAAK,EAAE,UAAU,EAAc,MAAM,2BAA2B,CAAC;AAExE,OAAO,EAAE,KAAK,WAAW,EAA2C,MAAM,gBAAgB,CAAC;AAQ3F,MAAM,MAAM,OAAO,GAAG,QAAQ,CAAC;IAAE,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;IAAC,IAAI,EAAE,GAAG,CAAA;CAAE,CAAC,CAAC;AAEpE,QAAA,MAAM,eAAe,kDAAyC,CAAC;AAC/D,QAAA,MAAM,eAAe,mDAAyC,CAAC;AAG/D,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,WAAW,IAC3C,UAAU,CAAC,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACpD,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,WAAW,IAC3C,UAAU,CAAC,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEpD,eAAO,MAAM,eAAe,GAAI,KAAK,CAAC,CAAC,SAAS,WAAW,aAAa,CAAC,kBAEnD,YAAY,KAAG,WAAW,CAAC,CAAC,CAIjD,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,KAAK,CAAC,CAAC,SAAS,WAAW,aAAa,CAAC,WAE1D,OAAO,KAAG,WAAW,CAAC,CAAC,CAQrC,CAAC"}
@@ -0,0 +1,26 @@
1
+ import { bytes, hex, succeeds } from "@onrail-xyz/utils";
2
+ import { deserialize, findVariantByRawId } from "@onrail-xyz/binary-layout";
3
+ import { selectorLength, sigSwitchItem, wordSize } from "./layouting.js";
4
+ const requireKnownSig = (item, prefix, what) => {
5
+ if (!succeeds(() => findVariantByRawId(item, deserialize(item.id, prefix))))
6
+ throw new Error(`unrecognized ${what}: ${hex.encode(prefix, true)}`);
7
+ };
8
+ const errorSwitchItem = sigSwitchItem(selectorLength, "error");
9
+ const eventSwitchItem = sigSwitchItem(wordSize, "event");
10
+ export const buildParseError = (errorDefs) => {
11
+ const item = errorSwitchItem(errorDefs);
12
+ return (revertData) => {
13
+ requireKnownSig(item, revertData.subarray(0, selectorLength), "error selector");
14
+ return deserialize(item, revertData);
15
+ };
16
+ };
17
+ export const buildParseEvent = (eventDefs) => {
18
+ const item = eventSwitchItem(eventDefs);
19
+ return (log) => {
20
+ if (log.topics.length === 0)
21
+ throw new Error("anonymous event: no topic0 to switch on");
22
+ const encoded = bytes.concat(...log.topics.map(t => hex.decode(t)), hex.decode(log.data));
23
+ requireKnownSig(item, encoded.subarray(0, wordSize), "event topic0");
24
+ return deserialize(item, encoded);
25
+ };
26
+ };