From 115193449b8acf6ae2c122f1fb09d519988a112e Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Wed, 23 Dec 2020 17:44:27 +0530 Subject: [PATCH] swarm import using web3-bzz --- libs/remix-url-resolver/src/resolve.ts | 12 +++++++-- libs/remix-url-resolver/tests/test.ts | 37 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/libs/remix-url-resolver/src/resolve.ts b/libs/remix-url-resolver/src/resolve.ts index 6ef9410305..e686f5c73d 100644 --- a/libs/remix-url-resolver/src/resolve.ts +++ b/libs/remix-url-resolver/src/resolve.ts @@ -1,4 +1,5 @@ import axios, { AxiosResponse } from 'axios' +var Bzz = require('web3-bzz'); export interface Imported { content: string; @@ -86,8 +87,15 @@ export class RemixURLResolver { } } - handleSwarm(url: string, cleanUrl: string) { - return + async handleSwarm(url: string, cleanUrl: string): Promise { + //eslint-disable-next-line no-useless-catch + try { + const bzz = new Bzz(Bzz.givenProvider || 'http://swarm-gateways.net'); + const contentInBuffer = await bzz.download(url) + return { content: contentInBuffer.toString(), cleanUrl } + } catch(e) { + throw e + } } /** diff --git a/libs/remix-url-resolver/tests/test.ts b/libs/remix-url-resolver/tests/test.ts index c3ee376504..38758bf49c 100644 --- a/libs/remix-url-resolver/tests/test.ts +++ b/libs/remix-url-resolver/tests/test.ts @@ -31,6 +31,12 @@ describe('testRunner', () => { // handle: (match: Array) => { return handleLocal(match[2], match[3]) } // } // ] +// browser/greeter.sol : +// bzz-raw://a728627437140f2b0b46c1bcfb0de2126d18b40e9b61c3e31bd96abebf714619 +// browser/mortal.sol : +// bzz-raw://bbd213997d71870470ad72e9b4a74d7898d11bfd3bfc483220f9fd0721607bff +// metadata.json : +// bzz-raw://ce595759fa991e3b165b280c9376f073c1d9d5591edaadf05fe76ddef8909249 // urlResolver.resolve(fileName, localFSHandler) // .then((sources: object) => { // results = sources @@ -172,6 +178,37 @@ describe('testRunner', () => { assert.deepEqual(results, expt) }) }) + + // Test SWARM imports + describe('test getting SWARM imports', () => { + const urlResolver = new RemixURLResolver() + const fileName = 'bzz-raw://a728627437140f2b0b46c1bcfb0de2126d18b40e9b61c3e31bd96abebf714619' + let results: object = {} + + before(done => { + urlResolver.resolve(fileName) + .then((sources: object) => { + results = sources + done() + }) + .catch((e: Error) => { + throw e + }) + }) + + it('should have 3 items', () => { + assert.equal(Object.keys(results).length, 3) + }) + it('should return contract content from raw github url', () => { + const content = fs.readFileSync(__dirname + '/example_1/greeter.sol', { encoding: 'utf8'}) + const expt: object = { + content: content, + cleanUrl: 'a728627437140f2b0b46c1bcfb0de2126d18b40e9b61c3e31bd96abebf714619', + type: 'swarm' + } + assert.deepEqual(results, expt) + }) + }) }) }) })