diff --git a/Cargo.lock b/Cargo.lock index 82f8d75..5f203c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1046,6 +1046,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "encoding_rs" +version = "0.8.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" +dependencies = [ + "cfg-if", +] + [[package]] name = "env_filter" version = "0.1.2" @@ -1169,6 +1178,21 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -1587,6 +1611,23 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-rustls" +version = "0.27.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" +dependencies = [ + "futures-util", + "http", + "hyper", + "hyper-util", + "rustls", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tower-service", +] + [[package]] name = "hyper-timeout" version = "0.5.1" @@ -1600,6 +1641,22 @@ dependencies = [ "tower-service", ] +[[package]] +name = "hyper-tls" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" +dependencies = [ + "bytes", + "http-body-util", + "hyper", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", +] + [[package]] name = "hyper-util" version = "0.1.9" @@ -1686,6 +1743,12 @@ version = "4.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d762194228a2f1c11063e46e32e5acb96e66e906382b9eb5441f2e0504bbd5a" +[[package]] +name = "ipnet" +version = "2.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" + [[package]] name = "is-terminal" version = "0.4.13" @@ -1930,6 +1993,23 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03" +[[package]] +name = "native-tls" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "nohash-hasher" version = "0.2.0" @@ -2093,12 +2173,50 @@ version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +[[package]] +name = "openssl" +version = "0.10.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] + [[package]] name = "openssl-probe" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "openssl-sys" +version = "0.9.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "pairing" version = "0.23.0" @@ -2195,6 +2313,12 @@ dependencies = [ "spki", ] +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + [[package]] name = "platform-cli" version = "0.1.5" @@ -2210,7 +2334,10 @@ dependencies = [ "log", "rand", "regex", + "reqwest", "rs-dapi-client", + "serde", + "serde_json", "sha256", "simple-signer", "tokio", @@ -2522,6 +2649,49 @@ dependencies = [ "bytecheck", ] +[[package]] +name = "reqwest" +version = "0.12.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" +dependencies = [ + "base64 0.22.1", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-tls", + "hyper-util", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper 1.0.1", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "windows-registry", +] + [[package]] name = "ring" version = "0.17.8" @@ -2863,6 +3033,18 @@ dependencies = [ "syn 2.0.79", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_with" version = "2.3.3" @@ -3121,6 +3303,30 @@ name = "sync_wrapper" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" +dependencies = [ + "futures-core", +] + +[[package]] +name = "system-configuration" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" +dependencies = [ + "bitflags", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] [[package]] name = "tap" @@ -3306,6 +3512,16 @@ dependencies = [ "syn 2.0.79", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.26.0" @@ -3582,6 +3798,12 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8c5f0a0af699448548ad1a2fbf920fb4bee257eae39953ba95cb84891a0446a" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.5" @@ -3682,6 +3904,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.95" @@ -3711,6 +3945,16 @@ version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" +[[package]] +name = "web-sys" +version = "0.3.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "webpki-roots" version = "0.26.6" @@ -3738,6 +3982,36 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-registry" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" +dependencies = [ + "windows-result", + "windows-strings", + "windows-targets", +] + +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result", + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 6655e6d..e4983cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,4 +20,7 @@ rand = "0.8.5" getrandom = "0.2.15" anyhow = "1.0.89" log = "0.4.22" -regex = "1.11.0" \ No newline at end of file +regex = "1.11.0" +serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1.0" } +reqwest = { version = "0.12.9", features = ["json"] } \ No newline at end of file diff --git a/src/api/digitalcash.rs b/src/api/digitalcash.rs new file mode 100644 index 0000000..a7d4698 --- /dev/null +++ b/src/api/digitalcash.rs @@ -0,0 +1,64 @@ +use std::collections::HashMap; +use dpp::dashcore::{Address, Network}; +use serde::{Deserialize, Serialize}; + +pub struct DigitalCashAPI { + url: String, +} + +#[derive(Serialize)] +pub struct JsonRPCArguments { + method: String, + params: Vec>> +} +#[derive(Serialize, Deserialize)] +pub struct AddressesUtxoResult { + pub address: String, + pub txid: String, + #[serde(rename(deserialize = "outputIndex"))] + pub output_index: u32, + pub script: String, + pub satoshis: u64, + pub height: u32 +} + +#[derive(Deserialize)] +pub struct JsonResponse { + result: Vec, +} + +impl DigitalCashAPI { + pub fn new(network: Network) -> Self { + let url = match network { + Network::Dash => "https://rpc.digitalcash.dev", + Network::Testnet => "https://trpc.digitalcash.dev", + _ => panic!("Network {} is not supported by digitalcash.dev", network) + }; + + return DigitalCashAPI { url: String::from(url) }; + } + + + pub async fn get_address_utxos(&self, address: Address) -> Vec { + let mut params: HashMap> = HashMap::new(); + + params.insert(String::from("addresses"), vec![address.to_string()]); + + let p = JsonRPCArguments { + method: String::from("getaddressutxos"), + params: vec![params], + }; + + let res = reqwest::Client::new() + .post(&self.url) + .json(&p) + .send() + .await.unwrap(); + + let js = res + .json::() + .await.unwrap(); + + return js.result; + } +} \ No newline at end of file diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 0000000..1241f13 --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1 @@ +pub mod digitalcash; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index c857268..ae70847 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,3 +1,4 @@ pub mod register_dpns_name; pub mod withdraw; pub mod masternode_vote_dpns_name; +pub mod register_identity; diff --git a/src/commands/register_identity.rs b/src/commands/register_identity.rs new file mode 100644 index 0000000..de1142f --- /dev/null +++ b/src/commands/register_identity.rs @@ -0,0 +1,353 @@ +use std::fs; +use std::ops::Add; +use std::str::FromStr; +use clap::{Parser}; +use dpp::dashcore::hashes::Hash; +use dpp::dashcore::{Address, Network, OutPoint, ScriptBuf, Transaction, Txid, TxIn, TxOut}; +use dpp::dashcore::psbt::serialize::Serialize; +use dpp::dashcore::secp256k1::hashes::hex::DisplayHex; +use dpp::dashcore::secp256k1::{Message, Secp256k1}; +use dpp::dashcore::sighash::{LegacySighash, SighashCache}; +use dpp::dashcore::transaction::special_transaction::asset_lock::AssetLockPayload; +use dpp::dashcore::transaction::special_transaction::TransactionPayload; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::serialization::PlatformSerializable; +use crate::errors::cli_argument_missing_error::CommandLineArgumentMissingError; +use crate::errors::Error; +use crate::utils::{MyDefaultEntropyGenerator, Utils}; +use crate::api::digitalcash::DigitalCashAPI; + +/// Register an Identity Name in the Dash Platform DPNS system. +#[derive(Parser)] +pub struct RegisterIdentityCommand { + /// Network, mainnet or testnet + #[clap(long, default_value(""))] + network: String, + + /// DAPI GRPC Endpoint URL, ex. https://127.0.0.1:1443 + #[clap(long, default_value(""))] + dapi_url: String, + + /// Path to file with private key from Identity in WIF format + #[clap(long, default_value(""))] + private_key: String, + + /// Amount in DASH + #[clap(long, default_value(""))] + amount: String, + + /// Enable verbose logging for a debugging + #[clap(long)] + pub verbose: bool, +} + +// create assetlock ( +// create identitycreate transition + +impl RegisterIdentityCommand { + pub async fn run(&self) -> Result<(), Error> { + if self.network.is_empty() { + return Err(Error::CommandLineArgumentMissingError(CommandLineArgumentMissingError::from("network"))); + } + + if self.dapi_url.is_empty() { + return Err(Error::CommandLineArgumentMissingError(CommandLineArgumentMissingError::from("dapi_url"))); + } + + if self.private_key.is_empty() { + return Err(Error::CommandLineArgumentMissingError(CommandLineArgumentMissingError::from("private_key"))); + } + + let secp = Secp256k1::new(); + let network_type: Network = Network::from_str(&self.network).expect("Could not parse network"); + let private_key_data = fs::read_to_string(&self.private_key).expect("Unable to read private key file"); + let private_key = Utils::decode_private_key_from_input_string(private_key_data.as_str(), network_type)?; + let public_key = private_key.public_key(&secp); + let address = Address::p2pkh(&public_key, network_type); + + let rpc = DigitalCashAPI::new(network_type); + let resp = rpc.get_address_utxos(address).await; + + let inputs: Vec<(TxIn, u64)> = resp.into_iter().map(|utxo| { + let tx_in = TxIn { + previous_output: OutPoint { + txid: Txid::from_hex(&utxo.txid).unwrap(), + vout: utxo.output_index, + }, + script_sig: ScriptBuf::from_hex(&utxo.script).unwrap(), + sequence: 0xFFFFFFFF, + witness: Default::default(), + }; + + return (tx_in, utxo.satoshis); + }) + .collect::>(); + + let balance = inputs.iter().fold(0, |acc, (_, satoshis)| { + return acc + satoshis; + }); + + let float = self.amount.parse::().unwrap(); + let funding_amount = (float * 10e7).floor() as u64; + let gas_fee: u64 = 10000; + + if &balance < (&funding_amount.add(gas_fee)) { + panic!() + } + + + println!(); + // let re = Regex::new(r"^[a-zA-Z01-]{3,19}$").unwrap(); + // + // let normalized_name = convert_to_homograph_safe_chars(&self.name); + // let full_domain_name = format!("{}.dash", &self.name); + // let is_contested = re.is_match(&self.name); + // + // info!("Starting registering DPNS name process ({})", &self.network); + // info!("Name: {}, Normalized Name: {}, Full Domain Name: {}, Is Contested: {}", &self.name, normalized_name.clone(), &full_domain_name, is_contested); + // + + // let identifier = Identifier::from_string(&self.identity, Base58).unwrap(); + // + // let dpns_contract = DataContract::from_value(Constants::dpns_data_contract_value(), true, PlatformVersion::latest()).unwrap(); + // + // // get utxo by the address + // + // + // create assetlock transaction + enum OpCode { + Return, + Dup, + Hash160, + EqualVerify, + CheckSig, + } + + let (assetlock_amount, change_amount) = Utils::coin_select(inputs.clone(), funding_amount, 10000); + + // change script (to the same address) + let (address_script, _) = inputs.clone().first().unwrap().clone(); + + let assetlock_output = TxOut { + value: assetlock_amount, + script_pubkey: ScriptBuf::from(vec![ + 0x6a, + 0x00, + ]), + }; + // 76a914a7b2ece31ad0d9b5857d110a1bc6447432470d4e88ac + let change_output = TxOut { value: change_amount, script_pubkey: address_script.script_sig }; + + let credit_output = TxOut { value: assetlock_amount, script_pubkey: Address::p2pkh(&public_key, network_type).script_pubkey() }; + + let mut transaction = Transaction { + version: 3, + lock_time: 0, + input: inputs.iter().map(|(input, _)| { input.clone() }).collect::>(), + output: vec![assetlock_output, change_output], + special_transaction_payload: Some(TransactionPayload::AssetLockPayloadType(AssetLockPayload { + version: 1, + credit_outputs: vec![credit_output], + })), + }; + + println!("{}", transaction.serialize().to_lower_hex_string()); + let cache = SighashCache::new(&transaction); + + let sighashes: Vec = transaction + .input + .iter() + .enumerate() + .map(|(i, input)| { + cache + .legacy_signature_hash(i, &input.script_sig, 1u32) + .expect("expected sighash") + }) + .collect(); + + transaction.input + .iter_mut() + .zip(sighashes.into_iter()) + .try_for_each(|(input, sighash)| { + let message = Message::from_digest(sighash.into()); + + // Sign the message with the private key + let sig = secp.sign_ecdsa(&message, &private_key.inner); + + // Serialize the DER-encoded signature and append the sighash type + let mut serialized_sig = sig.serialize_der().to_vec(); + + let mut sig_script = vec![serialized_sig.len() as u8 + 1]; + + sig_script.append(&mut serialized_sig); + + sig_script.push(1); + + let mut serialized_pub_key = private_key.public_key(&secp).serialize(); + + sig_script.push(serialized_pub_key.len() as u8); + sig_script.append(&mut serialized_pub_key); + // Create script_sig + input.script_sig = ScriptBuf::from_bytes(sig_script); + Ok::<(), String>(()) + }).unwrap(); + println!("{}", transaction.serialize().to_lower_hex_string()); + + // broadcast core transaction + + // wait for instant lock / chainlock + + // create assetLock Proof + + // create identitycreatetransition + + // sign & broadcast in the platform + + // let platform_grpc_client = PlatformGRPCClient::new(&self.dapi_url); + // + // let identity = platform_grpc_client + // .get_identity_by_identifier(identifier).await?; + // + // debug!("Identity with identifier {} found in the network", identity.id()); + // + // let identity_public_keys = platform_grpc_client + // .get_identity_keys(identity.id()).await; + // + // debug!("Finding matching IdentityPublicKey in the Identity against applied private key"); + // + // let identity_public_key = identity_public_keys + // .iter() + // .filter(|key| key.public_key_hash().unwrap() == <[u8; 20] as Into<[u8;20]>>::into(public_key.pubkey_hash().to_byte_array())) + // .collect::>() + // .first() + // .ok_or(Error::IdentityPublicKeyHashMismatchError(IdentityPublicKeyHashMismatchError::from((identifier, public_key.pubkey_hash()))))? + // .clone(); + // + // debug!("Found matching IdentityPublicKey id: {}, key_type: {}, pubkeyhash: {}, purpose: {}, security_level: {}", + // identity_public_key.id(), + // identity_public_key.key_type(), + // identity_public_key.public_key_hash().unwrap().to_lower_hex_string(), + // identity_public_key.purpose(), + // identity_public_key.security_level()); + // + // let identity_contract_nonce = platform_grpc_client.get_identity_contract_nonce(identity.id(), dpns_contract.id()).await; + // + // debug!("Identity contract nonce for identifier {} is {}", identity.id(), identity_contract_nonce.clone()); + // + // let mut rng = StdRng::from_entropy(); + // + // let salt: [u8; 32] = rng.gen(); + // + // let mut salted_domain_buffer: Vec = vec![]; + // salted_domain_buffer.extend(salt); + // salted_domain_buffer.extend((normalized_name.clone() + ".dash").as_bytes()); + // + // let salted_domain_hash = hash_double(salted_domain_buffer); + // + // debug!("Salted Domain Hash for {} is {}", normalized_name.clone() + ".dash", salted_domain_hash.to_lower_hex_string()); + // + // let generator = MyDefaultEntropyGenerator{}; + // let entropy = generator.generate().unwrap(); + // + // let pre_order_document = Factories::create_document(dpns_contract.id(), + // "preorder", + // identity.id(), + // platform_value!( + // { + // "saltedDomainHash": Value::Bytes32(salted_domain_hash) + // } + // ), Vec::from(entropy)); + // + // let pre_order_transition = Factories::document_create_transition(pre_order_document, "preorder", dpns_contract.id(), identity_contract_nonce.add(1), Vec::from(entropy), None); + // let mut preorder_state_transition = StateTransition::from(IdentityStateTransition{ + // identity: identity.id(), + // transitions: vec![pre_order_transition] + // }); + // + // debug!("Signing preorder transaction with IdentityPublicKey id: {}, key_type: {}, pubkeyhash: {}, purpose: {}, security_level: {}", + // identity_public_key.id(), + // identity_public_key.key_type(), + // identity_public_key.public_key_hash().unwrap().to_lower_hex_string(), + // identity_public_key.purpose(), + // identity_public_key.security_level()); + // preorder_state_transition.sign(identity_public_key, private_key.to_bytes().as_slice(), &MockBLS{}).unwrap(); + // + // let preorder_buffer = preorder_state_transition.clone().serialize_to_bytes().unwrap(); + // let preorder_hex = preorder_buffer.clone(); + // let preorder_hash = digest(preorder_buffer.clone()); + // + // debug!("Signed Preorder Transaction Hex: {}", preorder_hex.to_lower_hex_string()); + // info!("Preorder Transaction Hash: {}", preorder_hash); + // + // platform_grpc_client.broadcast_state_transition(preorder_state_transition).await; + // + // info!("Preorder document has been successfully sent into the network"); + // + // info!("Waiting 20s for a confirmation in the network"); + // sleep(Duration::from_millis(20000)).await; + // + // let domain_document = Factories::create_document(dpns_contract.id(), "domain", identity.id(), + // platform_value!( + // { + // "label": &self.name, + // "records": { + // "identity": identity.id(), + // }, + // "preorderSalt": Value::Bytes32(salt), + // "subdomainRules": { + // "allowSubdomains": false + // }, + // "normalizedLabel": normalized_name.clone(), + // "parentDomainName": "dash", + // "normalizedParentDomainName": "dash" + // } + // ), Vec::from(entropy)); + // + // let prefunding_voting_balance = match is_contested { + // true => {Some((String::from("parentNameAndLabel"), VOTE_RESOLUTION_FUND_FEES_VERSION1.contested_document_vote_resolution_fund_required_amount))}, + // false => None + // }; + // + // if !prefunding_voting_balance.is_none() { + // info!("Chosen name was detected as a contested resource, including 0.2 Dash in credits as a prefund for voting process"); + // } + // + // let domain_document_transition = Factories::document_create_transition( + // domain_document, + // "domain", + // dpns_contract.id(), + // identity_contract_nonce.add(2), + // Vec::from(entropy), prefunding_voting_balance); + // + // let mut domain_state_transition = StateTransition::from(IdentityStateTransition{ + // identity: identity.id(), + // transitions: vec![domain_document_transition] + // }); + // + // debug!("Signing domain transaction with IdentityPublicKey id: {}, key_type: {}, pubkeyhash: {}, purpose: {}, security_level: {}", + // identity_public_key.id(), + // identity_public_key.key_type(), + // identity_public_key.public_key_hash().unwrap().to_lower_hex_string(), + // identity_public_key.purpose(), + // identity_public_key.security_level()); + // domain_state_transition.sign(identity_public_key, private_key.to_bytes().as_slice(), &MockBLS{}).unwrap(); + // + // let domain_buffer = domain_state_transition.clone().serialize_to_bytes().unwrap(); + // let domain_hex = domain_buffer.clone(); + // let domain_hash = digest(domain_buffer.clone()); + // debug!("Signed Domain Transaction Hex: {}", domain_hex.to_lower_hex_string()); + // info!("Domain Transaction Hash: {}", domain_hash); + // + // platform_grpc_client.broadcast_state_transition(domain_state_transition).await; + // + // info!("Successfully registered DPNS Name {} for Identity {}", full_domain_name, identity.id().to_string(Base58)); + // info!("Please check your transactions on the Platform Explorer to make sure they all finished successfully"); + // + // if is_contested { + // info!("Your name was registered through the contested resource process, please check if your name appears on the https://dash.vote now"); + // } + + Ok(()) + } +} + diff --git a/src/main.rs b/src/main.rs index 39eb308..d40b31c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ pub(crate) mod utils; mod errors; mod logger; mod constants; +mod api; use clap::{Parser, Subcommand}; use dpp::{BlsModule, ProtocolError, PublicKeyValidationError}; @@ -13,6 +14,7 @@ use crate::commands::masternode_vote_dpns_name::MasternodeVoteDPNSNameCommand; use crate::commands::register_dpns_name::RegisterDPNSNameCommand; use crate::commands::withdraw::WithdrawCommand; use log::{info, LevelFilter}; +use crate::commands::register_identity::RegisterIdentityCommand; use crate::logger::Logger; pub struct MockBLS {} @@ -45,6 +47,7 @@ struct Args { #[derive(Subcommand)] enum MyCommand { Withdraw(WithdrawCommand), + RegisterIdentity(RegisterIdentityCommand), RegisterDPNSName(RegisterDPNSNameCommand), MasternodeVoteDPNSName(MasternodeVoteDPNSNameCommand) } @@ -82,6 +85,10 @@ async fn main() { set_logging_level(x.verbose).await; x.run().await }, + MyCommand::RegisterIdentity(x) => { + set_logging_level(x.verbose).await; + x.run().await + } }; match result { diff --git a/src/utils.rs b/src/utils.rs index fb15172..2d13a72 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,7 @@ use anyhow::Context; use base64::Engine; use base64::engine::general_purpose; -use dpp::dashcore::{Network, PrivateKey}; +use dpp::dashcore::{Network, PrivateKey, TxIn}; use dpp::util::entropy_generator::EntropyGenerator; use getrandom::getrandom; use crate::errors::cli_argument_invalid_input::CommandLineArgumentInvalidInput; @@ -39,10 +39,21 @@ impl Utils { } else if base64.len() > 0 { PrivateKey::from_slice(base64.as_slice(), network).expect("Unexpected error, could not construct private key from base64 after validation") } else { - return Err(Error::CommandLineArgumentInvalidInput(CommandLineArgumentInvalidInput::from("Could not decode private key type from file (should be in WIF or hex)"))) + return Err(Error::CommandLineArgumentInvalidInput(CommandLineArgumentInvalidInput::from("Could not decode private key type from file (should be in WIF or hex)"))); } }; Ok(private_key) } + pub fn coin_select(inputs: Vec<(TxIn, u64)>, needed_amount: u64, fee: u64) -> (u64, u64) { + return inputs.into_iter().fold((0, 0), |(output_amount, change_amount), (_, satoshis)| { + if output_amount + satoshis + fee >= needed_amount { + let change_amount = (output_amount + satoshis) - needed_amount - fee; + + return (needed_amount, change_amount) + } + + return (output_amount + satoshis, 0) + }) + } } \ No newline at end of file