|
|
|
@ -5,8 +5,6 @@ import { eachOfSeries } from 'async' |
|
|
|
|
import { linkBytecode } from 'solc/linker' |
|
|
|
|
import { isValidAddress, addHexPrefix } from 'ethereumjs-util' |
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* build the transaction data |
|
|
|
|
* |
|
|
|
@ -14,7 +12,7 @@ module.exports = { |
|
|
|
|
* @param {Object} values to encode |
|
|
|
|
* @param {String} contractbyteCode |
|
|
|
|
*/ |
|
|
|
|
encodeData: function (funABI, values, contractbyteCode) { |
|
|
|
|
export function encodeData (funABI, values, contractbyteCode) { |
|
|
|
|
let encoded |
|
|
|
|
let encodedHex |
|
|
|
|
try { |
|
|
|
@ -28,7 +26,7 @@ module.exports = { |
|
|
|
|
} else { |
|
|
|
|
return { data: helper.encodeFunctionId(funABI) + encodedHex.replace('0x', '') } |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* encode function / constructor parameters |
|
|
|
@ -37,7 +35,7 @@ module.exports = { |
|
|
|
|
* @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor. |
|
|
|
|
* @param {Function} callback - callback |
|
|
|
|
*/ |
|
|
|
|
encodeParams: function (params, funAbi, callback) { |
|
|
|
|
export function encodeParams (params, funAbi, callback) { |
|
|
|
|
let data: any = '' |
|
|
|
|
let dataHex = '' |
|
|
|
|
let funArgs |
|
|
|
@ -69,7 +67,7 @@ module.exports = { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
callback(null, { data: data, dataHex: dataHex, funArgs: funArgs }) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* encode function call (function id + encoded parameters) |
|
|
|
@ -78,12 +76,12 @@ module.exports = { |
|
|
|
|
* @param {Object} funAbi - abi definition of the function to call. null if building data for the ctor. |
|
|
|
|
* @param {Function} callback - callback |
|
|
|
|
*/ |
|
|
|
|
encodeFunctionCall: function (params, funAbi, callback) { |
|
|
|
|
export function encodeFunctionCall (params, funAbi, callback) { |
|
|
|
|
this.encodeParams(params, funAbi, (error, encodedParam) => { |
|
|
|
|
if (error) return callback(error) |
|
|
|
|
callback(null, { dataHex: helper.encodeFunctionId(funAbi) + encodedParam.dataHex, funAbi, funArgs: encodedParam.funArgs }) |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* encode constructor creation and link with provided libraries if needed |
|
|
|
@ -95,7 +93,7 @@ module.exports = { |
|
|
|
|
* @param {Object} linkReferences - given by the compiler, contains the proper linkReferences |
|
|
|
|
* @param {Function} callback - callback |
|
|
|
|
*/ |
|
|
|
|
encodeConstructorCallAndLinkLibraries: function (contract, params, funAbi, linkLibraries, linkReferences, callback) { |
|
|
|
|
export function encodeConstructorCallAndLinkLibraries (contract, params, funAbi, linkLibraries, linkReferences, callback) { |
|
|
|
|
this.encodeParams(params, funAbi, (error, encodedParam) => { |
|
|
|
|
if (error) return callback(error) |
|
|
|
|
let bytecodeToDeploy = contract.evm.bytecode.object |
|
|
|
@ -115,7 +113,7 @@ module.exports = { |
|
|
|
|
} |
|
|
|
|
return callback(null, { dataHex: bytecodeToDeploy + encodedParam.dataHex, funAbi, funArgs: encodedParam.funArgs, contractBytecode: contract.evm.bytecode.object }) |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* encode constructor creation and deploy librairies if needed |
|
|
|
@ -130,7 +128,7 @@ module.exports = { |
|
|
|
|
* @param {Function} callbackDeployLibrary - callbackDeployLibrary |
|
|
|
|
* @param {Function} callback - callback |
|
|
|
|
*/ |
|
|
|
|
encodeConstructorCallAndDeployLibraries: function (contractName, contract, contracts, params, funAbi, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
export function encodeConstructorCallAndDeployLibraries (contractName, contract, contracts, params, funAbi, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
this.encodeParams(params, funAbi, (error, encodedParam) => { |
|
|
|
|
if (error) return callback(error) |
|
|
|
|
let dataHex = '' |
|
|
|
@ -151,7 +149,7 @@ module.exports = { |
|
|
|
|
} |
|
|
|
|
callback(null, {dataHex: bytecodeToDeploy, funAbi, funArgs: encodedParam.funArgs, contractBytecode, contractName: contractName}) |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* (DEPRECATED) build the transaction data |
|
|
|
@ -166,7 +164,7 @@ module.exports = { |
|
|
|
|
* @param {Function} callbackStep - callbackStep |
|
|
|
|
* @param {Function} callbackDeployLibrary - callbackDeployLibrary |
|
|
|
|
*/ |
|
|
|
|
buildData: function (contractName, contract, contracts, isConstructor, funAbi, params, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
export function buildData (contractName, contract, contracts, isConstructor, funAbi, params, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
let funArgs = [] |
|
|
|
|
let data: any = '' |
|
|
|
|
let dataHex = '' |
|
|
|
@ -217,11 +215,11 @@ module.exports = { |
|
|
|
|
dataHex = helper.encodeFunctionId(funAbi) + dataHex |
|
|
|
|
} |
|
|
|
|
callback(null, { dataHex, funAbi, funArgs, contractBytecode, contractName: contractName }) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
atAddress: function () {}, |
|
|
|
|
export function atAddress () {} |
|
|
|
|
|
|
|
|
|
linkBytecodeStandard: function (contract, contracts, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
export function linkBytecodeStandard (contract, contracts, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
let contractBytecode = contract.evm.bytecode.object |
|
|
|
|
eachOfSeries(contract.evm.bytecode.linkReferences, (libs, file, cbFile) => { |
|
|
|
|
eachOfSeries(contract.evm.bytecode.linkReferences[file], (libRef, libName, cbLibDeployed) => { |
|
|
|
@ -250,9 +248,9 @@ module.exports = { |
|
|
|
|
} |
|
|
|
|
callback(error, contractBytecode) |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
linkBytecodeLegacy: function (contract, contracts, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
export function linkBytecodeLegacy (contract, contracts, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
const libraryRefMatch = contract.evm.bytecode.object.match(/__([^_]{1,36})__/) |
|
|
|
|
if (!libraryRefMatch) { |
|
|
|
|
return callback('Invalid bytecode format.') |
|
|
|
@ -282,9 +280,9 @@ module.exports = { |
|
|
|
|
contract.evm.bytecode.object = this.linkLibrary(libraryName, hexAddress, contract.evm.bytecode.object) |
|
|
|
|
this.linkBytecode(contract, contracts, callback, callbackStep, callbackDeployLibrary) |
|
|
|
|
}, callbackStep, callbackDeployLibrary) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
linkBytecode: function (contract, contracts, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
export function linkBytecode (contract, contracts, callback?, callbackStep?, callbackDeployLibrary?) { |
|
|
|
|
if (contract.evm.bytecode.object.indexOf('_') < 0) { |
|
|
|
|
return callback(null, contract.evm.bytecode.object) |
|
|
|
|
} |
|
|
|
@ -293,9 +291,9 @@ module.exports = { |
|
|
|
|
} else { |
|
|
|
|
this.linkBytecodeLegacy(contract, contracts, callback, callbackStep, callbackDeployLibrary) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
deployLibrary: function (libraryName, libraryShortName, library, contracts, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
export function deployLibrary (libraryName, libraryShortName, library, contracts, callback, callbackStep, callbackDeployLibrary) { |
|
|
|
|
const address = library.address |
|
|
|
|
if (address) { |
|
|
|
|
return callback(null, address) |
|
|
|
@ -321,9 +319,9 @@ module.exports = { |
|
|
|
|
callback(err, address) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
linkLibraryStandardFromlinkReferences: function (libraryName, address, bytecode, linkReferences) { |
|
|
|
|
export function linkLibraryStandardFromlinkReferences (libraryName, address, bytecode, linkReferences) { |
|
|
|
|
for (let file in linkReferences) { |
|
|
|
|
for (let libName in linkReferences[file]) { |
|
|
|
|
if (libraryName === libName) { |
|
|
|
@ -332,13 +330,13 @@ module.exports = { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return bytecode |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
linkLibraryStandard: function (libraryName, address, bytecode, contract) { |
|
|
|
|
export function linkLibraryStandard (libraryName, address, bytecode, contract) { |
|
|
|
|
return this.linkLibraryStandardFromlinkReferences(libraryName, address, bytecode, contract.evm.bytecode.linkReferences) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setLibraryAddress: function (address, bytecodeToLink, positions) { |
|
|
|
|
export function setLibraryAddress (address, bytecodeToLink, positions) { |
|
|
|
|
if (positions) { |
|
|
|
|
for (let pos of positions) { |
|
|
|
|
const regpos = bytecodeToLink.match(new RegExp(`(.{${2 * pos.start}})(.{${2 * pos.length}})(.*)`)) |
|
|
|
@ -348,13 +346,13 @@ module.exports = { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return bytecodeToLink |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
linkLibrary: function (libraryName, address, bytecodeToLink) { |
|
|
|
|
export function linkLibrary (libraryName, address, bytecodeToLink) { |
|
|
|
|
return linkBytecode(bytecodeToLink, { [libraryName]: addHexPrefix(address) }) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
decodeResponse: function (response, fnabi) { |
|
|
|
|
export function decodeResponse (response, fnabi) { |
|
|
|
|
// Only decode if there supposed to be fields
|
|
|
|
|
if (fnabi.outputs && fnabi.outputs.length > 0) { |
|
|
|
|
try { |
|
|
|
@ -383,9 +381,9 @@ module.exports = { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return {} |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
parseFunctionParams: function (params) { |
|
|
|
|
export function parseFunctionParams (params) { |
|
|
|
|
let args = [] |
|
|
|
|
// Check if parameter string starts with array or string
|
|
|
|
|
let startIndex = this.isArrayOrStringStart(params, 0) ? -1 : 0 |
|
|
|
@ -445,10 +443,10 @@ module.exports = { |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
return args |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
isArrayOrStringStart: function (str, index) { |
|
|
|
|
export function isArrayOrStringStart (str, index) { |
|
|
|
|
return str.charAt(index) === '"' || str.charAt(index) === '[' |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|