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

ethr-did-resolver: Cannot read property 'getResolver' of undefined #9

Open
pemmenegger opened this issue Apr 10, 2024 · 3 comments
Open

Comments

@pemmenegger
Copy link

Hi there,

First of all, thanks for your work facilitating DID management. I followed the tutorial and tried to add the did:ethr:sepolia provider and resolver, but I had some issues.

The following things work:

  • Creating and resolving identifiers via did:peer (as proposed by the tutorial)
  • Creating identifiers via did:ethr:sepolia using the package @veramo/did-provider-ethr (see screenshot 1)

The following things fail:

  • Resolving a DID document via did:ethr:sepolia using the package ethr-did-resolver (see screenshot 2 and 3)

My Approach

Basically, I have additionally installed and imported the following packages:

import { EthrDIDProvider } from "@veramo/did-provider-ethr";
import { getResolver as ethrDidResolver } from "ethr-did-resolver";

Then, I extended the agent creation code from the tutorial as follows:

// ...
new DIDManager({
  store: new DIDStore(dbConnection),
  defaultProvider: "did:ethr:sepolia",
  providers: {
     // WORKS! (see screenshot 1)
    "did:ethr:sepolia": new EthrDIDProvider({
      defaultKms: "local",
      network: "sepolia",
      rpcUrl: "https://sepolia.infura.io/v3/" + INFURA_PROJECT_ID,
    }),
    "did:peer": new PeerDIDProvider({
      defaultKms: "local",
    }),
  },
}),
new DIDResolverPlugin({
  ...peerDidResolver(),
  ...webDidResolver(),
  // THIS IS NOT WORKING (see error logs on screenshot 2 and 3)
  ...ethrDidResolver({ infuraProjectId: INFURA_PROJECT_ID }),
}),
// ...

After encountering the error message Cannot read property 'getResolver' of undefined, I am not sure if I am using the ethrDidResolver correctly. Can someone help me understand why it is not working?

Full Code

setup.ts

// imports:
// Core interfaces
import { createAgent, IDataStore, IDataStoreORM, IDIDManager, IKeyManager } from "@veramo/core";

// Core identity manager plugin. This allows you to create and manage DIDs by orchestrating different DID provider packages.
// This implements `IDIDManager`
import { DIDManager } from "@veramo/did-manager";

// Core key manager plugin. DIDs use keys and this key manager is required to know how to work with them.
// This implements `IKeyManager`
import { KeyManager } from "@veramo/key-manager";

// This plugin allows us to create and manage `did:peer` DIDs (used by DIDManager)
import { PeerDIDProvider } from "@veramo/did-provider-peer";

// A key management system that uses a local database to store keys (used by KeyManager)
import { KeyManagementSystem, SecretBox } from "@veramo/kms-local";

// Storage plugin using TypeORM to link to a database
import { Entities, KeyStore, DIDStore, migrations, PrivateKeyStore } from "@veramo/data-store";

// TypeORM is installed with '@veramo/data-store'
import { DataSource } from "typeorm";

// Core interfaces
import { IResolver } from "@veramo/core";

// Core DID resolver plugin. This plugin orchestrates different DID resolver drivers to resolve the corresponding DID Documents for the given DIDs.
// This plugin implements `IResolver`
import { DIDResolverPlugin } from "@veramo/did-resolver";

// the did:peer resolver package
import { getResolver as peerDidResolver } from "@veramo/did-provider-peer";
// the did:web resolver package
import { getResolver as webDidResolver } from "web-did-resolver";

// This plugin allows us to issue and verify W3C Verifiable Credentials with JWT proof format
import { CredentialPlugin, ICredentialIssuer, ICredentialVerifier } from "@veramo/credential-w3c";

// CUSTOM IMPORTS
import { EthrDIDProvider } from "@veramo/did-provider-ethr";
import { getResolver as ethrDidResolver } from "ethr-did-resolver";

// CONSTANTS

// This is a raw X25519 private key, provided as an example.
// You can run `npx @veramo/cli config create-secret-key` in a terminal to generate a new key.
// In a production app, this MUST NOT be hardcoded in your source code.
const DB_ENCRYPTION_KEY = "29739248cad1bd1a0fc4d9b75cd4d2990de535baf5caadfdf8d8f86664aa830c";
const INFURA_PROJECT_ID = "<I DON'T WANT TO LEAK MY INFURA PROJECT ID>";

