Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
78 changes: 68 additions & 10 deletions rs/registry/admin/bin/create_subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub(crate) struct ProposeToCreateSubnetCmd {
/// each with a subnet ID to request this key from.
///
/// key_id: Master public key ID formatted as "Scheme:AlgorithmID:KeyName".
/// pre_signatures_to_create_in_advance: Non-negative integer value.
/// pre_signatures_to_create_in_advance: Non-negative integer value. Omitted for keys without pre-signatures (e.g., vetKD).
/// max_queue_size: Integer value greater than or equal 1.
/// subnet_id: Principal ID of a subnet holding the requested key.
///
Expand All @@ -109,7 +109,12 @@ pub(crate) struct ProposeToCreateSubnetCmd {
/// "pre_signatures_to_create_in_advance": "98",
/// "max_queue_size": "154",
/// "subnet_id": "gxevo-lhkam-aaaaa-aaaap-yai"
/// }
/// },
/// {
/// "key_id": "vetkd:Bls12_381_G2:some_key_name_3",
/// "max_queue_size": "154",
/// "subnet_id": "gxevo-lhkam-aaaaa-aaaap-yai"
/// },
/// ]'
/// ```
#[clap(long)]
Expand Down Expand Up @@ -171,26 +176,31 @@ fn parse_key_config_requests_option(
})
.expect("Each element of the JSON object must specify a 'subnet_id'."));

let key_id = Some(btree
let key_id = btree
.get("key_id")
.map(|key| {
key.parse::<MasterPublicKeyId>()
.unwrap_or_else(|_| panic!("Could not parse key_id: '{key}'"))
})
.expect("Each element of the JSON object must specify a 'key_id'."));
.expect("Each element of the JSON object must specify a 'key_id'.");

let pre_signatures_to_create_in_advance = Some(btree
let pre_signatures_to_create_in_advance = btree
.get("pre_signatures_to_create_in_advance")
.map(|x| x.parse::<u32>().expect("pre_signatures_to_create_in_advance must be a u32."))
.expect("Each element of the JSON object must specify a 'pre_signatures_to_create_in_advance'."));
.map(|x| x.parse::<u32>().expect("pre_signatures_to_create_in_advance must be a u32."));
if key_id.requires_pre_signatures() && pre_signatures_to_create_in_advance.is_none() {
panic!("JSON object must specify a 'pre_signatures_to_create_in_advance' for key {key_id}.");
}
if !key_id.requires_pre_signatures() && pre_signatures_to_create_in_advance.is_some() {
panic!("JSON object must not specify a 'pre_signatures_to_create_in_advance' for key {key_id}.");
}

let max_queue_size = Some(btree
.get("max_queue_size")
.map(|x| x.parse::<u32>().expect("max_queue_size must be a u32"))
.expect("Each element of the JSON object must specify a 'max_queue_size'."));

let key_config = Some(do_create_subnet::KeyConfig {
key_id,
key_id: Some(key_id),
pre_signatures_to_create_in_advance,
max_queue_size
});
Expand Down Expand Up @@ -394,7 +404,6 @@ mod tests {
},
{
"key_id": "vetkd:Bls12_381_G2:some_key_name_3",
"pre_signatures_to_create_in_advance": "0",
"max_queue_size": "154",
"subnet_id": "gxevo-lhkam-aaaaa-aaaap-yai"
}]"#
Expand Down Expand Up @@ -452,7 +461,7 @@ mod tests {
curve: VetKdCurve::Bls12_381_G2,
name: "some_key_name_3".to_string(),
})),
pre_signatures_to_create_in_advance: Some(0),
pre_signatures_to_create_in_advance: None,
max_queue_size: Some(154),
}),
subnet_id: Some(
Expand All @@ -470,4 +479,53 @@ mod tests {
},
);
}

