Staking a Validator using the PowerStaking SDK
This tutorial goes through the step by step process of staking a validator via the PowerStaking Protocol using the PowerStaking-sdk
.
Please note that the user will still have to run their own validator client after the deposit has been registered.
The various steps required to stake a validator are:
Generating BLS credentials
Registering Validator's initials
Signing the data payload
Authenticating the BLS keystore
Registering the validator
SDK does heavy lifting and does all these steps in a single function. This function is deposit
function. This tutorial shows both the ways of staking with the SDK. The later section shows the inner workings of the deposit
function.
Here's how you can use the deposit
function:
Copy
The deposit
function can be used in multiple ways:
Via a dapp. While using the dapp, user can provide sign the transaction without providing the private key. The dapp when connected to a wallet would redirect user to a wallet pop up where they can sign the transaction
Via a representative. A user can assign another user to be representative and perform tasks on their behalf. For example, Alice has 32 ETH but isn't very familiar with the staking process. Whereas Bob has multiple node running services and can quickly spin up an Ethereum node. So, Alice can make Bob her representative while keeping the 32 ETH still with herself. Bob generates validator credentials and calls the
deposit
function. For this, Bob will have to provide an optional parameter i.e.user
(Alice's Ethereum execution layer address will be theuser
parameter). If all parameters are correct,deposit
function will return Bob with anencoded data
. Alice can easily sign and send a transaction with thisencoded data
to deposit her 32 ETH. There are many more ways to carry out this transaction. Alice and Bob can create an escrow which holds 32 ETH or maybe Alice trusts Bob with her 32 ETH and can directly make the deposit.
Following section goes through the internals of the deposit
function and how someone can stake a validator without calling the SDK's deposit
function.
After creating an empty JS project, follow this tutorial.
Environment Variables
In the .env
file, the following variables should be added
Copy
Installing Dependencies
This project requires the PowerStaking-sdk
, ethers
and dotenv
library.
Copy
Please note that the PowerStaking-sdk is at least version 3.0.0
Using the PowerStaking SDK
The first step is to get the provider
and signer
instance from the Private Key of the associated Execution Layer Account
.
Copy
Now that the base script is ready, use the PowerStaking-sdk
. It is as easy as instantiating the SDK. This is how the main
function should look:
Copy
Generating BLS Credentials
It is necessary to have a BLS credential to create a validator, because this is what indentifies the validator on Consensus layer. The BLS keystore must be protected by a password.
Please do not copy the password from this project and try to keep it as random and secure as possible.
Copy
Now, to generate the BLS credentials use the
generateCredentials
function from theutils
class of the SDK.Copy
Once, the credential is generated, it can be written into a
JSON
file.Copy
Register Validator's Initials
Before moving to this step, define values from the credentials.json
file which will be used more often.
Copy
The PowerStaking Protocol keeps note of all the validators registered through it. For this reason, it needs a validator's Execution Layer Address
, BLS Public Key
and BLS Signature
.
To register a validator with the newly created credentials, call the registerValidatorInitials
function of the SDK.
Copy
Since, the script does everything, it is important to make sure the validator's initials are registered. This transaction might take some time. Hence, it will be good to use a wait
function.
Copy
Signing data payload
In order to Authenticate the BLS keystore, it is necessary to sign the BLS Public Key and the BLS Signature with the associated Execution layer account using it's private key. This can be easliy achieved by calling the getPersonalSignInitialsByPrivateKey
function from the utils
class of the SDK. By signing the payload, the user confirms that they are the owner or the representative of the BLS credentials. If the user is indeed the rightful owner/representative of the credentials, the function returns a payload.
Copy
BLS Authentication
Now that the data payload is signed and ready to be authenticated, call the BLSAuthentication
function of the SDK. BLS Authentication is necessary as it validates that the keystore and the deposit data are correct and can be used to spin up a validator. This is a pre-deposit step which returns an authentication package required to stake the 32 ETH for a validator.
Copy
Registering Validator with 32 ETH
This is the crucial step which makes a deposit of 32 ETH
to the Ethereum Foundation's Deposit Contract.
Please be very cautious with this step as it requires ETH. Make sure you are connected to the correct network and you really want to stake a vlaidator. Once done, the 32 ETH are sent to Ethereum Foundation's Deposit Contract depending on the network you are on (
goerli
ormainnet
). TheregisterValidator
function requires as much as theExecution Layer Address
and theBLS Authentication Report
to send the 32 ETH for the validator.Copy
The Script
All the above steps have been compiled into the final script as shown below.
Last updated