// DB setup:
let dbConnection = new DataSource({
  type: "expo",
  driver: require("expo-sqlite"),
  database: "veramo.sqlite",
  migrations: migrations,
  migrationsRun: true,
  logging: ["error", "info", "warn"],
  entities: Entities,
}).initialize();

// Veramo agent setup
export const agent = createAgent<
  IDIDManager & IKeyManager & IDataStore & IDataStoreORM & IResolver & ICredentialIssuer & ICredentialVerifier
>({
  plugins: [
    new KeyManager({
      store: new KeyStore(dbConnection),
      kms: {
        local: new KeyManagementSystem(new PrivateKeyStore(dbConnection, new SecretBox(DB_ENCRYPTION_KEY))),
      },
    }),
    new DIDManager({
      store: new DIDStore(dbConnection),
      defaultProvider: "did:ethr:sepolia",
      providers: {
        "did:ethr:sepolia": new EthrDIDProvider({
          defaultKms: "local",
          network: "sepolia",
          rpcUrl: "https://sepolia.infura.io/v3/" + INFURA_PROJECT_ID,
        }),
        "did:peer": new PeerDIDProvider({
          defaultKms: "local",
        }),
      },
    }),
    new DIDResolverPlugin({
      ...peerDidResolver(),
      ...webDidResolver(),
      // THIS IS NOT WORKING
      ...ethrDidResolver({ infuraProjectId: INFURA_PROJECT_ID }),
    }),
    new CredentialPlugin(),
  ],
});

package.json

{
  "name": "veramomobile",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start --offline",
    "android": "expo run:android",
    "ios": "expo run:ios",
    "web": "expo start --web"
  },
  "dependencies": {
    "@ethersproject/shims": "^5.7.0",
    "@sinonjs/text-encoding": "^0.7.2",
    "@veramo/core": "^6.0.0",
    "@veramo/credential-w3c": "^6.0.0",
    "@veramo/data-store": "^6.0.0",
    "@veramo/did-manager": "^6.0.0",
    "@veramo/did-provider-ethr": "^6.0.0",
    "@veramo/did-provider-peer": "^6.0.0",
    "@veramo/did-resolver": "^6.0.0",
    "@veramo/key-manager": "^6.0.0",
    "@veramo/kms-local": "^6.0.0",
    "cross-fetch": "^4.0.0",
    "crypto-browserify": "^3.12.0",
    "ethr-did-resolver": "^10.1.5",
    "expo": "~49.0.6",
    "expo-sqlite": "~11.3.3",
    "expo-status-bar": "~1.6.0",
    "react": "18.2.0",
    "react-native": "0.72.10",
    "react-native-get-random-values": "~1.9.0",
    "stream-browserify": "^3.0.0",
    "web-did-resolver": "^2.0.27",
    "expo-splash-screen": "~0.20.5"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/plugin-syntax-import-assertions": "^7.24.1",
    "@types/react": "~18.2.14",
    "babel-plugin-rewrite-require": "^1.14.5",
    "babel-plugin-transform-typescript-metadata": "^0.3.2",
    "typescript": "^5.1.3"
  },
  "private": true
}

Screenshots

IMG_1463
IMG_1464
IMG_1465

@mirceanis
Copy link
Contributor

It's a known issue in ethr-did-resolver (decentralized-identity/ethr-did-resolver#186) it's caused by the build process which tries to produce dual ESM/CommonJS artifacts, but then these dual builds are failing with certain setups.

PRs are welcome if you have bandwidth to investigate and propose a fix

@pemmenegger pemmenegger changed the title How does "ethr-did-resolver" work with react native? ethr-did-resolver: Cannot read property 'getResolver' of undefined Apr 10, 2024
@dpzj
Copy link

dpzj commented Jun 26, 2024

Is there a solution now?

@pemmenegger
Copy link
Author

Workaround: I solved it by establishing an Express.js API that depends on the package ethr-did-resolver and returns the resolved DID document via an API endpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants