Skip to content

Commit

Permalink
fix(node): derive reward_key from main keypair
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi authored and joshuef committed Feb 6, 2024
1 parent 601100f commit 66e0be7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
4 changes: 4 additions & 0 deletions sn_node/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,8 @@ pub enum Error {
paid: NanoTokens,
expected: NanoTokens,
},

// ---------- Initialize Errors
#[error("Failed to generate a reward key")]
FailedToGenerateRewardKey,
}
16 changes: 13 additions & 3 deletions sn_node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::{error::Result, event::NodeEventsChannel, Marker, NodeEvent};
use super::{
error::{Error, Result},
event::NodeEventsChannel,
Marker, NodeEvent,
};
#[cfg(feature = "open-metrics")]
use crate::metrics::NodeMetrics;
use crate::RunningNode;
Expand Down Expand Up @@ -100,8 +104,14 @@ impl NodeBuilder {
///
/// Returns an error if there is a problem initializing the `SwarmDriver`.
pub fn build_and_run(self) -> Result<RunningNode> {
// TODO: Make this key settable, and accessible via API
let reward_key = MainSecretKey::random();
// Using the signature as the seed of generating the reward_key
let sig_vec = match self.keypair.sign(b"generate reward seed") {
Ok(sig) => sig,
Err(_err) => return Err(Error::FailedToGenerateRewardKey),
};
let mut rng = sn_transfers::rng::from_vec(&sig_vec);

let reward_key = MainSecretKey::random_from_rng(&mut rng);
let reward_address = reward_key.main_pubkey();

let mut wallet = HotWallet::load_from_main_key(&self.root_dir, reward_key)?;
Expand Down
33 changes: 33 additions & 0 deletions sn_transfers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub mod rng {
rngs::{StdRng, ThreadRng},
SeedableRng,
};
use tiny_keccak::{Hasher, Sha3};

pub fn thread_rng() -> ThreadRng {
crate::rand::thread_rng()
Expand All @@ -56,4 +57,36 @@ pub mod rng {
pub fn from_seed(seed: <StdRng as SeedableRng>::Seed) -> StdRng {
StdRng::from_seed(seed)
}

// Using hash to covert `Vec<u8>` into `[u8; 32]',
// and using it as seed to generate a determined Rng.
pub fn from_vec(vec: &[u8]) -> StdRng {
let mut sha3 = Sha3::v256();
sha3.update(vec);
let mut hash = [0u8; 32];
sha3.finalize(&mut hash);

from_seed(hash)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::rng::from_vec;

#[test]
fn confirm_generating_same_key() {
let rng_seed = b"testing generating same key";
let content = b"some context to try with";

let mut rng_1 = from_vec(rng_seed);
let reward_key_1 = MainSecretKey::random_from_rng(&mut rng_1);
let sig = reward_key_1.sign(content);

let mut rng_2 = from_vec(rng_seed);
let reward_key_2 = MainSecretKey::random_from_rng(&mut rng_2);

assert!(reward_key_2.main_pubkey().verify(&sig, content));
}
}

0 comments on commit 66e0be7

Please sign in to comment.