diff --git a/packages/connector/src/zkp/helpers/state-v2-helpers.ts b/packages/connector/src/zkp/helpers/state-v2-helpers.ts index 80cb5264..d0402b21 100644 --- a/packages/connector/src/zkp/helpers/state-v2-helpers.ts +++ b/packages/connector/src/zkp/helpers/state-v2-helpers.ts @@ -133,14 +133,10 @@ export const getGISTProof = async ({ rawProvider, ); - console.log('contractInstance', contractInstance); - const data = rootHash ? await contractInstance.getGISTProofByRoot(userId, rootHash) : await contractInstance.getGISTProof(userId); - console.log('data', data); - return { root: BigInt(data.root.toString()), existence: data.existence, diff --git a/packages/zkp-iden3/.eslintrc.js b/packages/zkp-iden3/.eslintrc.js index 656b2666..41316059 100644 --- a/packages/zkp-iden3/.eslintrc.js +++ b/packages/zkp-iden3/.eslintrc.js @@ -6,6 +6,7 @@ module.exports = { 'dist/', '**/*.js', '**/scripts/**/*.js', + '**/jest.config.ts', '**/*.test.ts', '**/src/**/types/**/contracts/**/*.ts', '**/src/**/types/**/graphql/**/*.ts', diff --git a/packages/zkp-iden3/.npmignore b/packages/zkp-iden3/.npmignore index ef7aa8ee..89661ced 100644 --- a/packages/zkp-iden3/.npmignore +++ b/packages/zkp-iden3/.npmignore @@ -1,4 +1,5 @@ src/**/*.test.ts +jest.config.ts jest.config.js tsconfig.json tsconfig.build.json diff --git a/packages/zkp-iden3/jest.config.js b/packages/zkp-iden3/jest.config.js index cb9e24d6..2c09c010 100644 --- a/packages/zkp-iden3/jest.config.js +++ b/packages/zkp-iden3/jest.config.js @@ -1,3 +1,18 @@ +const baseConfig = require('./jest.config'); +const fs = require('fs'); +const config = JSON.parse(fs.readFileSync(`${__dirname}/.swcrc`, 'utf-8')); + module.exports = { - ...require('../../jest.config.base.js'), + ...baseConfig, + roots: ['/src'], + testMatch: [ + '**/__tests__/**/*.+(ts|tsx|js)', + '**/?(*.)+(spec|tests|test).+(ts|tsx|js)', + ], + transform: { + '^.+\\.(t|j)sx?$': ['@swc/jest', { ...config, swcrc: false, exclude: [] }], + }, + moduleNameMapper: { + '^@/(.*)': '/src/$1', + }, }; diff --git a/packages/zkp-iden3/package.json b/packages/zkp-iden3/package.json index 44b2f448..1b554aa6 100644 --- a/packages/zkp-iden3/package.json +++ b/packages/zkp-iden3/package.json @@ -39,7 +39,7 @@ "build:esm": "npx swc src -d ./dist/esm --config-file ./.swcrc -C module.type=es6 isModule=true", "build:types": "tsc -p tsconfig.build.json --outDir ./dist/types --declaration --emitDeclarationOnly", "clean": "rm -rf dist", - "test": "yarn jest --verbose", + "test": "jest --verbose", "typecheck": "tsc --noEmit" }, "dependencies": { @@ -76,10 +76,12 @@ "ethers": "5.7.2", "graphql": "^16.8.1", "graphql-tag": "^2.12.6", + "idb-keyval": "^6.2.1", "key-did-provider-ed25519": "3.0.2", "key-did-resolver": "3.0.0", "lodash": "^4.17.21", "long": "^5.2.3", + "rfc4648": "^1.5.3", "tslib": "^2.5.0", "uuid": "9.0.0" }, diff --git a/packages/zkp-iden3/src/tests/index.test.ts b/packages/zkp-iden3/src/tests/index.test.ts new file mode 100644 index 00000000..fd27b6d3 --- /dev/null +++ b/packages/zkp-iden3/src/tests/index.test.ts @@ -0,0 +1,187 @@ +import { DID } from '@iden3/js-iden3-core'; +import { + type ChainZkpInfo, + // checkIfStateSynced, + CircuitId, + getCoreOperationByIndex, + type GetStateInfoResponse, + loadDataFromRarimoCore, + // type MerkleProof, + type SaveCredentialsRequestParams, + type W3CCredential, +} from '@rarimo/rarime-connector'; + +import { parseDidV2 } from '../helpers'; + +import { AuthZkp, Identity, ZkpGen } from '../instances'; + +const PK = 'c5f1c3e6db9204b037841005c183ee605e69683ca1fec67ac87db86f6639724b'; + +const userDid = 'did:iden3:readonly:tQJ8hwYwVr6M7JDVZLMrSjpi9DDistyDaBofjB6XY'; + +const AUTH_BJJ_CREDENTIAL_HASH = 'cca3371a6cb1b715004407e325bd993c'; +const ID_TYPE = Uint8Array.from([1, 0]); + +const offer: SaveCredentialsRequestParams = { + body: { + Credentials: [ + { + description: 'urn:uuid:6dff4518-5177-4f39-af58-9c156d9b6309', + id: '3dc7608e-dbce-11ee-b1f8-220bd8de42d4', + }, + ], + url: 'https://issuer.polygon.robotornot.mainnet-beta.rarimo.com/v1/agent', + }, + from: 'did:iden3:readonly:tLd8sbb1xTSvi2wtRF4TUVcfDUr8ppYMohLqjhGQT', + id: 'c4960a88-73ae-44b2-b6d5-e8326bd41521', + threadID: 'c4960a88-73ae-44b2-b6d5-e8326bd41521', + to: 'did:iden3:readonly:tQJ8hwYwVr6M7JDVZLMrSjpi9DDistyDaBofjB6XY', + typ: 'application/iden3comm-plain-json', + type: 'https://iden3-communication.io/credentials/1.0/offer', +}; + +const chainInfo: ChainZkpInfo = { + targetChainId: 11155111, + targetRpcUrl: 'https://endpoints.omniatech.io/v1/eth/sepolia/public', + targetStateContractAddress: '0x8a9F505bD8a22BF09b0c19F65C17426cd33f3912', + + rarimoApiUrl: 'https://rpc-api.node1.mainnet-beta.rarimo.com', + rarimoEvmRpcApiUrl: 'https://rpc.evm.node1.mainnet-beta.rarimo.com', + rarimoStateContractAddress: '0x753a8678c85d5fb70A97CFaE37c84CE2fD67EDE8', + + rarimoNetworkType: 'beta', +}; + +const MINUTE = 60 * 1000; + +describe('zkp flow', () => { + let vcs: W3CCredential[]; + + const getIdentity = async () => { + return Identity.create( + { + idType: ID_TYPE, + schemaHashHex: AUTH_BJJ_CREDENTIAL_HASH, + }, + PK, + ); + }; + + it( + 'should create identity', + async () => { + const identity = await getIdentity(); + + expect(identity.didString).toBe(userDid); + }, + MINUTE, + ); + + it( + 'should get VC', + async () => { + const identity = await getIdentity(); + + const authProof = new AuthZkp(identity, offer, { + chainInfo, + circuitsUrls: { + wasmUrl: + 'https://ipfs.tokend.io/ipfs/ipfs/QmYd41GHrKQLqbk96zHbmHU5rGVcxwmAgBpRqLCGLK7LQu', + keyUrl: + 'https://ipfs.tokend.io/ipfs/ipfs/QmWKor7i9r2zbM6oqSdgPUCvgyYESH39qXk1f5tbdeaAg7', + }, + }); + + vcs = await authProof.getVerifiableCredentials(); + + expect(vcs[0].id.includes(offer.body.Credentials[0].id)).toBe(true); + }, + MINUTE * 5, + ); + + it( + 'should create proof', + async () => { + const identity = await getIdentity(); + + // const isSynced = await checkIfStateSynced(chainInfo) + + const did = parseDidV2(vcs[0].issuer); + + const issuerId = DID.idFromDID(did); + + const issuerHexId = `0x0${issuerId.bigInt().toString(16)}`; + + const stateData = await loadDataFromRarimoCore( + `/rarimo/rarimo-core/identity/state/${issuerHexId}`, + chainInfo.rarimoApiUrl, + ); + // const merkleProof = await loadDataFromRarimoCore( + // `/rarimo/rarimo-core/identity/state/${issuerHexId}/proof`, + // chainInfo.rarimoApiUrl, + // stateData.state.createdAtBlock, + // ) + + const operation = await getCoreOperationByIndex( + chainInfo, + stateData.state.lastUpdateOperationIndex, + ); + + const zkpGen = new ZkpGen( + identity, + { + circuitId: CircuitId.AtomicQueryMTPV2OnChain, + accountAddress: '0xEA931A38726546cB7B5992483867387fC9FAdF7b', + + query: { + allowedIssuers: ['*'], + credentialSubject: { + isNatural: { + $eq: 1, + }, + }, + type: [vcs[0].type[1]], + }, + }, + vcs[0], + { + chainInfo, + circuitsUrls: { + [CircuitId.AtomicQuerySigV2]: { + wasmUrl: + 'https://storage.googleapis.com/rarimo-store/snap/QmYB5QLvH5ihiedxvzkG3XPQngjxcS8wc1xCAoKnGS5GfC', + keyUrl: + 'https://storage.googleapis.com/rarimo-store/snap/QmeXxRXxYGCwa48ANikH5Knzi9cgkmhumPbMtjTKNYkThL', + }, + [CircuitId.AtomicQueryMTPV2]: { + wasmUrl: + 'https://storage.googleapis.com/rarimo-store/snap/QmRqGgnN6Qy4LuPxQKH2wrADNe4aJb8wYJhS1ky9zbLS8t', + keyUrl: + 'https://storage.googleapis.com/rarimo-store/snap/QmcLyDLPWJpyEWeR9KkWQuGHAqifnwpDAWBX1R6a7g6F6a', + }, + [CircuitId.AtomicQuerySigV2OnChain]: { + wasmUrl: + 'https://storage.googleapis.com/rarimo-store/snap/QmS4vURQ1c8tgALSokdTYVqx5E9FmASbu964W3JevnM3B4', + keyUrl: + 'https://storage.googleapis.com/rarimo-store/snap/QmT45Y62hfZnADq6VvKGjNR8foNb2KjcyG4AStRRAN9iHm', + }, + [CircuitId.AtomicQueryMTPV2OnChain]: { + wasmUrl: + 'https://ipfs.tokend.io/ipfs/ipfs/QmPtPiFgZigau2VNpSCoagNj36ZpuuATRNvyNPAvvUgvq6', + keyUrl: + 'https://ipfs.tokend.io/ipfs/ipfs/QmU8fC3xwjMcmnsB88SrdKRZpskhxUwBRnaLMa1AcN9ERj', + }, + }, + }, + ); + + const zkpProof = await zkpGen.generateProof( + stateData.state.hash, + operation.operation.details.GISTHash, + ); + + expect(zkpProof).not.toBeNull(); + }, + MINUTE * 5, + ); +}); diff --git a/packages/zkp-iden3/tests/index.test.ts b/packages/zkp-iden3/tests/index.test.ts deleted file mode 100644 index dee63cb4..00000000 --- a/packages/zkp-iden3/tests/index.test.ts +++ /dev/null @@ -1,171 +0,0 @@ -import { DID } from '@iden3/js-iden3-core' -import { - type ChainZkpInfo, - // checkIfStateSynced, - CircuitId, - getCoreOperationByIndex, - type GetStateInfoResponse, - loadDataFromRarimoCore, - // type MerkleProof, - type SaveCredentialsRequestParams, - type W3CCredential, -} from '@rarimo/rarime-connector' - -import { AuthZkp, Identity, parseDidV2, ZkpGen } from '../src' - -const PK = 'c5f1c3e6db9204b037841005c183ee605e69683ca1fec67ac87db86f6639724b' - -const userDid = 'did:iden3:readonly:tQJ8hwYwVr6M7JDVZLMrSjpi9DDistyDaBofjB6XY' - -const AUTH_BJJ_CREDENTIAL_HASH = 'cca3371a6cb1b715004407e325bd993c' -const ID_TYPE = Uint8Array.from([1, 0]) - -const offer: SaveCredentialsRequestParams = { - body: { - Credentials: [ - { - description: 'urn:uuid:6dff4518-5177-4f39-af58-9c156d9b6309', - id: '3dc7608e-dbce-11ee-b1f8-220bd8de42d4', - }, - ], - url: 'https://issuer.polygon.robotornot.mainnet-beta.rarimo.com/v1/agent', - }, - from: 'did:iden3:readonly:tLd8sbb1xTSvi2wtRF4TUVcfDUr8ppYMohLqjhGQT', - id: 'c4960a88-73ae-44b2-b6d5-e8326bd41521', - threadID: 'c4960a88-73ae-44b2-b6d5-e8326bd41521', - to: 'did:iden3:readonly:tQJ8hwYwVr6M7JDVZLMrSjpi9DDistyDaBofjB6XY', - typ: 'application/iden3comm-plain-json', - type: 'https://iden3-communication.io/credentials/1.0/offer', -} - -const chainInfo: ChainZkpInfo = { - targetChainId: 11155111, - targetRpcUrl: 'https://endpoints.omniatech.io/v1/eth/sepolia/public', - targetStateContractAddress: '0x8a9F505bD8a22BF09b0c19F65C17426cd33f3912', - - rarimoApiUrl: 'https://rpc-api.node1.mainnet-beta.rarimo.com', - rarimoEvmRpcApiUrl: 'https://rpc.evm.node1.mainnet-beta.rarimo.com', - rarimoStateContractAddress: '0x753a8678c85d5fb70A97CFaE37c84CE2fD67EDE8', - - rarimoNetworkType: 'beta', -} - -describe('zkp flow', () => { - let vcs: W3CCredential[] - - const getIdentity = async () => { - return Identity.create( - { - idType: ID_TYPE, - schemaHashHex: AUTH_BJJ_CREDENTIAL_HASH, - }, - PK, - ) - } - - it('should create identity', async () => { - const identity = await getIdentity() - - expect(identity.didString).toBe(userDid) - }) - - it('should get VC', async () => { - const identity = await getIdentity() - - const authProof = new AuthZkp(identity, offer, { - chainInfo, - circuitsUrls: { - wasmUrl: - 'https://ipfs.tokend.io/ipfs/ipfs/QmYd41GHrKQLqbk96zHbmHU5rGVcxwmAgBpRqLCGLK7LQu', - keyUrl: - 'https://ipfs.tokend.io/ipfs/ipfs/QmWKor7i9r2zbM6oqSdgPUCvgyYESH39qXk1f5tbdeaAg7', - }, - }) - - vcs = await authProof.getVerifiableCredentials() - - expect(vcs[0].id.includes(offer.body.Credentials[0].id)).toBe(true) - }) - - it('should create proof', async () => { - const identity = await getIdentity() - - // const isSynced = await checkIfStateSynced(chainInfo) - - const did = parseDidV2(vcs[0].issuer) - - const issuerId = DID.idFromDID(did) - - const issuerHexId = `0x0${issuerId.bigInt().toString(16)}` - - const stateData = await loadDataFromRarimoCore( - `/rarimo/rarimo-core/identity/state/${issuerHexId}`, - chainInfo.rarimoApiUrl, - ) - // const merkleProof = await loadDataFromRarimoCore( - // `/rarimo/rarimo-core/identity/state/${issuerHexId}/proof`, - // chainInfo.rarimoApiUrl, - // stateData.state.createdAtBlock, - // ) - - const operation = await getCoreOperationByIndex( - chainInfo, - stateData.state.lastUpdateOperationIndex, - ) - - const zkpGen = new ZkpGen( - identity, - { - circuitId: CircuitId.AtomicQueryMTPV2OnChain, - accountAddress: '0xEA931A38726546cB7B5992483867387fC9FAdF7b', - - query: { - allowedIssuers: ['*'], - credentialSubject: { - isNatural: { - $eq: 1, - }, - }, - type: [vcs[0].type[1]], - }, - }, - vcs[0], - { - chainInfo, - circuitsUrls: { - [CircuitId.AtomicQuerySigV2]: { - wasmUrl: - 'https://storage.googleapis.com/rarimo-store/snap/QmYB5QLvH5ihiedxvzkG3XPQngjxcS8wc1xCAoKnGS5GfC', - keyUrl: - 'https://storage.googleapis.com/rarimo-store/snap/QmeXxRXxYGCwa48ANikH5Knzi9cgkmhumPbMtjTKNYkThL', - }, - [CircuitId.AtomicQueryMTPV2]: { - wasmUrl: - 'https://storage.googleapis.com/rarimo-store/snap/QmRqGgnN6Qy4LuPxQKH2wrADNe4aJb8wYJhS1ky9zbLS8t', - keyUrl: - 'https://storage.googleapis.com/rarimo-store/snap/QmcLyDLPWJpyEWeR9KkWQuGHAqifnwpDAWBX1R6a7g6F6a', - }, - [CircuitId.AtomicQuerySigV2OnChain]: { - wasmUrl: - 'https://storage.googleapis.com/rarimo-store/snap/QmS4vURQ1c8tgALSokdTYVqx5E9FmASbu964W3JevnM3B4', - keyUrl: - 'https://storage.googleapis.com/rarimo-store/snap/QmT45Y62hfZnADq6VvKGjNR8foNb2KjcyG4AStRRAN9iHm', - }, - [CircuitId.AtomicQueryMTPV2OnChain]: { - wasmUrl: - 'https://ipfs.tokend.io/ipfs/ipfs/QmPtPiFgZigau2VNpSCoagNj36ZpuuATRNvyNPAvvUgvq6', - keyUrl: - 'https://ipfs.tokend.io/ipfs/ipfs/QmU8fC3xwjMcmnsB88SrdKRZpskhxUwBRnaLMa1AcN9ERj', - }, - }, - }, - ) - - const zkpProof = await zkpGen.generateProof( - stateData.state.hash, - operation.operation.details.GISTHash, - ) - - expect(zkpProof).not.toBeNull() - }) -}) diff --git a/yarn.lock b/yarn.lock index 147c6b9a..cbe88cc8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5860,10 +5860,12 @@ __metadata: ethers: 5.7.2 graphql: ^16.8.1 graphql-tag: ^2.12.6 + idb-keyval: ^6.2.1 key-did-provider-ed25519: 3.0.2 key-did-resolver: 3.0.0 lodash: ^4.17.21 long: ^5.2.3 + rfc4648: ^1.5.3 tsc-alias: ^1.8.2 tslib: ^2.5.0 uuid: 9.0.0 @@ -12326,6 +12328,13 @@ __metadata: languageName: node linkType: hard +"idb-keyval@npm:^6.2.1": + version: 6.2.1 + resolution: "idb-keyval@npm:6.2.1" + checksum: 7c0836f832096086e99258167740181132a71dd2694c8b8454a4f5ec69114ba6d70983115153306f0b6de1c8d3bad04f67eed3dff8f50c96815b9985d6d78470 + languageName: node + linkType: hard + "idna-uts46-hx@npm:^2.3.1": version: 2.3.1 resolution: "idna-uts46-hx@npm:2.3.1" @@ -16621,6 +16630,13 @@ __metadata: languageName: node linkType: hard +"rfc4648@npm:^1.5.3": + version: 1.5.3 + resolution: "rfc4648@npm:1.5.3" + checksum: 19c81d502582e377125b00fbd7a5cdb0e351f9a1e40182fa9f608b48e1ab852d211b75facb2f4f3fa17f7c6ebc2ef4acca61ae7eb7fbcfa4768f11d2db678116 + languageName: node + linkType: hard + "rfdc@npm:^1.3.0": version: 1.3.1 resolution: "rfdc@npm:1.3.1"