Skip to content

Commit

Permalink
Add principal_generator (#6237)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Aug 14, 2024
1 parent ecd2fef commit e32d5fa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions backend/tools/principal_generator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "principal_generator"
version = "0.1.0"
edition = "2021"

[dependencies]
data-encoding = { workspace = true }
ic_principal = { workspace = true }
41 changes: 41 additions & 0 deletions backend/tools/principal_generator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use ic_principal::Principal;

fn main() {
let args = std::env::args().collect::<Vec<_>>();
let Some(target_name) = args.get(1).cloned() else {
panic!("No target name provided");
};

run(target_name);
}

fn run(target_name: String) -> Option<String> {
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
}

0 comments on commit e32d5fa

Please sign in to comment.