decentralized-identity / ethr-did-resolver

DID resolver for Ethereum Addresses with support for key management

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failure in creating contract instance in EthrDidController constructor

royce777 opened this issue · comments

this.contract = getContractForNetwork({ name: net, provider: prov, registry, rpcUrl })

import Resolver from 'did-resolver'
import getResolver from 'ethr-did-resolver'
import {EthrDID} from 'ethr-did'
import Web3 from 'web3'


//setup the provider 
Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send
console.log('Connecting to provider...');
const provider = new Web3.providers.HttpProvider('http://localhost:9545');
const web3 = new Web3(provider);


console.log('Connected to the provider');
//contract address of the registry
const RegAddress = '0x1482aDFDC2A33983EE69F9F8e4F852c467688Ea0';

//function where the creation of an identity will be tested
const test = async (accounts) => {
	const keyPair = EthrDID.createKeyPair();
	const did0 = createDid(RegAddress, keyPair);
	const ethrDidResolver = getResolver.getResolver(
		{
			name: 'test01',
			rpcUrl: 'http://localhost:9545',
			registry: RegAddress
		}
	);
	//create signing delegate valid for 1 day
	//await did0.createSigningDelegate();
	const didResolver = new Resolver.Resolver(ethrDidResolver);
	didResolver.resolve("did:ethr:test01:" + did0.address, ethrDidResolver).then((doc) => {
		console.log(doc);
		console.log(doc.didDocument.verificationMethod);
	});

}

//function to create and return the object used to manage a DID
const createDid = (RegAddress, keyPair) => {
	const conf = { 
		...keyPair, 
		registry: RegAddress,
		chainNameOrId : 1337,
		provider //  NOTE :  if i don't pass any provider here, it works, but provider is necessary for my work
	};
	const ethrDid = new EthrDID(conf);
	return ethrDid;
}

//actual function that starts executing and this will invoke all the other pieces of code

web3.eth.getAccounts().then((accounts) => { 
	test(accounts).catch(error => console.log(error));
});

causes the following error :

