From d3b02a3af01a72547f5c2b3ca3880e3266d82f31 Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Thu, 1 Apr 2021 17:06:08 +0200
Subject: [PATCH] [feat] bca: add req Identities

---
 .../modules/gva/bca/src/exec_req_type.rs      | 21 ++++++++++++++++++
 rust-libs/modules/gva/bca/src/lib.rs          |  4 ++--
 .../modules/gva/bca/types/src/identity.rs     | 22 +++++++++++++++++++
 rust-libs/modules/gva/bca/types/src/lib.rs    |  4 ++++
 4 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 rust-libs/modules/gva/bca/types/src/identity.rs

diff --git a/rust-libs/modules/gva/bca/src/exec_req_type.rs b/rust-libs/modules/gva/bca/src/exec_req_type.rs
index b5b75d083..6a4b226c7 100644
--- a/rust-libs/modules/gva/bca/src/exec_req_type.rs
+++ b/rust-libs/modules/gva/bca/src/exec_req_type.rs
@@ -69,5 +69,26 @@ pub(super) async fn execute_req_type(
         }),
         BcaReqTypeV0::Ping => Ok(BcaRespTypeV0::Pong),
         BcaReqTypeV0::SendTxs(txs) => send_txs::send_txs(bca_executor, txs).await,
+        BcaReqTypeV0::Identities(pubkeys) => {
+            let dbs_reader = bca_executor.dbs_reader();
+            Ok(BcaRespTypeV0::Identities(
+                bca_executor
+                    .dbs_pool
+                    .execute(move |dbs| {
+                        pubkeys
+                            .into_iter()
+                            .map(|pubkey| {
+                                dbs_reader.idty(&dbs.bc_db_ro, pubkey).map(|idty_opt| {
+                                    idty_opt.map(|idty| Identity {
+                                        is_member: idty.is_member,
+                                        username: idty.username,
+                                    })
+                                })
+                            })
+                            .collect::<KvResult<ArrayVec<_>>>()
+                    })
+                    .await??,
+            ))
+        }
     }
 }
diff --git a/rust-libs/modules/gva/bca/src/lib.rs b/rust-libs/modules/gva/bca/src/lib.rs
index b1a1dc1a5..0ac1a1c9f 100644
--- a/rust-libs/modules/gva/bca/src/lib.rs
+++ b/rust-libs/modules/gva/bca/src/lib.rs
@@ -37,8 +37,8 @@ use async_io_stream::IoStream;
 use bincode::Options as _;
 use dubp::crypto::keys::{ed25519::Ed25519KeyPair, Signator};
 use duniter_bca_types::{
-    amount::Amount, bincode_opts, BcaReq, BcaReqExecError, BcaReqTypeV0, BcaResp, BcaRespTypeV0,
-    BcaRespV0,
+    amount::Amount, bincode_opts, identity::Identity, BcaReq, BcaReqExecError, BcaReqTypeV0,
+    BcaResp, BcaRespTypeV0, BcaRespV0,
 };
 pub use duniter_dbs::kv_typed::prelude::*;
 use duniter_dbs::{FileBackend, SharedDbs};
diff --git a/rust-libs/modules/gva/bca/types/src/identity.rs b/rust-libs/modules/gva/bca/types/src/identity.rs
new file mode 100644
index 000000000..e2302a9bf
--- /dev/null
+++ b/rust-libs/modules/gva/bca/types/src/identity.rs
@@ -0,0 +1,22 @@
+//  Copyright (C) 2020 Éloïs SANCHEZ.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+use crate::*;
+
+#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
+pub struct Identity {
+    pub is_member: bool,
+    pub username: String,
+}
diff --git a/rust-libs/modules/gva/bca/types/src/lib.rs b/rust-libs/modules/gva/bca/types/src/lib.rs
index 8c9f84501..4182fb555 100644
--- a/rust-libs/modules/gva/bca/types/src/lib.rs
+++ b/rust-libs/modules/gva/bca/types/src/lib.rs
@@ -23,11 +23,13 @@
 )]
 
 pub mod amount;
+pub mod identity;
 pub mod prepare_payment;
 pub mod rejected_tx;
 pub mod utxo;
 
 use crate::amount::Amount;
+use crate::identity::Identity;
 use crate::prepare_payment::{PrepareSimplePayment, PrepareSimplePaymentResp};
 use crate::utxo::Utxo;
 
@@ -74,6 +76,7 @@ pub enum BcaReqTypeV0 {
     },
     Ping,
     SendTxs(Txs),
+    Identities(ArrayVec<[PublicKey; 16]>),
 }
 
 // Request types helpers
@@ -110,6 +113,7 @@ pub enum BcaRespTypeV0 {
     PrepareSimplePayment(PrepareSimplePaymentResp),
     Pong,
     RejectedTxs(Vec<rejected_tx::RejectedTx>),
+    Identities(ArrayVec<[Option<Identity>; 16]>),
 }
 
 // Result and error
-- 
GitLab