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
pub use polymesh_api::{
  client::{AccountId, IdentityId, Signer},
  polymesh::types::{
    polymesh_primitives::{
      secondary_key::{KeyRecord, Permissions, SecondaryKey},
      ticker::Ticker,
    },
    runtime::{events::*, RuntimeEvent},
  },
  Api,
};
pub use polymesh_api_client_extras as extras;

mod error;
use error::*;

mod db;
pub use db::*;

mod account;
pub use account::*;

mod tester;
pub use tester::*;

pub async fn client_api() -> Result<Api> {
  let url = std::env::var("POLYMESH_URL").unwrap_or_else(|_| "ws://localhost:9944".into());
  Ok(Api::new(&url).await?)
}

#[derive(Clone)]
pub struct User {
  /// Primary key signer.
  pub primary_key: AccountSigner,
  /// User's secondary keys.
  pub secondary_keys: Vec<AccountSigner>,
  /// User's identity if they have been onboarded.
  pub did: Option<IdentityId>,
}

#[async_trait::async_trait]
impl Signer for User {
  fn account(&self) -> AccountId {
    self.primary_key.account()
  }

  async fn nonce(&self) -> Option<u32> {
    self.primary_key.nonce().await
  }

  async fn set_nonce(&mut self, nonce: u32) {
    self.primary_key.set_nonce(nonce).await
  }

  async fn sign(&self, msg: &[u8]) -> polymesh_api::client::Result<sp_runtime::MultiSignature> {
    Ok(self.primary_key.sign(msg).await?)
  }
}

impl User {
  pub fn new(primary_key: AccountSigner) -> Self {
    Self {
      primary_key,
      secondary_keys: Vec::new(),
      did: None,
    }
  }

  pub fn account(&self) -> AccountId {
    self.primary_key.account()
  }
}