Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.0.1 adapter for non-merklized onchain issuer #284

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
486 changes: 167 additions & 319 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"mocha": "10.2.0",
"nock": "^14.0.0-beta.15",
"nock": "^14.0.0-beta.16",
"prettier": "^2.7.1",
"rimraf": "^5.0.5",
"rollup": "^4.14.3",
Expand All @@ -98,6 +98,7 @@
"snarkjs": "0.7.4"
},
"dependencies": {
"@iden3/onchain-non-merklized-issuer-base-abi": "^0.0.3",
Kolezhniuk marked this conversation as resolved.
Show resolved Hide resolved
"@noble/curves": "^1.4.0",
"ajv": "8.12.0",
"ajv-formats": "2.1.1",
Expand Down
100 changes: 11 additions & 89 deletions src/credentials/credential-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DID, getChainId } from '@iden3/js-iden3-core';

Check warning on line 1 in src/credentials/credential-wallet.ts

View workflow job for this annotation

GitHub Actions / build

'getChainId' is defined but never used
import { IDataStorage } from '../storage/interfaces';
import {
W3CCredential,
Expand Down Expand Up @@ -266,101 +266,23 @@
if (!schema.$metadata.uris['jsonLdContext']) {
throw new Error('jsonLdContext is missing is the schema');
}
request.context = request.context ?? [];
// do copy of request to avoid mutation
const r = { ...request };
r.context = r.context ?? [];
if (
request.displayMethod?.type === DisplayMethodType.Iden3BasicDisplayMethodV1 &&
!request.context.includes(VerifiableConstants.JSONLD_SCHEMA.IDEN3_DISPLAY_METHOD)
r.displayMethod?.type === DisplayMethodType.Iden3BasicDisplayMethodV1 &&
!r.context.includes(VerifiableConstants.JSONLD_SCHEMA.IDEN3_DISPLAY_METHOD)
) {
request.context.push(VerifiableConstants.JSONLD_SCHEMA.IDEN3_DISPLAY_METHOD);
r.context.push(VerifiableConstants.JSONLD_SCHEMA.IDEN3_DISPLAY_METHOD);
}
const context = [
VerifiableConstants.JSONLD_SCHEMA.W3C_CREDENTIAL_2018,
...request.context,
VerifiableConstants.JSONLD_SCHEMA.IDEN3_CREDENTIAL,
schema.$metadata.uris['jsonLdContext']
];
r.context.push(schema.$metadata.uris['jsonLdContext']);
r.expiration = !r.expiration || r.expiration == 0 ? undefined : r.expiration * 1000;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r.expiration = !r.expiration || r.expiration == 0 ? undefined : r.expiration * 1000;
r.expiration = r.expiration ? r.expiration * 1000 : undefined;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it's done

r.id = r.id ? r.id : `urn:${uuid.v4()}`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r.id = r.id ? r.id : `urn:${uuid.v4()}`;
r.id = r.id ?? `urn:${uuid.v4()}`;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this on a browser console and there is a result:

const foo = ""; foo ?? console.log(1);
returns: '' - empty string
const foo = ""; foo ? console.log(2) : console.log(1);
returns 1;

I expect with user pass empty string for the id field, we need generate uuid.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case replace ?? to || for example a = ''; b = '1'; a = a || b; // '1'

r.issuanceDate = r.issuanceDate ? r.issuanceDate * 1000 : Date.now();

const credentialType = [
VerifiableConstants.CREDENTIAL_TYPE.W3C_VERIFIABLE_CREDENTIAL,
request.type
];

const expirationDate =
!request.expiration || request.expiration == 0 ? null : request.expiration;

const credentialSubject = request.credentialSubject;
credentialSubject['type'] = request.type;

const cr = new W3CCredential();
cr.id = `urn:${uuid.v4()}`;
cr['@context'] = context;
cr.type = credentialType;
cr.expirationDate = expirationDate ? new Date(expirationDate * 1000).toISOString() : undefined;
cr.refreshService = request.refreshService;
cr.displayMethod = request.displayMethod;
cr.issuanceDate = new Date().toISOString();
cr.credentialSubject = credentialSubject;
cr.issuer = issuer.string();
cr.credentialSchema = {
id: request.credentialSchema,
type: VerifiableConstants.JSON_SCHEMA_VALIDATOR
};

cr.credentialStatus = this.buildCredentialStatus(request, issuer);

return cr;
return new W3CCredential().fromCredentialRequest(issuer, r);
Kolezhniuk marked this conversation as resolved.
Show resolved Hide resolved
};

/**
* Builds credential status
* @param {CredentialRequest} request
* @returns `CredentialStatus`
*/
private buildCredentialStatus(request: CredentialRequest, issuer: DID): CredentialStatus {
const credentialStatus: CredentialStatus = {
id: request.revocationOpts.id,
type: request.revocationOpts.type,
revocationNonce: request.revocationOpts.nonce
};

switch (request.revocationOpts.type) {
case CredentialStatusType.SparseMerkleTreeProof:
return {
...credentialStatus,
id: `${credentialStatus.id.replace(/\/$/, '')}/${credentialStatus.revocationNonce}`
};
case CredentialStatusType.Iden3ReverseSparseMerkleTreeProof:
return {
...credentialStatus,
id: request.revocationOpts.issuerState
? `${credentialStatus.id.replace(/\/$/, '')}/node?state=${
request.revocationOpts.issuerState
}`
: `${credentialStatus.id.replace(/\/$/, '')}`
};
case CredentialStatusType.Iden3OnchainSparseMerkleTreeProof2023: {
const issuerId = DID.idFromDID(issuer);
const chainId = getChainId(DID.blockchainFromId(issuerId), DID.networkIdFromId(issuerId));
const searchParams = [
['revocationNonce', request.revocationOpts.nonce?.toString() || ''],
['contractAddress', `${chainId}:${request.revocationOpts.id}`],
['state', request.revocationOpts.issuerState || '']
]
.filter(([, value]) => Boolean(value))
.map(([key, value]) => `${key}=${value}`)
.join('&');

return {
...credentialStatus,
// `[did]:[methodid]:[chain]:[network]:[id]/credentialStatus?(revocationNonce=value)&[contractAddress=[chainID]:[contractAddress]]&(state=issuerState)`
id: `${issuer.string()}/credentialStatus?${searchParams}`
};
}
default:
return credentialStatus;
}
}

/**
* {@inheritDoc ICredentialWallet.findById}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/credentials/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export type PublishMode = 'sync' | 'async' | 'callback';
* @interface CredentialRequest
*/
export interface CredentialRequest {
/**
* Credential ID
*/
id?: string;
/**
* JSON credential schema
*/
Expand Down Expand Up @@ -64,6 +68,10 @@ export interface CredentialRequest {
* merklizedRootPosition (index / value / none)
*/
merklizedRootPosition?: MerklizedRootPosition;
/**
* issuance Date
*/
issuanceDate?: number;

/**
* Revocation options
Expand Down
1 change: 1 addition & 0 deletions src/storage/blockchain/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './state';
export * from './onchain-zkp-verifier';
export * from './onchain-revocation';
export * from './onchain-issuer';
export * from './erc20-permit-sig';
Loading