# Generating custom principals/uuids in Rust

**URL:** <https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294>\
**Category:** Developers\
**Created:** [March 25, 2021, 11:05pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294 "2021-03-25T23:05:14Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [March 25, 2021, 11:05pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/1 "2021-03-25T23:05:14Z")

</div>

Hey, I’ve looked through quite a few different places, and I’m not quite sure on the best way to generate custom principals or uuids with Rust.

How would I go about doing this? Is it possible to create custom principals? I think it would just be nice to be able to generate uuids that have the same format as IC principals.

---

<div class="post-metadata">

**Author:** ![hansl](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/hansl/32/134_2.png) [@hansl](https://forum.dfinity.org/u/hansl)\
**Post date:** [March 25, 2021, 11:15pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/2 "2021-03-25T23:15:00Z")

</div>

Custom or random? Random ones can be generated from a random private/public key pair.

Principals follow a pattern and don’t make sense as a random blob; canister IDs are created by the system and cannot sign messages, only derived Principal IDs (derived from a private key) can sign messages.

So the only “custom” prinicpals I could see would be canister IDs, which you can use the text format to encode some. But the goal of Principals was always as an opaque blob that doesn’t make sense as a random value.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [March 25, 2021, 11:16pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/3 "2021-03-25T23:16:45Z")

</div>

Thank you.

I want to be able to create UUIDs from within a canister. What is the best way to go about that? I think it would be nice for the UUID generated to have the same format as a principal, even if it doesn’t have the special features of a principal. All of this in Rust

---

<div class="post-metadata">

**Author:** ![hansl](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/hansl/32/134_2.png) [@hansl](https://forum.dfinity.org/u/hansl)\
**Post date:** [March 25, 2021, 11:42pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/4 "2021-03-25T23:42:02Z")

</div>

You can use a PRNG, and you could seed it with the random beacon if you really want random stuff (otherwise timestamp would be enough?). The beacon API is here: [The Internet Computer Interface Specification :: Internet Computer](https://sdk.dfinity.org/docs/interface-spec/index.html#ic-raw_rand) (it’s `raw_rand : () -> (blob)`).

Creating principals from a UUID wouldn’t make much sense IMO.

Just to back up a little, what is the problem you’re trying to solve by getting Principals in this manner? How are the principals going to be used?

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [March 25, 2021, 11:47pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/5 "2021-03-25T23:47:50Z")

</div>

I’m just trying to generate UUIDs for database entries is all, and I would prefer to have UUIDs instead of incremental integers

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [March 25, 2021, 11:50pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/6 "2021-03-25T23:50:43Z")

</div>

How do I access `raw_rand` from a Rust canister? I don’t see it in the ic-cdk packages as far as I can tell. Am I supposed to treat the management canister as a canister and call it that way? Could you provide an example?

---

<div class="post-metadata">

**Author:** ![hansl](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/hansl/32/134_2.png) [@hansl](https://forum.dfinity.org/u/hansl)\
**Post date:** [March 26, 2021, 12:02am UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/7 "2021-03-26T00:02:51Z")

</div>

> [@lastmjs](#):
>
> Am I supposed to treat the management canister as a canister and call it that way?

Yes. It’s a bit low level currently but it should work as any inter canister calls.

```rust
let (bytes,): (Vec<u8>,) = ic_cdk::api::call(Principal.management_canister(), "raw_rand", ()).await?;

```

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [March 26, 2021, 12:06am UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/8 "2021-03-26T00:06:10Z")

</div>

Thank you! Are inter-canister calls documented for Rust at all? There doesn’t seem to be very much Rust documentation at all, unless I am not looking in the right places.

---

<div class="post-metadata">

**Author:** ![hansl](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/hansl/32/134_2.png) [@hansl](https://forum.dfinity.org/u/hansl)\
**Post date:** [March 26, 2021, 12:08am UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/9 "2021-03-26T00:08:49Z")

</div>

The CDK is a bit low level for the time being, just mapping on top of the System API exports with a bit of functionality for making calls async. Sorry about that, it’s still early in the development 🙂 We will get better over time.

---

<div class="post-metadata">

**Author:** ![rckprtr](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rckprtr/32/809_2.png) [@rckprtr](https://forum.dfinity.org/u/rckprtr)\
**Post date:** [March 26, 2021, 2:23am UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/10 "2021-03-26T02:23:00Z")

</div>

Very cool, I was looking for something similar for UUID’s, I ended up going with a global index.

---

<div class="post-metadata">

**Author:** ![Ori](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Ori](https://forum.dfinity.org/u/Ori)\
**Post date:** [March 26, 2021, 10:53am UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/11 "2021-03-26T10:53:00Z")

</div>

I did too for now. I was wondering about the cost of using raw\_rand for this?

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [April 1, 2021, 8:39pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/12 "2021-04-01T20:39:36Z")

</div>

I was able to get this working from a canister by creating a sha256 hash of the random bytes, which is awesome!

I am running into an issue with the IC’s async/await implementation though, I think. I’m using `async-graphql`, and from within an async mutation resolver, I’m getting some crazy errors about threading. I’m sure I’ll learn a lot more about this in the coming days and weeks, but can you give some insight into the async/await implementation that the IC has chosen? Here’s the issue I’ve opened in the `async-graphql` repo: [Having trouble using std::rc::Rc values within a Mutation object. · Issue #110 · async-graphql/async-graphql · GitHub](https://github.com/async-graphql/async-graphql/issues/110)

I’m fearing that the IC’s async/await implementation might not be compatible with `async-graphql`, perhaps because the IC’s async/await has made an assumption that it will only operate in a single-threaded environment.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [April 1, 2021, 9:52pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/13 "2021-04-01T21:52:56Z")

</div>

Also, would it be possible to make the raw\_rand function synchronous? For example, getting the current time is a simple synchronous call to ic\_cdk::api::time, so nice and simple. ic\_cdk::api::raw\_rand would be really nice.

---

<div class="post-metadata">

**Author:** ![hansl](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/hansl/32/134_2.png) [@hansl](https://forum.dfinity.org/u/hansl)\
**Post date:** [April 1, 2021, 10:02pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/14 "2021-04-01T22:02:32Z")

</div>

The random value returned is guaranteed to be truly random (using a physical random beacon), so it cannot be synchronous. If you don’t need that kind of security, I recommend to use a PRNG seeded with either the beacon or time.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [April 2, 2021, 12:21am UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/15 "2021-04-02T00:21:30Z")

</div>

@hansl Sounds good on the sync, thank you. My bigger problem from a couple comments back remains…and I’m afraid it’s going to become a very large problem once I need to do inter-canister calls

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [May 26, 2022, 9:00pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/16 "2022-05-26T21:00:02Z")

</div>

> [@rckprtr](#):
>
> Very cool, I was looking for something similar for UUID’s, I ended up going with a global index.

@rckprtr how does what you’re doing compare with what I wrote here?

> <https://github.com/roman-kashitsyn/mmapped.blog/issues/8>
>
> Hi @roman-kashitsyn!
> 
> I was re-reading \[Effective Rust Canisters\](https://mmap…ped.blog/posts/01-effective-rust-canisters.html) and I wanted to say thank you for writing it. It’s a great resource.
> 
> I had some questions/suggestions, particularly around generating sequential user numbers that I hoped you could help me with.
> 
> Thank you in advance!
> 
> \*\*\*
> 
> Here, an example is given that uses \`Cell\` for \`NEXT\_USER\_ID\`
> 
> https://github.com/roman-kashitsyn/mmapped.blog/blob/7eaa01608b73cde2331a84f0ce645da020b05876/posts/01-effective-rust-canisters.html.pm#L44
> 
> But later it is explained that variables using \`Cell\` “flexible” and are discarded on upgrades.
> 
> https://github.com/roman-kashitsyn/mmapped.blog/blob/7eaa01608b73cde2331a84f0ce645da020b05876/posts/01-effective-rust-canisters.html.pm#L159
> 
> https://github.com/roman-kashitsyn/mmapped.blog/blob/7eaa01608b73cde2331a84f0ce645da020b05876/posts/01-effective-rust-canisters.html.pm#L170
> 
> Would it make more sense to use \`RefCell\` in the original example?
> 
> \*\*\*
> 
> There are some examples of functions that take \`UserId\`:
> 
> https://github.com/roman-kashitsyn/mmapped.blog/blob/7eaa01608b73cde2331a84f0ce645da020b05876/posts/01-effective-rust-canisters.html.pm#L217
> 
> https://github.com/roman-kashitsyn/mmapped.blog/blob/7eaa01608b73cde2331a84f0ce645da020b05876/posts/01-effective-rust-canisters.html.pm#L250
> 
> But there isn’t an example of how one might properly manage the creation of new user IDs (assuming they are incremental, like in \`NEXT\_USER\_ID\`)
> 
> Could you provide an example of that?
> 
> Are there any pitfalls with something like the following?
> 
> \`\`\`rust
> thread\_local! {
> static NEXT\_USER\_ID: RefCell\<u128\> = RefCell::new(0);
> }
> 
> pub struct User {
> pub id: UserId,
> pub name: Name,
> }
> 
> impl User {
> pub fn new(name: Name) -\> Self {
> NEXT\_USER\_ID.with(|next\_user\_id| {
> let user = Self {
> id: UserId(\*next\_user\_id.borrow()),
> name,
> };
> \*next\_user\_id.borrow\_mut() += 1;
> user
> })
> }
> }
> 
> pub struct UserId(pub u128);
> \`\`\`
> 
> What do you recommend?
> 
> \*\*\*
> 
> There’s a link to Internet Identity:
> 
> https://github.com/roman-kashitsyn/mmapped.blog/blob/7eaa01608b73cde2331a84f0ce645da020b05876/posts/01-effective-rust-canisters.html.pm#L688
> 
> Internet Identity also has a sequential user number:
> 
> https://github.com/dfinity/internet-identity/blob/b434acea3831e4317cf20c26488f1d31570dd35b/src/internet\_identity/src/storage.rs#L128-L140
> 
> However, it uses stable memory which is much more involved than using a stable variable. Could you explain why that is?

---

<div class="post-metadata">

**Author:** ![rckprtr](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rckprtr/32/809_2.png) [@rckprtr](https://forum.dfinity.org/u/rckprtr)\
**Post date:** [May 26, 2022, 10:44pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/17 "2022-05-26T22:44:23Z")

</div>

Hey,

So we are probably going to use this method

> <https://github.com/dfinity/nns-dapp/blob/main/rs/src/state.rs>

We have not had a chance to upgrade to `!thread_local` yet as we use the old paradigm 200+ times in our repo and figure it would be better to do a complete refactor instead of a quick fix.

Rick

---

<div class="post-metadata">

**Author:** ![AliSci](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alisci/32/33799_2.png) [@AliSci](https://forum.dfinity.org/u/AliSci)\
**Post date:** [January 1, 2023, 8:59am UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/18 "2023-01-01T08:59:16Z")

</div>

Can you give me example about using PRNG?  
is it like

```plaintext
let id = rand::prng().gen(0..16)

```

?  
Because: when i tried `rand::prng` it `cannot find function `prng`in crate`rand` [E0425]`

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [January 1, 2023, 10:57pm UTC](https://forum.dfinity.org/t/generating-custom-principals-uuids-in-rust/2294/19 "2023-01-01T22:57:12Z")

</div>

I think you’d need to do something like this:

> <https://github.com/codebase-labs/ic-auth-tokens/blob/76113cbaa8b788b1989a9d5ef0868cdb12ee46af/crates/ic-auth-tokens/src/lib.rs#L75-L95>
