K2 Native Delegation SDK Tutorial
This guide outlines the steps for registering as a validator on the K2 Lending contract and performing native delegation. To complete the delegation process, you will need to first register as a validator on the Ethereum network and then execute the nodeOperatorDeposit function on the K2 Lending smart contract.
Create Validator on Ethereum
Creating a Validator on Ethereum requires 32 ETH for activating it for Consensus Layer Validation duty, there are currently few ways you could become a validator either with your own ETH or from a staking pool.
Once you have a validator on consensus layer, you are able join K2 staking via native delegation by proving your ownership of your validator signing with its signing Key on PoN proposer registry. This process will replicate the underlying validator consensus layer active state and register it with an ECDSA address on execution layer along with a designated fee recipient supplied by the registering user for receiving ETH.
Registering as a Validator on PON (Proof of Neutrality) Network
Before performing native delegation on the K2 Lending contract, ensure that your validator is registered and activated on the PON network. This can be done through the PON SDK.
PON SDK
Follow the PON SDK tutorial to register your validator with the PON network.
Obtaining BLS and ECDSA Signatures
To perform native delegation on the K2 Lending contract, you need to prove ownership of both BLS and ECDSA signing keys. Here’s how to obtain these signatures:
Install the required packages by running:
npm i @blockswaplab/pon-sdk ethers dotenv
Prepare the following parameters:
- Private key with a small amount of Goerli ETH (it must match the ECDSA public key of the representative address that is allowed to manage the PoN proposer registry login).
- Ethereum provider URL.
- Keystore file of your registered and active validator.
- Password for the keystore file.
Unlock the Keystore File:
Use the PON SDK to unlock the keystore file and obtain the BLS public key and private key.
const PoNSdk = require("@blockswaplab/pon-sdk");
const fs = require("fs");
require("dotenv").config();
const ethers = require("ethers");
const privateKey1 = process.env.PRIVATE_KEY;
const providerUrl = process.env.PROVIDER_URL;
const provider = new ethers.JsonRpcProvider(providerUrl);
const wallet = new ethers.Wallet(privateKey1, provider);
async function unlockKeystore() {
const pSDK = new PoNSdk(wallet);
const keystore = JSON.parse(fs.readFileSync("./path/to/keystore", "utf8"));
const password = "Password";
const { publicKey, privateKey } =
await pSDK.proposerRegistry.unlockBLSKeystore(keystore, password);
}
Prepare Registration:
Run the prepareRegistration function to obtain the required BLS and ECDSA signatures (proof of ownership to prevent spoofing).
const sig = await pSDK.proposerRegistry.prepareRegistration(
`0x${publicKey}`,
wallet.address,
wallet.address,
Buffer.from(privateKey, "hex")
);
console.log("Required signatures: ", sig.ecdsaSig);
You will receive the BLS and ECDSA signatures in the following format:
ecdsaSig: {
v: 27,
r: '0x',
s: '0x',
originalData: {
signature: '0x',
blsKey: '0x',
payoutRecipient: '0x',
representativeAddress: '0x'
}
}
Check these values to ensure they are correct before proceeding to the K2 native delegation.
Performing Native Delegation on K2 Lending
To complete the native delegation on the K2 Lending contract, follow these steps:
Install the K2 SDK : Install the K2 SDK by running npm i @blockswaplab/k2-sdk
.
Create Ethereum Signer: Create an Ethereum signer using your private key and Ethereum provider.
const provider = new ethers.providers.JsonRpcProvider(providerUrl); const signer = new ethers.Wallet(PRIV_KEY, provider);
Run nodeOperatorDeposit Function: Use the K2 SDK to call the nodeOperatorDeposit function with the required parameters.
const { K2 } = require("@blockswaplab/k2-sdk");
const ethers = require("ethers");
const PRIV_KEY = process.env.PRIVATE_KEY;
const providerUrl = process.env.PROVIDER_URL;
const provider = new ethers.providers.JsonRpcProvider(providerUrl);
const signer = new ethers.Wallet(PRIV_KEY, provider);
async function registerProposer() {
const sdk = new K2(signer);
const blsPubkey = "YourBlsKey";
const payoutRecipient = signer.address;
const ecdsaSignature = {
v: "27",
r: "0x",
s: "0x",
};
const blsSignature = "0x";
const tx = await sdk.kSquaredLending.nodeOperatorDeposit(
blsPubkey,
payoutRecipient,
blsSignature,
ecdsaSignature
);
console.log("tx.hash", tx.hash, tx);
}
registerProposer();
Note: as mentioned before, signer.address
must match the representative address in the PoN proposer registry login.
Confirming Native Delegation Success
To confirm if you have been successful in native delegation, you can check this by calling the “blsPublicKeyToNodeOperator” function of the K2 Lending contract. This function takes the BLS key as input and returns the address of the node operator.
This function will return the address of the node operator associated with the provided BLS key. Ensure that the returned address matches your expectations to verify the success of the native delegation process.
Congratulations on completing the K2 Native Delegation process!