poa-dapps-keys-generation/src/getWeb3.js

87 lines
2.9 KiB
JavaScript
Raw Normal View History

import { constants } from "./constants";
2017-12-06 23:02:38 -08:00
let errorMsgNoMetamaskAccount = `You haven't chosen any account in MetaMask.
2018-02-23 05:13:37 -08:00
Please choose your initial key in MetaMask and reload the page.
Check POA Network <a href='https://github.com/poanetwork/wiki' target='blank'>wiki</a> for more info.`;
2017-12-06 23:02:38 -08:00
2018-10-15 07:52:03 -07:00
function generateElement(msg) {
2017-12-06 23:02:38 -08:00
let errorNode = document.createElement("div");
2018-02-23 05:13:37 -08:00
errorNode.innerHTML = `<div style="line-height: 1.6;">
2017-12-06 23:02:38 -08:00
${msg}
</div>`;
return errorNode;
}
let getWeb3 = () => {
2018-10-15 07:52:03 -07:00
return new Promise(function(resolve, reject) {
2017-12-06 23:02:38 -08:00
// Wait for loading completion to avoid race conditions with web3 injection timing.
2018-10-15 07:52:03 -07:00
window.addEventListener("load", function() {
var results;
var web3 = window.web3;
2017-12-06 23:02:38 -08:00
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
2018-10-15 07:52:03 -07:00
if (typeof web3 !== "undefined") {
2017-12-06 23:02:38 -08:00
// Use Mist/MetaMask's provider.
var errorMsg = null;
2018-10-15 07:52:03 -07:00
web3 = new window.Web3(web3.currentProvider);
2017-12-06 23:02:38 -08:00
web3.version.getNetwork((err, netId) => {
let netIdName;
switch (netId) {
case constants.NETID_SOKOL:
netIdName = "Sokol";
console.log("This is sokol");
2018-10-15 07:52:03 -07:00
break;
case constants.NETID_DAI_TEST:
netIdName = "Dai-Test";
console.log("This is Dai-Test");
2018-10-15 07:52:03 -07:00
break;
case constants.NETID_CORE:
netIdName = "Core";
console.log("This is Core");
2018-10-15 07:52:03 -07:00
break;
case constants.NETID_DAI:
netIdName = "Dai";
console.log("This is Dai");
2018-10-15 07:52:03 -07:00
break;
2017-12-06 23:02:38 -08:00
default:
netIdName = "Unknown";
errorMsg = `You aren't connected to POA Network.
2018-02-23 05:13:37 -08:00
Please switch on Metamask and refresh the page.
Check POA Network <a href='https://github.com/poanetwork/wiki' target='blank'>wiki</a> for more info.
<b>Current Network ID</b> is <i>${netId}</i>`;
console.log("This is an unknown network.", netId);
2017-12-06 23:02:38 -08:00
}
results = {
web3Instance: web3,
netIdName,
netId,
injectedWeb3: true
2018-10-15 07:52:03 -07:00
};
document.title = `${netIdName} - Dapp Keys Generation`;
2017-12-06 23:02:38 -08:00
var defaultAccount = web3.eth.defaultAccount || null;
2018-10-15 07:52:03 -07:00
if (defaultAccount === null) {
reject({
msg: errorMsgNoMetamaskAccount,
node: generateElement(errorMsgNoMetamaskAccount)
});
2017-12-06 23:02:38 -08:00
}
2018-10-15 07:52:03 -07:00
if (errorMsg !== null) {
reject({ msg: errorMsg, node: generateElement(errorMsg) });
2017-12-06 23:02:38 -08:00
}
2018-10-15 07:52:03 -07:00
resolve(results);
});
2017-12-06 23:02:38 -08:00
2018-10-15 07:52:03 -07:00
console.log("Injected web3 detected.");
2017-12-06 23:02:38 -08:00
} else {
2018-10-15 07:52:03 -07:00
reject({
msg: errorMsgNoMetamaskAccount,
node: generateElement(errorMsgNoMetamaskAccount)
});
console.error("Metamask not found");
2017-12-06 23:02:38 -08:00
}
2018-10-15 07:52:03 -07:00
});
});
};
2017-12-06 23:02:38 -08:00
2018-10-15 07:52:03 -07:00
export default getWeb3;