AlphaWallet / AlphaWallet-web3-provider

[Used in AlphaWallet]
MIT License
57 stars 37 forks source link

Add support to extract and use basic auth username and password from RPC URL #9

Closed hboon closed 4 years ago

hboon commented 4 years ago

Add support to extract and use basic auth username and password from RPC URL.

Replace:

engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(rpcUrl)))

with:

    let username, password;
    let start = rpcUrl.indexOf("://");
    if (start != -1) {
      start += 3;
      const end = rpcUrl.indexOf("@", start + 1);
      if (end != -1) {
        const credentials = rpcUrl.substring(start, end);
        let [u, p] = credentials.split(":");
        username = u;
        password = p;
      }
    }
    if (typeof username === 'undefined' || typeof password === 'undefined') {
        engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(rpcUrl)))
    } else {
        engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(rpcUrl,0,username,password)))
    }

And minify :)