Merge pull request #464 from embark-framework/metaMaskCheck

Meta mask check
This commit is contained in:
Iuri Matias 2018-05-28 14:37:58 -04:00 committed by GitHub
commit 836fbfbc47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 11 deletions

View File

@ -27,6 +27,7 @@ class CodeGenerator {
this.storageConfig = options.storageConfig || {}; this.storageConfig = options.storageConfig || {};
this.communicationConfig = options.communicationConfig || {}; this.communicationConfig = options.communicationConfig || {};
this.namesystemConfig = options.namesystemConfig || {}; this.namesystemConfig = options.namesystemConfig || {};
this.env = options.env || 'development';
// TODO: this should also be removed and use events instead // TODO: this should also be removed and use events instead
this.contractsManager = options.contractsManager; this.contractsManager = options.contractsManager;
this.plugins = options.plugins; this.plugins = options.plugins;
@ -159,7 +160,11 @@ class CodeGenerator {
web3Load = Templates.define_web3_simple({url: connection, done: 'done();'}); web3Load = Templates.define_web3_simple({url: connection, done: 'done();'});
} else { } else {
let connectionList = "[" + this.contractsConfig.dappConnection.map((x) => '"' + x + '"').join(',') + "]"; let connectionList = "[" + this.contractsConfig.dappConnection.map((x) => '"' + x + '"').join(',') + "]";
web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done();'}); if (self.env === 'development') {
web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done();', warnAboutMetamask: true});
} else {
web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done();'});
}
} }
result += Templates.do_when_loaded({block: web3Load}); result += Templates.do_when_loaded({block: web3Load});

View File

@ -26,6 +26,11 @@ __reduce(<%- connectionList %>,function(prev, value, next) {
}, function(err, _result) { }, function(err, _result) {
__getAccounts(function(err, accounts) { __getAccounts(function(err, accounts) {
web3.eth.defaultAccount = accounts[0]; web3.eth.defaultAccount = accounts[0];
<% if (warnAboutMetamask) { %>
if (web3.eth.currentProvider.isMetaMask) {
console.log("Note: Embark has detected you are in the development environment and using Metamask, please make sure Metamask is connected to your local node");
}
<% } %>
<%- done %> <%- done %>
}); });
}); });

View File

@ -175,7 +175,8 @@ class Engine {
storageConfig: self.config.storageConfig, storageConfig: self.config.storageConfig,
namesystemConfig: self.config.namesystemConfig, namesystemConfig: self.config.namesystemConfig,
communicationConfig: self.config.communicationConfig, communicationConfig: self.config.communicationConfig,
events: self.events events: self.events,
env: self.env
}); });
this.codeGenerator.listenToCommands(); this.codeGenerator.listenToCommands();

View File

@ -10,15 +10,6 @@ function replaceCRLF(string) {
// and check the generate code interacts as expected // and check the generate code interacts as expected
describe('embark.CodeGenerator', function() { describe('embark.CodeGenerator', function() {
this.timeout(0); this.timeout(0);
describe('#generateProvider', function() {
let generator = new CodeGenerator({contractsConfig: {"dappConnection": [ "$WEB3", "http://somehost:1234" ] }, contractsManager: {}});
it('should generate code to connect to a provider', function() {
var providerCode = 'function __reduce(arr, memo, iteratee, cb) {\n if (typeof cb !== \'function\') {\n if (typeof memo === \'function\' && typeof iteratee === \'function\') {\n cb = iteratee;\n iteratee = memo;\n memo = [];\n } else {\n throw new TypeError(\'expected callback to be a function\');\n }\n }\n\n if (!Array.isArray(arr)) {\n cb(new TypeError(\'expected an array\'));\n return;\n }\n\n if (typeof iteratee !== \'function\') {\n cb(new TypeError(\'expected iteratee to be a function\'));\n return;\n }\n\n (function next(i, acc) {\n if (i === arr.length) {\n cb(null, acc);\n return;\n }\n\n iteratee(acc, arr[i], function(err, val) {\n if (err) {\n cb(err);\n return;\n }\n next(i + 1, val);\n });\n })(0, memo);\n};\n\nfunction __isNewWeb3_1() {\n return (typeof(web3.version) === \"string\");\n};\n\nfunction __getAccounts(cb) {\n if (__isNewWeb3_1()) {\n web3.eth.getAccounts().then(function(accounts) {\n cb(null, accounts);\n return null;\n }).catch(function(err) {\n cb(err);\n return null;\n });\n return;\n }\n web3.eth.getAccounts(cb);\n};\n\nvar __mainContext = __mainContext || this;\n__mainContext.__LoadManager = function() { this.list = []; this.done = false; }\n__mainContext.__LoadManager.prototype.execWhenReady = function(cb) { if (this.done) { cb(); } else { this.list.push(cb) } }\n__mainContext.__LoadManager.prototype.doFirst = function(todo) { var self = this; todo(function() { self.done = true; self.list.map((x) => x.apply()) }) }\n__mainContext.__loadManagerInstance = new __mainContext.__LoadManager();\nvar whenEnvIsLoaded = function(cb) {\n if (typeof document !== \'undefined\' && document !== null && !/comp|inter|loaded/.test(document.readyState)) {\n document.addEventListener(\'DOMContentLoaded\', cb);\n } else {\n cb();\n }\n}\nwhenEnvIsLoaded(function(){\n __mainContext.__loadManagerInstance.doFirst(function(done) {\n __mainContext.web3 = undefined;\n__reduce([\"$WEB3\",\"http://somehost:1234\"],function(prev, value, next) {\n if (prev === false) {\n return next(null, false);\n }\n\n if (value === \'$WEB3\' && (typeof web3 !== \'undefined\' && typeof Web3 !== \'undefined\')) {\n web3.setProvider(web3.givenProvider);\n } else if (value !== \'$WEB3\' && (typeof Web3 !== \'undefined\' && ((typeof web3 === \'undefined\') || (typeof web3 !== \'undefined\' && (!web3.isConnected || (web3.isConnected && !web3.isConnected())))))) {\n if (value.indexOf(\'ws://\') >= 0) {\n web3.setProvider(new Web3.providers.WebsocketProvider(value));\n } else {\n web3.setProvider(new Web3.providers.HttpProvider(value));\n }\n } else if (value === \'$WEB3\') {\n return next(null, \'\');\n }\n\n __getAccounts(function(err, account) {\n if (err) {\n next(null, true)\n } else {\n next(null, false)\n }\n });\n}, function(err, _result) {\n __getAccounts(function(err, accounts) {\n web3.eth.defaultAccount = accounts[0];\n done();\n });\n});\n\n })\n});\n\n'
assert.strictEqual(replaceCRLF(generator.generateProvider()), providerCode);
});
});
describe('#generateContracts', function() { describe('#generateContracts', function() {
let generator = new CodeGenerator({blockchainConfig: {}, contractsManager: { let generator = new CodeGenerator({blockchainConfig: {}, contractsManager: {