-
Notifications
You must be signed in to change notification settings - Fork 7
/
didRegistry.service.ts
281 lines (257 loc) · 9.31 KB
/
didRegistry.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { Wallet, providers } from "ethers";
import { AxiosError } from "axios";
import { KeyType } from "@ew-did-registry/keys";
import { JWT } from "@ew-did-registry/jwt";
import { ProxyOperator } from "@ew-did-registry/proxyidentity";
import { addressOf, EwSigner, Operator } from "@ew-did-registry/did-ethr-resolver";
import {
DIDAttribute,
Encoding,
IServiceEndpoint,
IUpdateData,
KeyTags,
ProviderTypes,
} from "@ew-did-registry/did-resolver-interface";
import { DIDDocumentFull, IDIDDocumentFull } from "@ew-did-registry/did-document";
import { DidStore } from "@ew-did-registry/did-ipfs-store";
import { Methods } from "@ew-did-registry/did";
import { ClaimsIssuer, ClaimsUser, IPublicClaim, ProofVerifier } from "@ew-did-registry/claims";
import { SignerService } from "../signer/signer.service";
import { ERROR_MESSAGES } from "../../errors";
import { CacheClient } from "../cacheClient/cacheClient.service";
import { ClaimData } from "../didRegistry/did.types";
import { chainConfigs } from "../../config/chain.config";
import { AssetsService } from "../assets/assets.service";
const { JsonRpcProvider } = providers;
export class DidRegistry {
private _identityOwner: EwSigner;
private _operator: Operator;
private _did: string;
private _document: IDIDDocumentFull;
private _ipfsStore: DidStore;
private _jwt: JWT;
private _userClaims: ClaimsUser;
private _issuerClaims: ClaimsIssuer;
constructor(
private _signerService: SignerService,
private _cacheClient: CacheClient,
private _assetsService: AssetsService,
private _ipfsUrl = "https://ipfs.infura.io:5001/api/v0/",
) {
this._signerService.onInit(this.init.bind(this));
}
static async connect(
signerService: SignerService,
cacheClient: CacheClient,
assetsService: AssetsService,
ipfsUrl?: string,
) {
const registry = new DidRegistry(signerService, cacheClient, assetsService, ipfsUrl);
await registry.init();
return registry;
}
get jwt() {
return this._jwt;
}
// temporarily, to allow claim service to save claim
get ipfsStore() {
return this._ipfsStore;
}
async init() {
this._ipfsStore = new DidStore(this._ipfsUrl);
await this._setOperator();
this.setJWT();
await this._setDocument();
this._setClaims();
}
async getDidDocument({
did = this._did,
includeClaims = true,
}: { did?: string; includeClaims?: boolean } | undefined = {}) {
if (this._cacheClient) {
try {
const didDoc = await this._cacheClient.getDidDocument(did, includeClaims);
return {
...didDoc,
service: didDoc.service as (IServiceEndpoint & ClaimData)[],
};
} catch (err) {
if ((err as AxiosError).response?.status === 401) {
throw err;
}
console.log(err);
}
}
const document = await this._operator.read(did);
return {
...document,
service: includeClaims
? await this.downloadClaims({
services: document.service && document.service.length > 0 ? document.service : [],
})
: [],
};
}
/**
* createPublicClaim
*
* @description create a public claim based on data provided
* @returns JWT token of created claim
*
*/
async createPublicClaim({ data, subject }: { data: ClaimData; subject?: string }) {
if (subject) {
return this._userClaims.createPublicClaim(data, { subject, issuer: "" });
}
return this._userClaims.createPublicClaim(data);
}
/**
* issuePublicClaim
*
* @description issue a public claim
* @returns return issued token
*
*/
async issuePublicClaim({ token, publicClaim }: { token?: string; publicClaim?: IPublicClaim }) {
if (publicClaim) {
return this._issuerClaims.issuePublicClaim(publicClaim);
}
if (token) {
return this._issuerClaims.issuePublicClaim(token);
}
throw new Error("unable to issue Public Claim");
}
/**
* verifyPublicClaim
*
* @description verifies issued token of claim
* @returns public claim data
*
*/
async verifyPublicClaim(token: string, iss: string) {
const issuerDoc = await this._cacheClient.getDidDocument(iss, true);
const verifier = new ProofVerifier(issuerDoc);
return verifier.verifyAssertionProof(token);
}
/**
* @param options Options to connect with blockchain
*
* @param options.didAttribute Type of document to be updated
*
* @param options.data New attribute value
* @param options.did Asset did to be updated
* @param options.validity Time (s) for the attribute to expire
*
* @description updates did document based on data provided
* @returns true if document is updated successfuly
*
*/
async updateDocument({
didAttribute,
data,
validity,
did = this._signerService.did,
}: {
didAttribute: DIDAttribute;
data: IUpdateData;
did?: string;
validity?: number;
}): Promise<boolean> {
if (did === this._signerService.did) {
const updated = await this._document.update(didAttribute, data, validity);
return Boolean(updated);
} else {
const assetDID = (await this._assetsService.getOwnedAssets()).find((a) => a.document.id === did)?.id;
if (!assetDID) {
throw new Error(ERROR_MESSAGES.CAN_NOT_UPDATE_NOT_CONTROLLED_DOCUMENT);
}
const updateData: IUpdateData = {
algo: KeyType.Secp256k1,
encoding: Encoding.HEX,
...data,
};
const { didRegistryAddress: didContractAddress } = chainConfigs()[this._signerService.chainId];
const operator = new ProxyOperator(
this._identityOwner,
{ address: didContractAddress },
addressOf(assetDID),
);
const update = await operator.update(did, didAttribute, updateData);
return Boolean(update);
}
}
/**
* @description create did document if not exists
* @returns true if document is created successfully
*/
async createDocument(): Promise<boolean> {
if (this._cacheClient) {
const cachedDoc = await this._cacheClient.getDidDocument(this._did);
const pubKey = cachedDoc.publicKey.find((pk) => pk.id.endsWith(KeyTags.OWNER));
if (!pubKey) {
return this._document.create();
}
return true;
}
return this._document.create();
}
/**
* revokeDidDocument
*
* @description revokes did document
* @returns information (true/false) if the DID document was revoked
*
*/
async revokeDidDocument(): Promise<boolean> {
await this._document.deactivate();
return true;
}
async decodeJWTToken({ token }: { token: string }) {
return this._jwt.decode(token);
}
private async _setOperator() {
const signer = this._signerService.signer;
const provider = signer.provider;
const publicKey = await this._signerService.publicKey();
if (signer instanceof Wallet && provider instanceof JsonRpcProvider) {
this._identityOwner = EwSigner.fromPrivateKey(signer.privateKey, {
type: ProviderTypes.HTTP,
uriOrInfo: provider.connection.url,
});
} else if (provider instanceof JsonRpcProvider) {
this._identityOwner = EwSigner.fromEthersSigner(signer, publicKey);
} else {
/** @todo from EIP1193Provider */
throw new Error(ERROR_MESSAGES.UNKNOWN_PROVIDER);
}
this._did = `did:${Methods.Erc1056}:${this._signerService.chainName()}:${await signer.getAddress()}`;
const address = chainConfigs()[this._signerService.chainId].didRegistryAddress;
this._operator = new Operator(this._identityOwner, { address });
}
private setJWT() {
this._jwt = new JWT(this._identityOwner);
}
private async _setDocument() {
this._document = new DIDDocumentFull(this._did, this._operator);
}
private _setClaims() {
this._userClaims = new ClaimsUser(this._identityOwner, this._document, this._ipfsStore);
this._issuerClaims = new ClaimsIssuer(this._identityOwner, this._document, this._ipfsStore);
}
private async downloadClaims({ services }: { services: IServiceEndpoint[] }) {
return Promise.all(
services.map(async ({ serviceEndpoint, ...rest }) => {
const data = await this._ipfsStore.get(serviceEndpoint);
const { claimData, ...claimRest } = this._jwt?.decode(data) as {
claimData: ClaimData;
};
return {
serviceEndpoint,
...rest,
...claimData,
...claimRest,
} as IServiceEndpoint & ClaimData;
}),
);
}
}