Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
feat/node: add data signing and signature verification public apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoga07 committed Oct 13, 2020
1 parent 425be3b commit c803d4a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
TransportConfig,
};
use bytes::Bytes;
use ed25519_dalek::Signature;
use itertools::Itertools;
use std::{net::SocketAddr, sync::Arc};
use tokio::sync::mpsc;
Expand Down Expand Up @@ -130,6 +131,16 @@ impl Node {
self.stage.public_key().await
}

/// Sign any data with the key of this node.
pub async fn sign(&self, data: &[u8]) -> Signature {
self.stage.sign(data).await
}

/// Verify any signed data with the key of this node.
pub async fn verify(&self, data: &[u8], signature: &Signature) -> bool {
self.stage.verify(data, signature).await
}

/// The name of this node.
pub async fn name(&self) -> XorName {
self.stage.name().await
Expand Down
20 changes: 19 additions & 1 deletion src/node/stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
};
use bls_signature_aggregator::Proof;
use bytes::Bytes;
use ed25519_dalek::PublicKey;
use ed25519_dalek::{PublicKey, Signature, Signer};
use std::{
fmt::{self, Debug, Formatter},
net::SocketAddr,
Expand Down Expand Up @@ -167,6 +167,24 @@ impl Stage {
node_info.keypair.public
}

pub async fn sign(&self, msg: &[u8]) -> Signature {
let state = self.state.lock().await;
match &*state {
State::Bootstrapping(state) => state.node_info.keypair.sign(msg),
State::Joining(state) => state.node_info.keypair.sign(msg),
State::Approved(state) => state.node_info.keypair.sign(msg),
}
}

pub async fn verify(&self, msg: &[u8], signature: &Signature) -> bool {
let state = self.state.lock().await;
match &*state {
State::Bootstrapping(state) => state.node_info.keypair.verify(msg, signature).is_ok(),
State::Joining(state) => state.node_info.keypair.verify(msg, signature).is_ok(),
State::Approved(state) => state.node_info.keypair.verify(msg, signature).is_ok(),
}
}

/// Our `Prefix` once we are a part of the section.
pub async fn our_prefix(&self) -> Option<Prefix> {
self.state
Expand Down

0 comments on commit c803d4a

Please sign in to comment.