@@ -4,16 +4,15 @@ import {
4
4
type AbiType ,
5
5
type BasicValue ,
6
6
type ContractArtifact ,
7
+ ContractArtifactSchema ,
7
8
type ContractNote ,
8
9
type FieldLayout ,
9
10
type FunctionArtifact ,
10
11
FunctionType ,
11
12
type IntegerValue ,
12
- NoteSelector ,
13
13
type StructValue ,
14
14
type TypedStructFieldValue ,
15
15
} from '@aztec/foundation/abi' ;
16
- import { Fr } from '@aztec/foundation/fields' ;
17
16
18
17
import {
19
18
AZTEC_INITIALIZER_ATTRIBUTE ,
@@ -53,18 +52,7 @@ export function contractArtifactToBuffer(artifact: ContractArtifact): Buffer {
53
52
* @returns Deserialized artifact.
54
53
*/
55
54
export function contractArtifactFromBuffer ( buffer : Buffer ) : ContractArtifact {
56
- return JSON . parse ( buffer . toString ( 'utf-8' ) , ( key , value ) => {
57
- if ( key === 'bytecode' && typeof value === 'string' ) {
58
- return Buffer . from ( value , 'base64' ) ;
59
- }
60
- if ( typeof value === 'object' && value !== null && value . type === 'NoteSelector' ) {
61
- return new NoteSelector ( Number ( value . value ) ) ;
62
- }
63
- if ( typeof value === 'object' && value !== null && value . type === 'Fr' ) {
64
- return new Fr ( BigInt ( value . value ) ) ;
65
- }
66
- return value ;
67
- } ) ;
55
+ return ContractArtifactSchema . parse ( JSON . parse ( buffer . toString ( 'utf-8' ) ) ) ;
68
56
}
69
57
70
58
/**
@@ -133,7 +121,10 @@ type NoirCompiledContractFunction = NoirCompiledContract['functions'][number];
133
121
* @param contract - Parent contract.
134
122
* @returns Function artifact.
135
123
*/
136
- function generateFunctionArtifact ( fn : NoirCompiledContractFunction , contract : NoirCompiledContract ) : FunctionArtifact {
124
+ function generateFunctionArtifact (
125
+ fn : NoirCompiledContractFunction ,
126
+ contract : NoirCompiledContract ,
127
+ ) : Omit < FunctionArtifact , 'bytecode' > & { bytecode : string } {
137
128
if ( fn . custom_attributes === undefined ) {
138
129
throw new Error (
139
130
`No custom attributes found for contract function ${ fn . name } . Try rebuilding the contract with the latest nargo version.` ,
@@ -178,7 +169,7 @@ function generateFunctionArtifact(fn: NoirCompiledContractFunction, contract: No
178
169
isInitializer : fn . custom_attributes . includes ( AZTEC_INITIALIZER_ATTRIBUTE ) ,
179
170
parameters,
180
171
returnTypes,
181
- bytecode : Buffer . from ( fn . bytecode , 'base64' ) ,
172
+ bytecode : fn . bytecode ,
182
173
debugSymbols : fn . debug_symbols ,
183
174
errorTypes : fn . abi . error_types ,
184
175
...( fn . assert_messages ? { assertMessages : fn . assert_messages } : undefined ) ,
@@ -238,11 +229,11 @@ function getStorageLayout(input: NoirCompiledContract) {
238
229
return { } ;
239
230
}
240
231
241
- return storageFields . reduce ( ( acc : Record < string , FieldLayout > , field ) => {
232
+ return storageFields . reduce ( ( acc : Record < string , Omit < FieldLayout , 'slot' > & { slot : string } > , field ) => {
242
233
const name = field . name ;
243
234
const slot = field . value . fields [ 0 ] . value as IntegerValue ;
244
235
acc [ name ] = {
245
- slot : Fr . fromString ( slot . value ) ,
236
+ slot : slot . value ,
246
237
} ;
247
238
return acc ;
248
239
} , { } ) ;
@@ -262,7 +253,7 @@ function getNoteTypes(input: NoirCompiledContract) {
262
253
return { } ;
263
254
}
264
255
265
- return notes . reduce ( ( acc : Record < string , ContractNote > , note ) => {
256
+ return notes . reduce ( ( acc : Record < string , Omit < ContractNote , 'id' > & { id : string } > , note ) => {
266
257
const noteFields = note . fields ;
267
258
268
259
// We find note type id by looking for respective kinds as each of them is unique
@@ -274,7 +265,7 @@ function getNoteTypes(input: NoirCompiledContract) {
274
265
throw new Error ( `Could not find note type id, name or fields for note ${ note } ` ) ;
275
266
}
276
267
277
- const noteTypeId = NoteSelector . fromField ( Fr . fromString ( rawNoteTypeId . value ) ) ;
268
+ const noteTypeId = rawNoteTypeId . value as string ;
278
269
const name = rawName . value as string ;
279
270
280
271
// Note type id is encoded as a hex string
@@ -301,15 +292,15 @@ function getNoteTypes(input: NoirCompiledContract) {
301
292
*/
302
293
function generateContractArtifact ( contract : NoirCompiledContract , aztecNrVersion ?: string ) : ContractArtifact {
303
294
try {
304
- return {
295
+ return ContractArtifactSchema . parse ( {
305
296
name : contract . name ,
306
297
functions : contract . functions . map ( f => generateFunctionArtifact ( f , contract ) ) ,
307
298
outputs : contract . outputs ,
308
299
storageLayout : getStorageLayout ( contract ) ,
309
300
notes : getNoteTypes ( contract ) ,
310
301
fileMap : contract . file_map ,
311
302
...( aztecNrVersion ? { aztecNrVersion } : { } ) ,
312
- } ;
303
+ } ) ;
313
304
} catch ( err ) {
314
305
throw new Error ( `Could not generate contract artifact for ${ contract . name } : ${ err } ` ) ;
315
306
}
0 commit comments