Skip to content

Commit

Permalink
work on nested recrods
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Nov 4, 2024
1 parent 1be834a commit 7fbe7d0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
28 changes: 24 additions & 4 deletions src/credentials/dynamic-record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 }
Expand All @@ -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,
Expand Down
13 changes: 10 additions & 3 deletions src/credentials/dynamic-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ function DynamicRecord<
.empty();

return class DynamicRecord extends DynamicRecordBase<TKnown> {
static from<T extends TKnown>(value: T): DynamicRecordBase<TKnown> {
// 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<A extends AKnown>(value: From<A>): DynamicRecordBase<TKnown> {
return DynamicRecord.provable.fromValue(value);
}

Expand Down Expand Up @@ -107,10 +109,15 @@ function DynamicRecord<
assertExtendsShape(actual, knownShape);

let entries = Object.entries<unknown>(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 };
Expand Down
7 changes: 5 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
);
}

Expand Down

0 comments on commit 7fbe7d0

Please sign in to comment.