Commit deb1a4f3 authored by Iuri Matias's avatar Iuri Matias

move compiler code from browser-solidity including txHelper

parent 7eb8cc31
'use strict'
// TODO: can just use request or fetch instead
var $ = require('jquery')
var base64 = require('js-base64').Base64
var swarmgw = require('swarmgw')
module.exports = {
handleGithubCall: function (root, path, cb) {
return $.getJSON('https://api.github.com/repos/' + root + '/contents/' + path)
.done(function (data) {
if ('content' in data) {
cb(null, base64.decode(data.content), root + '/' + path)
} else {
cb('Content not received')
}
})
.fail(function (xhr, text, err) {
// NOTE: on some browsers, err equals to '' for certain errors (such as offline browser)
cb(err || 'Unknown transport error')
})
},
handleSwarmImport: function (url, cb) {
swarmgw.get(url, function (err, content) {
cb(err, content, url)
})
},
handleIPFS: function (url, cb) {
// replace ipfs:// with /ipfs/
url = url.replace(/^ipfs:\/\/?/, 'ipfs/')
return $.ajax({ type: 'GET', url: 'https://gateway.ipfs.io/' + url })
.done(function (data) {
cb(null, data, url)
})
.fail(function (xhr, text, err) {
// NOTE: on some browsers, err equals to '' for certain errors (such as offline browser)
cb(err || 'Unknown transport error')
})
},
handlers: function () {
return [
{ type: 'github', match: /^(https?:\/\/)?(www.)?github.com\/([^/]*\/[^/]*)\/(.*)/, handler: (match, cb) => { this.handleGithubCall(match[3], match[4], cb) } },
{ type: 'swarm', match: /^(bzz[ri]?:\/\/?.*)$/, handler: (match, cb) => { this.handleSwarmImport(match[1], cb) } },
{ type: 'ipfs', match: /^(ipfs:\/\/?.+)/, handler: (match, cb) => { this.handleIPFS(match[1], cb) } }
]
},
import: function (url, cb) {
var handlers = this.handlers()
var found = false
handlers.forEach(function (handler) {
if (found) {
return
}
var match = handler.match.exec(url)
if (match) {
found = true
// TODO: this needs to be moved to the caller
$('#output').append($('<div/>').append($('<pre/>').text('Loading ' + url + ' ...')))
handler.handler(match, function (err, content, cleanUrl) {
if (err) {
cb('Unable to import "' + cleanUrl + '": ' + err)
return
}
cb(null, content, cleanUrl, handler.type, url)
})
}
})
if (found) {
return
} else if (/^[^:]*:\/\//.exec(url)) {
cb('Unable to import "' + url + '": Unsupported URL schema')
} else {
cb('Unable to import "' + url + '": File not found')
}
}
}
'use strict'
module.exports = (sources, opts) => {
return JSON.stringify({
language: 'Solidity',
sources: sources,
settings: {
optimizer: {
enabled: opts.optimize === true || opts.optimize === 1,
runs: 200
},
libraries: opts.libraries,
outputSelection: {
'*': {
'': [ 'legacyAST' ],
'*': [ 'abi', 'metadata', 'evm.legacyAssembly', 'evm.bytecode', 'evm.deployedBytecode', 'evm.methodIdentifiers', 'evm.gasEstimates' ]
}
}
}
})
}
'use strict'
var solc = require('solc/wrapper')
var compileJSON = function () { return '' }
var missingInputs = []
module.exports = function (self) {
self.addEventListener('message', function (e) {
var data = e.data
switch (data.cmd) {
case 'loadVersion':
delete self.Module
// NOTE: workaround some browsers?
self.Module = undefined
compileJSON = null
self.importScripts(data.data)
var compiler = solc(self.Module)
compileJSON = function (input) {
try {
return compiler.compileStandardWrapper(input, function (path) {
missingInputs.push(path)
return { 'error': 'Deferred import' }
})
} catch (exception) {
return JSON.stringify({ error: 'Uncaught JavaScript exception:\n' + exception })
}
}
self.postMessage({
cmd: 'versionLoaded',
data: compiler.version()
})
break
case 'compile':
missingInputs.length = 0
self.postMessage({cmd: 'compiled', job: data.job, data: compileJSON(data.input), missingInputs: missingInputs})
break
}
}, false)
}
This diff is collapsed.
'use strict'
var ethJSABI = require('ethereumjs-abi')
var $ = require('jquery')
module.exports = {
encodeParams: function (funABI, args) {
var types = []
if (funABI.inputs && funABI.inputs.length) {
for (var i = 0; i < funABI.inputs.length; i++) {
var type = funABI.inputs[i].type
types.push(type)
if (args.length < types.length) {
args.push('')
}
}
}
// NOTE: the caller will concatenate the bytecode and this
// it could be done here too for consistency
return ethJSABI.rawEncode(types, args)
},
encodeFunctionId: function (funABI) {
var types = []
if (funABI.inputs && funABI.inputs.length) {
for (var i = 0; i < funABI.inputs.length; i++) {
types.push(funABI.inputs[i].type)
}
}
return ethJSABI.methodID(funABI.name, types)
},
sortAbiFunction: function (contractabi) {
var abi = contractabi.sort(function (a, b) {
if (a.name > b.name) {
return -1
} else {
return 1
}
}).sort(function (a, b) {
if (a.constant === true) {
return -1
} else {
return 1
}
})
return abi
},
getConstructorInterface: function (abi) {
var funABI = { 'name': '', 'inputs': [], 'type': 'constructor', 'outputs': [] }
if (typeof abi === 'string') {
try {
abi = JSON.parse(abi)
} catch (e) {
console.log('exception retrieving ctor abi ' + abi)
return funABI
}
}
for (var i = 0; i < abi.length; i++) {
if (abi[i].type === 'constructor') {
funABI.inputs = abi[i].inputs || []
break
}
}
return funABI
},
getFunction: function (abi, fnName) {
for (var i = 0; i < abi.length; i++) {
if (abi[i].name === fnName) {
return abi[i]
}
}
return null
},
getFallbackInterface: function (abi) {
for (var i = 0; i < abi.length; i++) {
if (abi[i].type === 'fallback') {
return abi[i]
}
}
},
/**
* return the contract obj of the given @arg name. Uses last compilation result.
* return null if not found
* @param {String} name - contract name
* @returns contract obj and associated file: { contract, file } or null
*/
getContract: (contractName, contracts) => {
for (var file in contracts) {
if (contracts[file][contractName]) {
return { object: contracts[file][contractName], file: file }
}
}
return null
},
/**
* call the given @arg cb (function) for all the contracts. Uses last compilation result
* stop visiting when cb return true
* @param {Function} cb - callback
*/
visitContracts: (contracts, cb) => {
for (var file in contracts) {
for (var name in contracts[file]) {
if (cb({ name: name, object: contracts[file][name], file: file })) return
}
}
},
inputParametersDeclarationToString: function (abiinputs) {
var inputs = ''
if (abiinputs) {
$.each(abiinputs, function (i, inp) {
if (inputs !== '') {
inputs += ', '
}
inputs += inp.type + ' ' + inp.name
})
}
return inputs
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment