Arrays
Arrays in provable code must be of a fixed size. Normal JavaScript arrays are a suitable value to pass into a provable function, but
Array<T>
is not a suitable type. o1js exports the Provable.Array
helper to generate provable array classes.
const FieldArray3 = Provable.Array(Field, 3);
const SignatureArray2 = Provable.Array(Signature, 2);
JavaScript arrays will satisfy the provable array type as long as they are of the correct size, so you don't need to worry too much about casting them back and forth.
const fieldArray = Provable.witness(FieldArray3, () => {
return [Field(1), Field(2), Field(3)];
});
fieldArray.map((x) => x.toBigInt()); // [1n, 2n, 3n]
Here are some invalid examples:
// Invalid: Dynamic arrays are not allowed in provable code
// INVALID - DO NOT COPY
let dynamicArray = [];
dynamicArray.push(Field(1)); // Error: Cannot use dynamic array operations
dynamicArray.push(Field(2)); // Error: Array size must be known at compile time
// Invalid: Array size cannot change
// INVALID - DO NOT COPY
const arr = Provable.Array(Field, 2).empty();
arr.push(Field(1)); // Error: push() is not available on provable arrays
Read more at the language reference: Provable.Array.