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

[Feature]: Add Function That Creates Account from a Private Key #27

Open
1 task done
0xNeshi opened this issue Jan 23, 2025 · 0 comments
Open
1 task done

[Feature]: Add Function That Creates Account from a Private Key #27

0xNeshi opened this issue Jan 23, 2025 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@0xNeshi
Copy link
Collaborator

0xNeshi commented Jan 23, 2025

What is the feature you would like to see?

Account

It would be ergonomic to allow devs to pass an arbitrary string (or even number) as a "private key" when creating an Account, generating the required Address from the passed value.
This would be similar to existing conventions in Solidity Foundry & Starknet Foundry.

Example implementation:

    // we could even make this generic if you have any suggestions, though difficult to do with `const`.
    // we can use a different name if you have a better idea 
    #[must_use]
    pub const fn new(priv_key: &str) -> Self {
        let hash = Keccak256::new().update(priv_key.as_bytes()).finalize();
        Self { address: Self::bytes_to_address(&hash) }
    }

    const fn bytes_to_address(bytes: &[u8]) -> Address {
        let mut addr_bytes = [0u8; 20];
        let mut i = 0;
        while i < 20 {
            addr_bytes[i] = bytes[i];
            i += 1;
        }
        Address::new(addr_bytes)
    }

    // Later in tests...

    #[motsu::test]
    fn some_test() {
        // before
        let alice = Account::new_at(address!(
            "A11CEacF9aa32246d767FCCD72e02d6bCbcC375d"
        ));
        let bob = Account::new_at(address!(
            "B0B0cB49ec2e96DF5F5fFB081acaE66A2cBBc2e2"
        ));
        // after
        let alice = Account::new("alice");
        let bob = Account::new("bob");
        // ...
    }

We also make it clear that we prefer devs manually set addresses in order to avoid potential collisions (although the chances are fairly low).

This also ties naturally into how real EOAs are instantiated (with private keys), implying that priv_key should be unique, and if there are collisions, it's the dev's fault.

If devs still prefer to get random address, they can use ::random().

This strikes a perfect balance between safety and devX.

Original message: #14 (comment)

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines
@0xNeshi 0xNeshi added enhancement New feature or request good first issue Good for newcomers labels Jan 23, 2025
@0xNeshi 0xNeshi changed the title [Feature]: Add fn new(priv_key: &str) -> Self to Account [Feature]: Add Function That Creates Account from a Private Key Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant