Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nodeconfig)!: Default to Bech32 format for IOTA key pairs #4826

Merged
merged 25 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
472193b
line
Alex6323 Jan 10, 2025
2d81cf3
Merge branch 'develop' into dev-tools/bech32-private-keys
Alex6323 Jan 10, 2025
de76660
Merge branch 'develop' into dev-tools/bech32-private-keys
Alex6323 Jan 16, 2025
70b66b2
switch to Bech32 serialized IOTA key pairs
Alex6323 Jan 16, 2025
15f7b73
Merge branch 'develop' into dev-tools/bech32-private-keys
Alex6323 Jan 20, 2025
37d1837
Merge branch 'develop' into dev-tools/bech32-private-keys
Alex6323 Jan 21, 2025
6dab4e5
improve comment
Alex6323 Jan 21, 2025
10e43e9
Merge branch 'develop' into dev-tools/bech32-private-keys
Alex6323 Jan 21, 2025
1f2903a
clippy
Alex6323 Jan 21, 2025
747d6dd
Merge branch 'develop' into dev-tools/bech32-private-keys
Alex6323 Jan 23, 2025
8a51781
use config specific serialization for IotaKeyPair
Alex6323 Jan 23, 2025
aab6a51
expect
Alex6323 Jan 23, 2025
324ba1e
fix rebase
Alex6323 Jan 24, 2025
534227c
make generic
Alex6323 Jan 24, 2025
abd27ff
Merge branch 'develop' into dev-tools/bech32-private-keys
Alex6323 Jan 24, 2025
92fc5cf
suggestions
DaughterOfMars Jan 24, 2025
f6e3a42
one more
DaughterOfMars Jan 24, 2025
2ea14ca
improve comment
Alex6323 Jan 27, 2025
901d9ed
update network config snap
Alex6323 Jan 28, 2025
cd5d589
Merge branch 'develop' into dev-tools/bech32-private-keys
Alex6323 Jan 31, 2025
0b79c6b
Merge branch 'develop' into dev-tools/bech32-private-keys
Alex6323 Feb 3, 2025
e4f0a0c
Merge branch 'develop' into dev-tools/bech32-private-keys
thibault-martinez Feb 4, 2025
7558191
Merge branch 'develop' into dev-tools/bech32-private-keys
thibault-martinez Feb 4, 2025
855e727
nits
thibault-martinez Feb 4, 2025
155a46c
fmt
thibault-martinez Feb 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions crates/iota-config/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use iota_types::{
use once_cell::sync::OnceCell;
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
use tracing::info;

use crate::{
Expand All @@ -47,7 +46,6 @@ pub const DEFAULT_VALIDATOR_GAS_PRICE: u64 = iota_types::transaction::DEFAULT_VA
/// Default commission rate of 2%
pub const DEFAULT_COMMISSION_RATE: u64 = 200;

#[serde_as]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct NodeConfig {
Expand Down Expand Up @@ -1010,15 +1008,13 @@ pub struct KeyPairWithPath {
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
#[serde_as]
#[serde(untagged)]
enum KeyPairLocation {
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
InPlace {
#[serde_as(as = "Arc<KeyPairBase64>")]
#[serde(with = "bech32_formatted_keypair")]
value: Arc<IotaKeyPair>,
},
File {
#[serde(rename = "path")]
path: PathBuf,
muXxer marked this conversation as resolved.
Show resolved Hide resolved
},
}
Expand Down Expand Up @@ -1080,7 +1076,6 @@ pub struct AuthorityKeyPairWithPath {
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
#[serde_as]
#[serde(untagged)]
enum AuthorityKeyPairLocation {
InPlace { value: Arc<AuthorityKeyPair> },
Expand Down Expand Up @@ -1232,3 +1227,41 @@ impl RunWithRange {
matches!(self, RunWithRange::Checkpoint(seq) if *seq == seq_num)
}
}

/// A serde helper module used with #[serde(with = "...")] to change
/// the de/serialization format of an `IotaKeyPair` to Bech32 when written to or
/// read from a node config.
mod bech32_formatted_keypair {
use std::ops::Deref;

use iota_types::crypto::{EncodeDecodeBase64, IotaKeyPair};
use serde::{Deserialize, Deserializer, Serializer};

pub fn serialize<S, T>(kp: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Deref<Target = IotaKeyPair>,
{
use serde::ser::Error;
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
// Serialize the keypair to a Bech32 string
let s = kp.encode().map_err(Error::custom)?;
serializer.serialize_str(&s)
}

pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: From<IotaKeyPair>,
{
use serde::de::Error;
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
let s = String::deserialize(deserializer)?;
// Try to deserialize the keypair from a Bech32 formatted string
IotaKeyPair::decode(&s)
.or_else(|_| {
// For backwards compatibility try Base64 if Bech32 failed
IotaKeyPair::decode_base64(&s)
})
.map(Into::into)
.map_err(Error::custom)
}
}
1 change: 1 addition & 0 deletions crates/iota-types/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ impl EncodeDecodeBase64 for IotaKeyPair {
Self::from_bytes(&bytes).map_err(|_| FastCryptoError::InvalidInput)
}
}

impl IotaKeyPair {
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes: Vec<u8> = Vec::new();
Expand Down
Loading