@stacks/rendezvous 0.4.1 → 0.6.0

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/dist/app.js CHANGED
@@ -9,11 +9,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  step((generator = generator.apply(thisArg, _arguments || [])).next());
10
10
  });
11
11
  };
12
+ var __importDefault = (this && this.__importDefault) || function (mod) {
13
+ return (mod && mod.__esModule) ? mod : { "default": mod };
14
+ };
12
15
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.getManifestFileName = void 0;
16
+ exports.tryParseRemoteDataSettings = exports.getManifestFileName = exports.invalidRemoteDataErrorMessage = exports.noRemoteData = void 0;
14
17
  exports.main = main;
15
18
  const path_1 = require("path");
16
19
  const events_1 = require("events");
20
+ const toml_1 = __importDefault(require("toml"));
17
21
  const property_1 = require("./property");
18
22
  const invariant_1 = require("./invariant");
19
23
  const shared_1 = require("./shared");
@@ -25,6 +29,16 @@ const dialer_1 = require("./dialer");
25
29
  const logger = (log, logLevel = "log") => {
26
30
  console[logLevel](log);
27
31
  };
32
+ /**
33
+ * The object used to initialize an empty simnet session with, when no remote
34
+ * data is enabled in the `Clarinet.toml` file.
35
+ */
36
+ exports.noRemoteData = {
37
+ enabled: false,
38
+ api_url: "",
39
+ initial_height: 1,
40
+ };
41
+ exports.invalidRemoteDataErrorMessage = `Remote data settings not properly setup in Clarinet.toml! To use remote data, please provide the "api_url", "initial_height", and "enabled" fields under the "repl.remote_data" section in the Clarinet.toml file.`;
28
42
  /**
29
43
  * Gets the manifest file name for a Clarinet project.
30
44
  * If a custom manifest exists (`Clarinet-<contract-name>.toml`), it is used.
@@ -41,6 +55,22 @@ const getManifestFileName = (manifestDir, targetContractName) => {
41
55
  return "Clarinet.toml";
42
56
  };
43
57
  exports.getManifestFileName = getManifestFileName;
58
+ const tryParseRemoteDataSettings = (manifestPath, radio) => {
59
+ var _a, _b;
60
+ const clarinetToml = toml_1.default.parse((0, fs_1.readFileSync)((0, path_1.resolve)(manifestPath), "utf-8"));
61
+ const remoteDataUserSettings = (_b = (_a = clarinetToml.repl) === null || _a === void 0 ? void 0 : _a["remote_data"]) !== null && _b !== void 0 ? _b : undefined;
62
+ if (remoteDataUserSettings !== undefined &&
63
+ (!(remoteDataUserSettings === null || remoteDataUserSettings === void 0 ? void 0 : remoteDataUserSettings["api_url"]) ||
64
+ !(remoteDataUserSettings === null || remoteDataUserSettings === void 0 ? void 0 : remoteDataUserSettings["enabled"]) ||
65
+ !(remoteDataUserSettings === null || remoteDataUserSettings === void 0 ? void 0 : remoteDataUserSettings["initial_height"]))) {
66
+ throw new Error(exports.invalidRemoteDataErrorMessage);
67
+ }
68
+ if (remoteDataUserSettings) {
69
+ radio.emit("logMessage", (0, ansicolor_1.yellow)("\nUsing remote data. Setting up the environment can take up to a minute..."));
70
+ }
71
+ return remoteDataUserSettings || exports.noRemoteData;
72
+ };
73
+ exports.tryParseRemoteDataSettings = tryParseRemoteDataSettings;
44
74
  const helpMessage = `
45
75
  rv v${package_json_1.version}
46
76
 
@@ -130,7 +160,8 @@ function main() {
130
160
  if (dialerRegistry !== undefined) {
131
161
  dialerRegistry.registerDialers();
132
162
  }
133
- const simnet = yield (0, citizen_1.issueFirstClassCitizenship)(manifestDir, manifestPath, sutContractName);
163
+ const remoteDataSettings = (0, exports.tryParseRemoteDataSettings)(manifestPath, radio);
164
+ const simnet = yield (0, citizen_1.issueFirstClassCitizenship)(manifestDir, manifestPath, remoteDataSettings, sutContractName);
134
165
  /**
135
166
  * The list of contract IDs for the SUT contract names, as per the simnet.
136
167
  */
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/citizen.js CHANGED
@@ -29,11 +29,12 @@ const transactions_1 = require("@stacks/transactions");
29
29
  *
30
30
  * @param manifestDir The relative path to the manifest directory.
31
31
  * @param manifestPath The absolute path to the manifest file.
32
+ * @param remoteDataSettings The remote data settings.
32
33
  * @param sutContractName The target contract name.
33
34
  * @returns The initialized simnet instance with all contracts deployed, with
34
35
  * the test contract treated as a first-class citizen of the target contract.
35
36
  */
