Skip to content

Commit 36ddc67

Browse files
authored
Message bus POC (#179)
* Message bus POC
1 parent 30b4dbc commit 36ddc67

13 files changed

+726
-2051
lines changed

.github/workflows/ci.yaml

-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ jobs:
4343
- name: Run Build
4444
run: npm run build
4545

46-
- name: Circular dependency check
47-
run: npm run deps:check
48-
4946
- name: Download regular circuits for CI 'latest.zip' from S3
5047
run: mkdir ./tests/proofs/testdata && wget https://iden3-circuits-bucket.s3.eu-west-1.amazonaws.com/latest.zip -P ./tests/proofs/testdata
5148

package-lock.json

+462-1,920
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@0xpolygonid/js-sdk",
3-
"version": "1.7.5",
3+
"version": "1.8.0",
44
"description": "SDK to work with Polygon ID",
55
"main": "dist/node/cjs/index.js",
66
"module": "dist/node/esm/index.js",
@@ -33,8 +33,7 @@
3333
"tsc:declaration:watch": "tsc --watch --module commonjs --emitDeclarationOnly",
3434
"test": "env TS_NODE_COMPILER_OPTIONS='{\"strict\": false, \"module\": \"CommonJS\"}' mocha",
3535
"lint": "eslint --fix --ext .ts src/** tests/**",
36-
"format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\"",
37-
"deps:check": "madge --circular --extensions ts ./"
36+
"format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\""
3837
},
3938
"repository": {
4039
"type": "git",
@@ -67,6 +66,7 @@
6766
"@types/jsonld": "^1.5.11",
6867
"@types/mocha": "^10.0.3",
6968
"@types/node": "^20.8.9",
69+
"@types/pubsub-js": "^1.8.6",
7070
"@types/uuid": "^9.0.6",
7171
"@typescript-eslint/eslint-plugin": "^5.41.0",
7272
"chai": "^4.3.10",
@@ -75,7 +75,6 @@
7575
"chokidar": "^3.5.3",
7676
"eslint-config-prettier": "^8.8.0",
7777
"eslint-plugin-prettier": "^4.2.1",
78-
"madge": "^6.1.0",
7978
"mocha": "10.2.0",
8079
"prettier": "^2.7.1",
8180
"rimraf": "^5.0.5",
@@ -88,11 +87,11 @@
8887
"@iden3/js-crypto": "1.0.3",
8988
"@iden3/js-iden3-core": "1.2.1",
9089
"@iden3/js-jsonld-merklization": "1.1.2",
91-
"@iden3/js-jwz": "1.2.1",
90+
"@iden3/js-jwz": "1.3.0",
9291
"@iden3/js-merkletree": "1.1.2",
93-
"ffjavascript": "0.2.62",
94-
"snarkjs": "0.7.2",
95-
"rfc4648": "1.5.3"
92+
"ffjavascript": "0.2.63",
93+
"rfc4648": "1.5.3",
94+
"snarkjs": "0.7.3"
9695
},
9796
"dependencies": {
9897
"ajv": "8.12.0",
@@ -102,8 +101,9 @@
102101
"elliptic": "6.5.4",
103102
"ethers": "6.8.0",
104103
"idb-keyval": "6.2.0",
105-
"js-sha3": "0.9.2",
104+
"js-sha3": "0.9.3",
106105
"jsonld": "8.3.1",
106+
"pubsub-js": "1.9.4",
107107
"uuid": "9.0.1"
108108
},
109109
"browserslist": {

src/credentials/credential-wallet.ts

+3-68
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import {
44
W3CCredential,
55
ProofQuery,
66
VerifiableConstants,
7-
SubjectPosition,
8-
MerklizedRootPosition,
97
CredentialStatus,
108
RevocationStatus,
119
CredentialStatusType,
12-
State,
13-
RefreshService
10+
State
1411
} from './../verifiable';
1512

1613
import { JSONSchema } from '../schema-processor';
@@ -20,69 +17,7 @@ import { IssuerResolver } from './status/sparse-merkle-tree';
2017
import { AgentResolver } from './status/agent-revocation';
2118
import { CredentialStatusResolveOptions } from './status/resolver';
2219
import { getUserDIDFromCredential } from './utils';
23-
24-
// ErrAllClaimsRevoked all claims are revoked.
25-
const ErrAllClaimsRevoked = 'all claims are revoked';
26-
27-
/**
28-
* Request to core library to create Core Claim from W3C Verifiable Credential
29-
*
30-
* @public
31-
* @interface CredentialRequest
32-
*/
33-
export interface CredentialRequest {
34-
/**
35-
* JSON credential schema
36-
*/
37-
credentialSchema: string;
38-
/**
39-
* Credential type
40-
*/
41-
type: string;
42-
/**
43-
* Credential subject, usually contains claims and identifier
44-
*/
45-
credentialSubject: { [key: string]: string | object | number | boolean };
46-
/**
47-
* expiration time
48-
*/
49-
expiration?: number;
50-
/**
51-
* refreshService
52-
*/
53-
refreshService?: RefreshService;
54-
/**
55-
* claim version
56-
*/
57-
version?: number;
58-
59-
/**
60-
* subject position (index / value / none)
61-
*/
62-
subjectPosition?: SubjectPosition;
63-
/**
64-
* merklizedRootPosition (index / value / none)
65-
*/
66-
merklizedRootPosition?: MerklizedRootPosition;
67-
68-
/**
69-
* Revocation options
70-
*
71-
* @type {{
72-
* id: string;
73-
* nonce?: number;
74-
* type: CredentialStatusType;
75-
* issuerState?: string;
76-
* }}
77-
* @memberof CredentialRequest
78-
*/
79-
revocationOpts: {
80-
id: string;
81-
nonce?: number;
82-
type: CredentialStatusType;
83-
issuerState?: string;
84-
};
85-
}
20+
import { CredentialRequest } from './models';
8621

8722
/**
8823
* Interface to work with credential wallets
@@ -461,6 +396,6 @@ export class CredentialWallet implements ICredentialWallet {
461396
}
462397
return { cred, revStatus };
463398
}
464-
throw new Error(ErrAllClaimsRevoked);
399+
throw new Error('all claims are revoked');
465400
}
466401
}

src/credentials/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from './status/credential-status-publisher';
77
export * from './credential-wallet';
88
export * from './rhs';
99
export * from './utils';
10+
export * from './models';

src/credentials/models.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import {
2+
CredentialStatusType,
3+
MerklizedRootPosition,
4+
RefreshService,
5+
SubjectPosition
6+
} from '../verifiable';
7+
8+
/**
9+
* Represents the publish mode for identity wallet.
10+
* It can be one of the following values: 'sync', 'async', or 'callback'.
11+
* 'sync' - publish the status synchronously
12+
* 'async' - publish the status asynchronously via message bus
13+
* 'callback' - publish the status with a txCallback
14+
*/
15+
export type PublishMode = 'sync' | 'async' | 'callback';
16+
17+
/**
18+
* Request to core library to create Core Claim from W3C Verifiable Credential
19+
*
20+
* @public
21+
* @interface CredentialRequest
22+
*/
23+
export interface CredentialRequest {
24+
/**
25+
* JSON credential schema
26+
*/
27+
credentialSchema: string;
28+
/**
29+
* Credential type
30+
*/
31+
type: string;
32+
/**
33+
* Credential subject, usually contains claims and identifier
34+
*/
35+
credentialSubject: { [key: string]: string | object | number | boolean };
36+
/**
37+
* expiration time
38+
*/
39+
expiration?: number;
40+
/**
41+
* refreshService
42+
*/
43+
refreshService?: RefreshService;
44+
/**
45+
* claim version
46+
*/
47+
version?: number;
48+
49+
/**
50+
* subject position (index / value / none)
51+
*/
52+
subjectPosition?: SubjectPosition;
53+
/**
54+
* merklizedRootPosition (index / value / none)
55+
*/
56+
merklizedRootPosition?: MerklizedRootPosition;
57+
58+
/**
59+
* Revocation options
60+
*
61+
* @type {{
62+
* id: string;
63+
* nonce?: number;
64+
* type: CredentialStatusType;
65+
* issuerState?: string;
66+
* }}
67+
* @memberof CredentialRequest
68+
*/
69+
revocationOpts: {
70+
id: string;
71+
nonce?: number;
72+
type: CredentialStatusType;
73+
issuerState?: string;
74+
};
75+
}

src/credentials/status/credential-status-publisher.ts

+28-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { JSONObject } from '../../iden3comm';
33
import { OnChainRevocationStorage } from '../../storage';
44
import { CredentialStatusType } from '../../verifiable';
55
import { ProofNode } from './reverse-sparse-merkle-tree';
6+
import { MessageBus, SDK_EVENTS } from '../../utils';
7+
import { PublishMode } from '../models';
68

79
/**
810
* Represents a credential status publisher.
@@ -56,7 +58,10 @@ export class Iden3OnchainSmtCredentialStatusPublisher implements ICredentialStat
5658
public async publish(params: {
5759
nodes: ProofNode[];
5860
credentialStatusType: CredentialStatusType;
59-
onChain?: { txCallback?: (tx: TransactionReceipt) => Promise<void> };
61+
onChain?: {
62+
txCallback?: (tx: TransactionReceipt) => Promise<void>;
63+
publishMode?: PublishMode;
64+
};
6065
}): Promise<void> {
6166
if (
6267
![CredentialStatusType.Iden3OnchainSparseMerkleTreeProof2023].includes(
@@ -71,13 +76,31 @@ export class Iden3OnchainSmtCredentialStatusPublisher implements ICredentialStat
7176

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

79+
let publishMode = params.onChain?.publishMode ?? 'sync';
7480
if (params.onChain?.txCallback) {
75-
const cb = params.onChain?.txCallback;
76-
txPromise.then((receipt) => cb(receipt));
77-
return;
81+
publishMode = 'callback';
7882
}
7983

80-
await txPromise;
84+
switch (publishMode) {
85+
case 'sync':
86+
await txPromise;
87+
break;
88+
case 'callback': {
89+
if (!params.onChain?.txCallback) {
90+
throw new Error('txCallback is required for publishMode "callback"');
91+
}
92+
const cb = params.onChain?.txCallback;
93+
txPromise.then((receipt) => cb(receipt));
94+
break;
95+
}
96+
case 'async': {
97+
const mb = MessageBus.getInstance();
98+
txPromise.then((receipt) => mb.publish(SDK_EVENTS.TX_RECEIPT_ACCEPTED, receipt));
99+
break;
100+
}
101+
default:
102+
throw new Error(`Invalid publishMode: ${publishMode}`);
103+
}
81104
}
82105
}
83106

src/identity/identity-wallet.ts

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
getNodesRepresentation,
3939
ICredentialWallet,
4040
ProofNode,
41+
PublishMode,
4142
pushHashesToRHS,
4243
TreesModel
4344
} from '../credentials';
@@ -67,6 +68,7 @@ export type IdentityCreationOptions = {
6768
nonce?: number;
6869
onChain?: {
6970
txCallback?: (tx: TransactionReceipt) => Promise<void>;
71+
publishMode?: PublishMode;
7072
};
7173
};
7274
seed?: Uint8Array;
@@ -81,6 +83,7 @@ export type RevocationInfoOptions = {
8183
rhsUrl?: string;
8284
onChain?: {
8385
txCallback?: (tx: TransactionReceipt) => Promise<void>;
86+
publishMode?: PublishMode;
8487
};
8588
};
8689

src/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './encoding';
22
export * from './object';
33
export * from './did-helper';
4+
export * from './message-bus';
45
export * from './compare-func';

0 commit comments

Comments
 (0)