[Archive] How to get the private key for the wallet address created by web3Auth

:classical_building: Archived Content from Web3Auth Community

This topic was originally posted by david.test.wdc on 6/18/2024.
This content has been migrated from our previous community forum to preserve valuable discussions.


Hello,
I have project where I used wagmi to connect with wallets.
Now I want to add google sign in as well.
Right now, I am able to connect with google via web3auth-wagmi-connector, but I wanted to know about how do I get the private key of the wallet that was created.

If anyone could help me with this it would be much appreciated.

  • SDK Version:
    @web3auth/base”: “^8.6.2”,
    @web3auth/ethereum-provider”: “^8.4.1”,
    @web3auth/no-modal”: “^8.6.2”,
    @web3auth/wallet-services-plugin”: “^8.4.1”,
    @web3auth/web3auth-wagmi-connector”: “^6.0.0”,
  • Platform: Web Application (Next JS)

hi @david.test.wdc

I hope you are ok today. Could you please check this example → GitHub - Web3Auth/web3auth-pnp-examples at feat/add-w3a-context-wagmi-examples ?

If you have any question please get back to me.

Thanks

Hi @TomTom ,
I went through the documentation and added a context wrapper for the Web3Auth Plug n Play No Modal.

export const walletConfig = createConfig({
  chains,
  transports: { 
    [base.id]: http()
  },
  connectors: [
    coinbaseWallet(),
    walletConnect({
      projectId
    }),
    Web3AuthConnector({
      loginParams: { loginProvider: "google" },
      web3AuthInstance
    }) as any

]
});

export const web3AuthInstance = new Web3AuthNoModal({
clientId:
‘My-ClientID’,
privateKeyProvider,
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET

});

const openloginAdapterInstance = new OpenloginAdapter({
adapterSettings: {
uxMode: UX_MODE.POPUP,
whiteLabel: {
appName: name,
logoLight: iconUrl,
logoDark: iconUrl,
defaultLanguage: “en”,
mode: “light” // whether to enable dark mode. defaultValue: false
}
}
});
web3AuthInstance.configureAdapter(openloginAdapterInstance);

export function WalletWrapper({ children }: any) {
return (
<Web3AuthContext.Provider value={web3AuthInstance}>
<WagmiProvider config={walletConfig}>
{" "}
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
</WagmiProvider>
</Web3AuthContext.Provider>
);
}

After doing so, I see the google login pop up opening and by logging in I see that the wallet is created and connected to wagmi.

I had two questions:

  1. To disconnect, can I use the wagmi’s built in disconnect function from the useDisconnect hook?
  2. How do i get the private key for the account that just got created?

Any update on this @TomTom ?

Hi @david.test.wdc,

Thanks for your patience in waiting for the answer.

  1. Yes, In the example we are using it.
  2. Please, refer to this line in the example.
    var userInfo = await web3Auth?.getUserInfo();
    web3auth-pnp-examples/web-modal-sdk/wagmi-examples/wagmi-modal-example/src/App.tsx at 4133a8968e66330b7c63f0c227e971c58a8d7d10 · Web3Auth/web3auth-pnp-examples · GitHub

You can utilize this line above to obtain the private key

const privateKey = await web3Auth?.provider.request({
                    method: "eth_private_key",
                  });

Please reach out to me on the forum if you have any further questions.

Thanks for that!
One issue I am getting now, is that after a successful disconnect, when I am trying to log back in.
I am getting this error. But it goes away when I refresh the page.

Screenshot from 2024-06-20 15-52-00
Screenshot from 2024-06-20 15-52-001334×225 53 KB

do you have a working github project where we can check for this error ?