How is an Account be Created
We can create an account by wallet like MetaMask or StarMask, but I’m curious about how an account is created on the blockchain system.
As a wallet has been embedded in the starcoin node, we can use it to create an account as follow:
As above we can see our account has been created, and the address is: 0x2f1aeb63bd30d8eb841d6a941c5d6df3
. We’ll find some files are created, if we check our local directory at ~/.starcoin/dev
:
We’ll meet them later, for now, let’s follow the source code to find how an account is created.
Two Account Provider Strategies⌗
From the code here we can see:
- If there is a
account_dir
configured, then it should beAccountProviderStrategy::Local
- Otherwise
AccountProviderStrategy::RPC
is used.
As we specify the -d ~/.starcoin
to run above command to create an account, so I think the account_dir
could be ~/.starcoin/dev/account_vaults
.
Just simply add a print statement in account/provider/src/provider/mod.rs, and remove the directory, then build & run it again:
Emmm, things don’t always happen as your thoughts, aren’t they? AccountProviderStrategy
only will be used if we run command and pass --local-account-dir
option:
There it is.
AccountManager
& AccountStorage
⌗
I’m tracing the invocation path, and find that no matter which strategy is used, then eventually, the AccountManager is used to do the actually job. And it mainly relys on AccountStorage.
Now let’s check the defination of AccountManager.create_account
:
It’s very simple, just generate a private key, and use it’s public key to derive an address, that’s how your account address comes out.
Two more invocations are involved: generate_private_key
and AccountManager.save_account
. Let’s check the first one:
generate_private_key
⌗
Emmm, we are using Ed25519
, as konwn as EdDSA(Edwards-curve Digital Signature Algorithm).
Then how an address is derived?⌗
As we can see, the address is derived by a public key, which defined in starcoin_vm_types::transaction::authenticator::AccountPublicKey.derived_address.
The main function of this invocation is use starcoin_vm_types::transaction::authenticator::AuthenticationKey::from_preimage create and AuthenticationKey
can call
derived_address that bind to it:
How to avoid collision of address?⌗
TODO
How is an Account be Saved?⌗
It’s mainly implemented in AccountManager.save_account:
- Check existence.
- Verify private key and password.
- Invoke Account::create to create the account: store it to corresponding place, and change the corresponding settings.
- Add to store.
- Set it to default, if it’s the first address.
Conclusion⌗
So, an account has been created. There is no magic happens, which means no on chain operations are involved. Just generates a private key and public key pair, and derives an address.
The key pair is just a way that proof you own the address. So on the blockchain, you can send coins to any address, if nobody can proof that his owns that address, then the coins some how is lost forever.