diff --git a/Cargo.lock b/Cargo.lock index 92e8bcb6e4..9617092845 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2063,9 +2063,9 @@ dependencies = [ [[package]] name = "data-encoding" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" [[package]] name = "data-encoding-macro" @@ -6323,6 +6323,14 @@ dependencies = [ "elliptic-curve", ] +[[package]] +name = "principal_generator" +version = "0.1.0" +dependencies = [ + "data-encoding", + "ic_principal", +] + [[package]] name = "proc-macro-error" version = "1.0.4" diff --git a/Cargo.toml b/Cargo.toml index 53a91b97f2..7f71e9b102 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -156,6 +156,7 @@ members = [ "backend/notification_pusher/aws", "backend/notification_pusher/cli", "backend/notification_pusher/core", + "backend/tools/principal_generator", "backend/tools/translation_tool", ] resolver = "2" @@ -173,6 +174,7 @@ canister_sig_util = { git = "https://github.com/dfinity/internet-identity", rev ciborium = "0.2.2" clap = "4.5.4" ct-codecs = "1.1.1" +data-encoding = "2.6.0" dataurl = "0.1.2" dirs = "5.0.1" dotenv = "0.15.0" @@ -193,6 +195,7 @@ ic-cdk-timers = "0.8.0" ic-certificate-verification = "2.4.0" ic-certification = "2.5.0" ic-ledger-types = "0.11.0" +ic_principal = "0.1.1" ic-stable-structures = "0.6.4" ic-transport-types = "0.35.0" ic-utils = "0.35.0" diff --git a/backend/tools/principal_generator/Cargo.toml b/backend/tools/principal_generator/Cargo.toml new file mode 100644 index 0000000000..1a2187beb0 --- /dev/null +++ b/backend/tools/principal_generator/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "principal_generator" +version = "0.1.0" +edition = "2021" + +[dependencies] +data-encoding = { workspace = true } +ic_principal = { workspace = true } diff --git a/backend/tools/principal_generator/src/main.rs b/backend/tools/principal_generator/src/main.rs new file mode 100644 index 0000000000..d6f053dbd3 --- /dev/null +++ b/backend/tools/principal_generator/src/main.rs @@ -0,0 +1,41 @@ +use ic_principal::Principal; + +fn main() { + let args = std::env::args().collect::>(); + let Some(target_name) = args.get(1).cloned() else { + panic!("No target name provided"); + }; + + run(target_name); +} + +fn run(target_name: String) -> Option { + let mut best = None; + let mut fixed_bytes = [0; 10]; + fixed_bytes[9] = 1; + + let input: String = target_name.chars().filter(|c| *c != '-').take(11).collect(); + let prefix = format!("aaaaa{input}"); + let prefix_base32 = &data_encoding::BASE32_NOPAD + .decode(prefix.to_ascii_uppercase().as_bytes()) + .unwrap()[4..]; + fixed_bytes[..prefix_base32.len()].copy_from_slice(prefix_base32); + + for inner in 0u32..1 << 24 { + let mut canister_id_bytes = fixed_bytes; + canister_id_bytes[6..9].copy_from_slice(&inner.to_be_bytes()[1..]); + + let canister_id = Principal::from_slice(&canister_id_bytes); + let canister_id_string = canister_id.to_string(); + + if canister_id_string.as_str()[6..6 + target_name.len()] == target_name + && canister_id_string.ends_with("cai") + && best.as_ref().map_or(true, |s| canister_id_string > *s) + { + println!("Principal: {canister_id_string}. Bytes: {canister_id_bytes:?}"); + best = Some(canister_id_string); + } + } + + best +}