Skip to content

Commit

Permalink
add getGISTProof
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Nov 14, 2024
1 parent 856fd47 commit 743f5c4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/iden3comm/types/protocol/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
DIDDocument as DidResolverDidDocument,
VerificationMethod as DidResolverVerificationMethod
} from 'did-resolver';
import { RootInfo, StateInfo } from '../../../storage';
import { RootInfoWithProof, StateInfo } from '../../../storage';

/** AuthorizationResponseMessage is struct the represents iden3message authorization response */
export type AuthorizationResponseMessage = BasicMessage & {
Expand Down Expand Up @@ -89,5 +89,5 @@ export type DIDDocument = DidResolverDidDocument & {
export type VerificationMethod = DidResolverVerificationMethod & {
published?: boolean;
info?: StateInfo;
global?: RootInfo;
global?: RootInfoWithProof;
};
33 changes: 29 additions & 4 deletions src/storage/blockchain/did-resolver-readonly-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IStateStorage } from '../interfaces';
import { DID, Id } from '@iden3/js-iden3-core';
import { JsonRpcProvider } from 'ethers';

export class DidResolverReadonlyStorage implements IStateStorage {
export class DidResolverStateReadonlyStorage implements IStateStorage {
constructor(private readonly resolverUrl: string) {}
async getLatestStateById(id: bigint): Promise<StateInfo> {
return this.getStateInfo(id);
Expand All @@ -15,6 +15,34 @@ export class DidResolverReadonlyStorage implements IStateStorage {
return this.getStateInfo(id, state);
}

async getGISTProof(id: bigint): Promise<StateProof> {
const { didDocument } = await resolveDidDocument(
DID.parseFromId(Id.fromBigInt(id)),
this.resolverUrl
);
const vm = (didDocument as DIDDocument).verificationMethod?.find(
(i) => i.type === 'Iden3StateInfo2023'
);
if (!vm) {
throw new Error('Iden3StateInfo2023 verification method not found');
}
const { global } = vm as VerificationMethod;
if (!global) {
throw new Error('GIST root not found');
}
const { proof } = global;
return {
root: BigInt(proof.root),
existence: proof.existence,
siblings: proof.siblings?.map((sibling) => BigInt(sibling)),
index: BigInt(proof.index),
value: BigInt(proof.value),
auxExistence: proof.auxExistence,
auxIndex: BigInt(proof.auxIndex),
auxValue: BigInt(proof.auxValue)
};
}

async getGISTRootInfo(root: bigint, userId: bigint): Promise<RootInfo> {
const { didDocument } = await resolveDidDocument(
DID.parseFromId(Id.fromBigInt(userId)),
Expand All @@ -36,9 +64,6 @@ export class DidResolverReadonlyStorage implements IStateStorage {
return global;
}

async getGISTProof(): Promise<StateProof> {
throw new Error('Method not implemented.');
}
getRpcProvider(): JsonRpcProvider {
throw new Error('Method not implemented.');
}
Expand Down
10 changes: 10 additions & 0 deletions src/storage/entities/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ export interface RootInfo {
replacedAtBlock: bigint;
}

/**
* global identity state root info from DID resolver document
*
* @public
* @interface RootInfoWithProof
*/
export interface RootInfoWithProof extends RootInfo {
proof: StateProof;
}

/**
* identity state message
*
Expand Down
12 changes: 9 additions & 3 deletions tests/handlers/readonly-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { expect } from 'chai';
import { DidResolverReadonlyStorage, IStateStorage } from '../../src';
import { DidResolverStateReadonlyStorage, IStateStorage } from '../../src';
import { DID } from '@iden3/js-iden3-core';
import { Hash } from '@iden3/js-merkletree';

describe('resolver readonly storage', () => {
describe.skip('resolver readonly storage', () => {
let stateStorage: IStateStorage;
const did = DID.parse('did:polygonid:polygon:amoy:2qV7YACbSYpvuXySSqhBd6E4XAxzLE5kYmPqwxuvwD');
const id = DID.idFromDID(did);
beforeEach(async () => {
stateStorage = new DidResolverReadonlyStorage('https://resolver-dev.privado.id/');
stateStorage = new DidResolverStateReadonlyStorage('http://127.0.0.1:8080');
});

it('getLatestStateById', async () => {
Expand All @@ -24,6 +24,12 @@ describe('resolver readonly storage', () => {
expect(info).to.have.property('id');
});

it('getGISTProof', async () => {
const proof = await stateStorage.getGISTProof(id.bigInt());
expect(proof).to.be.an('object');
expect(proof).to.have.property('root');
});

it('getGISTRootInfo', async () => {
const root = Hash.fromHex('44f40dd34c5b840ef1c55e3805febef589069dab35f8684857ba118778826d1b');
const rootInfo = await stateStorage.getGISTRootInfo(root.bigInt(), id.bigInt());
Expand Down

0 comments on commit 743f5c4

Please sign in to comment.