2019-07-12 15:52:23 +00:00
|
|
|
const { __ } = require("embark-i18n");
|
2019-05-16 10:03:19 +00:00
|
|
|
const { dappPath, embarkPath } = require('embark-utils');
|
2019-02-11 15:35:28 +00:00
|
|
|
const path = require('path');
|
|
|
|
|
2019-07-11 23:00:25 +00:00
|
|
|
// TODO: embarkjs artifacts etc.. will need to be moved here
|
|
|
|
|
2019-02-25 13:55:47 +00:00
|
|
|
class EmbarkJSConnectorWeb3 {
|
2019-02-22 19:05:57 +00:00
|
|
|
constructor(embark, _options) {
|
|
|
|
this.embark = embark;
|
|
|
|
this.events = embark.events;
|
|
|
|
this.fs = embark.fs;
|
|
|
|
this.config = embark.config;
|
|
|
|
this.constants = embark.constants;
|
2019-07-11 23:00:25 +00:00
|
|
|
|
|
|
|
// this.registerProvider();
|
|
|
|
// TODO: will need to be changed to an action but that requires changes in the blockchain launch process
|
|
|
|
this.events.on("blockchain:ready", this.executeEmbarkJSBlockchain.bind(this));
|
2019-07-12 15:52:23 +00:00
|
|
|
this.events.on("blockchain:ready", this.registerWeb3Help.bind(this));
|
2019-02-22 19:05:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:00:25 +00:00
|
|
|
async executeEmbarkJSBlockchain() {
|
|
|
|
let code = "";
|
|
|
|
const connectorCode = this.fs.readFileSync(path.join(__dirname, 'embarkJSConnectorWeb3.js'), 'utf8');
|
|
|
|
code += connectorCode;
|
|
|
|
|
|
|
|
code += "\nEmbarkJS.Blockchain.registerProvider('web3', embarkJSConnectorWeb3);";
|
|
|
|
// code += "\nEmbarkJS.Blockchain.setProvider('web3', {});";
|
2019-02-22 19:05:57 +00:00
|
|
|
|
2019-07-11 23:00:25 +00:00
|
|
|
code += "\nEmbarkJS.Blockchain.setProvider('web3', {web3});";
|
2019-02-22 19:05:57 +00:00
|
|
|
|
2019-07-11 23:00:25 +00:00
|
|
|
this.events.request('runcode:eval', code, (err) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
2019-02-12 16:31:05 +00:00
|
|
|
}
|
2019-02-12 13:58:09 +00:00
|
|
|
});
|
2019-07-11 23:00:25 +00:00
|
|
|
}
|
2019-02-12 13:58:09 +00:00
|
|
|
|
2019-07-12 15:52:23 +00:00
|
|
|
registerWeb3Help() {
|
|
|
|
this.events.request('console:register:helpCmd', {
|
|
|
|
cmdName: "web3",
|
|
|
|
cmdHelp: __("instantiated web3.js object configured to the current environment")
|
|
|
|
}, () => { })
|
|
|
|
}
|
|
|
|
|
2019-07-11 23:00:25 +00:00
|
|
|
// ===============
|
|
|
|
// ===============
|
|
|
|
// ===============
|
2019-02-20 16:19:39 +00:00
|
|
|
|
2019-07-11 23:00:25 +00:00
|
|
|
async registerProvider() {
|
|
|
|
let web3Location = (await this.getWeb3Location()).replace(/\\/g, '/');
|
2019-02-12 13:58:09 +00:00
|
|
|
|
2019-02-22 19:05:57 +00:00
|
|
|
await this.registerVar('__Web3', require(web3Location));
|
2019-02-12 13:58:09 +00:00
|
|
|
|
2019-02-22 19:05:57 +00:00
|
|
|
const symlinkLocation = await this.generateSymlink(web3Location);
|
2019-02-12 13:58:09 +00:00
|
|
|
|
2019-02-20 16:19:39 +00:00
|
|
|
let code = `\nconst Web3 = global.__Web3 || require('${symlinkLocation}');`;
|
|
|
|
code += `\nglobal.Web3 = Web3;`;
|
2019-02-12 13:58:09 +00:00
|
|
|
|
2019-02-25 13:55:47 +00:00
|
|
|
const connectorCode = this.fs.readFileSync(path.join(__dirname, 'embarkJSConnectorWeb3.js'), 'utf8');
|
2019-02-20 16:19:39 +00:00
|
|
|
code += connectorCode;
|
2019-02-12 13:58:09 +00:00
|
|
|
|
2019-02-25 13:55:47 +00:00
|
|
|
code += "\nEmbarkJS.Blockchain.registerProvider('web3', embarkJSConnectorWeb3);";
|
2019-02-20 16:19:39 +00:00
|
|
|
code += "\nEmbarkJS.Blockchain.setProvider('web3', {});";
|
2019-02-12 16:31:05 +00:00
|
|
|
|
2019-05-15 08:09:50 +00:00
|
|
|
const configPath = dappPath(this.config.embarkConfig.generationDir, this.constants.dappArtifacts.dir, this.constants.dappArtifacts.blockchain).replace(/\\/g, '/');
|
2019-02-22 19:05:57 +00:00
|
|
|
|
|
|
|
code += `\nif (!global.__Web3) {`; // Only connect when in the Dapp
|
|
|
|
code += `\n const web3ConnectionConfig = require('${configPath}');`;
|
|
|
|
code += `\n EmbarkJS.Blockchain.connect(web3ConnectionConfig, (err) => {if (err) { console.error(err); } });`;
|
|
|
|
code += `\n}`;
|
|
|
|
|
|
|
|
this.embark.addCodeToEmbarkJS(code);
|
2019-02-12 16:31:05 +00:00
|
|
|
|
2019-02-20 16:19:39 +00:00
|
|
|
code = "EmbarkJS.Blockchain.setProvider('web3', {web3});";
|
2019-02-12 16:31:05 +00:00
|
|
|
|
2019-02-20 16:19:39 +00:00
|
|
|
const shouldInit = (_config) => {
|
|
|
|
return true;
|
|
|
|
};
|
2019-02-12 16:31:05 +00:00
|
|
|
|
2019-02-22 19:05:57 +00:00
|
|
|
this.embark.addConsoleProviderInit('blockchain', code, shouldInit);
|
|
|
|
}
|
|
|
|
|
|
|
|
getWeb3Location() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.events.request("version:get:web3", (web3Version) => {
|
|
|
|
if (web3Version === "1.0.0-beta") {
|
2019-05-15 08:09:50 +00:00
|
|
|
const nodePath = embarkPath('node_modules');
|
2019-02-22 19:05:57 +00:00
|
|
|
const web3Path = require.resolve("web3", {paths: [nodePath]});
|
|
|
|
return resolve(web3Path);
|
|
|
|
}
|
|
|
|
this.events.request("version:getPackageLocation", "web3", web3Version, (err, location) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
2019-05-15 08:09:50 +00:00
|
|
|
const locationPath = embarkPath(location);
|
2019-02-22 19:05:57 +00:00
|
|
|
resolve(locationPath);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
generateSymlink(location) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.events.request('code-generator:symlink:generate', location, 'web3', (err, symlinkDest) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(symlinkDest);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
registerVar(name, code) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
this.events.emit('runcode:register', name, code, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 13:55:47 +00:00
|
|
|
module.exports = EmbarkJSConnectorWeb3;
|