1
1
import { InvalidDataType } from '@vechain/sdk-errors' ;
2
2
import { Hex } from './Hex' ;
3
- import { HexUInt } from './HexUInt' ;
4
3
import { Txt } from './Txt' ;
5
4
6
5
/**
7
- * Represents a revision for a Thor transaction or block.
8
- *
9
- * @remarks The string representation of the revision is always expressed as a number in base 10.
6
+ * Represents a revision for a Thor transaction or block
7
+ * Revision strings can be one of the following:
8
+ * - "best": indicating the best revision
9
+ * - "finalized": indicating a finalized revision
10
+ * - "next": indicating the next revision
11
+ * - "justified": indicating the justified revision
12
+ * - A hex string prefixed with "0x" indicating a specific block id
13
+ * - A positive number indicating a specific block number
10
14
*
11
15
* @extends Txt
12
16
*/
13
17
class Revision extends Txt {
14
18
/**
15
19
* Regular expression pattern for revision strings.
16
- * Revision strings can be one of the following:
17
- * - "best": indicating the best revision
18
- * - "finalized": indicating a finalized revision
19
- * - A positive numeric string indicating a specific revision
20
20
*
21
21
* @type {RegExp }
22
22
*/
23
- private static readonly REGEX_DECIMAL_REVISION = / ^ ( b e s t | f i n a l i z e d | \d + ) $ / ;
23
+ private static readonly VALID_REVISION_REGEX =
24
+ / ^ ( b e s t | f i n a l i z e d | n e x t | j u s t i f i e d | 0 x [ a - f A - F 0 - 9 ] + | \d + ) $ / ;
24
25
25
26
/**
26
- * Determines if the given value is valid.
27
- * This is true if the given value is
28
- * - "best" string or {@link Txt}: indicating the best revision;
29
- * - "finalized" string or {@link Txt}: indicating a finalized revision;
30
- * - a positive number;
31
- * - a positive numeric decimal or `0x` prefixed hexadecimal string indicating a specific revision,
32
- *
33
- * @param {bigint | number | string | Hex | Txt } value - The value to be validated.
27
+ * Determines if the given value is a valid revision.
28
+ * @param {bigint| number | string | Hex } value - The value to be validated.
34
29
* @returns {boolean } - Returns `true` if the value is valid, `false` otherwise.
35
30
*/
36
- public static isValid ( value : number | string ) : boolean {
31
+ public static isValid ( value : bigint | number | string | Hex ) : boolean {
37
32
if ( typeof value === 'number' ) {
38
33
return Number . isInteger ( value ) && value >= 0 ;
39
34
}
40
- return (
41
- HexUInt . isValid0x ( value ) ||
42
- Revision . REGEX_DECIMAL_REVISION . test ( value )
43
- ) ;
35
+ if ( typeof value === 'bigint' ) {
36
+ return value >= BigInt ( 0 ) ;
37
+ }
38
+ if ( value instanceof Hex ) {
39
+ return Revision . isValid ( value . bi ) ;
40
+ }
41
+ return Revision . VALID_REVISION_REGEX . test ( value ) ;
44
42
}
45
43
46
44
/**
@@ -59,24 +57,25 @@ class Revision extends Txt {
59
57
*/
60
58
public static of ( value : bigint | number | string | Uint8Array | Hex ) : Txt {
61
59
try {
62
- let txt : string ;
63
- if ( value instanceof Hex ) {
64
- txt = value . bi . toString ( ) ;
65
- } else if (
66
- typeof value === 'bigint' ||
67
- typeof value === 'number' ||
68
- typeof value === 'string'
69
- ) {
70
- txt = `${ value } ` ;
71
- } else {
72
- txt = Txt . of ( value ) . toString ( ) ;
60
+ // handle Uint8Array which is needed to extend Txt.of
61
+ if ( ArrayBuffer . isView ( value ) ) {
62
+ const txtValue = Txt . of ( value ) . toString ( ) ;
63
+ if ( Revision . isValid ( txtValue ) ) {
64
+ return new Revision ( txtValue ) ;
65
+ } else {
66
+ throw new InvalidDataType ( 'Revision.of' , 'not a revision' , {
67
+ value : `${ value } `
68
+ } ) ;
69
+ }
73
70
}
74
- if ( Revision . isValid ( txt ) ) {
75
- return new Revision ( txt ) ;
71
+ // handle other types
72
+ if ( Revision . isValid ( value ) ) {
73
+ return new Revision ( `${ value } ` ) ;
74
+ } else {
75
+ throw new InvalidDataType ( 'Revision.of' , 'not a revision' , {
76
+ value : `${ value } `
77
+ } ) ;
76
78
}
77
- throw new InvalidDataType ( 'Revision.of' , 'not a revision' , {
78
- value : `${ value } `
79
- } ) ;
80
79
} catch ( e ) {
81
80
throw new InvalidDataType ( 'Revision.of' , 'not a revision' , {
82
81
value : `${ value } ` ,
@@ -94,6 +93,16 @@ class Revision extends Txt {
94
93
* Return the `finalized` revision instance.
95
94
*/
96
95
public static readonly FINALIZED : Revision = Revision . of ( 'finalized' ) ;
96
+
97
+ /**
98
+ * Return the `next` revision instance.
99
+ */
100
+ public static readonly NEXT : Revision = Revision . of ( 'next' ) ;
101
+
102
+ /**
103
+ * Return the `justified` revision instance.
104
+ */
105
+ public static readonly JUSTIFIED : Revision = Revision . of ( 'justified' ) ;
97
106
}
98
107
99
108
export { Revision } ;
0 commit comments