@unirep/core
Version:
Client library for protocol related functions which are used in UniRep protocol.
141 lines (140 loc) • 4.67 kB
TypeScript
import { CircuitConfig } from '@unirep/circuits';
/**
* Type describing each field in the user-defined schema. Schema field type must be a `uint`.
* :::caution
* Replacement field must be `uint205`
* :::
* @example
* ```ts
* const schema: SchemaField = {
* name: 'posRep', // field name
* type: 'uint64', // uint*
* updatedBy: 'sum', // either update by adding or replacing user data
* }
* ```
*/
export type SchemaField = {
name: string;
type: string;
updateBy: 'sum' | 'replace';
};
/**
* Type to be used with a deployed Unirep contract object
*/
export type Attestation = {
fieldIndex: number;
change: bigint;
};
/**
* The `DataSchema` class abstracts UniRep data into a JavaScript object.
* This class can be used to encode and decode attestation data,
* and build attestations that are ready to be submitted to the UniRep smart contract.
* @example
* ```ts
* import { Attestation, DataSchema, SchemaField } from '@unirep/core'
*
* const schema: SchemaField[] = [
* {name: 'posRep', type: 'uint64', updateBy: 'sum',},
* {name: 'negRep', type: 'uint64', updateBy: 'sum',},
* {name: 'graffiti', type: 'uint205', updateBy: 'replace',},
* {name: 'postCount', type: 'uint49', updateBy: 'sum',},
* {name: 'commentCount', type: 'uint49', updateBy: 'sum',},
* {name: 'voteCount', type: 'uint49', updateBy: 'sum',},
* ]
*
* const d = new DataSchema(schema)
* ```
*/
export declare class DataSchema {
schema: any[];
config: CircuitConfig;
constructor(schema: SchemaField[], config?: CircuitConfig);
/**
* Verify a user-defined data schema
* @param schema The array of `SchemaField`
* @returns
* ```ts
* {
* ...schema: SchemaField, // exploded `SchemaField` fields
* dataIndex: number,
* offset: number, // bit offset in attester change
* bits: number // bits allocated
* }
* ```
*/
parseSchema(schema: SchemaField[]): any[];
/**
* Build an `Attestation` object to be used for a UniRep contract
* @param change The data change. If it is `sum` field, the data will be changed by addition. If it is `replacement` field, the data will be changed by replacement.
* @returns The attestation object will be submitted to the Unirep contract.
* @example
* **Sum field**
* ```ts
* // 10 will be added to the 'posRep' field in the user data
* const sumChange = { name: 'posRep', val: BigInt(10) }
* const sumAttestation: Attestation = d.buildAttestation(sumChange)
* ```
*
* **Replacement field**
* ```ts
* // 20 will replace the current value in the 'graffiti' field in user data
* const replacementChange = { name: 'graffiti', val: BigInt(20) }
* const replacementAttestation: Attestation = d.buildAttestation(replacementChange)
* ```
*/
buildAttestation(change: {
name: string;
val: bigint;
}): Attestation;
/**
* Build multiple `Attestation` objects to be used for a UniRep contract
* @param changes The array of data change.
* @returns The array of attestations will be submitted to the Unirep contract.
* @example
* ```ts
* // Multiple attestations can be built using `buildAttestations()`
* const changes = [
* { name: 'posRep', val: BigInt(10) },
* { name: 'negRep', val: BigInt(10) },
* { name: 'negRep', val: BigInt(20) },
* { name: 'graffiti', val: BigInt(30) },
* ]
*
* //Returns two `Attestation` objects: 'posRep' and 'negRep' attestations are combined into one attestation
* const attestations: Attestation[] = d.buildAttestations(changes)
* ```
*/
buildAttestations(changes: {
name: string;
val: bigint;
}[]): Attestation[];
/**
* Parse encoded schema, producing a dictionary of user-defined field names and attestation values
* @param data The raw data appended to the Unirep contract.
* @returns The names of the data and its values.
* @example
* ```ts
* // JS literal representing emitted data from a UniRep contract
* const data = [
* 553402322211286548490n,
* 0n,
* 0n,
* 0n,
* 205688069665150755269371147819668813122841983204197482918576158n,
* 0n
* ]
*
* const parsedData = d.parseData(data)
* // Result:
* // parsedData = {
* // posRep: 10n,
* // negRep: 30n,
* // graffiti: 30n,
* // postCount: 0n,
* // commentCount: 0n,
* // voteCount: 0n
* // }
* ```
*/
parseData(data: bigint[]): any;
}