Skip to content

Commit

Permalink
upgrade latest state on transit (#134)
Browse files Browse the repository at this point in the history
* upgrade latest state on transit
  • Loading branch information
vmidyllic authored Sep 19, 2023
1 parent 77cbb31 commit dd82c0f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/identity/identity-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ export interface IIdentityWallet {
* @returns `{Promise<Profile>}`
*/
getProfileByVerifier(verifier: string): Promise<Profile | undefined>;

/**
*
* updates latest identity state in storage with given state or latest from the trees.
*
* @param {DID} issuerDID - identifier of the issuer
* @param {boolean} published - if states is published onchain
* @param {TreeState} treeState - contains state to upgrade
* @returns `{Promise<void>}`
*/
updateIdentityState(issuerDID: DID, published:boolean, treeState?: TreeState): Promise<void>;

}

/**
Expand All @@ -292,7 +304,7 @@ export class IdentityWallet implements IIdentityWallet {
private readonly _kms: KMS,
private readonly _storage: IDataStorage,
private readonly _credentialWallet: ICredentialWallet
) {}
) { }

/**
* {@inheritDoc IIdentityWallet.createIdentity}
Expand Down Expand Up @@ -757,17 +769,19 @@ export class IdentityWallet implements IIdentityWallet {
}

/** {@inheritDoc IIdentityWallet.generateIden3SparseMerkleTreeProof} */
// treeState - optional, if it is not passed proof of claim inclusion will be generated on the latest state in the tree.
async generateIden3SparseMerkleTreeProof(
issuerDID: DID,
credentials: W3CCredential[],
txId: string,
blockNumber?: number,
blockTimestamp?: number
blockTimestamp?: number,
treeState?: TreeState
): Promise<W3CCredential[]> {
for (let index = 0; index < credentials.length; index++) {
const credential = credentials[index];

const mtpWithProof = await this.generateCredentialMtp(issuerDID, credential);
const mtpWithProof = await this.generateCredentialMtp(issuerDID, credential, treeState);

// credential must have a bjj signature proof
const coreClaim = credential.getCoreClaimFromProof(ProofType.BJJSignature);
Expand Down Expand Up @@ -876,4 +890,17 @@ export class IdentityWallet implements IIdentityWallet {
);
});
}

/** {@inheritDoc IIdentityWallet.updateIdentityState} */
async updateIdentityState(issuerDID: DID, published:boolean, treeState?: TreeState): Promise<void> {
const latestTreeState = await this.getDIDTreeModel(issuerDID);

await this._storage.identity.saveIdentity({
did: issuerDID.string(),
state: treeState ? treeState.state : latestTreeState.state,
isStatePublished: published,
isStateGenesis: false
});
}

}
3 changes: 3 additions & 0 deletions src/proof/proof-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ export class ProofService implements IProofService {
const proof = await this._prover.generate(inputs, CircuitId.StateTransition);

const txId = await stateStorage.publishState(proof, ethSigner);

await this._identityWallet.updateIdentityState(did, true, newTreeState);

return txId;
}

Expand Down

0 comments on commit dd82c0f

Please sign in to comment.