@0xpolygonid/js-sdk
Version:
SDK to work with Polygon ID
282 lines (281 loc) • 13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AtomicQueryV3PubSignals = exports.AtomicQueryV3Inputs = void 0;
const js_iden3_core_1 = require("@iden3/js-iden3-core");
const common_1 = require("./common");
const models_1 = require("./models");
const js_merkletree_1 = require("@iden3/js-merkletree");
const utils_1 = require("../utils");
const verifiable_1 = require("../verifiable");
const zero = '0';
/**
* AtomicQueryV3Inputs ZK private inputs for credentialAtomicQueryV3.circom
*
* @beta
* @class AtomicQueryV3Inputs
* @extends {BaseConfig}
*/
class AtomicQueryV3Inputs extends common_1.BaseConfig {
validate() {
if (!this.requestID) {
throw new Error(models_1.CircuitError.EmptyRequestID);
}
if (!this.claim.nonRevProof.proof) {
throw new Error(models_1.CircuitError.EmptyClaimNonRevProof);
}
if (!this.query.values) {
throw new Error(models_1.CircuitError.EmptyQueryValue);
}
this.query.validateValueArraySize(this.getValueArrSize());
if (!this.proofType) {
throw new Error(models_1.CircuitError.InvalidProofType);
}
if (this.proofType === verifiable_1.ProofType.BJJSignature) {
if (!this.claim.signatureProof?.issuerAuthIncProof.proof) {
throw new Error(models_1.CircuitError.EmptyIssuerAuthClaimProof);
}
if (!this.claim.signatureProof.issuerAuthNonRevProof.proof) {
throw new Error(models_1.CircuitError.EmptyIssuerAuthClaimNonRevProof);
}
if (!this.claim.signatureProof.signature) {
throw new Error(models_1.CircuitError.EmptyClaimSignature);
}
}
if (this.proofType === verifiable_1.ProofType.Iden3SparseMerkleTreeProof) {
if (!this.claim?.incProof?.proof) {
throw new Error(models_1.CircuitError.EmptyClaimProof);
}
}
}
fillMTPProofsWithZero(s) {
s.issuerClaimMtp = (0, common_1.prepareSiblingsStr)(new js_merkletree_1.Proof(), this.getMTLevel());
s.issuerClaimClaimsTreeRoot = js_merkletree_1.ZERO_HASH.bigInt().toString();
s.issuerClaimRevTreeRoot = js_merkletree_1.ZERO_HASH.bigInt().toString();
s.issuerClaimRootsTreeRoot = js_merkletree_1.ZERO_HASH.bigInt().toString();
s.issuerClaimIdenState = js_merkletree_1.ZERO_HASH.bigInt().toString();
}
fillSigProofWithZero(s) {
s.issuerClaimSignatureR8x = zero;
s.issuerClaimSignatureR8y = zero;
s.issuerClaimSignatureS = zero;
s.issuerAuthClaim = new js_iden3_core_1.Claim().marshalJson();
s.issuerAuthClaimMtp = (0, common_1.prepareSiblingsStr)(new js_merkletree_1.Proof(), this.getMTLevel());
s.issuerAuthClaimsTreeRoot = zero;
s.issuerAuthRevTreeRoot = zero;
s.issuerAuthRootsTreeRoot = zero;
s.issuerAuthClaimNonRevMtp = (0, common_1.prepareSiblingsStr)(new js_merkletree_1.Proof(), this.getMTLevel());
s.issuerAuthClaimNonRevMtpAuxHi = js_merkletree_1.ZERO_HASH.bigInt().toString();
s.issuerAuthClaimNonRevMtpAuxHv = js_merkletree_1.ZERO_HASH.bigInt().toString();
s.issuerAuthClaimNonRevMtpNoAux = zero;
s.issuerAuthState = zero;
}
// InputsMarshal returns Circom private inputs for credentialAtomicQueryV3.circom
inputsMarshal() {
this.validate();
if (this.query.valueProof) {
this.query.validate();
this.query.valueProof.validate();
}
let valueProof = this.query.valueProof;
if (!valueProof) {
valueProof = new models_1.ValueProof();
valueProof.path = 0n;
valueProof.value = 0n;
valueProof.mtp = new js_merkletree_1.Proof();
}
let treeState = this.claim.nonRevProof.treeState;
if (this.proofType === verifiable_1.ProofType.BJJSignature && this.skipClaimRevocationCheck) {
treeState = this.claim.signatureProof?.issuerAuthNonRevProof.treeState;
}
if (!treeState) {
throw new Error(models_1.CircuitError.EmptyTreeState);
}
const s = {
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(),
issuerClaimNonRevClaimsTreeRoot: treeState.claimsRoot.bigInt().toString(),
issuerClaimNonRevRevTreeRoot: treeState.revocationRoot.bigInt().toString(),
issuerClaimNonRevRootsTreeRoot: treeState.rootOfRoots.bigInt().toString(),
issuerClaimNonRevState: treeState.state.bigInt().toString(),
issuerClaimNonRevMtp: (0, common_1.prepareSiblingsStr)(this.claim.nonRevProof.proof, this.getMTLevel()),
claimSchema: this.claim.claim.getSchemaHash().bigInt().toString(),
claimPathMtp: (0, common_1.prepareSiblingsStr)(valueProof.mtp, this.getMTLevelsClaim()),
claimPathValue: valueProof.value.toString(),
operator: this.query.operator,
timestamp: this.currentTimeStamp,
// value in this path in merklized json-ld document
slotIndex: this.query.slotIndex,
isRevocationChecked: 1
};
if (this.skipClaimRevocationCheck) {
s.isRevocationChecked = 0;
}
if (this.proofType === verifiable_1.ProofType.BJJSignature) {
const sigProof = this.claim.signatureProof;
s.proofType = '1';
s.issuerClaimSignatureR8x = sigProof.signature.R8[0].toString();
s.issuerClaimSignatureR8y = sigProof.signature.R8[1].toString();
s.issuerClaimSignatureS = sigProof.signature.S.toString();
s.issuerAuthClaim = sigProof.issuerAuthClaim?.marshalJson();
s.issuerAuthClaimMtp = (0, common_1.prepareSiblingsStr)(sigProof.issuerAuthIncProof.proof, this.getMTLevel());
s.issuerAuthClaimsTreeRoot = sigProof.issuerAuthIncProof.treeState?.claimsRoot
.bigInt()
.toString();
s.issuerAuthRevTreeRoot = sigProof.issuerAuthIncProof.treeState?.revocationRoot
.bigInt()
.toString();
s.issuerAuthRootsTreeRoot = sigProof.issuerAuthIncProof.treeState?.rootOfRoots
.bigInt()
.toString();
s.issuerAuthClaimNonRevMtp = (0, common_1.prepareSiblingsStr)(sigProof.issuerAuthNonRevProof.proof, this.getMTLevel());
const nodeAuxIssuerAuthNonRev = (0, common_1.getNodeAuxValue)(sigProof.issuerAuthNonRevProof.proof);
s.issuerAuthClaimNonRevMtpAuxHi = nodeAuxIssuerAuthNonRev.key.bigInt().toString();
s.issuerAuthClaimNonRevMtpAuxHv = nodeAuxIssuerAuthNonRev.value.bigInt().toString();
s.issuerAuthClaimNonRevMtpNoAux = nodeAuxIssuerAuthNonRev.noAux;
s.issuerAuthState = sigProof.issuerAuthIncProof.treeState?.state.bigInt().toString();
this.fillMTPProofsWithZero(s);
}
else if (this.proofType === verifiable_1.ProofType.Iden3SparseMerkleTreeProof) {
s.proofType = '2';
const incProofTreeState = this.claim.incProof?.treeState;
if (!incProofTreeState) {
throw new Error(models_1.CircuitError.EmptyTreeState);
}
s.issuerClaimMtp = (0, common_1.prepareSiblingsStr)(this.claim.incProof?.proof, this.getMTLevel());
s.issuerClaimClaimsTreeRoot = incProofTreeState.claimsRoot.bigInt().toString();
s.issuerClaimRevTreeRoot = incProofTreeState.revocationRoot.bigInt().toString();
s.issuerClaimRootsTreeRoot = incProofTreeState.rootOfRoots.bigInt().toString();
s.issuerClaimIdenState = incProofTreeState.state.bigInt().toString();
this.fillSigProofWithZero(s);
}
const nodeAuxNonRev = (0, common_1.getNodeAuxValue)(this.claim.nonRevProof.proof);
s.issuerClaimNonRevMtpAuxHi = nodeAuxNonRev.key.bigInt().toString();
s.issuerClaimNonRevMtpAuxHv = nodeAuxNonRev.value.bigInt().toString();
s.issuerClaimNonRevMtpNoAux = nodeAuxNonRev.noAux;
const nodAuxJSONLD = (0, common_1.getNodeAuxValue)(valueProof.mtp);
s.claimPathMtpNoAux = nodAuxJSONLD.noAux;
s.claimPathMtpAuxHi = nodAuxJSONLD.key.bigInt().toString();
s.claimPathMtpAuxHv = nodAuxJSONLD.value.bigInt().toString();
s.claimPathKey = valueProof.path.toString();
s.valueArraySize = this.query.values.length;
const values = (0, common_1.prepareCircuitArrayValues)(this.query.values, this.getValueArrSize());
s.value = (0, common_1.bigIntArrayToStringArray)(values);
s.linkNonce = this.linkNonce.toString();
s.verifierID = this.verifierID?.bigInt().toString() ?? '0';
s.nullifierSessionID = this.nullifierSessionID.toString();
return utils_1.byteEncoder.encode(JSON.stringify(s));
}
}
exports.AtomicQueryV3Inputs = AtomicQueryV3Inputs;
/**
* @beta
* AtomicQueryV3PubSignals public inputs
*/
class AtomicQueryV3PubSignals extends common_1.BaseConfig {
constructor() {
super(...arguments);
this.value = [];
}
// PubSignalsUnmarshal unmarshal credentialAtomicQueryV3.circom public signals
pubSignalsUnmarshal(data) {
// expected order:
// merklized
// userID
// issuerState
// linkID
// nullifier
// operatorOutput
// proofType
// requestID
// issuerID
// isRevocationChecked
// issuerClaimNonRevState
// timestamp
// claimSchema
// claimPathKey
// slotIndex
// operator
// value
// valueArraySize
// verifierID
// nullifierSessionID
// 19 is a number of fields in AtomicQueryV3PubSignals before values, values is last element in the proof and
// it is length could be different base on the circuit configuration. The length could be modified by set value
// in ValueArraySize
const fieldLength = 19;
const sVals = JSON.parse(utils_1.byteDecoder.decode(data));
if (sVals.length !== fieldLength + this.getValueArrSize()) {
throw new Error(`invalid number of Output values expected ${fieldLength + this.getValueArrSize()} got ${sVals.length}`);
}
let fieldIdx = 0;
// -- merklized
this.merklized = parseInt(sVals[fieldIdx]);
fieldIdx++;
// - userID
this.userID = js_iden3_core_1.Id.fromBigInt(BigInt(sVals[fieldIdx]));
fieldIdx++;
// - issuerState
this.issuerState = js_merkletree_1.Hash.fromString(sVals[fieldIdx]);
fieldIdx++;
// - linkID
this.linkID = BigInt(sVals[fieldIdx]);
fieldIdx++;
// - nullifier
this.nullifier = BigInt(sVals[fieldIdx]);
fieldIdx++;
// - operatorOutput
this.operatorOutput = BigInt(sVals[fieldIdx]);
fieldIdx++;
// - proofType
this.proofType = parseInt(sVals[fieldIdx]);
fieldIdx++;
// - requestID
this.requestID = BigInt(sVals[fieldIdx]);
fieldIdx++;
// - issuerID
this.issuerID = js_iden3_core_1.Id.fromBigInt(BigInt(sVals[fieldIdx]));
fieldIdx++;
// - isRevocationChecked
this.isRevocationChecked = parseInt(sVals[fieldIdx]);
fieldIdx++;
// - issuerClaimNonRevState
this.issuerClaimNonRevState = js_merkletree_1.Hash.fromString(sVals[fieldIdx]);
fieldIdx++;
// - timestamp
this.timestamp = parseInt(sVals[fieldIdx]);
fieldIdx++;
// - claimSchema
this.claimSchema = js_iden3_core_1.SchemaHash.newSchemaHashFromInt(BigInt(sVals[fieldIdx]));
fieldIdx++;
// - ClaimPathKey
this.claimPathKey = BigInt(sVals[fieldIdx]);
fieldIdx++;
// - slotIndex
this.slotIndex = parseInt(sVals[fieldIdx]);
fieldIdx++;
// - operator
this.operator = parseInt(sVals[fieldIdx]);
fieldIdx++;
// - values
for (let index = 0; index < this.getValueArrSize(); index++) {
this.value.push(BigInt(sVals[fieldIdx]));
fieldIdx++;
}
// - valueArraySize
this.valueArraySize = parseInt(sVals[fieldIdx]);
fieldIdx++;
// - verifierID
if (sVals[fieldIdx] !== '0') {
this.verifierID = js_iden3_core_1.Id.fromBigInt(BigInt(sVals[fieldIdx]));
}
fieldIdx++;
// - nullifierSessionID
this.nullifierSessionID = BigInt(sVals[fieldIdx]);
return this;
}
}
exports.AtomicQueryV3PubSignals = AtomicQueryV3PubSignals;