uport-project / ethr-did

Create ethr DIDs
Apache License 2.0
259 stars 53 forks source link

Unable to instantiate EthrDID #73

Closed gobberr closed 3 years ago

gobberr commented 3 years ago

I'm trying to instantiate EthrDID in the following way:

const providerConfig = { rpcUrl: 'https://rinkeby.infura.io/ethr-did' }
const issuer: Issuer = new EthrDID({
  identifier: identity.did, // my did
  privateKey: identity.privateKey, // my private key
  providerConfig  
})

I get this error:

error TS2351: This expression is not constructable.
Type 'typeof import("/app/node_modules/ethr-did/lib/index")' has no construct signatures.

I'm using "ethr-did": "^2.1.4" on package.json

Maybe I'm misconfiguring the providerConfig parameter? Thanks and best regards

mirceanis commented 3 years ago

You can use the rpcUrl parameter directly in the arguments object for the constructor:

const issuer: Issuer = new EthrDID({ identifier, privateKey, rpcUrl })

If you need to bundle the providerConfig as an object, then you can use the spread operator: ...providerConfig

const providerConfig = { rpcUrl: 'https://rinkeby.infura.io/<please get an infura project ID>' }
const issuer: Issuer = new EthrDID({
  identifier,
  privateKey,
  ...providerConfig  
})

This section of the readme details all the parameters you can configure.

If you intend to run on rinkeby, it's wise to also specify the chainNameOrId parameter:

const providerConfig = { }
const issuer: Issuer = new EthrDID({
  // ...
  rpcUrl: 'https://rinkeby.infura.io/<please get an infura project ID>',
  chainNameOrId: 'rinkeby'
})

NOTE The RPC URL you are using (https://rinkeby.infura.io/ethr-did) no longer works, please get a free infura project ID or use an alternative provider. You can also specify an ethers.js provider directly, like so:

const issuer: Issuer = new EthrDID({
  // ...
  provider: new InfuraProvider('rinkeby', <infura project ID>),
  chainNameOrId: 'rinkeby'
})

See InfuraProvider or AlchemyProvider

Closing this. Please reopen if the issue is not resolved

gobberr commented 3 years ago

Thanks for the answer.

I'm giving as input to the EthrDID constructor an object conforming to the IConfig interface, but I still get the error reported in the screenshot below.

Schermata 2021-06-03 alle 10 25 33

I can't figure out how I can fix it

mirceanis commented 3 years ago

Does this error only appear in VSCode or is it breaking your build? This may be obvious already but have you tried a clean build? (clear node_modules, yarn cache clean, reimport the project in vscode, etc..)

If it is breaking your build, can you isolate the error in a small project that we could look at?

gobberr commented 3 years ago

The build breaks and yes I also have already tried to clean up my development environment

You can have a look here, running the file run.ts with the command ts-node run.ts

That's my situation

Schermata 2021-06-03 alle 11 48 43
mirceanis commented 3 years ago

I think I see what's happening now, after you shared your project. the import statement should be import { EthrDID } from "ethr-did"; instead of import EthrDID from "ethr-did";

You seem to have stumbled upon a bug in the docs that are somewhat outdated now.

Here's a sample, based on your project, that works with ts-node:

import { EthrDID } from "ethr-did";
import { Issuer } from "did-jwt-vc";

const issuer: Issuer = new EthrDID({
  identifier: "did:ethr:0xb9c5714089478a327f09197987f16f9e5d936e8a",
  rpcUrl: "https://rinkeby.infura.io/v3/8a581af7416b4e7681d1f871b6945281",
  chainNameOrId: "rinkeby",
}) as Issuer;

console.log(issuer);
gobberr commented 3 years ago

Now it's works, thanks again!

uport-automation-bot commented 2 years ago

:tada: This issue has been resolved in version 2.1.5 :tada:

The release is available on:

Your semantic-release bot :package::rocket: