Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a non-testing AccountBuilder #948

Closed
PhilippGackstatter opened this issue Nov 1, 2024 · 1 comment
Closed

Add a non-testing AccountBuilder #948

PhilippGackstatter opened this issue Nov 1, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@PhilippGackstatter
Copy link
Contributor

PhilippGackstatter commented Nov 1, 2024

What should be done?

Add an AccountBuilder type that allows for easy and safe account creation.

As an example, the interface might look like this, copied from #766 (comment):

let (account, seed) = AccountBuilder::new(init_seed)
    .with_storage_mode(AccountStorageMode::Public)
    .with_basic_wallet()
    .with_component(RpoFalcon512::new(pub_key))
    .with_component(build_my_custom_component())
    .build();

And faucets (or any other account) could be created using the same interface:

let (account, seed) = AccountBuilder::new(init_seed)
    .with_type(AccountType::FungibleFaucet)
    .with_storage_mode(AccountStorageMode::Public)
    .with_component(BasicFungibleFaucet::new(symbol, 8, Felt::new(2 << 63))),
    .with_component(RpoFalcon512::new(pub_key))
    .build();

How should it be done?

We can consider moving the existing AccountBuilder behind the testing feature out and make it part of the non-testing interface. We can also check whether the inner AccountIdBuilder is truly necessary or if it can be replaced with something simpler.

In particular we want to be able to replace this pattern:

let account_type = AccountType::FungibleFaucet;
let (account_code, account_storage) = Account::initialize_from_components(
    account_type,
    &[
        RpoFalcon512::new(pub_key).into(),
        BasicFungibleFaucet::new(symbol, decimals, max_supply)?.into(),
    ],
)?;

let account_seed = AccountId::get_account_seed(
    init_seed,
    account_type,
    account_storage_mode,
    account_code.commitment(),
    account_storage.commitment(),
)?;

let account = Account::new(account_seed, account_code, account_storage)?;

with something like this:

let (account, seed) = AccountBuilder::new(MyRng::new())
        .account_type(AccountType::FungibleFaucet)
        .storage_mode(AccountStorageMode::Private)
        .add_component(RpoFalcon512::new(public_key))
        .add_component(BasicFungibleFaucet::new(symbol, decimals, max_supply)?)
        .build()?;

This is actually already working code using the existing builder.

Open Questions

  • Related to the AccountIdBuilder question we should decide whether to make the builder generic over Rng which is used for the AccountId build process as a source of randomness, or if an init_seed: [u8; 32] is sufficient (as in AccountId::get_account_seed. We might want to just pick a simple solution here for now, as Account ID structure updates #140 might change how we build AccountIds anyway, and this would be the proper time to revisit how to easily construct IDs.

When is this task done?

This task is done when we can build an Account using components in non-testing code with an AccountBuilder type.

We should ensure that, with the builder, it is not possible for users to build an Account using AccountCode and AccountStorage that does not match the type of the account. For example, if an AccountStorage was created from faucet components and thus has a reserved slot 0, then we should not be able to build a regular account from that storage. Note that this is possible using the above pattern that we want to replace since multiple calls are needed to build the full account.

Additional context

No response

@PhilippGackstatter
Copy link
Contributor Author

Closed by #952.

@bobbinth bobbinth added this to the v0.6 milestone Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

No branches or pull requests

2 participants