This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
package.js
355 lines (300 loc) · 10.7 KB
/
package.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const expect = require("@truffle/expect");
const TruffleError = require("@truffle/error");
const Networks = require("./networks");
const EthPM = require("ethpm");
const EthPMRegistry = require("ethpm-registry");
const Web3 = require("web3");
const { Web3Shim, InterfaceAdapter } = require("@truffle/interface-adapter");
const async = require("async");
const path = require("path");
const fs = require("fs");
const OS = require("os");
const Package = {
install: async function(options, callback) {
expect.options(options, ["working_directory", "ethpm"]);
expect.options(options.ethpm, ["registry", "ipfs_host"]);
expect.one(options.ethpm, ["provider", "install_provider_uri"]);
// ipfs_port and ipfs_protocol are optinal.
const provider =
options.ethpm.provider ||
new Web3.providers.HttpProvider(options.ethpm.install_provider_uri, {
keepAlive: false
});
let host = options.ethpm.ipfs_host;
if (host instanceof EthPM.hosts.IPFS === false) {
host = new EthPM.hosts.IPFSWithLocalReader(
options.ethpm.ipfs_host,
options.ethpm.ipfs_port,
options.ethpm.ipfs_protocol
);
}
// When installing, we use infura to make a bunch of eth_call's.
// We don't make any transactions. To satisfy APIs we'll put a from address,
// but it doesn't really matter in this case.
const fakeAddress = "0x1234567890123456789012345678901234567890";
let registry = options.ethpm.registry;
if (typeof registry === "string") {
registry = await EthPMRegistry.use(
options.ethpm.registry,
fakeAddress,
provider
);
}
const pkg = new EthPM(options.working_directory, host, registry);
if (options.packages) {
const promises = options.packages.map(package_name => {
const pieces = package_name.split("@");
package_name = pieces[0];
let version = "*";
if (pieces.length > 1) version = pieces[1];
return pkg.installDependency(package_name, version);
});
Promise.all(promises)
.then(() => {
if (options.packages.length > 0) {
console.log("");
console.log("Successfully installed the following package(s)...");
console.log("==================================================");
options.packages.forEach(singlePackage => {
console.log(`> ${singlePackage}`);
});
console.log("");
}
callback();
})
.catch(callback);
} else {
fs.access(
path.join(options.working_directory, "ethpm.json"),
fs.constants.R_OK,
error => {
let manifest;
// If the ethpm.json file doesn't exist, use the config as the manifest.
if (error) manifest = options;
pkg
.install(manifest)
.then(() => {
callback();
})
.catch(callback);
}
);
}
},
publish: function(options, callback) {
var self = this;
expect.options(options, [
"ethpm",
"working_directory",
"contracts_directory",
"networks"
]);
expect.options(options.ethpm, ["registry", "ipfs_host"]);
// ipfs_port and ipfs_protocol are optinal.
// When publishing, you need a ropsten network configured.
var ropsten = options.networks.ropsten;
if (!ropsten) {
return callback(
new TruffleError(
"You need to have a `ropsten` network configured in order to publish to the Ethereum Package Registry. See the following link for an example configuration:" +
OS.EOL +
OS.EOL +
" http://truffleframework.com/tutorials/using-infura-custom-provider" +
OS.EOL
)
);
}
options.network = "ropsten";
var provider = options.provider;
const interfaceAdapter = new InterfaceAdapter({
provider: options.provider,
networkType: "ethereum"
});
var web3 = new Web3Shim({
provider: options.provider,
networkType: "ethereum"
});
var host = options.ethpm.ipfs_host;
if (host instanceof EthPM.hosts.IPFS === false) {
host = new EthPM.hosts.IPFS(
options.ethpm.ipfs_host,
options.ethpm.ipfs_port,
options.ethpm.ipfs_protocol
);
}
options.logger.log("Finding publishable artifacts...");
self.publishable_artifacts(options, function(err, artifacts) {
if (err) return callback(err);
web3.eth
.getAccounts()
.then(async accs => {
var registry = await EthPMRegistry.use(
options.ethpm.registry,
accs[0],
provider
);
var pkg = new EthPM(options.working_directory, host, registry);
fs.access(
path.join(options.working_directory, "ethpm.json"),
fs.constants.R_OK,
function(err) {
var manifest;
// If the ethpm.json file doesn't exist, use the config as the manifest.
if (err) {
manifest = options;
}
options.logger.log(
"Uploading sources and publishing to registry..."
);
// TODO: Gather contract_types and deployments
pkg
.publish(
artifacts.contract_types,
artifacts.deployments,
manifest
)
.then(function(lockfile) {
// If we get here, publishing was a success.
options.logger.log(
"+ " + lockfile.package_name + "@" + lockfile.version
);
callback();
})
.catch(callback);
}
);
})
.catch(callback);
});
},
digest: function(options, callback) {
// async.parallel({
// contracts: provision.bind(provision, options, false),
// files: dir.files.bind(dir, options.contracts_directory)
// }, function(err, results) {
// if (err) return callback(err);
//
// results.contracts = results.contracts.map(function(contract) {
// return contract.contract_name;
// });
//
// callback(null, results);
// });
callback(new Error("Not yet implemented"));
},
// Return a list of publishable artifacts
publishable_artifacts: function(options, callback) {
// Filter out "test" and "development" networks.
var deployed_networks = Object.keys(options.networks).filter(function(
network_name
) {
return network_name !== "test" && network_name !== "development";
});
// Now get the URIs of each network that's been deployed to.
Networks.asURIs(options, deployed_networks, function(err, result) {
if (err) return callback(err);
var uris = result.uris;
if (result.failed.length > 0) {
return callback(
new Error(
"Could not connect to the following networks: " +
result.failed.join(", ") +
". These networks have deployed artifacts that can't be published as a package without an active and accessible connection. Please ensure clients for each network are up and running prior to publishing, or use the -n option to specify specific networks you'd like published."
)
);
}
var files = fs.readdirSync(options.contracts_build_directory);
files = files.filter(file => file.includes(".json"));
if (!files.length) {
var msg =
"Could not locate any publishable artifacts in " +
options.contracts_build_directory +
". " +
"Run `truffle compile` before publishing.";
return callback(new Error(msg));
}
var promises = files.map(function(file) {
return new Promise(function(accept, reject) {
fs.readFile(
path.join(options.contracts_build_directory, file),
"utf8",
function(err, body) {
if (err) return reject(err);
try {
body = JSON.parse(body);
} catch (e) {
return reject(e);
}
accept(body);
}
);
});
});
var contract_types = {};
var deployments = {};
Promise.all(promises)
.then(function(contracts) {
// contract_types first.
contracts.forEach(function(data) {
contract_types[data.contractName] = {
contract_name: data.contractName,
bytecode: data.bytecode,
abi: data.abi
};
});
//var network_cache = {};
var matching_promises = [];
contracts.forEach(function(data) {
Object.keys(data.networks).forEach(function(network_id) {
matching_promises.push(
new Promise(function(accept, reject) {
// Go through each deployed network and see if this network matches.
// Bail early if we foun done.
async.each(
deployed_networks,
function(deployed_network, finished) {
Networks.matchesNetwork(
network_id,
options.networks[deployed_network],
function(err, matches) {
if (err) return finished(err);
if (matches) {
var uri = uris[deployed_network];
if (!deployments[uri]) {
deployments[uri] = {};
}
deployments[uri][data.contractName] = {
contract_type: data.contractName, // TODO: Handle conflict resolution
address: data.networks[network_id].address
};
return finished("bail early");
}
finished();
}
);
},
function(err) {
if (err && err !== "bail early") {
return reject(err);
}
accept();
}
);
})
);
});
});
return Promise.all(matching_promises);
})
.then(function() {
var to_return = {
contract_types: contract_types,
deployments: deployments
};
callback(null, to_return);
})
.catch(callback);
});
}
};
module.exports = Package;