Skip to content

Commit

Permalink
feat: adds support for swarm imports in Solidity
Browse files Browse the repository at this point in the history
This commit enables users of Embark to import any Solidity contract using
the swarm protocol.

Example:

```
import "bzz:/someensdomain.eth/ERC725.sol";
```

Closes #766
  • Loading branch information
vyomshm authored and 0x-r4bbit committed Nov 9, 2018
1 parent fb69710 commit 62607b0
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 78 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ With Embark you can:
* Manage different chains (e.g testnet, private net, livenet)
* Easily manage complex systems of interdependent contracts.

**Decentralized Storage (IPFS)**
**Decentralized Storage (IPFS, Swarm)**
* Easily Store & Retrieve Data on the DApp through EmbarkJS. Including uploading and retrieving files.
* Deploy the full application to IPFS or Swarm.
* Import and deploy contracts hosted on Swarm.


**Decentralized Communication (Whisper, Orbit)**
Expand All @@ -44,4 +45,3 @@ $ npm -g install embark
```

See [Complete Documentation](https://embark.status.im/docs/).

8 changes: 4 additions & 4 deletions lib/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var Config = function(options) {
this.context = options.context || [constants.contexts.any];
this.shownNoAccountConfigMsg = false; // flag to ensure "no account config" message is only displayed once to the user
this.corsParts = [];
this.ipfsUrl = null;
this.providerUrl = null;
this.events.setCommandHandler("config:cors:add", (url) => {
this.corsParts.push(url);
this._updateBlockchainCors();
Expand Down Expand Up @@ -320,15 +320,15 @@ Config.prototype.loadExternalContractsFiles = function() {
let contracts = this.contractsConfig.contracts;
let storageConfig = this.storageConfig;
if (storageConfig && storageConfig.upload && storageConfig.upload.getUrl) {
this.ipfsUrl = storageConfig.upload.getUrl;
this.providerUrl = storageConfig.upload.getUrl;
}
for (let contractName in contracts) {
let contract = contracts[contractName];
if (!contract.file) {
continue;
}
if (contract.file.startsWith('http') || contract.file.startsWith('git') || contract.file.startsWith('ipfs')) {
const fileObj = utils.getExternalContractUrl(contract.file,this.ipfsUrl);
if (contract.file.startsWith('http') || contract.file.startsWith('git') || contract.file.startsWith('ipfs') || contract.file.startsWith('bzz')) {
const fileObj = utils.getExternalContractUrl(contract.file,this.providerUrl);
if (!fileObj) {
return this.logger.error(__("HTTP contract file not found") + ": " + contract.file);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/core/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class File {
this.downloadedImports = false;
this.importRemappings = []; // mapping downloaded imports to local file
this.storageConfig = options.storageConfig;
this.ipfsUrl = null;
this.providerUrl = null;
}

parseFileForImport(content, isHttpContract, callback) {
Expand All @@ -36,10 +36,10 @@ class File {
let newContent = content;
let storageConfig = self.storageConfig;
if (storageConfig && storageConfig.upload && storageConfig.upload.getUrl) {
self.ipfsUrl = storageConfig.upload.getUrl;
self.providerUrl = storageConfig.upload.getUrl;
}
while ((matches = regex.exec(content))) {
const httpFileObj = utils.getExternalContractUrl(matches[1],self.ipfsUrl);
const httpFileObj = utils.getExternalContractUrl(matches[1],self.providerUrl);
const fileObj = {
fileRelativePath: path.join(path.dirname(self.filename), matches[1]),
url: `${pathWithoutFile}/${matches[1]}`
Expand Down
8 changes: 4 additions & 4 deletions lib/modules/solidity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Solidity {
this.useDashboard = options.useDashboard;
this.options = embark.config.embarkConfig.options.solc;
this.storageConfig = embark.config.storageConfig;
this.ipfsUrl = null;
this.providerUrl = null;

embark.registerCompiler(".sol", this.compile_solidity.bind(this));

Expand Down Expand Up @@ -71,12 +71,12 @@ class Solidity {
if (self.solcAlreadyLoaded) {
return callback();
}

let storageConfig = self.storageConfig;
if (storageConfig && storageConfig.upload && storageConfig.upload.getUrl) {
self.ipfsUrl = storageConfig.upload.getUrl;
self.providerUrl = storageConfig.upload.getUrl;
}
self.solcW = new SolcW(self.embark, {logger: self.logger, events: self.events, ipc: self.ipc, useDashboard: self.useDashboard, ipfsUrl: self.ipfsUrl});

self.solcW = new SolcW(self.embark, {logger: self.logger, events: self.events, ipc: self.ipc, useDashboard: self.useDashboard, providerUrl: self.providerUrl});

self.logger.info(__("loading solc compiler") + "..");
self.solcW.load_compiler(function (err) {
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/solidity/solcP.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class SolcProcess extends ProcessWrapper {
super({pingParent: false});
this._logger = options.logger;
this._showSpinner = options.showSpinner === true;
this._ipfsUrl = options.ipfsUrl;
this._providerUrl = options.providerUrl;
}

findImports(filename) {
if (filename.startsWith('http') || filename.startsWith('git')) {
const fileObj = Utils.getExternalContractUrl(filename,this._ipfsUrl);
const fileObj = Utils.getExternalContractUrl(filename, this._providerUrl);
filename = fileObj.filePath;
}
if (fs.existsSync(filename)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/solidity/solcW.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SolcW {
this.compilerLoaded = false;
this.solcProcess = null;
this.useDashboard = options.useDashboard;
this.ipfsUrl = options.ipfsUrl;
this.providerUrl = options.providerUrl;
}

load_compiler(done) {
Expand Down Expand Up @@ -46,7 +46,7 @@ class SolcW {
modulePath: utils.joinPath(__dirname, 'solcP.js'),
logger: self.logger,
events: self.events,
ipfsUrl: self.ipfsUrl,
providerUrl: self.providerUrl,
silent: false
});

Expand Down
22 changes: 19 additions & 3 deletions lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,12 @@ function proposeAlternative(word, _dictionary, _exceptions) {
return propose(word, dictionary, {threshold: 0.3});
}

function getExternalContractUrl(file,ipfsUrl) {
function getExternalContractUrl(file,providerUrl) {
const constants = require('../constants');
let url;
const RAW_URL = 'https://raw.githubusercontent.com/';
const DEFAULT_SWARM_GATEWAY = 'https://swarm-gateways.net/';
const MALFORMED_SWARM_ERROR = 'Malformed Swarm gateway URL for ';
const MALFORMED_ERROR = 'Malformed Github URL for ';
const MALFORMED_IPFS_ERROR = 'Malformed IPFS URL for ';
const IPFS_GETURL_NOTAVAILABLE = 'IPFS getUrl is not available. Please set it in your storage config. For more info: https://embark.status.im/docs/storage_configuration.html';
Expand All @@ -239,7 +241,7 @@ function getExternalContractUrl(file,ipfsUrl) {
}
url = `${RAW_URL}${match[1].replace('blob/', '')}`;
} else if (file.startsWith('ipfs')) {
if(!ipfsUrl) {
if(!providerUrl) {
console.error(IPFS_GETURL_NOTAVAILABLE);
return null;
}
Expand All @@ -255,7 +257,7 @@ function getExternalContractUrl(file,ipfsUrl) {
if(match[2]) {
matchResult += '/' + match[2];
}
url = `${ipfsUrl}${matchResult}`;
url = `${providerUrl}${matchResult}`;
return {
url,
filePath: constants.httpContractsDirectory + matchResult
Expand All @@ -282,6 +284,20 @@ function getExternalContractUrl(file,ipfsUrl) {
url = `${RAW_URL}${match[2]}/${match[3]}/${branch}/${match[4]}`;
} else if (file.startsWith('http')) {
url = file;
} else if(file.startsWith('bzz')){
if(!providerUrl) {
url = DEFAULT_SWARM_GATEWAY + file;
} else {
let match = file.match(/bzz:\/([-a-zA-Z0-9]+)\/(.*)/);
if(!match){
match = file.match(/bzz:\/([-a-zA-Z0-9]+)/);
if(!match){
console.log(MALFORMED_SWARM_ERROR + file);
return null;
}
}
url = providerUrl + '/' + file;
}
} else {
return null;
}
Expand Down
Loading

0 comments on commit 62607b0

Please sign in to comment.