use std::error::Error; use std::io::{stdin, BufRead}; use ::cadence_json::{AddressOwned, ValueOwned}; use flow_sdk::prelude::*; use secp256k1::{PublicKey, Secp256k1, SecretKey}; #[tokio::main] async fn main() -> Result<(), Box> { let stdin = stdin(); let mut stdin = stdin.lock(); let mut buf = String::new(); // Let's make a transaction! // First, generate a keypair for us to use. let secp = Secp256k1::signing_only(); // `EntropyRng` is a secure random number generator. let mut rng = secp256k1::rand::rngs::EntropyRng::new(); let secret_key = SecretKey::new(&mut rng); let public_key = PublicKey::from_secret_key(&secp, &secret_key); // Print the public key as a hexadecimal. println!("This is your public key:"); let public_key_bytes = public_key.serialize_uncompressed(); // There is a leading 0x04 which we don't need to emit // https://bitcoin.stackexchange.com/a/3043 for ch in &public_key_bytes[1..] { print!("{:02x}", *ch); } println!(); println!("Go to https://flow-faucet.vercel.app/, select secp256k1 and sha3 and create your testnet account."); println!("Paste the address you get and press ENTER to continue"); stdin.read_line(&mut buf)?; let addr = buf.trim(); let address: AddressOwned = addr.parse()?; let net = TonicHyperFlowClient::testnet().await?; let mut account = Account::<_, _>::new(net, &address.data, secret_key).await?; let create_account = CreateAccountTransaction { public_keys: &[public_key], }; let create_account_header = create_account.to_header::<_, tiny_keccak::Sha3>(account.signer()); let res = account .send_transaction_header(&create_account_header) .await?; println!( "Just made {} to create another account :p", hex::encode(&res.id) ); let response = res.finalize(account.client()).await?; match response { Some(res) => { for ev in res.events.iter() { if ev.ty == "flow.AccountCreated" { let payload = ev.parse_payload()?; let addr = payload .fields .into_iter() .find(|field| field.name == "address") .map(|field| field.value) .unwrap(); if let ValueOwned::Address(addr) = addr { println!("Created account's address is: {}", addr); } } } } None => { panic!("The transaction did not get sealed... Perhaps the network is malfunctioning?") } } Ok(()) }