@aztec/cli 0.7.0 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.tsbuildinfo +1 -1
- package/README.md +3 -3
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +16 -6
- package/dest/unbox.d.ts +14 -0
- package/dest/unbox.d.ts.map +1 -0
- package/dest/unbox.js +247 -0
- package/package.json +9 -7
- package/src/index.ts +18 -5
- package/src/unbox.ts +303 -0
package/src/unbox.ts
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// inspired by https://github.com/trufflesuite/truffle/blob/develop/packages/box/lib/utils/unbox.ts
|
|
2
|
+
// however, their boxes are stored as standalone git repositories, while ours are subpackages in a monorepo
|
|
3
|
+
// so we do some hacky conversions post copy to make them work as standalone packages.
|
|
4
|
+
// We download the master branch of the monorepo, and then
|
|
5
|
+
// (1) copy "boxes/{CONTRACT_NAME}" subpackage to the specified output directory
|
|
6
|
+
// (2) if the box doesnt include noir source code, we copy it from the "noir-contracts" subpackage to into a new subdirectory "X/src/contracts",
|
|
7
|
+
// These are used by a simple frontend to interact with the contract and deploy to a local sandbox instance of aztec3.
|
|
8
|
+
// The unbox logic can be tested locally by running `$ts-node --esm src/bin/index.ts unbox PrivateToken`
|
|
9
|
+
// from `yarn-project/cli/`
|
|
10
|
+
import { LogFn } from '@aztec/foundation/log';
|
|
11
|
+
|
|
12
|
+
import { promises as fs } from 'fs';
|
|
13
|
+
import JSZip from 'jszip';
|
|
14
|
+
import fetch from 'node-fetch';
|
|
15
|
+
import * as path from 'path';
|
|
16
|
+
|
|
17
|
+
const GITHUB_OWNER = 'AztecProtocol';
|
|
18
|
+
const GITHUB_REPO = 'aztec-packages';
|
|
19
|
+
const NOIR_CONTRACTS_PATH = 'yarn-project/noir-contracts/src/contracts';
|
|
20
|
+
const BOXES_PATH = 'yarn-project/boxes';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Converts a contract name in "upper camel case" to a folder name in snake case.
|
|
24
|
+
* @param contractName - The contract name.
|
|
25
|
+
* @returns The folder name.
|
|
26
|
+
* */
|
|
27
|
+
function contractNameToFolder(contractName: string): string {
|
|
28
|
+
return contractName.replace(/[\w]([A-Z])/g, m => m[0] + '_' + m[1]).toLowerCase();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* If the box contains the noir contract source code, we don't need to download it from github.
|
|
33
|
+
* Otherwise, we download the contract source code from the `noir-contracts` and `noir-libs` subpackages.
|
|
34
|
+
*/
|
|
35
|
+
async function isDirectoryNonEmpty(directoryPath: string): Promise<boolean> {
|
|
36
|
+
const files = await fs.readdir(directoryPath);
|
|
37
|
+
return files.length > 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @param data - in memory unzipped clone of a github repo
|
|
43
|
+
* @param repositoryFolderPath - folder to copy from github repo
|
|
44
|
+
* @param localOutputPath - local path to copy to
|
|
45
|
+
*/
|
|
46
|
+
async function copyFolderFromGithub(data: JSZip, repositoryFolderPath: string, localOutputPath: string, log: LogFn) {
|
|
47
|
+
log('downloading from github:', repositoryFolderPath);
|
|
48
|
+
const repositoryDirectories = Object.values(data.files).filter(file => {
|
|
49
|
+
return file.dir && file.name.startsWith(repositoryFolderPath);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
for (const directory of repositoryDirectories) {
|
|
53
|
+
const relativePath = directory.name.replace(repositoryFolderPath, '');
|
|
54
|
+
const targetPath = `${localOutputPath}/${relativePath}`;
|
|
55
|
+
await fs.mkdir(targetPath, { recursive: true });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const starterFiles = Object.values(data.files).filter(file => {
|
|
59
|
+
return !file.dir && file.name.startsWith(repositoryFolderPath);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
for (const file of starterFiles) {
|
|
63
|
+
const relativePath = file.name.replace(repositoryFolderPath, '');
|
|
64
|
+
const targetPath = `${localOutputPath}/${relativePath}`;
|
|
65
|
+
const content = await file.async('nodebuffer');
|
|
66
|
+
await fs.writeFile(targetPath, content);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Not flexible at at all, but quick fix to download a noir smart contract from our
|
|
72
|
+
* monorepo on github. this will copy over the `yarn-projects/boxes/{contract_name}` folder
|
|
73
|
+
* as well as the specified `directoryPath` if the box doesn't include source code
|
|
74
|
+
* `directoryPath` should point to a single noir contract in `yarn-projects/noir-contracts/src/contracts/...`
|
|
75
|
+
* @param tagVersion - the version of the CLI that is running. we pull from the corresponding tag in the monorepo
|
|
76
|
+
* @param directoryPath - path to a noir contract's source code (folder) in the github repo
|
|
77
|
+
* @param outputPath - local path that we will copy the noir contracts and web3 starter kit to
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
async function downloadContractAndBoxFromGithub(
|
|
81
|
+
tagVersion: string,
|
|
82
|
+
contractName: string,
|
|
83
|
+
outputPath: string,
|
|
84
|
+
log: LogFn,
|
|
85
|
+
): Promise<void> {
|
|
86
|
+
// small string conversion, in the ABI the contract name looks like PrivateToken
|
|
87
|
+
// but in the repostory it looks like private_token
|
|
88
|
+
const snakeCaseContractName = contractNameToFolder(contractName);
|
|
89
|
+
|
|
90
|
+
log(`Downloaded '@aztex/boxes/${snakeCaseContractName}' to ${outputPath}`);
|
|
91
|
+
// Step 1: Fetch the monorepo ZIP from GitHub, matching the CLI version
|
|
92
|
+
const url = `https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}/archive/refs/tags/aztec-packages-v${tagVersion}.zip`;
|
|
93
|
+
const response = await fetch(url);
|
|
94
|
+
const buffer = await response.arrayBuffer();
|
|
95
|
+
|
|
96
|
+
const zip = new JSZip();
|
|
97
|
+
const data = await zip.loadAsync(buffer);
|
|
98
|
+
|
|
99
|
+
// Step 2: copy the '@aztec/boxes/{contract-name}' subpackage to the output directory
|
|
100
|
+
// this is currently only implemented for PrivateToken under 'boxes/private-token/'
|
|
101
|
+
const repoDirectoryPrefix = `${GITHUB_REPO}-v${tagVersion}/`;
|
|
102
|
+
|
|
103
|
+
const boxPath = `${repoDirectoryPrefix}${BOXES_PATH}/${snakeCaseContractName}`;
|
|
104
|
+
await copyFolderFromGithub(data, boxPath, outputPath, log);
|
|
105
|
+
|
|
106
|
+
const boxContainsNoirSource = await isDirectoryNonEmpty(`${outputPath}/src/contracts`);
|
|
107
|
+
if (boxContainsNoirSource) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
// this remaining logic only kicks in if the box doesn't already have a src/contracts folder
|
|
111
|
+
// in which case we optimistically grab the noir source files from the
|
|
112
|
+
// noir-contracts and noir-libs subpackages and pray that the versions are compatible
|
|
113
|
+
|
|
114
|
+
// source noir files for the contract are in this folder
|
|
115
|
+
const contractFolder = `${NOIR_CONTRACTS_PATH}/${snakeCaseContractName}_contract`;
|
|
116
|
+
// copy the noir contracts to the output directory under subdir /src/contracts/
|
|
117
|
+
const contractDirectoryPath = `${repoDirectoryPrefix}${contractFolder}/`;
|
|
118
|
+
|
|
119
|
+
const contractFiles = Object.values(data.files).filter(file => {
|
|
120
|
+
return !file.dir && file.name.startsWith(contractDirectoryPath);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const contractTargetDirectory = path.join(outputPath, 'src', 'contracts');
|
|
124
|
+
await fs.mkdir(contractTargetDirectory, { recursive: true });
|
|
125
|
+
// Nargo.toml file needs to be in the root of the contracts directory,
|
|
126
|
+
// and noir files in the src/ subdirectory
|
|
127
|
+
await fs.mkdir(path.join(contractTargetDirectory, 'src'), { recursive: true });
|
|
128
|
+
for (const file of contractFiles) {
|
|
129
|
+
const targetPath = path.join(contractTargetDirectory, file.name.replace(contractDirectoryPath, ''));
|
|
130
|
+
log(`Copying ${file.name} to ${targetPath}`);
|
|
131
|
+
const content = await file.async('nodebuffer');
|
|
132
|
+
await fs.writeFile(targetPath, content);
|
|
133
|
+
log(`Copied ${file.name} to ${targetPath}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Does some conversion from the package/build configurations in the monorepo to the
|
|
138
|
+
* something usable by the copied standalone unboxed folder. Adjusts relative paths
|
|
139
|
+
* and package versions.
|
|
140
|
+
* @param packageVersion - CLI npm version, which determines what npm version to grab
|
|
141
|
+
* @param outputPath - relative path where we are copying everything
|
|
142
|
+
* @param log - logger
|
|
143
|
+
*/
|
|
144
|
+
async function updatePackagingConfigurations(packageVersion: string, outputPath: string, log: LogFn): Promise<void> {
|
|
145
|
+
await updatePackageJsonVersions(packageVersion, outputPath, log);
|
|
146
|
+
await updateTsConfig(outputPath, log);
|
|
147
|
+
await updateNargoToml(packageVersion, outputPath, log);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* adjust the contract Nargo.toml file to use the same repository version as the npm packages
|
|
152
|
+
* @param packageVersion - CLI npm version, which determines what npm version to grab
|
|
153
|
+
* @param outputPath - relative path where we are copying everything
|
|
154
|
+
* @param log - logger
|
|
155
|
+
*/
|
|
156
|
+
async function updateNargoToml(packageVersion: string, outputPath: string, log: LogFn): Promise<void> {
|
|
157
|
+
const nargoTomlPath = path.join(outputPath, 'src', 'contracts', 'Nargo.toml');
|
|
158
|
+
const fileContent = await fs.readFile(nargoTomlPath, 'utf-8');
|
|
159
|
+
const lines = fileContent.split('\n');
|
|
160
|
+
const updatedLines = lines.map(line => line.replace(/tag="master"/g, `tag="v${packageVersion}"`));
|
|
161
|
+
const updatedContent = updatedLines.join('\n');
|
|
162
|
+
await fs.writeFile(nargoTomlPath, updatedContent);
|
|
163
|
+
log(`Updated Nargo.toml to point to local copy of noir-libs`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The `tsconfig.json` also needs to be updated to remove the "references" section, which
|
|
168
|
+
* points to the monorepo's subpackages. Those are unavailable in the cloned subpackage,
|
|
169
|
+
* so we remove the entries to install the the workspace packages from npm.
|
|
170
|
+
* @param outputPath - directory we are unboxing to
|
|
171
|
+
*/
|
|
172
|
+
async function updateTsConfig(outputPath: string, log: LogFn) {
|
|
173
|
+
try {
|
|
174
|
+
const tsconfigJsonPath = path.join(outputPath, 'tsconfig.json');
|
|
175
|
+
const data = await fs.readFile(tsconfigJsonPath, 'utf8');
|
|
176
|
+
const config = JSON.parse(data);
|
|
177
|
+
|
|
178
|
+
delete config.references;
|
|
179
|
+
|
|
180
|
+
const updatedData = JSON.stringify(config, null, 2);
|
|
181
|
+
await fs.writeFile(tsconfigJsonPath, updatedData, 'utf8');
|
|
182
|
+
|
|
183
|
+
log('tsconfig.json has been updated');
|
|
184
|
+
} catch (error) {
|
|
185
|
+
log('Error updating tsconfig.json:', error);
|
|
186
|
+
throw error;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* We pin to "workspace:^" in the package.json for subpackages, but we need to replace it with
|
|
192
|
+
* an the actual version number in the cloned starter kit
|
|
193
|
+
* We modify the copied package.json and pin to the version of the package that was downloaded
|
|
194
|
+
* @param packageVersion - CLI npm version, which determines what npm version to grab
|
|
195
|
+
* @param outputPath - directory we are unboxing to
|
|
196
|
+
* @param log - logger
|
|
197
|
+
*/
|
|
198
|
+
async function updatePackageJsonVersions(packageVersion: string, outputPath: string, log: LogFn): Promise<void> {
|
|
199
|
+
const packageJsonPath = path.join(outputPath, 'package.json');
|
|
200
|
+
const fileContent = await fs.readFile(packageJsonPath, 'utf-8');
|
|
201
|
+
const packageData = JSON.parse(fileContent);
|
|
202
|
+
|
|
203
|
+
// Check and replace "workspace^" pins in dependencies, which are monorepo yarn workspace references
|
|
204
|
+
if (packageData.dependencies) {
|
|
205
|
+
for (const [key, value] of Object.entries(packageData.dependencies)) {
|
|
206
|
+
if (value === 'workspace:^') {
|
|
207
|
+
packageData.dependencies[key] = `^${packageVersion}`;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Check and replace in devDependencies
|
|
213
|
+
if (packageData.devDependencies) {
|
|
214
|
+
for (const [key, value] of Object.entries(packageData.devDependencies)) {
|
|
215
|
+
if (value === 'workspace:^') {
|
|
216
|
+
// TODO: check if this right post landing. the package.json version looks like 0.1.0
|
|
217
|
+
// but the npm versions look like v0.1.0-alpha63 so we are not fully pinned
|
|
218
|
+
packageData.devDependencies[key] = `^${packageVersion}`;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// modify the version of the sandbox to pull - it's set to "latest" version in the monorepo,
|
|
224
|
+
// but we need to replace with the same tagVersion as the cli and the other aztec npm packages
|
|
225
|
+
// similarly, make sure we spinup the sandbox with the same version.
|
|
226
|
+
packageData.scripts['install:sandbox'] = packageData.scripts['install:sandbox'].replace(
|
|
227
|
+
'latest',
|
|
228
|
+
`v${packageVersion}`,
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
packageData.scripts['start:sandbox'] = packageData.scripts['start:sandbox'].replace('latest', `v${packageVersion}`);
|
|
232
|
+
|
|
233
|
+
// Convert back to a string and write back to the package.json file
|
|
234
|
+
const updatedContent = JSON.stringify(packageData, null, 2);
|
|
235
|
+
await fs.writeFile(packageJsonPath, updatedContent);
|
|
236
|
+
|
|
237
|
+
log(`Updated package.json versions to ${packageVersion}`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
*
|
|
242
|
+
* @param outputDirectoryName - user specified directory we are "unboxing" files into
|
|
243
|
+
* @param log - logger
|
|
244
|
+
* @returns
|
|
245
|
+
*/
|
|
246
|
+
async function createDirectory(outputDirectoryName: string, log: LogFn): Promise<string> {
|
|
247
|
+
const absolutePath = path.resolve(outputDirectoryName);
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
// Checking if the path exists and if it is a directory
|
|
251
|
+
const stats = await fs.stat(absolutePath);
|
|
252
|
+
if (!stats.isDirectory()) {
|
|
253
|
+
throw new Error(`The specified path ${outputDirectoryName} is not a directory/folder.`);
|
|
254
|
+
}
|
|
255
|
+
} catch (error: any) {
|
|
256
|
+
if (error.code === 'ENOENT') {
|
|
257
|
+
await fs.mkdir(absolutePath, { recursive: true });
|
|
258
|
+
log(`The directory did not exist and has been created: ${absolutePath}`);
|
|
259
|
+
} else {
|
|
260
|
+
throw error;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return absolutePath;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Unboxes a contract from `@aztec/boxes` by performing the following operations:
|
|
269
|
+
* 1. Copies the frontend template from `@aztec/boxes/{contract_name}` to the outputDirectory
|
|
270
|
+
* 2. Checks if the contract source was copied over from `@aztec/boxes/{contract_name}/src/contracts`
|
|
271
|
+
* 3. If not, copies the contract from the appropriate `@aztec/noir-contracts/src/contracts/...` folder.
|
|
272
|
+
*
|
|
273
|
+
* The box provides a simple React app which parses the contract ABI
|
|
274
|
+
* and generates a UI to deploy + interact with the contract on a local aztec testnet.
|
|
275
|
+
* @param contractName - name of contract from `@aztec/noir-contracts`, in a format like "PrivateToken" (rather than "private_token", as it appears in the noir-contracts repo)
|
|
276
|
+
* @param log - Logger instance that will output to the CLI
|
|
277
|
+
*/
|
|
278
|
+
export async function unboxContract(
|
|
279
|
+
contractName: string,
|
|
280
|
+
outputDirectoryName: string,
|
|
281
|
+
packageVersion: string,
|
|
282
|
+
log: LogFn,
|
|
283
|
+
) {
|
|
284
|
+
const contractNames = ['PrivateToken'];
|
|
285
|
+
|
|
286
|
+
if (!contractNames.includes(contractName)) {
|
|
287
|
+
log(
|
|
288
|
+
`The noir contract named "${contractName}" was not found in "@aztec/boxes" package. Valid options are:
|
|
289
|
+
${contractNames.join('\n\t')}
|
|
290
|
+
We recommend "PrivateToken" as a default.`,
|
|
291
|
+
);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const outputPath = await createDirectory(outputDirectoryName, log);
|
|
295
|
+
|
|
296
|
+
// downloads the selected contract's relevant folder in @aztec/boxes/{contract_name}
|
|
297
|
+
// and the noir source code from `noir-contracts` into `${outputDirectoryName}/src/contracts`
|
|
298
|
+
// if not present in the box
|
|
299
|
+
await downloadContractAndBoxFromGithub(packageVersion, contractName, outputPath, log);
|
|
300
|
+
// make adjustments for packaging to work as a standalone, as opposed to part of yarn workspace
|
|
301
|
+
// as in the monorepo source files
|
|
302
|
+
await updatePackagingConfigurations(packageVersion, outputPath, log);
|
|
303
|
+
}
|