ceramicnetwork / js-did

A simple interface to interact with DIDs that conform to the DID-provider interface.
Other
95 stars 28 forks source link

How to use did session with WalletConnect and wagmi hooks #149

Closed ghost closed 1 year ago

ghost commented 1 year ago

Description

I am trying to create a did session for users who are connected via walletconnect/ web3 modal. I have the user's account address and can use all hooks from wagmi.

But I am not able to create did session via signing the message.

Technical Information

Here's how I have it in code in my react app

const {address, isConnected} = useAccount();
const provider = useProvider({
  chainId: mainnet.id,
});

const handleDidSession = () => {
  const accountId = await getAccountId(provider, address);
  const authMethod = await EthereumWebAuth.getAuthMethod(
    provider,
    accountId
  );

  const oneWeek = 60 * 60 * 24 * 7 * 1;
  const session = await DIDSession.authorize(authMethod, {
    resources: [`ceramic://*`],
    expiresInSecs: oneHundredWeeks,
  });
}

If I replace provider with window.ethreum it works in browser, but I want to support other wallets and mobile devices, so I integrated wallet connect and web3 modal. Now I want to create didsession with this config.

Running the above code give me this error

Uncaught (in promise) Error: bad response (status=400, headers={"content-length":"74","content-type":"application/json"}, body="{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32600,\"message\":\"invalid json request\"}}", requestBody="{\"method\":{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"eth_chainId\",\"params\":[]},\"id\":51,\"jsonrpc\":\"2.0\"}", requestMethod="POST", url="https://rpc.walletconnect.com/v1/?chainId=eip155:1&projectId=8a26721xxxxxxxx", code=SERVER_ERROR, version=web/5.7.1)
    at Logger.makeError (index.js?accd:224:1)
    at Logger.throwError (index.js?accd:233:1)
    at eval (index.js?eae7:231:1)
    at Generator.next (<anonymous>)
    at fulfilled (index.js?eae7:5:43)
ap-atul commented 1 year ago

Solved! Needed to use provider from connector object. Here's the snippet, this works for injected connector as well as wallet connect via Qrcode, tested with mobile with metamask wallet.

const {address, isConnected, connector} = useAccount();

const handleDidSession = () => {
  const provider = await connector.getProvider();
  const accountId = await getAccountId(provider, address);
  const authMethod = await EthereumWebAuth.getAuthMethod(provider,accountId);
  const session = await DIDSession.authorize(authMethod, {
    resources: [`ceramic://*`],
    expiresInSecs: config.didSession.expiresInSecs,
  });
}
oed commented 1 year ago

Glad you it got resolved!