@aztec/foundation 0.0.1-commit.9b94fc1 → 0.0.1-commit.d3ec352c
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/branded-types/block_number.d.ts +53 -0
- package/dest/branded-types/block_number.d.ts.map +1 -0
- package/dest/branded-types/block_number.js +75 -0
- package/dest/branded-types/checkpoint_number.d.ts +46 -0
- package/dest/branded-types/checkpoint_number.d.ts.map +1 -0
- package/dest/branded-types/checkpoint_number.js +68 -0
- package/dest/branded-types/index.d.ts +3 -1
- package/dest/branded-types/index.d.ts.map +1 -1
- package/dest/branded-types/index.js +2 -0
- package/package.json +4 -4
- package/src/branded-types/block_number.ts +112 -0
- package/src/branded-types/checkpoint_number.ts +100 -0
- package/src/branded-types/index.ts +2 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { CheckpointNumber } from './checkpoint_number.js';
|
|
3
|
+
import type { Branded } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* A branded type representing an Aztec (L2) block number.
|
|
6
|
+
* Block numbers are sequential identifiers for blocks in the Aztec rollup chain.
|
|
7
|
+
*
|
|
8
|
+
* This is a nominal type backed by a number, but TypeScript will treat it as
|
|
9
|
+
* incompatible with plain numbers and other branded numeric types (like EpochNumber, SlotNumber, etc.).
|
|
10
|
+
*
|
|
11
|
+
* Note: This type should ONLY be used for Aztec L2 block numbers, NOT for Ethereum L1 block numbers.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const blockNumber = BlockNumber(100);
|
|
16
|
+
* const plainNumber: number = blockNumber; // OK - BlockNumber is assignable to number
|
|
17
|
+
* const blockFromNumber: BlockNumber = 100; // ERROR - number is not assignable to BlockNumber
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export type BlockNumber = Branded<number, 'BlockNumber'>;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a BlockNumber from a number.
|
|
23
|
+
* @param value - The block number (must be a non-negative integer)
|
|
24
|
+
* @returns The branded BlockNumber value
|
|
25
|
+
* @throws If the value is negative or not an integer
|
|
26
|
+
*/
|
|
27
|
+
export declare function BlockNumber(value: number): BlockNumber;
|
|
28
|
+
export declare namespace BlockNumber {
|
|
29
|
+
var fromBigInt: (value: bigint) => BlockNumber;
|
|
30
|
+
}
|
|
31
|
+
export declare namespace BlockNumber {
|
|
32
|
+
var fromString: (value: string) => BlockNumber;
|
|
33
|
+
}
|
|
34
|
+
export declare namespace BlockNumber {
|
|
35
|
+
var fromCheckpointNumber: (value: CheckpointNumber) => BlockNumber;
|
|
36
|
+
}
|
|
37
|
+
export declare namespace BlockNumber {
|
|
38
|
+
var isValid: (value: unknown) => value is BlockNumber;
|
|
39
|
+
}
|
|
40
|
+
export declare namespace BlockNumber {
|
|
41
|
+
var ZERO: BlockNumber;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Zod schema for parsing and validating BlockNumber values.
|
|
45
|
+
* Accepts numbers, bigints, or strings and coerces them to BlockNumber.
|
|
46
|
+
*/
|
|
47
|
+
export declare const BlockNumberSchema: z.ZodEffects<z.ZodPipeline<z.ZodUnion<[z.ZodNumber, z.ZodBigInt, z.ZodString]>, z.ZodNumber>, BlockNumber, string | number | bigint>;
|
|
48
|
+
/**
|
|
49
|
+
* Zod schema for parsing and validating BlockNumber values that are strictly positive.
|
|
50
|
+
* Accepts numbers, bigints, or strings and coerces them to BlockNumber.
|
|
51
|
+
*/
|
|
52
|
+
export declare const BlockNumberPositiveSchema: z.ZodEffects<z.ZodPipeline<z.ZodUnion<[z.ZodNumber, z.ZodBigInt, z.ZodString]>, z.ZodNumber>, BlockNumber, string | number | bigint>;
|
|
53
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmxvY2tfbnVtYmVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvYnJhbmRlZC10eXBlcy9ibG9ja19udW1iZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLENBQUMsRUFBRSxNQUFNLEtBQUssQ0FBQztBQUV4QixPQUFPLEtBQUssRUFBRSxnQkFBZ0IsRUFBRSxNQUFNLHdCQUF3QixDQUFDO0FBQy9ELE9BQU8sS0FBSyxFQUFFLE9BQU8sRUFBRSxNQUFNLFlBQVksQ0FBQztBQUUxQzs7Ozs7Ozs7Ozs7Ozs7O0dBZUc7QUFDSCxNQUFNLE1BQU0sV0FBVyxHQUFHLE9BQU8sQ0FBQyxNQUFNLEVBQUUsYUFBYSxDQUFDLENBQUM7QUFFekQ7Ozs7O0dBS0c7QUFDSCx3QkFBZ0IsV0FBVyxDQUFDLEtBQUssRUFBRSxNQUFNLEdBQUcsV0FBVyxDQVF0RDs7Ozs7Ozs7Ozs7Ozs7OztBQWdFRDs7O0dBR0c7QUFDSCxlQUFPLE1BQU0saUJBQWlCLHNJQUEyQixDQUFDO0FBRTFEOzs7R0FHRztBQUNILGVBQU8sTUFBTSx5QkFBeUIsc0lBQTJCLENBQUMifQ==
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"block_number.d.ts","sourceRoot":"","sources":["../../src/branded-types/block_number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE1C;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;AAEzD;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,CAQtD;;;;;;;;;;;;;;;;AAgED;;;GAGG;AACH,eAAO,MAAM,iBAAiB,sIAA2B,CAAC;AAE1D;;;GAGG;AACH,eAAO,MAAM,yBAAyB,sIAA2B,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a BlockNumber from a number.
|
|
4
|
+
* @param value - The block number (must be a non-negative integer)
|
|
5
|
+
* @returns The branded BlockNumber value
|
|
6
|
+
* @throws If the value is negative or not an integer
|
|
7
|
+
*/ export function BlockNumber(value) {
|
|
8
|
+
if (!Number.isInteger(value)) {
|
|
9
|
+
throw new Error(`BlockNumber must be an integer, got ${value}`);
|
|
10
|
+
}
|
|
11
|
+
if (value < 0) {
|
|
12
|
+
throw new Error(`BlockNumber must be non-negative, got ${value}`);
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Creates a BlockNumber from a bigint.
|
|
18
|
+
* @param value - The block number as bigint (must be a non-negative integer within safe integer range)
|
|
19
|
+
* @returns The branded BlockNumber value
|
|
20
|
+
* @throws If the value is negative or exceeds Number.MAX_SAFE_INTEGER
|
|
21
|
+
*/ BlockNumber.fromBigInt = function(value) {
|
|
22
|
+
if (value < 0n) {
|
|
23
|
+
throw new Error(`BlockNumber must be non-negative, got ${value}`);
|
|
24
|
+
}
|
|
25
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
26
|
+
throw new Error(`BlockNumber ${value} exceeds MAX_SAFE_INTEGER`);
|
|
27
|
+
}
|
|
28
|
+
return Number(value);
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Creates a BlockNumber from a string.
|
|
32
|
+
* @param value - The block number as a string
|
|
33
|
+
* @returns The branded BlockNumber value
|
|
34
|
+
* @throws If the string cannot be parsed as a valid block number
|
|
35
|
+
*/ BlockNumber.fromString = function(value) {
|
|
36
|
+
const parsed = parseInt(value, 10);
|
|
37
|
+
if (isNaN(parsed)) {
|
|
38
|
+
throw new Error(`Cannot parse BlockNumber from string: ${value}`);
|
|
39
|
+
}
|
|
40
|
+
return BlockNumber(parsed);
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Converts a CheckpointNumber to a BlockNumber.
|
|
44
|
+
* This is used in checkpoint-based systems where checkpoint numbers align with block numbers.
|
|
45
|
+
* WARNING: This should only be used when you know the checkpoint number corresponds to a block number.
|
|
46
|
+
* @param value - The checkpoint number to convert
|
|
47
|
+
* @returns The corresponding BlockNumber
|
|
48
|
+
*/ BlockNumber.fromCheckpointNumber = function(value) {
|
|
49
|
+
return value;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Type guard to check if a value is a valid BlockNumber.
|
|
53
|
+
* Note: At runtime, a BlockNumber is just a number, so this checks if the value
|
|
54
|
+
* is a non-negative integer.
|
|
55
|
+
*/ BlockNumber.isValid = function(value) {
|
|
56
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 0;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* The zero block value (genesis block).
|
|
60
|
+
*/ BlockNumber.ZERO = BlockNumber(0);
|
|
61
|
+
function makeBlockNumberSchema(minValue) {
|
|
62
|
+
return z.union([
|
|
63
|
+
z.number(),
|
|
64
|
+
z.bigint(),
|
|
65
|
+
z.string()
|
|
66
|
+
]).pipe(z.coerce.number().int().min(minValue)).transform((value)=>BlockNumber(value));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Zod schema for parsing and validating BlockNumber values.
|
|
70
|
+
* Accepts numbers, bigints, or strings and coerces them to BlockNumber.
|
|
71
|
+
*/ export const BlockNumberSchema = makeBlockNumberSchema(0);
|
|
72
|
+
/**
|
|
73
|
+
* Zod schema for parsing and validating BlockNumber values that are strictly positive.
|
|
74
|
+
* Accepts numbers, bigints, or strings and coerces them to BlockNumber.
|
|
75
|
+
*/ export const BlockNumberPositiveSchema = makeBlockNumberSchema(1);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { BlockNumber } from './block_number.js';
|
|
3
|
+
import type { Branded } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* A branded type representing an checkpoint number.
|
|
6
|
+
* Checkpoints are used in the Aztec protocol to group L2 blocks.
|
|
7
|
+
*
|
|
8
|
+
* This is a nominal type backed by a number, but TypeScript will treat it as
|
|
9
|
+
* incompatible with plain numbers and other branded numeric types (like Slot, BlockNumber, etc.).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const checkpoint = CheckpointNumber(5);
|
|
14
|
+
* const plainNumber: number = checkpoint; // OK - CheckpointNumber is assignable to number
|
|
15
|
+
* const checkpointFromNumber: CheckpointNumber = 5; // ERROR - number is not assignable to CheckpointNumber
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export type CheckpointNumber = Branded<number, 'CheckpointNumber'>;
|
|
19
|
+
/**
|
|
20
|
+
* Creates an CheckpointNumber from a number.
|
|
21
|
+
* @param value - The checkpoint number (must be a non-negative integer)
|
|
22
|
+
* @returns The branded CheckpointNumber value
|
|
23
|
+
* @throws If the value is negative or not an integer
|
|
24
|
+
*/
|
|
25
|
+
export declare function CheckpointNumber(value: number): CheckpointNumber;
|
|
26
|
+
export declare namespace CheckpointNumber {
|
|
27
|
+
var fromBlockNumber: (value: BlockNumber) => CheckpointNumber;
|
|
28
|
+
}
|
|
29
|
+
export declare namespace CheckpointNumber {
|
|
30
|
+
var fromBigInt: (value: bigint) => CheckpointNumber;
|
|
31
|
+
}
|
|
32
|
+
export declare namespace CheckpointNumber {
|
|
33
|
+
var fromString: (value: string) => CheckpointNumber;
|
|
34
|
+
}
|
|
35
|
+
export declare namespace CheckpointNumber {
|
|
36
|
+
var isValid: (value: unknown) => value is CheckpointNumber;
|
|
37
|
+
}
|
|
38
|
+
export declare namespace CheckpointNumber {
|
|
39
|
+
var ZERO: CheckpointNumber;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Zod schema for parsing and validating CheckpointNumber values.
|
|
43
|
+
* Accepts numbers, bigints, or strings and coerces them to CheckpointNumber.
|
|
44
|
+
*/
|
|
45
|
+
export declare const CheckpointNumberSchema: z.ZodEffects<z.ZodPipeline<z.ZodUnion<[z.ZodNumber, z.ZodBigInt, z.ZodString]>, z.ZodNumber>, CheckpointNumber, string | number | bigint>;
|
|
46
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hlY2twb2ludF9udW1iZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9icmFuZGVkLXR5cGVzL2NoZWNrcG9pbnRfbnVtYmVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxDQUFDLEVBQUUsTUFBTSxLQUFLLENBQUM7QUFFeEIsT0FBTyxLQUFLLEVBQUUsV0FBVyxFQUFFLE1BQU0sbUJBQW1CLENBQUM7QUFDckQsT0FBTyxLQUFLLEVBQUUsT0FBTyxFQUFFLE1BQU0sWUFBWSxDQUFDO0FBRTFDOzs7Ozs7Ozs7Ozs7O0dBYUc7QUFDSCxNQUFNLE1BQU0sZ0JBQWdCLEdBQUcsT0FBTyxDQUFDLE1BQU0sRUFBRSxrQkFBa0IsQ0FBQyxDQUFDO0FBRW5FOzs7OztHQUtHO0FBQ0gsd0JBQWdCLGdCQUFnQixDQUFDLEtBQUssRUFBRSxNQUFNLEdBQUcsZ0JBQWdCLENBUWhFOzs7Ozs7Ozs7Ozs7Ozs7O0FBeUREOzs7R0FHRztBQUNILGVBQU8sTUFBTSxzQkFBc0IsMklBR1csQ0FBQyJ9
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkpoint_number.d.ts","sourceRoot":"","sources":["../../src/branded-types/checkpoint_number.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE1C;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,gBAAgB,GAAG,OAAO,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;AAEnE;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB,CAQhE;;;;;;;;;;;;;;;;AAyDD;;;GAGG;AACH,eAAO,MAAM,sBAAsB,2IAGW,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Creates an CheckpointNumber from a number.
|
|
4
|
+
* @param value - The checkpoint number (must be a non-negative integer)
|
|
5
|
+
* @returns The branded CheckpointNumber value
|
|
6
|
+
* @throws If the value is negative or not an integer
|
|
7
|
+
*/ export function CheckpointNumber(value) {
|
|
8
|
+
if (!Number.isInteger(value)) {
|
|
9
|
+
throw new Error(`CheckpointNumber must be an integer, got ${value}`);
|
|
10
|
+
}
|
|
11
|
+
if (value < 0) {
|
|
12
|
+
throw new Error(`CheckpointNumber must be non-negative, got ${value}`);
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* @param value - The block number
|
|
18
|
+
* @returns The branded CheckpointNumber value
|
|
19
|
+
*
|
|
20
|
+
* @deprecated Checkpoint number and block number should no longer convert to each other once we support multiple blocks
|
|
21
|
+
* per checkpoint everywhere.
|
|
22
|
+
*/ CheckpointNumber.fromBlockNumber = function(value) {
|
|
23
|
+
return CheckpointNumber(value);
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Creates an CheckpointNumber from a bigint.
|
|
27
|
+
* @param value - The checkpoint number as bigint (must be a non-negative integer within safe integer range)
|
|
28
|
+
* @returns The branded CheckpointNumber value
|
|
29
|
+
* @throws If the value is negative or exceeds Number.MAX_SAFE_INTEGER
|
|
30
|
+
*/ CheckpointNumber.fromBigInt = function(value) {
|
|
31
|
+
if (value < 0n) {
|
|
32
|
+
throw new Error(`CheckpointNumber must be non-negative, got ${value}`);
|
|
33
|
+
}
|
|
34
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
35
|
+
throw new Error(`CheckpointNumber ${value} exceeds MAX_SAFE_INTEGER`);
|
|
36
|
+
}
|
|
37
|
+
return Number(value);
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Creates an CheckpointNumber from a string.
|
|
41
|
+
* @param value - The checkpoint number as a string
|
|
42
|
+
* @returns The branded CheckpointNumber value
|
|
43
|
+
* @throws If the string cannot be parsed as a valid checkpoint
|
|
44
|
+
*/ CheckpointNumber.fromString = function(value) {
|
|
45
|
+
const parsed = parseInt(value, 10);
|
|
46
|
+
if (isNaN(parsed)) {
|
|
47
|
+
throw new Error(`Cannot parse CheckpointNumber from string: ${value}`);
|
|
48
|
+
}
|
|
49
|
+
return CheckpointNumber(parsed);
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Type guard to check if a value is a valid CheckpointNumber.
|
|
53
|
+
* Note: At runtime, an CheckpointNumber is just a number, so this checks if the value
|
|
54
|
+
* is a non-negative integer.
|
|
55
|
+
*/ CheckpointNumber.isValid = function(value) {
|
|
56
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 0;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* The zero checkpoint value.
|
|
60
|
+
*/ CheckpointNumber.ZERO = CheckpointNumber(0);
|
|
61
|
+
/**
|
|
62
|
+
* Zod schema for parsing and validating CheckpointNumber values.
|
|
63
|
+
* Accepts numbers, bigints, or strings and coerces them to CheckpointNumber.
|
|
64
|
+
*/ export const CheckpointNumberSchema = z.union([
|
|
65
|
+
z.number(),
|
|
66
|
+
z.bigint(),
|
|
67
|
+
z.string()
|
|
68
|
+
]).pipe(z.coerce.number().int().min(0)).transform((value)=>CheckpointNumber(value));
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
export { BlockNumber, BlockNumberSchema, BlockNumberPositiveSchema } from './block_number.js';
|
|
2
|
+
export { CheckpointNumber, CheckpointNumberSchema } from './checkpoint_number.js';
|
|
1
3
|
export { EpochNumber, EpochNumberSchema } from './epoch.js';
|
|
2
4
|
export { SlotNumber, SlotNumberSchema } from './slot.js';
|
|
3
5
|
export type { Branded } from './types.js';
|
|
4
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
6
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9icmFuZGVkLXR5cGVzL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxXQUFXLEVBQUUsaUJBQWlCLEVBQUUseUJBQXlCLEVBQUUsTUFBTSxtQkFBbUIsQ0FBQztBQUM5RixPQUFPLEVBQUUsZ0JBQWdCLEVBQUUsc0JBQXNCLEVBQUUsTUFBTSx3QkFBd0IsQ0FBQztBQUNsRixPQUFPLEVBQUUsV0FBVyxFQUFFLGlCQUFpQixFQUFFLE1BQU0sWUFBWSxDQUFDO0FBQzVELE9BQU8sRUFBRSxVQUFVLEVBQUUsZ0JBQWdCLEVBQUUsTUFBTSxXQUFXLENBQUM7QUFFekQsWUFBWSxFQUFFLE9BQU8sRUFBRSxNQUFNLFlBQVksQ0FBQyJ9
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/branded-types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEzD,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/branded-types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AAC9F,OAAO,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAClF,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEzD,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -1,2 +1,4 @@
|
|
|
1
|
+
export { BlockNumber, BlockNumberSchema, BlockNumberPositiveSchema } from './block_number.js';
|
|
2
|
+
export { CheckpointNumber, CheckpointNumberSchema } from './checkpoint_number.js';
|
|
1
3
|
export { EpochNumber, EpochNumberSchema } from './epoch.js';
|
|
2
4
|
export { SlotNumber, SlotNumberSchema } from './slot.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/foundation",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.d3ec352c",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dest/index.js",
|
|
6
6
|
"types": "./dest/index.d.ts",
|
|
@@ -58,8 +58,8 @@
|
|
|
58
58
|
"./number": "./dest/number/index.js"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
|
-
"build": "yarn clean &&
|
|
62
|
-
"build:dev": "
|
|
61
|
+
"build": "yarn clean && ../scripts/tsc.sh",
|
|
62
|
+
"build:dev": "../scripts/tsc.sh --watch",
|
|
63
63
|
"clean": "rm -rf ./dest .tsbuildinfo",
|
|
64
64
|
"generate": "true",
|
|
65
65
|
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
|
|
@@ -105,7 +105,7 @@
|
|
|
105
105
|
"testEnvironment": "../../foundation/src/jest/env.mjs"
|
|
106
106
|
},
|
|
107
107
|
"dependencies": {
|
|
108
|
-
"@aztec/bb.js": "0.0.1-commit.
|
|
108
|
+
"@aztec/bb.js": "0.0.1-commit.d3ec352c",
|
|
109
109
|
"@koa/cors": "^5.0.0",
|
|
110
110
|
"@noble/curves": "=1.7.0",
|
|
111
111
|
"@noble/hashes": "^1.6.1",
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
import type { CheckpointNumber } from './checkpoint_number.js';
|
|
4
|
+
import type { Branded } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A branded type representing an Aztec (L2) block number.
|
|
8
|
+
* Block numbers are sequential identifiers for blocks in the Aztec rollup chain.
|
|
9
|
+
*
|
|
10
|
+
* This is a nominal type backed by a number, but TypeScript will treat it as
|
|
11
|
+
* incompatible with plain numbers and other branded numeric types (like EpochNumber, SlotNumber, etc.).
|
|
12
|
+
*
|
|
13
|
+
* Note: This type should ONLY be used for Aztec L2 block numbers, NOT for Ethereum L1 block numbers.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const blockNumber = BlockNumber(100);
|
|
18
|
+
* const plainNumber: number = blockNumber; // OK - BlockNumber is assignable to number
|
|
19
|
+
* const blockFromNumber: BlockNumber = 100; // ERROR - number is not assignable to BlockNumber
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export type BlockNumber = Branded<number, 'BlockNumber'>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Creates a BlockNumber from a number.
|
|
26
|
+
* @param value - The block number (must be a non-negative integer)
|
|
27
|
+
* @returns The branded BlockNumber value
|
|
28
|
+
* @throws If the value is negative or not an integer
|
|
29
|
+
*/
|
|
30
|
+
export function BlockNumber(value: number): BlockNumber {
|
|
31
|
+
if (!Number.isInteger(value)) {
|
|
32
|
+
throw new Error(`BlockNumber must be an integer, got ${value}`);
|
|
33
|
+
}
|
|
34
|
+
if (value < 0) {
|
|
35
|
+
throw new Error(`BlockNumber must be non-negative, got ${value}`);
|
|
36
|
+
}
|
|
37
|
+
return value as BlockNumber;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Creates a BlockNumber from a bigint.
|
|
42
|
+
* @param value - The block number as bigint (must be a non-negative integer within safe integer range)
|
|
43
|
+
* @returns The branded BlockNumber value
|
|
44
|
+
* @throws If the value is negative or exceeds Number.MAX_SAFE_INTEGER
|
|
45
|
+
*/
|
|
46
|
+
BlockNumber.fromBigInt = function (value: bigint): BlockNumber {
|
|
47
|
+
if (value < 0n) {
|
|
48
|
+
throw new Error(`BlockNumber must be non-negative, got ${value}`);
|
|
49
|
+
}
|
|
50
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
51
|
+
throw new Error(`BlockNumber ${value} exceeds MAX_SAFE_INTEGER`);
|
|
52
|
+
}
|
|
53
|
+
return Number(value) as BlockNumber;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Creates a BlockNumber from a string.
|
|
58
|
+
* @param value - The block number as a string
|
|
59
|
+
* @returns The branded BlockNumber value
|
|
60
|
+
* @throws If the string cannot be parsed as a valid block number
|
|
61
|
+
*/
|
|
62
|
+
BlockNumber.fromString = function (value: string): BlockNumber {
|
|
63
|
+
const parsed = parseInt(value, 10);
|
|
64
|
+
if (isNaN(parsed)) {
|
|
65
|
+
throw new Error(`Cannot parse BlockNumber from string: ${value}`);
|
|
66
|
+
}
|
|
67
|
+
return BlockNumber(parsed);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Converts a CheckpointNumber to a BlockNumber.
|
|
72
|
+
* This is used in checkpoint-based systems where checkpoint numbers align with block numbers.
|
|
73
|
+
* WARNING: This should only be used when you know the checkpoint number corresponds to a block number.
|
|
74
|
+
* @param value - The checkpoint number to convert
|
|
75
|
+
* @returns The corresponding BlockNumber
|
|
76
|
+
*/
|
|
77
|
+
BlockNumber.fromCheckpointNumber = function (value: CheckpointNumber): BlockNumber {
|
|
78
|
+
return value as unknown as BlockNumber;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Type guard to check if a value is a valid BlockNumber.
|
|
83
|
+
* Note: At runtime, a BlockNumber is just a number, so this checks if the value
|
|
84
|
+
* is a non-negative integer.
|
|
85
|
+
*/
|
|
86
|
+
BlockNumber.isValid = function (value: unknown): value is BlockNumber {
|
|
87
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 0;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The zero block value (genesis block).
|
|
92
|
+
*/
|
|
93
|
+
BlockNumber.ZERO = BlockNumber(0);
|
|
94
|
+
|
|
95
|
+
function makeBlockNumberSchema(minValue: number) {
|
|
96
|
+
return z
|
|
97
|
+
.union([z.number(), z.bigint(), z.string()])
|
|
98
|
+
.pipe(z.coerce.number().int().min(minValue))
|
|
99
|
+
.transform(value => BlockNumber(value));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Zod schema for parsing and validating BlockNumber values.
|
|
104
|
+
* Accepts numbers, bigints, or strings and coerces them to BlockNumber.
|
|
105
|
+
*/
|
|
106
|
+
export const BlockNumberSchema = makeBlockNumberSchema(0);
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Zod schema for parsing and validating BlockNumber values that are strictly positive.
|
|
110
|
+
* Accepts numbers, bigints, or strings and coerces them to BlockNumber.
|
|
111
|
+
*/
|
|
112
|
+
export const BlockNumberPositiveSchema = makeBlockNumberSchema(1);
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
import type { BlockNumber } from './block_number.js';
|
|
4
|
+
import type { Branded } from './types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A branded type representing an checkpoint number.
|
|
8
|
+
* Checkpoints are used in the Aztec protocol to group L2 blocks.
|
|
9
|
+
*
|
|
10
|
+
* This is a nominal type backed by a number, but TypeScript will treat it as
|
|
11
|
+
* incompatible with plain numbers and other branded numeric types (like Slot, BlockNumber, etc.).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const checkpoint = CheckpointNumber(5);
|
|
16
|
+
* const plainNumber: number = checkpoint; // OK - CheckpointNumber is assignable to number
|
|
17
|
+
* const checkpointFromNumber: CheckpointNumber = 5; // ERROR - number is not assignable to CheckpointNumber
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export type CheckpointNumber = Branded<number, 'CheckpointNumber'>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Creates an CheckpointNumber from a number.
|
|
24
|
+
* @param value - The checkpoint number (must be a non-negative integer)
|
|
25
|
+
* @returns The branded CheckpointNumber value
|
|
26
|
+
* @throws If the value is negative or not an integer
|
|
27
|
+
*/
|
|
28
|
+
export function CheckpointNumber(value: number): CheckpointNumber {
|
|
29
|
+
if (!Number.isInteger(value)) {
|
|
30
|
+
throw new Error(`CheckpointNumber must be an integer, got ${value}`);
|
|
31
|
+
}
|
|
32
|
+
if (value < 0) {
|
|
33
|
+
throw new Error(`CheckpointNumber must be non-negative, got ${value}`);
|
|
34
|
+
}
|
|
35
|
+
return value as CheckpointNumber;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param value - The block number
|
|
40
|
+
* @returns The branded CheckpointNumber value
|
|
41
|
+
*
|
|
42
|
+
* @deprecated Checkpoint number and block number should no longer convert to each other once we support multiple blocks
|
|
43
|
+
* per checkpoint everywhere.
|
|
44
|
+
*/
|
|
45
|
+
CheckpointNumber.fromBlockNumber = function (value: BlockNumber): CheckpointNumber {
|
|
46
|
+
return CheckpointNumber(value);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Creates an CheckpointNumber from a bigint.
|
|
51
|
+
* @param value - The checkpoint number as bigint (must be a non-negative integer within safe integer range)
|
|
52
|
+
* @returns The branded CheckpointNumber value
|
|
53
|
+
* @throws If the value is negative or exceeds Number.MAX_SAFE_INTEGER
|
|
54
|
+
*/
|
|
55
|
+
CheckpointNumber.fromBigInt = function (value: bigint): CheckpointNumber {
|
|
56
|
+
if (value < 0n) {
|
|
57
|
+
throw new Error(`CheckpointNumber must be non-negative, got ${value}`);
|
|
58
|
+
}
|
|
59
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
60
|
+
throw new Error(`CheckpointNumber ${value} exceeds MAX_SAFE_INTEGER`);
|
|
61
|
+
}
|
|
62
|
+
return Number(value) as CheckpointNumber;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Creates an CheckpointNumber from a string.
|
|
67
|
+
* @param value - The checkpoint number as a string
|
|
68
|
+
* @returns The branded CheckpointNumber value
|
|
69
|
+
* @throws If the string cannot be parsed as a valid checkpoint
|
|
70
|
+
*/
|
|
71
|
+
CheckpointNumber.fromString = function (value: string): CheckpointNumber {
|
|
72
|
+
const parsed = parseInt(value, 10);
|
|
73
|
+
if (isNaN(parsed)) {
|
|
74
|
+
throw new Error(`Cannot parse CheckpointNumber from string: ${value}`);
|
|
75
|
+
}
|
|
76
|
+
return CheckpointNumber(parsed);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Type guard to check if a value is a valid CheckpointNumber.
|
|
81
|
+
* Note: At runtime, an CheckpointNumber is just a number, so this checks if the value
|
|
82
|
+
* is a non-negative integer.
|
|
83
|
+
*/
|
|
84
|
+
CheckpointNumber.isValid = function (value: unknown): value is CheckpointNumber {
|
|
85
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= 0;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The zero checkpoint value.
|
|
90
|
+
*/
|
|
91
|
+
CheckpointNumber.ZERO = CheckpointNumber(0);
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Zod schema for parsing and validating CheckpointNumber values.
|
|
95
|
+
* Accepts numbers, bigints, or strings and coerces them to CheckpointNumber.
|
|
96
|
+
*/
|
|
97
|
+
export const CheckpointNumberSchema = z
|
|
98
|
+
.union([z.number(), z.bigint(), z.string()])
|
|
99
|
+
.pipe(z.coerce.number().int().min(0))
|
|
100
|
+
.transform(value => CheckpointNumber(value));
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { BlockNumber, BlockNumberSchema, BlockNumberPositiveSchema } from './block_number.js';
|
|
2
|
+
export { CheckpointNumber, CheckpointNumberSchema } from './checkpoint_number.js';
|
|
1
3
|
export { EpochNumber, EpochNumberSchema } from './epoch.js';
|
|
2
4
|
export { SlotNumber, SlotNumberSchema } from './slot.js';
|
|
3
5
|
|