pwr-rs 0.2.7

PWR Chain Rust Tool
Documentation
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
pub mod types;
pub mod keys;
use hex;

use std::{fmt::Display, hash::Hash};
use k256::ecdsa::{
    signature::DigestVerifier, Error, Signature, SigningKey,
};
use sha3::{Digest, Keccak256};

use crate::wallet::types::{PublicKey, Wallet};
use crate::transaction::types::{NewTransactionData, Transaction};
use crate::rpc::RPC;

const NODE_URL: &str = "https://pwrrpc.pwrlabs.io/";

impl Wallet {
    #[cfg(feature = "rand")]
    /// Generate a new wallet using random private key.
    pub fn random() -> Self {
        let mut thread_rng = rand::thread_rng();
        let signing_key = SigningKey::random(&mut thread_rng);

        Self {
            private_key: signing_key,
        }
    }

    pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
        let bytes = if hex_str.len() > 2 && (&hex_str[..2] == "0x" || &hex_str[..2] == "0X") {
            hex::decode(&hex_str[2..]).map_err(|_| Error::new())?
        } else {
            hex::decode(hex_str).map_err(|_| Error::new())?
        };
        let private_key = SigningKey::from_slice(&bytes)?;

        Ok(Self { private_key })
    }

    pub fn to_hex(&self) -> String {
        hex::encode_upper(self.private_key.to_bytes())
    }

    pub fn sign(&self, message: &[u8]) -> Result<[u8; 65], Error> {
        let digest = Keccak256::new_with_prefix(message);
        let (sign, rid) = self.private_key.sign_digest_recoverable(digest)?;
        let mut bytes = vec![];
        bytes.extend_from_slice(&sign.to_bytes());

        if rid.to_byte() == 0 || rid.to_byte() == 1 {
            bytes.push(rid.to_byte() + 27);
        }
        Ok(bytes.try_into().unwrap())
    }

    pub fn verify_sign(&self, message: &[u8], signature: &[u8; 65]) -> Result<(), Error> {
        let digest = Keccak256::new_with_prefix(message);
        let sign = Signature::from_slice(&signature[..64])?;
        self.private_key
            .verifying_key()
            .verify_digest(digest, &sign)
    }

    pub fn get_public_key(&self) -> PublicKey {
        PublicKey {
            verifying_key: *self.private_key.verifying_key(),
        }
    }

    pub fn get_private_key(&self) -> String {
        let pk = self.private_key.to_bytes();
        format!("0x{}", hex::encode(pk))
    }

    pub fn get_address(&self) -> String {
        let public_key = self.get_public_key().verifying_key.to_encoded_point(false);
        let digest = Keccak256::new_with_prefix(&public_key.as_bytes()[1..]).finalize();
        format!("0x{}", hex::encode_upper(&digest[12..]))
    }

    pub async fn get_balance(&self) -> u64 {
        let rpc = RPC::new(NODE_URL).await.unwrap();
        let balance = rpc.get_balance_of_address(&self.get_address()).await.unwrap();
        return balance;
    }

    pub async fn get_nonce(&self) -> u32 {
        let rpc = RPC::new(NODE_URL).await.unwrap();
        let nonce = rpc.get_nonce_of_address(&self.get_address()).await.unwrap();
        return nonce;
    }

    pub async fn transfer_pwr(&self, recipient: String, amount: u64) -> String {
        let tx = NewTransactionData::Transfer {
            amount: amount,
            recipient: recipient,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&tx, &self).await.unwrap();
        return hash;
    }

    pub async fn send_vm_data(&self, vm_id: u64, data: Vec<u8>) -> String {
        let new_tx = NewTransactionData::VmData { vm_id: vm_id, data: data };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn send_payable_vm_data(&self, vm_id: u64, amount: u64, data: Vec<u8>) -> String {
        let new_tx = NewTransactionData::PayableVmData { 
            vm_id: vm_id, 
            data: data, 
            amount: amount 
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn claim_vm_id(&self, vm_id: u64) -> String {
        let new_tx = NewTransactionData::ClaimVmID { vm_id: vm_id };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn join(&self, ip: String) -> String {
        let new_tx = NewTransactionData::Join { ip: ip };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn claim_spot(&self) -> String {
        let address = self.get_address().to_string().strip_prefix("0x").unwrap_or(&self.get_address()).to_string();
        let new_tx = NewTransactionData::ClaimSpot { validator: address };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn delegate(&self, validator: String, amount: u64) -> String {
        let new_tx = NewTransactionData::Delegate {
            amount: amount,
            validator: validator,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn withdraw(&self, validator: String, shares: u64) -> String {
        let new_tx = NewTransactionData::Withdraw {
            shares: shares,
            validator: validator,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn set_guardian(&self, guardian: String, guardian_expiry_date: u64) -> String {
        let new_tx = NewTransactionData::SetGuardian {
            guardian_expiry_date: guardian_expiry_date,
            guardian: guardian,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn send_guardian_approval_transaction(&self, transactions: Vec<Transaction>) -> String {
        let new_tx = NewTransactionData::GuardianApproval { transactions: transactions };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn remove_guardian(&self) -> String {
        let new_tx = NewTransactionData::RemoveGuardian;
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn set_conduits(&self, vm_id: u64, conduits: Vec<String>) -> String {
        let new_tx = NewTransactionData::SetConduits { vm_id: vm_id, conduits: conduits };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn add_conduits(&self, vm_id: u64, conduits: Vec<u8>) -> String {
        let new_tx = NewTransactionData::AddConduits { vm_id: vm_id, conduits: conduits };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn move_stake(
        &self, shares_amount: u64, from_validator: String, to_validator: String
    ) -> String {
        let new_tx = NewTransactionData::MoveStake {
            shares_amount: shares_amount,
            from_validator: from_validator,
            to_validator: to_validator,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn create_proposal_change_early_withdraw_penalty(
        &self, withdraw_penalty: u32, withdraw_penalty_time: u64, title: String, description: String
    ) -> String {
        let new_tx = NewTransactionData::ChangeEarlyWithdrawPenaltyProposal {
            title: title,
            description: description,
            withdraw_penalty_time: withdraw_penalty_time,
            withdraw_penalty: withdraw_penalty,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn create_proposal_change_fee_per_byte(
        &self, fee_per_byte: u64, title: String, description: String
    ) -> String {
        let new_tx = NewTransactionData::ChangeFeePerByteProposal {
            title: title,
            description: description,
            fee_per_byte: fee_per_byte,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn create_proposal_change_max_block_size(
        &self, max_block_size: u32, title: String, description: String
    ) -> String {
        let new_tx = NewTransactionData::ChangeMaxBlockSizeProposal {
            title: title,
            description: description,
            max_block_size: max_block_size,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn create_proposal_change_max_txn_size(
        &self, max_txn_size: u32, title: String, description: String
    ) -> String {
        let new_tx = NewTransactionData::ChangeMaxTxnSizeProposal {
            title: title,
            description: description,
            max_txn_size: max_txn_size,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn create_proposal_change_overall_burn_percentage(
        &self, burn_percentage: u32, title: String, description: String
    ) -> String {
        let new_tx = NewTransactionData::ChangeOverallBurnPercentageProposal {
            title: title,
            description: description,
            burn_percentage: burn_percentage,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash; 
    }

    pub async fn create_proposal_change_reward_per_year(
        &self, reward_per_year: u64, title: String, description: String
    ) -> String {
        let new_tx = NewTransactionData::ChangeRewardPerYearProposal {
            title: title,
            description: description,
            reward_per_year: reward_per_year,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn create_proposal_change_validator_count_limit(
        &self, validator_count_limit: u32, title: String, description: String
    ) -> String {
        let new_tx = NewTransactionData::ChangeValidatorCountLimitProposal {
            title: title,
            description: description,
            validator_count_limit: validator_count_limit,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn create_proposal_change_validator_joining_fee(
        &self, joining_fee: u64, title: String, description: String
    ) -> String {
        let new_tx = NewTransactionData::ChangeValidatorJoiningFeeProposal {
            title: title,
            description: description,
            joining_fee: joining_fee,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn create_proposal_change_vm_id_claiming_fee(
        &self, claiming_fee: u64, title: String, description: String
    ) -> String {
        let new_tx = NewTransactionData::ChangeVmIdClaimingFeeProposal {
            title: title,
            description: description,
            claiming_fee: claiming_fee,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn create_proposal_change_vm_owner_txn_fee_share(
        &self, fee_share: u32, title: String, description: String
    ) -> String {
        let new_tx = NewTransactionData::ChangeVmOwnerTxnFeeShareProposal {
            title: title,
            description: description,
            fee_share: fee_share,
        };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn create_proposal_other_proposal(&self, title: String, description: String) -> String {
        let new_tx = NewTransactionData::OtherProposalTxn { title: title, description: description };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    pub async fn vote_on_proposal(&self, proposal_hash: String, vote: u8) -> String {
        let new_tx = NewTransactionData::VoteOnProposalTxn { proposal_hash: proposal_hash, vote: vote };
        let hash = (self.get_rpc().await).broadcast_transaction(&new_tx, &self).await.unwrap();
        return hash;
    }

    async fn get_rpc(&self) -> RPC {
        RPC::new(NODE_URL).await.unwrap()
    }
}

impl Hash for Wallet {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        let bytes = self.private_key.to_bytes();
        state.write(&bytes)
    }
}

impl TryFrom<String> for Wallet {
    type Error = Error;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::from_hex(&value)
    }
}

impl From<Wallet> for String {
    fn from(value: Wallet) -> Self {
        value.to_hex()
    }
}

impl Display for Wallet {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.to_hex())
    }
}

#[cfg(feature = "borsh")]
impl borsh::BorshSerialize for super::Wallet {
    fn serialize<W: std::io::prelude::Write>(&self, writer: &mut W) -> std::io::Result<()> {
        write!(writer, "{}", self.to_hex())
    }
}

#[cfg(feature = "borsh")]
impl borsh::BorshDeserialize for super::Wallet {
    fn deserialize_reader<R: std::io::prelude::Read>(reader: &mut R) -> std::io::Result<Self> {
        let s = String::deserialize_reader(reader)?;
        Self::from_hex(&s)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e.to_string()))
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    const PRIVATE_KEY_HEX: &str =
        "0x9D4428C6E0638331B4866B70C831F8BA51C11B031F4B55EED4087BBB8EF0151F";

    #[test]
    fn wallet_can_be_created_from_hex_string() {
        Wallet::from_hex(PRIVATE_KEY_HEX).unwrap();
    }

    #[test]
    fn wallet_can_be_encoded_to_hex_string() {
        let wallet = Wallet::from_hex(PRIVATE_KEY_HEX).unwrap();
        let encoded_wallet = wallet.to_hex();
        assert_eq!(format!("0x{}", encoded_wallet), PRIVATE_KEY_HEX);
    }

    #[test]
    fn can_get_public_key_from_wallet() {
        let wallet = Wallet::from_hex(PRIVATE_KEY_HEX).unwrap();
        let public_key = wallet.get_public_key();
        assert_eq!(public_key, PublicKey::from_hex("040cd999a20b0eba1cf86362c738929671902c9b337ab1370d2ba790be68b01227cab9fa9096b87651686bf898acf11857906907ba7fca4f5f5d9513bdd16e0a52").unwrap());
    }

    #[test]
    fn can_get_address_from_public_key() {
        let wallet = Wallet::from_hex(PRIVATE_KEY_HEX).unwrap();
        let address = wallet.get_address();
        assert_eq!(address, "0xA4710E3D79E1ED973AF58E0F269E9B21DD11BC64");
    }

    #[test]
    fn can_sign_message() {
        let wallet = Wallet::from_hex(PRIVATE_KEY_HEX).unwrap();
        let sign = wallet.sign(b"Hello World").unwrap();
        assert_eq!(
            hex::encode_upper(&sign),
            "4BFE08E9CDD47B064A812011E8DEC867D35833C072047958729BD5FE950F62B53E47C450BA8FED1D190D6ABB60B20ADC32237C5C072C0E1AA56CDBA023062D621B"
        );
    }

    #[test]
    fn can_verify_signed_message() {
        let wallet = Wallet::from_hex(PRIVATE_KEY_HEX).unwrap();
        let sign = wallet.sign(b"Hello World").unwrap();
        wallet.verify_sign(b"Hello World", &sign).unwrap();
    }
}