36
- const issueFirstClassCitizenship = (manifestDir, manifestPath, sutContractName) => __awaiter(void 0, void 0, void 0, function* () {
37
+ const issueFirstClassCitizenship = (manifestDir, manifestPath, remoteDataSettings, sutContractName) => __awaiter(void 0, void 0, void 0, function* () {
37
38
  // Initialize the simnet, to generate the simnet plan and instance. The empty
38
39
  // session will be set up, and contracts will be deployed in the correct
39
40
  // order based on the simnet plan a few lines below.
@@ -43,13 +44,25 @@ const issueFirstClassCitizenship = (manifestDir, manifestPath, sutContractName)
43
44
  }));
44
45
  const sortedContractsByEpoch = (0, exports.groupContractsByEpochFromSimnetPlan)(simnetPlan);
45
46
  const simnetAddresses = [...simnet.getAccounts().values()];
46
- const balancesMap = new Map(Array.from(simnetAddresses, (address) => {
47
+ const stxBalancesMap = new Map(simnetAddresses.map((address) => {
47
48
  const balanceHex = simnet.runSnippet(`(stx-get-balance '${address})`);
48
49
  return [address, (0, transactions_1.cvToValue)((0, transactions_1.hexToCV)(balanceHex))];
49
50
  }));
50
- yield simnet.initEmptySession();
51
+ const sbtcBalancesMap = new Map(simnetAddresses.map((address) => {
52
+ try {
53
+ const { result: getBalanceResult } = simnet.callReadOnlyFn("SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token", "get-balance", [transactions_1.Cl.principal(address)], address);
54
+ // If the previous read-only call works, the user is working with
55
+ // sBTC. This means we can proceed with restoring sBTC balances.
56
+ const sbtcBalance = (0, transactions_1.cvToJSON)(getBalanceResult).value.value;
57
+ return [address, sbtcBalance];
58
+ }
59
+ catch (e) {
60
+ return [address, 0];
61
+ }
62
+ }));
63
+ yield simnet.initEmptySession(remoteDataSettings);
51
64
  simnetAddresses.forEach((address) => {
52
- simnet.mintSTX(address, balancesMap.get(address));
65
+ simnet.mintSTX(address, stxBalancesMap.get(address));
53
66
  });
54
67
  // Combine the target contract with its tests into a single contract. The
55
68
  // resulting contract will replace the target contract in the simnet.
@@ -62,6 +75,14 @@ const issueFirstClassCitizenship = (manifestDir, manifestPath, sutContractName)
62
75
  ]));
63
76
  // Deploy the contracts to the simnet in the correct order.
64
77
  yield deployContracts(simnet, sortedContractsByEpoch, (name, props) => (0, exports.getContractSource)([sutContractName], rendezvousSources, name, props, manifestDir));
78
+ // Filter out addresses with zero balance. They do not need to be restored.
79
+ const sbtcBalancesToRestore = new Map([...sbtcBalancesMap.entries()].filter(([_, balance]) => balance !== 0));
80
+ // After all the contracts and requirements are deployed, if the test wallets
81
+ // had sBTC balances previously, restore them. If no test wallet previously
82
+ // owned sBTC, skip this step.
83
+ if ([...sbtcBalancesToRestore.keys()].length > 0) {
84
+ restoreSbtcBalances(simnet, sbtcBalancesToRestore);
85
+ }
65
86
  return simnet;
66
87
  });
67
88
  exports.issueFirstClassCitizenship = issueFirstClassCitizenship;
