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

Message bus POC #179

Merged
merged 6 commits into from
Feb 24, 2024
Merged
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
3 changes: 0 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ jobs:
- name: Run Build
run: npm run build

- name: Circular dependency check
run: npm run deps:check

- name: Download regular circuits for CI 'latest.zip' from S3
run: mkdir ./tests/proofs/testdata && wget https://iden3-circuits-bucket.s3.eu-west-1.amazonaws.com/latest.zip -P ./tests/proofs/testdata

Expand Down
2,382 changes: 462 additions & 1,920 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@0xpolygonid/js-sdk",
"version": "1.7.5",
"version": "1.8.0",
"description": "SDK to work with Polygon ID",
"main": "dist/node/cjs/index.js",
"module": "dist/node/esm/index.js",
Expand Down Expand Up @@ -33,8 +33,7 @@
"tsc:declaration:watch": "tsc --watch --module commonjs --emitDeclarationOnly",
"test": "env TS_NODE_COMPILER_OPTIONS='{\"strict\": false, \"module\": \"CommonJS\"}' mocha",
"lint": "eslint --fix --ext .ts src/** tests/**",
"format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\"",
"deps:check": "madge --circular --extensions ts ./"
"format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\""
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -67,6 +66,7 @@
"@types/jsonld": "^1.5.11",
"@types/mocha": "^10.0.3",
"@types/node": "^20.8.9",
"@types/pubsub-js": "^1.8.6",
"@types/uuid": "^9.0.6",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"chai": "^4.3.10",
Expand All @@ -75,7 +75,6 @@
"chokidar": "^3.5.3",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"madge": "^6.1.0",
"mocha": "10.2.0",
"prettier": "^2.7.1",
"rimraf": "^5.0.5",
Expand All @@ -88,11 +87,11 @@
"@iden3/js-crypto": "1.0.3",
"@iden3/js-iden3-core": "1.2.1",
"@iden3/js-jsonld-merklization": "1.1.2",
"@iden3/js-jwz": "1.2.1",
"@iden3/js-jwz": "1.3.0",
"@iden3/js-merkletree": "1.1.2",
"ffjavascript": "0.2.62",
"snarkjs": "0.7.2",
"rfc4648": "1.5.3"
"ffjavascript": "0.2.63",
"rfc4648": "1.5.3",
"snarkjs": "0.7.3"
},
"dependencies": {
"ajv": "8.12.0",
Expand All @@ -102,8 +101,9 @@
"elliptic": "6.5.4",
"ethers": "6.8.0",
"idb-keyval": "6.2.0",
"js-sha3": "0.9.2",
"js-sha3": "0.9.3",
"jsonld": "8.3.1",
"pubsub-js": "1.9.4",
"uuid": "9.0.1"
},
"browserslist": {
Expand Down
71 changes: 3 additions & 68 deletions src/credentials/credential-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import {
W3CCredential,
ProofQuery,
VerifiableConstants,
SubjectPosition,
MerklizedRootPosition,
CredentialStatus,
RevocationStatus,
CredentialStatusType,
State,
RefreshService
State
} from './../verifiable';

import { JSONSchema } from '../schema-processor';
Expand All @@ -20,69 +17,7 @@ import { IssuerResolver } from './status/sparse-merkle-tree';
import { AgentResolver } from './status/agent-revocation';
import { CredentialStatusResolveOptions } from './status/resolver';
import { getUserDIDFromCredential } from './utils';

// ErrAllClaimsRevoked all claims are revoked.
const ErrAllClaimsRevoked = 'all claims are revoked';

/**
* Request to core library to create Core Claim from W3C Verifiable Credential
*
* @public
* @interface CredentialRequest
*/
export interface CredentialRequest {
/**
* JSON credential schema
*/
credentialSchema: string;
/**
* Credential type
*/
type: string;
/**
* Credential subject, usually contains claims and identifier
*/
credentialSubject: { [key: string]: string | object | number | boolean };
/**
* expiration time
*/
expiration?: number;
/**
* refreshService
*/
refreshService?: RefreshService;
/**
* claim version
*/
version?: number;

/**
* subject position (index / value / none)
*/
subjectPosition?: SubjectPosition;
/**
* merklizedRootPosition (index / value / none)
*/
merklizedRootPosition?: MerklizedRootPosition;

/**
* Revocation options
*
* @type {{
* id: string;
* nonce?: number;
* type: CredentialStatusType;
* issuerState?: string;
* }}
* @memberof CredentialRequest
*/
revocationOpts: {
id: string;
nonce?: number;
type: CredentialStatusType;
issuerState?: string;
};
}
import { CredentialRequest } from './models';

/**
* Interface to work with credential wallets
Expand Down Expand Up @@ -461,6 +396,6 @@ export class CredentialWallet implements ICredentialWallet {
}
return { cred, revStatus };
}
throw new Error(ErrAllClaimsRevoked);
throw new Error('all claims are revoked');
}
}
1 change: 1 addition & 0 deletions src/credentials/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './status/credential-status-publisher';
export * from './credential-wallet';
export * from './rhs';
export * from './utils';
export * from './models';
75 changes: 75 additions & 0 deletions src/credentials/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {
CredentialStatusType,
MerklizedRootPosition,
RefreshService,
SubjectPosition
} from '../verifiable';

/**
* Represents the publish mode for identity wallet.
* It can be one of the following values: 'sync', 'async', or 'callback'.
* 'sync' - publish the status synchronously
* 'async' - publish the status asynchronously via message bus
* 'callback' - publish the status with a txCallback
*/
export type PublishMode = 'sync' | 'async' | 'callback';

/**
* Request to core library to create Core Claim from W3C Verifiable Credential
*
* @public
* @interface CredentialRequest
*/
export interface CredentialRequest {
/**
* JSON credential schema
*/
credentialSchema: string;
/**
* Credential type
*/
type: string;
/**
* Credential subject, usually contains claims and identifier
*/
credentialSubject: { [key: string]: string | object | number | boolean };
/**
* expiration time
*/
expiration?: number;
/**
* refreshService
*/
refreshService?: RefreshService;
/**
* claim version
*/
version?: number;

/**
* subject position (index / value / none)
*/
subjectPosition?: SubjectPosition;
/**
* merklizedRootPosition (index / value / none)
*/
merklizedRootPosition?: MerklizedRootPosition;

/**
* Revocation options
*
* @type {{
* id: string;
* nonce?: number;
* type: CredentialStatusType;
* issuerState?: string;
* }}
* @memberof CredentialRequest
*/
revocationOpts: {
id: string;
nonce?: number;
type: CredentialStatusType;
issuerState?: string;
};
}
33 changes: 28 additions & 5 deletions src/credentials/status/credential-status-publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { JSONObject } from '../../iden3comm';
import { OnChainRevocationStorage } from '../../storage';
import { CredentialStatusType } from '../../verifiable';
import { ProofNode } from './reverse-sparse-merkle-tree';
import { MessageBus, SDK_EVENTS } from '../../utils';
import { PublishMode } from '../models';

/**
* Represents a credential status publisher.
Expand Down Expand Up @@ -56,7 +58,10 @@ export class Iden3OnchainSmtCredentialStatusPublisher implements ICredentialStat
public async publish(params: {
nodes: ProofNode[];
credentialStatusType: CredentialStatusType;
onChain?: { txCallback?: (tx: TransactionReceipt) => Promise<void> };
onChain?: {
txCallback?: (tx: TransactionReceipt) => Promise<void>;
publishMode?: PublishMode;
};
}): Promise<void> {
if (
![CredentialStatusType.Iden3OnchainSparseMerkleTreeProof2023].includes(
Expand All @@ -71,13 +76,31 @@ export class Iden3OnchainSmtCredentialStatusPublisher implements ICredentialStat

const txPromise = this._storage.saveNodes(nodesBigInts);

let publishMode = params.onChain?.publishMode ?? 'sync';
if (params.onChain?.txCallback) {
const cb = params.onChain?.txCallback;
txPromise.then((receipt) => cb(receipt));
return;
publishMode = 'callback';
}

await txPromise;
switch (publishMode) {
case 'sync':
await txPromise;
break;
case 'callback': {
if (!params.onChain?.txCallback) {
throw new Error('txCallback is required for publishMode "callback"');
}
const cb = params.onChain?.txCallback;
txPromise.then((receipt) => cb(receipt));
break;
}
case 'async': {
const mb = MessageBus.getInstance();
txPromise.then((receipt) => mb.publish(SDK_EVENTS.TX_RECEIPT_ACCEPTED, receipt));
break;
}
default:
throw new Error(`Invalid publishMode: ${publishMode}`);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/identity/identity-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
getNodesRepresentation,
ICredentialWallet,
ProofNode,
PublishMode,
pushHashesToRHS,
TreesModel
} from '../credentials';
Expand Down Expand Up @@ -67,6 +68,7 @@ export type IdentityCreationOptions = {
nonce?: number;
onChain?: {
txCallback?: (tx: TransactionReceipt) => Promise<void>;
publishMode?: PublishMode;
};
};
seed?: Uint8Array;
Expand All @@ -81,6 +83,7 @@ export type RevocationInfoOptions = {
rhsUrl?: string;
onChain?: {
txCallback?: (tx: TransactionReceipt) => Promise<void>;
publishMode?: PublishMode;
};
};

Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './encoding';
export * from './object';
export * from './did-helper';
export * from './message-bus';
export * from './compare-func';
Loading
Loading