uport-project / ethr-did

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

EthrDID constructor failure #66

Closed royce777 closed 3 years ago

royce777 commented 3 years ago

Describe the bug When I try to create a new EthrDID object, passing a provider to the constructor, the procedure fails.

To Reproduce

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));
});

Errors

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
    }
  }
}

Desktop (please complete the following information):

Observations 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 :

    line 629 of @ethersproject/contracts/src.ts/index.ts :
    } else if (Provider.isProvider(signerOrProvider)) {
m-yahya commented 3 years ago

@royce777 Hi, how did you manage to solve the issue? It would be nice you can share the workaround to fix it. Thanks.

ottomorac commented 2 years ago

I think this is the code where this was resolved: https://github.com/royce777/study-ethr-did/blob/d38beea87374e2465b7957ee7ccd885be2a9281c/newDid/signTokens.js