mina-attestations
Version:
Private Attestations on Mina
57 lines • 1.99 kB
JavaScript
import { UInt64, } from 'o1js';
import { assert, mapObject, zipObjects } from "../util.js";
import { ProvableType, } from "../o1js-missing.js";
import { provableTypeOf } from "./dynamic-hash.js";
import { NestedProvable } from "../nested.js";
export { Schema };
Schema.String = { type: 'string' };
Schema.Number = { type: 'number' };
Schema.Boolean = { type: 'boolean' };
Schema.Array = function SchemaArray(inner) {
return { type: 'array', inner };
};
function Schema(schema) {
return {
schema,
from(value) {
return validateAndConvert(schema, value);
},
nestedType(value) {
return mapObject(value, (v) => provableTypeOf(v));
},
type(value) {
return NestedProvable.get(this.nestedType(value));
},
};
}
// loosely-typed versions of the above functions that work without a schema object
Schema.nestedType = function nestedType(value) {
return mapObject(value, (v) => provableTypeOf(v));
};
Schema.type = function type(value) {
return NestedProvable.get(Schema.nestedType(value));
};
function validateAndConvert(schema, value) {
if (ProvableType.isProvableHashableType(schema)) {
return ProvableType.get(schema).fromValue(value);
}
switch (schema.type) {
case 'string':
assert(typeof value === 'string');
return value;
case 'number':
assert(typeof value === 'number');
assert(Number.isInteger(value));
return UInt64.from(value);
case 'boolean':
assert(typeof value === 'boolean');
return value;
case 'array':
assert(Array.isArray(value));
return value.map((v) => validateAndConvert(schema.inner, v));
default:
assert(typeof value === 'object' && value !== null);
return mapObject(zipObjects(schema, value), ([s, v]) => validateAndConvert(s, v));
}
}
//# sourceMappingURL=schema.js.map