You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]pubconstfnnew(priv_key:&str) -> Self{let hash = Keccak256::new().update(priv_key.as_bytes()).finalize();Self{address:Self::bytes_to_address(&hash)}}constfnbytes_to_address(bytes:&[u8]) -> Address{letmut addr_bytes = [0u8;20];letmut i = 0;while i < 20{
addr_bytes[i] = bytes[i];
i += 1;}Address::new(addr_bytes)}// Later in tests...#[motsu::test]fnsome_test(){// beforelet alice = Account::new_at(address!("A11CEacF9aa32246d767FCCD72e02d6bCbcC375d"));let bob = Account::new_at(address!("B0B0cB49ec2e96DF5F5fFB081acaE66A2cBBc2e2"));// afterlet 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.
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
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 requiredAddress
from the passed value.This would be similar to existing conventions in Solidity Foundry & Starknet Foundry.
Example implementation:
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
The text was updated successfully, but these errors were encountered: