Skip to content

Commit

Permalink
Fix optional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolezhniuk committed Jul 18, 2023
1 parent 64052d8 commit 74d80d3
Show file tree
Hide file tree
Showing 11 changed files with 419 additions and 448 deletions.
176 changes: 84 additions & 92 deletions src/circuits/atomic-query-mtp-v2-on-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ import { byteDecoder, byteEncoder } from '../utils';
*/
export class AtomicQueryMTPV2OnChainInputs extends BaseConfig {
// auth
id?: Id;
profileNonce?: bigint;
claimSubjectProfileNonce?: bigint;
id!: Id;
profileNonce!: bigint;
claimSubjectProfileNonce!: bigint;
// claim issued for user
claim?: ClaimWithMTPProof;
skipClaimRevocationCheck?: boolean;
requestID?: bigint;
claim!: ClaimWithMTPProof;
skipClaimRevocationCheck!: boolean;
requestID!: bigint;

currentTimeStamp?: number;
currentTimeStamp!: number;

authClaim?: Claim;
authClaimIncMtp?: Proof;
authClaimNonRevMtp?: Proof;
treeState?: TreeState;
authClaim!: Claim;
authClaimIncMtp!: Proof;
authClaimNonRevMtp!: Proof;
treeState!: TreeState;

gistProof?: GISTProof;
gistProof!: GISTProof;

signature?: Signature;
challenge?: bigint;
signature!: Signature;
challenge!: bigint;

// query
query?: Query;
query!: Query;

/**
* Validate inputs
Expand All @@ -60,7 +60,7 @@ export class AtomicQueryMTPV2OnChainInputs extends BaseConfig {
if (!this.authClaimNonRevMtp) {
throw new Error(CircuitError.EmptyAuthClaimNonRevProof);
}
if (!this.gistProof?.proof) {
if (!this.gistProof.proof) {
throw new Error(CircuitError.EmptyGISTProof);
}
if (!this.signature) {
Expand All @@ -78,68 +78,63 @@ export class AtomicQueryMTPV2OnChainInputs extends BaseConfig {
*/
inputsMarshal(): Uint8Array {
this.validate();
if (this.query?.valueProof) {
if (this.query.valueProof) {
this.query.validate();
this.query.valueProof.validate();
}

const valueProof = this.query?.valueProof ?? new ValueProof();
const valueProof = this.query.valueProof ?? new ValueProof();
const s: Partial<atomicQueryMTPV2OnChainCircuitInputs> = {
requestID: this.requestID?.toString(),
userGenesisID: this.id?.bigInt().toString(),
profileNonce: this.profileNonce?.toString(),
requestID: this.requestID.toString(),
userGenesisID: this.id.bigInt().toString(),
profileNonce: this.profileNonce.toString(),
claimSubjectProfileNonce: this.claimSubjectProfileNonce?.toString(),
issuerID: this.claim?.issuerID?.bigInt().toString(),
issuerClaim: this.claim?.claim?.marshalJson(),
issuerClaimMtp: this.claim?.incProof?.proof
? prepareSiblingsStr(this.claim.incProof.proof, this.getMTLevel())
: undefined,
issuerClaimClaimsTreeRoot: this.claim?.incProof?.treeState?.claimsRoot?.string(),
issuerClaimRevTreeRoot: this.claim?.incProof?.treeState?.revocationRoot?.string(),
issuerClaimRootsTreeRoot: this.claim?.incProof?.treeState?.rootOfRoots?.string(),
issuerClaimIdenState: this.claim?.incProof?.treeState?.state?.string(),
issuerClaimNonRevMtp: this.claim?.nonRevProof?.proof
? prepareSiblingsStr(this.claim.nonRevProof?.proof, this.getMTLevel())
: undefined,
issuerClaimNonRevClaimsTreeRoot: this.claim?.nonRevProof?.treeState?.claimsRoot?.string(),
issuerClaimNonRevRevTreeRoot: this.claim?.nonRevProof?.treeState?.revocationRoot?.string(),
issuerClaimNonRevRootsTreeRoot: this.claim?.nonRevProof?.treeState?.rootOfRoots?.string(),
issuerClaimNonRevState: this.claim?.nonRevProof?.treeState?.state?.string(),
claimSchema: this.claim?.claim?.getSchemaHash().bigInt().toString(),
issuerID: this.claim.issuerID?.bigInt().toString(),
issuerClaim: this.claim.claim?.marshalJson(),
issuerClaimMtp:
this.claim.incProof?.proof &&
prepareSiblingsStr(this.claim.incProof.proof, this.getMTLevel()),
issuerClaimClaimsTreeRoot: this.claim.incProof?.treeState?.claimsRoot?.string(),
issuerClaimRevTreeRoot: this.claim.incProof?.treeState?.revocationRoot?.string(),
issuerClaimRootsTreeRoot: this.claim.incProof?.treeState?.rootOfRoots?.string(),
issuerClaimIdenState: this.claim.incProof?.treeState?.state?.string(),
issuerClaimNonRevMtp:
this.claim.nonRevProof?.proof &&
prepareSiblingsStr(this.claim.nonRevProof?.proof, this.getMTLevel()),
issuerClaimNonRevClaimsTreeRoot: this.claim.nonRevProof?.treeState?.claimsRoot?.string(),
issuerClaimNonRevRevTreeRoot: this.claim.nonRevProof?.treeState?.revocationRoot?.string(),
issuerClaimNonRevRootsTreeRoot: this.claim.nonRevProof?.treeState?.rootOfRoots?.string(),
issuerClaimNonRevState: this.claim.nonRevProof?.treeState?.state?.string(),
claimSchema: this.claim.claim?.getSchemaHash().bigInt().toString(),
claimPathMtp: prepareSiblingsStr(valueProof.mtp, this.getMTLevelsClaimMerklization()),
claimPathValue: valueProof.value.toString(),
operator: this.query?.operator,
slotIndex: this.query?.slotIndex,
operator: this.query.operator,
slotIndex: this.query.slotIndex,
timestamp: this.currentTimeStamp ?? undefined,
isRevocationChecked: 1,
authClaim: this.authClaim?.marshalJson(),
authClaimIncMtp: this.authClaimIncMtp
? prepareSiblingsStr(this.authClaimIncMtp, this.getMTLevel())
: undefined,
authClaimNonRevMtp: this.authClaimNonRevMtp
? prepareSiblingsStr(this.authClaimNonRevMtp, this.getMTLevel())
: undefined,
challenge: this.challenge?.toString(),
challengeSignatureR8x: this.signature?.R8[0].toString(),
challengeSignatureR8y: this.signature?.R8[1].toString(),
challengeSignatureS: this.signature?.S.toString(),
userClaimsTreeRoot: this.treeState?.claimsRoot?.string(),
userRevTreeRoot: this.treeState?.revocationRoot?.string(),
userRootsTreeRoot: this.treeState?.rootOfRoots?.string(),
userState: this.treeState?.state?.string(),
gistRoot: this.gistProof?.root?.string(),
gistMtp: this.gistProof
? prepareSiblingsStr(this.gistProof.proof, this.getMTLevelOnChain())
: undefined
authClaim: this.authClaim.marshalJson(),
authClaimIncMtp:
this.authClaimIncMtp && prepareSiblingsStr(this.authClaimIncMtp, this.getMTLevel()),
authClaimNonRevMtp:
this.authClaimNonRevMtp && prepareSiblingsStr(this.authClaimNonRevMtp, this.getMTLevel()),
challenge: this.challenge.toString(),
challengeSignatureR8x: this.signature.R8[0].toString(),
challengeSignatureR8y: this.signature.R8[1].toString(),
challengeSignatureS: this.signature.S.toString(),
userClaimsTreeRoot: this.treeState.claimsRoot?.string(),
userRevTreeRoot: this.treeState.revocationRoot?.string(),
userRootsTreeRoot: this.treeState.rootOfRoots?.string(),
userState: this.treeState.state?.string(),
gistRoot: this.gistProof.root?.string(),
gistMtp: this.gistProof && prepareSiblingsStr(this.gistProof.proof, this.getMTLevelOnChain())
};

if (this.skipClaimRevocationCheck) {
s.isRevocationChecked = 0;
}

const nodeAuxNonRev = this.claim?.nonRevProof?.proof
? getNodeAuxValue(this.claim.nonRevProof.proof)
: undefined;
const nodeAuxNonRev =
this.claim.nonRevProof?.proof && getNodeAuxValue(this.claim.nonRevProof.proof);
s.issuerClaimNonRevMtpAuxHi = nodeAuxNonRev?.key.bigInt().toString();
s.issuerClaimNonRevMtpAuxHv = nodeAuxNonRev?.value.bigInt().toString();
s.issuerClaimNonRevMtpNoAux = nodeAuxNonRev?.noAux;
Expand All @@ -151,22 +146,19 @@ export class AtomicQueryMTPV2OnChainInputs extends BaseConfig {
s.claimPathMtpAuxHv = nodAuxJSONLD.value.bigInt().toString();

s.claimPathKey = valueProof.path.toString();
const values = this.query?.values
? prepareCircuitArrayValues(this.query.values, this.getValueArrSize())
: undefined;
s.value = values ? bigIntArrayToStringArray(values) : undefined;

const nodeAuxAuth = this.authClaimNonRevMtp
? getNodeAuxValue(this.authClaimNonRevMtp)
: undefined;
s.authClaimNonRevMtpAuxHi = nodeAuxAuth?.key.string();
s.authClaimNonRevMtpAuxHv = nodeAuxAuth?.value.string();
s.authClaimNonRevMtpNoAux = nodeAuxAuth?.noAux;

const globalNodeAux = this.gistProof ? getNodeAuxValue(this.gistProof.proof) : undefined;
s.gistMtpAuxHi = globalNodeAux?.key.string();
s.gistMtpAuxHv = globalNodeAux?.value.string();
s.gistMtpNoAux = globalNodeAux?.noAux;
const values =
this.query.values && prepareCircuitArrayValues(this.query.values, this.getValueArrSize());
s.value = values && bigIntArrayToStringArray(values);

const nodeAuxAuth = this.authClaimNonRevMtp && getNodeAuxValue(this.authClaimNonRevMtp);
s.authClaimNonRevMtpAuxHi = nodeAuxAuth.key.string();
s.authClaimNonRevMtpAuxHv = nodeAuxAuth.value.string();
s.authClaimNonRevMtpNoAux = nodeAuxAuth.noAux;

const globalNodeAux = this.gistProof && getNodeAuxValue(this.gistProof.proof);
s.gistMtpAuxHi = globalNodeAux.key.string();
s.gistMtpAuxHv = globalNodeAux.value.string();
s.gistMtpNoAux = globalNodeAux.noAux;

return byteEncoder.encode(JSON.stringify(s));
}
Expand All @@ -179,7 +171,7 @@ interface atomicQueryMTPV2OnChainCircuitInputs {
profileNonce: string;
claimSubjectProfileNonce: string;
issuerID: string;
issuerClaim?: string[];
issuerClaim: string[];

issuerClaimMtp: string[];
issuerClaimClaimsTreeRoot: string;
Expand All @@ -204,8 +196,8 @@ interface atomicQueryMTPV2OnChainCircuitInputs {
claimPathMtp: string[];
claimPathMtpNoAux: string; // 1 if aux node is empty,
// 0 if non-empty or for inclusion proofs
claimPathMtpAuxHi?: string; // 0 for inclusion proof
claimPathMtpAuxHv?: string; // 0 for inclusion proof
claimPathMtpAuxHi: string; // 0 for inclusion proof
claimPathMtpAuxHv: string; // 0 for inclusion proof
claimPathKey: string; // hash of path in merklized json-ld document
claimPathValue: string; // value in this path in merklized json-ld document

Expand Down Expand Up @@ -252,17 +244,17 @@ interface atomicQueryMTPV2OnChainCircuitInputs {
* @extends {BaseConfig}
*/
export class AtomicQueryMTPV2OnChainPubSignals extends BaseConfig {
requestID?: bigint;
userID?: Id;
issuerID?: Id;
issuerClaimIdenState?: Hash;
issuerClaimNonRevState?: Hash;
timestamp?: number;
merklized?: number;
isRevocationChecked?: number; // 0 revocation not check, // 1 for check revocation
circuitQueryHash?: bigint;
challenge?: bigint;
gistRoot?: Hash;
requestID!: bigint;
userID!: Id;
issuerID!: Id;
issuerClaimIdenState!: Hash;
issuerClaimNonRevState!: Hash;
timestamp!: number;
merklized!: number;
isRevocationChecked!: number; // 0 revocation not check, // 1 for check revocation
circuitQueryHash!: bigint;
challenge!: bigint;
gistRoot!: Hash;

/**
*
Expand Down
73 changes: 36 additions & 37 deletions src/circuits/atomic-query-mtp-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import { byteDecoder, byteEncoder } from '../utils';
*/
export class AtomicQueryMTPV2Inputs extends BaseConfig {
// auth
id?: Id;
profileNonce?: bigint;
claimSubjectProfileNonce?: bigint;
id!: Id;
profileNonce!: bigint;
claimSubjectProfileNonce!: bigint;
// claim issued for user
claim?: ClaimWithMTPProof;
skipClaimRevocationCheck?: boolean;
requestID?: bigint;
claim!: ClaimWithMTPProof;
skipClaimRevocationCheck!: boolean;
requestID!: bigint;

currentTimeStamp?: number;
currentTimeStamp!: number;

// query
query?: Query;
query!: Query;

/**
* Validate AtomicQueryMTPV2 inputs
Expand All @@ -52,49 +52,49 @@ export class AtomicQueryMTPV2Inputs extends BaseConfig {
*/
inputsMarshal(): Uint8Array {
this.validate();
if (this.query?.valueProof) {
if (this.query.valueProof) {
this.query.validate();
this.query.valueProof.validate();
}

const valueProof = this.query?.valueProof ?? new ValueProof();
const valueProof = this.query.valueProof ?? new ValueProof();

const s: Partial<AtomicQueryMTPV2CircuitInputs> = {
requestID: this.requestID?.toString(),
userGenesisID: this.id?.bigInt().toString(),
requestID: this.requestID.toString(),
userGenesisID: this.id.bigInt().toString(),
profileNonce: this.profileNonce?.toString(),
claimSubjectProfileNonce: this.claimSubjectProfileNonce?.toString(),
issuerID: this.claim?.issuerID?.bigInt().toString(),
issuerClaim: this.claim?.claim?.marshalJson(),
issuerClaimMtp: this.claim?.incProof?.proof
? prepareSiblingsStr(this.claim.incProof.proof, this.getMTLevel())
: undefined,
issuerClaimClaimsTreeRoot: this.claim?.incProof?.treeState?.claimsRoot?.bigInt().toString(),
issuerClaimRevTreeRoot: this.claim?.incProof?.treeState?.revocationRoot?.bigInt().toString(),
issuerClaimRootsTreeRoot: this.claim?.incProof?.treeState?.rootOfRoots?.bigInt().toString(),
issuerClaimIdenState: this.claim?.incProof?.treeState?.state?.bigInt().toString(),
issuerClaimNonRevMtp: this.claim?.nonRevProof?.proof
? prepareSiblingsStr(this.claim.nonRevProof.proof, this.getMTLevel())
: undefined,
issuerClaimNonRevClaimsTreeRoot: this.claim?.nonRevProof?.treeState?.claimsRoot
issuerID: this.claim.issuerID?.bigInt().toString(),
issuerClaim: this.claim.claim?.marshalJson(),
issuerClaimMtp:
this.claim.incProof?.proof &&
prepareSiblingsStr(this.claim.incProof.proof, this.getMTLevel()),
issuerClaimClaimsTreeRoot: this.claim.incProof?.treeState?.claimsRoot?.bigInt().toString(),
issuerClaimRevTreeRoot: this.claim.incProof?.treeState?.revocationRoot?.bigInt().toString(),
issuerClaimRootsTreeRoot: this.claim.incProof?.treeState?.rootOfRoots?.bigInt().toString(),
issuerClaimIdenState: this.claim.incProof?.treeState?.state?.bigInt().toString(),
issuerClaimNonRevMtp:
this.claim.nonRevProof?.proof &&
prepareSiblingsStr(this.claim.nonRevProof.proof, this.getMTLevel()),
issuerClaimNonRevClaimsTreeRoot: this.claim.nonRevProof?.treeState?.claimsRoot
?.bigInt()
.toString(),
issuerClaimNonRevRevTreeRoot: this.claim?.nonRevProof?.treeState?.revocationRoot
issuerClaimNonRevRevTreeRoot: this.claim.nonRevProof?.treeState?.revocationRoot
?.bigInt()
.toString(),
issuerClaimNonRevRootsTreeRoot: this.claim?.nonRevProof?.treeState?.rootOfRoots
issuerClaimNonRevRootsTreeRoot: this.claim.nonRevProof?.treeState?.rootOfRoots
?.bigInt()
.toString(),
issuerClaimNonRevState: this.claim?.nonRevProof?.treeState?.state?.bigInt().toString(),
claimSchema: this.claim?.claim?.getSchemaHash().bigInt().toString(),
issuerClaimNonRevState: this.claim.nonRevProof?.treeState?.state?.bigInt().toString(),
claimSchema: this.claim.claim?.getSchemaHash().bigInt().toString(),
claimPathMtp: prepareSiblingsStr(valueProof.mtp, this.getMTLevelsClaimMerklization()),
claimPathValue: valueProof.value.toString(),
operator: this.query?.operator,
slotIndex: this.query?.slotIndex,
timestamp: this.currentTimeStamp ?? undefined
operator: this.query.operator,
slotIndex: this.query.slotIndex,
timestamp: this.currentTimeStamp
};

const nodeAux = this.claim ? getNodeAuxValue(this.claim.nonRevProof?.proof) : undefined;
const nodeAux = getNodeAuxValue(this.claim.nonRevProof?.proof);
s.issuerClaimNonRevMtpAuxHi = nodeAux?.key.bigInt().toString();
s.issuerClaimNonRevMtpAuxHv = nodeAux?.value.bigInt().toString();
s.issuerClaimNonRevMtpNoAux = nodeAux?.noAux;
Expand All @@ -113,11 +113,10 @@ export class AtomicQueryMTPV2Inputs extends BaseConfig {
s.isRevocationChecked = 1;
}

const values = this.query?.values
? prepareCircuitArrayValues(this.query.values, this.getValueArrSize())
: undefined;
const values =
this.query.values && prepareCircuitArrayValues(this.query.values, this.getValueArrSize());

s.value = values ? bigIntArrayToStringArray(values) : undefined;
s.value = bigIntArrayToStringArray(values);

return byteEncoder.encode(JSON.stringify(s));
}
Expand Down
Loading

0 comments on commit 74d80d3

Please sign in to comment.