cli_util.rs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{
  19. collections::{HashMap, HashSet},
  20. io::{stdin, Cursor, Read},
  21. slice,
  22. str::FromStr,
  23. };
  24. use rodio::{Decoder, OutputStreamBuilder, Sink};
  25. use smol::channel::Sender;
  26. use structopt_toml::clap::{App, Arg, Shell, SubCommand};
  27. use darkfi::{
  28. cli_desc,
  29. tx::{ContractCallLeaf, Transaction, TransactionBuilder},
  30. util::{encoding::base64, parse::decode_base10},
  31. zk::Proof,
  32. Error, Result,
  33. };
  34. use darkfi_money_contract::model::TokenId;
  35. use darkfi_sdk::{
  36. crypto::{keypair::Address, pasta_prelude::PrimeField, FuncId, SecretKey},
  37. dark_tree::DarkTree,
  38. pasta::pallas,
  39. ContractCallImport,
  40. };
  41. use darkfi_serial::deserialize_async;
  42. use crate::{money::BALANCE_BASE10_DECIMALS, Drk};
  43. /// Auxiliary function to parse a base64 encoded transaction from stdin.
  44. pub async fn parse_tx_from_stdin() -> Result<Transaction> {
  45. let mut buf = String::new();
  46. stdin().read_to_string(&mut buf)?;
  47. match base64::decode(buf.trim()) {
  48. Some(bytes) => Ok(deserialize_async(&bytes).await?),
  49. None => Err(Error::ParseFailed("Failed to decode transaction")),
  50. }
  51. }
  52. /// Auxiliary function to parse a base64 encoded transaction from
  53. /// provided input or fallback to stdin if its empty.
  54. pub async fn parse_tx_from_input(input: &[String]) -> Result<Transaction> {
  55. match input.len() {
  56. 0 => parse_tx_from_stdin().await,
  57. 1 => match base64::decode(input[0].trim()) {
  58. Some(bytes) => Ok(deserialize_async(&bytes).await?),
  59. None => Err(Error::ParseFailed("Failed to decode transaction")),
  60. },
  61. _ => Err(Error::ParseFailed("Multiline input provided")),
  62. }
  63. }
  64. /// Auxiliary function to parse base64 encoded contract calls from stdin.
  65. pub async fn parse_calls_from_stdin() -> Result<Vec<ContractCallImport>> {
  66. let lines = stdin().lines();
  67. let mut calls = vec![];
  68. for line in lines {
  69. let Some(line) = base64::decode(&line?) else {
  70. return Err(Error::ParseFailed("Failed to decode base64"))
  71. };
  72. calls.push(deserialize_async(&line).await?);
  73. }
  74. Ok(calls)
  75. }
  76. /// Auxiliary function to parse base64 encoded contract calls from
  77. /// provided input or fallback to stdin if its empty.
  78. pub async fn parse_calls_from_input(input: &[String]) -> Result<Vec<ContractCallImport>> {
  79. if input.is_empty() {
  80. return parse_calls_from_stdin().await
  81. }
  82. let mut calls = vec![];
  83. for line in input {
  84. let Some(line) = base64::decode(line) else {
  85. return Err(Error::ParseFailed("Failed to decode base64"))
  86. };
  87. calls.push(deserialize_async(&line).await?);
  88. }
  89. Ok(calls)
  90. }
  91. /// Auxiliary function to parse provided string into a values pair.
  92. pub fn parse_value_pair(s: &str) -> Result<(u64, u64)> {
  93. let v: Vec<&str> = s.split(':').collect();
  94. if v.len() != 2 {
  95. return Err(Error::ParseFailed("Invalid value pair. Use a pair such as 13.37:11.0"))
  96. }
  97. let val0 = decode_base10(v[0], BALANCE_BASE10_DECIMALS, true);
  98. let val1 = decode_base10(v[1], BALANCE_BASE10_DECIMALS, true);
  99. if val0.is_err() || val1.is_err() {
  100. return Err(Error::ParseFailed("Invalid value pair. Use a pair such as 13.37:11.0"))
  101. }
  102. Ok((val0.unwrap(), val1.unwrap()))
  103. }
  104. /// Auxiliary function to parse provided string into a tokens pair.
  105. pub async fn parse_token_pair(drk: &Drk, s: &str) -> Result<(TokenId, TokenId)> {
  106. let v: Vec<&str> = s.split(':').collect();
  107. if v.len() != 2 {
  108. return Err(Error::ParseFailed(
  109. "Invalid token pair. Use a pair such as:\nWCKD:MLDY\nor\n\
  110. A7f1RKsCUUHrSXA7a9ogmwg8p3bs6F47ggsW826HD4yd:FCuoMii64H5Ee4eVWBjP18WTFS8iLUJmGi16Qti1xFQ2"
  111. ))
  112. }
  113. let tok0 = drk.get_token(v[0].to_string()).await;
  114. let tok1 = drk.get_token(v[1].to_string()).await;
  115. if tok0.is_err() || tok1.is_err() {
  116. return Err(Error::ParseFailed(
  117. "Invalid token pair. Use a pair such as:\nWCKD:MLDY\nor\n\
  118. A7f1RKsCUUHrSXA7a9ogmwg8p3bs6F47ggsW826HD4yd:FCuoMii64H5Ee4eVWBjP18WTFS8iLUJmGi16Qti1xFQ2"
  119. ))
  120. }
  121. Ok((tok0.unwrap(), tok1.unwrap()))
  122. }
  123. /// Fun police go away
  124. pub async fn kaching() {
  125. const WALLET_MP3: &[u8] = include_bytes!("../wallet.mp3");
  126. let cursor = Cursor::new(WALLET_MP3);
  127. let Ok(stream_handle) = OutputStreamBuilder::open_default_stream() else { return };
  128. let sink = Sink::connect_new(stream_handle.mixer());
  129. let Ok(source) = Decoder::new(cursor) else { return };
  130. sink.append(source);
  131. sink.detach();
  132. }
  133. /// Auxiliary function to generate provided shell completions.
  134. pub fn generate_completions(shell: &str) -> Result<String> {
  135. // Sub-commands
  136. // Interactive
  137. let interactive = SubCommand::with_name("interactive").about("Enter Drk interactive shell");
  138. // Kaching
  139. let kaching = SubCommand::with_name("kaching").about("Fun");
  140. // Ping
  141. let ping =
  142. SubCommand::with_name("ping").about("Send a ping request to the darkfid RPC endpoint");
  143. // Completions
  144. let shell_arg = Arg::with_name("shell").help("The Shell you want to generate script for");
  145. let completions = SubCommand::with_name("completions")
  146. .about("Generate a SHELL completion script and print to stdout")
  147. .arg(shell_arg);
  148. // Wallet
  149. let initialize = SubCommand::with_name("initialize").about("Initialize wallet database");
  150. let keygen = SubCommand::with_name("keygen").about("Generate a new keypair in the wallet");
  151. let balance = SubCommand::with_name("balance").about("Query the wallet for known balances");
  152. let address = SubCommand::with_name("address").about("Get the default address in the wallet");
  153. let addresses =
  154. SubCommand::with_name("addresses").about("Print all the addresses in the wallet");
  155. let index = Arg::with_name("index").help("Identifier of the address");
  156. let default_address = SubCommand::with_name("default-address")
  157. .about("Set the default address in the wallet")
  158. .arg(index.clone());
  159. let secrets =
  160. SubCommand::with_name("secrets").about("Print all the secret keys from the wallet");
  161. let import_secrets = SubCommand::with_name("import-secrets")
  162. .about("Import secret keys from stdin into the wallet, separated by newlines");
  163. let tree = SubCommand::with_name("tree").about("Print the Merkle tree in the wallet");
  164. let coins = SubCommand::with_name("coins").about("Print all the coins in the wallet");
  165. let spend_hook = Arg::with_name("spend-hook").help("Optional contract spend hook to use");
  166. let user_data = Arg::with_name("user-data").help("Optional user data to use");
  167. let mining_config = SubCommand::with_name("mining-config")
  168. .about("Print a wallet address mining configuration")
  169. .args(&[index, spend_hook.clone(), user_data.clone()]);
  170. let wallet = SubCommand::with_name("wallet").about("Wallet operations").subcommands(vec![
  171. initialize,
  172. keygen,
  173. balance,
  174. address,
  175. addresses,
  176. default_address,
  177. secrets,
  178. import_secrets,
  179. tree,
  180. coins,
  181. mining_config,
  182. ]);
  183. // Spend
  184. let spend = SubCommand::with_name("spend")
  185. .about("Read a transaction from stdin and mark its input coins as spent");
  186. // Unspend
  187. let coin = Arg::with_name("coin").help("base64-encoded coin to mark as unspent");
  188. let unspend = SubCommand::with_name("unspend").about("Unspend a coin").arg(coin);
  189. // Transfer
  190. let amount = Arg::with_name("amount").help("Amount to send");
  191. let token = Arg::with_name("token").help("Token ID to send");
  192. let recipient = Arg::with_name("recipient").help("Recipient address");
  193. let half_split = Arg::with_name("half-split")
  194. .long("half-split")
  195. .help("Split the output coin into two equal halves");
  196. let transfer = SubCommand::with_name("transfer").about("Create a payment transaction").args(&[
  197. amount.clone(),
  198. token.clone(),
  199. recipient.clone(),
  200. spend_hook.clone(),
  201. user_data.clone(),
  202. half_split,
  203. ]);
  204. // Otc
  205. let value_pair = Arg::with_name("value-pair")
  206. .short("v")
  207. .long("value-pair")
  208. .takes_value(true)
  209. .help("Value pair to send:recv (11.55:99.42)");
  210. let token_pair = Arg::with_name("token-pair")
  211. .short("t")
  212. .long("token-pair")
  213. .takes_value(true)
  214. .help("Token pair to send:recv (f00:b4r)");
  215. let init = SubCommand::with_name("init")
  216. .about("Initialize the first half of the atomic swap")
  217. .args(&[value_pair, token_pair]);
  218. let join =
  219. SubCommand::with_name("join").about("Build entire swap tx given the first half from stdin");
  220. let inspect = SubCommand::with_name("inspect")
  221. .about("Inspect a swap half or the full swap tx from stdin");
  222. let sign = SubCommand::with_name("sign").about("Sign a swap transaction given from stdin");
  223. let otc = SubCommand::with_name("otc")
  224. .about("OTC atomic swap")
  225. .subcommands(vec![init, join, inspect, sign]);
  226. // DAO
  227. let proposer_limit = Arg::with_name("proposer-limit")
  228. .help("The minimum amount of governance tokens needed to open a proposal for this DAO");
  229. let quorum = Arg::with_name("quorum")
  230. .help("Minimal threshold of participating total tokens needed for a proposal to pass");
  231. let early_exec_quorum = Arg::with_name("early-exec-quorum")
  232. .help("Minimal threshold of participating total tokens needed for a proposal to be considered as strongly supported, enabling early execution. Must be greater or equal to normal quorum.");
  233. let approval_ratio = Arg::with_name("approval-ratio")
  234. .help("The ratio of winning votes/total votes needed for a proposal to pass (2 decimals)");
  235. let gov_token_id = Arg::with_name("gov-token-id").help("DAO's governance token ID");
  236. let create = SubCommand::with_name("create").about("Create DAO parameters").args(&[
  237. proposer_limit,
  238. quorum,
  239. early_exec_quorum,
  240. approval_ratio,
  241. gov_token_id,
  242. ]);
  243. let view = SubCommand::with_name("view").about("View DAO data from stdin");
  244. let name = Arg::with_name("name").help("Name identifier for the DAO");
  245. let import = SubCommand::with_name("import")
  246. .about("Import DAO data from stdin")
  247. .args(slice::from_ref(&name));
  248. let opt_name = Arg::with_name("dao-alias").help("Name identifier for the DAO (optional)");
  249. let list = SubCommand::with_name("list")
  250. .about("List imported DAOs (or info about a specific one)")
  251. .args(&[opt_name]);
  252. let balance = SubCommand::with_name("balance")
  253. .about("Show the balance of a DAO")
  254. .args(slice::from_ref(&name));
  255. let mint = SubCommand::with_name("mint")
  256. .about("Mint an imported DAO on-chain")
  257. .args(slice::from_ref(&name));
  258. let duration = Arg::with_name("duration").help("Duration of the proposal, in block windows");
  259. let propose_transfer = SubCommand::with_name("propose-transfer")
  260. .about("Create a transfer proposal for a DAO")
  261. .args(&[
  262. name.clone(),
  263. duration.clone(),
  264. amount,
  265. token,
  266. recipient,
  267. spend_hook.clone(),
  268. user_data.clone(),
  269. ]);
  270. let propose_generic = SubCommand::with_name("propose-generic")
  271. .about("Create a generic proposal for a DAO")
  272. .args(&[name.clone(), duration, user_data.clone()]);
  273. let proposals = SubCommand::with_name("proposals").about("List DAO proposals").arg(&name);
  274. let bulla = Arg::with_name("bulla").help("Bulla identifier for the proposal");
  275. let export = Arg::with_name("export").help("Encrypt the proposal and encode it to base64");
  276. let mint_proposal = Arg::with_name("mint-proposal").help("Create the proposal transaction");
  277. let proposal = SubCommand::with_name("proposal").about("View a DAO proposal data").args(&[
  278. bulla.clone(),
  279. export,
  280. mint_proposal,
  281. ]);
  282. let proposal_import = SubCommand::with_name("proposal-import")
  283. .about("Import a base64 encoded and encrypted proposal from stdin");
  284. let vote = Arg::with_name("vote").help("Vote (0 for NO, 1 for YES)");
  285. let vote_weight =
  286. Arg::with_name("vote-weight").help("Optional vote weight (amount of governance tokens)");
  287. let vote = SubCommand::with_name("vote").about("Vote on a given proposal").args(&[
  288. bulla.clone(),
  289. vote,
  290. vote_weight,
  291. ]);
  292. let early = Arg::with_name("early").long("early").help("Execute the proposal early");
  293. let exec = SubCommand::with_name("exec").about("Execute a DAO proposal").args(&[bulla, early]);
  294. let spend_hook_cmd = SubCommand::with_name("spend-hook")
  295. .about("Print the DAO contract base64-encoded spend hook");
  296. let mining_config =
  297. SubCommand::with_name("mining-config").about("Print a DAO mining configuration").arg(name);
  298. let dao = SubCommand::with_name("dao").about("DAO functionalities").subcommands(vec![
  299. create,
  300. view,
  301. import,
  302. list,
  303. balance,
  304. mint,
  305. propose_transfer,
  306. propose_generic,
  307. proposals,
  308. proposal,
  309. proposal_import,
  310. vote,
  311. exec,
  312. spend_hook_cmd,
  313. mining_config,
  314. ]);
  315. // AttachFee
  316. let attach_fee = SubCommand::with_name("attach-fee")
  317. .about("Attach the fee call to a transaction given from stdin");
  318. // TxFromCalls
  319. let calls_map =
  320. Arg::with_name("calls-map").help("Optional parent/children dependency map for the calls");
  321. let tx_from_calls = SubCommand::with_name("tx-from-calls")
  322. .about(
  323. "Create a transaction from newline-separated calls from stdin and attach the fee call",
  324. )
  325. .args(&[calls_map]);
  326. // Inspect
  327. let inspect = SubCommand::with_name("inspect").about("Inspect a transaction from stdin");
  328. // Broadcast
  329. let broadcast =
  330. SubCommand::with_name("broadcast").about("Read a transaction from stdin and broadcast it");
  331. // Scan
  332. let reset = Arg::with_name("reset")
  333. .long("reset")
  334. .help("Reset wallet state to provided block height and start scanning");
  335. let scan = SubCommand::with_name("scan")
  336. .about("Scan the blockchain and parse relevant transactions")
  337. .args(&[reset]);
  338. // Explorer
  339. let tx_hash = Arg::with_name("tx-hash").help("Transaction hash");
  340. let encode = Arg::with_name("encode").long("encode").help("Encode transaction to base64");
  341. let fetch_tx = SubCommand::with_name("fetch-tx")
  342. .about("Fetch a blockchain transaction by hash")
  343. .args(&[tx_hash, encode]);
  344. let simulate_tx =
  345. SubCommand::with_name("simulate-tx").about("Read a transaction from stdin and simulate it");
  346. let tx_hash = Arg::with_name("tx-hash").help("Fetch specific history record (optional)");
  347. let encode = Arg::with_name("encode")
  348. .long("encode")
  349. .help("Encode specific history record transaction to base64");
  350. let txs_history = SubCommand::with_name("txs-history")
  351. .about("Fetch broadcasted transactions history")
  352. .args(&[tx_hash, encode]);
  353. let clear_reverted =
  354. SubCommand::with_name("clear-reverted").about("Remove reverted transactions from history");
  355. let height = Arg::with_name("height").help("Fetch specific height record (optional)");
  356. let scanned_blocks = SubCommand::with_name("scanned-blocks")
  357. .about("Fetch scanned blocks records")
  358. .args(&[height]);
  359. let mining_config = SubCommand::with_name("mining-config")
  360. .about("Read a mining configuration from stdin and display its parts");
  361. let explorer =
  362. SubCommand::with_name("explorer").about("Explorer related subcommands").subcommands(vec![
  363. fetch_tx,
  364. simulate_tx,
  365. txs_history,
  366. clear_reverted,
  367. scanned_blocks,
  368. mining_config,
  369. ]);
  370. // Alias
  371. let alias = Arg::with_name("alias").help("Token alias");
  372. let token = Arg::with_name("token").help("Token to create alias for");
  373. let add = SubCommand::with_name("add").about("Create a Token alias").args(&[alias, token]);
  374. let alias = Arg::with_name("alias")
  375. .short("a")
  376. .long("alias")
  377. .takes_value(true)
  378. .help("Token alias to search for");
  379. let token = Arg::with_name("token")
  380. .short("t")
  381. .long("token")
  382. .takes_value(true)
  383. .help("Token to search alias for");
  384. let show = SubCommand::with_name("show")
  385. .about(
  386. "Print alias info of optional arguments. \
  387. If no argument is provided, list all the aliases in the wallet.",
  388. )
  389. .args(&[alias, token]);
  390. let alias = Arg::with_name("alias").help("Token alias to remove");
  391. let remove = SubCommand::with_name("remove").about("Remove a Token alias").arg(alias);
  392. let alias = SubCommand::with_name("alias")
  393. .about("Manage Token aliases")
  394. .subcommands(vec![add, show, remove]);
  395. // Token
  396. let secret_key = Arg::with_name("secret-key").help("Mint authority secret key");
  397. let token_blind = Arg::with_name("token-blind").help("Mint authority token blind");
  398. let import = SubCommand::with_name("import")
  399. .about("Import a mint authority")
  400. .args(&[secret_key, token_blind]);
  401. let generate_mint =
  402. SubCommand::with_name("generate-mint").about("Generate a new mint authority");
  403. let list =
  404. SubCommand::with_name("list").about("List token IDs with available mint authorities");
  405. let token = Arg::with_name("token").help("Token ID to mint");
  406. let amount = Arg::with_name("amount").help("Amount to mint");
  407. let recipient = Arg::with_name("recipient").help("Recipient of the minted tokens");
  408. let mint = SubCommand::with_name("mint")
  409. .about("Mint tokens")
  410. .args(&[token, amount, recipient, spend_hook, user_data]);
  411. let token = Arg::with_name("token").help("Token ID to freeze");
  412. let freeze = SubCommand::with_name("freeze").about("Freeze a token mint").arg(token);
  413. let token = SubCommand::with_name("token").about("Token functionalities").subcommands(vec![
  414. import,
  415. generate_mint,
  416. list,
  417. mint,
  418. freeze,
  419. ]);
  420. // Contract
  421. let generate_deploy =
  422. SubCommand::with_name("generate-deploy").about("Generate a new deploy authority");
  423. let contract_id = Arg::with_name("contract-id").help("Contract ID (optional)");
  424. let list = SubCommand::with_name("list")
  425. .about("List deploy authorities in the wallet (or a specific one)")
  426. .args(&[contract_id]);
  427. let tx_hash = Arg::with_name("tx-hash").help("Record transaction hash");
  428. let export_data = SubCommand::with_name("export-data")
  429. .about("Export a contract history record wasm bincode and deployment instruction, encoded to base64")
  430. .args(&[tx_hash]);
  431. let deploy_auth = Arg::with_name("deploy-auth").help("Contract ID (deploy authority)");
  432. let wasm_path = Arg::with_name("wasm-path").help("Path to contract wasm bincode");
  433. let deploy_ix =
  434. Arg::with_name("deploy-ix").help("Optional path to serialized deploy instruction");
  435. let deploy = SubCommand::with_name("deploy").about("Deploy a smart contract").args(&[
  436. deploy_auth.clone(),
  437. wasm_path,
  438. deploy_ix,
  439. ]);
  440. let lock = SubCommand::with_name("lock").about("Lock a smart contract").args(&[deploy_auth]);
  441. let contract = SubCommand::with_name("contract")
  442. .about("Contract functionalities")
  443. .subcommands(vec![generate_deploy, list, export_data, deploy, lock]);
  444. // Main arguments
  445. let config = Arg::with_name("config")
  446. .short("c")
  447. .long("config")
  448. .takes_value(true)
  449. .help("Configuration file to use");
  450. let network = Arg::with_name("network")
  451. .long("network")
  452. .takes_value(true)
  453. .help("Blockchain network to use");
  454. let command = vec![
  455. interactive,
  456. kaching,
  457. ping,
  458. completions,
  459. wallet,
  460. spend,
  461. unspend,
  462. transfer,
  463. otc,
  464. attach_fee,
  465. tx_from_calls,
  466. inspect,
  467. broadcast,
  468. dao,
  469. scan,
  470. explorer,
  471. alias,
  472. token,
  473. contract,
  474. ];
  475. let fun = Arg::with_name("fun")
  476. .short("f")
  477. .long("fun")
  478. .help("Flag indicating whether you want some fun in your life");
  479. let log = Arg::with_name("log")
  480. .short("l")
  481. .long("log")
  482. .takes_value(true)
  483. .help("Set log file to ouput into");
  484. let verbose = Arg::with_name("verbose")
  485. .short("v")
  486. .multiple(true)
  487. .help("Increase verbosity (-vvv supported)");
  488. let mut app = App::new("drk")
  489. .about(cli_desc!())
  490. .args(&[config, network, fun, log, verbose])
  491. .subcommands(command);
  492. let shell = match Shell::from_str(shell) {
  493. Ok(s) => s,
  494. Err(e) => return Err(Error::Custom(e)),
  495. };
  496. let mut buf = vec![];
  497. app.gen_completions_to("./drk", shell, &mut buf);
  498. Ok(String::from_utf8(buf)?)
  499. }
  500. /// Auxiliary function to print provided string buffer.
  501. pub fn print_output(buf: &[String]) {
  502. for line in buf {
  503. println!("{line}");
  504. }
  505. }
  506. /// Auxiliary function to print or insert provided messages to given
  507. /// buffer reference. If a channel sender is provided, the messages
  508. /// are send to that instead.
  509. pub async fn append_or_print(
  510. buf: &mut Vec<String>,
  511. sender: Option<&Sender<Vec<String>>>,
  512. print: &bool,
  513. messages: Vec<String>,
  514. ) {
  515. // Send the messages to the channel, if provided
  516. if let Some(sender) = sender {
  517. if let Err(e) = sender.send(messages).await {
  518. let err_msg = format!("[append_or_print] Sending messages to channel failed: {e}");
  519. if *print {
  520. println!("{err_msg}");
  521. } else {
  522. buf.push(err_msg);
  523. }
  524. }
  525. return
  526. }
  527. // Print the messages
  528. if *print {
  529. for msg in messages {
  530. println!("{msg}");
  531. }
  532. return
  533. }
  534. // Insert the messages in the buffer
  535. for msg in messages {
  536. buf.push(msg);
  537. }
  538. }
  539. /// Auxiliary function to parse a base64 encoded mining configuration
  540. /// from stdin.
  541. pub async fn parse_mining_config_from_stdin(
  542. ) -> Result<(String, String, Option<String>, Option<String>)> {
  543. let mut buf = String::new();
  544. stdin().read_to_string(&mut buf)?;
  545. let config = buf.trim();
  546. let (recipient, spend_hook, user_data) = match base64::decode(config) {
  547. Some(bytes) => deserialize_async(&bytes).await?,
  548. None => return Err(Error::ParseFailed("Failed to decode mining configuration")),
  549. };
  550. Ok((config.to_string(), recipient, spend_hook, user_data))
  551. }
  552. /// Auxiliary function to parse a base64 encoded mining configuration
  553. /// from provided input or fallback to stdin if its empty.
  554. pub async fn parse_mining_config_from_input(
  555. input: &[String],
  556. ) -> Result<(String, String, Option<String>, Option<String>)> {
  557. match input.len() {
  558. 0 => parse_mining_config_from_stdin().await,
  559. 1 => {
  560. let config = input[0].trim();
  561. let (recipient, spend_hook, user_data) = match base64::decode(config) {
  562. Some(bytes) => deserialize_async(&bytes).await?,
  563. None => return Err(Error::ParseFailed("Failed to decode mining configuration")),
  564. };
  565. Ok((config.to_string(), recipient, spend_hook, user_data))
  566. }
  567. _ => Err(Error::ParseFailed("Multiline input provided")),
  568. }
  569. }
  570. /// Auxiliary function to display the parts of a mining configuration.
  571. pub fn display_mining_config(
  572. config: &str,
  573. recipient_str: &str,
  574. spend_hook: &Option<String>,
  575. user_data: &Option<String>,
  576. output: &mut Vec<String>,
  577. ) {
  578. output.push(format!("DarkFi mining configuration address: {config}"));
  579. match Address::from_str(recipient_str) {
  580. Ok(recipient) => {
  581. output.push(format!("Recipient: {recipient_str}"));
  582. output.push(format!("Public key: {}", recipient.public_key()));
  583. output.push(format!("Network: {:?}", recipient.network()));
  584. }
  585. Err(e) => output.push(format!("Recipient: Invalid ({e})")),
  586. }
  587. let spend_hook = match spend_hook {
  588. Some(spend_hook_str) => match FuncId::from_str(spend_hook_str) {
  589. Ok(_) => String::from(spend_hook_str),
  590. Err(e) => format!("Invalid ({e})"),
  591. },
  592. None => String::from("-"),
  593. };
  594. output.push(format!("Spend hook: {spend_hook}"));
  595. let user_data = match user_data {
  596. Some(user_data_str) => match bs58::decode(&user_data_str).into_vec() {
  597. Ok(bytes) => match bytes.try_into() {
  598. Ok(bytes) => {
  599. if pallas::Base::from_repr(bytes).is_some().into() {
  600. String::from(user_data_str)
  601. } else {
  602. String::from("Invalid")
  603. }
  604. }
  605. Err(e) => format!("Invalid ({e:?})"),
  606. },
  607. Err(e) => format!("Invalid ({e})"),
  608. },
  609. None => String::from("-"),
  610. };
  611. output.push(format!("User data: {user_data}"));
  612. }
  613. /// Cast `ContractCallImport` to `ContractCallLeaf`
  614. fn to_leaf(call: &ContractCallImport) -> ContractCallLeaf {
  615. ContractCallLeaf {
  616. call: call.call().clone(),
  617. proofs: call.proofs().iter().map(|p| Proof::new(p.clone())).collect(),
  618. }
  619. }
  620. /// Recursively build subtree for a DarkTree
  621. fn build_subtree(
  622. idx: usize,
  623. calls: &[ContractCallImport],
  624. children_map: &HashMap<usize, &Vec<usize>>,
  625. ) -> DarkTree<ContractCallLeaf> {
  626. let children_idx = children_map.get(&idx).map(|v| v.as_slice()).unwrap_or(&[]);
  627. let children: Vec<DarkTree<ContractCallLeaf>> =
  628. children_idx.iter().map(|&i| build_subtree(i, calls, children_map)).collect();
  629. DarkTree::new(to_leaf(&calls[idx]), children, None, None)
  630. }
  631. /// Build a `Transaction` given a slice of calls and their mapping
  632. pub fn tx_from_calls_mapped(
  633. calls: &[ContractCallImport],
  634. map: &[(usize, Vec<usize>)],
  635. ) -> Result<(TransactionBuilder, Vec<SecretKey>)> {
  636. assert_eq!(calls.len(), map.len());
  637. let signature_secrets: Vec<SecretKey> =
  638. calls.iter().flat_map(|c| c.secrets().to_vec()).collect();
  639. let children_map: HashMap<usize, &Vec<usize>> = map.iter().map(|(k, v)| (*k, v)).collect();
  640. let (root_idx, root_children_idx) = &map[0];
  641. let root_children: Vec<DarkTree<ContractCallLeaf>> =
  642. root_children_idx.iter().map(|&i| build_subtree(i, calls, &children_map)).collect();
  643. let tx_builder = TransactionBuilder::new(to_leaf(&calls[*root_idx]), root_children)?;
  644. Ok((tx_builder, signature_secrets))
  645. }
  646. /// Auxiliary function to parse a contract call mapping.
  647. ///
  648. /// The mapping is in the format of `{0: [1,2], 1: [], 2:[3], 3:[]}`.
  649. /// It supports nesting and this kind of logic as expected.
  650. ///
  651. /// Errors out if there are non-unique keys or cyclic references.
  652. pub fn parse_tree(input: &str) -> std::result::Result<Vec<(usize, Vec<usize>)>, String> {
  653. let s = input
  654. .trim()
  655. .strip_prefix('{')
  656. .and_then(|s| s.strip_suffix('}'))
  657. .ok_or("expected {}")?
  658. .trim();
  659. let mut entries = vec![];
  660. let mut seen_keys = HashSet::new();
  661. if s.is_empty() {
  662. return Ok(entries)
  663. }
  664. let mut rest = s;
  665. while !rest.is_empty() {
  666. // Parse key
  667. let (key_str, after_key) = rest.split_once(':').ok_or("expected ':'")?;
  668. let key: usize = key_str.trim().parse().map_err(|_| "invalid key")?;
  669. if !seen_keys.insert(key) {
  670. return Err(format!("duplicate key: {}", key));
  671. }
  672. // Parse array
  673. let after_key = after_key.trim();
  674. let arr_start = after_key.strip_prefix('[').ok_or("expected '['")?;
  675. let (arr_content, after_arr) = arr_start.split_once(']').ok_or("expected ']'")?;
  676. let children: Vec<usize> = arr_content
  677. .split(',')
  678. .map(|s| s.trim())
  679. .filter(|s| !s.is_empty())
  680. .map(|s| s.parse().map_err(|_| "invalid child"))
  681. .collect::<std::result::Result<_, _>>()?;
  682. entries.push((key, children));
  683. // Move to next entry
  684. rest = after_arr.trim().strip_prefix(',').unwrap_or(after_arr).trim();
  685. }
  686. check_cycles(&entries)?;
  687. Ok(entries)
  688. }
  689. fn check_cycles(entries: &[(usize, Vec<usize>)]) -> std::result::Result<(), String> {
  690. let graph: HashMap<usize, &Vec<usize>> = entries.iter().map(|(k, v)| (*k, v)).collect();
  691. let mut visited = HashSet::new();
  692. let mut path = Vec::new();
  693. fn dfs(
  694. node: usize,
  695. graph: &HashMap<usize, &Vec<usize>>,
  696. visited: &mut HashSet<usize>,
  697. path: &mut Vec<usize>,
  698. ) -> std::result::Result<(), String> {
  699. if let Some(pos) = path.iter().position(|&n| n == node) {
  700. let cycle: Vec<_> = path[pos..].iter().chain(&[node]).map(|n| n.to_string()).collect();
  701. return Err(format!("cycle detected: {}", cycle.join(" -> ")));
  702. }
  703. if visited.contains(&node) {
  704. return Ok(());
  705. }
  706. path.push(node);
  707. if let Some(children) = graph.get(&node) {
  708. for &child in *children {
  709. dfs(child, graph, visited, path)?;
  710. }
  711. }
  712. path.pop();
  713. visited.insert(node);
  714. Ok(())
  715. }
  716. for &(key, _) in entries {
  717. dfs(key, &graph, &mut visited, &mut path)?;
  718. }
  719. Ok(())
  720. }
  721. #[cfg(test)]
  722. mod tests {
  723. use super::*;
  724. #[test]
  725. fn test_parse_tree() {
  726. // Valid inputs
  727. assert_eq!(parse_tree("{}").unwrap(), vec![]);
  728. assert_eq!(parse_tree("{ }").unwrap(), vec![]);
  729. assert_eq!(parse_tree("{ 0: [] }").unwrap(), vec![(0, vec![])]);
  730. assert_eq!(parse_tree("{ 0: [1, 2, 3] }").unwrap(), vec![(0, vec![1, 2, 3])]);
  731. assert_eq!(parse_tree("{0:[],1:[2]}").unwrap(), vec![(0, vec![]), (1, vec![2])]);
  732. assert_eq!(parse_tree("{ 0: [], 1: [], }").unwrap(), vec![(0, vec![]), (1, vec![])]);
  733. assert_eq!(parse_tree("{ 0: [1, 2,] }").unwrap(), vec![(0, vec![1, 2])]);
  734. assert_eq!(
  735. parse_tree("{ 0: [], 1: [2, 3], 2: [], 3: [4], 4: [] }").unwrap(),
  736. vec![(0, vec![]), (1, vec![2, 3]), (2, vec![]), (3, vec![4]), (4, vec![])]
  737. );
  738. assert_eq!(
  739. parse_tree("{ 0 : [ ] , 1 : [ 2 , 3 ] }").unwrap(),
  740. vec![(0, vec![]), (1, vec![2, 3])]
  741. );
  742. assert_eq!(
  743. parse_tree("{ 999: [1000, 1001], 1000: [], 1001: [] }").unwrap(),
  744. vec![(999, vec![1000, 1001]), (1000, vec![]), (1001, vec![])]
  745. );
  746. // Order preservation
  747. let keys: Vec<usize> =
  748. parse_tree("{ 5: [], 2: [], 9: [], 0: [] }").unwrap().iter().map(|(k, _)| *k).collect();
  749. assert_eq!(keys, vec![5, 2, 9, 0]);
  750. // Valid DAG (not a cycle)
  751. assert!(parse_tree("{ 0: [1, 2], 1: [3], 2: [3], 3: [] }").is_ok());
  752. // Syntax errors
  753. assert!(parse_tree("0: [] }").is_err());
  754. assert!(parse_tree("{ 0: []").is_err());
  755. assert!(parse_tree("{ 0 [] }").is_err());
  756. assert!(parse_tree("{ 0: ] }").is_err());
  757. assert!(parse_tree("{ 0: [1, 2 }").is_err());
  758. assert!(parse_tree("{ abc: [] }").is_err());
  759. assert!(parse_tree("{ 0: [abc] }").is_err());
  760. assert!(parse_tree("{ -1: [] }").is_err());
  761. // Duplicate keys
  762. assert!(parse_tree("{ 0: [], 0: [1] }").unwrap_err().contains("duplicate key: 0"));
  763. assert!(parse_tree("{ 0: [], 1: [], 2: [], 1: [] }")
  764. .unwrap_err()
  765. .contains("duplicate key: 1"));
  766. // Cycle detection
  767. let err = parse_tree("{ 0: [0] }").unwrap_err();
  768. assert!(err.contains("cycle detected") && err.contains("0 -> 0"));
  769. let err = parse_tree("{ 0: [1], 1: [0] }").unwrap_err();
  770. assert!(err.contains("cycle detected"));
  771. let err = parse_tree("{ 0: [1], 1: [2], 2: [3], 3: [0] }").unwrap_err();
  772. assert!(err.contains("cycle detected") && err.contains("0 -> 1 -> 2 -> 3 -> 0"));
  773. let err = parse_tree("{ 0: [1], 1: [2], 2: [3], 3: [2] }").unwrap_err();
  774. assert!(err.contains("cycle detected") && err.contains("2 -> 3 -> 2"));
  775. }
  776. }