Skip to content

Commit e1d8cb8

Browse files
committed
Remove KeypairInsecureClone trait and add insecure_clone() instead
See discussion in solana-labs#26248
1 parent 4c94493 commit e1d8cb8

File tree

5 files changed

+27
-34
lines changed

5 files changed

+27
-34
lines changed

core/src/replay_stage.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3528,7 +3528,7 @@ pub(crate) mod tests {
35283528
hash::{hash, Hash},
35293529
instruction::InstructionError,
35303530
poh_config::PohConfig,
3531-
signature::{Keypair, KeypairInsecureClone, Signer},
3531+
signature::{Keypair, Signer},
35323532
system_transaction,
35333533
transaction::TransactionError,
35343534
},
@@ -3608,7 +3608,7 @@ pub(crate) mod tests {
36083608
let my_pubkey = my_keypairs.node_keypair.pubkey();
36093609
let cluster_info = ClusterInfo::new(
36103610
Node::new_localhost_with_pubkey(&my_pubkey).info,
3611-
Arc::new(my_keypairs.node_keypair.clone()),
3611+
Arc::new(my_keypairs.node_keypair.insecure_clone()),
36123612
SocketAddrSpace::Unspecified,
36133613
);
36143614
assert_eq!(my_pubkey, cluster_info.id());

local-cluster/src/local_cluster.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use {
3131
message::Message,
3232
poh_config::PohConfig,
3333
pubkey::Pubkey,
34-
signature::{Keypair, KeypairInsecureClone, Signer},
34+
signature::{Keypair, Signer},
3535
stake::{
3636
config as stake_config, instruction as stake_instruction,
3737
state::{Authorized, Lockup},
@@ -53,7 +53,6 @@ use {
5353
collections::HashMap,
5454
io::{Error, ErrorKind, Result},
5555
iter,
56-
ops::Deref,
5756
path::{Path, PathBuf},
5857
sync::{Arc, RwLock},
5958
},
@@ -202,8 +201,8 @@ impl LocalCluster {
202201
if *in_genesis {
203202
Some((
204203
ValidatorVoteKeypairs {
205-
node_keypair: node_keypair.deref().clone(),
206-
vote_keypair: vote_keypair.deref().clone(),
204+
node_keypair: node_keypair.insecure_clone(),
205+
vote_keypair: vote_keypair.insecure_clone(),
207206
stake_keypair: Keypair::new(),
208207
},
209208
stake,
@@ -262,8 +261,8 @@ impl LocalCluster {
262261
let mut leader_config = safe_clone_config(&config.validator_configs[0]);
263262
leader_config.rpc_addrs = Some((leader_node.info.rpc, leader_node.info.rpc_pubsub));
264263
Self::sync_ledger_path_across_nested_config_fields(&mut leader_config, &leader_ledger_path);
265-
let leader_keypair = Arc::new(leader_keypair.clone());
266-
let leader_vote_keypair = Arc::new(leader_vote_keypair.clone());
264+
let leader_keypair = Arc::new(leader_keypair.insecure_clone());
265+
let leader_vote_keypair = Arc::new(leader_vote_keypair.insecure_clone());
267266

268267
let leader_server = Validator::new(
269268
leader_node,
@@ -312,7 +311,7 @@ impl LocalCluster {
312311
.map(|keypairs| {
313312
(
314313
keypairs.node_keypair.pubkey(),
315-
Arc::new(keypairs.vote_keypair.clone()),
314+
Arc::new(keypairs.vote_keypair.insecure_clone()),
316315
)
317316
})
318317
.collect();

local-cluster/tests/local_cluster_slow_1.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ use {
2929
clock::{Slot, MAX_PROCESSING_AGE},
3030
hash::Hash,
3131
pubkey::Pubkey,
32-
signature::{KeypairInsecureClone, Signer},
32+
signature::Signer,
3333
},
3434
solana_streamer::socket::SocketAddrSpace,
3535
solana_vote_program::{vote_state::MAX_LOCKOUT_HISTORY, vote_transaction},
3636
std::{
3737
collections::{BTreeSet, HashSet},
38-
ops::Deref,
3938
path::Path,
4039
sync::{
4140
atomic::{AtomicBool, Ordering},
@@ -464,7 +463,7 @@ fn test_duplicate_shreds_broadcast_leader() {
464463
let (gossip_service, _tcp_listener, cluster_info) = gossip_service::make_gossip_node(
465464
// Need to use our validator's keypair to gossip EpochSlots and votes for our
466465
// node later.
467-
node_keypair.deref().clone(),
466+
node_keypair.insecure_clone(),
468467
Some(&cluster.entry_point_info.gossip),
469468
&exit,
470469
None,

runtime/src/genesis_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use {
77
genesis_config::{ClusterType, GenesisConfig},
88
pubkey::Pubkey,
99
rent::Rent,
10-
signature::{Keypair, KeypairInsecureClone, Signer},
10+
signature::{Keypair, Signer},
1111
stake::state::StakeState,
1212
system_program,
1313
},
@@ -111,7 +111,7 @@ pub fn create_genesis_config_with_vote_accounts_and_cluster_type(
111111
assert_eq!(voting_keypairs.len(), stakes.len());
112112

113113
let mint_keypair = Keypair::new();
114-
let voting_keypair = voting_keypairs[0].borrow().vote_keypair.clone();
114+
let voting_keypair = voting_keypairs[0].borrow().vote_keypair.insecure_clone();
115115

116116
let validator_pubkey = voting_keypairs[0].borrow().node_keypair.pubkey();
117117
let genesis_config = create_genesis_config_with_leader_ex(

sdk/src/signer/keypair.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ impl Keypair {
6464
pub fn secret(&self) -> &ed25519_dalek::SecretKey {
6565
&self.0.secret
6666
}
67+
68+
/// Allows Keypair cloning
69+
///
70+
/// Note that the `Clone` trait is intentionally unimplemented because making a
71+
/// second copy of sensitive secret keys in memory is usually a bad idea.
72+
///
73+
/// Only use this in tests or when strictly required. Consider using Arc<Keypair>
74+
/// instead.
75+
pub fn insecure_clone(&self) -> Self {
76+
Self(ed25519_dalek::Keypair {
77+
// This will never error since self is a valid keypair
78+
secret: ed25519_dalek::SecretKey::from_bytes(self.0.secret.as_bytes()).unwrap(),
79+
public: self.0.public,
80+
})
81+
}
6782
}
6883

6984
impl Signer for Keypair {
@@ -97,26 +112,6 @@ where
97112
}
98113
}
99114

100-
/// Allows Keypair cloning
101-
///
102-
/// Note that the `Clone` trait is intentionally unimplemented because making a
103-
/// second copy of sensitive secret keys in memory is usually a bad idea.
104-
///
105-
/// Only use this in tests or when strictly required.
106-
pub trait KeypairInsecureClone {
107-
fn clone(&self) -> Self;
108-
}
109-
110-
impl KeypairInsecureClone for Keypair {
111-
fn clone(&self) -> Self {
112-
Self(ed25519_dalek::Keypair {
113-
// This will never error since self is a valid keypair
114-
secret: ed25519_dalek::SecretKey::from_bytes(self.0.secret.as_bytes()).unwrap(),
115-
public: self.0.public,
116-
})
117-
}
118-
}
119-
120115
/// Reads a JSON-encoded `Keypair` from a `Reader` implementor
121116
pub fn read_keypair<R: Read>(reader: &mut R) -> Result<Keypair, Box<dyn error::Error>> {
122117
let bytes: Vec<u8> = serde_json::from_reader(reader)?;

0 commit comments

Comments
 (0)