@@ -239,3 +260,85 @@ function scheduleRendezvous(targetContractSource, tests) {
239
260
  (ok (map-set context function-name {called: called})))`;
240
261
  return `${targetContractSource}\n\n${context}\n\n${tests}`;
241
262
  }
263
+ /**
264
+ * Utility function that restores the test wallets' initial sBTC balances in
265
+ * the re-initialized first-class citizenship simnet.
266
+ *
267
+ * @param simnet The simnet instance.
268
+ * @param sbtcBalancesMap A map containing the test wallets' balances to be
269
+ * restored.
270
+ */
271
+ const restoreSbtcBalances = (simnet, sbtcBalancesMap) => {
272
+ // For each address present in the balances map, restore the balance.
273
+ [...sbtcBalancesMap.entries()]
274
+ // Re-assure the map does not contain nil balances.
275
+ .filter(([_, balance]) => balance !== 0)
276
+ .forEach(([address, balance]) => {
277
+ // To deposit sBTC, one needs a txId and a sweep txId. A deposit transaction
278
+ // must have a unique txId and sweep txId.
279
+ const txId = getUniqueHex();
280
+ const sweepTxId = getUniqueHex();
281
+ mintSbtc(simnet, balance, address, txId, sweepTxId);
282
+ });
283
+ };
284
+ /**
285
+ * Utility function to deposit an amount of sBTC to a Stacks address.
286
+ *
287
+ * @param simnet The simnet instance.
288
+ * @param amountSats The amount to mint in sats.
289
+ * @param recipient The Stacks address to mint sBTC to.
290
+ * @param txId A unique hex to use for the deposit.
291
+ * @param sweepTxId A unique hex to use for the deposit.
292
+ */
293
+ const mintSbtc = (simnet, amountSats, recipient, txId, sweepTxId) => {
294
+ // Calling `get-burn-header` only works for past block heights. We mine one
295
+ // empty Stacks block if the initial height is 0.
296
+ if (simnet.blockHeight === 0) {
297
+ simnet.mineEmptyBlock();
298
+ }
299
+ const previousStacksHeight = simnet.blockHeight - 1;
300
+ const burnHash = simnet.callReadOnlyFn("SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-deposit", "get-burn-header", [
301
+ // (height uint)
302
+ transactions_1.Cl.uint(previousStacksHeight),
303
+ ], simnet.deployer).result;
304
+ if (burnHash === null || burnHash.type === transactions_1.ClarityType.OptionalNone) {
305
+ throw new Error("Something went wrong trying to retrieve the burn header.");
306
+ }
307
+ const completeDepositTx = (0, transactions_1.cvToJSON)(simnet.callPublicFn("SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-deposit", "complete-deposit-wrapper", [
308
+ // (txid (buff 32))
309
+ transactions_1.Cl.bufferFromHex(txId),
310
+ // (vout-index uint)
311
+ transactions_1.Cl.uint(1),
312
+ // (amount uint)
313
+ transactions_1.Cl.uint(amountSats),
314
+ // (recipient principal)
315
+ transactions_1.Cl.principal(recipient),
316
+ // (burn-hash (buff 32))
317
+ burnHash.value,
318
+ // (burn-height uint)
319
+ transactions_1.Cl.uint(previousStacksHeight),
320
+ // (sweep-txid (buff 32))
321
+ transactions_1.Cl.bufferFromHex(sweepTxId),
322
+ ], "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4").result);
323
+ // If the deposit transaction fails, an unexpected outcome can happen. Throw
324
+ // an error if the transaction is not successful.
325
+ if (!completeDepositTx.success) {
326
+ throw new Error("Something went wrong trying to restore sBTC balances.");
327
+ }
328
+ };
329
+ /**
330
+ * Utility function that generates a random, unique hex to be used as txId in
331
+ * `mintSbtc`.
332
+ *
333
+ * @returns A random hex string.
334
+ */
335
+ const getUniqueHex = () => {
336
+ let hex;
337
+ // Generate a 32-byte (64 character) random hex string.
338
+ const bytes = new Uint8Array(32);
339
+ crypto.getRandomValues(bytes);
340
+ hex = Array.from(bytes)
341
+ .map((byte) => byte.toString(16).padStart(2, "0"))
342
+ .join("");
343
+ return hex;
344
+ };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stacks/rendezvous",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "description": "Meet your contract's vulnerabilities head-on.",
5
5
  "main": "app.js",
6
6
  "bin": {
@@ -30,14 +30,15 @@
30
30
  "url": "https://github.com/stacks-network/rendezvous.git"
31
31
  },
32
32
  "dependencies": {
33
- "@hirosystems/clarinet-sdk": "2.12.0",
33
+ "@hirosystems/clarinet-sdk": "2.15.2",
34
34
  "@stacks/transactions": "^6.16.1",
35
35
  "ansicolor": "^2.0.3",
36
36
  "fast-check": "^3.20.0",
37
+ "toml": "^3.0.0",
37
38
  "yaml": "^2.6.1"
38
39
  },
39
40
  "devDependencies": {
40
- "@hirosystems/clarinet-sdk-wasm": "2.12.0",
41
+ "@hirosystems/clarinet-sdk-wasm": "2.15.2",
41
42
  "@types/jest": "^29.5.12",
42
43
  "jest": "^29.7.0",
43
44
  "ts-jest": "^29.2.5",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stacks/rendezvous",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "description": "Meet your contract's vulnerabilities head-on.",
5
5
  "main": "app.js",
6
6
  "bin": {
@@ -30,14 +30,15 @@
30
30
  "url": "https://github.com/stacks-network/rendezvous.git"
31
31
  },
32
32
  "dependencies": {
33
- "@hirosystems/clarinet-sdk": "2.12.0",
33
+ "@hirosystems/clarinet-sdk": "2.15.2",
34
34
  "@stacks/transactions": "^6.16.1",
35
35
  "ansicolor": "^2.0.3",
36
36
  "fast-check": "^3.20.0",
37
+ "toml": "^3.0.0",
37
38
  "yaml": "^2.6.1"
38
39
  },
39
40
  "devDependencies": {
40
- "@hirosystems/clarinet-sdk-wasm": "2.12.0",
41
+ "@hirosystems/clarinet-sdk-wasm": "2.15.2",
41
42
  "@types/jest": "^29.5.12",
42
43
  "jest": "^29.7.0",
43
44
  "ts-jest": "^29.2.5",