Connecting to provider...
Connected to the provider
Error: invalid signer or provider (argument="signerOrProvider", value={"withCredentials":false,"timeout":0,"connected":true,"host":"http://localhost:9545","httpAgent":{"_events":{},"_eventsCount":2,"defaultPort":80,"protocol":"http:","options":{"keepAlive":true,"path":null},"requests":{},"sockets":{},"freeSockets":{"localhost:9545:":[{"connecting":false,"_hadError":false,"_parent":null,"_host":"localhost","_readableState":{"objectMode":false,"highWaterMark":16384,"buffer":{"head":null,"tail":null,"length":0},"length":0,"pipes":[],"flowing":true,"ended":false,"endEmitted":false,"reading":true,"sync":false,"needReadable":true,"emittedReadable":false,"readableListening":false,"resumeScheduled":false,"errorEmitted":false,"emitClose":false,"autoDestroy":false,"destroyed":false,"errored":null,"closed":false,"closeEmitted":false,"defaultEncoding":"utf8","awaitDrainWriters":null,"multiAwaitDrain":false,"readingMore":false,"decoder":null,"encoding":null},"_events":{},"_eventsCount":6,"_writableState":{"objectMode":false,"highWaterMark":16384,"finalCalled":false,"needDrain":false,"ending":false,"ended":false,"finished":false,"destroyed":false,"decodeStrings":false,"defaultEncoding":"utf8","length":0,"writing":false,"corked":0,"sync":false,"bufferProcessing":false,"writecb":null,"writelen":0,"afterWriteTickInfo":null,"buffered":[],"bufferedIndex":0,"allBuffers":true,"allNoop":true,"pendingcb":0,"prefinished":false,"errorEmitted":false,"emitClose":false,"autoDestroy":false,"errored":null,"closed":false,"closeEmitted":false,"writable":true},"allowHalfOpen":false,"_sockname":null,"_pendingData":null,"_pendingEncoding":"","server":null,"_server":null,"parser":null,"_httpMessage":null,"timeout":0}]},"keepAliveMsecs":1000,"keepAlive":true,"maxSockets":null,"maxFreeSockets":256,"scheduling":"fifo","maxTotalSockets":null,"totalSocketCount":0}}, code=INVALID_ARGUMENT, version=contracts/5.1.1)
    at Logger.makeError (/home/ruslan/coding/study-ethr-did/newDid/node_modules/@ethersproject/logger/lib/index.js:180:21)
    at Logger.throwError (/home/ruslan/coding/study-ethr-did/newDid/node_modules/@ethersproject/logger/lib/index.js:189:20)
    at Logger.throwArgumentError (/home/ruslan/coding/study-ethr-did/newDid/node_modules/@ethersproject/logger/lib/index.js:192:21)
    at Contract.BaseContract (/home/ruslan/coding/study-ethr-did/newDid/node_modules/@ethersproject/contracts/lib/index.js:598:20)
    at new Contract (/home/ruslan/coding/study-ethr-did/newDid/node_modules/@ethersproject/contracts/lib/index.js:1017:42)
    at Contract.BaseContract.connect (/home/ruslan/coding/study-ethr-did/newDid/node_modules/@ethersproject/contracts/lib/index.js:786:24)
    at P (/home/ruslan/coding/study-ethr-did/newDid/node_modules/ethr-did-resolver/lib/index.umd.js:1:3285)
    at new K (/home/ruslan/coding/study-ethr-did/newDid/node_modules/ethr-did-resolver/lib/index.umd.js:1:3985)
    at new o (/home/ruslan/coding/study-ethr-did/newDid/node_modules/ethr-did/lib/index.js:1:1618)
    at createDid (file:///home/ruslan/coding/study-ethr-did/newDid/create_identity.js:47:18)
    at test (file:///home/ruslan/coding/study-ethr-did/newDid/create_identity.js:21:15)
    at file:///home/ruslan/coding/study-ethr-did/newDid/create_identity.js:54:2
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  reason: 'invalid signer or provider',
  code: 'INVALID_ARGUMENT',
  argument: 'signerOrProvider',
  value: HttpProvider {
    withCredentials: false,
    timeout: 0,
    headers: undefined,
    agent: undefined,
    connected: true,
    host: 'http://localhost:9545',
    httpAgent: Agent {
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      defaultPort: 80,
      protocol: 'http:',
      options: [Object],
      requests: {},
      sockets: {},
      freeSockets: [Object],
      keepAliveMsecs: 1000,
      keepAlive: true,
      maxSockets: Infinity,
      maxFreeSockets: 256,
      scheduling: 'fifo',
      maxTotalSockets: Infinity,
      totalSocketCount: 0,
      [Symbol(kCapture)]: false
    }
  }
}

All of the packages referenced are updated to the last version, using node v14.16.1 (npm v6.14.12).
Following along with the files mentioned in the error, it seems like the Provider.isProvider() function from "@ethersproject/abstract-provider" returns false on the web3 provider being passed, causing the error, at this point I guess :

line 629 of @ethersproject/contracts/src.ts/index.ts :
    } else if (Provider.isProvider(signerOrProvider)) {

Hope my description was clear enough

EDIT :
Using the following as a provider, seems to have solved the issue, however it is quite strange :

import {ethers} from 'ethers'
const provider = new ethers.providers.JsonRpcProvider('http://localhost:9545');

@royce777 this library, uses the ethers.js stack, since the latest major version.

Since you seem to be approaching this from the web3 angle, consider using Web3Provider from ethers.js to wrap your provider.
Also, please take a look at https://docs.ethers.io/v5/migration/web3/#migration-from-web3-js

You seem to also have resolved your own issue by using JsonRpcProvider, which is the correct approach if you are connecting through JSON-RPC.
As you probably know already, you can also use rpcUrl directly in the config if you're not manipulating the provider properties.

Please close this issue if this solution works, otherwise please add more details about your use-case.