| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595 |
- /* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2024 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- use std::{
- io::{stdin, Read},
- process::exit,
- str::FromStr,
- sync::Arc,
- };
- use prettytable::{format, row, Table};
- use rand::rngs::OsRng;
- use smol::{fs::read_to_string, stream::StreamExt};
- use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
- use url::Url;
- use darkfi::{
- async_daemonize, cli_desc,
- util::{
- encoding::base64,
- parse::{decode_base10, encode_base10},
- path::{expand_path, get_config_path},
- },
- zk::halo2::Field,
- Error, Result,
- };
- use darkfi_dao_contract::{blockwindow, model::DaoProposalBulla, DaoFunction};
- use darkfi_money_contract::model::{Coin, CoinAttributes, TokenId};
- use darkfi_sdk::{
- crypto::{
- note::AeadEncryptedNote, BaseBlind, FuncId, FuncRef, Keypair, PublicKey, SecretKey,
- DAO_CONTRACT_ID,
- },
- pasta::{group::ff::PrimeField, pallas},
- tx::TransactionHash,
- };
- use darkfi_serial::{deserialize_async, serialize_async};
- use drk::{
- cli_util::{
- generate_completions, kaching, parse_token_pair, parse_tx_from_stdin, parse_value_pair,
- },
- dao::{DaoParams, ProposalRecord},
- money::BALANCE_BASE10_DECIMALS,
- swap::PartialSwapData,
- Drk,
- };
- const CONFIG_FILE: &str = "drk_config.toml";
- const CONFIG_FILE_CONTENTS: &str = include_str!("../drk_config.toml");
- // Dev Note: when adding/modifying args here,
- // don't forget to update cli_util::generate_completions()
- #[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
- #[serde(default)]
- #[structopt(name = "drk", about = cli_desc!())]
- struct Args {
- #[structopt(short, long)]
- /// Configuration file to use
- config: Option<String>,
- #[structopt(short, long, default_value = "testnet")]
- /// Blockchain network to use
- network: String,
- #[structopt(subcommand)]
- /// Sub command to execute
- command: Subcmd,
- #[structopt(short, long)]
- /// Flag indicating whether you want some fun in your life
- fun: bool,
- #[structopt(short, long)]
- /// Set log file to ouput into
- log: Option<String>,
- #[structopt(short, parse(from_occurrences))]
- /// Increase verbosity (-vvv supported)
- verbose: u8,
- }
- // Dev Note: when adding/modifying commands here,
- // don't forget to update cli_util::generate_completions()
- #[derive(Clone, Debug, Deserialize, StructOpt)]
- enum Subcmd {
- /// Fun
- Kaching,
- /// Send a ping request to the darkfid RPC endpoint
- Ping,
- /// Generate a SHELL completion script and print to stdout
- Completions {
- /// The Shell you want to generate script for
- shell: String,
- },
- /// Wallet operations
- Wallet {
- #[structopt(long)]
- /// Initialize wallet database
- initialize: bool,
- #[structopt(long)]
- /// Generate a new keypair in the wallet
- keygen: bool,
- #[structopt(long)]
- /// Query the wallet for known balances
- balance: bool,
- #[structopt(long)]
- /// Get the default address in the wallet
- address: bool,
- #[structopt(long)]
- /// Print all the addresses in the wallet
- addresses: bool,
- #[structopt(long)]
- /// Set the default address in the wallet
- default_address: Option<usize>,
- #[structopt(long)]
- /// Print all the secret keys from the wallet
- secrets: bool,
- #[structopt(long)]
- /// Import secret keys from stdin into the wallet, separated by newlines
- import_secrets: bool,
- #[structopt(long)]
- /// Print the Merkle tree in the wallet
- tree: bool,
- #[structopt(long)]
- /// Print all the coins in the wallet
- coins: bool,
- },
- /// Read a transaction from stdin and mark its input coins as spent
- Spend,
- /// Unspend a coin
- Unspend {
- /// base58-encoded coin to mark as unspent
- coin: String,
- },
- /// Create a payment transaction
- Transfer {
- /// Amount to send
- amount: String,
- /// Token ID to send
- token: String,
- /// Recipient address
- recipient: String,
- /// Optional contract spend hook to use
- spend_hook: Option<String>,
- /// Optional user data to use
- user_data: Option<String>,
- #[structopt(long)]
- /// Split the output coin into two equal halves
- half_split: bool,
- },
- /// OTC atomic swap
- Otc {
- #[structopt(subcommand)]
- /// Sub command to execute
- command: OtcSubcmd,
- },
- /// Attach the fee call to a transaction given from stdin
- AttachFee,
- /// Inspect a transaction from stdin
- Inspect,
- /// Read a transaction from stdin and broadcast it
- Broadcast,
- /// This subscription will listen for incoming blocks from darkfid and look
- /// through their transactions to see if there's any that interest us.
- /// With `drk` we look at transactions calling the money contract so we can
- /// find coins sent to us and fill our wallet with the necessary metadata.
- Subscribe,
- /// DAO functionalities
- Dao {
- #[structopt(subcommand)]
- /// Sub command to execute
- command: DaoSubcmd,
- },
- /// Scan the blockchain and parse relevant transactions
- Scan {
- #[structopt(long)]
- /// Reset wallet state to provided block height and start scanning
- reset: Option<u32>,
- },
- /// Explorer related subcommands
- Explorer {
- #[structopt(subcommand)]
- /// Sub command to execute
- command: ExplorerSubcmd,
- },
- /// Manage Token aliases
- Alias {
- #[structopt(subcommand)]
- /// Sub command to execute
- command: AliasSubcmd,
- },
- /// Token functionalities
- Token {
- #[structopt(subcommand)]
- /// Sub command to execute
- command: TokenSubcmd,
- },
- /// Contract functionalities
- Contract {
- #[structopt(subcommand)]
- /// Sub command to execute
- command: ContractSubcmd,
- },
- }
- #[derive(Clone, Debug, Deserialize, StructOpt)]
- enum OtcSubcmd {
- /// Initialize the first half of the atomic swap
- Init {
- /// Value pair to send:recv (11.55:99.42)
- #[structopt(short, long)]
- value_pair: String,
- /// Token pair to send:recv (f00:b4r)
- #[structopt(short, long)]
- token_pair: String,
- },
- /// Build entire swap tx given the first half from stdin
- Join,
- /// Inspect a swap half or the full swap tx from stdin
- Inspect,
- /// Sign a swap transaction given from stdin
- Sign,
- }
- #[derive(Clone, Debug, Deserialize, StructOpt)]
- enum DaoSubcmd {
- /// Create DAO parameters
- Create {
- /// The minimum amount of governance tokens needed to open a proposal for this DAO
- proposer_limit: String,
- /// Minimal threshold of participating total tokens needed for a proposal to pass
- quorum: String,
- /// 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.
- early_exec_quorum: String,
- /// The ratio of winning votes/total votes needed for a proposal to pass (2 decimals)
- approval_ratio: f64,
- /// DAO's governance token ID
- gov_token_id: String,
- },
- /// View DAO data from stdin
- View,
- /// Import DAO data from stdin
- Import {
- /// Name identifier for the DAO
- name: String,
- },
- /// Update DAO keys from stdin
- UpdateKeys,
- /// List imported DAOs (or info about a specific one)
- List {
- /// Name identifier for the DAO (optional)
- name: Option<String>,
- },
- /// Show the balance of a DAO
- Balance {
- /// Name identifier for the DAO
- name: String,
- },
- /// Mint an imported DAO on-chain
- Mint {
- /// Name identifier for the DAO
- name: String,
- },
- /// Create a transfer proposal for a DAO
- ProposeTransfer {
- /// Name identifier for the DAO
- name: String,
- /// Duration of the proposal, in block windows
- duration: u64,
- /// Amount to send
- amount: String,
- /// Token ID to send
- token: String,
- /// Recipient address
- recipient: String,
- /// Optional contract spend hook to use
- spend_hook: Option<String>,
- /// Optional user data to use
- user_data: Option<String>,
- },
- /// Create a generic proposal for a DAO
- ProposeGeneric {
- /// Name identifier for the DAO
- name: String,
- /// Duration of the proposal, in block windows
- duration: u64,
- /// Optional user data to use
- user_data: Option<String>,
- },
- /// List DAO proposals
- Proposals {
- /// Name identifier for the DAO
- name: String,
- },
- /// View a DAO proposal data
- Proposal {
- /// Bulla identifier for the proposal
- bulla: String,
- #[structopt(long)]
- /// Encrypt the proposal and encode it to base64
- export: bool,
- #[structopt(long)]
- /// Create the proposal transaction
- mint_proposal: bool,
- },
- /// Import a base64 encoded and encrypted proposal from stdin
- ProposalImport,
- /// Vote on a given proposal
- Vote {
- /// Bulla identifier for the proposal
- bulla: String,
- /// Vote (0 for NO, 1 for YES)
- vote: u8,
- /// Optional vote weight (amount of governance tokens)
- vote_weight: Option<String>,
- },
- /// Execute a DAO proposal
- Exec {
- /// Bulla identifier for the proposal
- bulla: String,
- #[structopt(long)]
- /// Execute the proposal early
- early: bool,
- },
- /// Print the DAO contract base58-encoded spend hook
- SpendHook,
- }
- #[derive(Clone, Debug, Deserialize, StructOpt)]
- enum ExplorerSubcmd {
- /// Fetch a blockchain transaction by hash
- FetchTx {
- /// Transaction hash
- tx_hash: String,
- #[structopt(long)]
- /// Print the full transaction information
- full: bool,
- #[structopt(long)]
- /// Encode transaction to base58
- encode: bool,
- },
- /// Read a transaction from stdin and simulate it
- SimulateTx,
- /// Fetch broadcasted transactions history
- TxsHistory {
- /// Fetch specific history record (optional)
- tx_hash: Option<String>,
- #[structopt(long)]
- /// Encode specific history record transaction to base58
- encode: bool,
- },
- /// Remove reverted transactions from history
- ClearReverted,
- /// Fetch scanned blocks records
- ScannedBlocks {
- /// Fetch specific height record (optional)
- height: Option<u32>,
- },
- }
- #[derive(Clone, Debug, Deserialize, StructOpt)]
- enum AliasSubcmd {
- /// Create a Token alias
- Add {
- /// Token alias
- alias: String,
- /// Token to create alias for
- token: String,
- },
- /// Print alias info of optional arguments.
- /// If no argument is provided, list all the aliases in the wallet.
- Show {
- /// Token alias to search for
- #[structopt(short, long)]
- alias: Option<String>,
- /// Token to search alias for
- #[structopt(short, long)]
- token: Option<String>,
- },
- /// Remove a Token alias
- Remove {
- /// Token alias to remove
- alias: String,
- },
- }
- #[derive(Clone, Debug, Deserialize, StructOpt)]
- enum TokenSubcmd {
- /// Import a mint authority
- Import {
- /// Mint authority secret key
- secret_key: String,
- /// Mint authority token blind
- token_blind: String,
- },
- /// Generate a new mint authority
- GenerateMint,
- /// List token IDs with available mint authorities
- List,
- /// Mint tokens
- Mint {
- /// Token ID to mint
- token: String,
- /// Amount to mint
- amount: String,
- /// Recipient of the minted tokens
- recipient: String,
- /// Optional contract spend hook to use
- spend_hook: Option<String>,
- /// Optional user data to use
- user_data: Option<String>,
- },
- /// Freeze a token mint
- Freeze {
- /// Token ID to freeze
- token: String,
- },
- }
- #[derive(Clone, Debug, Deserialize, StructOpt)]
- enum ContractSubcmd {
- /// Generate a new deploy authority
- GenerateDeploy,
- /// List deploy authorities in the wallet
- List,
- /// Deploy a smart contract
- Deploy {
- /// Contract ID (deploy authority)
- deploy_auth: u64,
- /// Path to contract wasm bincode
- wasm_path: String,
- /// Path to serialized deploy instruction
- deploy_ix: String,
- },
- /// Lock a smart contract
- Lock {
- /// Contract ID (deploy authority)
- deploy_auth: u64,
- },
- }
- /// Defines a blockchain network configuration.
- /// Default values correspond to a local network.
- #[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
- #[structopt()]
- struct BlockchainNetwork {
- #[structopt(long, default_value = "~/.local/share/darkfi/drk/localnet/wallet.db")]
- /// Path to wallet database
- wallet_path: String,
- #[structopt(long, default_value = "changeme")]
- /// Password for the wallet database
- wallet_pass: String,
- #[structopt(short, long, default_value = "tcp://127.0.0.1:8240")]
- /// darkfid JSON-RPC endpoint
- endpoint: Url,
- }
- /// Auxiliary function to parse darkfid configuration file and extract requested
- /// blockchain network config.
- async fn parse_blockchain_config(
- config: Option<String>,
- network: &str,
- ) -> Result<BlockchainNetwork> {
- // Grab config path
- let config_path = get_config_path(config, CONFIG_FILE)?;
- // Parse TOML file contents
- let contents = read_to_string(&config_path).await?;
- let contents: toml::Value = match toml::from_str(&contents) {
- Ok(v) => v,
- Err(e) => {
- eprintln!("Failed parsing TOML config: {e}");
- return Err(Error::ParseFailed("Failed parsing TOML config"))
- }
- };
- // Grab requested network config
- let Some(table) = contents.as_table() else { return Err(Error::ParseFailed("TOML not a map")) };
- let Some(network_configs) = table.get("network_config") else {
- return Err(Error::ParseFailed("TOML does not contain network configurations"))
- };
- let Some(network_configs) = network_configs.as_table() else {
- return Err(Error::ParseFailed("`network_config` not a map"))
- };
- let Some(network_config) = network_configs.get(network) else {
- return Err(Error::ParseFailed("TOML does not contain requested network configuration"))
- };
- let network_config = toml::to_string(&network_config).unwrap();
- let network_config =
- match BlockchainNetwork::from_iter_with_toml::<Vec<String>>(&network_config, vec![]) {
- Ok(v) => v,
- Err(e) => {
- eprintln!("Failed parsing requested network configuration: {e}");
- return Err(Error::ParseFailed("Failed parsing requested network configuration"))
- }
- };
- Ok(network_config)
- }
- /// Auxiliary function to create a `Drk` wallet for provided configuration.
- async fn new_wallet(
- wallet_path: String,
- wallet_pass: String,
- endpoint: Option<Url>,
- ex: Arc<smol::Executor<'static>>,
- fun: bool,
- ) -> Drk {
- // Script kiddies protection
- if wallet_pass == "changeme" {
- eprintln!("Please don't use default wallet password...");
- exit(2);
- }
- match Drk::new(wallet_path, wallet_pass, endpoint, ex, fun).await {
- Ok(wallet) => wallet,
- Err(e) => {
- eprintln!("Error initializing wallet: {e:?}");
- exit(2);
- }
- }
- }
- async_daemonize!(realmain);
- async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
- // Grab blockchain network configuration
- let blockchain_config = match args.network.as_str() {
- "localnet" => parse_blockchain_config(args.config, "localnet").await?,
- "testnet" => parse_blockchain_config(args.config, "testnet").await?,
- "mainnet" => parse_blockchain_config(args.config, "mainnet").await?,
- _ => {
- eprintln!("Unsupported chain `{}`", args.network);
- return Err(Error::UnsupportedChain)
- }
- };
- match args.command {
- Subcmd::Kaching => {
- if !args.fun {
- println!("Apparently you don't like fun...");
- return Ok(())
- }
- kaching().await;
- Ok(())
- }
- Subcmd::Ping => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- drk.ping().await?;
- drk.stop_rpc_client().await
- }
- Subcmd::Completions { shell } => generate_completions(&shell),
- Subcmd::Wallet {
- initialize,
- keygen,
- balance,
- address,
- addresses,
- default_address,
- secrets,
- import_secrets,
- tree,
- coins,
- } => {
- if !initialize &&
- !keygen &&
- !balance &&
- !address &&
- !addresses &&
- default_address.is_none() &&
- !secrets &&
- !tree &&
- !coins &&
- !import_secrets
- {
- eprintln!("Error: You must use at least one flag for this subcommand");
- eprintln!("Run with \"wallet -h\" to see the subcommand usage.");
- exit(2);
- }
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if initialize {
- if let Err(e) = drk.initialize_wallet().await {
- eprintln!("Error initializing wallet: {e:?}");
- exit(2);
- }
- if let Err(e) = drk.initialize_money().await {
- eprintln!("Failed to initialize Money: {e:?}");
- exit(2);
- }
- if let Err(e) = drk.initialize_dao().await {
- eprintln!("Failed to initialize DAO: {e:?}");
- exit(2);
- }
- if let Err(e) = drk.initialize_deployooor() {
- eprintln!("Failed to initialize Deployooor: {e:?}");
- exit(2);
- }
- return Ok(())
- }
- if keygen {
- if let Err(e) = drk.money_keygen().await {
- eprintln!("Failed to generate keypair: {e:?}");
- exit(2);
- }
- return Ok(())
- }
- if balance {
- let balmap = drk.money_balance().await?;
- let aliases_map = drk.get_aliases_mapped_by_token().await?;
- // Create a prettytable with the new data:
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row!["Token ID", "Aliases", "Balance"]);
- for (token_id, balance) in balmap.iter() {
- let aliases = match aliases_map.get(token_id) {
- Some(a) => a,
- None => "-",
- };
- table.add_row(row![
- token_id,
- aliases,
- encode_base10(*balance, BALANCE_BASE10_DECIMALS)
- ]);
- }
- if table.is_empty() {
- println!("No unspent balances found");
- } else {
- println!("{table}");
- }
- return Ok(())
- }
- if address {
- let address = match drk.default_address().await {
- Ok(a) => a,
- Err(e) => {
- eprintln!("Failed to fetch default address: {e:?}");
- exit(2);
- }
- };
- println!("{address}");
- return Ok(())
- }
- if addresses {
- let addresses = drk.addresses().await?;
- // Create a prettytable with the new data:
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row!["Key ID", "Public Key", "Secret Key", "Is Default"]);
- for (key_id, public_key, secret_key, is_default) in addresses {
- let is_default = match is_default {
- 1 => "*",
- _ => "",
- };
- table.add_row(row![key_id, public_key, secret_key, is_default]);
- }
- if table.is_empty() {
- println!("No addresses found");
- } else {
- println!("{table}");
- }
- return Ok(())
- }
- if let Some(idx) = default_address {
- if let Err(e) = drk.set_default_address(idx) {
- eprintln!("Failed to set default address: {e:?}");
- exit(2);
- }
- return Ok(())
- }
- if secrets {
- let v = drk.get_money_secrets().await?;
- for i in v {
- println!("{i}");
- }
- return Ok(())
- }
- if import_secrets {
- let mut secrets = vec![];
- let lines = stdin().lines();
- for (i, line) in lines.enumerate() {
- if let Ok(line) = line {
- let bytes = bs58::decode(&line.trim()).into_vec()?;
- let Ok(secret) = deserialize_async(&bytes).await else {
- println!("Warning: Failed to deserialize secret on line {i}");
- continue
- };
- secrets.push(secret);
- }
- }
- let pubkeys = match drk.import_money_secrets(secrets).await {
- Ok(p) => p,
- Err(e) => {
- eprintln!("Failed to import secret keys into wallet: {e:?}");
- exit(2);
- }
- };
- for key in pubkeys {
- println!("{key}");
- }
- return Ok(())
- }
- if tree {
- let tree = drk.get_money_tree().await?;
- println!("{tree:#?}");
- return Ok(())
- }
- if coins {
- let coins = drk.get_coins(true).await?;
- let aliases_map = drk.get_aliases_mapped_by_token().await?;
- if coins.is_empty() {
- return Ok(())
- }
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row![
- "Coin",
- "Spent",
- "Token ID",
- "Aliases",
- "Value",
- "Spend Hook",
- "User Data",
- "Spent TX"
- ]);
- for coin in coins {
- let aliases = match aliases_map.get(&coin.0.note.token_id.to_string()) {
- Some(a) => a,
- None => "-",
- };
- let spend_hook = if coin.0.note.spend_hook != FuncId::none() {
- format!("{}", coin.0.note.spend_hook)
- } else {
- String::from("-")
- };
- let user_data = if coin.0.note.user_data != pallas::Base::ZERO {
- bs58::encode(&serialize_async(&coin.0.note.user_data).await)
- .into_string()
- .to_string()
- } else {
- String::from("-")
- };
- table.add_row(row![
- bs58::encode(&serialize_async(&coin.0.coin.inner()).await)
- .into_string()
- .to_string(),
- coin.1,
- coin.0.note.token_id,
- aliases,
- format!(
- "{} ({})",
- coin.0.note.value,
- encode_base10(coin.0.note.value, BALANCE_BASE10_DECIMALS)
- ),
- spend_hook,
- user_data,
- coin.2
- ]);
- }
- println!("{table}");
- return Ok(())
- }
- unreachable!()
- }
- Subcmd::Spend => {
- let tx = parse_tx_from_stdin().await?;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.mark_tx_spend(&tx).await {
- eprintln!("Failed to mark transaction coins as spent: {e:?}");
- exit(2);
- };
- Ok(())
- }
- Subcmd::Unspend { coin } => {
- let bytes: [u8; 32] = match bs58::decode(&coin).into_vec()?.try_into() {
- Ok(b) => b,
- Err(e) => {
- eprintln!("Invalid coin: {e:?}");
- exit(2);
- }
- };
- let elem: pallas::Base = match pallas::Base::from_repr(bytes).into() {
- Some(v) => v,
- None => {
- eprintln!("Invalid coin");
- exit(2);
- }
- };
- let coin = Coin::from(elem);
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.unspend_coin(&coin).await {
- eprintln!("Failed to mark coin as unspent: {e:?}");
- exit(2);
- }
- Ok(())
- }
- Subcmd::Transfer { amount, token, recipient, spend_hook, user_data, half_split } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = f64::from_str(&amount) {
- eprintln!("Invalid amount: {e:?}");
- exit(2);
- }
- let rcpt = match PublicKey::from_str(&recipient) {
- Ok(r) => r,
- Err(e) => {
- eprintln!("Invalid recipient: {e:?}");
- exit(2);
- }
- };
- let token_id = match drk.get_token(token).await {
- Ok(t) => t,
- Err(e) => {
- eprintln!("Invalid token alias: {e:?}");
- exit(2);
- }
- };
- let spend_hook = match spend_hook {
- Some(s) => match FuncId::from_str(&s) {
- Ok(s) => Some(s),
- Err(e) => {
- eprintln!("Invalid spend hook: {e:?}");
- exit(2);
- }
- },
- None => None,
- };
- let user_data = match user_data {
- Some(u) => {
- let bytes: [u8; 32] = match bs58::decode(&u).into_vec()?.try_into() {
- Ok(b) => b,
- Err(e) => {
- eprintln!("Invalid user data: {e:?}");
- exit(2);
- }
- };
- match pallas::Base::from_repr(bytes).into() {
- Some(v) => Some(v),
- None => {
- eprintln!("Invalid user data");
- exit(2);
- }
- }
- }
- None => None,
- };
- let tx = match drk
- .transfer(&amount, token_id, rcpt, spend_hook, user_data, half_split)
- .await
- {
- Ok(t) => t,
- Err(e) => {
- eprintln!("Failed to create payment transaction: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- drk.stop_rpc_client().await
- }
- Subcmd::Otc { command } => match command {
- OtcSubcmd::Init { value_pair, token_pair } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let value_pair = parse_value_pair(&value_pair)?;
- let token_pair = parse_token_pair(&drk, &token_pair).await?;
- let half = match drk.init_swap(value_pair, token_pair, None, None, None).await {
- Ok(h) => h,
- Err(e) => {
- eprintln!("Failed to create swap transaction half: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&half).await));
- drk.stop_rpc_client().await
- }
- OtcSubcmd::Join => {
- let mut buf = String::new();
- stdin().read_to_string(&mut buf)?;
- let Some(bytes) = base64::decode(buf.trim()) else {
- eprintln!("Failed to decode partial swap data");
- exit(2);
- };
- let partial: PartialSwapData = deserialize_async(&bytes).await?;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let tx = match drk.join_swap(partial, None, None, None).await {
- Ok(tx) => tx,
- Err(e) => {
- eprintln!("Failed to create a join swap transaction: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- drk.stop_rpc_client().await
- }
- OtcSubcmd::Inspect => {
- let mut buf = String::new();
- stdin().read_to_string(&mut buf)?;
- let Some(bytes) = base64::decode(buf.trim()) else {
- eprintln!("Failed to decode swap transaction");
- exit(2);
- };
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.inspect_swap(bytes).await {
- eprintln!("Failed to inspect swap: {e:?}");
- exit(2);
- };
- Ok(())
- }
- OtcSubcmd::Sign => {
- let mut tx = parse_tx_from_stdin().await?;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.sign_swap(&mut tx).await {
- eprintln!("Failed to sign joined swap transaction: {e:?}");
- exit(2);
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- Ok(())
- }
- },
- Subcmd::Dao { command } => match command {
- DaoSubcmd::Create {
- proposer_limit,
- quorum,
- early_exec_quorum,
- approval_ratio,
- gov_token_id,
- } => {
- if let Err(e) = f64::from_str(&proposer_limit) {
- eprintln!("Invalid proposer limit: {e:?}");
- exit(2);
- }
- if let Err(e) = f64::from_str(&quorum) {
- eprintln!("Invalid quorum: {e:?}");
- exit(2);
- }
- if let Err(e) = f64::from_str(&early_exec_quorum) {
- eprintln!("Invalid early exec quorum: {e:?}");
- exit(2);
- }
- let proposer_limit = decode_base10(&proposer_limit, BALANCE_BASE10_DECIMALS, true)?;
- let quorum = decode_base10(&quorum, BALANCE_BASE10_DECIMALS, true)?;
- let early_exec_quorum =
- decode_base10(&early_exec_quorum, BALANCE_BASE10_DECIMALS, true)?;
- if approval_ratio > 1.0 {
- eprintln!("Error: Approval ratio cannot be >1.0");
- exit(2);
- }
- let approval_ratio_base = 100_u64;
- let approval_ratio_quot = (approval_ratio * approval_ratio_base as f64) as u64;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- let gov_token_id = match drk.get_token(gov_token_id).await {
- Ok(g) => g,
- Err(e) => {
- eprintln!("Invalid Token ID: {e:?}");
- exit(2);
- }
- };
- let notes_keypair = Keypair::random(&mut OsRng);
- let proposer_keypair = Keypair::random(&mut OsRng);
- let proposals_keypair = Keypair::random(&mut OsRng);
- let votes_keypair = Keypair::random(&mut OsRng);
- let exec_keypair = Keypair::random(&mut OsRng);
- let early_exec_keypair = Keypair::random(&mut OsRng);
- let bulla_blind = BaseBlind::random(&mut OsRng);
- let params = DaoParams::new(
- proposer_limit,
- quorum,
- early_exec_quorum,
- approval_ratio_base,
- approval_ratio_quot,
- gov_token_id,
- Some(notes_keypair.secret),
- notes_keypair.public,
- Some(proposer_keypair.secret),
- proposer_keypair.public,
- Some(proposals_keypair.secret),
- proposals_keypair.public,
- Some(votes_keypair.secret),
- votes_keypair.public,
- Some(exec_keypair.secret),
- exec_keypair.public,
- Some(early_exec_keypair.secret),
- early_exec_keypair.public,
- bulla_blind,
- );
- println!("{}", params.toml_str());
- Ok(())
- }
- DaoSubcmd::View => {
- let mut buf = String::new();
- stdin().read_to_string(&mut buf)?;
- let params = DaoParams::from_toml_str(&buf)?;
- println!("{params}");
- Ok(())
- }
- DaoSubcmd::Import { name } => {
- let mut buf = String::new();
- stdin().read_to_string(&mut buf)?;
- let params = DaoParams::from_toml_str(&buf)?;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.import_dao(&name, ¶ms).await {
- eprintln!("Failed to import DAO: {e:?}");
- exit(2);
- }
- Ok(())
- }
- DaoSubcmd::UpdateKeys => {
- let mut buf = String::new();
- stdin().read_to_string(&mut buf)?;
- let params = DaoParams::from_toml_str(&buf)?;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.update_dao_keys(¶ms).await {
- eprintln!("Failed to update DAO keys: {e:?}");
- exit(2);
- }
- Ok(())
- }
- DaoSubcmd::List { name } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.dao_list(&name).await {
- eprintln!("Failed to list DAO: {e:?}");
- exit(2);
- }
- Ok(())
- }
- DaoSubcmd::Balance { name } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- let balmap = match drk.dao_balance(&name).await {
- Ok(b) => b,
- Err(e) => {
- eprintln!("Failed to fetch DAO balance: {e:?}");
- exit(2);
- }
- };
- let aliases_map = match drk.get_aliases_mapped_by_token().await {
- Ok(a) => a,
- Err(e) => {
- eprintln!("Failed to fetch wallet aliases: {e:?}");
- exit(2);
- }
- };
- // Create a prettytable with the new data:
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row!["Token ID", "Aliases", "Balance"]);
- for (token_id, balance) in balmap.iter() {
- let aliases = match aliases_map.get(token_id) {
- Some(a) => a,
- None => "-",
- };
- table.add_row(row![
- token_id,
- aliases,
- encode_base10(*balance, BALANCE_BASE10_DECIMALS)
- ]);
- }
- if table.is_empty() {
- println!("No unspent balances found");
- } else {
- println!("{table}");
- }
- Ok(())
- }
- DaoSubcmd::Mint { name } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let tx = match drk.dao_mint(&name).await {
- Ok(tx) => tx,
- Err(e) => {
- eprintln!("Failed to mint DAO: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- drk.stop_rpc_client().await
- }
- DaoSubcmd::ProposeTransfer {
- name,
- duration,
- amount,
- token,
- recipient,
- spend_hook,
- user_data,
- } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = f64::from_str(&amount) {
- eprintln!("Invalid amount: {e:?}");
- exit(2);
- }
- let rcpt = match PublicKey::from_str(&recipient) {
- Ok(r) => r,
- Err(e) => {
- eprintln!("Invalid recipient: {e:?}");
- exit(2);
- }
- };
- let token_id = match drk.get_token(token).await {
- Ok(t) => t,
- Err(e) => {
- eprintln!("Invalid token alias: {e:?}");
- exit(2);
- }
- };
- let spend_hook = match spend_hook {
- Some(s) => match FuncId::from_str(&s) {
- Ok(s) => Some(s),
- Err(e) => {
- eprintln!("Invalid spend hook: {e:?}");
- exit(2);
- }
- },
- None => None,
- };
- let user_data = match user_data {
- Some(u) => {
- let bytes: [u8; 32] = match bs58::decode(&u).into_vec()?.try_into() {
- Ok(b) => b,
- Err(e) => {
- eprintln!("Invalid user data: {e:?}");
- exit(2);
- }
- };
- match pallas::Base::from_repr(bytes).into() {
- Some(v) => Some(v),
- None => {
- eprintln!("Invalid user data");
- exit(2);
- }
- }
- }
- None => None,
- };
- let proposal = match drk
- .dao_propose_transfer(
- &name, duration, &amount, token_id, rcpt, spend_hook, user_data,
- )
- .await
- {
- Ok(p) => p,
- Err(e) => {
- eprintln!("Failed to create DAO transfer proposal: {e:?}");
- exit(2);
- }
- };
- println!("Generated proposal: {}", proposal.bulla());
- drk.stop_rpc_client().await
- }
- DaoSubcmd::ProposeGeneric { name, duration, user_data } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let user_data = match user_data {
- Some(u) => {
- let bytes: [u8; 32] = match bs58::decode(&u).into_vec()?.try_into() {
- Ok(b) => b,
- Err(e) => {
- eprintln!("Invalid user data: {e:?}");
- exit(2);
- }
- };
- match pallas::Base::from_repr(bytes).into() {
- Some(v) => Some(v),
- None => {
- eprintln!("Invalid user data");
- exit(2);
- }
- }
- }
- None => None,
- };
- let proposal = match drk.dao_propose_generic(&name, duration, user_data).await {
- Ok(p) => p,
- Err(e) => {
- eprintln!("Failed to create DAO transfer proposal: {e:?}");
- exit(2);
- }
- };
- println!("Generated proposal: {}", proposal.bulla());
- drk.stop_rpc_client().await
- }
- DaoSubcmd::Proposals { name } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- let proposals = drk.get_dao_proposals(&name).await?;
- for (i, proposal) in proposals.iter().enumerate() {
- println!("{i}. {}", proposal.bulla());
- }
- Ok(())
- }
- DaoSubcmd::Proposal { bulla, export, mint_proposal } => {
- let bulla = match DaoProposalBulla::from_str(&bulla) {
- Ok(b) => b,
- Err(e) => {
- eprintln!("Invalid proposal bulla: {e:?}");
- exit(2);
- }
- };
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let proposal = drk.get_dao_proposal_by_bulla(&bulla).await?;
- if export {
- // Retrieve the DAO
- let dao = drk.get_dao_by_bulla(&proposal.proposal.dao_bulla).await?;
- // Encypt the proposal
- let enc_note = AeadEncryptedNote::encrypt(
- &proposal,
- &dao.params.dao.proposals_public_key,
- &mut OsRng,
- )
- .unwrap();
- // Export it to base64
- println!("{}", base64::encode(&serialize_async(&enc_note).await));
- return drk.stop_rpc_client().await
- }
- if mint_proposal {
- // Identify proposal type by its auth calls
- for call in &proposal.proposal.auth_calls {
- // We only support transfer right now
- if call.function_code == DaoFunction::AuthMoneyTransfer as u8 {
- let tx = match drk.dao_transfer_proposal_tx(&proposal).await {
- Ok(tx) => tx,
- Err(e) => {
- eprintln!("Failed to create DAO transfer proposal: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- return drk.stop_rpc_client().await
- }
- }
- // If proposal has no auth calls, we consider it a generic one
- if proposal.proposal.auth_calls.is_empty() {
- let tx = match drk.dao_generic_proposal_tx(&proposal).await {
- Ok(tx) => tx,
- Err(e) => {
- eprintln!("Failed to create DAO generic proposal: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- return drk.stop_rpc_client().await
- }
- eprintln!("Unsuported DAO proposal");
- exit(2);
- }
- println!("{proposal}");
- let mut contract_calls = "\nInvoked contracts:\n".to_string();
- for call in proposal.proposal.auth_calls {
- contract_calls.push_str(&format!(
- "\tContract: {}\n\tFunction: {}\n\tData: ",
- call.contract_id, call.function_code
- ));
- if call.auth_data.is_empty() {
- contract_calls.push_str("-\n");
- continue;
- }
- if call.function_code == DaoFunction::AuthMoneyTransfer as u8 {
- // We know that the plaintext data live in the data plaintext vec
- if proposal.data.is_none() {
- contract_calls.push_str("-\n");
- continue;
- }
- let coin: CoinAttributes =
- deserialize_async(proposal.data.as_ref().unwrap()).await?;
- let spend_hook = if coin.spend_hook == FuncId::none() {
- "-".to_string()
- } else {
- format!("{}", coin.spend_hook)
- };
- let user_data = if coin.user_data == pallas::Base::ZERO {
- "-".to_string()
- } else {
- format!("{:?}", coin.user_data)
- };
- contract_calls.push_str(&format!("\n\t\t{}: {}\n\t\t{}: {} ({})\n\t\t{}: {}\n\t\t{}: {}\n\t\t{}: {}\n\t\t{}: {}\n\n",
- "Recipient",
- coin.public_key,
- "Amount",
- coin.value,
- encode_base10(coin.value, BALANCE_BASE10_DECIMALS),
- "Token",
- coin.token_id,
- "Spend hook",
- spend_hook,
- "User data",
- user_data,
- "Blind",
- coin.blind));
- }
- }
- println!("{contract_calls}");
- let votes = drk.get_dao_proposal_votes(&bulla).await?;
- let mut total_yes_vote_value = 0;
- let mut total_no_vote_value = 0;
- let mut total_all_vote_value = 0;
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row!["Transaction", "Tokens", "Vote"]);
- for vote in votes {
- let vote_option = if vote.vote_option {
- total_yes_vote_value += vote.all_vote_value;
- "Yes"
- } else {
- total_no_vote_value += vote.all_vote_value;
- "No"
- };
- total_all_vote_value += vote.all_vote_value;
- table.add_row(row![
- vote.tx_hash,
- encode_base10(vote.all_vote_value, BALANCE_BASE10_DECIMALS),
- vote_option
- ]);
- }
- let outcome = if table.is_empty() {
- println!("Votes: No votes found");
- "Unknown"
- } else {
- println!("Votes:");
- println!("{table}");
- println!(
- "Total tokens votes: {}",
- encode_base10(total_all_vote_value, BALANCE_BASE10_DECIMALS)
- );
- let approval_ratio =
- (total_yes_vote_value as f64 * 100.0) / total_all_vote_value as f64;
- println!(
- "Total tokens Yes votes: {} ({approval_ratio:.2}%)",
- encode_base10(total_yes_vote_value, BALANCE_BASE10_DECIMALS)
- );
- println!(
- "Total tokens No votes: {} ({:.2}%)",
- encode_base10(total_no_vote_value, BALANCE_BASE10_DECIMALS),
- (total_no_vote_value as f64 * 100.0) / total_all_vote_value as f64
- );
- let dao = drk.get_dao_by_bulla(&proposal.proposal.dao_bulla).await?;
- if total_all_vote_value >= dao.params.dao.quorum &&
- approval_ratio >=
- (dao.params.dao.approval_ratio_quot /
- dao.params.dao.approval_ratio_base)
- as f64
- {
- "Approved"
- } else {
- "Rejected"
- }
- };
- if let Some(exec_tx_hash) = proposal.exec_tx_hash {
- println!("Proposal was executed on transaction: {exec_tx_hash}");
- return drk.stop_rpc_client().await
- }
- // Retrieve next block height and current block time target,
- // to compute their window.
- let next_block_height = drk.get_next_block_height().await?;
- let block_target = drk.get_block_target().await?;
- let current_window = blockwindow(next_block_height, block_target);
- let end_time = proposal.proposal.creation_blockwindow +
- proposal.proposal.duration_blockwindows;
- let (voting_status, proposal_status_message) = if current_window < end_time {
- ("Ongoing", format!("Current proposal outcome: {outcome}"))
- } else {
- ("Concluded", format!("Proposal outcome: {outcome}"))
- };
- println!("Voting status: {voting_status}");
- println!("{proposal_status_message}");
- drk.stop_rpc_client().await
- }
- DaoSubcmd::ProposalImport {} => {
- let mut buf = String::new();
- stdin().read_to_string(&mut buf)?;
- let Some(bytes) = base64::decode(buf.trim()) else {
- eprintln!("Failed to decode encrypted proposal data");
- exit(2);
- };
- let encrypted_proposal: AeadEncryptedNote = deserialize_async(&bytes).await?;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- // Retrieve all DAOs to try to decrypt the proposal
- let daos = drk.get_daos().await?;
- for dao in &daos {
- // Check if we have the proposals key
- let Some(proposals_secret_key) = dao.params.proposals_secret_key else {
- continue
- };
- // Try to decrypt the proposal
- let Ok(proposal) =
- encrypted_proposal.decrypt::<ProposalRecord>(&proposals_secret_key)
- else {
- continue
- };
- let proposal = match drk.get_dao_proposal_by_bulla(&proposal.bulla()).await {
- Ok(p) => {
- let mut our_proposal = p;
- our_proposal.data = proposal.data;
- our_proposal
- }
- Err(_) => proposal,
- };
- return drk.put_dao_proposal(&proposal).await
- }
- eprintln!("Couldn't decrypt the proposal with out DAO keys");
- exit(2);
- }
- DaoSubcmd::Vote { bulla, vote, vote_weight } => {
- let bulla = match DaoProposalBulla::from_str(&bulla) {
- Ok(b) => b,
- Err(e) => {
- eprintln!("Invalid proposal bulla: {e:?}");
- exit(2);
- }
- };
- if vote > 1 {
- eprintln!("Vote can be either 0 (NO) or 1 (YES)");
- exit(2);
- }
- let vote = vote != 0;
- let weight = match vote_weight {
- Some(w) => {
- if let Err(e) = f64::from_str(&w) {
- eprintln!("Invalid vote weight: {e:?}");
- exit(2);
- }
- Some(decode_base10(&w, BALANCE_BASE10_DECIMALS, true)?)
- }
- None => None,
- };
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let tx = match drk.dao_vote(&bulla, vote, weight).await {
- Ok(tx) => tx,
- Err(e) => {
- eprintln!("Failed to create DAO Vote transaction: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- drk.stop_rpc_client().await
- }
- DaoSubcmd::Exec { bulla, early } => {
- let bulla = match DaoProposalBulla::from_str(&bulla) {
- Ok(b) => b,
- Err(e) => {
- eprintln!("Invalid proposal bulla: {e:?}");
- exit(2);
- }
- };
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let proposal = drk.get_dao_proposal_by_bulla(&bulla).await?;
- // Identify proposal type by its auth calls
- for call in &proposal.proposal.auth_calls {
- // We only support transfer right now
- if call.function_code == DaoFunction::AuthMoneyTransfer as u8 {
- let tx = match drk.dao_exec_transfer(&proposal, early).await {
- Ok(tx) => tx,
- Err(e) => {
- eprintln!("Failed to execute DAO transfer proposal: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- return drk.stop_rpc_client().await
- }
- }
- // If proposal has no auth calls, we consider it a generic one
- if proposal.proposal.auth_calls.is_empty() {
- let tx = match drk.dao_exec_generic(&proposal, early).await {
- Ok(tx) => tx,
- Err(e) => {
- eprintln!("Failed to execute DAO generic proposal: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- return drk.stop_rpc_client().await
- }
- eprintln!("Unsuported DAO proposal");
- exit(2);
- }
- DaoSubcmd::SpendHook => {
- let spend_hook =
- FuncRef { contract_id: *DAO_CONTRACT_ID, func_code: DaoFunction::Exec as u8 }
- .to_func_id();
- println!("{spend_hook}");
- Ok(())
- }
- },
- Subcmd::AttachFee => {
- let mut tx = parse_tx_from_stdin().await?;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.attach_fee(&mut tx).await {
- eprintln!("Failed to attach the fee call to the transaction: {e:?}");
- exit(2);
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- drk.stop_rpc_client().await
- }
- Subcmd::Inspect => {
- let tx = parse_tx_from_stdin().await?;
- println!("{tx:#?}");
- Ok(())
- }
- Subcmd::Broadcast => {
- let tx = parse_tx_from_stdin().await?;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.simulate_tx(&tx).await {
- eprintln!("Failed to simulate tx: {e:?}");
- exit(2);
- };
- if let Err(e) = drk.mark_tx_spend(&tx).await {
- eprintln!("Failed to mark transaction coins as spent: {e:?}");
- exit(2);
- };
- let txid = match drk.broadcast_tx(&tx).await {
- Ok(t) => t,
- Err(e) => {
- eprintln!("Failed to broadcast transaction: {e:?}");
- exit(2);
- }
- };
- println!("Transaction ID: {txid}");
- drk.stop_rpc_client().await
- }
- Subcmd::Subscribe => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint.clone()),
- ex.clone(),
- args.fun,
- )
- .await;
- if let Err(e) = drk.subscribe_blocks(blockchain_config.endpoint, ex).await {
- eprintln!("Block subscription failed: {e:?}");
- exit(2);
- }
- drk.stop_rpc_client().await
- }
- Subcmd::Scan { reset } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- if let Some(height) = reset {
- if let Err(e) = drk.reset_to_height(height).await {
- eprintln!("Failed during wallet reset: {e:?}");
- exit(2);
- }
- }
- if let Err(e) = drk.scan_blocks().await {
- eprintln!("Failed during scanning: {e:?}");
- exit(2);
- }
- println!("Finished scanning blockchain");
- drk.stop_rpc_client().await
- }
- Subcmd::Explorer { command } => match command {
- ExplorerSubcmd::FetchTx { tx_hash, full, encode } => {
- let tx_hash = TransactionHash(*blake3::Hash::from_hex(&tx_hash)?.as_bytes());
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let tx = match drk.get_tx(&tx_hash).await {
- Ok(tx) => tx,
- Err(e) => {
- eprintln!("Failed to fetch transaction: {e:?}");
- exit(2);
- }
- };
- let Some(tx) = tx else {
- println!("Transaction was not found");
- exit(1);
- };
- // Make sure the tx is correct
- assert_eq!(tx.hash(), tx_hash);
- if encode {
- println!("{}", base64::encode(&serialize_async(&tx).await));
- exit(1)
- }
- println!("Transaction ID: {tx_hash}");
- if full {
- println!("{tx:?}");
- }
- drk.stop_rpc_client().await
- }
- ExplorerSubcmd::SimulateTx => {
- let tx = parse_tx_from_stdin().await?;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let is_valid = match drk.simulate_tx(&tx).await {
- Ok(b) => b,
- Err(e) => {
- eprintln!("Failed to simulate tx: {e:?}");
- exit(2);
- }
- };
- println!("Transaction ID: {}", tx.hash());
- println!("State: {}", if is_valid { "valid" } else { "invalid" });
- drk.stop_rpc_client().await
- }
- ExplorerSubcmd::TxsHistory { tx_hash, encode } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Some(c) = tx_hash {
- let (tx_hash, status, tx) = drk.get_tx_history_record(&c).await?;
- if encode {
- println!("{}", base64::encode(&serialize_async(&tx).await));
- exit(1)
- }
- println!("Transaction ID: {tx_hash}");
- println!("Status: {status}");
- println!("{tx:?}");
- return Ok(())
- }
- let map = match drk.get_txs_history() {
- Ok(m) => m,
- Err(e) => {
- eprintln!("Failed to retrieve transactions history records: {e:?}");
- exit(2);
- }
- };
- // Create a prettytable with the new data:
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row!["Transaction Hash", "Status"]);
- for (txs_hash, status) in map.iter() {
- table.add_row(row![txs_hash, status]);
- }
- if table.is_empty() {
- println!("No transactions found");
- } else {
- println!("{table}");
- }
- Ok(())
- }
- ExplorerSubcmd::ClearReverted => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.remove_reverted_txs() {
- eprintln!("Failed to remove reverted transactions: {e:?}");
- exit(2);
- };
- Ok(())
- }
- ExplorerSubcmd::ScannedBlocks { height } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Some(h) = height {
- let (height, hash, _) = match drk.get_scanned_block_record(h) {
- Ok(ret) => ret,
- Err(e) => {
- eprintln!("Failed to retrieve scanned block record: {e:?}");
- exit(2);
- }
- };
- println!("Height: {height}");
- println!("Hash: {hash}");
- return Ok(())
- }
- let map = match drk.get_scanned_block_records() {
- Ok(m) => m,
- Err(e) => {
- eprintln!("Failed to retrieve scanned blocks records: {e:?}");
- exit(2);
- }
- };
- // Create a prettytable with the new data:
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row!["Height", "Hash"]);
- for (height, hash, _) in map.iter() {
- table.add_row(row![height, hash]);
- }
- if table.is_empty() {
- println!("No scanned blocks records found");
- } else {
- println!("{table}");
- }
- Ok(())
- }
- },
- Subcmd::Alias { command } => match command {
- AliasSubcmd::Add { alias, token } => {
- if alias.chars().count() > 5 {
- eprintln!("Error: Alias exceeds 5 characters");
- exit(2);
- }
- let token_id = match TokenId::from_str(token.as_str()) {
- Ok(t) => t,
- Err(e) => {
- eprintln!("Invalid Token ID: {e:?}");
- exit(2);
- }
- };
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.add_alias(alias, token_id).await {
- eprintln!("Failed to add alias: {e:?}");
- exit(2);
- }
- Ok(())
- }
- AliasSubcmd::Show { alias, token } => {
- let token_id = match token {
- Some(t) => match TokenId::from_str(t.as_str()) {
- Ok(t) => Some(t),
- Err(e) => {
- eprintln!("Invalid Token ID: {e:?}");
- exit(2);
- }
- },
- None => None,
- };
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- let map = drk.get_aliases(alias, token_id).await?;
- // Create a prettytable with the new data:
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row!["Alias", "Token ID"]);
- for (alias, token_id) in map.iter() {
- table.add_row(row![alias, token_id]);
- }
- if table.is_empty() {
- println!("No aliases found");
- } else {
- println!("{table}");
- }
- Ok(())
- }
- AliasSubcmd::Remove { alias } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.remove_alias(alias).await {
- eprintln!("Failed to remove alias: {e:?}");
- exit(2);
- }
- Ok(())
- }
- },
- Subcmd::Token { command } => match command {
- TokenSubcmd::Import { secret_key, token_blind } => {
- let mint_authority = match SecretKey::from_str(&secret_key) {
- Ok(ma) => ma,
- Err(e) => {
- eprintln!("Invalid mint authority: {e:?}");
- exit(2);
- }
- };
- let token_blind = match BaseBlind::from_str(&token_blind) {
- Ok(tb) => tb,
- Err(e) => {
- eprintln!("Invalid token blind: {e:?}");
- exit(2);
- }
- };
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- let token_id = drk.import_mint_authority(mint_authority, token_blind).await?;
- println!("Successfully imported mint authority for token ID: {token_id}");
- Ok(())
- }
- TokenSubcmd::GenerateMint => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- let mint_authority = SecretKey::random(&mut OsRng);
- let token_blind = BaseBlind::random(&mut OsRng);
- let token_id = drk.import_mint_authority(mint_authority, token_blind).await?;
- println!("Successfully imported mint authority for token ID: {token_id}");
- Ok(())
- }
- TokenSubcmd::List => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- let tokens = drk.get_mint_authorities().await?;
- let aliases_map = match drk.get_aliases_mapped_by_token().await {
- Ok(map) => map,
- Err(e) => {
- eprintln!("Failed to fetch wallet aliases: {e:?}");
- exit(2);
- }
- };
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row![
- "Token ID",
- "Aliases",
- "Mint Authority",
- "Token Blind",
- "Frozen"
- ]);
- for (token_id, authority, blind, frozen) in tokens {
- let aliases = match aliases_map.get(&token_id.to_string()) {
- Some(a) => a,
- None => "-",
- };
- table.add_row(row![token_id, aliases, authority, blind, frozen]);
- }
- if table.is_empty() {
- println!("No tokens found");
- } else {
- println!("{table}");
- }
- Ok(())
- }
- TokenSubcmd::Mint { token, amount, recipient, spend_hook, user_data } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = f64::from_str(&amount) {
- eprintln!("Invalid amount: {e:?}");
- exit(2);
- }
- let rcpt = match PublicKey::from_str(&recipient) {
- Ok(r) => r,
- Err(e) => {
- eprintln!("Invalid recipient: {e:?}");
- exit(2);
- }
- };
- let token_id = match drk.get_token(token).await {
- Ok(t) => t,
- Err(e) => {
- eprintln!("Invalid Token ID: {e:?}");
- exit(2);
- }
- };
- let spend_hook = match spend_hook {
- Some(s) => match FuncId::from_str(&s) {
- Ok(s) => Some(s),
- Err(e) => {
- eprintln!("Invalid spend hook: {e:?}");
- exit(2);
- }
- },
- None => None,
- };
- let user_data = match user_data {
- Some(u) => {
- let bytes: [u8; 32] = match bs58::decode(&u).into_vec()?.try_into() {
- Ok(b) => b,
- Err(e) => {
- eprintln!("Invalid user data: {e:?}");
- exit(2);
- }
- };
- match pallas::Base::from_repr(bytes).into() {
- Some(v) => Some(v),
- None => {
- eprintln!("Invalid user data");
- exit(2);
- }
- }
- }
- None => None,
- };
- let tx = match drk.mint_token(&amount, rcpt, token_id, spend_hook, user_data).await
- {
- Ok(tx) => tx,
- Err(e) => {
- eprintln!("Failed to create token mint transaction: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- drk.stop_rpc_client().await
- }
- TokenSubcmd::Freeze { token } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let token_id = match drk.get_token(token).await {
- Ok(t) => t,
- Err(e) => {
- eprintln!("Invalid Token ID: {e:?}");
- exit(2);
- }
- };
- let tx = match drk.freeze_token(token_id).await {
- Ok(tx) => tx,
- Err(e) => {
- eprintln!("Failed to create token freeze transaction: {e:?}");
- exit(2);
- }
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- drk.stop_rpc_client().await
- }
- },
- Subcmd::Contract { command } => match command {
- ContractSubcmd::GenerateDeploy => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- if let Err(e) = drk.deploy_auth_keygen().await {
- eprintln!("Error creating deploy auth keypair: {:?}", e);
- exit(2);
- }
- Ok(())
- }
- ContractSubcmd::List => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- None,
- ex,
- args.fun,
- )
- .await;
- let auths = drk.list_deploy_auth().await?;
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row!["Index", "Contract ID", "Frozen"]);
- for (idx, contract_id, frozen) in auths {
- table.add_row(row![idx, contract_id, frozen]);
- }
- if table.is_empty() {
- println!("No deploy authorities found");
- } else {
- println!("{table}");
- }
- Ok(())
- }
- ContractSubcmd::Deploy { deploy_auth, wasm_path, deploy_ix } => {
- // Read the wasm bincode and deploy instruction
- let wasm_bin = smol::fs::read(expand_path(&wasm_path)?).await?;
- let deploy_ix = smol::fs::read(expand_path(&deploy_ix)?).await?;
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let mut tx = match drk.deploy_contract(deploy_auth, wasm_bin, deploy_ix).await {
- Ok(v) => v,
- Err(e) => {
- eprintln!("Error creating contract deployment tx: {}", e);
- exit(2);
- }
- };
- if let Err(e) = drk.attach_fee(&mut tx).await {
- eprintln!("Failed to attach the fee call to the transaction: {e:?}");
- exit(2);
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- drk.stop_rpc_client().await
- }
- ContractSubcmd::Lock { deploy_auth } => {
- let drk = new_wallet(
- blockchain_config.wallet_path,
- blockchain_config.wallet_pass,
- Some(blockchain_config.endpoint),
- ex,
- args.fun,
- )
- .await;
- let mut tx = match drk.lock_contract(deploy_auth).await {
- Ok(v) => v,
- Err(e) => {
- eprintln!("Error creating contract lock tx: {}", e);
- exit(2);
- }
- };
- if let Err(e) = drk.attach_fee(&mut tx).await {
- eprintln!("Failed to attach the fee call to the transaction: {e:?}");
- exit(2);
- };
- println!("{}", base64::encode(&serialize_async(&tx).await));
- drk.stop_rpc_client().await
- }
- },
- }
- }
|