mina-attestations
Version:
Private Attestations on Mina
94 lines • 2.64 kB
JavaScript
/**
* This is a little library to streamline the creation of new provable types.
*/
import { provable, } from 'o1js';
export { TypeBuilder, TypeBuilderPure };
class TypeBuilder {
type;
constructor(type) {
this.type = type;
}
static shape(nested) {
return new TypeBuilder(provable(nested));
}
build() {
return this.type;
}
forClass(Class) {
return this.forConstructor((t) => new Class(t));
}
forConstructor(constructor) {
let type = this.type;
return new TypeBuilder({
...type,
fromFields(fields, aux) {
return constructor(type.fromFields(fields, aux));
},
fromValue(value) {
return constructor(type.fromValue(value));
},
empty() {
return constructor(type.empty());
},
toCanonical(x) {
if (type.toCanonical === undefined)
return x;
return constructor(type.toCanonical(x));
},
});
}
mapValue(transform) {
let type = this.type;
return new TypeBuilder({
...type,
toValue(value) {
return transform.there(type.toValue(value));
},
fromValue(value) {
if ('backAndDistinguish' in transform) {
return type.fromValue(transform.backAndDistinguish(value));
}
if (transform.distinguish(value))
return value;
return type.fromValue(transform.back(value));
},
});
}
replaceCheck(check) {
return new TypeBuilder({ ...this.type, check });
}
withAdditionalCheck(check) {
let originalCheck = this.type.check;
return this.replaceCheck((x) => {
originalCheck(x);
check(x);
});
}
hashInput(toInput) {
let type = this.type;
return new TypeBuilder({ ...type, toInput });
}
}
class TypeBuilderPure extends TypeBuilder {
type;
constructor(type) {
super(type);
this.type = type;
}
forClass(Class) {
return super.forClass(Class);
}
forConstructor(constructor) {
return super.forConstructor(constructor);
}
mapValue(transform) {
return super.mapValue(transform);
}
replaceCheck(check) {
return super.replaceCheck(check);
}
withAdditionalCheck(check) {
return super.withAdditionalCheck(check);
}
}
//# sourceMappingURL=provable-type-builder.js.map