web3js – How to Implement WalletConnect with Vanilla JavaScript

walletconnectweb3js

I would like to implement many connectors in a vanilla javascript website. I found https://github.com/Web3Modal/web3modal-vanilla-js-example but the look & feel is not very customizable (and I would need to change it completely). Therefore, I am trying to implement WalletConnect from scratch but all the examples I found were for React or Node.js.
Thanks!

Best Answer

Yes - this is possible, but you need the full packaged suite of JS that it depends on. Unpkg makes this available, and you can use a Web3 wrapper so that it can be tidily manipulated in vanilla JS.

Try this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/web3/3.0.0-rc.5/web3.min.js"></script>
<script src="https://unpkg.com/@walletconnect/web3-provider@1.7.1/dist/umd/index.min.js"></script>
//Make sure the above loads first (either stick it in the head, or manage your load sequence)...

<script>
//An infura ID, or custom ETH node is required for Ethereum, for Binance Smart Chain you can just use their public endpoint
var provider = new WalletConnectProvider.default(
  { 
    infuraId: "<<YOUR INFURA ID>>", 
    rpc: {56: "https://bsc-dataseed.binance.org/"}
  });
//to set it to BSC, uncomment the following line
//provider.chainId = 56;
//present the Wallet Connect QR code
provider.enable().then(function(res){ 
  //get wallet addrs and then wrap this into the Web3 JS
  let web3 = new Web3(provider);
  //now do all the web3 stuff you want...
  //awesome web3 application goes here
});

EDIT: I created some small helper files to deal with this, and to also include other wallets. You can find them here

Related Topic