-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmasternode_vote_dpns_name.rs
More file actions
172 lines (142 loc) · 7.79 KB
/
masternode_vote_dpns_name.rs
File metadata and controls
172 lines (142 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::fs;
use std::str::FromStr;
use clap::Parser;
use dpp::dashcore::hashes::Hash;
use dpp::dashcore::key::Secp256k1;
use dpp::dashcore::{Network, ProTxHash};
use dpp::dashcore::secp256k1::hashes::hex::DisplayHex;
use dpp::identifier::{Identifier, MasternodeIdentifiers};
use dpp::identity::accessors::IdentityGettersV0;
use dpp::identity::hash::IdentityPublicKeyHashMethodsV0;
use dpp::identity::{IdentityPublicKey};
use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0;
use dpp::platform_value::string_encoding::Encoding::{Base58};
use dpp::platform_value::Value;
use dpp::serialization::PlatformSerializable;
use dpp::state_transition::StateTransition;
use dpp::voting::vote_choices::resource_vote_choice::ResourceVoteChoice;
use log::{debug, info};
use sha256::digest;
use crate::errors::cli_argument_missing_error::CommandLineArgumentMissingError;
use crate::errors::Error;
use crate::errors::identity_public_key_hash_mismatch_error::IdentityPublicKeyHashMismatchError;
use crate::factories::Factories;
use crate::grpc::PlatformGRPCClient;
use crate::MockBLS;
use crate::utils::Utils;
/// Perform a masternode vote towards contested DPNS name
#[derive(Parser)]
pub struct MasternodeVoteDPNSNameCommand {
/// 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,
/// ProTxHash of the Masternode performing a Vote, in hex
#[clap(long, default_value(""))]
pro_tx_hash: String,
/// Path to file with voting (or owner) private key in WIF format
#[clap(long, default_value(""))]
private_key: String,
/// Normalized label to vote upon (can be grabbed from https//dash.vote)
#[clap(long, default_value(""))]
normalized_label: String,
/// The choice of the Vote.
/// It can be an Identifier you are voting towards (ex. BMJWm8wKmbApR7nQ6q7RG3HgD8maJ8t7B4yWBKRe7aZ6), or Lock, or Abstain
#[clap(long, default_value(""))]
choice: String,
/// Verbose
#[clap(long)]
pub verbose: bool,
}
const DPNS_DATA_CONTRACT_IDENTIFIER: &str = "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec";
impl MasternodeVoteDPNSNameCommand {
pub async fn run(&self) -> Result<(), Error> {
if self.network.is_empty() {
return Err(Error::CommandLineArgumentMissingError(CommandLineArgumentMissingError::from("network")));
}
if self.pro_tx_hash.is_empty() {
return Err(Error::CommandLineArgumentMissingError(CommandLineArgumentMissingError::from("pro_tx_hash")));
}
if self.normalized_label.is_empty() {
return Err(Error::CommandLineArgumentMissingError(CommandLineArgumentMissingError::from("normalized_label")));
}
if self.private_key.is_empty() {
return Err(Error::CommandLineArgumentMissingError(CommandLineArgumentMissingError::from("private_key")));
}
if self.dapi_url.is_empty() {
return Err(Error::CommandLineArgumentMissingError(CommandLineArgumentMissingError::from("dapi_url")));
}
if self.choice.is_empty() {
return Err(Error::CommandLineArgumentMissingError(CommandLineArgumentMissingError::from("choice")));
}
info!("Starting Masternode Vote on {}.dash DPNS name process with choice {} ({})", &self.normalized_label, &self.choice, &self.network);
let secp = Secp256k1::new();
let network = if &self.network == "mainnet" { "dash" } else { &self.network };
let network_type = Network::from_str(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 pro_tx_hash = ProTxHash::from_hex(&self.pro_tx_hash).expect("Could not decode pro tx hash");
let voting_address = public_key.pubkey_hash().to_byte_array();
let buffer: [u8; 32] = <[u8; 32]>::try_from(hex::decode(&self.pro_tx_hash).unwrap()).unwrap();
let voter_identity_id = Identifier::create_voter_identifier(&buffer, &voting_address);
let platform_grpc_client = PlatformGRPCClient::new(&self.dapi_url);
let identity = platform_grpc_client.get_identity_by_identifier(voter_identity_id).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::<Vec<&IdentityPublicKey>>()
.first()
.ok_or(Error::IdentityPublicKeyHashMismatchError(IdentityPublicKeyHashMismatchError::from((identity.id(), 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 nonce = platform_grpc_client.get_identity_nonce(identity.id()).await?;
debug!("Identity nonce for identifier {} is {}", identity.id(), nonce.clone());
let choice = match self.choice.as_str() {
"Lock" => ResourceVoteChoice::Lock,
"Abstain" => ResourceVoteChoice::Abstain,
_ => ResourceVoteChoice::TowardsIdentity(Identifier::from_string(&self.choice, Base58).unwrap()),
};
let masternode_vote_transition = Factories::create_masternode_vote_state_transition(
&pro_tx_hash.to_hex(),
voter_identity_id,
nonce,
Identifier::from_string(DPNS_DATA_CONTRACT_IDENTIFIER, Base58).unwrap(),
"domain",
"parentNameAndLabel",
vec![
Value::Text("dash".to_string()),
Value::Text(self.normalized_label.clone()),
],
choice,
);
let mut masternode_vote_state_transition = StateTransition::from(masternode_vote_transition);
debug!("Signing MasternodeVote 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());
masternode_vote_state_transition.sign(&identity_public_key, private_key.to_bytes().as_slice(), &MockBLS{}).unwrap();
let masternode_vote_buffer = masternode_vote_state_transition.clone().serialize_to_bytes().unwrap();
let masternode_vote_hex = masternode_vote_buffer.clone();
let masternode_vote_hash = digest(masternode_vote_buffer.clone());
debug!("Signed MasternodeVote Transaction Hex: {}", masternode_vote_hex.to_lower_hex_string());
info!("MasternodeVote Transaction Hash: {}", masternode_vote_hash);
platform_grpc_client.broadcast_state_transition(masternode_vote_state_transition).await;
println!("Masternode Vote for {}.dash DPNS name has been sucessfully submitted", &self.normalized_label);
info!("Please check your transaction on the Platform Explorer to make sure it finished successfully");
Ok(())
}
}