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

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
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;
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 {
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;
// 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;
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ validator_configs:
- authority-key-pair:
value: VTDx4HjVmRBqdqBWg2zN+zcFE20io3CrBchGy/iV1lo=
protocol-key-pair:
value: AH7M/Ot6iUd/Jj47r5aGmQROL24mxT4K8EF1Gvjhk0zT
value: iotaprivkey1qplvel8t02y5wlex8ca6l95xnyzyutmwymznuzhsg963478pjdxdxvys9a8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering, was the test failing because we create a config to compare to this one? Because if we read this snapshot, base64 should still work?

Copy link
Contributor Author

@Alex6323 Alex6323 Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand snapshot based testing, it uses line by line string comparisons (i.e. line-by-line diffs). So the test was failing, because the old snapshot https://github.com/iotaledger/iota/blob/develop/crates/iota-swarm-config/tests/snapshots/snapshot_tests__network_config_snapshot_matches.snap
diffed from the new one because it was generated with the Base64 keypair format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thibault-martinez Can we resolve?

account-key-pair:
value: AH45w+v4xGlEc6QI3WT6iaD1K9yo+kl3XxPyZGlRclZp
value: iotaprivkey1qplrnsltlrzxj3rn5syd6e863xs0227u4rayja6lz0exg623wftxj6nvtyy
network-key-pair:
value: AGXlMKrYkj+CLojtVPwa9rE1QUKZHJA+XGhfgWp7porR
value: iotaprivkey1qpj72v92mzfrlq3w3rk4flq676cn2s2znywfq0judp0cz6nm569dzwvvq2s
db-path: /tmp/foo/
network-address: ""
json-rpc-address: "0.0.0.0:1"
Expand Down Expand Up @@ -126,11 +126,11 @@ validator_configs:
- authority-key-pair:
value: avYcyVgYMXTyaUYh9IRwLK0gSzl7YF6ZQDAbrS1Bhvo=
protocol-key-pair:
value: ABZRTK3YmdGRxzGX33BUjWwIxKGcl9/6vrJAKvjNY3gN
value: iotaprivkey1qqt9zn9dmzvaryw8xxta7uz534kq339pnjtal747kfqz47xdvduq6jtnwz4
account-key-pair:
value: AClK2/gtiS+cypPl2il6YC49PJgkHoAgnpKZ0dEiwIUw
value: iotaprivkey1qq554klc9kyjl8x2j0ja52t6vqhr60ycys0gqgy7j2var5fzczznq9mn90k
network-key-pair:
value: AKTwSrpgq5BEhJgIhv+1H0jMiJQMigkGsrAW9EQPaMSI
value: iotaprivkey1qzj0qj46vz4eq3yynqygdla4rayvezy5pj9qjp4jkqt0g3q0drzgs580hm5
db-path: /tmp/foo/
network-address: ""
json-rpc-address: "0.0.0.0:1"
Expand Down Expand Up @@ -246,11 +246,11 @@ validator_configs:
- authority-key-pair:
value: OXnx3yM1C/ppgnDMx/o1d49fJs7E05kq11mXNae/O+I=
protocol-key-pair:
value: ANuTFymJGw6l1rSGV5du4G4ROOLd4adUItxxVhy31lnN
value: iotaprivkey1qrdex9ef3ydsafwkkjr909mwuphpzw8zmhs6w4pzm3c4v89h6evu6kw8vxg
account-key-pair:
value: AHs3lfh8LlhanWE/zTjOqDFkNiDL2ouxjjJ+WDT5/hVg
value: iotaprivkey1qpan090c0sh9sk5avylu6wxw4qckgd3qe0dghvvwxfl9sd8elc2kq83nhzj
network-key-pair:
value: AJKPlasm4k2SQnJsg8qDJZ3xZFL7Q2TaHy5394eVjf6M
value: iotaprivkey1qzfgl9dtym3ymyjzwfkg8j5rykwlzezjldpkfksl9eml0pu43hlgc4kp8cf
db-path: /tmp/foo/
network-address: ""
json-rpc-address: "0.0.0.0:1"
Expand Down Expand Up @@ -366,11 +366,11 @@ validator_configs:
- authority-key-pair:
value: CyNkjqNVr3HrHTH7f/NLs7u5lUHJzuPAw0PqMTD2y2s=
protocol-key-pair:
value: AGGCaIy1OwvvLtr2rcHewlJJBILLzhrEao4QmF0D2UTz
value: iotaprivkey1qpscy6yvk5ashmewmtm2msw7cffyjpyze08p43r23cgfshgrm9z0xgyvu9e
account-key-pair:
value: ANQ4duxTsOdofUAHfSIo8kauNj7SRmLZgmdkb12DwxF6
value: iotaprivkey1qr2rsahv2wcww6ragqrh6g3g7fr2ud376frx9kvzvajx7hvrcvgh5409rah
network-key-pair:
value: AKFIgWPoTZb9/zUu/1P8wFtxDDYuObk1xr/QaxZ2H48i
value: iotaprivkey1qzs53qtrapxedl0lx5h075lucpdhzrpk9cumjdwxhlgxk9nkr78jy3z53gh
db-path: /tmp/foo/
network-address: ""
json-rpc-address: "0.0.0.0:1"
Expand Down Expand Up @@ -486,11 +486,11 @@ validator_configs:
- authority-key-pair:
value: X/I/kM+KvHcxAKEf2UU6Sr7SpN3bhiE9nP5CuM/iIY0=
protocol-key-pair:
value: ABB1+x4nRFoyYG3rKucsVmnAZB1Mf8ACwfdDJvEomhGW
value: iotaprivkey1qqg8t7c7yaz95vnqdh4j4eev2e5uqeqaf3luqqkp7apjdufgnggevfkz7yg
account-key-pair:
value: AAQtIbw/tafBD2X+p6ef4RsZM1kKG4vvWMebYPcunc+5
value: iotaprivkey1qqzz6gdu87660sg0vhl20fuluyd3jv6epgdchm6cc7dkpaewnh8mju4tkfv
network-key-pair:
value: AJFy2uQrEISFM5YRQRBUz9XisSepQRkWMzZDo6w2ppVP
value: iotaprivkey1qzgh9khy9vggfpfnjcg5zyz5el279vf849q3j93nxep68tpk56257n95j5f
db-path: /tmp/foo/
network-address: ""
json-rpc-address: "0.0.0.0:1"
Expand Down Expand Up @@ -606,11 +606,11 @@ validator_configs:
- authority-key-pair:
value: N272EiFDyKtxRbDKbyN6ujenJ+skPcRoc/XolpOLGnU=
protocol-key-pair:
value: AHNbJ+TbMK2C5vo2buMguifUhvMpyG2dyqGXF69UJSTC
value: iotaprivkey1qpe4kflymvc2mqhxlgmxaceqhgnafphn98yxm8w25xt30t65y5jvykfzwpl
account-key-pair:
value: AI0OYrH3grOXDvJEH0aDYhOhMdudJ4hLVwh+1OGNyoXe
value: iotaprivkey1qzxsuc4377pt89cw7fzp735rvgf6zvwmn5ncsj6hppldfcvde2zau923v9l
network-key-pair:
value: AFJKmNZNkqyS85XUqAKsw2adb7nVejMwGaqI2ptF2NmT
value: iotaprivkey1qpfy4xxkfkf2eyhnjh22sq4vcdnf6mae64arxvqe42yd4x69mrvexzudlfv
db-path: /tmp/foo/
network-address: ""
json-rpc-address: "0.0.0.0:1"
Expand Down Expand Up @@ -726,11 +726,11 @@ validator_configs:
- authority-key-pair:
value: a74f03IOjL8ZFSWFChFVEi+wiMwHNwNCPDGIYkGfgjs=
protocol-key-pair:
value: AFKq0My7gQHTDG58VQMYImhU18+j2le9HtF08nhehbvK
value: iotaprivkey1qpf245xvhwqsr5cvde792qccyf59f47050d900g76960y7z7skau5ta2yhj
account-key-pair:
value: ACdIQ0IILpTytmU6RjVq3Ifr4O9zkPZVn0V7vbjfkKPP
value: iotaprivkey1qqn5ss6zpqhffu4kv5ayvdt2mjr7hc80wwg0v4vlg4ammwxljz3u73462qv
network-key-pair:
value: AG9uDSdI54AnHant8fiWuNdavPdx76lZfVjbuiHRO8aE
value: iotaprivkey1qphkurf8frncqfca48klr7ykhrt4408hw8h6jktatrdm5gw380rggynrrlj
db-path: /tmp/foo/
network-address: ""
json-rpc-address: "0.0.0.0:1"
Expand Down
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