Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
Fix project:/ prefix issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rkalis committed Jul 7, 2021
1 parent df14e1c commit 2d832d0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
44 changes: 39 additions & 5 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,55 @@ const enforceOrThrow = (condition, message) => {
* not make any changes on platforms that aren't Windows.
*
* @param {string} contractPath path to a contract file in any format.
* @param {any} options Options object containing the parsed truffle-plugin-verify options
* @returns {string} path to the contract in Windows format when on Windows, or Unix format otherwise.
*/
const normaliseContractPath = (contractPath) => {
const normaliseContractPath = (contractPath, options) => {
// Replace the 'project:' prefix that was added in Truffle v5.3.14 with the actual project path
const absolutePath = getAbsolutePath(contractPath, options)

// If the current platform is not Windows, the path does not need to be changed
if (process.platform !== 'win32') return contractPath
if (process.platform !== 'win32') return absolutePath

// If the contract path doesn't start with '/[A-Z]/' it is not a Unixified Windows path
if (!contractPath.match(/^\/[A-Z]\//i)) return contractPath
if (!absolutePath.match(/^\/[A-Z]\//i)) return absolutePath

const driveLetter = contractPath.substring(1, 2)
const normalisedContractPath = path.resolve(`${driveLetter}:/${contractPath.substring(3)}`)
const driveLetter = absolutePath.substring(1, 2)
const normalisedContractPath = path.resolve(`${driveLetter}:/${absolutePath.substring(3)}`)

return normalisedContractPath
}

/**
* @param {string} contractPath path to a contract file in any format.
* @param {string} contractsDir path to the directory containing contract source files.
* @returns {string} absolute path to the contract source file
*/
const getAbsolutePath = (contractPath, contractsDir) => {
// Older versions of truffle already used the absolute path
if (!contractPath.startsWith('project:/')) return contractPath

// Figure out the project path and use it to construct the absolute path
const relativeContractPath = contractPath.replace('project:/', '')
const projectPath = findProjectPath(relativeContractPath, contractsDir)
const absolutePath = path.join(projectPath, relativeContractPath)

return absolutePath
}

/**
* @param {string} relativeContractPath path to a contract file in any format.
* @param {string} contractsPath path to the directory containing contract source files.
* @returns {string} project path
*/
const findProjectPath = (relativeContractPath, contractsDir) => {
for (let currentPath = relativeContractPath; currentPath.length > 0; currentPath = currentPath.slice(0, -1)) {
if (contractsDir.endsWith(currentPath)) {
return contractsDir.slice(0, -1 * currentPath.length)
}
}
}

module.exports = {
abort,
enforce,
Expand Down
36 changes: 24 additions & 12 deletions verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const parseConfig = (config) => {

const workingDir = config.working_directory
const contractsBuildDir = config.contracts_build_directory
const contractsDir = config.contracts_directory

let forceConstructorArgsType, forceConstructorArgs
if (config.forceConstructorArgs) {
Expand All @@ -106,6 +107,7 @@ const parseConfig = (config) => {
networkId,
workingDir,
contractsBuildDir,
contractsDir,
forceConstructorArgs
}
}
Expand Down Expand Up @@ -142,14 +144,17 @@ const sendVerifyRequest = async (artifact, options) => {
const encodedConstructorArgs = options.forceConstructorArgs || await fetchConstructorValues(artifact, options)
const inputJSON = getInputJSON(artifact, options)

// Remove the 'project:' prefix that was added in Truffle v5.3.14
const relativeFilePath = artifact.ast.absolutePath.replace('project:', '')

const postQueries = {
apikey: options.apiKey,
module: 'contract',
action: 'verifysourcecode',
contractaddress: artifact.networks[`${options.networkId}`].address,
sourceCode: JSON.stringify(inputJSON),
codeformat: 'solidity-standard-json-input',
contractname: `${artifact.ast.absolutePath}:${artifact.contractName}`,
contractname: `${relativeFilePath}:${artifact.contractName}`,
compilerversion: compilerVersion,
constructorArguements: encodedConstructorArgs
}
Expand Down Expand Up @@ -209,12 +214,25 @@ const fetchConstructorValues = async (artifact, options) => {

const getInputJSON = (artifact, options) => {
const metadata = JSON.parse(artifact.metadata)

const libraries = getLibraries(artifact, options)

const sources = {}
for (const contractPath in metadata.sources) {
// If we're on Windows we need to de-Unixify the path so that Windows can read the file
// We also need to replace the 'project:' prefix so that the file can be read
const normalisedContractPath = normaliseContractPath(contractPath, options.contractsDir)
const absolutePath = require.resolve(normalisedContractPath)
const content = fs.readFileSync(absolutePath, 'utf8')

// Remove the 'project:' prefix that was added in Truffle v5.3.14
const relativeContractPath = contractPath.replace('project:', '')

sources[relativeContractPath] = { content }
}

const inputJSON = {
language: metadata.language,
sources: metadata.sources,
sources,
settings: {
remappings: metadata.settings.remappings,
optimizer: metadata.settings.optimizer,
Expand All @@ -223,14 +241,6 @@ const getInputJSON = (artifact, options) => {
}
}

for (const contractPath in inputJSON.sources) {
// If we're on Windows we need to de-Unixify the path so that Windows can read the file
const normalisedContractPath = normaliseContractPath(contractPath, logger)
const absolutePath = require.resolve(normalisedContractPath)
const content = fs.readFileSync(absolutePath, 'utf8')
inputJSON.sources[contractPath] = { content }
}

return inputJSON
}

Expand All @@ -248,7 +258,9 @@ const getLibraries = (artifact, options) => {
for (const libraryName in links) {
// Retrieve the source path for this library
const libraryArtifact = getArtifact(libraryName, options)
const librarySourceFile = libraryArtifact.ast.absolutePath

// Remove the 'project:' prefix that was added in Truffle v5.3.14
const librarySourceFile = libraryArtifact.ast.absolutePath.replace('project:', '')

// Add the library to the object of libraries for this source path
const librariesForSourceFile = libraries[librarySourceFile] || {}
Expand Down

0 comments on commit 2d832d0

Please sign in to comment.