#[test]
#[should_panic(
expected = "must specify a 'pre_signatures_to_create_in_advance' for key ecdsa:Secp256k1:some_key_name"
)]
fn should_panic_when_key_requiring_pre_signatures_is_missing_pre_signatures_to_create() {
let initial_chain_key_configs_to_request = r#"[{
"key_id": "ecdsa:Secp256k1:some_key_name",
"max_queue_size": "155",
"subnet_id": "gxevo-lhkam-aaaaa-aaaap-yai"
}]"#
.to_string();

let cmd = ProposeToCreateSubnetCmd {
initial_chain_key_configs_to_request: Some(initial_chain_key_configs_to_request),
replica_version_id: Some(ReplicaVersion::default()),
signature_request_timeout_ns: Some(111),
features: Some(SubnetFeatures::default()),
..empty_propose_to_create_subnet_cmd()
};

// This should panic when parsing the key config
let _ = cmd.new_payload();
}

#[test]
#[should_panic(
expected = "must not specify a 'pre_signatures_to_create_in_advance' for key vetkd:Bls12_381_G2:some_key_name"
)]
fn should_panic_when_key_not_requiring_pre_signatures_has_pre_signatures_to_create() {
let initial_chain_key_configs_to_request = r#"[{
"key_id": "vetkd:Bls12_381_G2:some_key_name",
"pre_signatures_to_create_in_advance": "99",
"max_queue_size": "155",
"subnet_id": "gxevo-lhkam-aaaaa-aaaap-yai"
}]"#
.to_string();

let cmd = ProposeToCreateSubnetCmd {
initial_chain_key_configs_to_request: Some(initial_chain_key_configs_to_request),
replica_version_id: Some(ReplicaVersion::default()),
signature_request_timeout_ns: Some(111),
features: Some(SubnetFeatures::default()),
..empty_propose_to_create_subnet_cmd()
};

// This should panic when parsing the key config
let _ = cmd.new_payload();
}
}
90 changes: 79 additions & 11 deletions rs/registry/admin/bin/recover_subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) struct ProposeToUpdateRecoveryCupCmd {
/// each with a subnet ID to request this key from.
///
/// key_id: Master public key ID formatted as "Scheme:AlgorithmID:KeyName".
/// pre_signatures_to_create_in_advance: Non-negative integer value.
/// pre_signatures_to_create_in_advance: Non-negative integer value. Omitted for keys without pre-signatures (e.g., vetKD).
/// max_queue_size: Integer value greater than or equal 1.
/// subnet_id: Principal ID of a subnet holding the requested key.
///
Expand All @@ -78,6 +78,11 @@ pub(crate) struct ProposeToUpdateRecoveryCupCmd {
/// "pre_signatures_to_create_in_advance": "98",
/// "max_queue_size": "154",
/// "subnet_id": "gxevo-lhkam-aaaaa-aaaap-yai"
/// },
/// {
/// "key_id": "vetkd:Bls12_381_G2:some_key_name_3",
/// "max_queue_size": "154",
/// "subnet_id": "gxevo-lhkam-aaaaa-aaaap-yai"
/// }
/// ]'
/// ```
Expand Down Expand Up @@ -136,26 +141,33 @@ fn parse_key_config_requests_option(
})
.expect("Each element of the JSON object must specify a 'subnet_id'."));

let key_id = Some(btree
let key_id = btree
.get("key_id")
.map(|key| {
key.parse::<MasterPublicKeyId>()
.unwrap_or_else(|_| panic!("Could not parse key_id: '{key}'"))
})
.expect("Each element of the JSON object must specify a 'key_id'."));

let pre_signatures_to_create_in_advance = Some(btree
.get("pre_signatures_to_create_in_advance")
.map(|x| x.parse::<u32>().expect("pre_signatures_to_create_in_advance must be a u32."))
.expect("Each element of the JSON object must specify a 'pre_signatures_to_create_in_advance'."));
.expect("Each element of the JSON object must specify a 'key_id'.");

