3

I have a struct which I want to store on the blockchain:

pub struct Item {
  pub id: String,
  pub link: String,
  pub user_address: Pubkey,
}

Now I want to generate an id for it in the Solana program. If I add the UUID package and try to call Uuid::new_v4(), I get the following error.

error: target is not supported, for more information see: https://docs.rs/getrandom/#unsupported-targets

Question: How to generated a UUID in a Solana program? Or alternatively, how to generate a unique identifier of any sort?

1 Answer 1

3

Without much context, your best bet may be to use a public key as the id?

let id = Keypair::new().pubkey();

Though I can imagine that the Item struct is stored in an account. So that accounts' address can already act as the unique id.

1
  • 1
    using the account's address as the id is The Way :tm:
    – trent.sol
    Commented Aug 17, 2022 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.