mwc_web3/api/
parity_accounts.rs

1use crate::{
2    api::Namespace,
3    helpers::{self, CallFuture},
4    types::{Address, H256},
5    Transport,
6};
7
8/// `Parity_Accounts` namespace
9#[derive(Debug, Clone)]
10pub struct ParityAccounts<T> {
11    transport: T,
12}
13
14impl<T: Transport> Namespace<T> for ParityAccounts<T> {
15    fn new(transport: T) -> Self
16    where
17        Self: Sized,
18    {
19        ParityAccounts { transport }
20    }
21
22    fn transport(&self) -> &T {
23        &self.transport
24    }
25}
26
27impl<T: Transport> ParityAccounts<T> {
28    /// Given an address of an account and its password deletes the account from the parity node
29    pub fn parity_kill_account(&self, address: &Address, pwd: &str) -> CallFuture<bool, T::Out> {
30        let address = helpers::serialize(&address);
31        let pwd = helpers::serialize(&pwd);
32        CallFuture::new(self.transport.execute("parity_killAccount", vec![address, pwd]))
33    }
34    /// Imports an account from a given seed/phrase
35    /// Retunrs the address of the corresponding seed vinculated account
36    pub fn parity_new_account_from_phrase(&self, seed: &str, pwd: &str) -> CallFuture<Address, T::Out> {
37        let seed = helpers::serialize(&seed);
38        let pwd = helpers::serialize(&pwd);
39        CallFuture::new(self.transport.execute("parity_newAccountFromPhrase", vec![seed, pwd]))
40    }
41    /// Imports an account from a given secret key.
42    /// Returns the address of the corresponding Sk vinculated account.
43    pub fn new_account_from_secret(&self, secret: &H256, pwd: &str) -> CallFuture<Address, T::Out> {
44        let secret = helpers::serialize(&secret);
45        let pwd = helpers::serialize(&pwd);
46        CallFuture::new(self.transport.execute("parity_newAccountFromSecret", vec![secret, pwd]))
47    }
48    /// Imports an account from a JSON encoded Wallet file.
49    /// Returns the address of the corresponding wallet.
50    pub fn parity_new_account_from_wallet(&self, wallet: &str, pwd: &str) -> CallFuture<Address, T::Out> {
51        let wallet = helpers::serialize(&wallet);
52        let pwd = helpers::serialize(&pwd);
53        CallFuture::new(self.transport.execute("parity_newAccountFromWallet", vec![wallet, pwd]))
54    }
55    /// Removes the address of the Parity node addressbook.
56    /// Returns true if the operation suceeded.
57    pub fn parity_remove_address(&self, address: &Address) -> CallFuture<bool, T::Out> {
58        let address = helpers::serialize(&address);
59        CallFuture::new(self.transport.execute("parity_removeAddress", vec![address]))
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::ParityAccounts;
66    use crate::{api::Namespace, rpc::Value};
67    use ethereum_types::{Address, H256};
68
69    rpc_test! (
70        ParityAccounts :   parity_kill_account,  &"9b776baeaf3896657a9ba0db5564623b3e0173e0".parse::<Address>().unwrap(), "123456789"
71        => "parity_killAccount", vec![r#""0x9b776baeaf3896657a9ba0db5564623b3e0173e0""#, r#""123456789""#];
72        Value::Bool(true) => true
73    );
74
75    rpc_test! (
76        ParityAccounts :   parity_new_account_from_phrase,  "member funny cloth wrist ugly water tuition always fall recycle maze long", "123456789"
77        => "parity_newAccountFromPhrase", vec![r#""member funny cloth wrist ugly water tuition always fall recycle maze long""#, r#""123456789""#];
78        Value::String("0xE43eD16390bd419d48B09d6E2aa20203D1eF93E1".into()) => "E43eD16390bd419d48B09d6E2aa20203D1eF93E1".parse::<Address>().unwrap()
79    );
80
81    rpc_test! (
82        ParityAccounts :   new_account_from_secret,  &"c6592108cc3577f6a2d6178bc6947b43db39057195802caa0120f26e39af4945".parse::<H256>().unwrap(), "123456789"
83        => "parity_newAccountFromSecret", vec![r#""0xc6592108cc3577f6a2d6178bc6947b43db39057195802caa0120f26e39af4945""#, r#""123456789""#];
84        Value::String("0x9b776Baeaf3896657A9ba0db5564623B3E0173e0".into()) => "9b776Baeaf3896657A9ba0db5564623B3E0173e0".parse::<Address>().unwrap()
85    );
86
87    rpc_test! (
88        ParityAccounts :   parity_new_account_from_wallet,  r#"{"version":3,"id":"3b330c3b-b0b3-4e39-b62e-c2041a98d673","address":"4c8ab9d3e938285776d6717d7319f6a9b1d809dd","Crypto":{"ciphertext":"bb3a6dbf21f0bf2b5eb0b43426590f16650acee9462ab710cca18781691a5739","cipherparams":{"iv":"6a533f77fc5cb8a752a16ec6a3200da1"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"a58609853dec53c81feb165e346c700e714285771825bb4cbf87c4ea1996b682","n":8192,"r":8,"p":1},"mac":"a71edeb659ed628db13579ce9f75c80c9d386c1239b280548d9a0e58ad20d6c7"}}"#, "123456789"
89        => "parity_newAccountFromWallet", vec![r#""{\"version\":3,\"id\":\"3b330c3b-b0b3-4e39-b62e-c2041a98d673\",\"address\":\"4c8ab9d3e938285776d6717d7319f6a9b1d809dd\",\"Crypto\":{\"ciphertext\":\"bb3a6dbf21f0bf2b5eb0b43426590f16650acee9462ab710cca18781691a5739\",\"cipherparams\":{\"iv\":\"6a533f77fc5cb8a752a16ec6a3200da1\"},\"cipher\":\"aes-128-ctr\",\"kdf\":\"scrypt\",\"kdfparams\":{\"dklen\":32,\"salt\":\"a58609853dec53c81feb165e346c700e714285771825bb4cbf87c4ea1996b682\",\"n\":8192,\"r\":8,\"p\":1},\"mac\":\"a71edeb659ed628db13579ce9f75c80c9d386c1239b280548d9a0e58ad20d6c7\"}}""#, r#""123456789""#];
90        Value::String("0x4C8aB9d3e938285776d6717d7319F6a9B1d809DD".into()) => "4C8aB9d3e938285776d6717d7319F6a9B1d809DD".parse::<Address>().unwrap()
91    );
92
93    rpc_test! (
94        ParityAccounts :   parity_remove_address,  &"9b776baeaf3896657a9ba0db5564623b3e0173e0".parse::<Address>().unwrap()
95        => "parity_removeAddress", vec![r#""0x9b776baeaf3896657a9ba0db5564623b3e0173e0""#];
96        Value::Bool(true) => true
97    );
98}