diff --git a/package.json b/package.json index cfc2da7..f59b28c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@auxo-dev/auxo-libs", - "version": "0.2.8", + "version": "0.2.9", "description": "", "author": "", "license": "Apache-2.0", diff --git a/src/DynamicArray.test.ts b/src/DynamicArray.test.ts index 07154da..b53b3fa 100644 --- a/src/DynamicArray.test.ts +++ b/src/DynamicArray.test.ts @@ -55,9 +55,7 @@ describe('DynamicArray', () => { ); fieldDeserialized.length.assertEquals(fieldArray.length); for (let i = 0; i < fieldArray.values.length; i++) { - (fieldDeserialized as DynamicFieldArray) - .get(Field(i)) - .assertEquals(fieldArray.get(Field(i))); + fieldDeserialized.get(Field(i)).assertEquals(fieldArray.get(Field(i))); } // Group @@ -69,9 +67,7 @@ describe('DynamicArray', () => { ); groupDeserialized.length.assertEquals(groupArray.length); for (let i = 0; i < groupArray.values.length; i++) { - (groupDeserialized as DynamicGroupArray) - .get(Field(i)) - .assertEquals(groupArray.get(Field(i))); + groupDeserialized.get(Field(i)).assertEquals(groupArray.get(Field(i))); } // Scalar @@ -86,9 +82,7 @@ describe('DynamicArray', () => { ); scalarDeserialized.length.assertEquals(scalarArray.length); for (let i = 0; i < scalarArray.values.length; i++) { - (scalarDeserialized as DynamicScalarArray) - .get(Field(i)) - .assertEquals(scalarArray.get(Field(i))); + scalarDeserialized.get(Field(i)).assertEquals(scalarArray.get(Field(i))); } }); }); diff --git a/src/DynamicArray.ts b/src/DynamicArray.ts index 057ef66..a1d6fef 100644 --- a/src/DynamicArray.ts +++ b/src/DynamicArray.ts @@ -45,7 +45,6 @@ export function PublicKeyDynamicArray(maxLength: number) { export function DynamicArray(type: ProvablePure, maxLength: number) { const _type = hashable(type); - return class _DynamicArray extends Struct({ length: Field, values: Provable.Array(type, maxLength), @@ -54,6 +53,22 @@ export function DynamicArray(type: ProvablePure, maxLength: number) { return new _DynamicArray(values); } + static fromFields(fields: Field[]): _DynamicArray { + let length = fields[0]; + let sizeInFields = type.sizeInFields(); + let values: T[] = []; + Provable.asProver(() => { + for (let i = 0; i < Number(length); i++) { + values.push( + type.fromFields( + fields.slice(1 + sizeInFields * i, 1 + sizeInFields * (i + 1)) + ) + ); + } + }); + return new _DynamicArray(values); + } + static empty(length?: Field): _DynamicArray { const arr = new _DynamicArray(); arr.length = length ?? Field(0);