@@ -3,12 +3,14 @@ import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
3
3
4
4
import type { Blob as BlobBuffer } from 'c-kzg' ;
5
5
6
+ // Note duplicated from circuit-types !
6
7
// This will appear as 0x74785f7374617274 in logs
7
8
export const TX_START_PREFIX = 8392562855083340404n ;
8
9
// These are helper constants to decode tx effects from blob encoded fields
9
10
export const TX_START_PREFIX_BYTES_LENGTH = TX_START_PREFIX . toString ( 16 ) . length / 2 ;
10
11
// 7 bytes for: | 0 | txlen[0] | txlen[1] | 0 | REVERT_CODE_PREFIX | 0 | revertCode |
11
12
export const TX_EFFECT_PREFIX_BYTE_LENGTH = TX_START_PREFIX_BYTES_LENGTH + 7 ;
13
+ export const REVERT_CODE_PREFIX = 1 ;
12
14
13
15
/**
14
16
* Deserializes a blob buffer into an array of field elements.
@@ -66,11 +68,50 @@ export function deserializeEncodedBlobToFields(blob: BlobBuffer): Fr[] {
66
68
return array . slice ( 0 , fieldReader . cursor ) ;
67
69
}
68
70
71
+ /**
72
+ * Get the length of the transaction from the first field.
73
+ *
74
+ * @param firstField - The first field of the transaction.
75
+ * @returns The length of the transaction.
76
+ *
77
+ * @throws If the first field does not include the correct prefix - encoding invalid.
78
+ */
69
79
export function getLengthFromFirstField ( firstField : Fr ) : number {
80
+ // Check that the first field includes the correct prefix
81
+ if ( ! isValidFirstField ( firstField ) ) {
82
+ throw new Error ( 'Invalid prefix' ) ;
83
+ }
70
84
const buf = firstField . toBuffer ( ) . subarray ( - TX_EFFECT_PREFIX_BYTE_LENGTH ) ;
71
85
return new Fr ( buf . subarray ( TX_START_PREFIX_BYTES_LENGTH + 1 , TX_START_PREFIX_BYTES_LENGTH + 3 ) ) . toNumber ( ) ;
72
86
}
73
87
88
+ // NOTE: duplicated from circuit-types tx effect!
89
+ /**
90
+ * Determines whether a field is the first field of a tx effect
91
+ */
92
+ export function isValidFirstField ( field : Fr ) : boolean {
93
+ const buf = field . toBuffer ( ) ;
94
+ if (
95
+ ! buf
96
+ . subarray ( 0 , field . size - TX_EFFECT_PREFIX_BYTE_LENGTH )
97
+ . equals ( Buffer . alloc ( field . size - TX_EFFECT_PREFIX_BYTE_LENGTH ) )
98
+ ) {
99
+ return false ;
100
+ }
101
+ const sliced = buf . subarray ( - TX_EFFECT_PREFIX_BYTE_LENGTH ) ;
102
+ if (
103
+ // Checking we start with the correct prefix...
104
+ ! new Fr ( sliced . subarray ( 0 , TX_START_PREFIX_BYTES_LENGTH ) ) . equals ( new Fr ( TX_START_PREFIX ) ) ||
105
+ // ...and include the revert code prefix..
106
+ sliced [ sliced . length - 3 ] !== REVERT_CODE_PREFIX ||
107
+ // ...and the following revert code is valid.
108
+ sliced [ sliced . length - 1 ] > 4
109
+ ) {
110
+ return false ;
111
+ }
112
+ return true ;
113
+ }
114
+
74
115
/**
75
116
* Extract the fields from a blob buffer, but do not take into account encoding
76
117
* that will include trailing zeros.
0 commit comments