@aztec/foundation 0.17.0 → 0.19.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.
- package/dest/abi/decoder.d.ts.map +1 -1
- package/dest/abi/decoder.js +12 -3
- package/dest/abi/encoder.d.ts.map +1 -1
- package/dest/abi/encoder.js +9 -2
- package/dest/abi/index.d.ts +1 -1
- package/dest/abi/index.d.ts.map +1 -1
- package/dest/abi/index.js +2 -2
- package/dest/abi/selector.d.ts +128 -0
- package/dest/abi/selector.d.ts.map +1 -0
- package/dest/abi/selector.js +179 -0
- package/dest/array/array.d.ts +54 -0
- package/dest/array/array.d.ts.map +1 -0
- package/dest/array/array.js +59 -0
- package/dest/array/index.d.ts +2 -0
- package/dest/array/index.d.ts.map +1 -0
- package/dest/array/index.js +2 -0
- package/dest/bigint-buffer/index.d.ts +6 -0
- package/dest/bigint-buffer/index.d.ts.map +1 -1
- package/dest/bigint-buffer/index.js +13 -1
- package/dest/fifo/memory_fifo.d.ts +2 -1
- package/dest/fifo/memory_fifo.d.ts.map +1 -1
- package/dest/fifo/memory_fifo.js +6 -2
- package/dest/fifo/serial_queue.d.ts +1 -1
- package/dest/fifo/serial_queue.d.ts.map +1 -1
- package/dest/fifo/serial_queue.js +6 -3
- package/dest/json-rpc/server/index.d.ts +1 -1
- package/dest/json-rpc/server/index.d.ts.map +1 -1
- package/dest/json-rpc/server/index.js +2 -2
- package/dest/json-rpc/server/json_rpc_server.d.ts +10 -0
- package/dest/json-rpc/server/json_rpc_server.d.ts.map +1 -1
- package/dest/json-rpc/server/json_rpc_server.js +15 -1
- package/dest/serialize/free_funcs.d.ts +7 -119
- package/dest/serialize/free_funcs.d.ts.map +1 -1
- package/dest/serialize/free_funcs.js +17 -127
- package/dest/serialize/index.d.ts +1 -0
- package/dest/serialize/index.d.ts.map +1 -1
- package/dest/serialize/index.js +2 -1
- package/dest/serialize/serialize.d.ts +167 -0
- package/dest/serialize/serialize.d.ts.map +1 -0
- package/dest/serialize/serialize.js +212 -0
- package/package.json +4 -3
- package/dest/abi/function_selector.d.ts +0 -83
- package/dest/abi/function_selector.d.ts.map +0 -1
- package/dest/abi/function_selector.js +0 -124
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/**
|
|
3
|
+
* For serializing an array of fixed length buffers.
|
|
4
|
+
* TODO move to foundation pkg.
|
|
5
|
+
* @param arr - Array of buffers.
|
|
6
|
+
* @returns The serialized buffers.
|
|
7
|
+
*/
|
|
8
|
+
export declare function serializeBufferArrayToVector(arr: Buffer[]): Buffer;
|
|
9
|
+
/**
|
|
10
|
+
* Helper function for deserializeArrayFromVector.
|
|
11
|
+
*/
|
|
12
|
+
type DeserializeFn<T> = (buf: Buffer, offset: number) => {
|
|
13
|
+
/**
|
|
14
|
+
* The deserialized type.
|
|
15
|
+
*/
|
|
16
|
+
elem: T;
|
|
17
|
+
/**
|
|
18
|
+
* How many bytes to advance by.
|
|
19
|
+
*/
|
|
20
|
+
adv: number;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Deserializes an array from a vector on an element-by-element basis.
|
|
24
|
+
* @param deserialize - A function used to deserialize each element of the vector.
|
|
25
|
+
* @param vector - The vector to deserialize.
|
|
26
|
+
* @param offset - The position in the vector to start deserializing from.
|
|
27
|
+
* @returns Deserialized array and how many bytes we advanced by.
|
|
28
|
+
*
|
|
29
|
+
* TODO: move to foundation pkg.
|
|
30
|
+
*/
|
|
31
|
+
export declare function deserializeArrayFromVector<T>(deserialize: DeserializeFn<T>, vector: Buffer, offset?: number): {
|
|
32
|
+
/**
|
|
33
|
+
* The deserialized array.
|
|
34
|
+
*/
|
|
35
|
+
elem: T[];
|
|
36
|
+
/**
|
|
37
|
+
* How many bytes we advanced by.
|
|
38
|
+
*/
|
|
39
|
+
adv: number;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Cast a uint8 array to a number.
|
|
43
|
+
* @param array - The uint8 array.
|
|
44
|
+
* @returns The number.
|
|
45
|
+
*/
|
|
46
|
+
export declare function uint8ArrayToNum(array: Uint8Array): number;
|
|
47
|
+
/**
|
|
48
|
+
* Serializes a boolean to a buffer.
|
|
49
|
+
* @param value - Value to serialize.
|
|
50
|
+
* @returns The serialized boolean.
|
|
51
|
+
*/
|
|
52
|
+
export declare function boolToBuffer(value: boolean, bufferSize?: number): Buffer;
|
|
53
|
+
/**
|
|
54
|
+
* Deserialize the 256-bit number at address `offset`.
|
|
55
|
+
* @param buf - The buffer.
|
|
56
|
+
* @param offset - The address.
|
|
57
|
+
* @returns The deserialized 256-bit field.
|
|
58
|
+
*/
|
|
59
|
+
export declare function deserializeField(buf: Buffer, offset?: number): {
|
|
60
|
+
elem: Buffer;
|
|
61
|
+
adv: number;
|
|
62
|
+
};
|
|
63
|
+
/** A type that can be written to a buffer. */
|
|
64
|
+
export type Bufferable = boolean | Buffer | number | string | {
|
|
65
|
+
/**
|
|
66
|
+
* Serialize to a buffer of 32 bytes.
|
|
67
|
+
*/
|
|
68
|
+
toBuffer32: () => Buffer;
|
|
69
|
+
} | {
|
|
70
|
+
/**
|
|
71
|
+
* Serialize to a buffer.
|
|
72
|
+
*/
|
|
73
|
+
toBuffer: () => Buffer;
|
|
74
|
+
} | Bufferable[];
|
|
75
|
+
/**
|
|
76
|
+
* Serializes a list of objects contiguously.
|
|
77
|
+
* @param objs - Objects to serialize.
|
|
78
|
+
* @returns A buffer list with the concatenation of all fields.
|
|
79
|
+
*/
|
|
80
|
+
export declare function serializeToBufferArray(...objs: Bufferable[]): Buffer[];
|
|
81
|
+
/**
|
|
82
|
+
* Serializes a list of objects contiguously.
|
|
83
|
+
* @param objs - Objects to serialize.
|
|
84
|
+
* @returns A single buffer with the concatenation of all fields.
|
|
85
|
+
*/
|
|
86
|
+
export declare function serializeToBuffer(...objs: Bufferable[]): Buffer;
|
|
87
|
+
/**
|
|
88
|
+
* Returns a user-friendly JSON representation of an object, showing buffers as hex strings.
|
|
89
|
+
* @param obj - Object to json-stringify.
|
|
90
|
+
* @returns A JSON string.
|
|
91
|
+
*/
|
|
92
|
+
export declare function toFriendlyJSON(obj: object): string;
|
|
93
|
+
/**
|
|
94
|
+
* Serialize a BigInt value into a Buffer of specified width.
|
|
95
|
+
* The function converts the input BigInt into its big-endian representation and stores it in a Buffer of the given width.
|
|
96
|
+
* If the width is not provided, a default value of 32 bytes will be used. It is important to provide an appropriate width
|
|
97
|
+
* to avoid truncation or incorrect serialization of large BigInt values.
|
|
98
|
+
*
|
|
99
|
+
* @param n - The BigInt value to be serialized.
|
|
100
|
+
* @param width - The width (in bytes) of the output Buffer, optional with default value 32.
|
|
101
|
+
* @returns A Buffer containing the serialized BigInt value in big-endian format.
|
|
102
|
+
*/
|
|
103
|
+
export declare function serializeBigInt(n: bigint, width?: number): Buffer;
|
|
104
|
+
/**
|
|
105
|
+
* Deserialize a big integer from a buffer, given an offset and width.
|
|
106
|
+
* Reads the specified number of bytes from the buffer starting at the offset, converts it to a big integer, and returns the deserialized result along with the number of bytes read (advanced).
|
|
107
|
+
*
|
|
108
|
+
* @param buf - The buffer containing the big integer to be deserialized.
|
|
109
|
+
* @param offset - The position in the buffer where the big integer starts. Defaults to 0.
|
|
110
|
+
* @param width - The number of bytes to read from the buffer for the big integer. Defaults to 32.
|
|
111
|
+
* @returns An object containing the deserialized big integer value ('elem') and the number of bytes advanced ('adv').
|
|
112
|
+
*/
|
|
113
|
+
export declare function deserializeBigInt(buf: Buffer, offset?: number, width?: number): {
|
|
114
|
+
elem: bigint;
|
|
115
|
+
adv: number;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Serializes a Date object into a Buffer containing its timestamp as a big integer value.
|
|
119
|
+
* The resulting Buffer has a fixed width of 8 bytes, representing a 64-bit big-endian integer.
|
|
120
|
+
* This function is useful for converting date values into a binary format that can be stored or transmitted easily.
|
|
121
|
+
*
|
|
122
|
+
* @param date - The Date object to be serialized.
|
|
123
|
+
* @returns A Buffer containing the serialized timestamp of the input Date object.
|
|
124
|
+
*/
|
|
125
|
+
export declare function serializeDate(date: Date): Buffer;
|
|
126
|
+
/**
|
|
127
|
+
* Deserialize a boolean value from a given buffer at the specified offset.
|
|
128
|
+
* Reads a single byte at the provided offset in the buffer and returns
|
|
129
|
+
* the deserialized boolean value along with the number of bytes read (adv).
|
|
130
|
+
*
|
|
131
|
+
* @param buf - The buffer containing the serialized boolean value.
|
|
132
|
+
* @param offset - The position in the buffer to start reading the boolean value.
|
|
133
|
+
* @returns An object containing the deserialized boolean value (elem) and the number of bytes read (adv).
|
|
134
|
+
*/
|
|
135
|
+
export declare function deserializeBool(buf: Buffer, offset?: number): {
|
|
136
|
+
elem: number;
|
|
137
|
+
adv: number;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Deserialize a 4-byte unsigned integer from a buffer, starting at the specified offset.
|
|
141
|
+
* The deserialization reads 4 bytes from the given buffer and converts it into a number.
|
|
142
|
+
* Returns an object containing the deserialized unsigned integer and the number of bytes advanced (4).
|
|
143
|
+
*
|
|
144
|
+
* @param buf - The buffer containing the serialized unsigned integer.
|
|
145
|
+
* @param offset - The starting position in the buffer to deserialize from (default is 0).
|
|
146
|
+
* @returns An object with the deserialized unsigned integer as 'elem' and the number of bytes advanced ('adv') as 4.
|
|
147
|
+
*/
|
|
148
|
+
export declare function deserializeUInt32(buf: Buffer, offset?: number): {
|
|
149
|
+
elem: number;
|
|
150
|
+
adv: number;
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Deserialize a signed 32-bit integer from a buffer at the given offset.
|
|
154
|
+
* The input 'buf' should be a Buffer containing binary data, and 'offset' should be the position in the buffer
|
|
155
|
+
* where the signed 32-bit integer starts. Returns an object with both the deserialized integer (elem) and the
|
|
156
|
+
* number of bytes advanced in the buffer (adv, always equal to 4).
|
|
157
|
+
*
|
|
158
|
+
* @param buf - The buffer containing the binary data.
|
|
159
|
+
* @param offset - Optional, the position in the buffer where the signed 32-bit integer starts (default is 0).
|
|
160
|
+
* @returns An object with the deserialized integer as 'elem' and the number of bytes advanced as 'adv'.
|
|
161
|
+
*/
|
|
162
|
+
export declare function deserializeInt32(buf: Buffer, offset?: number): {
|
|
163
|
+
elem: number;
|
|
164
|
+
adv: number;
|
|
165
|
+
};
|
|
166
|
+
export {};
|
|
167
|
+
//# sourceMappingURL=serialize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize.d.ts","sourceRoot":"","sources":["../../src/serialize/serialize.ts"],"names":[],"mappings":";AAIA;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,CAIlE;AAED;;GAEG;AACH,KAAK,aAAa,CAAC,CAAC,IAAI,CACtB,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,KACX;IACH;;OAEG;IACH,IAAI,EAAE,CAAC,CAAC;IACR;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,EAC1C,WAAW,EAAE,aAAa,CAAC,CAAC,CAAC,EAC7B,MAAM,EAAE,MAAM,EACd,MAAM,SAAI,GACT;IACD;;OAEG;IACH,IAAI,EAAE,CAAC,EAAE,CAAC;IACV;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;CACb,CAWA;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAGzD;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,SAAI,GAAG,MAAM,CAInE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI;;;EAGvD;AAED,8CAA8C;AAC9C,MAAM,MAAM,UAAU,GAClB,OAAO,GACP,MAAM,GACN,MAAM,GACN,MAAM,GACN;IACE;;OAEG;IACH,UAAU,EAAE,MAAM,MAAM,CAAC;CAC1B,GACD;IACE;;OAEG;IACH,QAAQ,EAAE,MAAM,MAAM,CAAC;CACxB,GACD,UAAU,EAAE,CAAC;AAuBjB;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,IAAI,EAAE,UAAU,EAAE,GAAG,MAAM,EAAE,CAsBtE;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,IAAI,EAAE,UAAU,EAAE,GAAG,MAAM,CAE/D;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CA0BlD;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,SAAK,UAEpD;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI,EAAE,KAAK,SAAK;;;EAEpE;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,UAEvC;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI;;;EAGtD;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI;;;EAGxD;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,SAAI;;;EAGvD"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer';
|
|
2
|
+
import { numToUInt32BE } from './free_funcs.js';
|
|
3
|
+
/**
|
|
4
|
+
* For serializing an array of fixed length buffers.
|
|
5
|
+
* TODO move to foundation pkg.
|
|
6
|
+
* @param arr - Array of buffers.
|
|
7
|
+
* @returns The serialized buffers.
|
|
8
|
+
*/
|
|
9
|
+
export function serializeBufferArrayToVector(arr) {
|
|
10
|
+
const lengthBuf = Buffer.alloc(4);
|
|
11
|
+
lengthBuf.writeUInt32BE(arr.length, 0);
|
|
12
|
+
return Buffer.concat([lengthBuf, ...arr]);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Deserializes an array from a vector on an element-by-element basis.
|
|
16
|
+
* @param deserialize - A function used to deserialize each element of the vector.
|
|
17
|
+
* @param vector - The vector to deserialize.
|
|
18
|
+
* @param offset - The position in the vector to start deserializing from.
|
|
19
|
+
* @returns Deserialized array and how many bytes we advanced by.
|
|
20
|
+
*
|
|
21
|
+
* TODO: move to foundation pkg.
|
|
22
|
+
*/
|
|
23
|
+
export function deserializeArrayFromVector(deserialize, vector, offset = 0) {
|
|
24
|
+
let pos = offset;
|
|
25
|
+
const size = vector.readUInt32BE(pos);
|
|
26
|
+
pos += 4;
|
|
27
|
+
const arr = new Array(size);
|
|
28
|
+
for (let i = 0; i < size; ++i) {
|
|
29
|
+
const { elem, adv } = deserialize(vector, pos);
|
|
30
|
+
pos += adv;
|
|
31
|
+
arr[i] = elem;
|
|
32
|
+
}
|
|
33
|
+
return { elem: arr, adv: pos - offset };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Cast a uint8 array to a number.
|
|
37
|
+
* @param array - The uint8 array.
|
|
38
|
+
* @returns The number.
|
|
39
|
+
*/
|
|
40
|
+
export function uint8ArrayToNum(array) {
|
|
41
|
+
const buf = Buffer.from(array);
|
|
42
|
+
return buf.readUint32LE();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Serializes a boolean to a buffer.
|
|
46
|
+
* @param value - Value to serialize.
|
|
47
|
+
* @returns The serialized boolean.
|
|
48
|
+
*/
|
|
49
|
+
export function boolToBuffer(value, bufferSize = 1) {
|
|
50
|
+
const buf = Buffer.alloc(bufferSize);
|
|
51
|
+
buf.writeUInt8(value ? 1 : 0, bufferSize - 1);
|
|
52
|
+
return buf;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Deserialize the 256-bit number at address `offset`.
|
|
56
|
+
* @param buf - The buffer.
|
|
57
|
+
* @param offset - The address.
|
|
58
|
+
* @returns The deserialized 256-bit field.
|
|
59
|
+
*/
|
|
60
|
+
export function deserializeField(buf, offset = 0) {
|
|
61
|
+
const adv = 32;
|
|
62
|
+
return { elem: buf.slice(offset, offset + adv), adv };
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Checks whether an object implements the toBuffer32 method.
|
|
66
|
+
* @param obj - The object to check.
|
|
67
|
+
* @returns Whether the object implements the toBuffer32 method.
|
|
68
|
+
*/
|
|
69
|
+
function isSerializableToBuffer32(obj) {
|
|
70
|
+
return !!obj.toBuffer32;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Serializes a list of objects contiguously.
|
|
74
|
+
* @param objs - Objects to serialize.
|
|
75
|
+
* @returns A buffer list with the concatenation of all fields.
|
|
76
|
+
*/
|
|
77
|
+
export function serializeToBufferArray(...objs) {
|
|
78
|
+
let ret = [];
|
|
79
|
+
for (const obj of objs) {
|
|
80
|
+
if (Array.isArray(obj)) {
|
|
81
|
+
ret = [...ret, ...serializeToBufferArray(...obj)];
|
|
82
|
+
}
|
|
83
|
+
else if (Buffer.isBuffer(obj)) {
|
|
84
|
+
ret.push(obj);
|
|
85
|
+
}
|
|
86
|
+
else if (typeof obj === 'boolean') {
|
|
87
|
+
ret.push(boolToBuffer(obj));
|
|
88
|
+
}
|
|
89
|
+
else if (typeof obj === 'number') {
|
|
90
|
+
// Note: barretenberg assumes everything is big-endian
|
|
91
|
+
ret.push(numToUInt32BE(obj)); // TODO: Are we always passing numbers as UInt32?
|
|
92
|
+
}
|
|
93
|
+
else if (typeof obj === 'string') {
|
|
94
|
+
ret.push(numToUInt32BE(obj.length));
|
|
95
|
+
ret.push(Buffer.from(obj));
|
|
96
|
+
}
|
|
97
|
+
else if (isSerializableToBuffer32(obj)) {
|
|
98
|
+
ret.push(obj.toBuffer32());
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
ret.push(obj.toBuffer());
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return ret;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Serializes a list of objects contiguously.
|
|
108
|
+
* @param objs - Objects to serialize.
|
|
109
|
+
* @returns A single buffer with the concatenation of all fields.
|
|
110
|
+
*/
|
|
111
|
+
export function serializeToBuffer(...objs) {
|
|
112
|
+
return Buffer.concat(serializeToBufferArray(...objs));
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Returns a user-friendly JSON representation of an object, showing buffers as hex strings.
|
|
116
|
+
* @param obj - Object to json-stringify.
|
|
117
|
+
* @returns A JSON string.
|
|
118
|
+
*/
|
|
119
|
+
export function toFriendlyJSON(obj) {
|
|
120
|
+
return JSON.stringify(obj, (key, value) => {
|
|
121
|
+
if (value !== null && typeof value === 'object' && value.type === 'Buffer' && Array.isArray(value.data)) {
|
|
122
|
+
return '0x' + Buffer.from(value.data).toString('hex');
|
|
123
|
+
}
|
|
124
|
+
else if (typeof value === 'bigint') {
|
|
125
|
+
return value.toString();
|
|
126
|
+
}
|
|
127
|
+
else if (value &&
|
|
128
|
+
value.toFriendlyJSON) {
|
|
129
|
+
return value.toFriendlyJSON();
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
}, 2);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Serialize a BigInt value into a Buffer of specified width.
|
|
138
|
+
* The function converts the input BigInt into its big-endian representation and stores it in a Buffer of the given width.
|
|
139
|
+
* If the width is not provided, a default value of 32 bytes will be used. It is important to provide an appropriate width
|
|
140
|
+
* to avoid truncation or incorrect serialization of large BigInt values.
|
|
141
|
+
*
|
|
142
|
+
* @param n - The BigInt value to be serialized.
|
|
143
|
+
* @param width - The width (in bytes) of the output Buffer, optional with default value 32.
|
|
144
|
+
* @returns A Buffer containing the serialized BigInt value in big-endian format.
|
|
145
|
+
*/
|
|
146
|
+
export function serializeBigInt(n, width = 32) {
|
|
147
|
+
return toBufferBE(n, width);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Deserialize a big integer from a buffer, given an offset and width.
|
|
151
|
+
* Reads the specified number of bytes from the buffer starting at the offset, converts it to a big integer, and returns the deserialized result along with the number of bytes read (advanced).
|
|
152
|
+
*
|
|
153
|
+
* @param buf - The buffer containing the big integer to be deserialized.
|
|
154
|
+
* @param offset - The position in the buffer where the big integer starts. Defaults to 0.
|
|
155
|
+
* @param width - The number of bytes to read from the buffer for the big integer. Defaults to 32.
|
|
156
|
+
* @returns An object containing the deserialized big integer value ('elem') and the number of bytes advanced ('adv').
|
|
157
|
+
*/
|
|
158
|
+
export function deserializeBigInt(buf, offset = 0, width = 32) {
|
|
159
|
+
return { elem: toBigIntBE(buf.subarray(offset, offset + width)), adv: width };
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Serializes a Date object into a Buffer containing its timestamp as a big integer value.
|
|
163
|
+
* The resulting Buffer has a fixed width of 8 bytes, representing a 64-bit big-endian integer.
|
|
164
|
+
* This function is useful for converting date values into a binary format that can be stored or transmitted easily.
|
|
165
|
+
*
|
|
166
|
+
* @param date - The Date object to be serialized.
|
|
167
|
+
* @returns A Buffer containing the serialized timestamp of the input Date object.
|
|
168
|
+
*/
|
|
169
|
+
export function serializeDate(date) {
|
|
170
|
+
return serializeBigInt(BigInt(date.getTime()), 8);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Deserialize a boolean value from a given buffer at the specified offset.
|
|
174
|
+
* Reads a single byte at the provided offset in the buffer and returns
|
|
175
|
+
* the deserialized boolean value along with the number of bytes read (adv).
|
|
176
|
+
*
|
|
177
|
+
* @param buf - The buffer containing the serialized boolean value.
|
|
178
|
+
* @param offset - The position in the buffer to start reading the boolean value.
|
|
179
|
+
* @returns An object containing the deserialized boolean value (elem) and the number of bytes read (adv).
|
|
180
|
+
*/
|
|
181
|
+
export function deserializeBool(buf, offset = 0) {
|
|
182
|
+
const adv = 1;
|
|
183
|
+
return { elem: buf.readUInt8(offset), adv };
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Deserialize a 4-byte unsigned integer from a buffer, starting at the specified offset.
|
|
187
|
+
* The deserialization reads 4 bytes from the given buffer and converts it into a number.
|
|
188
|
+
* Returns an object containing the deserialized unsigned integer and the number of bytes advanced (4).
|
|
189
|
+
*
|
|
190
|
+
* @param buf - The buffer containing the serialized unsigned integer.
|
|
191
|
+
* @param offset - The starting position in the buffer to deserialize from (default is 0).
|
|
192
|
+
* @returns An object with the deserialized unsigned integer as 'elem' and the number of bytes advanced ('adv') as 4.
|
|
193
|
+
*/
|
|
194
|
+
export function deserializeUInt32(buf, offset = 0) {
|
|
195
|
+
const adv = 4;
|
|
196
|
+
return { elem: buf.readUInt32BE(offset), adv };
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Deserialize a signed 32-bit integer from a buffer at the given offset.
|
|
200
|
+
* The input 'buf' should be a Buffer containing binary data, and 'offset' should be the position in the buffer
|
|
201
|
+
* where the signed 32-bit integer starts. Returns an object with both the deserialized integer (elem) and the
|
|
202
|
+
* number of bytes advanced in the buffer (adv, always equal to 4).
|
|
203
|
+
*
|
|
204
|
+
* @param buf - The buffer containing the binary data.
|
|
205
|
+
* @param offset - Optional, the position in the buffer where the signed 32-bit integer starts (default is 0).
|
|
206
|
+
* @returns An object with the deserialized integer as 'elem' and the number of bytes advanced as 'adv'.
|
|
207
|
+
*/
|
|
208
|
+
export function deserializeInt32(buf, offset = 0) {
|
|
209
|
+
const adv = 4;
|
|
210
|
+
return { elem: buf.readInt32BE(offset), adv };
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2VyaWFsaXplLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJj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FDLFNBQVMsQ0FBQyxNQUFNLENBQUMsRUFBRSxHQUFHLEVBQUUsQ0FBQztBQUM5QyxDQUFDO0FBRUQ7Ozs7Ozs7O0dBUUc7QUFDSCxNQUFNLFVBQVUsaUJBQWlCLENBQUMsR0FBVyxFQUFFLE1BQU0sR0FBRyxDQUFDO0lBQ3ZELE1BQU0sR0FBRyxHQUFHLENBQUMsQ0FBQztJQUNkLE9BQU8sRUFBRSxJQUFJLEVBQUUsR0FBRyxDQUFDLFlBQVksQ0FBQyxNQUFNLENBQUMsRUFBRSxHQUFHLEVBQUUsQ0FBQztBQUNqRCxDQUFDO0FBRUQ7Ozs7Ozs7OztHQVNHO0FBQ0gsTUFBTSxVQUFVLGdCQUFnQixDQUFDLEdBQVcsRUFBRSxNQUFNLEdBQUcsQ0FBQztJQUN0RCxNQUFNLEdBQUcsR0FBRyxDQUFDLENBQUM7SUFDZCxPQUFPLEVBQUUsSUFBSSxFQUFFLEdBQUcsQ0FBQyxXQUFXLENBQUMsTUFBTSxDQUFDLEVBQUUsR0FBRyxFQUFFLENBQUM7QUFDaEQsQ0FBQyJ9
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/foundation",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"packageManager": "yarn@3.4.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dest/index.js",
|
|
@@ -36,7 +36,8 @@
|
|
|
36
36
|
"./url": "./dest/url/index.js",
|
|
37
37
|
"./committable": "./dest/committable/index.js",
|
|
38
38
|
"./noir": "./dest/noir/index.js",
|
|
39
|
-
"./testing": "./dest/testing/index.js"
|
|
39
|
+
"./testing": "./dest/testing/index.js",
|
|
40
|
+
"./array": "./dest/array/index.js"
|
|
40
41
|
},
|
|
41
42
|
"scripts": {
|
|
42
43
|
"build": "yarn clean && tsc -b",
|
|
@@ -59,7 +60,7 @@
|
|
|
59
60
|
"rootDir": "./src"
|
|
60
61
|
},
|
|
61
62
|
"dependencies": {
|
|
62
|
-
"@aztec/bb.js": "0.
|
|
63
|
+
"@aztec/bb.js": "0.19.0",
|
|
63
64
|
"@koa/cors": "^4.0.0",
|
|
64
65
|
"@noble/curves": "^1.2.0",
|
|
65
66
|
"bn.js": "^5.2.1",
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
import { BufferReader } from '@aztec/foundation/serialize';
|
|
3
|
-
import { Fr } from '../fields/index.js';
|
|
4
|
-
import { ABIParameter } from './abi.js';
|
|
5
|
-
/**
|
|
6
|
-
* A function selector is the first 4 bytes of the hash of a function signature.
|
|
7
|
-
*/
|
|
8
|
-
export declare class FunctionSelector {
|
|
9
|
-
value: number;
|
|
10
|
-
/**
|
|
11
|
-
* The size of the function selector in bytes.
|
|
12
|
-
*/
|
|
13
|
-
static SIZE: number;
|
|
14
|
-
constructor(/** number representing the function selector */ value: number);
|
|
15
|
-
/**
|
|
16
|
-
* Checks if the function selector is empty (all bytes are 0).
|
|
17
|
-
* @returns True if the function selector is empty (all bytes are 0).
|
|
18
|
-
*/
|
|
19
|
-
isEmpty(): boolean;
|
|
20
|
-
/**
|
|
21
|
-
* Serialize as a buffer.
|
|
22
|
-
* @param bufferSize - The buffer size.
|
|
23
|
-
* @returns The buffer.
|
|
24
|
-
*/
|
|
25
|
-
toBuffer(bufferSize?: number): Buffer;
|
|
26
|
-
/**
|
|
27
|
-
* Serialize as a hex string.
|
|
28
|
-
* @returns The string.
|
|
29
|
-
*/
|
|
30
|
-
toString(): string;
|
|
31
|
-
/**
|
|
32
|
-
* Checks if this function selector is equal to another.
|
|
33
|
-
* @param other - The other function selector.
|
|
34
|
-
* @returns True if the function selectors are equal.
|
|
35
|
-
*/
|
|
36
|
-
equals(other: FunctionSelector): boolean;
|
|
37
|
-
/**
|
|
38
|
-
* Deserializes from a buffer or reader, corresponding to a write in cpp.
|
|
39
|
-
* @param buffer - Buffer or BufferReader to read from.
|
|
40
|
-
* @returns The FunctionSelector.
|
|
41
|
-
*/
|
|
42
|
-
static fromBuffer(buffer: Buffer | BufferReader): FunctionSelector;
|
|
43
|
-
/**
|
|
44
|
-
* Returns a new field with the same contents as this EthAddress.
|
|
45
|
-
*
|
|
46
|
-
* @returns An Fr instance.
|
|
47
|
-
*/
|
|
48
|
-
toField(): Fr;
|
|
49
|
-
/**
|
|
50
|
-
* Converts a field to function selector.
|
|
51
|
-
* @param fr - The field to convert.
|
|
52
|
-
* @returns The function selector.
|
|
53
|
-
*/
|
|
54
|
-
static fromField(fr: Fr): FunctionSelector;
|
|
55
|
-
/**
|
|
56
|
-
* Creates a function selector from a signature.
|
|
57
|
-
* @param signature - Signature of the function to generate the selector for (e.g. "transfer(field,field)").
|
|
58
|
-
* @returns Function selector.
|
|
59
|
-
*/
|
|
60
|
-
static fromSignature(signature: string): FunctionSelector;
|
|
61
|
-
/**
|
|
62
|
-
* Creates a function selector for a given function name and parameters.
|
|
63
|
-
* @param name - The name of the function.
|
|
64
|
-
* @param parameters - An array of ABIParameter objects, each containing the type information of a function parameter.
|
|
65
|
-
* @returns A Buffer containing the 4-byte function selector.
|
|
66
|
-
*/
|
|
67
|
-
static fromNameAndParameters(name: string, parameters: ABIParameter[]): FunctionSelector;
|
|
68
|
-
/**
|
|
69
|
-
* Create an AztecAddress instance from a hex-encoded string.
|
|
70
|
-
* The input 'address' should be prefixed with '0x' or not, and have exactly 64 hex characters.
|
|
71
|
-
* Throws an error if the input length is invalid or address value is out of range.
|
|
72
|
-
*
|
|
73
|
-
* @param selector - The hex-encoded string representing the Aztec address.
|
|
74
|
-
* @returns An AztecAddress instance.
|
|
75
|
-
*/
|
|
76
|
-
static fromString(selector: string): FunctionSelector;
|
|
77
|
-
/**
|
|
78
|
-
* Creates an empty function selector.
|
|
79
|
-
* @returns An empty function selector.
|
|
80
|
-
*/
|
|
81
|
-
static empty(): FunctionSelector;
|
|
82
|
-
}
|
|
83
|
-
//# sourceMappingURL=function_selector.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"function_selector.d.ts","sourceRoot":"","sources":["../../src/abi/function_selector.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAG3D,OAAO,EAAE,EAAE,EAAE,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAGxC;;GAEG;AACH,qBAAa,gBAAgB;IAMyC,KAAK,EAAE,MAAM;IALjF;;OAEG;IACH,OAAc,IAAI,SAAK;gBAEX,gDAAgD,CAAQ,KAAK,EAAE,MAAM;IAMjF;;;OAGG;IACI,OAAO,IAAI,OAAO;IAIzB;;;;OAIG;IACH,QAAQ,CAAC,UAAU,SAAwB,GAAG,MAAM;IAIpD;;;OAGG;IACH,QAAQ,IAAI,MAAM;IAIlB;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO;IAIxC;;;;OAIG;IACH,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,GAAG,gBAAgB;IAMlE;;;;OAIG;IACI,OAAO;IAId;;;;OAIG;IACH,MAAM,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,GAAG,gBAAgB;IAI1C;;;;OAIG;IACH,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,gBAAgB;IAQzD;;;;;OAKG;IACH,MAAM,CAAC,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE;IAQrE;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,QAAQ,EAAE,MAAM;IAQlC;;;OAGG;IACH,MAAM,CAAC,KAAK,IAAI,gBAAgB;CAGjC"}
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer';
|
|
2
|
-
import { BufferReader } from '@aztec/foundation/serialize';
|
|
3
|
-
import { keccak } from '../crypto/keccak/index.js';
|
|
4
|
-
import { Fr } from '../fields/index.js';
|
|
5
|
-
import { decodeFunctionSignature } from './decoder.js';
|
|
6
|
-
/**
|
|
7
|
-
* A function selector is the first 4 bytes of the hash of a function signature.
|
|
8
|
-
*/
|
|
9
|
-
export class FunctionSelector {
|
|
10
|
-
constructor(/** number representing the function selector */ value) {
|
|
11
|
-
this.value = value;
|
|
12
|
-
if (value > 2 ** (FunctionSelector.SIZE * 8) - 1) {
|
|
13
|
-
throw new Error(`Function selector must fit in ${FunctionSelector.SIZE} bytes.`);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Checks if the function selector is empty (all bytes are 0).
|
|
18
|
-
* @returns True if the function selector is empty (all bytes are 0).
|
|
19
|
-
*/
|
|
20
|
-
isEmpty() {
|
|
21
|
-
return this.value === 0;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Serialize as a buffer.
|
|
25
|
-
* @param bufferSize - The buffer size.
|
|
26
|
-
* @returns The buffer.
|
|
27
|
-
*/
|
|
28
|
-
toBuffer(bufferSize = FunctionSelector.SIZE) {
|
|
29
|
-
return toBufferBE(BigInt(this.value), bufferSize);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Serialize as a hex string.
|
|
33
|
-
* @returns The string.
|
|
34
|
-
*/
|
|
35
|
-
toString() {
|
|
36
|
-
return this.toBuffer().toString('hex');
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Checks if this function selector is equal to another.
|
|
40
|
-
* @param other - The other function selector.
|
|
41
|
-
* @returns True if the function selectors are equal.
|
|
42
|
-
*/
|
|
43
|
-
equals(other) {
|
|
44
|
-
return this.value === other.value;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Deserializes from a buffer or reader, corresponding to a write in cpp.
|
|
48
|
-
* @param buffer - Buffer or BufferReader to read from.
|
|
49
|
-
* @returns The FunctionSelector.
|
|
50
|
-
*/
|
|
51
|
-
static fromBuffer(buffer) {
|
|
52
|
-
const reader = BufferReader.asReader(buffer);
|
|
53
|
-
const value = Number(toBigIntBE(reader.readBytes(FunctionSelector.SIZE)));
|
|
54
|
-
return new FunctionSelector(value);
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Returns a new field with the same contents as this EthAddress.
|
|
58
|
-
*
|
|
59
|
-
* @returns An Fr instance.
|
|
60
|
-
*/
|
|
61
|
-
toField() {
|
|
62
|
-
return new Fr(BigInt(this.value));
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Converts a field to function selector.
|
|
66
|
-
* @param fr - The field to convert.
|
|
67
|
-
* @returns The function selector.
|
|
68
|
-
*/
|
|
69
|
-
static fromField(fr) {
|
|
70
|
-
return new FunctionSelector(Number(fr.toBigInt()));
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Creates a function selector from a signature.
|
|
74
|
-
* @param signature - Signature of the function to generate the selector for (e.g. "transfer(field,field)").
|
|
75
|
-
* @returns Function selector.
|
|
76
|
-
*/
|
|
77
|
-
static fromSignature(signature) {
|
|
78
|
-
// throw if signature contains whitespace
|
|
79
|
-
if (/\s/.test(signature)) {
|
|
80
|
-
throw new Error('Function Signature cannot contain whitespace');
|
|
81
|
-
}
|
|
82
|
-
return FunctionSelector.fromBuffer(keccak(Buffer.from(signature)).subarray(0, FunctionSelector.SIZE));
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Creates a function selector for a given function name and parameters.
|
|
86
|
-
* @param name - The name of the function.
|
|
87
|
-
* @param parameters - An array of ABIParameter objects, each containing the type information of a function parameter.
|
|
88
|
-
* @returns A Buffer containing the 4-byte function selector.
|
|
89
|
-
*/
|
|
90
|
-
static fromNameAndParameters(name, parameters) {
|
|
91
|
-
const signature = decodeFunctionSignature(name, parameters);
|
|
92
|
-
const selector = FunctionSelector.fromSignature(signature);
|
|
93
|
-
// If using the debug logger here it kill the typing in the `server_world_state_synchronizer` and jest tests.
|
|
94
|
-
// console.log(`Function selector for ${signature} is ${selector}`);
|
|
95
|
-
return selector;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Create an AztecAddress instance from a hex-encoded string.
|
|
99
|
-
* The input 'address' should be prefixed with '0x' or not, and have exactly 64 hex characters.
|
|
100
|
-
* Throws an error if the input length is invalid or address value is out of range.
|
|
101
|
-
*
|
|
102
|
-
* @param selector - The hex-encoded string representing the Aztec address.
|
|
103
|
-
* @returns An AztecAddress instance.
|
|
104
|
-
*/
|
|
105
|
-
static fromString(selector) {
|
|
106
|
-
const buf = Buffer.from(selector.replace(/^0x/i, ''), 'hex');
|
|
107
|
-
if (buf.length !== FunctionSelector.SIZE) {
|
|
108
|
-
throw new Error(`Invalid FunctionSelector length ${buf.length}.`);
|
|
109
|
-
}
|
|
110
|
-
return FunctionSelector.fromBuffer(buf);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Creates an empty function selector.
|
|
114
|
-
* @returns An empty function selector.
|
|
115
|
-
*/
|
|
116
|
-
static empty() {
|
|
117
|
-
return new FunctionSelector(0);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* The size of the function selector in bytes.
|
|
122
|
-
*/
|
|
123
|
-
FunctionSelector.SIZE = 4;
|
|
124
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZnVuY3Rpb25fc2VsZWN0b3IuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvYWJpL2Z1bmN0aW9uX3NlbGVjdG9yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxVQUFVLEVBQUUsVUFBVSxFQUFFLE1BQU0saUNBQWlDLENBQUM7QUFDekUsT0FBTyxFQUFFLFlBQVksRUFBRSxNQUFNLDZCQUE2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