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

Fix/minor-fixes #109

Merged
merged 4 commits into from
Jul 26, 2023
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
4 changes: 2 additions & 2 deletions src/credentials/status/agent-revocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CredentialStatusResolver, CredentialStatusResolveOptions } from './reso
import { RevocationStatusRequestMessage } from '../../iden3comm/types';
import { MediaType, PROTOCOL_MESSAGE_TYPE } from '../../iden3comm/constants';
import * as uuid from 'uuid';
import { RevocationStatusDTO } from './sparse-merkle-tree';
import { toRevocationStatus } from './sparse-merkle-tree';

/**
* AgentResolver is a class that allows to interact with the issuer's agent to get revocation status.
Expand Down Expand Up @@ -46,7 +46,7 @@ export class AgentResolver implements CredentialStatusResolver {
}
});
const agentResponse = await response.json();
return new RevocationStatusDTO(agentResponse.body).toRevocationStatus();
return toRevocationStatus(agentResponse.body);
}
}

Expand Down
77 changes: 36 additions & 41 deletions src/credentials/status/sparse-merkle-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,61 +21,56 @@ export class IssuerResolver implements CredentialStatusResolver {
async resolve(credentialStatus: CredentialStatus): Promise<RevocationStatus> {
const revStatusResp = await fetch(credentialStatus.id);
const revStatus = await revStatusResp.json();
return new RevocationStatusDTO(revStatus).toRevocationStatus();
return toRevocationStatus(revStatus);
}
}

/**
* Proof dto as a partial result of fetching credential status with type SparseMerkleTreeProof
* RevocationStatusResponse is a response of fetching credential status with type SparseMerkleTreeProof
*
* @interface ProofDTO
* @export
* @interface RevocationStatusResponse
*/
export interface ProofDTO {
existence: boolean;
siblings: string[];
node_aux: {
key: string;
value: string;
export interface RevocationStatusResponse {
issuer: Issuer;
mtp: {
existence: boolean;
siblings: string[];
node_aux: {
key: string;
value: string;
};
};
}

/**
* RevocationStatusDTO is a result of fetching credential status with type SparseMerkleTreeProof
* toRevocationStatus is a result of fetching credential status with type SparseMerkleTreeProof converts to RevocationStatus
*
* @public
* @class RevocationStatusDTO
* @param {RevocationStatusResponse} { issuer, mtp }
* @returns {RevocationStatus} RevocationStatus
*/
export class RevocationStatusDTO {
issuer!: Issuer;
mtp!: ProofDTO;

constructor(payload: object) {
Object.assign(this, payload);
export const toRevocationStatus = ({ issuer, mtp }: RevocationStatusResponse): RevocationStatus => {
const p = new Proof();
p.existence = mtp.existence;
if (mtp.node_aux) {
p.nodeAux = {
key: newHashFromBigInt(BigInt(mtp.node_aux.key)),
value: newHashFromBigInt(BigInt(mtp.node_aux.value))
};
}
const s = mtp.siblings.map((s) => newHashFromBigInt(BigInt(s)));

toRevocationStatus(): RevocationStatus {
const p = new Proof();
p.existence = this.mtp.existence;
if (this.mtp.node_aux) {
p.nodeAux = {
key: newHashFromBigInt(BigInt(this.mtp.node_aux.key)),
value: newHashFromBigInt(BigInt(this.mtp.node_aux.value))
};
}
const s = this.mtp.siblings.map((s) => newHashFromBigInt(BigInt(s)));
p.siblings = [];
p.depth = s.length;

p.siblings = [];
p.depth = s.length;

for (let lvl = 0; lvl < s.length; lvl++) {
if (s[lvl].bigInt() !== BigInt(0)) {
setBitBigEndian(p.notEmpties, lvl);
p.siblings.push(s[lvl]);
}
for (let lvl = 0; lvl < s.length; lvl++) {
if (s[lvl].bigInt() !== BigInt(0)) {
setBitBigEndian(p.notEmpties, lvl);
p.siblings.push(s[lvl]);
}
return {
mtp: p,
issuer: this.issuer
};
}
}
return {
mtp: p,
issuer
};
};
16 changes: 2 additions & 14 deletions src/storage/indexed-db/merkletree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { IdentityMerkleTreeMetaInformation, MerkleTreeType } from '../entities/m
import * as uuid from 'uuid';

import { IMerkleTreeStorage } from '../interfaces/merkletree';

const mtTypes = [MerkleTreeType.Claims, MerkleTreeType.Revocations, MerkleTreeType.Roots];
import { createMerkleTreeMetaInfo } from '../utils';

/**
* Merkle tree storage that uses browser indexed db storage
Expand Down Expand Up @@ -48,24 +47,13 @@ export class MerkleTreeIndexedDBStorage implements IMerkleTreeStorage {
if (!identifier) {
identifier = `${uuid.v4()}`;
}
const createMetaInfo = () => {
const treesMeta: IdentityMerkleTreeMetaInformation[] = [];
for (let index = 0; index < mtTypes.length; index++) {
const mType = mtTypes[index];
const treeId = identifier.concat('+' + mType.toString());
const metaInfo = { treeId, identifier, type: mType };
treesMeta.push(metaInfo);
}
return treesMeta;
};

const existingBinging = await get(identifier, this._bindingStore);
if (existingBinging) {
throw new Error(
`Present merkle tree meta information in the store for current identifier ${identifier}`
);
}
const treesMeta = createMetaInfo();
const treesMeta = createMerkleTreeMetaInfo(identifier);
await set(identifier, treesMeta, this._merkleTreeMetaStore);
return treesMeta;
}
Expand Down
36 changes: 11 additions & 25 deletions src/storage/local-storage/merkletree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { IdentityMerkleTreeMetaInformation, MerkleTreeType } from '../entities/m
import * as uuid from 'uuid';

import { IMerkleTreeStorage } from '../interfaces/merkletree';

const mtTypes = [MerkleTreeType.Claims, MerkleTreeType.Revocations, MerkleTreeType.Roots];
import { createMerkleTreeMetaInfo } from '../utils';

/**
* Merkle tree storage that uses browser local storage
Expand Down Expand Up @@ -34,16 +33,6 @@ export class MerkleTreeLocalStorage implements IMerkleTreeStorage {
if (!identifier) {
identifier = `${uuid.v4()}`;
}
const createMetaInfo = () => {
const treesMeta: IdentityMerkleTreeMetaInformation[] = [];
for (let index = 0; index < mtTypes.length; index++) {
const mType = mtTypes[index];
const treeId = identifier.concat('+' + mType.toString());
const metaInfo = { treeId, identifier, type: mType };
treesMeta.push(metaInfo);
}
return treesMeta;
};
const meta = localStorage.getItem(MerkleTreeLocalStorage.storageKeyMeta);
if (meta) {
const metaInfo: IdentityMerkleTreeMetaInformation[] = JSON.parse(meta);
Expand All @@ -57,15 +46,15 @@ export class MerkleTreeLocalStorage implements IMerkleTreeStorage {
if (identityMetaInfo.length > 0) {
return identityMetaInfo;
}
const treesMeta = createMetaInfo();
const treesMeta = createMerkleTreeMetaInfo(identifier);
localStorage.setItem(
MerkleTreeLocalStorage.storageKeyMeta,
JSON.stringify([...metaInfo, ...treesMeta])
);

return [...metaInfo, ...treesMeta];
}
const treesMeta = createMetaInfo();
const treesMeta = createMerkleTreeMetaInfo(identifier);
localStorage.setItem(MerkleTreeLocalStorage.storageKeyMeta, JSON.stringify(treesMeta));
return treesMeta;
}
Expand All @@ -91,6 +80,11 @@ export class MerkleTreeLocalStorage implements IMerkleTreeStorage {
identifier: string,
mtType: MerkleTreeType
): Promise<Merkletree> {
const resultMeta = this.getMeta(identifier, mtType);
return new Merkletree(new LocalStorageDB(str2Bytes(resultMeta.treeId)), true, this._mtDepth);
}

private getMeta(identifier: string, mtType: MerkleTreeType) {
const meta = localStorage.getItem(MerkleTreeLocalStorage.storageKeyMeta);
const err = new Error(`Merkle tree not found for identifier ${identifier} and type ${mtType}`);
if (!meta) {
Expand All @@ -102,25 +96,17 @@ export class MerkleTreeLocalStorage implements IMerkleTreeStorage {
if (!resultMeta) {
throw err;
}
return new Merkletree(new LocalStorageDB(str2Bytes(resultMeta.treeId)), true, this._mtDepth);
return resultMeta;
}

/** adds to merkle tree in the local storage */
async addToMerkleTree(
identifier: string,
mtType: MerkleTreeType,
hindex: bigint,
hvalue: bigint
): Promise<void> {
const meta = localStorage.getItem(MerkleTreeLocalStorage.storageKeyMeta);
if (!meta) {
throw new Error(`Merkle tree meta not found for identifier ${identifier}`);
}

const metaInfo: IdentityMerkleTreeMetaInformation[] = JSON.parse(meta);
const resultMeta = metaInfo.filter((m) => m.identifier === identifier && m.type === mtType)[0];
if (!resultMeta) {
throw new Error(`Merkle tree not found for identifier ${identifier} and type ${mtType}`);
}
const resultMeta = this.getMeta(identifier, mtType);

const tree = new Merkletree(
new LocalStorageDB(str2Bytes(resultMeta.treeId)),
Expand Down
9 changes: 4 additions & 5 deletions src/storage/memory/merkletree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { IdentityMerkleTreeMetaInformation, MerkleTreeType } from '../entities/m
import * as uuid from 'uuid';

import { IMerkleTreeStorage } from '../interfaces/merkletree';
import { MERKLE_TREE_TYPES } from '../utils';

const mtTypes = [MerkleTreeType.Claims, MerkleTreeType.Revocations, MerkleTreeType.Roots];

declare type TreeWithMetaInfo = {
export interface TreeWithMetaInfo {
tree: Merkletree;
metaInfo: IdentityMerkleTreeMetaInformation;
};
}

/**
*
Expand Down Expand Up @@ -59,7 +58,7 @@ export class InMemoryMerkleTreeStorage implements IMerkleTreeStorage {
this._data[identifier] = [];

const treesMeta: IdentityMerkleTreeMetaInformation[] = [];
mtTypes.forEach((t) => {
MERKLE_TREE_TYPES.forEach((t) => {
const treeId = identifier.concat('+' + t.toString());
const tree = new Merkletree(new InMemoryDB(str2Bytes(treeId)), true, this.mtDepth);

Expand Down
19 changes: 19 additions & 0 deletions src/storage/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IdentityMerkleTreeMetaInformation, MerkleTreeType } from './entities';

export const MERKLE_TREE_TYPES: MerkleTreeType[] = [
MerkleTreeType.Claims,
MerkleTreeType.Revocations,
MerkleTreeType.Roots
];

export const createMerkleTreeMetaInfo = (
identifier: string
): IdentityMerkleTreeMetaInformation[] => {
const treesMeta: IdentityMerkleTreeMetaInformation[] = [];
for (let index = 0; index < MERKLE_TREE_TYPES.length; index++) {
const mType = MERKLE_TREE_TYPES[index];
const treeId = `${identifier}+${mType}`;
treesMeta.push({ treeId, identifier, type: mType });
}
return treesMeta;
};
Loading