Skip to content
Open
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
24 changes: 12 additions & 12 deletions src/abstractmetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export abstract class AbstractMetadata extends DIDEntity<AbstractMetadata> imple
*
* @param store the DIDStore
*/
constructor(store?: DIDStore) {
protected constructor(store?: DIDStore) {
super();
this.store = store;
this.props = {};
Expand Down Expand Up @@ -89,13 +89,13 @@ export abstract class AbstractMetadata extends DIDEntity<AbstractMetadata> imple
return this.props[name];
}

protected put(name: string, value: JSONValue | Date ) {
protected put(name: string, value: JSONValue | Date ): Promise<void> {
if (value === null || value === undefined)
delete this.props[name];
else
this.props[name] = value instanceof Date ? value.toISOString() : value;

this.save();
return this.save();
}

protected getBoolean(name: string, defaultValue: boolean = false): boolean {
Expand Down Expand Up @@ -127,10 +127,10 @@ export abstract class AbstractMetadata extends DIDEntity<AbstractMetadata> imple
return value;
}

protected remove(name: string): any {
protected async remove(name: string): Promise<any> {
let value = this.props[name];
delete this.props[name];
this.save();
await this.save();
return value;
}

Expand All @@ -143,8 +143,8 @@ export abstract class AbstractMetadata extends DIDEntity<AbstractMetadata> imple
*
* @param alias alias string
*/
public setAlias(alias: string) {
this.put(AbstractMetadata.ALIAS, alias);
public setAlias(alias: string): Promise<void> {
return this.put(AbstractMetadata.ALIAS, alias);
}

/**
Expand All @@ -162,9 +162,9 @@ export abstract class AbstractMetadata extends DIDEntity<AbstractMetadata> imple
* @param key the key string
* @param value the value
*/
public setExtra(key: string, value: any) {
public setExtra(key: string, value: any): Promise<void> {
checkArgument(key != null && key != "", "Invalid key");
this.put(AbstractMetadata.USER_EXTRA_PREFIX + key, value);
return this.put(AbstractMetadata.USER_EXTRA_PREFIX + key, value);
}

/**
Expand Down Expand Up @@ -193,9 +193,9 @@ export abstract class AbstractMetadata extends DIDEntity<AbstractMetadata> imple
return this.getDate(AbstractMetadata.USER_EXTRA_PREFIX + key, defaultValue);
}

public removeExtra(key: string): string {
public async removeExtra(key: string): Promise<string> {
checkArgument(key && key != null, "Invalid key");
return this.remove(AbstractMetadata.USER_EXTRA_PREFIX + key);
return await this.remove(AbstractMetadata.USER_EXTRA_PREFIX + key);
}

/**
Expand Down Expand Up @@ -233,5 +233,5 @@ export abstract class AbstractMetadata extends DIDEntity<AbstractMetadata> imple
this.props = JSON.parse(JSON.stringify(json));
}

protected abstract save();
protected abstract save(): Promise<void>;
}
6 changes: 3 additions & 3 deletions src/conflicthandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
* SOFTWARE.
*/

import type { DIDDocument } from "./internals";
import type {DIDDocument} from "./internals";

/**
* The interface for ConflictHandle to indicate how to resolve the conflict,
* if the local document is different with the one resolved from chain.
*/
export interface ConflictHandle {
export interface ConflictHandle {
/**
* The method to merge two did document.
*
* @param chainCopy the document from chain
* @param localCopy the document from local device
* @return the merged DIDDocument object
*/
merge(chainCopy: DIDDocument, localCopy: DIDDocument): DIDDocument;
merge(chainCopy: DIDDocument, localCopy: DIDDocument): Promise<DIDDocument>;
}
16 changes: 8 additions & 8 deletions src/credentialmetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export class CredentialMetadata extends AbstractMetadata implements Cloneable<Cr
*
* @param txid the transaction id string
*/
public setTransactionId(txid: string) {
this.put(CredentialMetadata.TXID, txid);
public setTransactionId(txid: string): Promise<void> {
return this.put(CredentialMetadata.TXID, txid);
}

/**
Expand All @@ -85,10 +85,10 @@ export class CredentialMetadata extends AbstractMetadata implements Cloneable<Cr
*
* @param timestamp the time published
*/
public setPublished(timestamp: Date) {
public setPublished(timestamp: Date): Promise<void> {
checkArgument(timestamp != null, "Invalid timestamp");

this.put(CredentialMetadata.PUBLISHED, timestamp);
return this.put(CredentialMetadata.PUBLISHED, timestamp);
}

/**
Expand All @@ -105,8 +105,8 @@ export class CredentialMetadata extends AbstractMetadata implements Cloneable<Cr
*
* @param revoked the revocation status
*/
public setRevoked(revoked: boolean) {
this.put(CredentialMetadata.REVOKED, revoked);
public setRevoked(revoked: boolean): Promise<void> {
return this.put(CredentialMetadata.REVOKED, revoked);
}

/**
Expand Down Expand Up @@ -135,10 +135,10 @@ export class CredentialMetadata extends AbstractMetadata implements Cloneable<Cr
}
}

protected save() {
protected async save(): Promise<void> {
if (this.attachedStore()) {
try {
this.getStore().storeCredentialMetadata(this.id, this);
await this.getStore().storeCredentialMetadata(this.id, this);
} catch (e) {
// DIDStoreException
CredentialMetadata.log.error("INTERNAL - error store metadata for credential {}", this.id);
Expand Down
4 changes: 2 additions & 2 deletions src/defaultconflicthandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class DefaultConflictHandle implements ConflictHandle {
return DefaultConflictHandle.instance;
}

public merge(chainDoc: DIDDocument, localDoc: DIDDocument): DIDDocument {
return localDoc;
public merge(chainDoc: DIDDocument, localDoc: DIDDocument): Promise<DIDDocument> {
return Promise.resolve(localDoc);
}
}
22 changes: 11 additions & 11 deletions src/didbackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ export class DIDBackend {
let doc = tx.getRequest().getDocument().clone();
await doc.resolveControllers();
let metadata = doc.getMetadata();
metadata.setTransactionId(tx.getTransactionId());
metadata.setSignature(doc.getProof().getSignature());
metadata.setPublished(tx.getTimestamp());
await metadata.setTransactionId(tx.getTransactionId());
await metadata.setSignature(doc.getProof().getSignature());
await metadata.setPublished(tx.getTimestamp());
if (bio.getStatus().equals(DIDBiographyStatus.DEACTIVATED))
metadata.setDeactivated(true);
await metadata.setDeactivated(true);

return doc;
}
Expand Down Expand Up @@ -367,11 +367,11 @@ export class DIDBackend {
let doc = tx.getRequest().getDocument().clone();
await doc.resolveControllers();
let metadata = doc.getMetadata();
metadata.setTransactionId(tx.getTransactionId());
metadata.setSignature(doc.getProof().getSignature());
metadata.setPublished(tx.getTimestamp());
await metadata.setTransactionId(tx.getTransactionId());
await metadata.setSignature(doc.getProof().getSignature());
await metadata.setPublished(tx.getTimestamp());
if (bio.getStatus() == DIDBiographyStatus.DEACTIVATED)
metadata.setDeactivated(true);
await metadata.setDeactivated(true);

return doc;
}
Expand Down Expand Up @@ -447,10 +447,10 @@ export class DIDBackend {

let vc = tx.getRequest().getCredential();
let metadata = new CredentialMetadata(vc.getId());
metadata.setTransactionId(tx.getTransactionId());
metadata.setPublished(tx.getTimestamp());
await metadata.setTransactionId(tx.getTransactionId());
await metadata.setPublished(tx.getTimestamp());
if (bio.getStatus() == CredentialBiographyStatus.REVOKED)
metadata.setRevoked(true);
await metadata.setRevoked(true);
vc.setMetadata(metadata);
return vc;
}
Expand Down
26 changes: 13 additions & 13 deletions src/diddocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,14 @@ export class DIDDocument extends DIDEntity<DIDDocument> {
* @return the key exists or not
* @throws DIDStoreException there is no store
*/
public hasPrivateKey(idOrString: DIDURL | string): boolean {
public async hasPrivateKey(idOrString: DIDURL | string): Promise<boolean> {
if (typeof idOrString === "string")
idOrString = this.canonicalId(idOrString);

checkArgument(idOrString != null, "Invalid publicKey id");

if (this.hasPublicKey(idOrString) && this.getMetadata().attachedStore())
return this.getMetadata().getStore().containsPrivateKey(idOrString);
return await this.getMetadata().getStore().containsPrivateKey(idOrString);
else
return false;
}
Expand Down Expand Up @@ -1258,7 +1258,7 @@ export class DIDDocument extends DIDEntity<DIDDocument> {
let deactivated = bio.getStatus() == DIDBiographyStatus.DEACTIVATED;

if (deactivated)
this.getMetadata().setDeactivated(deactivated);
await this.getMetadata().setDeactivated(deactivated);

return deactivated;
}
Expand Down Expand Up @@ -1605,7 +1605,7 @@ export class DIDDocument extends DIDEntity<DIDDocument> {
key = doc.canonicalId(keyid);

let store = doc.getMetadata().getStore();
if (!store.containsPrivateKey(key))
if (!await store.containsPrivateKey(key))
return null;

let hk = HDKey.deserialize(await store.loadPrivateKey(key, password));
Expand Down Expand Up @@ -1795,7 +1795,7 @@ export class DIDDocument extends DIDEntity<DIDDocument> {
let resolvedDoc = await this.getSubject().resolve(true);
if (resolvedDoc != null) {
if (await resolvedDoc.isDeactivated()) {
this.getMetadata().setDeactivated(true);
await this.getMetadata().setDeactivated(true);

DIDDocument.log.error("Publish failed because DID is deactivated.");
throw new DIDDeactivatedException(this.getSubject().toString() + " is deactivated");
Expand Down Expand Up @@ -1868,8 +1868,8 @@ export class DIDDocument extends DIDEntity<DIDDocument> {
}

if (resolvedSignature != null)
this.getMetadata().setPreviousSignature(resolvedSignature);
this.getMetadata().setSignature(this.getProof().getSignature());
await this.getMetadata().setPreviousSignature(resolvedSignature);
await this.getMetadata().setSignature(this.getProof().getSignature());
}

// TODO: to be remove in the future
Expand Down Expand Up @@ -1903,7 +1903,7 @@ export class DIDDocument extends DIDEntity<DIDDocument> {
let resolvedDoc = await DIDBackend.getInstance().resolveUntrustedDid(this.getSubject(), true);
if (resolvedDoc != null) {
if (await resolvedDoc.isDeactivated()) {
this.getMetadata().setDeactivated(true);
await this.getMetadata().setDeactivated(true);

DIDDocument.log.error("Publish failed because DID is deactivated.");
throw new DIDDeactivatedException(this.getSubject().toString() + " is deactivated");
Expand All @@ -1929,8 +1929,8 @@ export class DIDDocument extends DIDEntity<DIDDocument> {
}

if (resolvedSignature != null)
this.getMetadata().setPreviousSignature(resolvedSignature);
this.getMetadata().setSignature(this.getProof().getSignature());
await this.getMetadata().setPreviousSignature(resolvedSignature);
await this.getMetadata().setSignature(this.getProof().getSignature());
}


Expand Down Expand Up @@ -2851,7 +2851,7 @@ export namespace DIDDocument {
* @param force the owner of public key
* @return the DID Document Builder object
*/
public removePublicKey(id: DIDURL | string, force = false): Builder {
public async removePublicKey(id: DIDURL | string, force = false): Promise<Builder> {
this.checkNotSealed();
checkArgument(id != null, "Invalid publicKey id");

Expand Down Expand Up @@ -2879,7 +2879,7 @@ export namespace DIDDocument {
try {
// TODO: should delete the loosed private key when store the document
if (this.document.getMetadata().attachedStore())
this.document.getMetadata().getStore().deletePrivateKey(id);
await this.document.getMetadata().getStore().deletePrivateKey(id);
} catch (ignore) {
// DIDStoreException
Builder.log.error("INTERNAL - Remove private key", ignore);
Expand Down Expand Up @@ -3196,7 +3196,7 @@ export namespace DIDDocument {
"Invalid publicKey id");
checkArgument(storepass && storepass != null, "Invalid storepass");

let issuer = new Issuer(this.document);
let issuer = await Issuer.create(this.document);
let cb = issuer.issueFor(this.document.getSubject());

if (expirationDate == null)
Expand Down
6 changes: 4 additions & 2 deletions src/didentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export abstract class DIDEntity<T> {
* @throws DIDSyntaxException if a parse error occurs
*/
protected static deserialize<T extends DIDEntity<T>>(source: JSONObject | string, type: (new () => T), context: DID = null): T {
return this.deserializeWithObj<T>(source, new type(), context);
}

protected static deserializeWithObj<T extends DIDEntity<T>>(source: JSONObject | string, obj: T, context: DID = null): T {
checkArgument(source && source !== "", "Invalid JSON content");

let content: JSONObject;
Expand All @@ -85,9 +89,7 @@ export abstract class DIDEntity<T> {
content = source;
}

let obj = new type();
obj.fromJSON(content, context);

return obj;
}

Expand Down
Loading