MCPcopy Create free account
hub / github.com/PacktPublishing/Rust-for-Blockchain-Application-Development / main

Function main

chapter 5/main.rs:60–185  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

58}
59
60fn main() {
61 env_logger::builder().filter_level(LevelFilter::Info).init();
62 let opt = Opt::from_args();
63 match opt.command {
64 Command::Createblockchain { address } => {
65 let blockchain = Blockchain::create_blockchain(address.as_str());
66 let utxo_set = UTXOSet::new(blockchain);
67 utxo_set.reindex();
68 println!("Done!");
69 }
70 Command::Createwallet => {
71 let mut wallet = Wallets::new();
72 let address = wallet.create_wallet();
73 println!("Your new address: {}", address)
74 }
75 Command::GetBalance { address } => {
76 let address_valid = validate_address(address.as_str());
77 if address_valid == false {
78 panic!("ERROR: Address is not valid")
79 }
80 let payload = utils::base58_decode(address.as_str());
81 let pub_key_hash = &payload[1..payload.len() - ADDRESS_CHECK_SUM_LEN];
82
83 let blockchain = Blockchain::new_blockchain();
84 let utxo_set = UTXOSet::new(blockchain);
85 let utxos = utxo_set.find_utxo(pub_key_hash);
86 let mut balance = 0;
87 for utxo in utxos {
88 balance += utxo.get_value();
89 }
90 println!("Balance of {}: {}", address, balance);
91 }
92 Command::ListAddresses => {
93 let wallets = Wallets::new();
94 for address in wallets.get_addresses() {
95 println!("{}", address)
96 }
97 }
98 Command::Send {
99 from,
100 to,
101 amount,
102 mine,
103 } => {
104 if !validate_address(from.as_str()) {
105 panic!("ERROR: Sender address is not valid")
106 }
107 if !validate_address(to.as_str()) {
108 panic!("ERROR: Recipient address is not valid")
109 }
110 let blockchain = Blockchain::new_blockchain();
111 let utxo_set = UTXOSet::new(blockchain.clone());
112
113 let transaction =
114 Transaction::new_utxo_transaction(from.as_str(), to.as_str(), amount, &utxo_set);
115
116 if mine == MINE_TRUE {
117

Callers

nothing calls this directly

Calls 15

validate_addressFunction · 0.70
base58_decodeFunction · 0.70
hash_pub_keyFunction · 0.70
convert_addressFunction · 0.70
send_txFunction · 0.50
reindexMethod · 0.45
create_walletMethod · 0.45
lenMethod · 0.45
find_utxoMethod · 0.45
get_valueMethod · 0.45
get_addressesMethod · 0.45
mine_blockMethod · 0.45

Tested by

no test coverage detected