Skip to content

Commit

Permalink
Feature/pid-1040 (#123)
Browse files Browse the repository at this point in the history
* Non-merklize credential support


---------

Co-authored-by: vmidyllic <[email protected]>
  • Loading branch information
Kolezhniuk and vmidyllic authored Sep 5, 2023
1 parent 24cafa0 commit 16f7ba7
Show file tree
Hide file tree
Showing 20 changed files with 1,034 additions and 294 deletions.
142 changes: 47 additions & 95 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@
},
"dependencies": {
"@iden3/js-crypto": "1.0.1",
"@iden3/js-iden3-core": "1.0.1",
"@iden3/js-jsonld-merklization": "1.0.1",
"@iden3/js-jwz": "1.0.1",
"@iden3/js-merkletree": "1.0.0",
"@iden3/js-iden3-core": "1.0.2",
"@iden3/js-jsonld-merklization": "1.0.2",
"@iden3/js-jwz": "1.0.2",
"@iden3/js-merkletree": "1.0.1",
"@lumeweb/js-sha3-browser": "^0.8.1",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"base58-js": "^1.0.4",
"buffer-browserify": "^0.2.5",
"cross-sha256": "^1.2.0",
Expand All @@ -85,7 +86,7 @@
"ethers": "^5.7.2",
"ffjavascript": "^0.2.57",
"idb-keyval": "^6.2.0",
"jsonld": "^8.1.1",
"jsonld": "^8.2.0",
"multiformats": "^9.6.5",
"rfc4648": "^1.5.2",
"snarkjs": "^0.5.0",
Expand Down
47 changes: 11 additions & 36 deletions src/identity/identity-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { hashElems, ZERO_HASH } from '@iden3/js-merkletree';

import { generateProfileDID, subjectPositionIndex } from './common';
import * as uuid from 'uuid';
import { JSONSchema, Parser, CoreClaimOptions } from '../schema-processor';
import { JSONSchema, Parser, CoreClaimOptions, JsonSchemaValidator } from '../schema-processor';
import { IDataStorage } from '../storage/interfaces/data-storage';
import { MerkleTreeType } from '../storage/entities/mt';
import { getRandomBytes, keyPath } from '../kms/provider-helpers';
Expand All @@ -38,7 +38,7 @@ import { CredentialRequest, ICredentialWallet } from '../credentials';
import { pushHashesToRHS, TreesModel } from '../credentials/rhs';
import { TreeState } from '../circuits';
import { byteEncoder } from '../utils';
import { Options, Path, getDocumentLoader } from '@iden3/js-jsonld-merklization';
import { Options, getDocumentLoader } from '@iden3/js-jsonld-merklization';
import { sha256js } from 'cross-sha256';
import { Profile } from '../storage';

Expand Down Expand Up @@ -627,42 +627,27 @@ export class IdentityWallet implements IIdentityWallet {

try {
credential = this._credentialWallet.createCredential(issuerDID, req, jsonSchema);

const encodedCred = byteEncoder.encode(JSON.stringify(credential));
const encodedSchema = byteEncoder.encode(JSON.stringify(schema));

await new JsonSchemaValidator().validate(encodedCred, encodedSchema);
} catch (e) {
throw new Error('Error create Iden3Credential');
throw new Error(`Error create Iden3Credential ${(e as Error).message}`);
}

const issuerAuthBJJCredential = await this._credentialWallet.getAuthBJJCredential(issuerDID);

const coreClaimOpts: CoreClaimOptions = {
revNonce: req.revocationOpts.nonce,
subjectPosition: req.subjectPosition,
merklizedRootPosition: this.defineMTRootPosition(jsonSchema, req.merklizedRootPosition),
merklizedRootPosition: req.merklizedRootPosition ?? MerklizedRootPosition.None,
updatable: false,
version: 0,
merklizeOpts: opts
};

let jsonLDCtx: object;
try {
jsonLDCtx = (await loader(jsonSchema.$metadata.uris.jsonLdContext)).document;
} catch (e) {
throw new Error(`can't load json-ld schema ${jsonSchema.$metadata.uris.jsonLdContext}`);
}

const schemaBytes = byteEncoder.encode(JSON.stringify(jsonSchema));

const credentialType = await Path.getTypeIDFromContext(
JSON.stringify(jsonLDCtx),
req.type,
opts
);

const coreClaim = await new Parser().parseClaim(
credential,
credentialType,
schemaBytes,
coreClaimOpts
);
const coreClaim = await Parser.parseClaim(credential, coreClaimOpts);

const { hi, hv } = coreClaim.hiHv();

Expand Down Expand Up @@ -732,7 +717,7 @@ export class IdentityWallet implements IIdentityWallet {
const credential = credentials[index];

// credential must have a bjj signature proof
const coreClaim = await credential.getCoreClaimFromProof(ProofType.BJJSignature);
const coreClaim = credential.getCoreClaimFromProof(ProofType.BJJSignature);

if (!coreClaim) {
throw new Error('credential must have coreClaim representation in the signature proof');
Expand Down Expand Up @@ -847,16 +832,6 @@ export class IdentityWallet implements IIdentityWallet {
return { type: KmsKeyType.BabyJubJub, id: kp };
}

private defineMTRootPosition(schema: JSONSchema, position?: string): string {
if (schema.$metadata?.serialization) {
return '';
}
if (position) {
return position;
}
return MerklizedRootPosition.Index;
}

public async getCoreClaimFromCredential(credential: W3CCredential): Promise<Claim> {
const coreClaimFromSigProof = credential.getCoreClaimFromProof(ProofType.BJJSignature);

Expand Down
Loading

0 comments on commit 16f7ba7

Please sign in to comment.