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

Commit

Permalink
Support listing packages for ethpm v1
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Oct 30, 2020
1 parent 164030b commit 4780edb
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 61 deletions.
5 changes: 3 additions & 2 deletions packages/config/src/configDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ export const getInitialConfig = ({
ipfsProtocol: "https",
ipfsPort: "5001",
registry: {
address: "0x8011df4830b4f696cd81393997e5371b93338878",
address: "0xD230Dd91A049284a236f6ccba4412F647BDAAD9e",
network: "ropsten"
}
},
version: "3"
},
ens: {
enabled: false,
Expand Down
33 changes: 20 additions & 13 deletions packages/core/lib/commands/install.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
const { callbackify } = require("util");

const command = {
command: "install",
description: "Install a package from the Ethereum Package Registry",
builder: {},
help: {
usage: "truffle install <packageId> [--alias]",
usage: "truffle install <package-identifier> [--alias]",
options: [
{
option: "packageId",
description: `(required) Name of the package as listed in the Ethereum Package Registry.
Accepted formats: packageName, packageName@version, ethpm URI, ipfs URI.`
option: "packageIdentifier",
description: `(required) Name of the package as listed in the Ethereum Package Registry. Accepted formats: packageName, packageName@version, ethpm URI, ipfs URI.`
},
{
option: "--alias",
description:
"A different name under which this package will be installed."
description: "A different name under which to install this package."
}
]
},
run: async function (options, done) {
run: function (options, done) {
const Config = require("@truffle/config");
const PackageV1 = require("@truffle/ethpm-v1");
const PackageV3 = require("@truffle/ethpm-v3");

if (options._ && options._.length == 0) {
done(new Error(`Please provide a packageId.`));
done(
new Error(
`Please provide a package identifier. Run 'truffle help install' for more information on valid package identifiers.`
)
);
}
if (options._ && options._.length > 1) {
done(new Error(`Only one packageId can be installed at a time.`));
done(
new Error(`Only one package identifier can be installed at a time.`)
);
}
options.packageId = options._[0];
options.packageIdentifier = options._[0];

const config = Config.detect(options);
let callbackFunction;

if (config.ethpm.version == "1") {
await PackageV1.install(config);
callbackFunction = callbackify(PackageV1.install);
} else if (config.ethpm.version == "3") {
await PackageV3.install(config);
callbackFunction = callbackify(PackageV3.install);
} else {
done(new Error(`Unsupported ethpm version: ${config.ethpm.version}.`));
}
done();
callbackFunction(config, done);
}
};

Expand Down
14 changes: 10 additions & 4 deletions packages/core/lib/commands/packages.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { callbackify } = require("util");

var command = {
command: "packages",
description: "List available packages on connected EthPM Registry",
Expand All @@ -6,18 +8,22 @@ var command = {
usage: "truffle packages",
options: []
},
run: async function (options, done) {
run: function (options, done) {
var Config = require("@truffle/config");
var PackageV1 = require("@truffle/ethpm-v1");
var PackageV3 = require("@truffle/ethpm-v3");

var config = Config.detect(options);
let callbackFunction;

if (config.ethpm.version == "1" || config.ethpm.version == "3") {
await PackageV3.packages(config);
if (config.ethpm.version == "1") {
callbackFunction = callbackify(PackageV1.packages);
} else if (config.ethpm.version == "3") {
callbackFunction = callbackify(PackageV3.packages);
} else {
done(new Error(`Unsupported ethpm version: ${config.ethpm.version}.`));
}
done();
callbackFunction(config, done);
}
};

Expand Down
11 changes: 7 additions & 4 deletions packages/core/lib/commands/publish.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { callbackify } = require("util");

var command = {
command: "publish",
description: "Publish a package to the Ethereum Package Registry",
Expand All @@ -6,21 +8,22 @@ var command = {
usage: "truffle publish",
options: []
},
run: async function (options, done) {
run: function (options, done) {
var Config = require("@truffle/config");
var PackageV1 = require("@truffle/ethpm-v1");
var PackageV3 = require("@truffle/ethpm-v3");

var config = Config.detect(options);
let callbackFunction;

if (config.ethpm.version == "1") {
await PackageV1.publish(config);
callbackFunction = callbackify(PackageV1.publish);
} else if (config.ethpm.version == "3") {
await PackageV3.publish(config);
callbackFunction = callbackify(PackageV3.publish);
} else {
done(new Error(`Unsupported ethpm version: ${config.ethpm.version}.`));
}
done();
callbackFunction(config, done);
}
};

Expand Down
54 changes: 54 additions & 0 deletions packages/ethpm-v1/lib/ethpm-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,66 @@ const Networks = require("@truffle/core/lib/networks");
const EthPM = require("ethpm");
const EthPMRegistry = require("ethpm-registry");
const Web3 = require("web3");
const registryAbi = require("./registryAbi");
const { createInterfaceAdapter } = require("@truffle/interface-adapter");
const path = require("path");
const fs = require("fs");
const OS = require("os");

const PackageV1 = {
packages: async options => {
try {
expect.options(options, [
"ethpm",
"logger",
"working_directory",
"contracts_build_directory",
"networks"
]);
} catch (err) {
throw new TruffleError(
`Invalid ethpm configuration in truffle-config: ${err.message}`
);
}

const ethpmV1Network = "ropsten";
if (!options.networks[ethpmV1Network]) {
throw new TruffleError(
`Please include a provider in your networks config for the Ropsten testnet, the testnet where the ethpmV1 registry is deployed.`
);
}
const provider = options.networks["ropsten"].provider();
const web3 = new Web3(provider);
const ethpmV1RegistryAddress = "0x8011df4830b4f696cd81393997e5371b93338878";
var contract = new web3.eth.Contract(
registryAbi.abi,
ethpmV1RegistryAddress
);
var owner = await contract.methods.owner().call();

// Display info about connected registry
options.logger.log(
`Searching for packages published on registry located on the ethpm V1 registry: ${ethpmV1RegistryAddress}`
);
options.logger.log(`Registry controlled by : ${owner}`);

// Display info about all releases on registry
var numPackages = await contract.methods.getNumPackages().call();
for (var x = 0; x < numPackages; x++) {
let packageName = await contract.methods.getPackageName(x).call();
let releaseHashes = await contract.methods
.getAllPackageReleaseHashes(packageName)
.call();
options.logger.log(packageName);
for (let hash of releaseHashes) {
let releaseData = await contract.methods.getReleaseData(hash).call();
let version = `${releaseData.major}.${releaseData.minor}.${releaseData.patch}`;
options.logger.log(`- ${version} @ ${releaseData.releaseLockfileURI}`);
}
}
return;
},

install: async function (options, callback) {
const callbackPassed = typeof callback === "function";
expect.options(options, ["working_directory", "ethpm"]);
Expand Down
Loading

0 comments on commit 4780edb

Please sign in to comment.