move trust-graph service api from pks to peer ids

This commit is contained in:
Alexey Proshutinskiy 2021-08-12 16:16:58 +03:00
parent 968b9a95e4
commit c67579c0bd
6 changed files with 49 additions and 10 deletions

18
Cargo.lock generated
View File

@ -587,6 +587,16 @@ dependencies = [
"libc",
]
[[package]]
name = "eyre"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "221239d1d5ea86bf5d6f91c9d6bc3646ffe471b08ff9b0f91c44f115ac969d2b"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "failure"
version = "0.1.8"
@ -750,6 +760,7 @@ dependencies = [
"bs58 0.3.1",
"ed25519",
"ed25519-dalek",
"eyre",
"fluence-fork-libp2p-core",
"lazy_static",
"libsecp256k1",
@ -1041,6 +1052,12 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "indexmap"
version = "1.7.0"
@ -2488,6 +2505,7 @@ dependencies = [
"bincode",
"boolinator",
"bs58 0.3.1",
"fluence-fork-libp2p-core",
"fluence-keypair",
"log",
"marine-rs-sdk",

View File

@ -24,6 +24,7 @@ sha2 = "0.9.1"
zeroize = "1"
serde_bytes = "0.11"
libp2p-core = { package = "fluence-fork-libp2p-core", version = "0.27.2" }
eyre = "0.6.5"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false }

View File

@ -114,6 +114,10 @@ impl From<libp2p_core::identity::PublicKey> for PublicKey {
}
}
pub fn peer_id_to_fluence_pk(peer_id: libp2p_core::PeerId) -> eyre::Result<PublicKey> {
Ok(peer_id.as_public_key().ok_or(eyre::eyre!("public key is not inlined in peer id: {}", peer_id))?.into())
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -16,6 +16,8 @@ fluence-keypair = { version = "0.3.0", path = "../keypair" }
marine-rs-sdk = { version = "0.6.11", features = ["logger"] }
marine-sqlite-connector = "0.5.0"
libp2p-core = { package = "fluence-fork-libp2p-core", version = "0.27.2" }
log = "0.4.8"
anyhow = "1.0.31"
boolinator = "2.4.0"

View File

@ -21,8 +21,8 @@ fn insert_cert(certificate: Certificate, current_time: u64) -> InsertResult {
}
#[marine]
fn get_weight(public_key: String) -> WeightResult {
get_weight_impl(public_key).into()
fn get_weight(peer_id: String) -> WeightResult {
get_weight_impl(peer_id).into()
}
#[marine]
@ -32,11 +32,11 @@ fn get_all_certs(issued_for: String) -> AllCertsResult {
#[marine]
/// could add only a host of a trust graph service
fn add_root(pk: String, weight: u32) -> AddRootResult {
fn add_root(peer_id: String, weight: u32) -> AddRootResult {
let call_parameters: CallParameters = marine_rs_sdk::get_call_parameters();
let init_peer_id = call_parameters.init_peer_id.clone();
if call_parameters.host_id == init_peer_id {
add_root_impl(pk, weight).into()
add_root_impl(peer_id, weight).into()
} else {
return AddRootResult {
ret_code: 1,

View File

@ -7,9 +7,15 @@ use std::str::FromStr;
use std::time::Duration;
use thiserror::Error as ThisError;
use trust_graph::{CertificateError, TrustGraphError};
use fluence_keypair::public_key::peer_id_to_fluence_pk;
use libp2p_core::PeerId;
#[derive(ThisError, Debug)]
pub enum ServiceError {
#[error("peer id parse error: {0}")]
PeerIdParseError(String),
#[error("public key extraction from peer id failed: {0}")]
PublicKeyExtractionError(String),
#[error("{0}")]
PublicKeyDecodeError(
#[from]
@ -36,9 +42,17 @@ pub enum ServiceError {
),
}
pub fn get_weight_impl(public_key: String) -> Result<Option<u32>, ServiceError> {
fn parse_peer_id(peer_id: String) -> Result<PeerId, ServiceError> {
libp2p_core::PeerId::from_str(&peer_id).map_err(|e| ServiceError::PeerIdParseError(format!("{:?}", e)))
}
fn extract_public_key(peer_id: String) -> Result<PublicKey, ServiceError> {
peer_id_to_fluence_pk(parse_peer_id(peer_id)?).map_err(|e| ServiceError::PublicKeyExtractionError(e.to_string()))
}
pub fn get_weight_impl(peer_id: String) -> Result<Option<u32>, ServiceError> {
let tg = get_data().lock();
let public_key = string_to_public_key(public_key)?;
let public_key = extract_public_key(peer_id)?;
let weight = tg.weight(public_key)?;
Ok(weight)
}
@ -66,7 +80,7 @@ fn string_to_public_key(public_key: String) -> Result<PublicKey, ServiceError> {
pub fn get_all_certs_impl(issued_for: String) -> Result<Vec<Certificate>, ServiceError> {
let tg = get_data().lock();
let public_key = string_to_public_key(issued_for)?;
let public_key = extract_public_key(issued_for)?;
let certs = tg.get_all_certs(public_key, &[])?;
Ok(certs.into_iter().map(|c| c.into()).collect())
}
@ -78,9 +92,9 @@ pub fn insert_cert_impl(certificate: Certificate, duration: u64) -> Result<(), S
Ok(())
}
pub fn add_root_impl(pk: String, weight: u32) -> Result<(), ServiceError> {
pub fn add_root_impl(peer_id: String, weight: u32) -> Result<(), ServiceError> {
let mut tg = get_data().lock();
let pk = PublicKey::from_base58(&pk)?.into();
tg.add_root_weight(pk, weight)?;
let public_key = extract_public_key(peer_id)?;
tg.add_root_weight(public_key, weight)?;
Ok(())
}