From 7fbe7d034cda6dc14ffa1c808d943e1b6f1efb05 Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 4 Nov 2024 09:03:39 +0100 Subject: [PATCH] work on nested recrods --- src/credentials/dynamic-record.test.ts | 28 ++++++++++++++++++++++---- src/credentials/dynamic-record.ts | 13 +++++++++--- src/util.ts | 7 +++++-- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/credentials/dynamic-record.test.ts b/src/credentials/dynamic-record.test.ts index 357a9ed..522832e 100644 --- a/src/credentials/dynamic-record.test.ts +++ b/src/credentials/dynamic-record.test.ts @@ -35,13 +35,15 @@ const OriginalSchema = Schema({ fifth: { field: Field, string: String }, }); -let original = OriginalSchema.from({ +let input = { first: 1, second: true, third: 'something', fourth: 123n, fifth: { field: 2, string: '...' }, -}); +}; + +let original = OriginalSchema.from(input); const expectedHash = OriginalSchema.hash(original); const OriginalWrappedInStruct = Struct(OriginalSchema.schema); @@ -52,8 +54,12 @@ let originalStruct = OriginalWrappedInStruct.fromValue(original); const Subschema = DynamicRecord( { // not necessarily in order - fourth: UInt64, third: DynamicString({ maxLength: 10 }), + fifth: DynamicRecord( + { field: Field, string: DynamicString({ maxLength: 5 }) }, + { maxEntries: 5 } + ), + fourth: UInt64, first: Field, }, { maxEntries: 10 } @@ -69,11 +75,25 @@ async function circuit() { await test('DynamicRecord.get()', () => { record.get('first').assertEquals(1, 'first'); Provable.assertEqual(String, record.get('third'), String.from('something')); - record.get('fourth').assertEquals(UInt64.from(123n)); + + // TODO fix hashing so that this works + // const Fifth = DynamicRecord( + // { field: Field, string: DynamicString({ maxLength: 5 }) }, + // { maxEntries: 5 } + // ); + // Provable.assertEqual( + // Fifth, + // record.get('fifth'), + // Fifth.from({ field: 2, string: '...' }) + // ); }); await test('DynamicRecord.getAny()', () => { record.getAny(Bool, 'second').assertEquals(true, 'second'); + record.getAny(UInt64, 'fourth').assertEquals(UInt64.from(123n)); + + // TODO fix hashing so that this no longer works + // records should be hashed in dynamic record form const Fifth = Struct({ field: Field, string: String }); Provable.assertEqual( Fifth, diff --git a/src/credentials/dynamic-record.ts b/src/credentials/dynamic-record.ts index 6135ab3..acb48bd 100644 --- a/src/credentials/dynamic-record.ts +++ b/src/credentials/dynamic-record.ts @@ -78,7 +78,9 @@ function DynamicRecord< .empty(); return class DynamicRecord extends DynamicRecordBase { - static from(value: T): DynamicRecordBase { + // TODO: actually, the type should be From<> for the known subfields and unchanged for the unknown ones + // or anything really, when we have general hashing? + static from(value: From): DynamicRecordBase { return DynamicRecord.provable.fromValue(value); } @@ -107,10 +109,15 @@ function DynamicRecord< assertExtendsShape(actual, knownShape); let entries = Object.entries(actual).map(([key, value]) => { - let type = NestedProvable.get(NestedProvable.fromValue(value)); + let type = NestedProvable.get( + key in knownShape + ? // ? (knownShape[key] as any) + NestedProvable.fromValue(value) // TODO change after making hashing general + : NestedProvable.fromValue(value) + ); return { key: packStringToField(key).toBigInt(), - value: packToField(type, value).toBigInt(), + value: packToField(type, type.fromValue(value)).toBigInt(), }; }); return { entries: pad(entries, maxEntries, undefined), actual }; diff --git a/src/util.ts b/src/util.ts index 886ee49..9e631d2 100644 --- a/src/util.ts +++ b/src/util.ts @@ -17,7 +17,7 @@ export { function assert( condition: boolean, - message?: string | (() => string) + message?: string | (() => string | undefined) ): asserts condition { if (!condition) { message = typeof message === 'function' ? message() : message; @@ -40,7 +40,10 @@ function assertIsObject( ): asserts obj is object | Function { assert( (typeof obj === 'object' && obj !== null) || typeof obj === 'function', - message + () => { + // console.log('not an object:', obj); + return message; + } ); }