mirror of
https://github.com/fluencelabs/trust-graph-test
synced 2025-04-25 06:32:33 +00:00
move trust-graph service api from pks to peer ids
This commit is contained in:
parent
968b9a95e4
commit
c67579c0bd
18
Cargo.lock
generated
18
Cargo.lock
generated
@ -587,6 +587,16 @@ dependencies = [
|
|||||||
"libc",
|
"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]]
|
[[package]]
|
||||||
name = "failure"
|
name = "failure"
|
||||||
version = "0.1.8"
|
version = "0.1.8"
|
||||||
@ -750,6 +760,7 @@ dependencies = [
|
|||||||
"bs58 0.3.1",
|
"bs58 0.3.1",
|
||||||
"ed25519",
|
"ed25519",
|
||||||
"ed25519-dalek",
|
"ed25519-dalek",
|
||||||
|
"eyre",
|
||||||
"fluence-fork-libp2p-core",
|
"fluence-fork-libp2p-core",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"libsecp256k1",
|
"libsecp256k1",
|
||||||
@ -1041,6 +1052,12 @@ dependencies = [
|
|||||||
"unicode-normalization",
|
"unicode-normalization",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "indenter"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
version = "1.7.0"
|
version = "1.7.0"
|
||||||
@ -2488,6 +2505,7 @@ dependencies = [
|
|||||||
"bincode",
|
"bincode",
|
||||||
"boolinator",
|
"boolinator",
|
||||||
"bs58 0.3.1",
|
"bs58 0.3.1",
|
||||||
|
"fluence-fork-libp2p-core",
|
||||||
"fluence-keypair",
|
"fluence-keypair",
|
||||||
"log",
|
"log",
|
||||||
"marine-rs-sdk",
|
"marine-rs-sdk",
|
||||||
|
@ -24,6 +24,7 @@ sha2 = "0.9.1"
|
|||||||
zeroize = "1"
|
zeroize = "1"
|
||||||
serde_bytes = "0.11"
|
serde_bytes = "0.11"
|
||||||
libp2p-core = { package = "fluence-fork-libp2p-core", version = "0.27.2" }
|
libp2p-core = { package = "fluence-fork-libp2p-core", version = "0.27.2" }
|
||||||
|
eyre = "0.6.5"
|
||||||
|
|
||||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false }
|
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false }
|
||||||
|
@ -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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -16,6 +16,8 @@ fluence-keypair = { version = "0.3.0", path = "../keypair" }
|
|||||||
marine-rs-sdk = { version = "0.6.11", features = ["logger"] }
|
marine-rs-sdk = { version = "0.6.11", features = ["logger"] }
|
||||||
marine-sqlite-connector = "0.5.0"
|
marine-sqlite-connector = "0.5.0"
|
||||||
|
|
||||||
|
libp2p-core = { package = "fluence-fork-libp2p-core", version = "0.27.2" }
|
||||||
|
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
anyhow = "1.0.31"
|
anyhow = "1.0.31"
|
||||||
boolinator = "2.4.0"
|
boolinator = "2.4.0"
|
||||||
|
@ -21,8 +21,8 @@ fn insert_cert(certificate: Certificate, current_time: u64) -> InsertResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[marine]
|
#[marine]
|
||||||
fn get_weight(public_key: String) -> WeightResult {
|
fn get_weight(peer_id: String) -> WeightResult {
|
||||||
get_weight_impl(public_key).into()
|
get_weight_impl(peer_id).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[marine]
|
#[marine]
|
||||||
@ -32,11 +32,11 @@ fn get_all_certs(issued_for: String) -> AllCertsResult {
|
|||||||
|
|
||||||
#[marine]
|
#[marine]
|
||||||
/// could add only a host of a trust graph service
|
/// 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 call_parameters: CallParameters = marine_rs_sdk::get_call_parameters();
|
||||||
let init_peer_id = call_parameters.init_peer_id.clone();
|
let init_peer_id = call_parameters.init_peer_id.clone();
|
||||||
if call_parameters.host_id == init_peer_id {
|
if call_parameters.host_id == init_peer_id {
|
||||||
add_root_impl(pk, weight).into()
|
add_root_impl(peer_id, weight).into()
|
||||||
} else {
|
} else {
|
||||||
return AddRootResult {
|
return AddRootResult {
|
||||||
ret_code: 1,
|
ret_code: 1,
|
||||||
|
@ -7,9 +7,15 @@ use std::str::FromStr;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use thiserror::Error as ThisError;
|
use thiserror::Error as ThisError;
|
||||||
use trust_graph::{CertificateError, TrustGraphError};
|
use trust_graph::{CertificateError, TrustGraphError};
|
||||||
|
use fluence_keypair::public_key::peer_id_to_fluence_pk;
|
||||||
|
use libp2p_core::PeerId;
|
||||||
|
|
||||||
#[derive(ThisError, Debug)]
|
#[derive(ThisError, Debug)]
|
||||||
pub enum ServiceError {
|
pub enum ServiceError {
|
||||||
|
#[error("peer id parse error: {0}")]
|
||||||
|
PeerIdParseError(String),
|
||||||
|
#[error("public key extraction from peer id failed: {0}")]
|
||||||
|
PublicKeyExtractionError(String),
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
PublicKeyDecodeError(
|
PublicKeyDecodeError(
|
||||||
#[from]
|
#[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 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)?;
|
let weight = tg.weight(public_key)?;
|
||||||
Ok(weight)
|
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> {
|
pub fn get_all_certs_impl(issued_for: String) -> Result<Vec<Certificate>, ServiceError> {
|
||||||
let tg = get_data().lock();
|
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, &[])?;
|
let certs = tg.get_all_certs(public_key, &[])?;
|
||||||
Ok(certs.into_iter().map(|c| c.into()).collect())
|
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(())
|
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 mut tg = get_data().lock();
|
||||||
let pk = PublicKey::from_base58(&pk)?.into();
|
let public_key = extract_public_key(peer_id)?;
|
||||||
tg.add_root_weight(pk, weight)?;
|
tg.add_root_weight(public_key, weight)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user