let pre_signatures_to_create_in_advance =
btree.get("pre_signatures_to_create_in_advance").map(|x| {
x.parse::<u32>()
.expect("pre_signatures_to_create_in_advance must be a u32.")
});
if key_id.requires_pre_signatures() && pre_signatures_to_create_in_advance.is_none() {
panic!("JSON object must specify a 'pre_signatures_to_create_in_advance' for key {key_id}.");
}
if !key_id.requires_pre_signatures() && pre_signatures_to_create_in_advance.is_some() {
panic!("JSON object must not specify a 'pre_signatures_to_create_in_advance' for key {key_id}.");
}

let max_queue_size = Some(btree
.get("max_queue_size")
.map(|x| x.parse::<u32>().expect("max_queue_size must be a u32"))
.expect("Each element of the JSON object must specify a 'max_queue_size'."));

let key_config = Some(do_recover_subnet::KeyConfig {
key_id,
key_id: Some(key_id),
pre_signatures_to_create_in_advance,
max_queue_size
});
Expand Down Expand Up @@ -309,7 +321,6 @@ mod tests {
},
{
"key_id": "vetkd:Bls12_381_G2:some_key_name_3",
"pre_signatures_to_create_in_advance": "0",
"max_queue_size": "154",
"subnet_id": "gxevo-lhkam-aaaaa-aaaap-yai"
}]"#
Expand Down Expand Up @@ -364,7 +375,7 @@ mod tests {
curve: VetKdCurve::Bls12_381_G2,
name: "some_key_name_3".to_string(),
})),
pre_signatures_to_create_in_advance: Some(0),
pre_signatures_to_create_in_advance: None,
max_queue_size: Some(154),
}),
subnet_id: Some(
Expand All @@ -380,4 +391,61 @@ mod tests {
},
);
}

#[test]
#[should_panic(
expected = "must specify a 'pre_signatures_to_create_in_advance' for key ecdsa:Secp256k1:some_key_name"
)]
fn should_panic_when_key_requiring_pre_signatures_is_missing_pre_signatures_to_create() {
let subnet_id = SubnetId::from(PrincipalId::new_user_test_id(1));
let height = 107428000;
let time_ns = 1719241477392602354;
let state_hash =
"5d6601ac575f565b7c61d6bf5f9b25fa503bf7d756210a9a1fe8d8a32967f2e5".to_string();

let initial_chain_key_configs_to_request = r#"[{
"key_id": "ecdsa:Secp256k1:some_key_name",
"max_queue_size": "155",
"subnet_id": "gxevo-lhkam-aaaaa-aaaap-yai"
}]"#
.to_string();

let cmd = ProposeToUpdateRecoveryCupCmd {
initial_chain_key_configs_to_request: Some(initial_chain_key_configs_to_request),
signature_request_timeout_ns: Some(111),
..empty_propose_to_recover_subnet_cmd(subnet_id, height, time_ns, state_hash)
};

// This should panic when parsing the key config
let _ = cmd.new_payload_for_subnet(subnet_id);
}

#[test]
#[should_panic(
expected = "must not specify a 'pre_signatures_to_create_in_advance' for key vetkd:Bls12_381_G2:some_key_name"
)]
fn should_panic_when_key_not_requiring_pre_signatures_has_pre_signatures_to_create() {
let subnet_id = SubnetId::from(PrincipalId::new_user_test_id(1));
let height = 107428000;
let time_ns = 1719241477392602354;
let state_hash =
"5d6601ac575f565b7c61d6bf5f9b25fa503bf7d756210a9a1fe8d8a32967f2e5".to_string();

let initial_chain_key_configs_to_request = r#"[{
"key_id": "vetkd:Bls12_381_G2:some_key_name",
"pre_signatures_to_create_in_advance": "99",
"max_queue_size": "155",
"subnet_id": "gxevo-lhkam-aaaaa-aaaap-yai"
}]"#
.to_string();

let cmd = ProposeToUpdateRecoveryCupCmd {
initial_chain_key_configs_to_request: Some(initial_chain_key_configs_to_request),
signature_request_timeout_ns: Some(111),
..empty_propose_to_recover_subnet_cmd(subnet_id, height, time_ns, state_hash)
};

// This should panic when parsing the key config
let _ = cmd.new_payload_for_subnet(subnet_id);
}
}
Loading
Loading